tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

y4m_test.cc (9369B)


      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 "gtest/gtest.h"
     18 #include "test/md5_helper.h"
     19 #include "test/util.h"
     20 #include "test/y4m_video_source.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:
     67  Y4mVideoSourceTest() : Y4mVideoSource("", 0, 0) {}
     68 
     69  ~Y4mVideoSourceTest() override { CloseSource(); }
     70 
     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
     80  void HeaderChecks(unsigned int bit_depth, aom_img_fmt_t fmt) {
     81    ASSERT_NE(input_file_, nullptr);
     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
    106  void Md5Check(const string &expected_md5) {
    107    ASSERT_NE(input_file_, nullptr);
    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 
    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:
    129  Y4mVideoWriteTest() : tmpfile_(nullptr) {}
    130 
    131  ~Y4mVideoWriteTest() override {
    132    delete tmpfile_;
    133    input_file_ = nullptr;
    134  }
    135 
    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
    145  void WriteY4mAndReadBack() {
    146    ASSERT_NE(input_file_, nullptr);
    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_NE(tmpfile_, nullptr);
    151    ASSERT_NE(tmpfile_->file(), nullptr);
    152    y4m_write_file_header(buf, sizeof(buf), kWidth, kHeight, &framerate,
    153                          img()->monochrome, img()->csp, y4m_.aom_fmt,
    154                          y4m_.bit_depth, AOM_CR_STUDIO_RANGE);
    155    fputs(buf, tmpfile_->file());
    156    for (unsigned int i = start_; i < limit_; i++) {
    157      y4m_write_frame_header(buf, sizeof(buf));
    158      fputs(buf, tmpfile_->file());
    159      y4m_write_image_file(img(), PLANES_YUV, tmpfile_->file());
    160      Next();
    161    }
    162    ReplaceInputFile(tmpfile_->file());
    163  }
    164 
    165  void Init(const std::string &file_name, int limit) override {
    166    Y4mVideoSourceTest::Init(file_name, limit);
    167    WriteY4mAndReadBack();
    168  }
    169  libaom_test::TempOutFile *tmpfile_;
    170 };
    171 
    172 TEST_P(Y4mVideoWriteTest, WriteTest) {
    173  const Y4mTestParam t = GetParam();
    174  Init(t.filename, kFrames);
    175  HeaderChecks(t.bit_depth, t.format);
    176  Md5Check(t.md5raw);
    177 }
    178 
    179 INSTANTIATE_TEST_SUITE_P(C, Y4mVideoWriteTest,
    180                         ::testing::ValuesIn(kY4mTestVectors));
    181 
    182 static const char kY4MRegularHeader[] =
    183    "YUV4MPEG2 W4 H4 F30:1 Ip A0:0 C420jpeg XYSCSS=420JPEG\n"
    184    "FRAME\n"
    185    "012345678912345601230123";
    186 
    187 TEST(Y4MHeaderTest, RegularHeader) {
    188  libaom_test::TempOutFile f;
    189  ASSERT_NE(f.file(), nullptr);
    190  fwrite(kY4MRegularHeader, 1, sizeof(kY4MRegularHeader), f.file());
    191  fflush(f.file());
    192  EXPECT_EQ(0, fseek(f.file(), 0, 0));
    193 
    194  y4m_input y4m;
    195  EXPECT_EQ(y4m_input_open(&y4m, f.file(), nullptr, 0, AOM_CSP_UNKNOWN,
    196                           /*only_420=*/0),
    197            0);
    198  EXPECT_EQ(y4m.pic_w, 4);
    199  EXPECT_EQ(y4m.pic_h, 4);
    200  EXPECT_EQ(y4m.fps_n, 30);
    201  EXPECT_EQ(y4m.fps_d, 1);
    202  EXPECT_EQ(y4m.interlace, 'p');
    203  EXPECT_EQ(y4m.color_range, AOM_CR_STUDIO_RANGE);
    204  EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
    205  y4m_input_close(&y4m);
    206 }
    207 
    208 // Testing that headers over 100 characters can be parsed.
    209 static const char kY4MLongHeader[] =
    210    "YUV4MPEG2 W4 H4 F30:1 Ip A0:0 C420jpeg XYSCSS=420JPEG "
    211    "XCOLORRANGE=LIMITED XSOME_UNKNOWN_METADATA XOTHER_UNKNOWN_METADATA\n"
    212    "FRAME\n"
    213    "012345678912345601230123";
    214 
    215 TEST(Y4MHeaderTest, LongHeader) {
    216  libaom_test::TempOutFile tmpfile;
    217  FILE *f = tmpfile.file();
    218  ASSERT_NE(f, nullptr);
    219  fwrite(kY4MLongHeader, 1, sizeof(kY4MLongHeader), f);
    220  fflush(f);
    221  EXPECT_EQ(fseek(f, 0, 0), 0);
    222 
    223  y4m_input y4m;
    224  EXPECT_EQ(y4m_input_open(&y4m, f, nullptr, 0, AOM_CSP_UNKNOWN,
    225                           /*only_420=*/0),
    226            0);
    227  EXPECT_EQ(y4m.pic_w, 4);
    228  EXPECT_EQ(y4m.pic_h, 4);
    229  EXPECT_EQ(y4m.fps_n, 30);
    230  EXPECT_EQ(y4m.fps_d, 1);
    231  EXPECT_EQ(y4m.interlace, 'p');
    232  EXPECT_EQ(y4m.color_range, AOM_CR_STUDIO_RANGE);
    233  EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
    234  y4m_input_close(&y4m);
    235 }
    236 
    237 static const char kY4MFullRangeHeader[] =
    238    "YUV4MPEG2 W4 H4 F30:1 Ip A0:0 C420jpeg XYSCSS=420JPEG XCOLORRANGE=FULL\n"
    239    "FRAME\n"
    240    "012345678912345601230123";
    241 
    242 TEST(Y4MHeaderTest, FullRangeHeader) {
    243  libaom_test::TempOutFile tmpfile;
    244  FILE *f = tmpfile.file();
    245  ASSERT_NE(f, nullptr);
    246  fwrite(kY4MFullRangeHeader, 1, sizeof(kY4MFullRangeHeader), f);
    247  fflush(f);
    248  EXPECT_EQ(fseek(f, 0, 0), 0);
    249 
    250  y4m_input y4m;
    251  EXPECT_EQ(y4m_input_open(&y4m, f, nullptr, 0, AOM_CSP_UNKNOWN,
    252                           /*only_420=*/0),
    253            0);
    254  EXPECT_EQ(y4m.pic_w, 4);
    255  EXPECT_EQ(y4m.pic_h, 4);
    256  EXPECT_EQ(y4m.fps_n, 30);
    257  EXPECT_EQ(y4m.fps_d, 1);
    258  EXPECT_EQ(y4m.interlace, 'p');
    259  EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
    260  EXPECT_EQ(y4m.color_range, AOM_CR_FULL_RANGE);
    261  y4m_input_close(&y4m);
    262 }
    263 
    264 TEST(Y4MHeaderTest, WriteStudioColorRange) {
    265  char buf[128];
    266  struct AvxRational framerate = { /*numerator=*/30, /*denominator=*/1 };
    267  EXPECT_GE(y4m_write_file_header(
    268                buf, /*len=*/128, /*width=*/4, /*height=*/5, &framerate,
    269                /*monochrome=*/0, AOM_CSP_UNKNOWN, AOM_IMG_FMT_I420,
    270                /*bit_depth=*/8, AOM_CR_STUDIO_RANGE),
    271            0);
    272  EXPECT_EQ(strcmp("YUV4MPEG2 W4 H5 F30:1 Ip C420jpeg\n", buf), 0);
    273 }
    274 
    275 TEST(Y4MHeaderTest, WriteFullColorRange) {
    276  char buf[128];
    277  struct AvxRational framerate = { /*numerator=*/30, /*denominator=*/1 };
    278  EXPECT_GE(y4m_write_file_header(
    279                buf, /*len=*/128, /*width=*/4, /*height=*/5, &framerate,
    280                /*monochrome=*/0, AOM_CSP_UNKNOWN, AOM_IMG_FMT_I420,
    281                /*bit_depth=*/8, AOM_CR_FULL_RANGE),
    282            0);
    283  EXPECT_EQ(strcmp("YUV4MPEG2 W4 H5 F30:1 Ip C420jpeg XCOLORRANGE=FULL\n", buf),
    284            0);
    285 }
    286 
    287 }  // namespace