svc_test.cc 25.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

Yaowu Xu's avatar
Yaowu Xu committed
17
#include "av1/decoder/decoder.h"
18

Yaowu Xu's avatar
Yaowu Xu committed
19
20
21
#include "aom/svc_context.h"
#include "aom/vp8cx.h"
#include "aom/vpx_encoder.h"
22
23
24
25
26

namespace {

using libvpx_test::CodecFactory;
using libvpx_test::Decoder;
27
using libvpx_test::DxDataIterator;
28
29
30
31
32
33
34
35
using libvpx_test::VP9CodecFactory;

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

  SvcTest()
clang-format's avatar
clang-format committed
36
37
      : codec_iface_(0), test_file_name_("hantro_collage_w352h288.yuv"),
        codec_initialized_(false), decoder_(0) {
38
39
40
41
    memset(&svc_, 0, sizeof(svc_));
    memset(&codec_, 0, sizeof(codec_));
    memset(&codec_enc_, 0, sizeof(codec_enc_));
  }
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

  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;

61
    vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
62
63
    VP9CodecFactory codec_factory;
    decoder_ = codec_factory.CreateDecoder(dec_cfg, 0);
64
65
66

    tile_columns_ = 0;
    tile_rows_ = 0;
67
68
69
  }

  virtual void TearDown() {
70
    ReleaseEncoder();
clang-format's avatar
clang-format committed
71
    delete (decoder_);
72
73
74
75
76
77
  }

  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);
78
    vpx_codec_control(&codec_, VP8E_SET_CPUUSED, 4);  // Make the test faster
79
80
    vpx_codec_control(&codec_, VP9E_SET_TILE_COLUMNS, tile_columns_);
    vpx_codec_control(&codec_, VP9E_SET_TILE_ROWS, tile_rows_);
81
82
83
84
85
    codec_initialized_ = true;
  }

  void ReleaseEncoder() {
    vpx_svc_release(&svc_);
86
    if (codec_initialized_) vpx_codec_destroy(&codec_);
87
88
89
    codec_initialized_ = false;
  }

90
91
92
93
94
95
96
97
  void GetStatsData(std::string *const stats_buf) {
    vpx_codec_iter_t iter = NULL;
    const vpx_codec_cx_pkt_t *cx_pkt;

    while ((cx_pkt = vpx_codec_get_cx_data(&codec_, &iter)) != NULL) {
      if (cx_pkt->kind == VPX_CODEC_STATS_PKT) {
        EXPECT_GT(cx_pkt->data.twopass_stats.sz, 0U);
        ASSERT_TRUE(cx_pkt->data.twopass_stats.buf != NULL);
clang-format's avatar
clang-format committed
98
        stats_buf->append(static_cast<char *>(cx_pkt->data.twopass_stats.buf),
99
100
101
102
103
                          cx_pkt->data.twopass_stats.sz);
      }
    }
  }

104
105
106
107
108
109
110
111
112
113
  void Pass1EncodeNFrames(const int n, const int layers,
                          std::string *const stats_buf) {
    vpx_codec_err_t res;

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

clang-format's avatar
clang-format committed
114
115
116
    libvpx_test::I420VideoSource video(
        test_file_name_, codec_enc_.g_w, codec_enc_.g_h,
        codec_enc_.g_timebase.den, codec_enc_.g_timebase.num, 0, 30);
117
118
119
120
121
122
    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);
123
      GetStatsData(stats_buf);
124
125
126
      video.Next();
    }

127
    // Flush encoder and test EOS packet.
clang-format's avatar
clang-format committed
128
129
    res = vpx_svc_encode(&svc_, &codec_, NULL, video.pts(), video.duration(),
                         VPX_DL_GOOD_QUALITY);
130
131
    ASSERT_EQ(VPX_CODEC_OK, res);
    GetStatsData(stats_buf);
