Ignore audio streams with no codec, instead of crashing.
[dcpomatic.git] / test / make_black_test.cc
1 /*
2     Copyright (C) 2012 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 /** @file  test/make_black_test.cc
22  *  @brief Check that Image::make_black works and doesn't use values which crash
23  *  sws_scale().
24  *  @ingroup selfcontained
25  *  @see test/image_test.cc
26  */
27
28 #include <boost/test/unit_test.hpp>
29 #include <dcp/util.h>
30 extern "C" {
31 #include <libavutil/pixfmt.h>
32 }
33 #include "lib/ffmpeg_image_proxy.h"
34 #include "lib/image.h"
35
36 using std::list;
37 using boost::shared_ptr;
38
39 BOOST_AUTO_TEST_CASE (make_black_test)
40 {
41         dcp::Size in_size (512, 512);
42         dcp::Size out_size (1024, 1024);
43
44         list<AVPixelFormat> pix_fmts;
45         pix_fmts.push_back (AV_PIX_FMT_RGB24); // 2
46         pix_fmts.push_back (AV_PIX_FMT_ARGB);
47         pix_fmts.push_back (AV_PIX_FMT_RGBA);
48         pix_fmts.push_back (AV_PIX_FMT_ABGR);
49         pix_fmts.push_back (AV_PIX_FMT_BGRA);
50         pix_fmts.push_back (AV_PIX_FMT_YUV420P); // 0
51         pix_fmts.push_back (AV_PIX_FMT_YUV411P);
52         pix_fmts.push_back (AV_PIX_FMT_YUV422P10LE);
53         pix_fmts.push_back (AV_PIX_FMT_YUV422P16LE);
54         pix_fmts.push_back (AV_PIX_FMT_YUV444P9LE);
55         pix_fmts.push_back (AV_PIX_FMT_YUV444P9BE);
56         pix_fmts.push_back (AV_PIX_FMT_YUV444P10LE);
57         pix_fmts.push_back (AV_PIX_FMT_YUV444P10BE);
58         pix_fmts.push_back (AV_PIX_FMT_UYVY422);
59         pix_fmts.push_back (AV_PIX_FMT_YUVJ420P);
60         pix_fmts.push_back (AV_PIX_FMT_YUVJ422P);
61         pix_fmts.push_back (AV_PIX_FMT_YUVJ444P);
62         pix_fmts.push_back (AV_PIX_FMT_YUVA420P9BE);
63         pix_fmts.push_back (AV_PIX_FMT_YUVA422P9BE);
64         pix_fmts.push_back (AV_PIX_FMT_YUVA444P9BE);
65         pix_fmts.push_back (AV_PIX_FMT_YUVA420P9LE);
66         pix_fmts.push_back (AV_PIX_FMT_YUVA422P9LE);
67         pix_fmts.push_back (AV_PIX_FMT_YUVA444P9LE);
68         pix_fmts.push_back (AV_PIX_FMT_YUVA420P10BE);
69         pix_fmts.push_back (AV_PIX_FMT_YUVA422P10BE);
70         pix_fmts.push_back (AV_PIX_FMT_YUVA444P10BE);
71         pix_fmts.push_back (AV_PIX_FMT_YUVA420P10LE);
72         pix_fmts.push_back (AV_PIX_FMT_YUVA422P10LE);
73         pix_fmts.push_back (AV_PIX_FMT_YUVA444P10LE);
74         pix_fmts.push_back (AV_PIX_FMT_YUVA420P16BE);
75         pix_fmts.push_back (AV_PIX_FMT_YUVA422P16BE);
76         pix_fmts.push_back (AV_PIX_FMT_YUVA444P16BE);
77         pix_fmts.push_back (AV_PIX_FMT_YUVA420P16LE);
78         pix_fmts.push_back (AV_PIX_FMT_YUVA422P16LE);
79         pix_fmts.push_back (AV_PIX_FMT_YUVA444P16LE);
80         pix_fmts.push_back (AV_PIX_FMT_RGB555LE); // 46
81
82         int N = 0;
83         for (list<AVPixelFormat>::const_iterator i = pix_fmts.begin(); i != pix_fmts.end(); ++i) {
84                 boost::shared_ptr<Image> foo (new Image (*i, in_size, true));
85                 foo->make_black ();
86                 boost::shared_ptr<Image> bar = foo->scale (out_size, dcp::YUV_TO_RGB_REC601, AV_PIX_FMT_RGB24, true, false);
87
88                 uint8_t* p = bar->data()[0];
89                 for (int y = 0; y < bar->size().height; ++y) {
90                         uint8_t* q = p;
91                         for (int x = 0; x < bar->line_size()[0]; ++x) {
92                                 if (*q != 0) {
93                                         std::cerr << "x=" << x << ", (x%3)=" << (x%3) << "\n";
94                                 }
95                                 BOOST_CHECK_EQUAL (*q++, 0);
96                         }
97                         p += bar->stride()[0];
98                 }
99
100                 ++N;
101         }
102 }
103
104
105 BOOST_AUTO_TEST_CASE (make_part_black_test)
106 {
107         shared_ptr<FFmpegImageProxy> proxy (new FFmpegImageProxy("test/data/flat_red.png"));
108         shared_ptr<Image> original = proxy->image().first;
109
110         list<AVPixelFormat> pix_fmts = {
111                 AV_PIX_FMT_RGB24,
112                 AV_PIX_FMT_ARGB,
113                 AV_PIX_FMT_RGBA,
114                 AV_PIX_FMT_ABGR,
115                 AV_PIX_FMT_BGRA,
116                 AV_PIX_FMT_YUV420P,
117                 AV_PIX_FMT_YUV422P10LE,
118         };
119
120         list<std::pair<int, int> > positions = {
121                 { 0, 256 },
122                 { 128, 64 },
123         };
124
125         int N = 0;
126         for (list<AVPixelFormat>::const_iterator i = pix_fmts.begin(); i != pix_fmts.end(); ++i) {
127                 for (list<std::pair<int, int> >::const_iterator j = positions.begin(); j != positions.end(); ++j) {
128                         shared_ptr<Image> foo = original->convert_pixel_format(dcp::YUV_TO_RGB_REC601, *i, true, false);
129                         foo->make_part_black (j->first, j->second);
130                         shared_ptr<Image> bar = foo->convert_pixel_format (dcp::YUV_TO_RGB_REC601, AV_PIX_FMT_RGB24, true, false);
131
132                         uint8_t* p = bar->data()[0];
133                         for (int y = 0; y < bar->size().height; ++y) {
134                                 uint8_t* q = p;
135                                 for (int x = 0; x < bar->size().width; ++x) {
136                                         int r = *q++;
137                                         int g = *q++;
138                                         int b = *q++;
139                                         if (x >= j->first && x < (j->first + j->second)) {
140                                                 BOOST_CHECK_MESSAGE (
141                                                         r < 3, "red=" << static_cast<int>(r) << " at x=" << x << " format " << *i << " from " << j->first << " width " << j->second
142                                                         );
143                                         } else {
144                                                 BOOST_CHECK_MESSAGE (
145                                                         r >= 252, "red=" << static_cast<int>(r) << " at x=" << x << " format " << *i << " from " << j->first << " width " << j->second
146                                                         );
147
148                                         }
149                                         BOOST_CHECK_MESSAGE (
150                                                 g == 0, "green=" << static_cast<int>(g) << " at x=" << x << " format " << *i << " from " << j->first << " width " << j->second
151                                                 );
152                                         BOOST_CHECK_MESSAGE (
153                                                 b == 0, "blue=" << static_cast<int>(b) << " at x=" << x << " format " << *i << " from " << j->first << " width " << j->second
154                                                 );
155                                 }
156                                 p += bar->stride()[0];
157                         }
158
159                         ++N;
160                 }
161         }
162 }
163