svc_test.cc 17.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 *  Copyright (c) 2013 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.
 */

#include <string>
#include "third_party/googletest/src/include/gtest/gtest.h"
#include "test/codec_factory.h"
#include "test/decode_test_driver.h"
#include "test/i420_video_source.h"
#include "vpx/svc_context.h"
#include "vpx/vp8cx.h"
#include "vpx/vpx_encoder.h"

namespace {

using libvpx_test::CodecFactory;
using libvpx_test::Decoder;
using libvpx_test::VP9CodecFactory;

class SvcTest : public ::testing::Test {
 protected:
  static const uint32_t kWidth = 352;
  static const uint32_t kHeight = 288;

  SvcTest()
      : codec_iface_(0),
        test_file_name_("hantro_collage_w352h288.yuv"),
34
35
36
37
38
39
        codec_initialized_(false),
        decoder_(0) {
    memset(&svc_, 0, sizeof(svc_));
    memset(&codec_, 0, sizeof(codec_));
    memset(&codec_enc_, 0, sizeof(codec_enc_));
  }
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

  virtual ~SvcTest() {}

  virtual void SetUp() {
    svc_.encoding_mode = INTER_LAYER_PREDICTION_IP;
    svc_.log_level = SVC_LOG_DEBUG;
    svc_.log_print = 0;

    codec_iface_ = vpx_codec_vp9_cx();
    const vpx_codec_err_t res =
        vpx_codec_enc_config_default(codec_iface_, &codec_enc_, 0);
    EXPECT_EQ(VPX_CODEC_OK, res);

    codec_enc_.g_w = kWidth;
    codec_enc_.g_h = kHeight;
    codec_enc_.g_timebase.num = 1;
    codec_enc_.g_timebase.den = 60;
    codec_enc_.kf_min_dist = 100;
    codec_enc_.kf_max_dist = 100;

    vpx_codec_dec_cfg_t dec_cfg = {0};
    VP9CodecFactory codec_factory;
    decoder_ = codec_factory.CreateDecoder(dec_cfg, 0);
  }

  virtual void TearDown() {
    vpx_svc_release(&svc_);
67
68
    delete(decoder_);
    if (codec_initialized_) vpx_codec_destroy(&codec_);
69
70
71
72
73
74
75
  }

  SvcContext svc_;
  vpx_codec_ctx_t codec_;
  struct vpx_codec_enc_cfg codec_enc_;
  vpx_codec_iface_t *codec_iface_;
  std::string test_file_name_;
76
  bool codec_initialized_;
77
78
79
80
  Decoder *decoder_;
};

TEST_F(SvcTest, SvcInit) {
81
82
  // test missing parameters
  vpx_codec_err_t res = vpx_svc_init(NULL, &codec_, codec_iface_, &codec_enc_);
83
84
85
86
87
88
89
90
91
92
93
94
95
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  res = vpx_svc_init(&svc_, NULL, codec_iface_, &codec_enc_);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
  res = vpx_svc_init(&svc_, &codec_, NULL, &codec_enc_);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  res = vpx_svc_init(&svc_, &codec_, codec_iface_, NULL);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  svc_.spatial_layers = 6;  // too many layers
  res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

96
97
98
99
100
101
102
103
  svc_.spatial_layers = 0;  // use default layers
  res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
  codec_initialized_ = true;
  EXPECT_EQ(VPX_SS_DEFAULT_LAYERS, svc_.spatial_layers);
}

TEST_F(SvcTest, InitTwoLayers) {
104
105
  svc_.spatial_layers = 2;
  vpx_svc_set_scale_factors(&svc_, "4/16,16*16");  // invalid scale values
106
  vpx_codec_err_t res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
107
108
109
110
111
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  vpx_svc_set_scale_factors(&svc_, "4/16,16/16");  // valid scale values
  res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
112
  codec_initialized_ = true;
113
114
}

115
116
TEST_F(SvcTest, InvalidOptions) {
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, NULL);
117
118
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

119
120
121
  res = vpx_svc_set_options(&svc_, "not-an-option=1");
  EXPECT_EQ(VPX_CODEC_OK, res);
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
122
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
123
}
124

125
126
127
TEST_F(SvcTest, SetLayersOption) {
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, "layers=3");
  EXPECT_EQ(VPX_CODEC_OK, res);
128
129
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
130
  codec_initialized_ = true;
131
  EXPECT_EQ(3, svc_.spatial_layers);
132
}
133

134
135
136
TEST_F(SvcTest, SetEncodingMode) {
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, "encoding-mode=alt-ip");
  EXPECT_EQ(VPX_CODEC_OK, res);