132
133
134
135
136
137
138

    ReleaseEncoder();
  }

  void StoreFrames(const size_t max_frame_received,
                   struct vpx_fixed_buf *const outputs,
                   size_t *const frame_received) {
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    vpx_codec_iter_t iter = NULL;
    const vpx_codec_cx_pkt_t *cx_pkt;

    while ((cx_pkt = vpx_codec_get_cx_data(&codec_, &iter)) != NULL) {
      if (cx_pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
        const size_t frame_size = cx_pkt->data.frame.sz;

        EXPECT_GT(frame_size, 0U);
        ASSERT_TRUE(cx_pkt->data.frame.buf != NULL);
        ASSERT_LT(*frame_received, max_frame_received);

        if (*frame_received == 0)
          EXPECT_EQ(1, !!(cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY));

        outputs[*frame_received].buf = malloc(frame_size + 16);
        ASSERT_TRUE(outputs[*frame_received].buf != NULL);
        memcpy(outputs[*frame_received].buf, cx_pkt->data.frame.buf,
               frame_size);
        outputs[*frame_received].sz = frame_size;
        ++(*frame_received);
159
160
161
162
      }
    }
  }

clang-format's avatar
clang-format committed
163
164
  void Pass2EncodeNFrames(std::string *const stats_buf, const int n,
                          const int layers,
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
                          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();

clang-format's avatar
clang-format committed
182
183
184
    libvpx_test::I420VideoSource video(
        test_file_name_, codec_enc_.g_w, codec_enc_.g_h,
        codec_enc_.g_timebase.den, codec_enc_.g_timebase.num, 0, 30);
185
186
187
188
189
190
191
192
193
194
    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();
    }

195
    // Flush encoder.
clang-format's avatar
clang-format committed
196
197
    res = vpx_svc_encode(&svc_, &codec_, NULL, 0, video.duration(),
                         VPX_DL_GOOD_QUALITY);
198
199
200
    EXPECT_EQ(VPX_CODEC_OK, res);
    StoreFrames(n, outputs, &frame_received);

201
    EXPECT_EQ(frame_received, static_cast<size_t>(n));
202
203
204
205
206
207
208
209
210
211
212
213
214
215

    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);
clang-format's avatar
clang-format committed
216
217
      const vpx_codec_err_t res_dec = decoder_->DecodeFrame(
          static_cast<const uint8_t *>(inputs[i].buf), inputs[i].sz);
218
219
220
221
      ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
      ++decoded_frames;

      DxDataIterator dec_iter = decoder_->GetDxData();
222
      while (dec_iter.Next() != NULL) {
223
224
225
226
227
228
229
        ++received_frames;
      }
    }
    EXPECT_EQ(decoded_frames, n);
    EXPECT_EQ(received_frames, n);
  }

230
231
232
  void DropEnhancementLayers(struct vpx_fixed_buf *const inputs,
                             const int num_super_frames,
                             const int remained_spatial_layers) {
233
234
    ASSERT_TRUE(inputs != NULL);
    ASSERT_GT(num_super_frames, 0);
235
    ASSERT_GT(remained_spatial_layers, 0);
236
237

    for (int i = 0; i < num_super_frames; ++i) {
clang-format's avatar
clang-format committed
238
      uint32_t frame_sizes[8] = { 0 };
239
240
241
242
243
244
      int frame_count = 0;
      int frames_found = 0;
      int frame;
      ASSERT_TRUE(inputs[i].buf != NULL);
      ASSERT_GT(inputs[i].sz, 0U);

clang-format's avatar
clang-format committed
245
246
247
      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);
248
249
      ASSERT_EQ(VPX_CODEC_OK, res);

250
251
252
253
254
      if (frame_count == 0) {
        // There's no super frame but only a single frame.
        ASSERT_EQ(1, remained_spatial_layers);
      } else {
        // Found a super frame.
clang-format's avatar
clang-format committed
255
        uint8_t *frame_data = static_cast<uint8_t *>(inputs[i].buf);
256
257
258
259
260
        uint8_t *frame_start = frame_data;
        for (frame = 0; frame < frame_count; ++frame) {
          // Looking for a visible frame.
          if (frame_data[0] & 0x02) {
            ++frames_found;
clang-format's avatar
clang-format committed
261
            if (frames_found == remained_spatial_layers) break;
262
263
264
          }
          frame_data += frame_sizes[frame];
        }
clang-format's avatar
clang-format committed
265
266
        ASSERT_LT(frame, frame_count)
            << "Couldn't find a visible frame. "
267
            << "remained_spatial_layers: " << remained_spatial_layers
268
            << "    super_frame: " << i;
clang-format's avatar
clang-format committed
269
        if (frame == frame_count - 1) continue;
270

271
        frame_data += frame_sizes[frame];
272
273
274

        // We need to add one more frame for multiple frame contexts.
        uint8_t marker =
clang-format's avatar
clang-format committed
275
            static_cast<const uint8_t *>(inputs[i].buf)[inputs[i].sz - 1];
276
277
278
279
280
281
282
        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;

        // Copy existing frame sizes.
283
284
        memmove(frame_data + 1, frame_start + inputs[i].sz - index_sz + 1,
                new_index_sz - 2);
285
286
287
288
289
290
        // New marker.
        frame_data[0] = marker;
        frame_data += (mag * (frame + 1) + 1);

        *frame_data++ = marker;
        inputs[i].sz = frame_data - frame_start;
291
      }
292
293
294
295
296
297
298
299
300
301
302
303
    }
  }

  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;
    }
