encode_test_driver.cc 8.29 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
10

11 12
#include <string>

13 14
#include "third_party/googletest/src/include/gtest/gtest.h"

Yaowu Xu's avatar
Yaowu Xu committed
15
#include "./vpx_config.h"
16
#include "test/codec_factory.h"
17
#include "test/decode_test_driver.h"
18
#include "test/encode_test_driver.h"
19
#include "test/register_state_check.h"
20 21 22
#include "test/video_source.h"

namespace libvpx_test {
23 24 25 26 27 28 29 30 31
void Encoder::InitEncoder(VideoSource *video) {
  vpx_codec_err_t res;
  const vpx_image_t *img = video->img();

  if (video->img() && !encoder_.priv) {
    cfg_.g_w = img->d_w;
    cfg_.g_h = img->d_h;
    cfg_.g_timebase = video->timebase();
    cfg_.rc_twopass_stats_in = stats_->buf();
32

clang-format's avatar
clang-format committed
33
    res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_, init_flags_);
34
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
35

36 37 38
#if CONFIG_VP9_ENCODER
    if (CodecInterface() == &vpx_codec_vp9_cx_algo) {
      // Default to 1 tile column for VP9.
39
      const int log2_tile_columns = 0;
40 41 42 43
      res = vpx_codec_control_(&encoder_, VP9E_SET_TILE_COLUMNS,
                               log2_tile_columns);
      ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
    } else
44 45 46 47 48 49
#endif
    {
#if CONFIG_VP8_ENCODER
      ASSERT_EQ(&vpx_codec_vp8_cx_algo, CodecInterface())
          << "Unknown Codec Interface";
#endif
50
    }
51 52 53
  }
}

54
void Encoder::EncodeFrame(VideoSource *video, const unsigned long frame_flags) {
55
  if (video->img()) {
56
    EncodeFrameInternal(*video, frame_flags);
57
  } else {
58
    Flush();
59
  }
60 61 62 63 64

  // Handle twopass stats
  CxDataIterator iter = GetCxData();

  while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
clang-format's avatar
clang-format committed
65
    if (pkt->kind != VPX_CODEC_STATS_PKT) continue;
66 67 68 69 70 71

    stats_->Append(*pkt);
  }
}

void Encoder::EncodeFrameInternal(const VideoSource &video,
72
                                  const unsigned long frame_flags) {
73 74 75 76 77 78 79 80 81 82 83 84
  vpx_codec_err_t res;
  const vpx_image_t *img = video.img();

  // Handle frame resizing
  if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
    cfg_.g_w = img->d_w;
    cfg_.g_h = img->d_h;
    res = vpx_codec_enc_config_set(&encoder_, &cfg_);
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  }

  // Encode the frame
clang-format's avatar
clang-format committed
85 86 87
  API_REGISTER_STATE_CHECK(res = vpx_codec_encode(&encoder_, img, video.pts(),
                                                  video.duration(), frame_flags,
                                                  deadline_));
88 89 90 91
  ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
}

void Encoder::Flush() {
clang-format's avatar
clang-format committed
92 93
  const vpx_codec_err_t res =
      vpx_codec_encode(&encoder_, NULL, 0, 0, 0, deadline_);
94 95 96 97
  if (!encoder_.priv)
    ASSERT_EQ(VPX_CODEC_ERROR, res) << EncoderError();
  else
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
98 99
}

100 101
void EncoderTest::InitializeConfig() {
  const vpx_codec_err_t res = codec_->DefaultEncoderConfig(&cfg_, 0);
102
  dec_cfg_ = vpx_codec_dec_cfg_t();
103 104 105
  ASSERT_EQ(VPX_CODEC_OK, res);
}

106 107
void EncoderTest::SetMode(TestMode mode) {
  switch (mode) {
clang-format's avatar
clang-format committed
108
    case kRealTime: deadline_ = VPX_DL_REALTIME; break;
109 110

    case kOnePassGood:
clang-format's avatar
clang-format committed
111
    case kTwoPassGood: deadline_ = VPX_DL_GOOD_QUALITY; break;
112 113

    case kOnePassBest:
clang-format's avatar
clang-format committed
114
    case kTwoPassBest: deadline_ = VPX_DL_BEST_QUALITY; break;
115

clang-format's avatar
clang-format committed
116
    default: ASSERT_TRUE(false) << "Unexpected mode " << mode;
117 118
  }

119
  if (mode == kTwoPassGood || mode == kTwoPassBest) {
120
    passes_ = 2;
121
  } else {
122
    passes_ = 1;
123
  }
124
}
125 126
// The function should return "true" most of the time, therefore no early
// break-out is implemented within the match checking process.
clang-format's avatar
clang-format committed
127 128 129 130 131
static bool compare_img(const vpx_image_t *img1, const vpx_image_t *img2) {
  bool match = (img1->fmt == img2->fmt) && (img1->cs == img2->cs) &&
               (img1->d_w == img2->d_w) && (img1->d_h == img2->d_h);

  const unsigned int width_y = img1->d_w;
132 133
  const unsigned int height_y = img1->d_h;
  unsigned int i;
134
  for (i = 0; i < height_y; ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
135 136
    match = (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y],
                    img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y],
clang-format's avatar
clang-format committed
137 138
                    width_y) == 0) &&
            match;
139
  }
clang-format's avatar
clang-format committed
140
  const unsigned int width_uv = (img1->d_w + 1) >> 1;
141
  const unsigned int height_uv = (img1->d_h + 1) >> 1;