137
138
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
139
  codec_initialized_ = true;
140
  EXPECT_EQ(ALT_INTER_LAYER_PREDICTION_IP, svc_.encoding_mode);
141
}
142

143
144
TEST_F(SvcTest, SetMultipleOptions) {
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, "layers=2 encoding-mode=ip");
145
146
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
147
  codec_initialized_ = true;
148
149
  EXPECT_EQ(2, svc_.spatial_layers);
  EXPECT_EQ(INTER_LAYER_PREDICTION_IP, svc_.encoding_mode);
150
}
151

152
153
154
155
156
TEST_F(SvcTest, SetScaleFactorsOption) {
  svc_.spatial_layers = 2;
  vpx_codec_err_t res =
      vpx_svc_set_options(&svc_, "scale-factors=not-scale-factors");
  EXPECT_EQ(VPX_CODEC_OK, res);
157
158
159
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

160
161
  res = vpx_svc_set_options(&svc_, "scale-factors=1/3,2/3");
  EXPECT_EQ(VPX_CODEC_OK, res);
162
163
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
164
165
  codec_initialized_ = true;
}
166

167
168
169
170
TEST_F(SvcTest, SetQuantizersOption) {
  svc_.spatial_layers = 2;
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, "quantizers=not-quantizers");
  EXPECT_EQ(VPX_CODEC_OK, res);
171
172
173
174
175
176
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  vpx_svc_set_options(&svc_, "quantizers=40,45");
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
177
  codec_initialized_ = true;
178
179
}

180
181
182
183
184
185
186
187
188
189
190
191
192
193
TEST_F(SvcTest, SetKeyFrameQuantizersOption) {
  svc_.spatial_layers = 2;
  vpx_codec_err_t res = vpx_svc_set_options(&svc_,
                                       "quantizers-keyframe=not-quantizers");
  EXPECT_EQ(VPX_CODEC_OK, res);
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  vpx_svc_set_options(&svc_, "quantizers-keyframe=40,45");
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
  codec_initialized_ = true;
}

194
TEST_F(SvcTest, SetQuantizers) {
195
  vpx_codec_err_t res = vpx_svc_set_quantizers(NULL, "40,30", 0);
196
197
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

198
  res = vpx_svc_set_quantizers(&svc_, NULL, 0);
199
200
201
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  svc_.spatial_layers = 2;
202
  res = vpx_svc_set_quantizers(&svc_, "40", 0);
203
204
  EXPECT_EQ(VPX_CODEC_OK, res);
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
205
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
206

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  res = vpx_svc_set_quantizers(&svc_, "40,30", 0);
  EXPECT_EQ(VPX_CODEC_OK, res);
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
  codec_initialized_ = true;
}

TEST_F(SvcTest, SetKeyFrameQuantizers) {
  vpx_codec_err_t res = vpx_svc_set_quantizers(NULL, "40,31", 1);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  res = vpx_svc_set_quantizers(&svc_, NULL, 1);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  res = vpx_svc_set_quantizers(&svc_, "40,30", 1);
222
223
  EXPECT_EQ(VPX_CODEC_OK, res);
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
224
225
  EXPECT_EQ(VPX_CODEC_OK, res);
  codec_initialized_ = true;
226
227
228
229
230
231
232
233
234
235
}

TEST_F(SvcTest, SetScaleFactors) {
  vpx_codec_err_t res = vpx_svc_set_scale_factors(NULL, "4/16,16/16");
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  res = vpx_svc_set_scale_factors(&svc_, NULL);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  svc_.spatial_layers = 2;
236
  res = vpx_svc_set_scale_factors(&svc_, "4/16");
237
238
  EXPECT_EQ(VPX_CODEC_OK, res);
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
239
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
240

241
  res = vpx_svc_set_scale_factors(&svc_, "4/16,16/16");
242
243
  EXPECT_EQ(VPX_CODEC_OK, res);
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
244
245
  EXPECT_EQ(VPX_CODEC_OK, res);
  codec_initialized_ = true;
246
247
}

248
249
// Test that decoder can handle an SVC frame as the first frame in a sequence.
TEST_F(SvcTest, FirstFrameHasLayers) {
250
251
  svc_.spatial_layers = 2;
  vpx_svc_set_scale_factors(&svc_, "4/16,16/16");
252
  vpx_svc_set_quantizers(&svc_, "40,30", 0);
253
254
255
256

  vpx_codec_err_t res =
      vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
257
  codec_initialized_ = true;
258
259
260
261
262
263
264

  libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight,
                                     codec_enc_.g_timebase.den,
                                     codec_enc_.g_timebase.num, 0, 30);
  video.Begin();

  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
