1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <string>
13 
14 #include "config/aom_config.h"
15 
16 #include "common/y4menc.h"
17 #include "test/md5_helper.h"
18 #include "test/util.h"
19 #include "test/y4m_video_source.h"
20 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
21 
22 namespace {
23 
24 using std::string;
25 
26 static const unsigned int kWidth = 160;
27 static const unsigned int kHeight = 90;
28 static const unsigned int kFrames = 10;
29 
30 struct Y4mTestParam {
31   const char *filename;
32   unsigned int bit_depth;
33   aom_img_fmt format;
34   const char *md5raw;
35 };
36 
37 const Y4mTestParam kY4mTestVectors[] = {
38   { "park_joy_90p_8_420.y4m", 8, AOM_IMG_FMT_I420,
39     "e5406275b9fc6bb3436c31d4a05c1cab" },
40   { "park_joy_90p_8_420_monochrome.y4m", 8, AOM_IMG_FMT_I420,
41     "95ef5bf6218580588be24a5271bb6a7f" },
42   { "park_joy_90p_8_420_vertical_csp.y4m", 8, AOM_IMG_FMT_I420,
43     "e5406275b9fc6bb3436c31d4a05c1cab" },
44   { "park_joy_90p_8_422.y4m", 8, AOM_IMG_FMT_I422,
45     "284a47a47133b12884ec3a14e959a0b6" },
46   { "park_joy_90p_8_444.y4m", 8, AOM_IMG_FMT_I444,
47     "90517ff33843d85de712fd4fe60dbed0" },
48   { "park_joy_90p_10_420.y4m", 10, AOM_IMG_FMT_I42016,
49     "63f21f9f717d8b8631bd2288ee87137b" },
50   { "park_joy_90p_10_422.y4m", 10, AOM_IMG_FMT_I42216,
51     "48ab51fb540aed07f7ff5af130c9b605" },
52   { "park_joy_90p_10_444.y4m", 10, AOM_IMG_FMT_I44416,
53     "067bfd75aa85ff9bae91fa3e0edd1e3e" },
54   { "park_joy_90p_12_420.y4m", 12, AOM_IMG_FMT_I42016,
55     "9e6d8f6508c6e55625f6b697bc461cef" },
56   { "park_joy_90p_12_422.y4m", 12, AOM_IMG_FMT_I42216,
57     "b239c6b301c0b835485be349ca83a7e3" },
58   { "park_joy_90p_12_444.y4m", 12, AOM_IMG_FMT_I44416,
59     "5a6481a550821dab6d0192f5c63845e9" },
60 };
61 
62 static const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
63 
64 class Y4mVideoSourceTest : public ::testing::TestWithParam<Y4mTestParam>,
65                            public ::libaom_test::Y4mVideoSource {
66  protected:
Y4mVideoSourceTest()67   Y4mVideoSourceTest() : Y4mVideoSource("", 0, 0) {}
68 
~Y4mVideoSourceTest()69   virtual ~Y4mVideoSourceTest() { CloseSource(); }
70 
Init(const std::string & file_name,int limit)71   virtual void Init(const std::string &file_name, int limit) {
72     file_name_ = file_name;
73     start_ = 0;
74     limit_ = limit;
75     frame_ = 0;
76     Begin();
77   }
78 
79   // Checks y4m header information
HeaderChecks(unsigned int bit_depth,aom_img_fmt_t fmt)80   void HeaderChecks(unsigned int bit_depth, aom_img_fmt_t fmt) {
81     ASSERT_TRUE(input_file_ != NULL);
82     ASSERT_EQ(y4m_.pic_w, (int)kWidth);
83     ASSERT_EQ(y4m_.pic_h, (int)kHeight);
84     ASSERT_EQ(img()->d_w, kWidth);
85     ASSERT_EQ(img()->d_h, kHeight);
86     ASSERT_EQ(y4m_.bit_depth, bit_depth);
87     ASSERT_EQ(y4m_.aom_fmt, fmt);
88     if (fmt == AOM_IMG_FMT_I420 || fmt == AOM_IMG_FMT_I42016) {
89       ASSERT_EQ(y4m_.bps, (int)y4m_.bit_depth * 3 / 2);
90       ASSERT_EQ(img()->x_chroma_shift, 1U);
91       ASSERT_EQ(img()->y_chroma_shift, 1U);
92     }
93     if (fmt == AOM_IMG_FMT_I422 || fmt == AOM_IMG_FMT_I42216) {
94       ASSERT_EQ(y4m_.bps, (int)y4m_.bit_depth * 2);
95       ASSERT_EQ(img()->x_chroma_shift, 1U);
96       ASSERT_EQ(img()->y_chroma_shift, 0U);
97     }
98     if (fmt == AOM_IMG_FMT_I444 || fmt == AOM_IMG_FMT_I44416) {
99       ASSERT_EQ(y4m_.bps, (int)y4m_.bit_depth * 3);
100       ASSERT_EQ(img()->x_chroma_shift, 0U);
101       ASSERT_EQ(img()->y_chroma_shift, 0U);
102     }
103   }
104 
105   // Checks MD5 of the raw frame data
Md5Check(const string & expected_md5)106   void Md5Check(const string &expected_md5) {
107     ASSERT_TRUE(input_file_ != NULL);
108     libaom_test::MD5 md5;
109     for (unsigned int i = start_; i < limit_; i++) {
110       md5.Add(img());
111       Next();
112     }
113     ASSERT_EQ(string(md5.Get()), expected_md5);
114   }
115 };
116 
TEST_P(Y4mVideoSourceTest,SourceTest)117 TEST_P(Y4mVideoSourceTest, SourceTest) {
118   const Y4mTestParam t = GetParam();
119   Init(t.filename, kFrames);
120   HeaderChecks(t.bit_depth, t.format);
121   Md5Check(t.md5raw);
122 }
123 
124 INSTANTIATE_TEST_SUITE_P(C, Y4mVideoSourceTest,
125                          ::testing::ValuesIn(kY4mTestVectors));
126 
127 class Y4mVideoWriteTest : public Y4mVideoSourceTest {
128  protected:
Y4mVideoWriteTest()129   Y4mVideoWriteTest() : tmpfile_(NULL) {}
130 
~Y4mVideoWriteTest()131   virtual ~Y4mVideoWriteTest() {
132     delete tmpfile_;
133     input_file_ = NULL;
134   }
135 
ReplaceInputFile(FILE * input_file)136   void ReplaceInputFile(FILE *input_file) {
137     CloseSource();
138     frame_ = 0;
139     input_file_ = input_file;
140     rewind(input_file_);
141     ReadSourceToStart();
142   }
143 
144   // Writes out a y4m file and then reads it back
WriteY4mAndReadBack()145   void WriteY4mAndReadBack() {
146     ASSERT_TRUE(input_file_ != NULL);
147     char buf[Y4M_BUFFER_SIZE] = { 0 };
148     const struct AvxRational framerate = { y4m_.fps_n, y4m_.fps_d };
149     tmpfile_ = new libaom_test::TempOutFile;
150     ASSERT_TRUE(tmpfile_->file() != NULL);
151     y4m_write_file_header(buf, sizeof(buf), kWidth, kHeight, &framerate,
152                           img()->monochrome, img()->csp, y4m_.aom_fmt,
153                           y4m_.bit_depth, AOM_CR_STUDIO_RANGE);
154     fputs(buf, tmpfile_->file());
155     for (unsigned int i = start_; i < limit_; i++) {
156       y4m_write_frame_header(buf, sizeof(buf));
157       fputs(buf, tmpfile_->file());
158       y4m_write_image_file(img(), PLANES_YUV, tmpfile_->file());
159       Next();
160     }
161     ReplaceInputFile(tmpfile_->file());
162   }
163 
Init(const std::string & file_name,int limit)164   virtual void Init(const std::string &file_name, int limit) {
165     Y4mVideoSourceTest::Init(file_name, limit);
166     WriteY4mAndReadBack();
167   }
168   libaom_test::TempOutFile *tmpfile_;
169 };
170 
TEST_P(Y4mVideoWriteTest,WriteTest)171 TEST_P(Y4mVideoWriteTest, WriteTest) {
172   const Y4mTestParam t = GetParam();
173   Init(t.filename, kFrames);
174   HeaderChecks(t.bit_depth, t.format);
175   Md5Check(t.md5raw);
176 }
177 
178 INSTANTIATE_TEST_SUITE_P(C, Y4mVideoWriteTest,
179                          ::testing::ValuesIn(kY4mTestVectors));
180 
181 static const char kY4MRegularHeader[] =
182     "YUV4MPEG2 W4 H4 F30:1 Ip A0:0 C420jpeg XYSCSS=420JPEG\n"
183     "FRAME\n"
184     "012345678912345601230123";
185 
TEST(Y4MHeaderTest,RegularHeader)186 TEST(Y4MHeaderTest, RegularHeader) {
187   libaom_test::TempOutFile f;
188   fwrite(kY4MRegularHeader, 1, sizeof(kY4MRegularHeader), f.file());
189   fflush(f.file());
190   EXPECT_EQ(0, fseek(f.file(), 0, 0));
191 
192   y4m_input y4m;
193   EXPECT_EQ(y4m_input_open(&y4m, f.file(), NULL, 0, AOM_CSP_UNKNOWN,
194                            /*only_420=*/0),
195             0);
196   EXPECT_EQ(y4m.pic_w, 4);
197   EXPECT_EQ(y4m.pic_h, 4);
198   EXPECT_EQ(y4m.fps_n, 30);
199   EXPECT_EQ(y4m.fps_d, 1);
200   EXPECT_EQ(y4m.interlace, 'p');
201   EXPECT_EQ(y4m.color_range, AOM_CR_STUDIO_RANGE);
202   EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
203   y4m_input_close(&y4m);
204 }
205 
206 // Testing that headers over 100 characters can be parsed.
207 static const char kY4MLongHeader[] =
208     "YUV4MPEG2 W4 H4 F30:1 Ip A0:0 C420jpeg XYSCSS=420JPEG "
209     "XCOLORRANGE=LIMITED XSOME_UNKNOWN_METADATA XOTHER_UNKNOWN_METADATA\n"
210     "FRAME\n"
211     "012345678912345601230123";
212 
TEST(Y4MHeaderTest,LongHeader)213 TEST(Y4MHeaderTest, LongHeader) {
214   libaom_test::TempOutFile tmpfile;
215   FILE *f = tmpfile.file();
216   fwrite(kY4MLongHeader, 1, sizeof(kY4MLongHeader), f);
217   fflush(f);
218   EXPECT_EQ(fseek(f, 0, 0), 0);
219 
220   y4m_input y4m;
221   EXPECT_EQ(y4m_input_open(&y4m, f, NULL, 0, AOM_CSP_UNKNOWN,
222                            /*only_420=*/0),
223             0);
224   EXPECT_EQ(y4m.pic_w, 4);
225   EXPECT_EQ(y4m.pic_h, 4);
226   EXPECT_EQ(y4m.fps_n, 30);
227   EXPECT_EQ(y4m.fps_d, 1);
228   EXPECT_EQ(y4m.interlace, 'p');
229   EXPECT_EQ(y4m.color_range, AOM_CR_STUDIO_RANGE);
230   EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
231   y4m_input_close(&y4m);
232 }
233 
234 static const char kY4MFullRangeHeader[] =
235     "YUV4MPEG2 W4 H4 F30:1 Ip A0:0 C420jpeg XYSCSS=420JPEG XCOLORRANGE=FULL\n"
236     "FRAME\n"
237     "012345678912345601230123";
238 
TEST(Y4MHeaderTest,FullRangeHeader)239 TEST(Y4MHeaderTest, FullRangeHeader) {
240   libaom_test::TempOutFile tmpfile;
241   FILE *f = tmpfile.file();
242   fwrite(kY4MFullRangeHeader, 1, sizeof(kY4MFullRangeHeader), f);
243   fflush(f);
244   EXPECT_EQ(fseek(f, 0, 0), 0);
245 
246   y4m_input y4m;
247   EXPECT_EQ(y4m_input_open(&y4m, f, NULL, 0, AOM_CSP_UNKNOWN,
248                            /*only_420=*/0),
249             0);
250   EXPECT_EQ(y4m.pic_w, 4);
251   EXPECT_EQ(y4m.pic_h, 4);
252   EXPECT_EQ(y4m.fps_n, 30);
253   EXPECT_EQ(y4m.fps_d, 1);
254   EXPECT_EQ(y4m.interlace, 'p');
255   EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
256   EXPECT_EQ(y4m.color_range, AOM_CR_FULL_RANGE);
257   y4m_input_close(&y4m);
258 }
259 
TEST(Y4MHeaderTest,WriteStudioColorRange)260 TEST(Y4MHeaderTest, WriteStudioColorRange) {
261   char buf[128];
262   struct AvxRational framerate = { /*numerator=*/30, /*denominator=*/1 };
263   EXPECT_GE(y4m_write_file_header(
264                 buf, /*len=*/128, /*width=*/4, /*height=*/5, &framerate,
265                 /*monochrome=*/0, AOM_CSP_UNKNOWN, AOM_IMG_FMT_I420,
266                 /*bit_depth=*/8, AOM_CR_STUDIO_RANGE),
267             0);
268   EXPECT_EQ(strcmp("YUV4MPEG2 W4 H5 F30:1 Ip C420jpeg\n", buf), 0);
269 }
270 
TEST(Y4MHeaderTest,WriteFullColorRange)271 TEST(Y4MHeaderTest, WriteFullColorRange) {
272   char buf[128];
273   struct AvxRational framerate = { /*numerator=*/30, /*denominator=*/1 };
274   EXPECT_GE(y4m_write_file_header(
275                 buf, /*len=*/128, /*width=*/4, /*height=*/5, &framerate,
276                 /*monochrome=*/0, AOM_CSP_UNKNOWN, AOM_IMG_FMT_I420,
277                 /*bit_depth=*/8, AOM_CR_FULL_RANGE),
278             0);
279   EXPECT_EQ(strcmp("YUV4MPEG2 W4 H5 F30:1 Ip C420jpeg XCOLORRANGE=FULL\n", buf),
280             0);
281 }
282 
283 }  // namespace
284