VP9: Add speed 9 for subpel search.
[platform/upstream/libvpx.git] / test / svc_datarate_test.cc
1 /*
2  *  Copyright (c) 2012 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 #include "./vpx_config.h"
11 #include "third_party/googletest/src/include/gtest/gtest.h"
12 #include "test/codec_factory.h"
13 #include "test/encode_test_driver.h"
14 #include "test/i420_video_source.h"
15 #include "test/util.h"
16 #include "test/y4m_video_source.h"
17 #include "vpx/vpx_codec.h"
18 #include "vpx_ports/bitops.h"
19
20 namespace {
21
22 void AssignLayerBitrates(vpx_codec_enc_cfg_t *const enc_cfg,
23                          const vpx_svc_extra_cfg_t *svc_params,
24                          int spatial_layers, int temporal_layers,
25                          int temporal_layering_mode,
26                          int *layer_target_avg_bandwidth,
27                          int64_t *bits_in_buffer_model) {
28   int sl, spatial_layer_target;
29   float total = 0;
30   float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
31   float framerate = 30.0;
32   for (sl = 0; sl < spatial_layers; ++sl) {
33     if (svc_params->scaling_factor_den[sl] > 0) {
34       alloc_ratio[sl] = (float)(svc_params->scaling_factor_num[sl] * 1.0 /
35                                 svc_params->scaling_factor_den[sl]);
36       total += alloc_ratio[sl];
37     }
38   }
39   for (sl = 0; sl < spatial_layers; ++sl) {
40     enc_cfg->ss_target_bitrate[sl] = spatial_layer_target =
41         (unsigned int)(enc_cfg->rc_target_bitrate * alloc_ratio[sl] / total);
42     const int index = sl * temporal_layers;
43     if (temporal_layering_mode == 3) {
44       enc_cfg->layer_target_bitrate[index] = spatial_layer_target >> 1;
45       enc_cfg->layer_target_bitrate[index + 1] =
46           (spatial_layer_target >> 1) + (spatial_layer_target >> 2);
47       enc_cfg->layer_target_bitrate[index + 2] = spatial_layer_target;
48     } else if (temporal_layering_mode == 2) {
49       enc_cfg->layer_target_bitrate[index] = spatial_layer_target * 2 / 3;
50       enc_cfg->layer_target_bitrate[index + 1] = spatial_layer_target;
51     } else if (temporal_layering_mode <= 1) {
52       enc_cfg->layer_target_bitrate[index] = spatial_layer_target;
53     }
54   }
55   for (sl = 0; sl < spatial_layers; ++sl) {
56     for (int tl = 0; tl < temporal_layers; ++tl) {
57       const int layer = sl * temporal_layers + tl;
58       float layer_framerate = framerate;
59       if (temporal_layers == 2 && tl == 0) layer_framerate = framerate / 2;
60       if (temporal_layers == 3 && tl == 0) layer_framerate = framerate / 4;
61       if (temporal_layers == 3 && tl == 1) layer_framerate = framerate / 2;
62       layer_target_avg_bandwidth[layer] = static_cast<int>(
63           enc_cfg->layer_target_bitrate[layer] * 1000.0 / layer_framerate);
64       bits_in_buffer_model[layer] =
65           enc_cfg->layer_target_bitrate[layer] * enc_cfg->rc_buf_initial_sz;
66     }
67   }
68 }
69
70 void CheckLayerRateTargeting(vpx_codec_enc_cfg_t *const cfg,
71                              int number_spatial_layers,
72                              int number_temporal_layers, double *file_datarate,
73                              double thresh_overshoot,
74                              double thresh_undershoot) {
75   for (int sl = 0; sl < number_spatial_layers; ++sl)
76     for (int tl = 0; tl < number_temporal_layers; ++tl) {
77       const int layer = sl * number_temporal_layers + tl;
78       ASSERT_GE(cfg->layer_target_bitrate[layer],
79                 file_datarate[layer] * thresh_overshoot)
80           << " The datarate for the file exceeds the target by too much!";
81       ASSERT_LE(cfg->layer_target_bitrate[layer],
82                 file_datarate[layer] * thresh_undershoot)
83           << " The datarate for the file is lower than the target by too much!";
84     }
85 }
86
87 class DatarateOnePassCbrSvc : public ::libvpx_test::EncoderTest {
88  public:
89   explicit DatarateOnePassCbrSvc(const ::libvpx_test::CodecFactory *codec)
90       : EncoderTest(codec) {}
91
92  protected:
93   virtual ~DatarateOnePassCbrSvc() {}
94
95   virtual void ResetModel() {
96     last_pts_ = 0;
97     duration_ = 0.0;
98     mismatch_psnr_ = 0.0;
99     mismatch_nframes_ = 0;
100     denoiser_on_ = 0;
101     tune_content_ = 0;
102     base_speed_setting_ = 5;
103     spatial_layer_id_ = 0;
104     temporal_layer_id_ = 0;
105     update_pattern_ = 0;
106     memset(bits_in_buffer_model_, 0, sizeof(bits_in_buffer_model_));
107     memset(bits_total_, 0, sizeof(bits_total_));
108     memset(layer_target_avg_bandwidth_, 0, sizeof(layer_target_avg_bandwidth_));
109     dynamic_drop_layer_ = false;
110     change_bitrate_ = false;
111     last_pts_ref_ = 0;
112     middle_bitrate_ = 0;
113     top_bitrate_ = 0;
114     superframe_count_ = -1;
115     key_frame_spacing_ = 9999;
116     num_nonref_frames_ = 0;
117     layer_framedrop_ = 0;
118   }
119   virtual void BeginPassHook(unsigned int /*pass*/) {}
120
121   // Example pattern for spatial layers and 2 temporal layers used in the
122   // bypass/flexible mode. The pattern corresponds to the pattern
123   // VP9E_TEMPORAL_LAYERING_MODE_0101 (temporal_layering_mode == 2) used in
124   // non-flexible mode, except that we disable inter-layer prediction.
125   void set_frame_flags_bypass_mode(
126       int tl, int num_spatial_layers, int is_key_frame,
127       vpx_svc_ref_frame_config_t *ref_frame_config) {
128     for (int sl = 0; sl < num_spatial_layers; ++sl) {
129       if (!tl) {
130         if (!sl) {
131           ref_frame_config->frame_flags[sl] =
132               VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
133               VP8_EFLAG_NO_UPD_ARF;
134         } else {
135           if (is_key_frame) {
136             ref_frame_config->frame_flags[sl] =
137                 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
138                 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
139           } else {
140             ref_frame_config->frame_flags[sl] =
141                 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
142                 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
143           }
144         }
145       } else if (tl == 1) {
146         if (!sl) {
147           ref_frame_config->frame_flags[sl] =
148               VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
149               VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
150         } else {
151           ref_frame_config->frame_flags[sl] =
152               VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
153               VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_REF_GF;
154         }
155       }
156       if (tl == 0) {
157         ref_frame_config->lst_fb_idx[sl] = sl;
158         if (sl) {
159           if (is_key_frame) {
160             ref_frame_config->lst_fb_idx[sl] = sl - 1;
161             ref_frame_config->gld_fb_idx[sl] = sl;
162           } else {
163             ref_frame_config->gld_fb_idx[sl] = sl - 1;
164           }
165         } else {
166           ref_frame_config->gld_fb_idx[sl] = 0;
167         }
168         ref_frame_config->alt_fb_idx[sl] = 0;
169       } else if (tl == 1) {
170         ref_frame_config->lst_fb_idx[sl] = sl;
171         ref_frame_config->gld_fb_idx[sl] = num_spatial_layers + sl - 1;
172         ref_frame_config->alt_fb_idx[sl] = num_spatial_layers + sl;
173       }
174     }
175   }
176
177   virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
178                                   ::libvpx_test::Encoder *encoder) {
179     if (video->frame() == 0) {
180       int i;
181       for (i = 0; i < VPX_MAX_LAYERS; ++i) {
182         svc_params_.max_quantizers[i] = 63;
183         svc_params_.min_quantizers[i] = 0;
184       }
185       svc_params_.speed_per_layer[0] = base_speed_setting_;
186       for (i = 1; i < VPX_SS_MAX_LAYERS; ++i) {
187         svc_params_.speed_per_layer[i] = speed_setting_;
188       }
189
190       encoder->Control(VP9E_SET_NOISE_SENSITIVITY, denoiser_on_);
191       encoder->Control(VP9E_SET_SVC, 1);
192       encoder->Control(VP9E_SET_SVC_PARAMETERS, &svc_params_);
193       encoder->Control(VP8E_SET_CPUUSED, speed_setting_);
194       encoder->Control(VP9E_SET_TILE_COLUMNS, 0);
195       encoder->Control(VP8E_SET_MAX_INTRA_BITRATE_PCT, 300);
196       encoder->Control(VP9E_SET_TILE_COLUMNS, get_msb(cfg_.g_threads));
197       encoder->Control(VP9E_SET_ROW_MT, 1);
198       encoder->Control(VP8E_SET_STATIC_THRESHOLD, 1);
199       encoder->Control(VP9E_SET_TUNE_CONTENT, tune_content_);
200
201       if (layer_framedrop_) {
202         vpx_svc_frame_drop_t svc_drop_frame;
203         svc_drop_frame.framedrop_mode = LAYER_DROP;
204         for (i = 0; i < number_spatial_layers_; i++)
205           svc_drop_frame.framedrop_thresh[i] = 30;
206         encoder->Control(VP9E_SET_SVC_FRAME_DROP_LAYER, &svc_drop_frame);
207       }
208     }
209
210     superframe_count_++;
211     temporal_layer_id_ = 0;
212     if (number_temporal_layers_ == 2)
213       temporal_layer_id_ = (superframe_count_ % 2 != 0);
214     else if (number_temporal_layers_ == 3) {
215       if (superframe_count_ % 2 != 0) temporal_layer_id_ = 2;
216       if (superframe_count_ > 1) {
217         if ((superframe_count_ - 2) % 4 == 0) temporal_layer_id_ = 1;
218       }
219     }
220
221     if (update_pattern_ && video->frame() >= 100) {
222       vpx_svc_layer_id_t layer_id;
223       if (video->frame() == 100) {
224         cfg_.temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS;
225         encoder->Config(&cfg_);
226       }
227       // Set layer id since the pattern changed.
228       layer_id.spatial_layer_id = 0;
229       layer_id.temporal_layer_id = (video->frame() % 2 != 0);
230       temporal_layer_id_ = layer_id.temporal_layer_id;
231       encoder->Control(VP9E_SET_SVC_LAYER_ID, &layer_id);
232       set_frame_flags_bypass_mode(layer_id.temporal_layer_id,
233                                   number_spatial_layers_, 0, &ref_frame_config);
234       encoder->Control(VP9E_SET_SVC_REF_FRAME_CONFIG, &ref_frame_config);
235     }
236
237     if (change_bitrate_ && video->frame() == 200) {
238       duration_ = (last_pts_ + 1) * timebase_;
239       for (int sl = 0; sl < number_spatial_layers_; ++sl) {
240         for (int tl = 0; tl < number_temporal_layers_; ++tl) {
241           const int layer = sl * number_temporal_layers_ + tl;
242           const double file_size_in_kb = bits_total_[layer] / 1000.;
243           file_datarate_[layer] = file_size_in_kb / duration_;
244         }
245       }
246
247       CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
248                               number_temporal_layers_, file_datarate_, 0.78,
249                               1.15);
250
251       memset(file_datarate_, 0, sizeof(file_datarate_));
252       memset(bits_total_, 0, sizeof(bits_total_));
253       int64_t bits_in_buffer_model_tmp[VPX_MAX_LAYERS];
254       last_pts_ref_ = last_pts_;
255       // Set new target bitarate.
256       cfg_.rc_target_bitrate = cfg_.rc_target_bitrate >> 1;
257       // Buffer level should not reset on dynamic bitrate change.
258       memcpy(bits_in_buffer_model_tmp, bits_in_buffer_model_,
259              sizeof(bits_in_buffer_model_));
260       AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
261                           cfg_.ts_number_layers, cfg_.temporal_layering_mode,
262                           layer_target_avg_bandwidth_, bits_in_buffer_model_);
263       memcpy(bits_in_buffer_model_, bits_in_buffer_model_tmp,
264              sizeof(bits_in_buffer_model_));
265
266       // Change config to update encoder with new bitrate configuration.
267       encoder->Config(&cfg_);
268     }
269
270     if (dynamic_drop_layer_) {
271       if (video->frame() == 50) {
272         // Change layer bitrates to set top layers to 0. This will trigger skip
273         // encoding/dropping of top two spatial layers.
274         cfg_.rc_target_bitrate -=
275             (cfg_.layer_target_bitrate[1] + cfg_.layer_target_bitrate[2]);
276         middle_bitrate_ = cfg_.layer_target_bitrate[1];
277         top_bitrate_ = cfg_.layer_target_bitrate[2];
278         cfg_.layer_target_bitrate[1] = 0;
279         cfg_.layer_target_bitrate[2] = 0;
280         encoder->Config(&cfg_);
281       } else if (video->frame() == 100) {
282         // Change layer bitrate on second layer to non-zero to start
283         // encoding it again.
284         cfg_.layer_target_bitrate[1] = middle_bitrate_;
285         cfg_.rc_target_bitrate += cfg_.layer_target_bitrate[1];
286         encoder->Config(&cfg_);
287       } else if (video->frame() == 200) {
288         // Change layer bitrate on top layer to non-zero to start
289         // encoding it again.
290         cfg_.layer_target_bitrate[2] = top_bitrate_;
291         cfg_.rc_target_bitrate += cfg_.layer_target_bitrate[2];
292         encoder->Config(&cfg_);
293       }
294     }
295     const vpx_rational_t tb = video->timebase();
296     timebase_ = static_cast<double>(tb.num) / tb.den;
297     duration_ = 0;
298   }
299
300   virtual void PostEncodeFrameHook(::libvpx_test::Encoder *encoder) {
301     vpx_svc_layer_id_t layer_id;
302     encoder->Control(VP9E_GET_SVC_LAYER_ID, &layer_id);
303     temporal_layer_id_ = layer_id.temporal_layer_id;
304     for (int sl = 0; sl < number_spatial_layers_; ++sl) {
305       for (int tl = temporal_layer_id_; tl < number_temporal_layers_; ++tl) {
306         const int layer = sl * number_temporal_layers_ + tl;
307         bits_in_buffer_model_[layer] +=
308             static_cast<int64_t>(layer_target_avg_bandwidth_[layer]);
309       }
310     }
311   }
312
313   vpx_codec_err_t parse_superframe_index(const uint8_t *data, size_t data_sz,
314                                          uint32_t sizes[8], int *count) {
315     uint8_t marker;
316     marker = *(data + data_sz - 1);
317     *count = 0;
318     if ((marker & 0xe0) == 0xc0) {
319       const uint32_t frames = (marker & 0x7) + 1;
320       const uint32_t mag = ((marker >> 3) & 0x3) + 1;
321       const size_t index_sz = 2 + mag * frames;
322       // This chunk is marked as having a superframe index but doesn't have
323       // enough data for it, thus it's an invalid superframe index.
324       if (data_sz < index_sz) return VPX_CODEC_CORRUPT_FRAME;
325       {
326         const uint8_t marker2 = *(data + data_sz - index_sz);
327         // This chunk is marked as having a superframe index but doesn't have
328         // the matching marker byte at the front of the index therefore it's an
329         // invalid chunk.
330         if (marker != marker2) return VPX_CODEC_CORRUPT_FRAME;
331       }
332       {
333         uint32_t i, j;
334         const uint8_t *x = &data[data_sz - index_sz + 1];
335         for (i = 0; i < frames; ++i) {
336           uint32_t this_sz = 0;
337
338           for (j = 0; j < mag; ++j) this_sz |= (*x++) << (j * 8);
339           sizes[i] = this_sz;
340         }
341         *count = frames;
342       }
343     }
344     return VPX_CODEC_OK;
345   }
346
347   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
348     uint32_t sizes[8] = { 0 };
349     uint32_t sizes_parsed[8] = { 0 };
350     int count = 0;
351     int num_layers_encoded = 0;
352     last_pts_ = pkt->data.frame.pts;
353     const bool key_frame =
354         (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
355     if (key_frame) {
356       temporal_layer_id_ = 0;
357       superframe_count_ = 0;
358     }
359     parse_superframe_index(static_cast<const uint8_t *>(pkt->data.frame.buf),
360                            pkt->data.frame.sz, sizes_parsed, &count);
361     // Count may be less than number of spatial layers because of frame drops.
362     for (int sl = 0; sl < number_spatial_layers_; ++sl) {
363       if (pkt->data.frame.spatial_layer_encoded[sl]) {
364         sizes[sl] = sizes_parsed[num_layers_encoded];
365         num_layers_encoded++;
366       }
367     }
368     ASSERT_EQ(count, num_layers_encoded);
369     // In the constrained frame drop mode, if a given spatial is dropped all
370     // upper layers must be dropped too.
371     if (!layer_framedrop_) {
372       for (int sl = 0; sl < number_spatial_layers_; ++sl) {
373         if (!pkt->data.frame.spatial_layer_encoded[sl]) {
374           // Check that all upper layers are dropped.
375           for (int sl2 = sl + 1; sl2 < number_spatial_layers_; ++sl2)
376             ASSERT_EQ(pkt->data.frame.spatial_layer_encoded[sl2], 0);
377         }
378       }
379     }
380     // Keep track of number of non-reference frames, needed for mismatch check.
381     // Non-reference frames are top spatial and temporal layer frames,
382     // for TL > 0.
383     if (temporal_layer_id_ == number_temporal_layers_ - 1 &&
384         temporal_layer_id_ > 0 &&
385         pkt->data.frame.spatial_layer_encoded[number_spatial_layers_ - 1])
386       num_nonref_frames_++;
387     for (int sl = 0; sl < number_spatial_layers_; ++sl) {
388       sizes[sl] = sizes[sl] << 3;
389       // Update the total encoded bits per layer.
390       // For temporal layers, update the cumulative encoded bits per layer.
391       for (int tl = temporal_layer_id_; tl < number_temporal_layers_; ++tl) {
392         const int layer = sl * number_temporal_layers_ + tl;
393         bits_total_[layer] += static_cast<int64_t>(sizes[sl]);
394         // Update the per-layer buffer level with the encoded frame size.
395         bits_in_buffer_model_[layer] -= static_cast<int64_t>(sizes[sl]);
396         // There should be no buffer underrun, except on the base
397         // temporal layer, since there may be key frames there.
398         // Fo short key frame spacing, buffer can underrun on individual frames.
399         if (!key_frame && tl > 0 && key_frame_spacing_ < 100) {
400           ASSERT_GE(bits_in_buffer_model_[layer], 0)
401               << "Buffer Underrun at frame " << pkt->data.frame.pts;
402         }
403       }
404
405       ASSERT_EQ(pkt->data.frame.width[sl],
406                 top_sl_width_ * svc_params_.scaling_factor_num[sl] /
407                     svc_params_.scaling_factor_den[sl]);
408
409       ASSERT_EQ(pkt->data.frame.height[sl],
410                 top_sl_height_ * svc_params_.scaling_factor_num[sl] /
411                     svc_params_.scaling_factor_den[sl]);
412     }
413   }
414
415   virtual void EndPassHook(void) {
416     if (change_bitrate_) last_pts_ = last_pts_ - last_pts_ref_;
417     duration_ = (last_pts_ + 1) * timebase_;
418     for (int sl = 0; sl < number_spatial_layers_; ++sl) {
419       for (int tl = 0; tl < number_temporal_layers_; ++tl) {
420         const int layer = sl * number_temporal_layers_ + tl;
421         const double file_size_in_kb = bits_total_[layer] / 1000.;
422         file_datarate_[layer] = file_size_in_kb / duration_;
423       }
424     }
425   }
426
427   virtual void MismatchHook(const vpx_image_t *img1, const vpx_image_t *img2) {
428     double mismatch_psnr = compute_psnr(img1, img2);
429     mismatch_psnr_ += mismatch_psnr;
430     ++mismatch_nframes_;
431   }
432
433   unsigned int GetMismatchFrames() { return mismatch_nframes_; }
434
435   vpx_codec_pts_t last_pts_;
436   int64_t bits_in_buffer_model_[VPX_MAX_LAYERS];
437   double timebase_;
438   int64_t bits_total_[VPX_MAX_LAYERS];
439   double duration_;
440   double file_datarate_[VPX_MAX_LAYERS];
441   size_t bits_in_last_frame_;
442   vpx_svc_extra_cfg_t svc_params_;
443   int speed_setting_;
444   double mismatch_psnr_;
445   int mismatch_nframes_;
446   int denoiser_on_;
447   int tune_content_;
448   int base_speed_setting_;
449   int spatial_layer_id_;
450   int temporal_layer_id_;
451   int number_spatial_layers_;
452   int number_temporal_layers_;
453   int layer_target_avg_bandwidth_[VPX_MAX_LAYERS];
454   bool dynamic_drop_layer_;
455   unsigned int top_sl_width_;
456   unsigned int top_sl_height_;
457   vpx_svc_ref_frame_config_t ref_frame_config;
458   int update_pattern_;
459   bool change_bitrate_;
460   vpx_codec_pts_t last_pts_ref_;
461   int middle_bitrate_;
462   int top_bitrate_;
463   int superframe_count_;
464   int key_frame_spacing_;
465   unsigned int num_nonref_frames_;
466   int layer_framedrop_;
467 };
468
469 // Params: speed setting.
470 class DatarateOnePassCbrSvcSingleBR
471     : public DatarateOnePassCbrSvc,
472       public ::libvpx_test::CodecTestWithParam<int> {
473  public:
474   DatarateOnePassCbrSvcSingleBR() : DatarateOnePassCbrSvc(GET_PARAM(0)) {
475     memset(&svc_params_, 0, sizeof(svc_params_));
476   }
477   virtual ~DatarateOnePassCbrSvcSingleBR() {}
478
479  protected:
480   virtual void SetUp() {
481     InitializeConfig();
482     SetMode(::libvpx_test::kRealTime);
483     speed_setting_ = GET_PARAM(1);
484     ResetModel();
485   }
486 };
487
488 // Check basic rate targeting for 1 pass CBR SVC: 2 spatial layers and 1
489 // temporal layer, with screen content mode on and same speed setting for all
490 // layers.
491 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc2SL1TLScreenContent1) {
492   cfg_.rc_buf_initial_sz = 500;
493   cfg_.rc_buf_optimal_sz = 500;
494   cfg_.rc_buf_sz = 1000;
495   cfg_.rc_min_quantizer = 0;
496   cfg_.rc_max_quantizer = 63;
497   cfg_.rc_end_usage = VPX_CBR;
498   cfg_.g_lag_in_frames = 0;
499   cfg_.ss_number_layers = 2;
500   cfg_.ts_number_layers = 1;
501   cfg_.ts_rate_decimator[0] = 1;
502   cfg_.g_error_resilient = 1;
503   cfg_.g_threads = 1;
504   cfg_.temporal_layering_mode = 0;
505   svc_params_.scaling_factor_num[0] = 144;
506   svc_params_.scaling_factor_den[0] = 288;
507   svc_params_.scaling_factor_num[1] = 288;
508   svc_params_.scaling_factor_den[1] = 288;
509   cfg_.rc_dropframe_thresh = 10;
510   cfg_.kf_max_dist = 9999;
511   number_spatial_layers_ = cfg_.ss_number_layers;
512   number_temporal_layers_ = cfg_.ts_number_layers;
513   ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
514   top_sl_width_ = 1280;
515   top_sl_height_ = 720;
516   cfg_.rc_target_bitrate = 500;
517   ResetModel();
518   tune_content_ = 1;
519   base_speed_setting_ = speed_setting_;
520   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
521                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
522                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
523   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
524   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
525                           number_temporal_layers_, file_datarate_, 0.78, 1.15);
526 #if CONFIG_VP9_DECODER
527   // The non-reference frames are expected to be mismatched frames as the
528   // encoder will avoid loopfilter on these frames.
529   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
530 #endif
531 }
532
533 // Check basic rate targeting for 1 pass CBR SVC: 3 spatial layers and
534 // 3 temporal layers. Run CIF clip with 1 thread.
535 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL3TL) {
536   cfg_.rc_buf_initial_sz = 500;
537   cfg_.rc_buf_optimal_sz = 500;
538   cfg_.rc_buf_sz = 1000;
539   cfg_.rc_min_quantizer = 0;
540   cfg_.rc_max_quantizer = 63;
541   cfg_.rc_end_usage = VPX_CBR;
542   cfg_.g_lag_in_frames = 0;
543   cfg_.ss_number_layers = 3;
544   cfg_.ts_number_layers = 3;
545   cfg_.ts_rate_decimator[0] = 4;
546   cfg_.ts_rate_decimator[1] = 2;
547   cfg_.ts_rate_decimator[2] = 1;
548   cfg_.g_error_resilient = 1;
549   cfg_.g_threads = 1;
550   cfg_.temporal_layering_mode = 3;
551   svc_params_.scaling_factor_num[0] = 72;
552   svc_params_.scaling_factor_den[0] = 288;
553   svc_params_.scaling_factor_num[1] = 144;
554   svc_params_.scaling_factor_den[1] = 288;
555   svc_params_.scaling_factor_num[2] = 288;
556   svc_params_.scaling_factor_den[2] = 288;
557   cfg_.rc_dropframe_thresh = 30;
558   cfg_.kf_max_dist = 9999;
559   number_spatial_layers_ = cfg_.ss_number_layers;
560   number_temporal_layers_ = cfg_.ts_number_layers;
561   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
562                                        0, 400);
563   top_sl_width_ = 640;
564   top_sl_height_ = 480;
565   cfg_.rc_target_bitrate = 800;
566   ResetModel();
567   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
568                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
569                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
570   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
571   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
572                           number_temporal_layers_, file_datarate_, 0.78, 1.15);
573 #if CONFIG_VP9_DECODER
574   // The non-reference frames are expected to be mismatched frames as the
575   // encoder will avoid loopfilter on these frames.
576   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
577 #endif
578 }
579
580 // Check rate targeting for 1 pass CBR SVC: 3 spatial layers and
581 // 3 temporal layers, changing the target bitrate at the middle of encoding.
582 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL3TLDynamicBitrateChange) {
583   cfg_.rc_buf_initial_sz = 500;
584   cfg_.rc_buf_optimal_sz = 500;
585   cfg_.rc_buf_sz = 1000;
586   cfg_.rc_min_quantizer = 0;
587   cfg_.rc_max_quantizer = 63;
588   cfg_.rc_end_usage = VPX_CBR;
589   cfg_.g_lag_in_frames = 0;
590   cfg_.ss_number_layers = 3;
591   cfg_.ts_number_layers = 3;
592   cfg_.ts_rate_decimator[0] = 4;
593   cfg_.ts_rate_decimator[1] = 2;
594   cfg_.ts_rate_decimator[2] = 1;
595   cfg_.g_error_resilient = 1;
596   cfg_.g_threads = 1;
597   cfg_.temporal_layering_mode = 3;
598   svc_params_.scaling_factor_num[0] = 72;
599   svc_params_.scaling_factor_den[0] = 288;
600   svc_params_.scaling_factor_num[1] = 144;
601   svc_params_.scaling_factor_den[1] = 288;
602   svc_params_.scaling_factor_num[2] = 288;
603   svc_params_.scaling_factor_den[2] = 288;
604   cfg_.rc_dropframe_thresh = 30;
605   cfg_.kf_max_dist = 9999;
606   number_spatial_layers_ = cfg_.ss_number_layers;
607   number_temporal_layers_ = cfg_.ts_number_layers;
608   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
609                                        0, 400);
610   top_sl_width_ = 640;
611   top_sl_height_ = 480;
612   cfg_.rc_target_bitrate = 800;
613   ResetModel();
614   change_bitrate_ = true;
615   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
616                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
617                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
618   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
619   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
620                           number_temporal_layers_, file_datarate_, 0.78, 1.15);
621 #if CONFIG_VP9_DECODER
622   // The non-reference frames are expected to be mismatched frames as the
623   // encoder will avoid loopfilter on these frames.
624   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
625 #endif
626 }
627
628 // Check basic rate targeting for 1 pass CBR SVC: 3 spatial layers and
629 // 2 temporal layers, with a change on the fly from the fixed SVC pattern to one
630 // generate via SVC_SET_REF_FRAME_CONFIG. The new pattern also disables
631 // inter-layer prediction.
632 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL2TLDynamicPatternChange) {
633   cfg_.rc_buf_initial_sz = 500;
634   cfg_.rc_buf_optimal_sz = 500;
635   cfg_.rc_buf_sz = 1000;
636   cfg_.rc_min_quantizer = 0;
637   cfg_.rc_max_quantizer = 63;
638   cfg_.rc_end_usage = VPX_CBR;
639   cfg_.g_lag_in_frames = 0;
640   cfg_.ss_number_layers = 3;
641   cfg_.ts_number_layers = 2;
642   cfg_.ts_rate_decimator[0] = 2;
643   cfg_.ts_rate_decimator[1] = 1;
644   cfg_.g_error_resilient = 1;
645   cfg_.g_threads = 1;
646   cfg_.temporal_layering_mode = 2;
647   svc_params_.scaling_factor_num[0] = 72;
648   svc_params_.scaling_factor_den[0] = 288;
649   svc_params_.scaling_factor_num[1] = 144;
650   svc_params_.scaling_factor_den[1] = 288;
651   svc_params_.scaling_factor_num[2] = 288;
652   svc_params_.scaling_factor_den[2] = 288;
653   cfg_.rc_dropframe_thresh = 30;
654   cfg_.kf_max_dist = 9999;
655   number_spatial_layers_ = cfg_.ss_number_layers;
656   number_temporal_layers_ = cfg_.ts_number_layers;
657   // Change SVC pattern on the fly.
658   update_pattern_ = 1;
659   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
660                                        0, 400);
661   top_sl_width_ = 640;
662   top_sl_height_ = 480;
663   cfg_.rc_target_bitrate = 800;
664   ResetModel();
665   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
666                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
667                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
668   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
669   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
670                           number_temporal_layers_, file_datarate_, 0.78, 1.15);
671 #if CONFIG_VP9_DECODER
672   // The non-reference frames are expected to be mismatched frames as the
673   // encoder will avoid loopfilter on these frames.
674   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
675 #endif
676 }
677
678 // Check basic rate targeting for 1 pass CBR SVC with 3 spatial layers and on
679 // the fly switching to 1 and then 2 and back to 3 spatial layers. This switch
680 // is done by setting spatial layer bitrates to 0, and then back to non-zero,
681 // during the sequence.
682 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL_DisableEnableLayers) {
683   cfg_.rc_buf_initial_sz = 500;
684   cfg_.rc_buf_optimal_sz = 500;
685   cfg_.rc_buf_sz = 1000;
686   cfg_.rc_min_quantizer = 0;
687   cfg_.rc_max_quantizer = 63;
688   cfg_.rc_end_usage = VPX_CBR;
689   cfg_.g_lag_in_frames = 0;
690   cfg_.ss_number_layers = 3;
691   cfg_.ts_number_layers = 1;
692   cfg_.ts_rate_decimator[0] = 1;
693   cfg_.g_error_resilient = 1;
694   cfg_.g_threads = 1;
695   cfg_.temporal_layering_mode = 0;
696   svc_params_.scaling_factor_num[0] = 72;
697   svc_params_.scaling_factor_den[0] = 288;
698   svc_params_.scaling_factor_num[1] = 144;
699   svc_params_.scaling_factor_den[1] = 288;
700   svc_params_.scaling_factor_num[2] = 288;
701   svc_params_.scaling_factor_den[2] = 288;
702   cfg_.rc_dropframe_thresh = 30;
703   cfg_.kf_max_dist = 9999;
704   number_spatial_layers_ = cfg_.ss_number_layers;
705   number_temporal_layers_ = cfg_.ts_number_layers;
706   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
707                                        0, 400);
708   top_sl_width_ = 640;
709   top_sl_height_ = 480;
710   cfg_.rc_target_bitrate = 800;
711   ResetModel();
712   dynamic_drop_layer_ = true;
713   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
714                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
715                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
716   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
717   // Don't check rate targeting on top spatial layer since it will be skipped
718   // for part of the sequence.
719   CheckLayerRateTargeting(&cfg_, number_spatial_layers_ - 1,
720                           number_temporal_layers_, file_datarate_, 0.78, 1.15);
721 #if CONFIG_VP9_DECODER
722   // The non-reference frames are expected to be mismatched frames as the
723   // encoder will avoid loopfilter on these frames.
724   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
725 #endif
726 }
727
728 // Params: speed setting and index for bitrate array.
729 class DatarateOnePassCbrSvcMultiBR
730     : public DatarateOnePassCbrSvc,
731       public ::libvpx_test::CodecTestWith2Params<int, int> {
732  public:
733   DatarateOnePassCbrSvcMultiBR() : DatarateOnePassCbrSvc(GET_PARAM(0)) {
734     memset(&svc_params_, 0, sizeof(svc_params_));
735   }
736   virtual ~DatarateOnePassCbrSvcMultiBR() {}
737
738  protected:
739   virtual void SetUp() {
740     InitializeConfig();
741     SetMode(::libvpx_test::kRealTime);
742     speed_setting_ = GET_PARAM(1);
743     ResetModel();
744   }
745 };
746
747 // Check basic rate targeting for 1 pass CBR SVC: 2 spatial layers and
748 // 3 temporal layers. Run CIF clip with 1 thread.
749 TEST_P(DatarateOnePassCbrSvcMultiBR, OnePassCbrSvc2SL3TL) {
750   cfg_.rc_buf_initial_sz = 500;
751   cfg_.rc_buf_optimal_sz = 500;
752   cfg_.rc_buf_sz = 1000;
753   cfg_.rc_min_quantizer = 0;
754   cfg_.rc_max_quantizer = 63;
755   cfg_.rc_end_usage = VPX_CBR;
756   cfg_.g_lag_in_frames = 0;
757   cfg_.ss_number_layers = 2;
758   cfg_.ts_number_layers = 3;
759   cfg_.ts_rate_decimator[0] = 4;
760   cfg_.ts_rate_decimator[1] = 2;
761   cfg_.ts_rate_decimator[2] = 1;
762   cfg_.g_error_resilient = 1;
763   cfg_.g_threads = 1;
764   cfg_.temporal_layering_mode = 3;
765   svc_params_.scaling_factor_num[0] = 144;
766   svc_params_.scaling_factor_den[0] = 288;
767   svc_params_.scaling_factor_num[1] = 288;
768   svc_params_.scaling_factor_den[1] = 288;
769   cfg_.rc_dropframe_thresh = 30;
770   cfg_.kf_max_dist = 9999;
771   number_spatial_layers_ = cfg_.ss_number_layers;
772   number_temporal_layers_ = cfg_.ts_number_layers;
773   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
774                                        0, 400);
775   top_sl_width_ = 640;
776   top_sl_height_ = 480;
777   const int bitrates[3] = { 200, 400, 600 };
778   // TODO(marpan): Check that effective_datarate for each layer hits the
779   // layer target_bitrate.
780   cfg_.rc_target_bitrate = bitrates[GET_PARAM(2)];
781   ResetModel();
782   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
783                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
784                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
785   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
786   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
787                           number_temporal_layers_, file_datarate_, 0.75, 1.2);
788 #if CONFIG_VP9_DECODER
789   // The non-reference frames are expected to be mismatched frames as the
790   // encoder will avoid loopfilter on these frames.
791   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
792 #endif
793 }
794
795 // Params: speed setting, layer framedrop control and index for bitrate array.
796 class DatarateOnePassCbrSvcFrameDropMultiBR
797     : public DatarateOnePassCbrSvc,
798       public ::libvpx_test::CodecTestWith3Params<int, int, int> {
799  public:
800   DatarateOnePassCbrSvcFrameDropMultiBR()
801       : DatarateOnePassCbrSvc(GET_PARAM(0)) {
802     memset(&svc_params_, 0, sizeof(svc_params_));
803   }
804   virtual ~DatarateOnePassCbrSvcFrameDropMultiBR() {}
805
806  protected:
807   virtual void SetUp() {
808     InitializeConfig();
809     SetMode(::libvpx_test::kRealTime);
810     speed_setting_ = GET_PARAM(1);
811     ResetModel();
812   }
813 };
814
815 // Check basic rate targeting for 1 pass CBR SVC: 2 spatial layers and
816 // 3 temporal layers. Run HD clip with 4 threads.
817 TEST_P(DatarateOnePassCbrSvcFrameDropMultiBR, OnePassCbrSvc2SL3TL4Threads) {
818   cfg_.rc_buf_initial_sz = 500;
819   cfg_.rc_buf_optimal_sz = 500;
820   cfg_.rc_buf_sz = 1000;
821   cfg_.rc_min_quantizer = 0;
822   cfg_.rc_max_quantizer = 63;
823   cfg_.rc_end_usage = VPX_CBR;
824   cfg_.g_lag_in_frames = 0;
825   cfg_.ss_number_layers = 2;
826   cfg_.ts_number_layers = 3;
827   cfg_.ts_rate_decimator[0] = 4;
828   cfg_.ts_rate_decimator[1] = 2;
829   cfg_.ts_rate_decimator[2] = 1;
830   cfg_.g_error_resilient = 1;
831   cfg_.g_threads = 4;
832   cfg_.temporal_layering_mode = 3;
833   svc_params_.scaling_factor_num[0] = 144;
834   svc_params_.scaling_factor_den[0] = 288;
835   svc_params_.scaling_factor_num[1] = 288;
836   svc_params_.scaling_factor_den[1] = 288;
837   cfg_.rc_dropframe_thresh = 30;
838   cfg_.kf_max_dist = 9999;
839   number_spatial_layers_ = cfg_.ss_number_layers;
840   number_temporal_layers_ = cfg_.ts_number_layers;
841   ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
842   top_sl_width_ = 1280;
843   top_sl_height_ = 720;
844   layer_framedrop_ = 0;
845   const int bitrates[3] = { 200, 400, 600 };
846   cfg_.rc_target_bitrate = bitrates[GET_PARAM(3)];
847   ResetModel();
848   layer_framedrop_ = GET_PARAM(2);
849   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
850                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
851                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
852   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
853   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
854                           number_temporal_layers_, file_datarate_, 0.75, 1.45);
855 #if CONFIG_VP9_DECODER
856   // The non-reference frames are expected to be mismatched frames as the
857   // encoder will avoid loopfilter on these frames.
858   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
859 #endif
860 }
861
862 // Check basic rate targeting for 1 pass CBR SVC: 3 spatial layers and
863 // 3 temporal layers. Run HD clip with 4 threads.
864 TEST_P(DatarateOnePassCbrSvcFrameDropMultiBR, OnePassCbrSvc3SL3TL4Threads) {
865   cfg_.rc_buf_initial_sz = 500;
866   cfg_.rc_buf_optimal_sz = 500;
867   cfg_.rc_buf_sz = 1000;
868   cfg_.rc_min_quantizer = 0;
869   cfg_.rc_max_quantizer = 63;
870   cfg_.rc_end_usage = VPX_CBR;
871   cfg_.g_lag_in_frames = 0;
872   cfg_.ss_number_layers = 3;
873   cfg_.ts_number_layers = 3;
874   cfg_.ts_rate_decimator[0] = 4;
875   cfg_.ts_rate_decimator[1] = 2;
876   cfg_.ts_rate_decimator[2] = 1;
877   cfg_.g_error_resilient = 1;
878   cfg_.g_threads = 4;
879   cfg_.temporal_layering_mode = 3;
880   svc_params_.scaling_factor_num[0] = 72;
881   svc_params_.scaling_factor_den[0] = 288;
882   svc_params_.scaling_factor_num[1] = 144;
883   svc_params_.scaling_factor_den[1] = 288;
884   svc_params_.scaling_factor_num[2] = 288;
885   svc_params_.scaling_factor_den[2] = 288;
886   cfg_.rc_dropframe_thresh = 30;
887   cfg_.kf_max_dist = 9999;
888   number_spatial_layers_ = cfg_.ss_number_layers;
889   number_temporal_layers_ = cfg_.ts_number_layers;
890   ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
891   top_sl_width_ = 1280;
892   top_sl_height_ = 720;
893   layer_framedrop_ = 0;
894   const int bitrates[3] = { 200, 400, 600 };
895   cfg_.rc_target_bitrate = bitrates[GET_PARAM(3)];
896   ResetModel();
897   layer_framedrop_ = GET_PARAM(2);
898   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
899                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
900                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
901   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
902   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
903                           number_temporal_layers_, file_datarate_, 0.73, 1.2);
904 #if CONFIG_VP9_DECODER
905   // The non-reference frames are expected to be mismatched frames as the
906   // encoder will avoid loopfilter on these frames.
907   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
908 #endif
909 }
910
911 // Run SVC encoder for 1 temporal layer, 2 spatial layers, with spatial
912 // downscale 5x5.
913 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc2SL1TL5x5MultipleRuns) {
914   cfg_.rc_buf_initial_sz = 500;
915   cfg_.rc_buf_optimal_sz = 500;
916   cfg_.rc_buf_sz = 1000;
917   cfg_.rc_min_quantizer = 0;
918   cfg_.rc_max_quantizer = 63;
919   cfg_.rc_end_usage = VPX_CBR;
920   cfg_.g_lag_in_frames = 0;
921   cfg_.ss_number_layers = 2;
922   cfg_.ts_number_layers = 1;
923   cfg_.ts_rate_decimator[0] = 1;
924   cfg_.g_error_resilient = 1;
925   cfg_.g_threads = 3;
926   cfg_.temporal_layering_mode = 0;
927   svc_params_.scaling_factor_num[0] = 256;
928   svc_params_.scaling_factor_den[0] = 1280;
929   svc_params_.scaling_factor_num[1] = 1280;
930   svc_params_.scaling_factor_den[1] = 1280;
931   cfg_.rc_dropframe_thresh = 10;
932   cfg_.kf_max_dist = 999999;
933   cfg_.kf_min_dist = 0;
934   cfg_.ss_target_bitrate[0] = 300;
935   cfg_.ss_target_bitrate[1] = 1400;
936   cfg_.layer_target_bitrate[0] = 300;
937   cfg_.layer_target_bitrate[1] = 1400;
938   cfg_.rc_target_bitrate = 1700;
939   number_spatial_layers_ = cfg_.ss_number_layers;
940   number_temporal_layers_ = cfg_.ts_number_layers;
941   ResetModel();
942   layer_target_avg_bandwidth_[0] = cfg_.layer_target_bitrate[0] * 1000 / 30;
943   bits_in_buffer_model_[0] =
944       cfg_.layer_target_bitrate[0] * cfg_.rc_buf_initial_sz;
945   layer_target_avg_bandwidth_[1] = cfg_.layer_target_bitrate[1] * 1000 / 30;
946   bits_in_buffer_model_[1] =
947       cfg_.layer_target_bitrate[1] * cfg_.rc_buf_initial_sz;
948   ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
949   top_sl_width_ = 1280;
950   top_sl_height_ = 720;
951   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
952   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
953                           number_temporal_layers_, file_datarate_, 0.78, 1.15);
954 #if CONFIG_VP9_DECODER
955   // The non-reference frames are expected to be mismatched frames as the
956   // encoder will avoid loopfilter on these frames.
957   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
958 #endif
959 }
960
961 #if CONFIG_VP9_TEMPORAL_DENOISING
962 // Params: speed setting, noise sensitivity and index for bitrate array.
963 class DatarateOnePassCbrSvcDenoiser
964     : public DatarateOnePassCbrSvc,
965       public ::libvpx_test::CodecTestWith3Params<int, int, int> {
966  public:
967   DatarateOnePassCbrSvcDenoiser() : DatarateOnePassCbrSvc(GET_PARAM(0)) {
968     memset(&svc_params_, 0, sizeof(svc_params_));
969   }
970   virtual ~DatarateOnePassCbrSvcDenoiser() {}
971
972  protected:
973   virtual void SetUp() {
974     InitializeConfig();
975     SetMode(::libvpx_test::kRealTime);
976     speed_setting_ = GET_PARAM(1);
977     ResetModel();
978   }
979 };
980
981 // Check basic rate targeting for 1 pass CBR SVC with denoising.
982 // 2 spatial layers and 3 temporal layer. Run HD clip with 2 threads.
983 TEST_P(DatarateOnePassCbrSvcDenoiser, OnePassCbrSvc2SL3TLDenoiserOn) {
984   cfg_.rc_buf_initial_sz = 500;
985   cfg_.rc_buf_optimal_sz = 500;
986   cfg_.rc_buf_sz = 1000;
987   cfg_.rc_min_quantizer = 0;
988   cfg_.rc_max_quantizer = 63;
989   cfg_.rc_end_usage = VPX_CBR;
990   cfg_.g_lag_in_frames = 0;
991   cfg_.ss_number_layers = 2;
992   cfg_.ts_number_layers = 3;
993   cfg_.ts_rate_decimator[0] = 4;
994   cfg_.ts_rate_decimator[1] = 2;
995   cfg_.ts_rate_decimator[2] = 1;
996   cfg_.g_error_resilient = 1;
997   cfg_.g_threads = 2;
998   cfg_.temporal_layering_mode = 3;
999   svc_params_.scaling_factor_num[0] = 144;
1000   svc_params_.scaling_factor_den[0] = 288;
1001   svc_params_.scaling_factor_num[1] = 288;
1002   svc_params_.scaling_factor_den[1] = 288;
1003   cfg_.rc_dropframe_thresh = 30;
1004   cfg_.kf_max_dist = 9999;
1005   number_spatial_layers_ = cfg_.ss_number_layers;
1006   number_temporal_layers_ = cfg_.ts_number_layers;
1007   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1008                                        0, 400);
1009   top_sl_width_ = 640;
1010   top_sl_height_ = 480;
1011   const int bitrates[3] = { 600, 800, 1000 };
1012   // TODO(marpan): Check that effective_datarate for each layer hits the
1013   // layer target_bitrate.
1014   // For SVC, noise_sen = 1 means denoising only the top spatial layer
1015   // noise_sen = 2 means denoising the two top spatial layers.
1016   cfg_.rc_target_bitrate = bitrates[GET_PARAM(3)];
1017   ResetModel();
1018   denoiser_on_ = GET_PARAM(2);
1019   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
1020                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
1021                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
1022   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1023   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
1024                           number_temporal_layers_, file_datarate_, 0.78, 1.15);
1025 #if CONFIG_VP9_DECODER
1026   // The non-reference frames are expected to be mismatched frames as the
1027   // encoder will avoid loopfilter on these frames.
1028   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
1029 #endif
1030 }
1031 #endif
1032
1033 // Params: speed setting, key frame dist.
1034 class DatarateOnePassCbrSvcSmallKF
1035     : public DatarateOnePassCbrSvc,
1036       public ::libvpx_test::CodecTestWith2Params<int, int> {
1037  public:
1038   DatarateOnePassCbrSvcSmallKF() : DatarateOnePassCbrSvc(GET_PARAM(0)) {
1039     memset(&svc_params_, 0, sizeof(svc_params_));
1040   }
1041   virtual ~DatarateOnePassCbrSvcSmallKF() {}
1042
1043  protected:
1044   virtual void SetUp() {
1045     InitializeConfig();
1046     SetMode(::libvpx_test::kRealTime);
1047     speed_setting_ = GET_PARAM(1);
1048     ResetModel();
1049   }
1050 };
1051
1052 // Check basic rate targeting for 1 pass CBR SVC: 3 spatial layers and 3
1053 // temporal layers. Run CIF clip with 1 thread, and few short key frame periods.
1054 TEST_P(DatarateOnePassCbrSvcSmallKF, OnePassCbrSvc3SL3TLSmallKf) {
1055   cfg_.rc_buf_initial_sz = 500;
1056   cfg_.rc_buf_optimal_sz = 500;
1057   cfg_.rc_buf_sz = 1000;
1058   cfg_.rc_min_quantizer = 0;
1059   cfg_.rc_max_quantizer = 63;
1060   cfg_.rc_end_usage = VPX_CBR;
1061   cfg_.g_lag_in_frames = 0;
1062   cfg_.ss_number_layers = 3;
1063   cfg_.ts_number_layers = 3;
1064   cfg_.ts_rate_decimator[0] = 4;
1065   cfg_.ts_rate_decimator[1] = 2;
1066   cfg_.ts_rate_decimator[2] = 1;
1067   cfg_.g_error_resilient = 1;
1068   cfg_.g_threads = 1;
1069   cfg_.temporal_layering_mode = 3;
1070   svc_params_.scaling_factor_num[0] = 72;
1071   svc_params_.scaling_factor_den[0] = 288;
1072   svc_params_.scaling_factor_num[1] = 144;
1073   svc_params_.scaling_factor_den[1] = 288;
1074   svc_params_.scaling_factor_num[2] = 288;
1075   svc_params_.scaling_factor_den[2] = 288;
1076   cfg_.rc_dropframe_thresh = 10;
1077   cfg_.rc_target_bitrate = 800;
1078   number_spatial_layers_ = cfg_.ss_number_layers;
1079   number_temporal_layers_ = cfg_.ts_number_layers;
1080   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1081                                        0, 400);
1082   top_sl_width_ = 640;
1083   top_sl_height_ = 480;
1084   // For this 3 temporal layer case, pattern repeats every 4 frames, so choose
1085   // 4 key neighboring key frame periods (so key frame will land on 0-2-1-2).
1086   const int kf_dist = GET_PARAM(2);
1087   cfg_.kf_max_dist = kf_dist;
1088   key_frame_spacing_ = kf_dist;
1089   ResetModel();
1090   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
1091                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
1092                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
1093   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1094   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
1095                           number_temporal_layers_, file_datarate_, 0.78, 1.15);
1096 #if CONFIG_VP9_DECODER
1097   // The non-reference frames are expected to be mismatched frames as the
1098   // encoder will avoid loopfilter on these frames.
1099   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
1100 #endif
1101 }
1102
1103 // Check basic rate targeting for 1 pass CBR SVC: 2 spatial layers and 3
1104 // temporal layers. Run CIF clip with 1 thread, and few short key frame periods.
1105 TEST_P(DatarateOnePassCbrSvcSmallKF, OnePassCbrSvc2SL3TLSmallKf) {
1106   cfg_.rc_buf_initial_sz = 500;
1107   cfg_.rc_buf_optimal_sz = 500;
1108   cfg_.rc_buf_sz = 1000;
1109   cfg_.rc_min_quantizer = 0;
1110   cfg_.rc_max_quantizer = 63;
1111   cfg_.rc_end_usage = VPX_CBR;
1112   cfg_.g_lag_in_frames = 0;
1113   cfg_.ss_number_layers = 2;
1114   cfg_.ts_number_layers = 3;
1115   cfg_.ts_rate_decimator[0] = 4;
1116   cfg_.ts_rate_decimator[1] = 2;
1117   cfg_.ts_rate_decimator[2] = 1;
1118   cfg_.g_error_resilient = 1;
1119   cfg_.g_threads = 1;
1120   cfg_.temporal_layering_mode = 3;
1121   svc_params_.scaling_factor_num[0] = 144;
1122   svc_params_.scaling_factor_den[0] = 288;
1123   svc_params_.scaling_factor_num[1] = 288;
1124   svc_params_.scaling_factor_den[1] = 288;
1125   cfg_.rc_dropframe_thresh = 10;
1126   cfg_.rc_target_bitrate = 400;
1127   number_spatial_layers_ = cfg_.ss_number_layers;
1128   number_temporal_layers_ = cfg_.ts_number_layers;
1129   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1130                                        0, 400);
1131   top_sl_width_ = 640;
1132   top_sl_height_ = 480;
1133   // For this 3 temporal layer case, pattern repeats every 4 frames, so choose
1134   // 4 key neighboring key frame periods (so key frame will land on 0-2-1-2).
1135   const int kf_dist = GET_PARAM(2) + 32;
1136   cfg_.kf_max_dist = kf_dist;
1137   key_frame_spacing_ = kf_dist;
1138   ResetModel();
1139   AssignLayerBitrates(&cfg_, &svc_params_, cfg_.ss_number_layers,
1140                       cfg_.ts_number_layers, cfg_.temporal_layering_mode,
1141                       layer_target_avg_bandwidth_, bits_in_buffer_model_);
1142   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1143   CheckLayerRateTargeting(&cfg_, number_spatial_layers_,
1144                           number_temporal_layers_, file_datarate_, 0.78, 1.15);
1145 #if CONFIG_VP9_DECODER
1146   // The non-reference frames are expected to be mismatched frames as the
1147   // encoder will avoid loopfilter on these frames.
1148   EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
1149 #endif
1150 }
1151
1152 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcSingleBR,
1153                           ::testing::Range(5, 10));
1154
1155 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcMultiBR, ::testing::Range(5, 10),
1156                           ::testing::Range(0, 3));
1157
1158 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcFrameDropMultiBR,
1159                           ::testing::Range(5, 10), ::testing::Range(0, 2),
1160                           ::testing::Range(0, 3));
1161
1162 #if CONFIG_VP9_TEMPORAL_DENOISING
1163 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcDenoiser,
1164                           ::testing::Range(5, 10), ::testing::Range(1, 3),
1165                           ::testing::Range(0, 3));
1166 #endif
1167
1168 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcSmallKF, ::testing::Range(5, 10),
1169                           ::testing::Range(32, 36));
1170 }  // namespace