Jim Bankoski's avatar
Jim Bankoski committed
265
                       video.duration(), VPX_DL_GOOD_QUALITY);
266
267
  EXPECT_EQ(VPX_CODEC_OK, res);

268
269
270
271
272
273
274
275
276
  if (vpx_svc_get_frame_size(&svc_) == 0) {
    // Flush encoder
    res = vpx_svc_encode(&svc_, &codec_, NULL, 0,
                         video.duration(), VPX_DL_GOOD_QUALITY);
    EXPECT_EQ(VPX_CODEC_OK, res);
  }

  int frame_size = vpx_svc_get_frame_size(&svc_);
  EXPECT_GT(frame_size, 0);
277
  const vpx_codec_err_t res_dec = decoder_->DecodeFrame(
278
      static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
279
280
281
282
283
284
285
286

  // this test fails with a decoder error
  ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
}

TEST_F(SvcTest, EncodeThreeFrames) {
  svc_.spatial_layers = 2;
  vpx_svc_set_scale_factors(&svc_, "4/16,16/16");
287
  vpx_svc_set_quantizers(&svc_, "40,30", 0);
288
289
290
  int decoded_frames = 0;
  vpx_codec_err_t res_dec;
  int frame_size;
291
292
293
294

  vpx_codec_err_t res =
      vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  ASSERT_EQ(VPX_CODEC_OK, res);
295
  codec_initialized_ = true;
296
297
298
299

  libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight,
                                     codec_enc_.g_timebase.den,
                                     codec_enc_.g_timebase.num, 0, 30);
300
  // FRAME 0
301
  video.Begin();
302
  // This frame is a keyframe.
303
  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
Jim Bankoski's avatar
Jim Bankoski committed
304
                       video.duration(), VPX_DL_GOOD_QUALITY);
305

306
307
308
309
310
311
312
  if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
    EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
    res_dec = decoder_->DecodeFrame(
        static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
    ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
    ++decoded_frames;
  }
313

314
  // FRAME 1
315
  video.Next();
316
  // This is a P-frame.
317
  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
Jim Bankoski's avatar
Jim Bankoski committed
318
                       video.duration(), VPX_DL_GOOD_QUALITY);
319
320
  ASSERT_EQ(VPX_CODEC_OK, res);

321
322
323
324
325
326
327
  if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
    EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
    res_dec = decoder_->DecodeFrame(
        static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
    ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
    ++decoded_frames;
  }
328
329
330

  // FRAME 2
  video.Next();
331
  // This is a P-frame.
332
  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
Jim Bankoski's avatar
Jim Bankoski committed
333
                       video.duration(), VPX_DL_GOOD_QUALITY);
334
335
  ASSERT_EQ(VPX_CODEC_OK, res);

336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
    EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
    res_dec = decoder_->DecodeFrame(
        static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
    ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
    ++decoded_frames;
  }

  // Flush encoder
  res = vpx_svc_encode(&svc_, &codec_, NULL, 0,
                       video.duration(), VPX_DL_GOOD_QUALITY);
  EXPECT_EQ(VPX_CODEC_OK, res);

  while ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
    EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
    res_dec = decoder_->DecodeFrame(
        static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
    ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
    ++decoded_frames;
  }

  EXPECT_EQ(decoded_frames, 3);
358
359
360
361
362
}

TEST_F(SvcTest, GetLayerResolution) {
  svc_.spatial_layers = 2;
  vpx_svc_set_scale_factors(&svc_, "4/16,8/16");
363
  vpx_svc_set_quantizers(&svc_, "40,30", 0);
364
365
366
367

  vpx_codec_err_t res =
      vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_OK, res);
368
  codec_initialized_ = true;
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395

  // ensure that requested layer is a valid layer
  uint32_t layer_width, layer_height;
  res = vpx_svc_get_layer_resolution(&svc_, svc_.spatial_layers,
                                     &layer_width, &layer_height);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  res = vpx_svc_get_layer_resolution(NULL, 0, &layer_width, &layer_height);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  res = vpx_svc_get_layer_resolution(&svc_, 0, NULL, &layer_height);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  res = vpx_svc_get_layer_resolution(&svc_, 0, &layer_width, NULL);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  res = vpx_svc_get_layer_resolution(&svc_, 0, &layer_width, &layer_height);
  EXPECT_EQ(VPX_CODEC_OK, res);
  EXPECT_EQ(kWidth * 4 / 16, layer_width);
  EXPECT_EQ(kHeight * 4 / 16, layer_height);

  res = vpx_svc_get_layer_resolution(&svc_, 1, &layer_width, &layer_height);
  EXPECT_EQ(VPX_CODEC_OK, res);
  EXPECT_EQ(kWidth * 8 / 16, layer_width);
  EXPECT_EQ(kHeight * 8 / 16, layer_height);
}

