tor-browser

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

encode_test_driver.cc (10289B)


      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 <memory>
     13 #include <string>
     14 
     15 #include "gtest/gtest.h"
     16 
     17 #include "config/aom_config.h"
     18 
     19 #include "aom_ports/mem.h"
     20 #include "test/codec_factory.h"
     21 #include "test/decode_test_driver.h"
     22 #include "test/encode_test_driver.h"
     23 #include "test/register_state_check.h"
     24 #include "test/video_source.h"
     25 
     26 namespace libaom_test {
     27 void Encoder::InitEncoder(VideoSource *video) {
     28  aom_codec_err_t res;
     29  const aom_image_t *img = video->img();
     30 
     31  if (img && !encoder_.priv) {
     32    cfg_.g_w = img->d_w;
     33    cfg_.g_h = img->d_h;
     34    cfg_.g_timebase = video->timebase();
     35    cfg_.rc_twopass_stats_in = stats_->buf();
     36 
     37    res = aom_codec_enc_init(&encoder_, CodecInterface(), &cfg_, init_flags_);
     38    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
     39  }
     40 }
     41 
     42 void Encoder::EncodeFrame(VideoSource *video,
     43                          const aom_enc_frame_flags_t frame_flags) {
     44  if (video->img())
     45    EncodeFrameInternal(*video, frame_flags);
     46  else
     47    Flush();
     48 
     49  // Handle twopass stats
     50  CxDataIterator iter = GetCxData();
     51 
     52  while (const aom_codec_cx_pkt_t *pkt = iter.Next()) {
     53    if (pkt->kind != AOM_CODEC_STATS_PKT) continue;
     54 
     55    stats_->Append(*pkt);
     56  }
     57 }
     58 
     59 void Encoder::EncodeFrameInternal(const VideoSource &video,
     60                                  const aom_enc_frame_flags_t frame_flags) {
     61  aom_codec_err_t res;
     62  const aom_image_t *img = video.img();
     63 
     64  // Handle frame resizing
     65  if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
     66    cfg_.g_w = img->d_w;
     67    cfg_.g_h = img->d_h;
     68    res = aom_codec_enc_config_set(&encoder_, &cfg_);
     69    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
     70  }
     71 
     72  // Encode the frame
     73  API_REGISTER_STATE_CHECK(res =
     74                               aom_codec_encode(&encoder_, img, video.pts(),
     75                                                video.duration(), frame_flags));
     76  ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
     77 }
     78 
     79 void Encoder::Flush() {
     80  const aom_codec_err_t res = aom_codec_encode(&encoder_, nullptr, 0, 0, 0);
     81  if (!encoder_.priv)
     82    ASSERT_EQ(AOM_CODEC_ERROR, res) << EncoderError();
     83  else
     84    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
     85 }
     86 
     87 void EncoderTest::InitializeConfig(TestMode mode) {
     88  int usage = AOM_USAGE_GOOD_QUALITY;
     89  switch (mode) {
     90    case kOnePassGood:
     91    case kTwoPassGood:
     92    case kLowComplexityDecode: break;
     93    case kRealTime: usage = AOM_USAGE_REALTIME; break;
     94    case kAllIntra: usage = AOM_USAGE_ALL_INTRA; break;
     95    default: ASSERT_TRUE(false) << "Unexpected mode " << mode;
     96  }
     97  mode_ = mode;
     98  passes_ = (mode == kTwoPassGood || mode == kLowComplexityDecode) ? 2 : 1;
     99 
    100  const aom_codec_err_t res = codec_->DefaultEncoderConfig(&cfg_, usage);
    101  ASSERT_EQ(AOM_CODEC_OK, res);
    102 }
    103 
    104 static bool compare_plane(const uint8_t *const buf1, int stride1,
    105                          const uint8_t *const buf2, int stride2, int w, int h,
    106                          int *const mismatch_row, int *const mismatch_col,
    107                          int *const mismatch_pix1, int *const mismatch_pix2) {
    108  int r, c;
    109 
    110  for (r = 0; r < h; ++r) {
    111    for (c = 0; c < w; ++c) {
    112      const int pix1 = buf1[r * stride1 + c];
    113      const int pix2 = buf2[r * stride2 + c];
    114 
    115      if (pix1 != pix2) {
    116        if (mismatch_row != nullptr) *mismatch_row = r;
    117        if (mismatch_col != nullptr) *mismatch_col = c;
    118        if (mismatch_pix1 != nullptr) *mismatch_pix1 = pix1;
    119        if (mismatch_pix2 != nullptr) *mismatch_pix2 = pix2;
    120        return false;
    121      }
    122    }
    123  }
    124 
    125  return true;
    126 }
    127 
    128 // The function should return "true" most of the time, therefore no early
    129 // break-out is implemented within the match checking process.
    130 static bool compare_img(const aom_image_t *img1, const aom_image_t *img2,
    131                        int *const mismatch_row, int *const mismatch_col,
    132                        int *const mismatch_plane, int *const mismatch_pix1,
    133                        int *const mismatch_pix2) {
    134  if (img1->fmt != img2->fmt || img1->cp != img2->cp || img1->tc != img2->tc ||
    135      img1->mc != img2->mc || img1->d_w != img2->d_w ||
    136      img1->d_h != img2->d_h || img1->monochrome != img2->monochrome) {
    137    if (mismatch_row != nullptr) *mismatch_row = -1;
    138    if (mismatch_col != nullptr) *mismatch_col = -1;
    139    return false;
    140  }
    141 
    142  const int num_planes = img1->monochrome ? 1 : 3;
    143  for (int plane = 0; plane < num_planes; plane++) {
    144    if (!compare_plane(img1->planes[plane], img1->stride[plane],
    145                       img2->planes[plane], img2->stride[plane],
    146                       aom_img_plane_width(img1, plane),
    147                       aom_img_plane_height(img1, plane), mismatch_row,
    148                       mismatch_col, mismatch_pix1, mismatch_pix2)) {
    149      if (mismatch_plane != nullptr) *mismatch_plane = plane;
    150      return false;
    151    }
    152  }
    153 
    154  return true;
    155 }
    156 
    157 void EncoderTest::MismatchHook(const aom_image_t *img_enc,
    158                               const aom_image_t *img_dec) {
    159  int mismatch_row = 0;
    160  int mismatch_col = 0;
    161  int mismatch_plane = 0;
    162  int mismatch_pix_enc = 0;
    163  int mismatch_pix_dec = 0;
    164 
    165  ASSERT_FALSE(compare_img(img_enc, img_dec, &mismatch_row, &mismatch_col,
    166                           &mismatch_plane, &mismatch_pix_enc,
    167                           &mismatch_pix_dec));
    168 
    169  GTEST_FAIL() << "Encode/Decode mismatch found:" << std::endl
    170               << "  pixel value enc/dec: " << mismatch_pix_enc << "/"
    171               << mismatch_pix_dec << std::endl
    172               << "                plane: " << mismatch_plane << std::endl
    173               << "              row/col: " << mismatch_row << "/"
    174               << mismatch_col << std::endl;
    175 }
    176 
    177 void EncoderTest::RunLoop(VideoSource *video) {
    178  stats_.Reset();
    179 
    180  ASSERT_TRUE(passes_ == 1 || passes_ == 2);
    181  for (unsigned int pass = 0; pass < passes_; pass++) {
    182    aom_codec_pts_t last_pts = 0;
    183 
    184    if (passes_ == 1)
    185      cfg_.g_pass = AOM_RC_ONE_PASS;
    186    else if (pass == 0)
    187      cfg_.g_pass = AOM_RC_FIRST_PASS;
    188    else
    189      cfg_.g_pass = AOM_RC_LAST_PASS;
    190 
    191    BeginPassHook(pass);
    192    std::unique_ptr<Encoder> encoder(
    193        codec_->CreateEncoder(cfg_, init_flags_, &stats_));
    194    ASSERT_NE(encoder, nullptr);
    195 
    196    ASSERT_NO_FATAL_FAILURE(video->Begin());
    197    encoder->InitEncoder(video);
    198 
    199    if (mode_ == kRealTime) {
    200      encoder->Control(AOME_SET_ENABLEAUTOALTREF, 0);
    201    }
    202 
    203    ASSERT_FALSE(::testing::Test::HasFatalFailure());
    204 #if CONFIG_AV1_DECODER
    205    aom_codec_dec_cfg_t dec_cfg = aom_codec_dec_cfg_t();
    206    dec_cfg.allow_lowbitdepth = 1;
    207    std::unique_ptr<Decoder> decoder(
    208        codec_->CreateDecoder(dec_cfg, 0 /* flags */));
    209    if (decoder->IsAV1()) {
    210      // Set dec_cfg.tile_row = -1 and dec_cfg.tile_col = -1 so that the whole
    211      // frame is decoded.
    212      decoder->Control(AV1_SET_TILE_MODE, cfg_.large_scale_tile);
    213      decoder->Control(AV1D_EXT_TILE_DEBUG, 1);
    214      decoder->Control(AV1_SET_DECODE_TILE_ROW, -1);
    215      decoder->Control(AV1_SET_DECODE_TILE_COL, -1);
    216    }
    217 #endif
    218 
    219    int number_spatial_layers = GetNumSpatialLayers();
    220 
    221    bool again;
    222    for (again = true; again; video->Next()) {
    223      again = (video->img() != nullptr);
    224 
    225      for (int sl = 0; sl < number_spatial_layers; sl++) {
    226        PreEncodeFrameHook(video, encoder.get());
    227        encoder->EncodeFrame(video, frame_flags_);
    228        PostEncodeFrameHook(encoder.get());
    229        CxDataIterator iter = encoder->GetCxData();
    230        bool has_cxdata = false;
    231 
    232 #if CONFIG_AV1_DECODER
    233        bool has_dxdata = false;
    234 #endif
    235        while (const aom_codec_cx_pkt_t *pkt = iter.Next()) {
    236          pkt = MutateEncoderOutputHook(pkt);
    237          again = true;
    238          switch (pkt->kind) {
    239            case AOM_CODEC_CX_FRAME_PKT:  //
    240              has_cxdata = true;
    241 #if CONFIG_AV1_DECODER
    242              if (decoder.get() != nullptr && DoDecode()) {
    243                aom_codec_err_t res_dec;
    244                if (DoDecodeInvisible()) {
    245                  res_dec = decoder->DecodeFrame(
    246                      (const uint8_t *)pkt->data.frame.buf, pkt->data.frame.sz);
    247                } else {
    248                  res_dec = decoder->DecodeFrame(
    249                      (const uint8_t *)pkt->data.frame.buf +
    250                          (pkt->data.frame.sz - pkt->data.frame.vis_frame_size),
    251                      pkt->data.frame.vis_frame_size);
    252                }
    253 
    254                if (!HandleDecodeResult(res_dec, decoder.get())) break;
    255 
    256                has_dxdata = true;
    257              }
    258 #endif
    259              ASSERT_GE(pkt->data.frame.pts, last_pts);
    260              if (sl == number_spatial_layers - 1)
    261                last_pts = pkt->data.frame.pts;
    262              FramePktHook(pkt);
    263              break;
    264 
    265            case AOM_CODEC_PSNR_PKT: PSNRPktHook(pkt); break;
    266 
    267            case AOM_CODEC_STATS_PKT: StatsPktHook(pkt); break;
    268 
    269            default: break;
    270          }
    271        }
    272        if (has_cxdata) {
    273          const aom_image_t *img_enc = encoder->GetPreviewFrame();
    274          if (img_enc) {
    275            CalculateFrameLevelSSIM(video->img(), img_enc, cfg_.g_bit_depth,
    276                                    cfg_.g_input_bit_depth);
    277          }
    278 #if CONFIG_AV1_DECODER
    279          if (has_dxdata) {
    280            DxDataIterator dec_iter = decoder->GetDxData();
    281            const aom_image_t *img_dec = dec_iter.Next();
    282            if (img_enc && img_dec) {
    283              const bool res = compare_img(img_enc, img_dec, nullptr, nullptr,
    284                                           nullptr, nullptr, nullptr);
    285              if (!res) {  // Mismatch
    286                MismatchHook(img_enc, img_dec);
    287              }
    288            }
    289            if (img_dec) DecompressedFrameHook(*img_dec, video->pts());
    290          }
    291 #endif
    292        }
    293        if (!Continue()) break;
    294      }  // Loop over spatial layers
    295    }
    296 
    297    EndPassHook();
    298 
    299    if (!Continue()) break;
    300  }
    301 }
    302 
    303 }  // namespace libaom_test