encode_test_driver.cc 11.5 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
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 "vpx_ports/mem.h"
17
#include "test/codec_factory.h"
18
#include "test/decode_test_driver.h"
19
#include "test/encode_test_driver.h"
20
#include "test/register_state_check.h"
John Koleszar's avatar
John Koleszar committed
21
22
23
#include "test/video_source.h"

namespace libvpx_test {
24
25
26
27
28
29
30
31
32
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();
33

34
35
36
    res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_,
                             init_flags_);
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
37

38
39
40
#if CONFIG_VP9_ENCODER
    if (CodecInterface() == &vpx_codec_vp9_cx_algo) {
      // Default to 1 tile column for VP9.
41
42
43
44
      const int log2_tile_columns = 0;
      res = vpx_codec_control_(&encoder_, VP9E_SET_TILE_COLUMNS,
                               log2_tile_columns);
      ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
45
    } else
46
47
48
#endif
#if CONFIG_VP10_ENCODER
    if (CodecInterface() == &vpx_codec_vp10_cx_algo) {
49
50
51
      // Default to 1 tile column for VP10. With CONFIG_EXT_TILE, the
      // default is already the largest possible tile size
#if !CONFIG_EXT_TILE
52
53
54
55
      const int log2_tile_columns = 0;
      res = vpx_codec_control_(&encoder_, VP9E_SET_TILE_COLUMNS,
                               log2_tile_columns);
      ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
56
#endif  // !CONFIG_EXT_TILE
57
    } else
58
#endif
Yaowu Xu's avatar
Yaowu Xu committed
59
    {
60
#if CONFIG_VP8_ENCODER
Yaowu Xu's avatar
Yaowu Xu committed
61
62
63
64
      if (CodecInterface() == &vpx_codec_vp8_cx_algo) {
        ASSERT_EQ(&vpx_codec_vp8_cx_algo, CodecInterface())
            << "Unknown Codec Interface";
      }
65
#endif
66
    }
67
68
69
  }
}

70
void Encoder::EncodeFrame(VideoSource *video, const unsigned long frame_flags) {
John Koleszar's avatar
John Koleszar committed
71
  if (video->img())
72
    EncodeFrameInternal(*video, frame_flags);
John Koleszar's avatar
John Koleszar committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  else
    Flush();

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

  while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
    if (pkt->kind != VPX_CODEC_STATS_PKT)
      continue;

    stats_->Append(*pkt);
  }
}

void Encoder::EncodeFrameInternal(const VideoSource &video,
88
                                  const unsigned long frame_flags) {
John Koleszar's avatar
John Koleszar committed
89
90
91
92
93
94
95
96
97
98
99
100
  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
101
  API_REGISTER_STATE_CHECK(
102
      res = vpx_codec_encode(&encoder_, img, video.pts(), video.duration(),
103
                             frame_flags, deadline_));
John Koleszar's avatar
John Koleszar committed
104
105
106
107
108
109
  ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
}

void Encoder::Flush() {
  const vpx_codec_err_t res = vpx_codec_encode(&encoder_, NULL, 0, 0, 0,
                                               deadline_);
110
111
112
113
  if (!encoder_.priv)
    ASSERT_EQ(VPX_CODEC_ERROR, res) << EncoderError();
  else
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
John Koleszar's avatar
John Koleszar committed
114
115
}

116
117
void EncoderTest::InitializeConfig() {
  const vpx_codec_err_t res = codec_->DefaultEncoderConfig(&cfg_, 0);
118
  dec_cfg_ = vpx_codec_dec_cfg_t();
119
120
121
  ASSERT_EQ(VPX_CODEC_OK, res);
}

John Koleszar's avatar
John Koleszar committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
void EncoderTest::SetMode(TestMode mode) {
  switch (mode) {
    case kRealTime:
      deadline_ = VPX_DL_REALTIME;
      break;

    case kOnePassGood:
    case kTwoPassGood:
      deadline_ = VPX_DL_GOOD_QUALITY;
      break;

    case kOnePassBest:
    case kTwoPassBest:
      deadline_ = VPX_DL_BEST_QUALITY;
      break;

    default:
      ASSERT_TRUE(false) << "Unexpected mode " << mode;
  }

  if (mode == kTwoPassGood || mode == kTwoPassBest)
    passes_ = 2;
  else
    passes_ = 1;
}
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