304
305
306
307
308
309
310
  }

  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_;
311
  bool codec_initialized_;
312
  Decoder *decoder_;
313
314
  int tile_columns_;
  int tile_rows_;
315
316
317
};

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

333
  svc_.spatial_layers = 0;  // use default layers
334
  InitializeEncoder();
335
336
337
338
  EXPECT_EQ(VPX_SS_DEFAULT_LAYERS, svc_.spatial_layers);
}

TEST_F(SvcTest, InitTwoLayers) {
339
  svc_.spatial_layers = 2;
340
  InitializeEncoder();
341
342
}

343
344
TEST_F(SvcTest, InvalidOptions) {
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, NULL);
345
346
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

347
348
349
  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_);
350
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
351
}
352

353
TEST_F(SvcTest, SetLayersOption) {
354
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, "spatial-layers=3");
355
  EXPECT_EQ(VPX_CODEC_OK, res);
356
  InitializeEncoder();
357
  EXPECT_EQ(3, svc_.spatial_layers);
358
}
359

360
TEST_F(SvcTest, SetMultipleOptions) {
361
  vpx_codec_err_t res =
362
      vpx_svc_set_options(&svc_, "spatial-layers=2 scale-factors=1/3,2/3");
363
  EXPECT_EQ(VPX_CODEC_OK, res);
364
  InitializeEncoder();
365
  EXPECT_EQ(2, svc_.spatial_layers);
366
}
367

368
369
370
371
372
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);
373
374
375
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

376
377
378
379
380
381
382
383
384
385
  res = vpx_svc_set_options(&svc_, "scale-factors=1/3, 3*3");
  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_, "scale-factors=1/3");
  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);

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

391
392
TEST_F(SvcTest, SetQuantizersOption) {
  svc_.spatial_layers = 2;
393
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, "max-quantizers=nothing");
394
  EXPECT_EQ(VPX_CODEC_OK, res);
395
396
397
  res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
  EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);

398
  res = vpx_svc_set_options(&svc_, "min-quantizers=nothing");
399
400
401
402
  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);

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  res = vpx_svc_set_options(&svc_, "max-quantizers=40");
  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_, "min-quantizers=40");
  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_, "max-quantizers=30,30 min-quantizers=40,40");
  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_, "max-quantizers=40,40 min-quantizers=30,30");
419
  InitializeEncoder();
420
421
}

422
423
424
425
426
427
428
429
430
431
432
433
434
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");
435
  InitializeEncoder();
436
437
}

438
// Test that decoder can handle an SVC frame as the first frame in a sequence.
439
440
TEST_F(SvcTest, OnePassEncodeOneFrame) {
  codec_enc_.g_pass = VPX_RC_ONE_PASS;
clang-format's avatar
clang-format committed
441
  vpx_fixed_buf output = { 0 };
442
443
444
  Pass2EncodeNFrames(NULL, 1, 2, &output);
  DecodeNFrames(&output, 1);
  FreeBitstreamBuffers(&output, 1);
445
446
}