396
397
398
TEST_F(SvcTest, TwoPassEncode) {
  // First pass encode
  std::string stats_buf;
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
  svc_.spatial_layers = 2;
  codec_enc_.g_pass = VPX_RC_FIRST_PASS;
  vpx_svc_set_scale_factors(&svc_, "4/16,16/16");
  vpx_svc_set_quantizers(&svc_, "40,30", 0);

  vpx_codec_err_t res =
      vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  ASSERT_EQ(VPX_CODEC_OK, res);
  codec_initialized_ = true;

  libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight,
                                     codec_enc_.g_timebase.den,
                                     codec_enc_.g_timebase.num, 0, 30);
  // FRAME 0
  video.Begin();
  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
                       video.duration(), VPX_DL_GOOD_QUALITY);
  ASSERT_EQ(VPX_CODEC_OK, res);
417
418
419
420
421
  size_t stats_size = vpx_svc_get_rc_stats_buffer_size(&svc_);
  EXPECT_GT(stats_size, 0U);
  const char *stats_data = vpx_svc_get_rc_stats_buffer(&svc_);
  ASSERT_TRUE(stats_data != NULL);
  stats_buf.append(stats_data, stats_size);
422
423
424
425
426

  // FRAME 1
  video.Next();
  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
                       video.duration(), VPX_DL_GOOD_QUALITY);
427
428
429
430
431
  stats_size = vpx_svc_get_rc_stats_buffer_size(&svc_);
  EXPECT_GT(stats_size, 0U);
  stats_data = vpx_svc_get_rc_stats_buffer(&svc_);
  ASSERT_TRUE(stats_data != NULL);
  stats_buf.append(stats_data, stats_size);
432
433
434
435

  // Flush encoder and test EOS packet
  res = vpx_svc_encode(&svc_, &codec_, NULL, video.pts(),
                       video.duration(), VPX_DL_GOOD_QUALITY);
436
437
438
439
440
  stats_size = vpx_svc_get_rc_stats_buffer_size(&svc_);
  EXPECT_GT(stats_size, 0U);
  stats_data = vpx_svc_get_rc_stats_buffer(&svc_);
  ASSERT_TRUE(stats_data != NULL);
  stats_buf.append(stats_data, stats_size);
441

442
443
444
  // Tear down encoder
  vpx_svc_release(&svc_);
  vpx_codec_destroy(&codec_);
445

446
  // Second pass encode
447
448
449
  int decoded_frames = 0;
  vpx_codec_err_t res_dec;
  int frame_size;
450
451
452
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  codec_enc_.rc_twopass_stats_in.buf = &stats_buf[0];
  codec_enc_.rc_twopass_stats_in.sz = stats_buf.size();
453

454
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
455
  ASSERT_EQ(VPX_CODEC_OK, res);
456
457
458
459
460
461
462
463
464
  codec_initialized_ = true;

  // FRAME 0
  video.Begin();
  // This frame is a keyframe.
  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
                       video.duration(), VPX_DL_GOOD_QUALITY);
  ASSERT_EQ(VPX_CODEC_OK, res);

465
466
467
468
469
470
471
  if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
    EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
    res_dec = decoder_->DecodeFrame(
        static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
    ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
    ++decoded_frames;
  }
472
473
474
475
476
477
478
479

  // FRAME 1
  video.Next();
  // This is a P-frame.
  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
                       video.duration(), VPX_DL_GOOD_QUALITY);
  ASSERT_EQ(VPX_CODEC_OK, res);

480
481
482
483
484
485
486
  if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
    EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
    res_dec = decoder_->DecodeFrame(
        static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
    ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
    ++decoded_frames;
  }
487
488
489
490
491
492
493
494

  // FRAME 2
  video.Next();
  // This is a P-frame.
  res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
                       video.duration(), VPX_DL_GOOD_QUALITY);
  ASSERT_EQ(VPX_CODEC_OK, res);

495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
    EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
    res_dec = decoder_->DecodeFrame(
        static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
    ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
    ++decoded_frames;
  }

  // Flush encoder
  res = vpx_svc_encode(&svc_, &codec_, NULL, 0,
                       video.duration(), VPX_DL_GOOD_QUALITY);
  EXPECT_EQ(VPX_CODEC_OK, res);

  while ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
    EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
    res_dec = decoder_->DecodeFrame(
        static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
    ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
    ++decoded_frames;
  }

  EXPECT_EQ(decoded_frames, 3);
517
518
}

519
}  // namespace