svc_test.cc 23.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 *  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"
16
17
18

#include "vp9/decoder/vp9_decoder.h"

19
20
21
22
23
24
25
26
#include "vpx/svc_context.h"
#include "vpx/vp8cx.h"
#include "vpx/vpx_encoder.h"

namespace {

using libvpx_test::CodecFactory;
using libvpx_test::Decoder;
27
using libvpx_test::DxDataIterator;
28
29
30
31
32
33
34
35
36
37
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"),
38
39
40
41
42
43
        codec_initialized_(false),
        decoder_(0) {
    memset(&svc_, 0, sizeof(svc_));
    memset(&codec_, 0, sizeof(codec_));
    memset(&codec_enc_, 0, sizeof(codec_enc_));
  }
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

  virtual ~SvcTest() {}

  virtual void SetUp() {
    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() {
69
    ReleaseEncoder();
70
    delete(decoder_);
71
72
73
74
75
76
77
78
79
80
81
  }

  void InitializeEncoder() {
    const vpx_codec_err_t res =
        vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
    EXPECT_EQ(VPX_CODEC_OK, res);
    codec_initialized_ = true;
  }

  void ReleaseEncoder() {
    vpx_svc_release(&svc_);
82
    if (codec_initialized_) vpx_codec_destroy(&codec_);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    codec_initialized_ = false;
  }

  void Pass1EncodeNFrames(const int n, const int layers,
                          std::string *const stats_buf) {
    vpx_codec_err_t res;
    size_t stats_size = 0;
    const char *stats_data = NULL;

    ASSERT_GT(n, 0);
    ASSERT_GT(layers, 0);
    svc_.spatial_layers = layers;
    codec_enc_.g_pass = VPX_RC_FIRST_PASS;
    InitializeEncoder();

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

    for (int i = 0; i < n; ++i) {
      res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
                           video.duration(), VPX_DL_GOOD_QUALITY);
      ASSERT_EQ(VPX_CODEC_OK, res);
      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);
      video.Next();
    }

115
    // Flush encoder and test EOS packet.
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    res = vpx_svc_encode(&svc_, &codec_, NULL, video.pts(),
                         video.duration(), VPX_DL_GOOD_QUALITY);
    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);

    ReleaseEncoder();
  }

  void StoreFrames(const size_t max_frame_received,
                   struct vpx_fixed_buf *const outputs,
                   size_t *const frame_received) {
    size_t frame_size;
    while ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
      ASSERT_LT(*frame_received, max_frame_received);

      if (*frame_received == 0) {
        EXPECT_EQ(1, vpx_svc_is_keyframe(&svc_));
      }

138
      outputs[*frame_received].buf = malloc(frame_size + 16);
139
140
141
142
143
144
145
146
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
      ASSERT_TRUE(outputs[*frame_received].buf != NULL);
      memcpy(outputs[*frame_received].buf, vpx_svc_get_buffer(&svc_),
             frame_size);
      outputs[*frame_received].sz = frame_size;
      ++(*frame_received);
    }
  }

  void Pass2EncodeNFrames(std::string *const stats_buf,
                          const int n, const int layers,
                          struct vpx_fixed_buf *const outputs) {
    vpx_codec_err_t res;
    size_t frame_received = 0;

    ASSERT_TRUE(outputs != NULL);
    ASSERT_GT(n, 0);
    ASSERT_GT(layers, 0);
    svc_.spatial_layers = layers;
    codec_enc_.rc_target_bitrate = 500;
    if (codec_enc_.g_pass == VPX_RC_LAST_PASS) {
      ASSERT_TRUE(stats_buf != NULL);
      ASSERT_GT(stats_buf->size(), 0U);
      codec_enc_.rc_twopass_stats_in.buf = &(*stats_buf)[0];
      codec_enc_.rc_twopass_stats_in.sz = stats_buf->size();
    }
    InitializeEncoder();

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

    for (int i = 0; i < n; ++i) {
      res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
                           video.duration(), VPX_DL_GOOD_QUALITY);
      ASSERT_EQ(VPX_CODEC_OK, res);
      StoreFrames(n, outputs, &frame_received);
      video.Next();
    }

179
    // Flush encoder.
180
181
182
183
184
    res = vpx_svc_encode(&svc_, &codec_, NULL, 0,
                         video.duration(), VPX_DL_GOOD_QUALITY);
    EXPECT_EQ(VPX_CODEC_OK, res);
    StoreFrames(n, outputs, &frame_received);