447
448
TEST_F(SvcTest, OnePassEncodeThreeFrames) {
  codec_enc_.g_pass = VPX_RC_ONE_PASS;
449
  codec_enc_.g_lag_in_frames = 0;
450
451
452
453
454
  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);
455
456
}

457
TEST_F(SvcTest, TwoPassEncode10Frames) {
458
459
  // First pass encode
  std::string stats_buf;
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
  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;
478
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
479
480
481
482
483
484
  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);
}
485

486
TEST_F(SvcTest, TwoPassEncode2SpatialLayersDecodeBaseLayerOnly) {
487
488
489
  // First pass encode
  std::string stats_buf;
  Pass1EncodeNFrames(10, 2, &stats_buf);
490

491
492
  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
493
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
494
495
496
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
497
  DropEnhancementLayers(&outputs[0], 10, 1);
498
499
500
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}
501

502
TEST_F(SvcTest, TwoPassEncode5SpatialLayersDecode54321Layers) {
503
504
505
  // First pass encode
  std::string stats_buf;
  Pass1EncodeNFrames(10, 5, &stats_buf);
506

507
508
509
510
511
512
513
514
  // 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);
515
  DropEnhancementLayers(&outputs[0], 10, 4);
516
  DecodeNFrames(&outputs[0], 10);
517
  DropEnhancementLayers(&outputs[0], 10, 3);
518
  DecodeNFrames(&outputs[0], 10);
519
  DropEnhancementLayers(&outputs[0], 10, 2);
520
  DecodeNFrames(&outputs[0], 10);
521
  DropEnhancementLayers(&outputs[0], 10, 1);
522
523
524
525
  DecodeNFrames(&outputs[0], 10);

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

527
528
529
530
531
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);
532

533
534
  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
clang-format's avatar
clang-format committed
535
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 scale-factors=1/1,1/1");
536
537
538
539
540
541
  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);
}
542

543
544
545
546
547
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);
548

549
550
  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
clang-format's avatar
clang-format committed
551
  vpx_svc_set_options(&svc_, "auto-alt-refs=1,1,1 scale-factors=1/1,1/1,1/1");
552
553
554
555
  vpx_fixed_buf outputs[20];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 20, 3, &outputs[0]);
  DecodeNFrames(&outputs[0], 20);
556
  DropEnhancementLayers(&outputs[0], 20, 2);
557
  DecodeNFrames(&outputs[0], 20);
558
  DropEnhancementLayers(&outputs[0], 20, 1);
559
560
561
  DecodeNFrames(&outputs[0], 20);

  FreeBitstreamBuffers(&outputs[0], 20);
562
563
}

564
TEST_F(SvcTest, SetMultipleFrameContextsOption) {
565
  svc_.spatial_layers = 5;
clang-format's avatar
clang-format committed
566
  vpx_codec_err_t res = vpx_svc_set_options(&svc_, "multi-frame-contexts=1");
567
568
569
570
571
572
573
574
575
  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();
}

576
TEST_F(SvcTest, TwoPassEncode2SpatialLayersWithMultipleFrameContexts) {
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
  // 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]);
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}

592
593
TEST_F(SvcTest,
       TwoPassEncode2SpatialLayersWithMultipleFrameContextsDecodeBaselayer) {
594
595
596
597
598
599
600
601
602
603
604
  // 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]);
605
  DropEnhancementLayers(&outputs[0], 10, 1);
606
607
608
609
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}

610
TEST_F(SvcTest, TwoPassEncode2SNRLayersWithMultipleFrameContexts) {
611
612
613
614
615
616
617
618
  // 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;
clang-format's avatar
clang-format committed
619
620
  vpx_svc_set_options(&svc_,
                      "auto-alt-refs=1,1 scale-factors=1/1,1/1 "
621
622
623
624
625
626
627
628
                      "multi-frame-contexts=1");
  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);
}