static bool compare_plane(const uint8_t *const buf1, const int stride1,
                          const uint8_t *const buf2, const int stride2,
                          const int w, const int h,
                          int *const mismatch_row,
                          int *const mismatch_col,
                          int *const mismatch_pix1,
                          int *const mismatch_pix2) {
  int r, c;

  for (r = 0; r < h; ++r) {
    for (c = 0; c < w; ++c) {
      const int pix1 = buf1[r * stride1 + c];
      const int pix2 = buf2[r * stride2 + c];

      if (pix1 != pix2) {
        if (mismatch_row != NULL)
          *mismatch_row = r;
        if (mismatch_col != NULL)
          *mismatch_col = c;
        if (mismatch_pix1 != NULL)
          *mismatch_pix1 = pix1;
        if (mismatch_pix2 != NULL)
          *mismatch_pix2 = pix2;
        return false;
      }
    }
  }

  return true;
}

179
180
181
// The function should return "true" most of the time, therefore no early
// break-out is implemented within the match checking process.
static bool compare_img(const vpx_image_t *img1,
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
                        const vpx_image_t *img2,
                        int *const mismatch_row,
                        int *const mismatch_col,
                        int *const mismatch_plane,
                        int *const mismatch_pix1,
                        int *const mismatch_pix2) {

  const unsigned int w_y = img1->d_w;
  const unsigned int h_y = img1->d_h;
  const unsigned int w_uv = ROUNDZ_POWER_OF_TWO(w_y, img1->x_chroma_shift);
  const unsigned int h_uv = ROUNDZ_POWER_OF_TWO(h_y, img1->y_chroma_shift);

  if (img1->fmt != img2->fmt
      || img1->cs != img2->cs
      || img1->d_w != img2->d_w
      || img1->d_h != img2->d_h) {
    if (mismatch_row != NULL)
      *mismatch_row = -1;
    if (mismatch_col != NULL)
      *mismatch_col = -1;
    return false;
  }

  if (!compare_plane(img1->planes[VPX_PLANE_Y],  img1->stride[VPX_PLANE_Y],
                     img2->planes[VPX_PLANE_Y],  img2->stride[VPX_PLANE_Y],
                     w_y, h_y,
                     mismatch_row, mismatch_col,
                     mismatch_pix1, mismatch_pix2)) {
    if (mismatch_plane != NULL)
      *mismatch_plane = VPX_PLANE_Y;
    return false;
  }

  if (!compare_plane(img1->planes[VPX_PLANE_U],  img1->stride[VPX_PLANE_U],
                     img2->planes[VPX_PLANE_U],  img2->stride[VPX_PLANE_U],
                     w_uv, h_uv,
                     mismatch_row, mismatch_col,
                     mismatch_pix1, mismatch_pix2)) {
    if (mismatch_plane != NULL)
      *mismatch_plane = VPX_PLANE_U;
    return false;
  }

  if (!compare_plane(img1->planes[VPX_PLANE_V],  img1->stride[VPX_PLANE_V],
                     img2->planes[VPX_PLANE_V],  img2->stride[VPX_PLANE_V],
                     w_uv, h_uv,
                     mismatch_row, mismatch_col,
                     mismatch_pix1, mismatch_pix2)) {
    if (mismatch_plane != NULL)
      *mismatch_plane = VPX_PLANE_U;
    return false;
  }

  return true;
236
}
John Koleszar's avatar
John Koleszar committed
237

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
void EncoderTest::MismatchHook(const vpx_image_t* img_enc,
                               const vpx_image_t* img_dec) {
  int mismatch_row;
  int mismatch_col;
  int mismatch_plane;
  int mismatch_pix_enc;
  int mismatch_pix_dec;

  ASSERT_FALSE(compare_img(img_enc, img_dec,
                           &mismatch_row, &mismatch_col,
                           &mismatch_plane,
                           &mismatch_pix_enc,
                           &mismatch_pix_dec));

  GTEST_FAIL()
    << "Encode/Decode mismatch found:"
    << std::endl
    << "  pixel value enc/dec: "  << mismatch_pix_enc << "/" << mismatch_pix_dec
    << std::endl
    << "                plane: " << mismatch_plane
    << std::endl
    << "              row/col: " << mismatch_row << "/" << mismatch_col
    << std::endl;
261
262
}