185
    EXPECT_EQ(frame_received, static_cast<size_t>(n));
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

    ReleaseEncoder();
  }

  void DecodeNFrames(const struct vpx_fixed_buf *const inputs, const int n) {
    int decoded_frames = 0;
    int received_frames = 0;

    ASSERT_TRUE(inputs != NULL);
    ASSERT_GT(n, 0);

    for (int i = 0; i < n; ++i) {
      ASSERT_TRUE(inputs[i].buf != NULL);
      ASSERT_GT(inputs[i].sz, 0U);
      const vpx_codec_err_t res_dec =
          decoder_->DecodeFrame(static_cast<const uint8_t *>(inputs[i].buf),
                                inputs[i].sz);
      ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
      ++decoded_frames;

      DxDataIterator dec_iter = decoder_->GetDxData();
207
      while (dec_iter.Next() != NULL) {
208
209
210
211
212
213
214
215
216
        ++received_frames;
      }
    }
    EXPECT_EQ(decoded_frames, n);
    EXPECT_EQ(received_frames, n);
  }

  void DropEnhancementLayers(struct vpx_fixed_buf *const inputs,
                             const int num_super_frames,
217
218
                             const int remained_layers,
                             const bool is_multiple_frame_context) {
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    ASSERT_TRUE(inputs != NULL);
    ASSERT_GT(num_super_frames, 0);
    ASSERT_GT(remained_layers, 0);

    for (int i = 0; i < num_super_frames; ++i) {
      uint32_t frame_sizes[8] = {0};
      int frame_count = 0;
      int frames_found = 0;
      int frame;
      ASSERT_TRUE(inputs[i].buf != NULL);
      ASSERT_GT(inputs[i].sz, 0U);

      vpx_codec_err_t res =
          vp9_parse_superframe_index(static_cast<const uint8_t*>(inputs[i].buf),
                                     inputs[i].sz, frame_sizes, &frame_count,
                                     NULL, NULL);
      ASSERT_EQ(VPX_CODEC_OK, res);

      uint8_t *frame_data = static_cast<uint8_t *>(inputs[i].buf);
      uint8_t *frame_start = frame_data;
      for (frame = 0; frame < frame_count; ++frame) {
240
        // Looking for a visible frame.
241
242
243
244
245
246
247
        if (frame_data[0] & 0x02) {
          ++frames_found;
          if (frames_found == remained_layers)
            break;
        }
        frame_data += frame_sizes[frame];
      }
248
249
250
251
252
      ASSERT_LT(frame, frame_count) << "Couldn't find a visible frame. "
          << "remaining_layers: " << remained_layers
          << "    super_frame: " << i
          << "    is_multiple_frame_context: " << is_multiple_frame_context;
      if (frame == frame_count - 1 && !is_multiple_frame_context)
253
254
255
        continue;

      frame_data += frame_sizes[frame];
256
257
258
      // We need to add one more frame for multiple frame context.
      if (is_multiple_frame_context)
        ++frame;
259
260
261
262
263
264
265
      uint8_t marker =
          static_cast<const uint8_t *>(inputs[i].buf)[inputs[i].sz - 1];
      const uint32_t mag = ((marker >> 3) & 0x3) + 1;
      const size_t index_sz = 2 + mag * frame_count;
      const size_t new_index_sz = 2 + mag * (frame + 1);
      marker &= 0x0f8;
      marker |= frame;
266
267
268
269
270
271
272
273
274

      // Copy existing frame sizes.
      memmove(frame_data + (is_multiple_frame_context ? 2 : 1),
              frame_start + inputs[i].sz - index_sz + 1, new_index_sz - 2);
      if (is_multiple_frame_context) {
        // Add a one byte frame with flag show_existing frame.
        *frame_data++ = 0x88 | (remained_layers - 1);
      }
      // New marker.
275
      frame_data[0] = marker;
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
      frame_data += (mag * (frame + 1) + 1);

      if (is_multiple_frame_context) {
        // Write the frame size for the one byte frame.
        frame_data -= mag;
        *frame_data++ = 1;
        for (uint32_t j = 1; j < mag; ++j) {
          *frame_data++ = 0;
        }
      }

      *frame_data++ = marker;
      inputs[i].sz = frame_data - frame_start;

      if (is_multiple_frame_context) {
        // Change the show frame flag to 0 for all frames.
        for (int j = 0; j < frame; ++j) {
          frame_start[0] &= ~2;
          frame_start += frame_sizes[j];
        }
      }
297
298
299
300
301
302
303
304
305
306
307
308
    }
  }

  void FreeBitstreamBuffers(struct vpx_fixed_buf *const inputs, const int n) {
    ASSERT_TRUE(inputs != NULL);
    ASSERT_GT(n, 0);

    for (int i = 0; i < n; ++i) {
      free(inputs[i].buf);
      inputs[i].buf = NULL;
      inputs[i].sz = 0;
    }
309
310
311
312
313
314
315
  }

  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_;
