Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / test / svc_test.cc
1 /*
2  *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <string>
12 #include "third_party/googletest/src/include/gtest/gtest.h"
13 #include "test/codec_factory.h"
14 #include "test/decode_test_driver.h"
15 #include "test/i420_video_source.h"
16
17 #include "vp9/decoder/vp9_decoder.h"
18
19 #include "vpx/svc_context.h"
20 #include "vpx/vp8cx.h"
21 #include "vpx/vpx_encoder.h"
22
23 namespace {
24
25 using libvpx_test::CodecFactory;
26 using libvpx_test::Decoder;
27 using libvpx_test::DxDataIterator;
28 using libvpx_test::VP9CodecFactory;
29
30 class SvcTest : public ::testing::Test {
31  protected:
32   static const uint32_t kWidth = 352;
33   static const uint32_t kHeight = 288;
34
35   SvcTest()
36       : codec_iface_(0),
37         test_file_name_("hantro_collage_w352h288.yuv"),
38         codec_initialized_(false),
39         decoder_(0) {
40     memset(&svc_, 0, sizeof(svc_));
41     memset(&codec_, 0, sizeof(codec_));
42     memset(&codec_enc_, 0, sizeof(codec_enc_));
43   }
44
45   virtual ~SvcTest() {}
46
47   virtual void SetUp() {
48     svc_.log_level = SVC_LOG_DEBUG;
49     svc_.log_print = 0;
50
51     codec_iface_ = vpx_codec_vp9_cx();
52     const vpx_codec_err_t res =
53         vpx_codec_enc_config_default(codec_iface_, &codec_enc_, 0);
54     EXPECT_EQ(VPX_CODEC_OK, res);
55
56     codec_enc_.g_w = kWidth;
57     codec_enc_.g_h = kHeight;
58     codec_enc_.g_timebase.num = 1;
59     codec_enc_.g_timebase.den = 60;
60     codec_enc_.kf_min_dist = 100;
61     codec_enc_.kf_max_dist = 100;
62
63     vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
64     VP9CodecFactory codec_factory;
65     decoder_ = codec_factory.CreateDecoder(dec_cfg, 0);
66   }
67
68   virtual void TearDown() {
69     ReleaseEncoder();
70     delete(decoder_);
71   }
72
73   void InitializeEncoder() {
74     const vpx_codec_err_t res =
75         vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
76     EXPECT_EQ(VPX_CODEC_OK, res);
77     vpx_codec_control(&codec_, VP8E_SET_CPUUSED, 4);  // Make the test faster
78     codec_initialized_ = true;
79   }
80
81   void ReleaseEncoder() {
82     vpx_svc_release(&svc_);
83     if (codec_initialized_) vpx_codec_destroy(&codec_);
84     codec_initialized_ = false;
85   }
86
87   void GetStatsData(std::string *const stats_buf) {
88     vpx_codec_iter_t iter = NULL;
89     const vpx_codec_cx_pkt_t *cx_pkt;
90
91     while ((cx_pkt = vpx_codec_get_cx_data(&codec_, &iter)) != NULL) {
92       if (cx_pkt->kind == VPX_CODEC_STATS_PKT) {
93         EXPECT_GT(cx_pkt->data.twopass_stats.sz, 0U);
94         ASSERT_TRUE(cx_pkt->data.twopass_stats.buf != NULL);
95         stats_buf->append(static_cast<char*>(cx_pkt->data.twopass_stats.buf),
96                           cx_pkt->data.twopass_stats.sz);
97       }
98     }
99   }
100
101   void Pass1EncodeNFrames(const int n, const int layers,
102                           std::string *const stats_buf) {
103     vpx_codec_err_t res;
104
105     ASSERT_GT(n, 0);
106     ASSERT_GT(layers, 0);
107     svc_.spatial_layers = layers;
108     codec_enc_.g_pass = VPX_RC_FIRST_PASS;
109     InitializeEncoder();
110
111     libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight,
112                                        codec_enc_.g_timebase.den,
113                                        codec_enc_.g_timebase.num, 0, 30);
114     video.Begin();
115
116     for (int i = 0; i < n; ++i) {
117       res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
118                            video.duration(), VPX_DL_GOOD_QUALITY);
119       ASSERT_EQ(VPX_CODEC_OK, res);
120       GetStatsData(stats_buf);
121       video.Next();
122     }
123
124     // Flush encoder and test EOS packet.
125     res = vpx_svc_encode(&svc_, &codec_, NULL, video.pts(),
126                          video.duration(), VPX_DL_GOOD_QUALITY);
127     ASSERT_EQ(VPX_CODEC_OK, res);
128     GetStatsData(stats_buf);
129
130     ReleaseEncoder();
131   }
132
133   void StoreFrames(const size_t max_frame_received,
134                    struct vpx_fixed_buf *const outputs,
135                    size_t *const frame_received) {
136     vpx_codec_iter_t iter = NULL;
137     const vpx_codec_cx_pkt_t *cx_pkt;
138
139     while ((cx_pkt = vpx_codec_get_cx_data(&codec_, &iter)) != NULL) {
140       if (cx_pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
141         const size_t frame_size = cx_pkt->data.frame.sz;
142
143         EXPECT_GT(frame_size, 0U);
144         ASSERT_TRUE(cx_pkt->data.frame.buf != NULL);
145         ASSERT_LT(*frame_received, max_frame_received);
146
147         if (*frame_received == 0)
148           EXPECT_EQ(1, !!(cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY));
149
150         outputs[*frame_received].buf = malloc(frame_size + 16);
151         ASSERT_TRUE(outputs[*frame_received].buf != NULL);
152         memcpy(outputs[*frame_received].buf, cx_pkt->data.frame.buf,
153                frame_size);
154         outputs[*frame_received].sz = frame_size;
155         ++(*frame_received);
156       }
157     }
158   }
159
160   void Pass2EncodeNFrames(std::string *const stats_buf,
161                           const int n, const int layers,
162                           struct vpx_fixed_buf *const outputs) {
163     vpx_codec_err_t res;
164     size_t frame_received = 0;
165
166     ASSERT_TRUE(outputs != NULL);
167     ASSERT_GT(n, 0);
168     ASSERT_GT(layers, 0);
169     svc_.spatial_layers = layers;
170     codec_enc_.rc_target_bitrate = 500;
171     if (codec_enc_.g_pass == VPX_RC_LAST_PASS) {
172       ASSERT_TRUE(stats_buf != NULL);
173       ASSERT_GT(stats_buf->size(), 0U);
174       codec_enc_.rc_twopass_stats_in.buf = &(*stats_buf)[0];
175       codec_enc_.rc_twopass_stats_in.sz = stats_buf->size();
176     }
177     InitializeEncoder();
178
179     libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight,
180                                        codec_enc_.g_timebase.den,
181                                        codec_enc_.g_timebase.num, 0, 30);
182     video.Begin();
183
184     for (int i = 0; i < n; ++i) {
185       res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
186                            video.duration(), VPX_DL_GOOD_QUALITY);
187       ASSERT_EQ(VPX_CODEC_OK, res);
188       StoreFrames(n, outputs, &frame_received);
189       video.Next();
190     }
191
192     // Flush encoder.
193     res = vpx_svc_encode(&svc_, &codec_, NULL, 0,
194                          video.duration(), VPX_DL_GOOD_QUALITY);
195     EXPECT_EQ(VPX_CODEC_OK, res);
196     StoreFrames(n, outputs, &frame_received);
197
198     EXPECT_EQ(frame_received, static_cast<size_t>(n));
199
200     ReleaseEncoder();
201   }
202
203   void DecodeNFrames(const struct vpx_fixed_buf *const inputs, const int n) {
204     int decoded_frames = 0;
205     int received_frames = 0;
206
207     ASSERT_TRUE(inputs != NULL);
208     ASSERT_GT(n, 0);
209
210     for (int i = 0; i < n; ++i) {
211       ASSERT_TRUE(inputs[i].buf != NULL);
212       ASSERT_GT(inputs[i].sz, 0U);
213       const vpx_codec_err_t res_dec =
214           decoder_->DecodeFrame(static_cast<const uint8_t *>(inputs[i].buf),
215                                 inputs[i].sz);
216       ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
217       ++decoded_frames;
218
219       DxDataIterator dec_iter = decoder_->GetDxData();
220       while (dec_iter.Next() != NULL) {
221         ++received_frames;
222       }
223     }
224     EXPECT_EQ(decoded_frames, n);
225     EXPECT_EQ(received_frames, n);
226   }
227
228   void DropEnhancementLayers(struct vpx_fixed_buf *const inputs,
229                              const int num_super_frames,
230                              const int remained_spatial_layers) {
231     ASSERT_TRUE(inputs != NULL);
232     ASSERT_GT(num_super_frames, 0);
233     ASSERT_GT(remained_spatial_layers, 0);
234
235     for (int i = 0; i < num_super_frames; ++i) {
236       uint32_t frame_sizes[8] = {0};
237       int frame_count = 0;
238       int frames_found = 0;
239       int frame;
240       ASSERT_TRUE(inputs[i].buf != NULL);
241       ASSERT_GT(inputs[i].sz, 0U);
242
243       vpx_codec_err_t res =
244           vp9_parse_superframe_index(static_cast<const uint8_t*>(inputs[i].buf),
245                                      inputs[i].sz, frame_sizes, &frame_count,
246                                      NULL, NULL);
247       ASSERT_EQ(VPX_CODEC_OK, res);
248
249       if (frame_count == 0) {
250         // There's no super frame but only a single frame.
251         ASSERT_EQ(1, remained_spatial_layers);
252       } else {
253         // Found a super frame.
254         uint8_t *frame_data = static_cast<uint8_t*>(inputs[i].buf);
255         uint8_t *frame_start = frame_data;
256         for (frame = 0; frame < frame_count; ++frame) {
257           // Looking for a visible frame.
258           if (frame_data[0] & 0x02) {
259             ++frames_found;
260             if (frames_found == remained_spatial_layers)
261               break;
262           }
263           frame_data += frame_sizes[frame];
264         }
265         ASSERT_LT(frame, frame_count) << "Couldn't find a visible frame. "
266             << "remained_spatial_layers: " << remained_spatial_layers
267             << "    super_frame: " << i;
268         if (frame == frame_count - 1)
269           continue;
270
271         frame_data += frame_sizes[frame];
272
273         // We need to add one more frame for multiple frame contexts.
274         uint8_t marker =
275             static_cast<const uint8_t*>(inputs[i].buf)[inputs[i].sz - 1];
276         const uint32_t mag = ((marker >> 3) & 0x3) + 1;
277         const size_t index_sz = 2 + mag * frame_count;
278         const size_t new_index_sz = 2 + mag * (frame + 1);
279         marker &= 0x0f8;
280         marker |= frame;
281
282         // Copy existing frame sizes.
283         memmove(frame_data + 1, frame_start + inputs[i].sz - index_sz + 1,
284                 new_index_sz - 2);
285         // New marker.
286         frame_data[0] = marker;
287         frame_data += (mag * (frame + 1) + 1);
288
289         *frame_data++ = marker;
290         inputs[i].sz = frame_data - frame_start;
291       }
292     }
293   }
294
295   void FreeBitstreamBuffers(struct vpx_fixed_buf *const inputs, const int n) {
296     ASSERT_TRUE(inputs != NULL);
297     ASSERT_GT(n, 0);
298
299     for (int i = 0; i < n; ++i) {
300       free(inputs[i].buf);
301       inputs[i].buf = NULL;
302       inputs[i].sz = 0;
303     }
304   }
305
306   SvcContext svc_;
307   vpx_codec_ctx_t codec_;
308   struct vpx_codec_enc_cfg codec_enc_;
309   vpx_codec_iface_t *codec_iface_;
310   std::string test_file_name_;
311   bool codec_initialized_;
312   Decoder *decoder_;
313 };
314
315 TEST_F(SvcTest, SvcInit) {
316   // test missing parameters
317   vpx_codec_err_t res = vpx_svc_init(NULL, &codec_, codec_iface_, &codec_enc_);
318   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
319   res = vpx_svc_init(&svc_, NULL, codec_iface_, &codec_enc_);
320   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
321   res = vpx_svc_init(&svc_, &codec_, NULL, &codec_enc_);
322   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
323
324   res = vpx_svc_init(&svc_, &codec_, codec_iface_, NULL);
325   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
326
327   svc_.spatial_layers = 6;  // too many layers
328   res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_);
329   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
330
331   svc_.spatial_layers = 0;  // use default layers
332   InitializeEncoder();
333   EXPECT_EQ(VPX_SS_DEFAULT_LAYERS, svc_.spatial_layers);
334 }
335
336 TEST_F(SvcTest, InitTwoLayers) {
337   svc_.spatial_layers = 2;
338   InitializeEncoder();
339 }
340
341 TEST_F(SvcTest, InvalidOptions) {
342   vpx_codec_err_t res = vpx_svc_set_options(&svc_, NULL);
343   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
344
345   res = vpx_svc_set_options(&svc_, "not-an-option=1");
346   EXPECT_EQ(VPX_CODEC_OK, res);
347   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
348   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
349 }
350
351 TEST_F(SvcTest, SetLayersOption) {
352   vpx_codec_err_t res = vpx_svc_set_options(&svc_, "spatial-layers=3");
353   EXPECT_EQ(VPX_CODEC_OK, res);
354   InitializeEncoder();
355   EXPECT_EQ(3, svc_.spatial_layers);
356 }
357
358 TEST_F(SvcTest, SetMultipleOptions) {
359   vpx_codec_err_t res =
360       vpx_svc_set_options(&svc_, "spatial-layers=2 scale-factors=1/3,2/3");
361   EXPECT_EQ(VPX_CODEC_OK, res);
362   InitializeEncoder();
363   EXPECT_EQ(2, svc_.spatial_layers);
364 }
365
366 TEST_F(SvcTest, SetScaleFactorsOption) {
367   svc_.spatial_layers = 2;
368   vpx_codec_err_t res =
369       vpx_svc_set_options(&svc_, "scale-factors=not-scale-factors");
370   EXPECT_EQ(VPX_CODEC_OK, res);
371   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
372   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
373
374   res = vpx_svc_set_options(&svc_, "scale-factors=1/3, 3*3");
375   EXPECT_EQ(VPX_CODEC_OK, res);
376   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
377   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
378
379   res = vpx_svc_set_options(&svc_, "scale-factors=1/3");
380   EXPECT_EQ(VPX_CODEC_OK, res);
381   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
382   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
383
384   res = vpx_svc_set_options(&svc_, "scale-factors=1/3,2/3");
385   EXPECT_EQ(VPX_CODEC_OK, res);
386   InitializeEncoder();
387 }
388
389 TEST_F(SvcTest, SetQuantizersOption) {
390   svc_.spatial_layers = 2;
391   vpx_codec_err_t res = vpx_svc_set_options(&svc_, "max-quantizers=nothing");
392   EXPECT_EQ(VPX_CODEC_OK, res);
393   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
394   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
395
396   res = vpx_svc_set_options(&svc_, "min-quantizers=nothing");
397   EXPECT_EQ(VPX_CODEC_OK, res);
398   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
399   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
400
401   res = vpx_svc_set_options(&svc_, "max-quantizers=40");
402   EXPECT_EQ(VPX_CODEC_OK, res);
403   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
404   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
405
406   res = vpx_svc_set_options(&svc_, "min-quantizers=40");
407   EXPECT_EQ(VPX_CODEC_OK, res);
408   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
409   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
410
411   res = vpx_svc_set_options(&svc_, "max-quantizers=30,30 min-quantizers=40,40");
412   EXPECT_EQ(VPX_CODEC_OK, res);
413   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
414   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
415
416   res = vpx_svc_set_options(&svc_, "max-quantizers=40,40 min-quantizers=30,30");
417   InitializeEncoder();
418 }
419
420 TEST_F(SvcTest, SetAutoAltRefOption) {
421   svc_.spatial_layers = 5;
422   vpx_codec_err_t res = vpx_svc_set_options(&svc_, "auto-alt-refs=none");
423   EXPECT_EQ(VPX_CODEC_OK, res);
424   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
425   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
426
427   res = vpx_svc_set_options(&svc_, "auto-alt-refs=1,1,1,1,0");
428   EXPECT_EQ(VPX_CODEC_OK, res);
429   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
430   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
431
432   vpx_svc_set_options(&svc_, "auto-alt-refs=0,1,1,1,0");
433   InitializeEncoder();
434 }
435
436 // Test that decoder can handle an SVC frame as the first frame in a sequence.
437 TEST_F(SvcTest, OnePassEncodeOneFrame) {
438   codec_enc_.g_pass = VPX_RC_ONE_PASS;
439   vpx_fixed_buf output = {0};
440   Pass2EncodeNFrames(NULL, 1, 2, &output);
441   DecodeNFrames(&output, 1);
442   FreeBitstreamBuffers(&output, 1);
443 }
444
445 TEST_F(SvcTest, OnePassEncodeThreeFrames) {
446   codec_enc_.g_pass = VPX_RC_ONE_PASS;
447   vpx_fixed_buf outputs[3];
448   memset(&outputs[0], 0, sizeof(outputs));
449   Pass2EncodeNFrames(NULL, 3, 2, &outputs[0]);
450   DecodeNFrames(&outputs[0], 3);
451   FreeBitstreamBuffers(&outputs[0], 3);
452 }
453
454 TEST_F(SvcTest, TwoPassEncode10Frames) {
455   // First pass encode
456   std::string stats_buf;
457   Pass1EncodeNFrames(10, 2, &stats_buf);
458
459   // Second pass encode
460   codec_enc_.g_pass = VPX_RC_LAST_PASS;
461   vpx_fixed_buf outputs[10];
462   memset(&outputs[0], 0, sizeof(outputs));
463   Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
464   DecodeNFrames(&outputs[0], 10);
465   FreeBitstreamBuffers(&outputs[0], 10);
466 }
467
468 TEST_F(SvcTest, TwoPassEncode20FramesWithAltRef) {
469   // First pass encode
470   std::string stats_buf;
471   Pass1EncodeNFrames(20, 2, &stats_buf);
472
473   // Second pass encode
474   codec_enc_.g_pass = VPX_RC_LAST_PASS;
475   vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
476   vpx_fixed_buf outputs[20];
477   memset(&outputs[0], 0, sizeof(outputs));
478   Pass2EncodeNFrames(&stats_buf, 20, 2, &outputs[0]);
479   DecodeNFrames(&outputs[0], 20);
480   FreeBitstreamBuffers(&outputs[0], 20);
481 }
482
483 TEST_F(SvcTest, TwoPassEncode2SpatialLayersDecodeBaseLayerOnly) {
484   // First pass encode
485   std::string stats_buf;
486   Pass1EncodeNFrames(10, 2, &stats_buf);
487
488   // Second pass encode
489   codec_enc_.g_pass = VPX_RC_LAST_PASS;
490   vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
491   vpx_fixed_buf outputs[10];
492   memset(&outputs[0], 0, sizeof(outputs));
493   Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
494   DropEnhancementLayers(&outputs[0], 10, 1);
495   DecodeNFrames(&outputs[0], 10);
496   FreeBitstreamBuffers(&outputs[0], 10);
497 }
498
499 TEST_F(SvcTest, TwoPassEncode5SpatialLayersDecode54321Layers) {
500   // First pass encode
501   std::string stats_buf;
502   Pass1EncodeNFrames(10, 5, &stats_buf);
503
504   // Second pass encode
505   codec_enc_.g_pass = VPX_RC_LAST_PASS;
506   vpx_svc_set_options(&svc_, "auto-alt-refs=0,1,1,1,0");
507   vpx_fixed_buf outputs[10];
508   memset(&outputs[0], 0, sizeof(outputs));
509   Pass2EncodeNFrames(&stats_buf, 10, 5, &outputs[0]);
510
511   DecodeNFrames(&outputs[0], 10);
512   DropEnhancementLayers(&outputs[0], 10, 4);
513   DecodeNFrames(&outputs[0], 10);
514   DropEnhancementLayers(&outputs[0], 10, 3);
515   DecodeNFrames(&outputs[0], 10);
516   DropEnhancementLayers(&outputs[0], 10, 2);
517   DecodeNFrames(&outputs[0], 10);
518   DropEnhancementLayers(&outputs[0], 10, 1);
519   DecodeNFrames(&outputs[0], 10);
520
521   FreeBitstreamBuffers(&outputs[0], 10);
522 }
523
524 TEST_F(SvcTest, TwoPassEncode2SNRLayers) {
525   // First pass encode
526   std::string stats_buf;
527   vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1");
528   Pass1EncodeNFrames(20, 2, &stats_buf);
529
530   // Second pass encode
531   codec_enc_.g_pass = VPX_RC_LAST_PASS;
532   vpx_svc_set_options(&svc_,
533                       "auto-alt-refs=1,1 scale-factors=1/1,1/1");
534   vpx_fixed_buf outputs[20];
535   memset(&outputs[0], 0, sizeof(outputs));
536   Pass2EncodeNFrames(&stats_buf, 20, 2, &outputs[0]);
537   DecodeNFrames(&outputs[0], 20);
538   FreeBitstreamBuffers(&outputs[0], 20);
539 }
540
541 TEST_F(SvcTest, TwoPassEncode3SNRLayersDecode321Layers) {
542   // First pass encode
543   std::string stats_buf;
544   vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1,1/1");
545   Pass1EncodeNFrames(20, 3, &stats_buf);
546
547   // Second pass encode
548   codec_enc_.g_pass = VPX_RC_LAST_PASS;
549   vpx_svc_set_options(&svc_,
550                       "auto-alt-refs=1,1,1 scale-factors=1/1,1/1,1/1");
551   vpx_fixed_buf outputs[20];
552   memset(&outputs[0], 0, sizeof(outputs));
553   Pass2EncodeNFrames(&stats_buf, 20, 3, &outputs[0]);
554   DecodeNFrames(&outputs[0], 20);
555   DropEnhancementLayers(&outputs[0], 20, 2);
556   DecodeNFrames(&outputs[0], 20);
557   DropEnhancementLayers(&outputs[0], 20, 1);
558   DecodeNFrames(&outputs[0], 20);
559
560   FreeBitstreamBuffers(&outputs[0], 20);
561 }
562
563 TEST_F(SvcTest, SetMultipleFrameContextsOption) {
564   svc_.spatial_layers = 5;
565   vpx_codec_err_t res =
566       vpx_svc_set_options(&svc_, "multi-frame-contexts=1");
567   EXPECT_EQ(VPX_CODEC_OK, res);
568   res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
569   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
570
571   svc_.spatial_layers = 2;
572   res = vpx_svc_set_options(&svc_, "multi-frame-contexts=1");
573   InitializeEncoder();
574 }
575
576 TEST_F(SvcTest, TwoPassEncode2SpatialLayersWithMultipleFrameContexts) {
577   // First pass encode
578   std::string stats_buf;
579   Pass1EncodeNFrames(10, 2, &stats_buf);
580
581   // Second pass encode
582   codec_enc_.g_pass = VPX_RC_LAST_PASS;
583   codec_enc_.g_error_resilient = 0;
584   vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 multi-frame-contexts=1");
585   vpx_fixed_buf outputs[10];
586   memset(&outputs[0], 0, sizeof(outputs));
587   Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
588   DecodeNFrames(&outputs[0], 10);
589   FreeBitstreamBuffers(&outputs[0], 10);
590 }
591
592 TEST_F(SvcTest,
593        TwoPassEncode2SpatialLayersWithMultipleFrameContextsDecodeBaselayer) {
594   // First pass encode
595   std::string stats_buf;
596   Pass1EncodeNFrames(10, 2, &stats_buf);
597
598   // Second pass encode
599   codec_enc_.g_pass = VPX_RC_LAST_PASS;
600   codec_enc_.g_error_resilient = 0;
601   vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 multi-frame-contexts=1");
602   vpx_fixed_buf outputs[10];
603   memset(&outputs[0], 0, sizeof(outputs));
604   Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
605   DropEnhancementLayers(&outputs[0], 10, 1);
606   DecodeNFrames(&outputs[0], 10);
607   FreeBitstreamBuffers(&outputs[0], 10);
608 }
609
610 TEST_F(SvcTest, TwoPassEncode2SNRLayersWithMultipleFrameContexts) {
611   // First pass encode
612   std::string stats_buf;
613   vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1");
614   Pass1EncodeNFrames(10, 2, &stats_buf);
615
616   // Second pass encode
617   codec_enc_.g_pass = VPX_RC_LAST_PASS;
618   codec_enc_.g_error_resilient = 0;
619   vpx_svc_set_options(&svc_, "auto-alt-refs=1,1 scale-factors=1/1,1/1 "
620                       "multi-frame-contexts=1");
621   vpx_fixed_buf outputs[10];
622   memset(&outputs[0], 0, sizeof(outputs));
623   Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
624   DecodeNFrames(&outputs[0], 10);
625   FreeBitstreamBuffers(&outputs[0], 10);
626 }
627
628 TEST_F(SvcTest,
629        TwoPassEncode3SNRLayersWithMultipleFrameContextsDecode321Layer) {
630   // First pass encode
631   std::string stats_buf;
632   vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1,1/1");
633   Pass1EncodeNFrames(10, 3, &stats_buf);
634
635   // Second pass encode
636   codec_enc_.g_pass = VPX_RC_LAST_PASS;
637   codec_enc_.g_error_resilient = 0;
638   vpx_svc_set_options(&svc_, "auto-alt-refs=1,1,1 scale-factors=1/1,1/1,1/1 "
639                       "multi-frame-contexts=1");
640   vpx_fixed_buf outputs[10];
641   memset(&outputs[0], 0, sizeof(outputs));
642   Pass2EncodeNFrames(&stats_buf, 10, 3, &outputs[0]);
643
644   DecodeNFrames(&outputs[0], 10);
645   DropEnhancementLayers(&outputs[0], 10, 2);
646   DecodeNFrames(&outputs[0], 10);
647   DropEnhancementLayers(&outputs[0], 10, 1);
648   DecodeNFrames(&outputs[0], 10);
649
650   FreeBitstreamBuffers(&outputs[0], 10);
651 }
652
653 TEST_F(SvcTest, TwoPassEncode2TemporalLayers) {
654   // First pass encode
655   std::string stats_buf;
656   vpx_svc_set_options(&svc_, "scale-factors=1/1");
657   svc_.temporal_layers = 2;
658   Pass1EncodeNFrames(10, 1, &stats_buf);
659
660   // Second pass encode
661   codec_enc_.g_pass = VPX_RC_LAST_PASS;
662   svc_.temporal_layers = 2;
663   vpx_svc_set_options(&svc_, "auto-alt-refs=1 scale-factors=1/1");
664   vpx_fixed_buf outputs[10];
665   memset(&outputs[0], 0, sizeof(outputs));
666   Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
667   DecodeNFrames(&outputs[0], 10);
668   FreeBitstreamBuffers(&outputs[0], 10);
669 }
670
671 TEST_F(SvcTest, TwoPassEncode2TemporalLayersWithMultipleFrameContexts) {
672   // First pass encode
673   std::string stats_buf;
674   vpx_svc_set_options(&svc_, "scale-factors=1/1");
675   svc_.temporal_layers = 2;
676   Pass1EncodeNFrames(10, 1, &stats_buf);
677
678   // Second pass encode
679   codec_enc_.g_pass = VPX_RC_LAST_PASS;
680   svc_.temporal_layers = 2;
681   codec_enc_.g_error_resilient = 0;
682   vpx_svc_set_options(&svc_, "auto-alt-refs=1 scale-factors=1/1 "
683                       "multi-frame-contexts=1");
684   vpx_fixed_buf outputs[10];
685   memset(&outputs[0], 0, sizeof(outputs));
686   Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
687   DecodeNFrames(&outputs[0], 10);
688   FreeBitstreamBuffers(&outputs[0], 10);
689 }
690
691 TEST_F(SvcTest, TwoPassEncode2TemporalLayersDecodeBaseLayer) {
692   // First pass encode
693   std::string stats_buf;
694   vpx_svc_set_options(&svc_, "scale-factors=1/1");
695   svc_.temporal_layers = 2;
696   Pass1EncodeNFrames(10, 1, &stats_buf);
697
698   // Second pass encode
699   codec_enc_.g_pass = VPX_RC_LAST_PASS;
700   svc_.temporal_layers = 2;
701   vpx_svc_set_options(&svc_, "auto-alt-refs=1 scale-factors=1/1");
702   vpx_fixed_buf outputs[10];
703   memset(&outputs[0], 0, sizeof(outputs));
704   Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
705
706   vpx_fixed_buf base_layer[5];
707   for (int i = 0; i < 5; ++i)
708     base_layer[i] = outputs[i * 2];
709
710   DecodeNFrames(&base_layer[0], 5);
711   FreeBitstreamBuffers(&outputs[0], 10);
712 }
713
714 TEST_F(SvcTest,
715        TwoPassEncode2TemporalLayersWithMultipleFrameContextsDecodeBaseLayer) {
716   // First pass encode
717   std::string stats_buf;
718   vpx_svc_set_options(&svc_, "scale-factors=1/1");
719   svc_.temporal_layers = 2;
720   Pass1EncodeNFrames(10, 1, &stats_buf);
721
722   // Second pass encode
723   codec_enc_.g_pass = VPX_RC_LAST_PASS;
724   svc_.temporal_layers = 2;
725   codec_enc_.g_error_resilient = 0;
726   vpx_svc_set_options(&svc_, "auto-alt-refs=1 scale-factors=1/1 "
727                       "multi-frame-contexts=1");
728   vpx_fixed_buf outputs[10];
729   memset(&outputs[0], 0, sizeof(outputs));
730   Pass2EncodeNFrames(&stats_buf, 10, 1, &outputs[0]);
731
732   vpx_fixed_buf base_layer[5];
733   for (int i = 0; i < 5; ++i)
734     base_layer[i] = outputs[i * 2];
735
736   DecodeNFrames(&base_layer[0], 5);
737   FreeBitstreamBuffers(&outputs[0], 10);
738 }
739
740 }  // namespace