142
  for (i = 0; i < height_uv; ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
143 144
    match = (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U],
                    img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U],
clang-format's avatar
clang-format committed
145 146
                    width_uv) == 0) &&
            match;
147 148
  }
  for (i = 0; i < height_uv; ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
149 150
    match = (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V],
                    img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V],
clang-format's avatar
clang-format committed
151 152
                    width_uv) == 0) &&
            match;
153
  }
154 155
  return match;
}
156

clang-format's avatar
clang-format committed
157 158
void EncoderTest::MismatchHook(const vpx_image_t * /*img1*/,
                               const vpx_image_t * /*img2*/) {
159 160 161
  ASSERT_TRUE(0) << "Encode/Decode mismatch found";
}

162
void EncoderTest::RunLoop(VideoSource *video) {
163 164
  vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();

165 166
  stats_.Reset();

167
  ASSERT_TRUE(passes_ == 1 || passes_ == 2);
168
  for (unsigned int pass = 0; pass < passes_; pass++) {
169 170
    last_pts_ = 0;

171
    if (passes_ == 1) {
172
      cfg_.g_pass = VPX_RC_ONE_PASS;
173
    } else if (pass == 0) {
174
      cfg_.g_pass = VPX_RC_FIRST_PASS;
175
    } else {
176
      cfg_.g_pass = VPX_RC_LAST_PASS;
177
    }
178 179

    BeginPassHook(pass);
180 181 182
    testing::internal::scoped_ptr<Encoder> encoder(
        codec_->CreateEncoder(cfg_, deadline_, init_flags_, &stats_));
    ASSERT_TRUE(encoder.get() != NULL);
183

184
    ASSERT_NO_FATAL_FAILURE(video->Begin());
185
    encoder->InitEncoder(video);
186
    ASSERT_FALSE(::testing::Test::HasFatalFailure());
187

188 189 190
    unsigned long dec_init_flags = 0;  // NOLINT
    // Use fragment decoder if encoder outputs partitions.
    // NOTE: fragment decoder and partition encoder are only supported by VP8.
191
    if (init_flags_ & VPX_CODEC_USE_OUTPUT_PARTITION) {
192
      dec_init_flags |= VPX_CODEC_USE_INPUT_FRAGMENTS;
193
    }
194
    testing::internal::scoped_ptr<Decoder> decoder(
195
        codec_->CreateDecoder(dec_cfg, dec_init_flags));
196
    bool again;
197
    for (again = true; again; video->Next()) {
198
      again = (video->img() != NULL);
199 200

      PreEncodeFrameHook(video);
201
      PreEncodeFrameHook(video, encoder.get());
202
      encoder->EncodeFrame(video, frame_flags_);
203

204 205
      PostEncodeFrameHook(encoder.get());

206
      CxDataIterator iter = encoder->GetCxData();
207

208 209
      bool has_cxdata = false;
      bool has_dxdata = false;
210
      while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
John Koleszar's avatar
John Koleszar committed
211
        pkt = MutateEncoderOutputHook(pkt);
212
        again = true;
213 214 215
        switch (pkt->kind) {
          case VPX_CODEC_CX_FRAME_PKT:
            has_cxdata = true;
216
            if (decoder.get() != NULL && DoDecode()) {
217
              vpx_codec_err_t res_dec = decoder->DecodeFrame(
clang-format's avatar
clang-format committed
218
                  (const uint8_t *)pkt->data.frame.buf, pkt->data.frame.sz);
219

clang-format's avatar
clang-format committed
220
              if (!HandleDecodeResult(res_dec, *video, decoder.get())) break;
221

222 223
              has_dxdata = true;
            }
224 225 226 227 228
            ASSERT_GE(pkt->data.frame.pts, last_pts_);
            last_pts_ = pkt->data.frame.pts;
            FramePktHook(pkt);
            break;

clang-format's avatar
clang-format committed
229
          case VPX_CODEC_PSNR_PKT: PSNRPktHook(pkt); break;
230

231 232
          case VPX_CODEC_STATS_PKT: StatsPktHook(pkt); break;

clang-format's avatar
clang-format committed
233
          default: break;
234
        }
235 236
      }

237 238 239
      // Flush the decoder when there are no more fragments.
      if ((init_flags_ & VPX_CODEC_USE_OUTPUT_PARTITION) && has_dxdata) {
        const vpx_codec_err_t res_dec = decoder->DecodeFrame(NULL, 0);
clang-format's avatar
clang-format committed
240
        if (!HandleDecodeResult(res_dec, *video, decoder.get())) break;
241 242
      }

243
      if (has_dxdata && has_cxdata) {
244 245
        const vpx_image_t *img_enc = encoder->GetPreviewFrame();
        DxDataIterator dec_iter = decoder->GetDxData();
246
        const vpx_image_t *img_dec = dec_iter.Next();
247
        if (img_enc && img_dec) {
248
          const bool res = compare_img(img_enc, img_dec);
249 250 251
          if (!res) {  // Mismatch
            MismatchHook(img_enc, img_dec);
          }
252
        }
clang-format's avatar
clang-format committed
253
        if (img_dec) DecompressedFrameHook(*img_dec, video->pts());
254
      }
clang-format's avatar
clang-format committed
255
      if (!Continue()) break;
256 257 258 259
    }

    EndPassHook();

clang-format's avatar
clang-format committed
260
    if (!Continue()) break;
261 262
  }
}
263

264
}  // namespace libvpx_test