316
  bool codec_initialized_;
317
318
319
320
  Decoder *decoder_;
};

TEST_F(SvcTest, SvcInit) {
321
322
  // test missing parameters
  vpx_codec_err_t res = vpx_svc_init(NULL, &codec_, codec_iface_, &codec_enc_);
323
324
325
326
327
328
329
330
331
332
333
334
335
  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);

336
  svc_.spatial_layers = 0;  // use default layers
337
  InitializeEncoder();
338
339
340
341
  EXPECT_EQ(VPX_SS_DEFAULT_LAYERS, svc_.spatial_layers);
}

TEST_F(SvcTest, InitTwoLayers) {
342
343
  svc_.spatial_layers = 2;
  vpx_svc_set_scale_factors(&svc_, "4/16,16*16");  // invalid scale values
344
  vpx_codec_err_t res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
345
346
347
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  vpx_svc_set_scale_factors(&svc_, "4/16,16/16");  // valid scale values
348
  InitializeEncoder();
349
350
}

351
352
TEST_F(SvcTest, InvalidOptions) {
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, NULL);
353
354
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

355
356
357
  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_);
358
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
359
}
360

361
362
363
TEST_F(SvcTest, SetLayersOption) {
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, "layers=3");
  EXPECT_EQ(VPX_CODEC_OK, res);
364
  InitializeEncoder();
365
  EXPECT_EQ(3, svc_.spatial_layers);
366
}
367

368
TEST_F(SvcTest, SetMultipleOptions) {
369
370
  vpx_codec_err_t res =
      vpx_svc_set_options(&svc_, "layers=2 scale-factors=1/3,2/3");
371
  EXPECT_EQ(VPX_CODEC_OK, res);
372
  InitializeEncoder();
373
  EXPECT_EQ(2, svc_.spatial_layers);
374
}
375

376
377
378
379
380
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);
381
382
383
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

384
385
  res = vpx_svc_set_options(&svc_, "scale-factors=1/3,2/3");
  EXPECT_EQ(VPX_CODEC_OK, res);
386
  InitializeEncoder();
387
}
388

389
390
391
392
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);
393
394
395
396
  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");
397
  InitializeEncoder();
398
399
}

400
401
402
403
404
405
406
407
408
409
410
411
412
TEST_F(SvcTest, SetAutoAltRefOption) {
  svc_.spatial_layers = 5;
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, "auto-alt-refs=none");
  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);

  res = vpx_svc_set_options(&svc_, "auto-alt-refs=1,1,1,1,0");
  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_, "auto-alt-refs=0,1,1,1,0");
413
  InitializeEncoder();
414
415
}

416
TEST_F(SvcTest, SetQuantizers) {
417
  vpx_codec_err_t res = vpx_svc_set_quantizers(NULL, "40,30");
418
419
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

420
  res = vpx_svc_set_quantizers(&svc_, NULL);
421
422
423
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

  svc_.spatial_layers = 2;
424
  res = vpx_svc_set_quantizers(&svc_, "40");
425
426
  EXPECT_EQ(VPX_CODEC_OK, res);
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
427
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
428

429
  res = vpx_svc_set_quantizers(&svc_, "40,30");
430
  EXPECT_EQ(VPX_CODEC_OK, res);
431
  InitializeEncoder();
432
433
434
435
436
437
438
439
440
441
}

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;
442
  res = vpx_svc_set_scale_factors(&svc_, "4/16");
443
444
  EXPECT_EQ(VPX_CODEC_OK, res);
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
445
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
446

447
  res = vpx_svc_set_scale_factors(&svc_, "4/16,16/16");
