xref: /aosp_15_r20/external/libvpx/test/y4m_test.cc (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <string>
12 
13 #include "gtest/gtest.h"
14 
15 #include "./vpx_config.h"
16 #include "./y4menc.h"
17 #include "test/md5_helper.h"
18 #include "test/util.h"
19 #include "test/y4m_video_source.h"
20 
21 namespace {
22 
23 using std::string;
24 
25 static const unsigned int kWidth = 160;
26 static const unsigned int kHeight = 90;
27 static const unsigned int kFrames = 10;
28 
29 struct Y4mTestParam {
30   const char *filename;
31   unsigned int bit_depth;
32   vpx_img_fmt format;
33   const char *md5raw;
34 };
35 
36 const Y4mTestParam kY4mTestVectors[] = {
37   { "park_joy_90p_8_420.y4m", 8, VPX_IMG_FMT_I420,
38     "e5406275b9fc6bb3436c31d4a05c1cab" },
39   { "park_joy_90p_8_422.y4m", 8, VPX_IMG_FMT_I422,
40     "284a47a47133b12884ec3a14e959a0b6" },
41   { "park_joy_90p_8_444.y4m", 8, VPX_IMG_FMT_I444,
42     "90517ff33843d85de712fd4fe60dbed0" },
43   { "park_joy_90p_10_420_20f.y4m", 10, VPX_IMG_FMT_I42016,
44     "2f56ab9809269f074df7e3daf1ce0be6" },
45   { "park_joy_90p_10_422_20f.y4m", 10, VPX_IMG_FMT_I42216,
46     "1b5c73d2e8e8c4e02dc4889ecac41c83" },
47   { "park_joy_90p_10_444_20f.y4m", 10, VPX_IMG_FMT_I44416,
48     "ec4ab5be53195c5b838d1d19e1bc2674" },
49   { "park_joy_90p_12_420_20f.y4m", 12, VPX_IMG_FMT_I42016,
50     "3370856c8ddebbd1f9bb2e66f97677f4" },
51   { "park_joy_90p_12_422_20f.y4m", 12, VPX_IMG_FMT_I42216,
52     "4eab364318dd8201acbb182e43bd4966" },
53   { "park_joy_90p_12_444_20f.y4m", 12, VPX_IMG_FMT_I44416,
54     "f189dfbbd92119fc8e5f211a550166be" },
55 };
56 
write_image_file(const vpx_image_t * img,FILE * file)57 static void write_image_file(const vpx_image_t *img, FILE *file) {
58   int plane, y;
59   for (plane = 0; plane < 3; ++plane) {
60     const unsigned char *buf = img->planes[plane];
61     const int stride = img->stride[plane];
62     const int bytes_per_sample = (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
63     const int h =
64         (plane ? (img->d_h + img->y_chroma_shift) >> img->y_chroma_shift
65                : img->d_h);
66     const int w =
67         (plane ? (img->d_w + img->x_chroma_shift) >> img->x_chroma_shift
68                : img->d_w);
69     for (y = 0; y < h; ++y) {
70       fwrite(buf, bytes_per_sample, w, file);
71       buf += stride;
72     }
73   }
74 }
75 
76 class Y4mVideoSourceTest : public ::testing::TestWithParam<Y4mTestParam>,
77                            public ::libvpx_test::Y4mVideoSource {
78  protected:
Y4mVideoSourceTest()79   Y4mVideoSourceTest() : Y4mVideoSource("", 0, 0) {}
80 
~Y4mVideoSourceTest()81   ~Y4mVideoSourceTest() override { CloseSource(); }
82 
Init(const std::string & file_name,int limit)83   virtual void Init(const std::string &file_name, int limit) {
84     file_name_ = file_name;
85     start_ = 0;
86     limit_ = limit;
87     frame_ = 0;
88     Begin();
89   }
90 
91   // Checks y4m header information
HeaderChecks(unsigned int bit_depth,vpx_img_fmt_t fmt)92   void HeaderChecks(unsigned int bit_depth, vpx_img_fmt_t fmt) {
93     ASSERT_NE(input_file_, nullptr);
94     ASSERT_EQ(y4m_.pic_w, (int)kWidth);
95     ASSERT_EQ(y4m_.pic_h, (int)kHeight);
96     ASSERT_EQ(img()->d_w, kWidth);
97     ASSERT_EQ(img()->d_h, kHeight);
98     ASSERT_EQ(y4m_.bit_depth, bit_depth);
99     ASSERT_EQ(y4m_.vpx_fmt, fmt);
100     if (fmt == VPX_IMG_FMT_I420 || fmt == VPX_IMG_FMT_I42016) {
101       ASSERT_EQ(y4m_.bps, (int)y4m_.bit_depth * 3 / 2);
102       ASSERT_EQ(img()->x_chroma_shift, 1U);
103       ASSERT_EQ(img()->y_chroma_shift, 1U);
104     }
105     if (fmt == VPX_IMG_FMT_I422 || fmt == VPX_IMG_FMT_I42216) {
106       ASSERT_EQ(y4m_.bps, (int)y4m_.bit_depth * 2);
107       ASSERT_EQ(img()->x_chroma_shift, 1U);
108       ASSERT_EQ(img()->y_chroma_shift, 0U);
109     }
110     if (fmt == VPX_IMG_FMT_I444 || fmt == VPX_IMG_FMT_I44416) {
111       ASSERT_EQ(y4m_.bps, (int)y4m_.bit_depth * 3);
112       ASSERT_EQ(img()->x_chroma_shift, 0U);
113       ASSERT_EQ(img()->y_chroma_shift, 0U);
114     }
115   }
116 
117   // Checks MD5 of the raw frame data
Md5Check(const string & expected_md5)118   void Md5Check(const string &expected_md5) {
119     ASSERT_NE(input_file_, nullptr);
120     libvpx_test::MD5 md5;
121     for (unsigned int i = start_; i < limit_; i++) {
122       md5.Add(img());
123       Next();
124     }
125     ASSERT_EQ(string(md5.Get()), expected_md5);
126   }
127 };
128 
TEST_P(Y4mVideoSourceTest,SourceTest)129 TEST_P(Y4mVideoSourceTest, SourceTest) {
130   const Y4mTestParam t = GetParam();
131   Init(t.filename, kFrames);
132   HeaderChecks(t.bit_depth, t.format);
133   Md5Check(t.md5raw);
134 }
135 
136 INSTANTIATE_TEST_SUITE_P(C, Y4mVideoSourceTest,
137                          ::testing::ValuesIn(kY4mTestVectors));
138 
139 class Y4mVideoWriteTest : public Y4mVideoSourceTest {
140  protected:
Y4mVideoWriteTest()141   Y4mVideoWriteTest() : tmpfile_(nullptr) {}
142 
~Y4mVideoWriteTest()143   ~Y4mVideoWriteTest() override {
144     delete tmpfile_;
145     input_file_ = nullptr;
146   }
147 
ReplaceInputFile(FILE * input_file)148   void ReplaceInputFile(FILE *input_file) {
149     CloseSource();
150     frame_ = 0;
151     input_file_ = input_file;
152     rewind(input_file_);
153     ReadSourceToStart();
154   }
155 
156   // Writes out a y4m file and then reads it back
WriteY4mAndReadBack()157   void WriteY4mAndReadBack() {
158     ASSERT_NE(input_file_, nullptr);
159     char buf[Y4M_BUFFER_SIZE] = { 0 };
160     const struct VpxRational framerate = { y4m_.fps_n, y4m_.fps_d };
161     tmpfile_ = new libvpx_test::TempOutFile;
162     ASSERT_NE(tmpfile_->file(), nullptr);
163     y4m_write_file_header(buf, sizeof(buf), kWidth, kHeight, &framerate,
164                           y4m_.vpx_fmt, y4m_.bit_depth);
165     fputs(buf, tmpfile_->file());
166     for (unsigned int i = start_; i < limit_; i++) {
167       y4m_write_frame_header(buf, sizeof(buf));
168       fputs(buf, tmpfile_->file());
169       write_image_file(img(), tmpfile_->file());
170       Next();
171     }
172     ReplaceInputFile(tmpfile_->file());
173   }
174 
Init(const std::string & file_name,int limit)175   void Init(const std::string &file_name, int limit) override {
176     Y4mVideoSourceTest::Init(file_name, limit);
177     WriteY4mAndReadBack();
178   }
179   libvpx_test::TempOutFile *tmpfile_;
180 };
181 
TEST_P(Y4mVideoWriteTest,WriteTest)182 TEST_P(Y4mVideoWriteTest, WriteTest) {
183   const Y4mTestParam t = GetParam();
184   Init(t.filename, kFrames);
185   HeaderChecks(t.bit_depth, t.format);
186   Md5Check(t.md5raw);
187 }
188 
189 INSTANTIATE_TEST_SUITE_P(C, Y4mVideoWriteTest,
190                          ::testing::ValuesIn(kY4mTestVectors));
191 
192 static const char kY4MRegularHeader[] =
193     "YUV4MPEG2 W4 H4 F30:1 Ip A0:0 C420jpeg XYSCSS=420JPEG\n"
194     "FRAME\n"
195     "012345678912345601230123";
196 
TEST(Y4MHeaderTest,RegularHeader)197 TEST(Y4MHeaderTest, RegularHeader) {
198   libvpx_test::TempOutFile f;
199   ASSERT_NE(f.file(), nullptr);
200   fwrite(kY4MRegularHeader, 1, sizeof(kY4MRegularHeader), f.file());
201   fflush(f.file());
202   EXPECT_EQ(0, fseek(f.file(), 0, 0));
203 
204   y4m_input y4m;
205   EXPECT_EQ(y4m_input_open(&y4m, f.file(), /*skip_buffer=*/nullptr,
206                            /*num_skip=*/0, /*only_420=*/0),
207             0);
208   EXPECT_EQ(y4m.pic_w, 4);
209   EXPECT_EQ(y4m.pic_h, 4);
210   EXPECT_EQ(y4m.fps_n, 30);
211   EXPECT_EQ(y4m.fps_d, 1);
212   EXPECT_EQ(y4m.interlace, 'p');
213   EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
214   y4m_input_close(&y4m);
215 }
216 
217 // Testing that headers over 100 characters can be parsed.
218 static const char kY4MLongHeader[] =
219     "YUV4MPEG2 W4 H4 F30:1 Ip A0:0 C420jpeg XYSCSS=420JPEG "
220     "XCOLORRANGE=LIMITED XSOME_UNKNOWN_METADATA XOTHER_UNKNOWN_METADATA\n"
221     "FRAME\n"
222     "012345678912345601230123";
223 
TEST(Y4MHeaderTest,LongHeader)224 TEST(Y4MHeaderTest, LongHeader) {
225   libvpx_test::TempOutFile f;
226   ASSERT_NE(f.file(), nullptr);
227   fwrite(kY4MLongHeader, 1, sizeof(kY4MLongHeader), f.file());
228   fflush(f.file());
229   EXPECT_EQ(fseek(f.file(), 0, 0), 0);
230 
231   y4m_input y4m;
232   EXPECT_EQ(y4m_input_open(&y4m, f.file(), /*skip_buffer=*/nullptr,
233                            /*num_skip=*/0, /*only_420=*/0),
234             0);
235   EXPECT_EQ(y4m.pic_w, 4);
236   EXPECT_EQ(y4m.pic_h, 4);
237   EXPECT_EQ(y4m.fps_n, 30);
238   EXPECT_EQ(y4m.fps_d, 1);
239   EXPECT_EQ(y4m.interlace, 'p');
240   EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
241   y4m_input_close(&y4m);
242 }
243 
244 }  // namespace
245