Add a simple benchmark for scaling images.
[dcpomatic.git] / benchmark / scaling.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "lib/compose.hpp"
23 #include "lib/image.h"
24 #include "lib/timer.h"
25
26
27 using boost::shared_ptr;
28
29
30 #define ITERATIONS 500
31
32
33 static
34 shared_ptr<Image>
35 dummy_image(dcp::Size size)
36 {
37         shared_ptr<Image> image(new Image(AV_PIX_FMT_RGB24, size, true));
38         int v = 0;
39         for (int y = 0; y < size.height; ++y) {
40                 for (int c = 0; c < image->planes(); ++c) {
41                         uint8_t* p = image->data()[c] + y * image->stride()[c];
42                         int const line_size = image->line_size()[c];
43                         for (int x = 0; x < line_size; ++x) {
44                                 *p++ = v++ % 256;
45                         }
46                 }
47         }
48
49         return image;
50 }
51
52
53 static
54 void
55 test (dcp::Size from, dcp::Size to, bool fast)
56 {
57         shared_ptr<Image> image = dummy_image(from);
58         PeriodTimer pt (String::compose("%1:%2 -> %3:%4 %5", from.width, from.height, to.width, to.height, fast ? "fast" : "slow"));
59         for (int i = 0; i < ITERATIONS; ++i) {
60                 image->scale (to, dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB48, true, fast);
61         }
62 }
63
64
65 int
66 main ()
67 {
68         test (dcp::Size(1998, 1080), dcp::Size(1998, 1080), true);
69         test (dcp::Size(1998, 1080), dcp::Size(1998, 1080), false);
70
71         test (dcp::Size(1998, 1080), dcp::Size(2006, 1088), true);
72         test (dcp::Size(1998, 1080), dcp::Size(2006, 1088), false);
73
74         test (dcp::Size(996, 540), dcp::Size(1998, 1080), true);
75         test (dcp::Size(996, 540), dcp::Size(1998, 1080), false);
76
77         test (dcp::Size(498, 270), dcp::Size(1998, 1080), true);
78         test (dcp::Size(498, 270), dcp::Size(1998, 1080), false);
79
80         return 0;
81 }
82