448
  EXPECT_EQ(VPX_CODEC_OK, res);
449
  InitializeEncoder();
450
451
}

452
// Test that decoder can handle an SVC frame as the first frame in a sequence.
453
454
455
456
457
458
TEST_F(SvcTest, OnePassEncodeOneFrame) {
  codec_enc_.g_pass = VPX_RC_ONE_PASS;
  vpx_fixed_buf output = {0};
  Pass2EncodeNFrames(NULL, 1, 2, &output);
  DecodeNFrames(&output, 1);
  FreeBitstreamBuffers(&output, 1);
459
460
}

461
462
463
464
465
466
467
TEST_F(SvcTest, OnePassEncodeThreeFrames) {
  codec_enc_.g_pass = VPX_RC_ONE_PASS;
  vpx_fixed_buf outputs[3];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(NULL, 3, 2, &outputs[0]);
  DecodeNFrames(&outputs[0], 3);
  FreeBitstreamBuffers(&outputs[0], 3);
468
469
470
471
472
}

TEST_F(SvcTest, GetLayerResolution) {
  svc_.spatial_layers = 2;
  vpx_svc_set_scale_factors(&svc_, "4/16,8/16");
473
  vpx_svc_set_quantizers(&svc_, "40,30");
474

475
  InitializeEncoder();
476
477
478

  // ensure that requested layer is a valid layer
  uint32_t layer_width, layer_height;
479
  vpx_codec_err_t res = vpx_svc_get_layer_resolution(&svc_, svc_.spatial_layers,
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
                                     &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);
}

503
TEST_F(SvcTest, TwoPassEncode10Frames) {
504
505
  // First pass encode
  std::string stats_buf;
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  Pass1EncodeNFrames(10, 2, &stats_buf);

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}

TEST_F(SvcTest, TwoPassEncode20FramesWithAltRef) {
  // First pass encode
  std::string stats_buf;
  Pass1EncodeNFrames(20, 2, &stats_buf);

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
524
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
525
526
527
528
529
530
  vpx_fixed_buf outputs[20];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 20, 2, &outputs[0]);
  DecodeNFrames(&outputs[0], 20);
  FreeBitstreamBuffers(&outputs[0], 20);
}
531

532
533
534
535
TEST_F(SvcTest, TwoPassEncode2LayersDecodeBaseLayerOnly) {
  // First pass encode
  std::string stats_buf;
  Pass1EncodeNFrames(10, 2, &stats_buf);
536

537
538
  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
539
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
540
541
542
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
543
  DropEnhancementLayers(&outputs[0], 10, 1, false);
544
545
546
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}
547

548
549
550
551
TEST_F(SvcTest, TwoPassEncode5LayersDecode54321Layers) {
  // First pass encode
  std::string stats_buf;
  Pass1EncodeNFrames(10, 5, &stats_buf);
552

553
554
555
556
557
558
559
560
  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  vpx_svc_set_options(&svc_, "auto-alt-refs=0,1,1,1,0");
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 5, &outputs[0]);

  DecodeNFrames(&outputs[0], 10);
561
  DropEnhancementLayers(&outputs[0], 10, 4, false);
562
  DecodeNFrames(&outputs[0], 10);
563
  DropEnhancementLayers(&outputs[0], 10, 3, false);
564
  DecodeNFrames(&outputs[0], 10);
565
  DropEnhancementLayers(&outputs[0], 10, 2, false);
566
  DecodeNFrames(&outputs[0], 10);
567
  DropEnhancementLayers(&outputs[0], 10, 1, false);
568
569
570
571
  DecodeNFrames(&outputs[0], 10);

  FreeBitstreamBuffers(&outputs[0], 10);
}
572

573
574
575
576
577
TEST_F(SvcTest, TwoPassEncode2SNRLayers) {
  // First pass encode
  std::string stats_buf;
  vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1");
  Pass1EncodeNFrames(20, 2, &stats_buf);
578

579
580
581
582
583
584
585
586
587
588
  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  vpx_svc_set_options(&svc_,
                      "auto-alt-refs=1,1 scale-factors=1/1,1/1");
  vpx_fixed_buf outputs[20];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 20, 2, &outputs[0]);
  DecodeNFrames(&outputs[0], 20);
  FreeBitstreamBuffers(&outputs[0], 20);
}
589