John Koleszar's avatar
John Koleszar committed
263
void EncoderTest::RunLoop(VideoSource *video) {
264
265
  vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();

266
267
  stats_.Reset();

268
  ASSERT_TRUE(passes_ == 1 || passes_ == 2);
John Koleszar's avatar
John Koleszar committed
269
  for (unsigned int pass = 0; pass < passes_; pass++) {
270
271
    last_pts_ = 0;

John Koleszar's avatar
John Koleszar committed
272
273
274
275
276
277
278
279
    if (passes_ == 1)
      cfg_.g_pass = VPX_RC_ONE_PASS;
    else if (pass == 0)
      cfg_.g_pass = VPX_RC_FIRST_PASS;
    else
      cfg_.g_pass = VPX_RC_LAST_PASS;

    BeginPassHook(pass);
280
281
282
    Encoder* const encoder = codec_->CreateEncoder(cfg_, deadline_, init_flags_,
                                                   &stats_);
    ASSERT_TRUE(encoder != NULL);
283

284
285
    video->Begin();
    encoder->InitEncoder(video);
286
    ASSERT_FALSE(::testing::Test::HasFatalFailure());
287

288
289
290
291
292
293
    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.
    if (init_flags_ & VPX_CODEC_USE_OUTPUT_PARTITION)
      dec_init_flags |= VPX_CODEC_USE_INPUT_FRAGMENTS;
    Decoder* const decoder = codec_->CreateDecoder(dec_cfg, dec_init_flags, 0);
294
295
296
297
298
299
300
301
302
#if CONFIG_VP10 && CONFIG_EXT_TILE
    if (decoder->IsVP10()) {
      // Set dec_cfg.tile_row = -1 and dec_cfg.tile_col = -1 so that the whole
      // frame is decoded.
      decoder->Control(VP10_SET_DECODE_TILE_ROW, -1);
      decoder->Control(VP10_SET_DECODE_TILE_COL, -1);
    }
#endif

John Koleszar's avatar
John Koleszar committed
303
    bool again;
304
    for (again = true; again; video->Next()) {
305
      again = (video->img() != NULL);
John Koleszar's avatar
John Koleszar committed
306
307

      PreEncodeFrameHook(video);
308
309
      PreEncodeFrameHook(video, encoder);
      encoder->EncodeFrame(video, frame_flags_);
John Koleszar's avatar
John Koleszar committed
310

311
      CxDataIterator iter = encoder->GetCxData();
John Koleszar's avatar
John Koleszar committed
312

313
314
      bool has_cxdata = false;
      bool has_dxdata = false;
John Koleszar's avatar
John Koleszar committed
315
      while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
John Koleszar's avatar
John Koleszar committed
316
        pkt = MutateEncoderOutputHook(pkt);
John Koleszar's avatar
John Koleszar committed
317
        again = true;
318
319
320
        switch (pkt->kind) {
          case VPX_CODEC_CX_FRAME_PKT:
            has_cxdata = true;
321
            if (decoder && DoDecode()) {
322
323
              vpx_codec_err_t res_dec = decoder->DecodeFrame(
                  (const uint8_t*)pkt->data.frame.buf, pkt->data.frame.sz);
324
325
326
327

              if (!HandleDecodeResult(res_dec, *video, decoder))
                break;

328
329
              has_dxdata = true;
            }
330
331
332
333
334
335
336
337
338
339
340
341
            ASSERT_GE(pkt->data.frame.pts, last_pts_);
            last_pts_ = pkt->data.frame.pts;
            FramePktHook(pkt);
            break;

          case VPX_CODEC_PSNR_PKT:
            PSNRPktHook(pkt);
            break;

          default:
            break;
        }
John Koleszar's avatar
John Koleszar committed
342
343
      }

344
345
346
347
348
349
350
      // 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);
        if (!HandleDecodeResult(res_dec, *video, decoder))
          break;
      }

351
      if (has_dxdata && has_cxdata) {
352
353
        const vpx_image_t *img_enc = encoder->GetPreviewFrame();
        DxDataIterator dec_iter = decoder->GetDxData();
354
        const vpx_image_t *img_dec = dec_iter.Next();
355
        if (img_enc && img_dec) {
356
357
          const bool res = compare_img(img_enc, img_dec,
                                       NULL, NULL, NULL, NULL, NULL);
358
359
360
          if (!res) {  // Mismatch
            MismatchHook(img_enc, img_dec);
          }
361
        }
362
363
        if (img_dec)
          DecompressedFrameHook(*img_dec, video->pts());
364
      }
John Koleszar's avatar
John Koleszar committed
365
366
367
368
369
370
      if (!Continue())
        break;
    }

    EndPassHook();

371
372
373
374
    if (decoder)
      delete decoder;
    delete encoder;

John Koleszar's avatar
John Koleszar committed
375
376
377
378
    if (!Continue())
      break;
  }
}
379

John Koleszar's avatar
John Koleszar committed
380
}  // namespace libvpx_test