629
630
TEST_F(SvcTest,
       TwoPassEncode3SNRLayersWithMultipleFrameContextsDecode321Layer) {
631
632
633
634
635
636
637
638
  // 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;
clang-format's avatar
clang-format committed
639
640
  vpx_svc_set_options(&svc_,
                      "auto-alt-refs=1,1,1 scale-factors=1/1,1/1,1/1 "
641
642
643
644
645
                      "multi-frame-contexts=1");
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 3, &outputs[0]);

646
647
648
649
650
  DecodeNFrames(&outputs[0], 10);
  DropEnhancementLayers(&outputs[0], 10, 2);
  DecodeNFrames(&outputs[0], 10);
  DropEnhancementLayers(&outputs[0], 10, 1);
  DecodeNFrames(&outputs[0], 10);
651
652
653
654

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

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
TEST_F(SvcTest, TwoPassEncode2TemporalLayers) {
  // First pass encode
  std::string stats_buf;
  vpx_svc_set_options(&svc_, "scale-factors=1/1");
  svc_.temporal_layers = 2;
  Pass1EncodeNFrames(10, 1, &stats_buf);

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

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

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  svc_.temporal_layers = 2;
  codec_enc_.g_error_resilient = 0;
clang-format's avatar
clang-format committed
684
685
  vpx_svc_set_options(&svc_,
                      "auto-alt-refs=1 scale-factors=1/1 "
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
                      "multi-frame-contexts=1");
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}

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

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

  vpx_fixed_buf base_layer[5];
clang-format's avatar
clang-format committed
710
  for (int i = 0; i < 5; ++i) base_layer[i] = outputs[i * 2];
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727

  DecodeNFrames(&base_layer[0], 5);
  FreeBitstreamBuffers(&outputs[0], 10);
}

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

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  svc_.temporal_layers = 2;
  codec_enc_.g_error_resilient = 0;
clang-format's avatar
clang-format committed
728
729
  vpx_svc_set_options(&svc_,
                      "auto-alt-refs=1 scale-factors=1/1 "
730
731
732
733
734
735
                      "multi-frame-contexts=1");
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);

  vpx_fixed_buf base_layer[5];
clang-format's avatar
clang-format committed
736
  for (int i = 0; i < 5; ++i) base_layer[i] = outputs[i * 2];
737
738
739
740
741

  DecodeNFrames(&base_layer[0], 5);
  FreeBitstreamBuffers(&outputs[0], 10);
}

742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
TEST_F(SvcTest, TwoPassEncode2TemporalLayersWithTiles) {
  // First pass encode
  std::string stats_buf;
  vpx_svc_set_options(&svc_, "scale-factors=1/1");
  svc_.temporal_layers = 2;
  Pass1EncodeNFrames(10, 1, &stats_buf);

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  svc_.temporal_layers = 2;
  vpx_svc_set_options(&svc_, "auto-alt-refs=1 scale-factors=1/1");
  codec_enc_.g_w = 704;
  codec_enc_.g_h = 144;
  tile_columns_ = 1;
  tile_rows_ = 1;
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}

clang-format's avatar
clang-format committed
764
TEST_F(SvcTest, TwoPassEncode2TemporalLayersWithMultipleFrameContextsAndTiles) {
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  // First pass encode
  std::string stats_buf;
  vpx_svc_set_options(&svc_, "scale-factors=1/1");
  svc_.temporal_layers = 2;
  Pass1EncodeNFrames(10, 1, &stats_buf);

  // Second pass encode
  codec_enc_.g_pass = VPX_RC_LAST_PASS;
  svc_.temporal_layers = 2;
  codec_enc_.g_error_resilient = 0;
  codec_enc_.g_w = 704;
  codec_enc_.g_h = 144;
  tile_columns_ = 1;
  tile_rows_ = 1;
clang-format's avatar
clang-format committed
779
780
  vpx_svc_set_options(&svc_,
                      "auto-alt-refs=1 scale-factors=1/1 "
781
782
783
784
785
786
787
788
                      "multi-frame-contexts=1");
  vpx_fixed_buf outputs[10];
  memset(&outputs[0], 0, sizeof(outputs));
  Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
  DecodeNFrames(&outputs[0], 10);
  FreeBitstreamBuffers(&outputs[0], 10);
}

789
}  // namespace