590
591
592
593
594
TEST_F(SvcTest, TwoPassEncode3SNRLayersDecode321Layers) {
  // First pass encode
  std::string stats_buf;
  vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1,1/1");
  Pass1EncodeNFrames(20, 3, &stats_buf);
595

596
597
598
599
600
601
602
603
  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  vpx_svc_set_options(&svc_,
                      "auto-alt-refs=1,1,1 scale-factors=1/1,1/1,1/1");
  vpx_fixed_buf outputs[20];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 20, 3, &outputs[0]);
  DecodeNFrames(&outputs[0], 20);
604
  DropEnhancementLayers(&outputs[0], 20, 2, false);
605
  DecodeNFrames(&outputs[0], 20);
606
  DropEnhancementLayers(&outputs[0], 20, 1, false);
607
608
609
  DecodeNFrames(&outputs[0], 20);

  FreeBitstreamBuffers(&outputs[0], 20);
610
611
}

612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
TEST_F(SvcTest, SetMultipleFrameContextOption) {
  svc_.spatial_layers = 5;
  vpx_codec_err_t res =
      vpx_svc_set_options(&svc_, "multi-frame-contexts=1");
  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);

  svc_.spatial_layers = 2;
  res = vpx_svc_set_options(&svc_, "multi-frame-contexts=1");
  InitializeEncoder();
}

TEST_F(SvcTest, TwoPassEncode2LayersWithMultipleFrameContext) {
  // First pass encode
  std::string stats_buf;
  Pass1EncodeNFrames(10, 2, &stats_buf);

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  codec_enc_.g_error_resilient = 0;
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 multi-frame-contexts=1");
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
  DropEnhancementLayers(&outputs[0], 10, 2, true);
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}

TEST_F(SvcTest, TwoPassEncode2LayersWithMultipleFrameContextDecodeBaselayer) {
  // First pass encode
  std::string stats_buf;
  Pass1EncodeNFrames(10, 2, &stats_buf);

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  codec_enc_.g_error_resilient = 0;
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 multi-frame-contexts=1");
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
  DropEnhancementLayers(&outputs[0], 10, 1, true);
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}

TEST_F(SvcTest, TwoPassEncode2SNRLayersWithMultipleFrameContext) {
  // First pass encode
  std::string stats_buf;
  vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1");
  Pass1EncodeNFrames(10, 2, &stats_buf);

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  codec_enc_.g_error_resilient = 0;
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 scale-factors=1/1,1/1 "
                      "multi-frame-contexts=1");
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
  DropEnhancementLayers(&outputs[0], 10, 2, true);
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}

TEST_F(SvcTest, TwoPassEncode3SNRLayersWithMultipleFrameContextDecode321Layer) {
  // First pass encode
  std::string stats_buf;
  vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1,1/1");
  Pass1EncodeNFrames(10, 3, &stats_buf);

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  codec_enc_.g_error_resilient = 0;
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1,1 scale-factors=1/1,1/1,1/1 "
                      "multi-frame-contexts=1");
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 3, &outputs[0]);

  vpx_fixed_buf outputs_new[10];
  for (int i = 0; i < 10; ++i) {
    outputs_new[i].buf = malloc(outputs[i].sz + 16);
    ASSERT_TRUE(outputs_new[i].buf != NULL);
    memcpy(outputs_new[i].buf, outputs[i].buf, outputs[i].sz);
    outputs_new[i].sz = outputs[i].sz;
  }
  DropEnhancementLayers(&outputs_new[0], 10, 3, true);
  DecodeNFrames(&outputs_new[0], 10);

  for (int i = 0; i < 10; ++i) {
    memcpy(outputs_new[i].buf, outputs[i].buf, outputs[i].sz);
    outputs_new[i].sz = outputs[i].sz;
  }
  DropEnhancementLayers(&outputs_new[0], 10, 2, true);
  DecodeNFrames(&outputs_new[0], 10);

  for (int i = 0; i < 10; ++i) {
    memcpy(outputs_new[i].buf, outputs[i].buf, outputs[i].sz);
    outputs_new[i].sz = outputs[i].sz;
  }
  DropEnhancementLayers(&outputs_new[0], 10, 1, true);
  DecodeNFrames(&outputs_new[0], 10);

  FreeBitstreamBuffers(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs_new[0], 10);
}

721
}  // namespace