]> granicus.if.org Git - libvpx/blob - test/svc_datarate_test.cc
Merge "Add Tile-SB-Row based Multi-threading in Decoder"
[libvpx] / 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/svc_test.h"
16 #include "test/util.h"
17 #include "test/y4m_video_source.h"
18 #include "vp9/common/vp9_onyxc_int.h"
19 #include "vpx/vpx_codec.h"
20 #include "vpx_ports/bitops.h"
21
22 namespace svc_test {
23 namespace {
24
25 class DatarateOnePassCbrSvc : public OnePassCbrSvc {
26  public:
27   explicit DatarateOnePassCbrSvc(const ::libvpx_test::CodecFactory *codec)
28       : OnePassCbrSvc(codec) {
29     inter_layer_pred_mode_ = 0;
30   }
31
32  protected:
33   virtual ~DatarateOnePassCbrSvc() {}
34
35   virtual void ResetModel() {
36     last_pts_ = 0;
37     duration_ = 0.0;
38     mismatch_psnr_ = 0.0;
39     mismatch_nframes_ = 0;
40     denoiser_on_ = 0;
41     tune_content_ = 0;
42     base_speed_setting_ = 5;
43     spatial_layer_id_ = 0;
44     temporal_layer_id_ = 0;
45     update_pattern_ = 0;
46     memset(bits_in_buffer_model_, 0, sizeof(bits_in_buffer_model_));
47     memset(bits_total_, 0, sizeof(bits_total_));
48     memset(layer_target_avg_bandwidth_, 0, sizeof(layer_target_avg_bandwidth_));
49     dynamic_drop_layer_ = false;
50     change_bitrate_ = false;
51     last_pts_ref_ = 0;
52     middle_bitrate_ = 0;
53     top_bitrate_ = 0;
54     superframe_count_ = -1;
55     key_frame_spacing_ = 9999;
56     num_nonref_frames_ = 0;
57     layer_framedrop_ = 0;
58     force_key_ = 0;
59     force_key_test_ = 0;
60     insert_layer_sync_ = 0;
61     layer_sync_on_base_ = 0;
62     force_intra_only_frame_ = 0;
63     superframe_has_intra_only_ = 0;
64     use_post_encode_drop_ = 0;
65     denoiser_off_on_ = false;
66     denoiser_enable_layers_ = false;
67   }
68   virtual void BeginPassHook(unsigned int /*pass*/) {}
69
70   // Example pattern for spatial layers and 2 temporal layers used in the
71   // bypass/flexible mode. The pattern corresponds to the pattern
72   // VP9E_TEMPORAL_LAYERING_MODE_0101 (temporal_layering_mode == 2) used in
73   // non-flexible mode, except that we disable inter-layer prediction.
74   void set_frame_flags_bypass_mode(
75       int tl, int num_spatial_layers, int is_key_frame,
76       vpx_svc_ref_frame_config_t *ref_frame_config) {
77     for (int sl = 0; sl < num_spatial_layers; ++sl)
78       ref_frame_config->update_buffer_slot[sl] = 0;
79
80     for (int sl = 0; sl < num_spatial_layers; ++sl) {
81       if (tl == 0) {
82         ref_frame_config->lst_fb_idx[sl] = sl;
83         if (sl) {
84           if (is_key_frame) {
85             ref_frame_config->lst_fb_idx[sl] = sl - 1;
86             ref_frame_config->gld_fb_idx[sl] = sl;
87           } else {
88             ref_frame_config->gld_fb_idx[sl] = sl - 1;
89           }
90         } else {
91           ref_frame_config->gld_fb_idx[sl] = 0;
92         }
93         ref_frame_config->alt_fb_idx[sl] = 0;
94       } else if (tl == 1) {
95         ref_frame_config->lst_fb_idx[sl] = sl;
96         ref_frame_config->gld_fb_idx[sl] =
97             VPXMIN(REF_FRAMES - 1, num_spatial_layers + sl - 1);
98         ref_frame_config->alt_fb_idx[sl] =
99             VPXMIN(REF_FRAMES - 1, num_spatial_layers + sl);
100       }
101       if (!tl) {
102         if (!sl) {
103           ref_frame_config->reference_last[sl] = 1;
104           ref_frame_config->reference_golden[sl] = 0;
105           ref_frame_config->reference_alt_ref[sl] = 0;
106           ref_frame_config->update_buffer_slot[sl] |=
107               1 << ref_frame_config->lst_fb_idx[sl];
108         } else {
109           if (is_key_frame) {
110             ref_frame_config->reference_last[sl] = 1;
111             ref_frame_config->reference_golden[sl] = 0;
112             ref_frame_config->reference_alt_ref[sl] = 0;
113             ref_frame_config->update_buffer_slot[sl] |=
114                 1 << ref_frame_config->gld_fb_idx[sl];
115           } else {
116             ref_frame_config->reference_last[sl] = 1;
117             ref_frame_config->reference_golden[sl] = 0;
118             ref_frame_config->reference_alt_ref[sl] = 0;
119             ref_frame_config->update_buffer_slot[sl] |=
120                 1 << ref_frame_config->lst_fb_idx[sl];
121           }
122         }
123       } else if (tl == 1) {
124         if (!sl) {
125           ref_frame_config->reference_last[sl] = 1;
126           ref_frame_config->reference_golden[sl] = 0;
127           ref_frame_config->reference_alt_ref[sl] = 0;
128           ref_frame_config->update_buffer_slot[sl] |=
129               1 << ref_frame_config->alt_fb_idx[sl];
130         } else {
131           ref_frame_config->reference_last[sl] = 1;
132           ref_frame_config->reference_golden[sl] = 0;
133           ref_frame_config->reference_alt_ref[sl] = 0;
134           ref_frame_config->update_buffer_slot[sl] |=
135               1 << ref_frame_config->alt_fb_idx[sl];
136         }
137       }
138     }
139   }
140
141   void CheckLayerRateTargeting(int num_spatial_layers, int num_temporal_layers,
142                                double thresh_overshoot,
143                                double thresh_undershoot) const {
144     for (int sl = 0; sl < num_spatial_layers; ++sl)
145       for (int tl = 0; tl < num_temporal_layers; ++tl) {
146         const int layer = sl * num_temporal_layers + tl;
147         ASSERT_GE(cfg_.layer_target_bitrate[layer],
148                   file_datarate_[layer] * thresh_overshoot)
149             << " The datarate for the file exceeds the target by too much!";
150         ASSERT_LE(cfg_.layer_target_bitrate[layer],
151                   file_datarate_[layer] * thresh_undershoot)
152             << " The datarate for the file is lower than the target by too "
153                "much!";
154       }
155   }
156
157   virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
158                                   ::libvpx_test::Encoder *encoder) {
159     PreEncodeFrameHookSetup(video, encoder);
160
161     if (video->frame() == 0) {
162       if (force_intra_only_frame_) {
163         // Decoder sets the color_space for Intra-only frames
164         // to BT_601 (see line 1810 in vp9_decodeframe.c).
165         // So set it here in these tess to avoid encoder-decoder
166         // mismatch check on color space setting.
167         encoder->Control(VP9E_SET_COLOR_SPACE, VPX_CS_BT_601);
168       }
169       encoder->Control(VP9E_SET_NOISE_SENSITIVITY, denoiser_on_);
170       encoder->Control(VP9E_SET_TUNE_CONTENT, tune_content_);
171       encoder->Control(VP9E_SET_SVC_INTER_LAYER_PRED, inter_layer_pred_mode_);
172
173       if (layer_framedrop_) {
174         vpx_svc_frame_drop_t svc_drop_frame;
175         svc_drop_frame.framedrop_mode = LAYER_DROP;
176         for (int i = 0; i < number_spatial_layers_; i++)
177           svc_drop_frame.framedrop_thresh[i] = 30;
178         svc_drop_frame.max_consec_drop = 30;
179         encoder->Control(VP9E_SET_SVC_FRAME_DROP_LAYER, &svc_drop_frame);
180       }
181
182       if (use_post_encode_drop_) {
183         encoder->Control(VP9E_SET_POSTENCODE_DROP, use_post_encode_drop_);
184       }
185     }
186
187     if (denoiser_off_on_) {
188       encoder->Control(VP9E_SET_AQ_MODE, 3);
189       // Set inter_layer_pred to INTER_LAYER_PRED_OFF_NONKEY (K-SVC).
190       encoder->Control(VP9E_SET_SVC_INTER_LAYER_PRED, 2);
191       if (!denoiser_enable_layers_) {
192         if (video->frame() == 0)
193           encoder->Control(VP9E_SET_NOISE_SENSITIVITY, 0);
194         else if (video->frame() == 100)
195           encoder->Control(VP9E_SET_NOISE_SENSITIVITY, 1);
196       } else {
197         // Cumulative bitrates for top spatial layers, for
198         // 3 temporal layers.
199         if (video->frame() == 0) {
200           encoder->Control(VP9E_SET_NOISE_SENSITIVITY, 0);
201           // Change layer bitrates to set top spatial layer to 0.
202           // This is for 3 spatial 3 temporal layers.
203           // This will trigger skip encoding/dropping of top spatial layer.
204           cfg_.rc_target_bitrate -= cfg_.layer_target_bitrate[8];
205           for (int i = 0; i < 3; i++)
206             bitrate_sl3_[i] = cfg_.layer_target_bitrate[i + 6];
207           cfg_.layer_target_bitrate[6] = 0;
208           cfg_.layer_target_bitrate[7] = 0;
209           cfg_.layer_target_bitrate[8] = 0;
210           encoder->Config(&cfg_);
211         } else if (video->frame() == 100) {
212           // Change layer bitrates to non-zero on top spatial layer.
213           // This will trigger skip encoding of top spatial layer
214           // on key frame (period = 100).
215           for (int i = 0; i < 3; i++)
216             cfg_.layer_target_bitrate[i + 6] = bitrate_sl3_[i];
217           cfg_.rc_target_bitrate += cfg_.layer_target_bitrate[8];
218           encoder->Config(&cfg_);
219         } else if (video->frame() == 120) {
220           // Enable denoiser and top spatial layer after key frame (period is
221           // 100).
222           encoder->Control(VP9E_SET_NOISE_SENSITIVITY, 1);
223         }
224       }
225     }
226
227     if (update_pattern_ && video->frame() >= 100) {
228       vpx_svc_layer_id_t layer_id;
229       if (video->frame() == 100) {
230         cfg_.temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS;
231         encoder->Config(&cfg_);
232       }
233       // Set layer id since the pattern changed.
234       layer_id.spatial_layer_id = 0;
235       layer_id.temporal_layer_id = (video->frame() % 2 != 0);
236       temporal_layer_id_ = layer_id.temporal_layer_id;
237       for (int i = 0; i < number_spatial_layers_; i++)
238         layer_id.temporal_layer_id_per_spatial[i] = temporal_layer_id_;
239       encoder->Control(VP9E_SET_SVC_LAYER_ID, &layer_id);
240       set_frame_flags_bypass_mode(layer_id.temporal_layer_id,
241                                   number_spatial_layers_, 0, &ref_frame_config);
242       encoder->Control(VP9E_SET_SVC_REF_FRAME_CONFIG, &ref_frame_config);
243     }
244
245     if (change_bitrate_ && video->frame() == 200) {
246       duration_ = (last_pts_ + 1) * timebase_;
247       for (int sl = 0; sl < number_spatial_layers_; ++sl) {
248         for (int tl = 0; tl < number_temporal_layers_; ++tl) {
249           const int layer = sl * number_temporal_layers_ + tl;
250           const double file_size_in_kb = bits_total_[layer] / 1000.;
251           file_datarate_[layer] = file_size_in_kb / duration_;
252         }
253       }
254
255       CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_,
256                               0.78, 1.15);
257
258       memset(file_datarate_, 0, sizeof(file_datarate_));
259       memset(bits_total_, 0, sizeof(bits_total_));
260       int64_t bits_in_buffer_model_tmp[VPX_MAX_LAYERS];
261       last_pts_ref_ = last_pts_;
262       // Set new target bitarate.
263       cfg_.rc_target_bitrate = cfg_.rc_target_bitrate >> 1;
264       // Buffer level should not reset on dynamic bitrate change.
265       memcpy(bits_in_buffer_model_tmp, bits_in_buffer_model_,
266              sizeof(bits_in_buffer_model_));
267       AssignLayerBitrates();
268       memcpy(bits_in_buffer_model_, bits_in_buffer_model_tmp,
269              sizeof(bits_in_buffer_model_));
270
271       // Change config to update encoder with new bitrate configuration.
272       encoder->Config(&cfg_);
273     }
274
275     if (dynamic_drop_layer_) {
276       // TODO(jian): Disable AQ Mode for this test for now.
277       encoder->Control(VP9E_SET_AQ_MODE, 0);
278       if (video->frame() == 0) {
279         // Change layer bitrates to set top layers to 0. This will trigger skip
280         // encoding/dropping of top two spatial layers.
281         cfg_.rc_target_bitrate -=
282             (cfg_.layer_target_bitrate[1] + cfg_.layer_target_bitrate[2]);
283         middle_bitrate_ = cfg_.layer_target_bitrate[1];
284         top_bitrate_ = cfg_.layer_target_bitrate[2];
285         cfg_.layer_target_bitrate[1] = 0;
286         cfg_.layer_target_bitrate[2] = 0;
287         encoder->Config(&cfg_);
288       } else if (video->frame() == 50) {
289         // Change layer bitrates to non-zero on two top spatial layers.
290         // This will trigger skip encoding of top two spatial layers.
291         cfg_.layer_target_bitrate[1] = middle_bitrate_;
292         cfg_.layer_target_bitrate[2] = top_bitrate_;
293         cfg_.rc_target_bitrate +=
294             cfg_.layer_target_bitrate[2] + cfg_.layer_target_bitrate[1];
295         encoder->Config(&cfg_);
296       } else if (video->frame() == 100) {
297         // Change layer bitrates to set top layers to 0. This will trigger skip
298         // encoding/dropping of top two spatial layers.
299         cfg_.rc_target_bitrate -=
300             (cfg_.layer_target_bitrate[1] + cfg_.layer_target_bitrate[2]);
301         middle_bitrate_ = cfg_.layer_target_bitrate[1];
302         top_bitrate_ = cfg_.layer_target_bitrate[2];
303         cfg_.layer_target_bitrate[1] = 0;
304         cfg_.layer_target_bitrate[2] = 0;
305         encoder->Config(&cfg_);
306       } else if (video->frame() == 150) {
307         // Change layer bitrate on second layer to non-zero to start
308         // encoding it again.
309         cfg_.layer_target_bitrate[1] = middle_bitrate_;
310         cfg_.rc_target_bitrate += cfg_.layer_target_bitrate[1];
311         encoder->Config(&cfg_);
312       } else if (video->frame() == 200) {
313         // Change layer bitrate on top layer to non-zero to start
314         // encoding it again.
315         cfg_.layer_target_bitrate[2] = top_bitrate_;
316         cfg_.rc_target_bitrate += cfg_.layer_target_bitrate[2];
317         encoder->Config(&cfg_);
318       }
319     }
320
321     if (force_key_test_ && force_key_) frame_flags_ = VPX_EFLAG_FORCE_KF;
322
323     if (insert_layer_sync_) {
324       vpx_svc_spatial_layer_sync_t svc_layer_sync;
325       svc_layer_sync.base_layer_intra_only = 0;
326       for (int i = 0; i < number_spatial_layers_; i++)
327         svc_layer_sync.spatial_layer_sync[i] = 0;
328       if (force_intra_only_frame_) {
329         superframe_has_intra_only_ = 0;
330         if (video->frame() == 0) {
331           svc_layer_sync.base_layer_intra_only = 1;
332           svc_layer_sync.spatial_layer_sync[0] = 1;
333           encoder->Control(VP9E_SET_SVC_SPATIAL_LAYER_SYNC, &svc_layer_sync);
334           superframe_has_intra_only_ = 1;
335         } else if (video->frame() == 100) {
336           svc_layer_sync.base_layer_intra_only = 1;
337           svc_layer_sync.spatial_layer_sync[0] = 1;
338           encoder->Control(VP9E_SET_SVC_SPATIAL_LAYER_SYNC, &svc_layer_sync);
339           superframe_has_intra_only_ = 1;
340         }
341       } else {
342         layer_sync_on_base_ = 0;
343         if (video->frame() == 150) {
344           svc_layer_sync.spatial_layer_sync[1] = 1;
345           encoder->Control(VP9E_SET_SVC_SPATIAL_LAYER_SYNC, &svc_layer_sync);
346         } else if (video->frame() == 240) {
347           svc_layer_sync.spatial_layer_sync[2] = 1;
348           encoder->Control(VP9E_SET_SVC_SPATIAL_LAYER_SYNC, &svc_layer_sync);
349         } else if (video->frame() == 320) {
350           svc_layer_sync.spatial_layer_sync[0] = 1;
351           layer_sync_on_base_ = 1;
352           encoder->Control(VP9E_SET_SVC_SPATIAL_LAYER_SYNC, &svc_layer_sync);
353         }
354       }
355     }
356
357     const vpx_rational_t tb = video->timebase();
358     timebase_ = static_cast<double>(tb.num) / tb.den;
359     duration_ = 0;
360   }
361
362   vpx_codec_err_t parse_superframe_index(const uint8_t *data, size_t data_sz,
363                                          uint32_t sizes[8], int *count) {
364     uint8_t marker;
365     marker = *(data + data_sz - 1);
366     *count = 0;
367     if ((marker & 0xe0) == 0xc0) {
368       const uint32_t frames = (marker & 0x7) + 1;
369       const uint32_t mag = ((marker >> 3) & 0x3) + 1;
370       const size_t index_sz = 2 + mag * frames;
371       // This chunk is marked as having a superframe index but doesn't have
372       // enough data for it, thus it's an invalid superframe index.
373       if (data_sz < index_sz) return VPX_CODEC_CORRUPT_FRAME;
374       {
375         const uint8_t marker2 = *(data + data_sz - index_sz);
376         // This chunk is marked as having a superframe index but doesn't have
377         // the matching marker byte at the front of the index therefore it's an
378         // invalid chunk.
379         if (marker != marker2) return VPX_CODEC_CORRUPT_FRAME;
380       }
381       {
382         uint32_t i, j;
383         const uint8_t *x = &data[data_sz - index_sz + 1];
384         for (i = 0; i < frames; ++i) {
385           uint32_t this_sz = 0;
386
387           for (j = 0; j < mag; ++j) this_sz |= (*x++) << (j * 8);
388           sizes[i] = this_sz;
389         }
390         *count = frames;
391       }
392     }
393     return VPX_CODEC_OK;
394   }
395
396   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
397     uint32_t sizes[8] = { 0 };
398     uint32_t sizes_parsed[8] = { 0 };
399     int count = 0;
400     int num_layers_encoded = 0;
401     last_pts_ = pkt->data.frame.pts;
402     const bool key_frame =
403         (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
404     if (key_frame) {
405       // For test that inserts layer sync frames: requesting a layer_sync on
406       // the base layer must force key frame. So if any key frame occurs after
407       // first superframe it must due to layer sync on base spatial layer.
408       if (superframe_count_ > 0 && insert_layer_sync_ &&
409           !force_intra_only_frame_) {
410         ASSERT_EQ(layer_sync_on_base_, 1);
411       }
412       temporal_layer_id_ = 0;
413       superframe_count_ = 0;
414     }
415     parse_superframe_index(static_cast<const uint8_t *>(pkt->data.frame.buf),
416                            pkt->data.frame.sz, sizes_parsed, &count);
417     // Count may be less than number of spatial layers because of frame drops.
418     for (int sl = 0; sl < number_spatial_layers_; ++sl) {
419       if (pkt->data.frame.spatial_layer_encoded[sl]) {
420         sizes[sl] = sizes_parsed[num_layers_encoded];
421         num_layers_encoded++;
422       }
423     }
424     // For superframe with Intra-only count will be +1 larger
425     // because of no-show frame.
426     if (force_intra_only_frame_ && superframe_has_intra_only_)
427       ASSERT_EQ(count, num_layers_encoded + 1);
428     else
429       ASSERT_EQ(count, num_layers_encoded);
430
431     // In the constrained frame drop mode, if a given spatial is dropped all
432     // upper layers must be dropped too.
433     if (!layer_framedrop_) {
434       int num_layers_dropped = 0;
435       for (int sl = 0; sl < number_spatial_layers_; ++sl) {
436         if (!pkt->data.frame.spatial_layer_encoded[sl]) {
437           // Check that all upper layers are dropped.
438           num_layers_dropped++;
439           for (int sl2 = sl + 1; sl2 < number_spatial_layers_; ++sl2)
440             ASSERT_EQ(pkt->data.frame.spatial_layer_encoded[sl2], 0);
441         }
442       }
443       if (num_layers_dropped == number_spatial_layers_ - 1)
444         force_key_ = 1;
445       else
446         force_key_ = 0;
447     }
448     // Keep track of number of non-reference frames, needed for mismatch check.
449     // Non-reference frames are top spatial and temporal layer frames,
450     // for TL > 0.
451     if (temporal_layer_id_ == number_temporal_layers_ - 1 &&
452         temporal_layer_id_ > 0 &&
453         pkt->data.frame.spatial_layer_encoded[number_spatial_layers_ - 1])
454       num_nonref_frames_++;
455     for (int sl = 0; sl < number_spatial_layers_; ++sl) {
456       sizes[sl] = sizes[sl] << 3;
457       // Update the total encoded bits per layer.
458       // For temporal layers, update the cumulative encoded bits per layer.
459       for (int tl = temporal_layer_id_; tl < number_temporal_layers_; ++tl) {
460         const int layer = sl * number_temporal_layers_ + tl;
461         bits_total_[layer] += static_cast<int64_t>(sizes[sl]);
462         // Update the per-layer buffer level with the encoded frame size.
463         bits_in_buffer_model_[layer] -= static_cast<int64_t>(sizes[sl]);
464         // There should be no buffer underrun, except on the base
465         // temporal layer, since there may be key frames there.
466         // Fo short key frame spacing, buffer can underrun on individual frames.
467         if (!key_frame && tl > 0 && key_frame_spacing_ < 100) {
468           ASSERT_GE(bits_in_buffer_model_[layer], 0)
469               << "Buffer Underrun at frame " << pkt->data.frame.pts;
470         }
471       }
472
473       ASSERT_EQ(pkt->data.frame.width[sl],
474                 top_sl_width_ * svc_params_.scaling_factor_num[sl] /
475                     svc_params_.scaling_factor_den[sl]);
476
477       ASSERT_EQ(pkt->data.frame.height[sl],
478                 top_sl_height_ * svc_params_.scaling_factor_num[sl] /
479                     svc_params_.scaling_factor_den[sl]);
480     }
481   }
482
483   virtual void EndPassHook(void) {
484     if (change_bitrate_) last_pts_ = last_pts_ - last_pts_ref_;
485     duration_ = (last_pts_ + 1) * timebase_;
486     for (int sl = 0; sl < number_spatial_layers_; ++sl) {
487       for (int tl = 0; tl < number_temporal_layers_; ++tl) {
488         const int layer = sl * number_temporal_layers_ + tl;
489         const double file_size_in_kb = bits_total_[layer] / 1000.;
490         file_datarate_[layer] = file_size_in_kb / duration_;
491       }
492     }
493   }
494
495   virtual void MismatchHook(const vpx_image_t *img1, const vpx_image_t *img2) {
496     double mismatch_psnr = compute_psnr(img1, img2);
497     mismatch_psnr_ += mismatch_psnr;
498     ++mismatch_nframes_;
499   }
500
501   unsigned int GetMismatchFrames() { return mismatch_nframes_; }
502   unsigned int GetNonRefFrames() { return num_nonref_frames_; }
503
504   vpx_codec_pts_t last_pts_;
505   double timebase_;
506   int64_t bits_total_[VPX_MAX_LAYERS];
507   double duration_;
508   double file_datarate_[VPX_MAX_LAYERS];
509   size_t bits_in_last_frame_;
510   double mismatch_psnr_;
511   int denoiser_on_;
512   int tune_content_;
513   int spatial_layer_id_;
514   bool dynamic_drop_layer_;
515   unsigned int top_sl_width_;
516   unsigned int top_sl_height_;
517   vpx_svc_ref_frame_config_t ref_frame_config;
518   int update_pattern_;
519   bool change_bitrate_;
520   vpx_codec_pts_t last_pts_ref_;
521   int middle_bitrate_;
522   int top_bitrate_;
523   int key_frame_spacing_;
524   int layer_framedrop_;
525   int force_key_;
526   int force_key_test_;
527   int inter_layer_pred_mode_;
528   int insert_layer_sync_;
529   int layer_sync_on_base_;
530   int force_intra_only_frame_;
531   int superframe_has_intra_only_;
532   int use_post_encode_drop_;
533   int bitrate_sl3_[3];
534   // Denoiser switched on the fly.
535   bool denoiser_off_on_;
536   // Top layer enabled on the fly.
537   bool denoiser_enable_layers_;
538
539  private:
540   virtual void SetConfig(const int num_temporal_layer) {
541     cfg_.rc_end_usage = VPX_CBR;
542     cfg_.g_lag_in_frames = 0;
543     cfg_.g_error_resilient = 1;
544     if (num_temporal_layer == 3) {
545       cfg_.ts_rate_decimator[0] = 4;
546       cfg_.ts_rate_decimator[1] = 2;
547       cfg_.ts_rate_decimator[2] = 1;
548       cfg_.temporal_layering_mode = 3;
549     } else if (num_temporal_layer == 2) {
550       cfg_.ts_rate_decimator[0] = 2;
551       cfg_.ts_rate_decimator[1] = 1;
552       cfg_.temporal_layering_mode = 2;
553     } else if (num_temporal_layer == 1) {
554       cfg_.ts_rate_decimator[0] = 1;
555       cfg_.temporal_layering_mode = 0;
556     }
557   }
558
559   unsigned int num_nonref_frames_;
560   unsigned int mismatch_nframes_;
561 };
562
563 // Params: speed setting.
564 class DatarateOnePassCbrSvcSingleBR
565     : public DatarateOnePassCbrSvc,
566       public ::libvpx_test::CodecTestWithParam<int> {
567  public:
568   DatarateOnePassCbrSvcSingleBR() : DatarateOnePassCbrSvc(GET_PARAM(0)) {
569     memset(&svc_params_, 0, sizeof(svc_params_));
570   }
571   virtual ~DatarateOnePassCbrSvcSingleBR() {}
572
573  protected:
574   virtual void SetUp() {
575     InitializeConfig();
576     SetMode(::libvpx_test::kRealTime);
577     speed_setting_ = GET_PARAM(1);
578     ResetModel();
579   }
580 };
581
582 // Check basic rate targeting for 1 pass CBR SVC: 2 spatial layers and 1
583 // temporal layer, with screen content mode on and same speed setting for all
584 // layers.
585 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc2SL1TLScreenContent1) {
586   SetSvcConfig(2, 1);
587   cfg_.rc_buf_initial_sz = 500;
588   cfg_.rc_buf_optimal_sz = 500;
589   cfg_.rc_buf_sz = 1000;
590   cfg_.rc_min_quantizer = 0;
591   cfg_.rc_max_quantizer = 63;
592   cfg_.g_threads = 1;
593   cfg_.rc_dropframe_thresh = 10;
594   cfg_.kf_max_dist = 9999;
595
596   ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
597   top_sl_width_ = 1280;
598   top_sl_height_ = 720;
599   cfg_.rc_target_bitrate = 500;
600   ResetModel();
601   tune_content_ = 1;
602   AssignLayerBitrates();
603   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
604   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.78,
605                           1.15);
606 #if CONFIG_VP9_DECODER
607   // The non-reference frames are expected to be mismatched frames as the
608   // encoder will avoid loopfilter on these frames.
609   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
610 #endif
611 }
612
613 // Check basic rate targeting for 1 pass CBR SVC: 3 spatial layers and
614 // 3 temporal layers, with force key frame after frame drop
615 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL3TLForceKey) {
616   SetSvcConfig(3, 3);
617   cfg_.rc_buf_initial_sz = 500;
618   cfg_.rc_buf_optimal_sz = 500;
619   cfg_.rc_buf_sz = 1000;
620   cfg_.rc_min_quantizer = 0;
621   cfg_.rc_max_quantizer = 63;
622   cfg_.g_threads = 1;
623   cfg_.rc_dropframe_thresh = 30;
624   cfg_.kf_max_dist = 9999;
625   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
626                                        0, 400);
627   top_sl_width_ = 640;
628   top_sl_height_ = 480;
629   cfg_.rc_target_bitrate = 100;
630   ResetModel();
631   AssignLayerBitrates();
632   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
633   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.78,
634                           1.25);
635 #if CONFIG_VP9_DECODER
636   // The non-reference frames are expected to be mismatched frames as the
637   // encoder will avoid loopfilter on these frames.
638   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
639 #endif
640 }
641
642 // Check basic rate targeting for 1 pass CBR SVC: 3 spatial layers and
643 // 2 temporal layers, with a change on the fly from the fixed SVC pattern to one
644 // generate via SVC_SET_REF_FRAME_CONFIG. The new pattern also disables
645 // inter-layer prediction.
646 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL2TLDynamicPatternChange) {
647   SetSvcConfig(3, 2);
648   cfg_.rc_buf_initial_sz = 500;
649   cfg_.rc_buf_optimal_sz = 500;
650   cfg_.rc_buf_sz = 1000;
651   cfg_.rc_min_quantizer = 0;
652   cfg_.rc_max_quantizer = 63;
653   cfg_.g_threads = 1;
654   cfg_.rc_dropframe_thresh = 30;
655   cfg_.kf_max_dist = 9999;
656   // Change SVC pattern on the fly.
657   update_pattern_ = 1;
658   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
659                                        0, 400);
660   top_sl_width_ = 640;
661   top_sl_height_ = 480;
662   cfg_.rc_target_bitrate = 800;
663   ResetModel();
664   AssignLayerBitrates();
665   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
666   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.78,
667                           1.15);
668 #if CONFIG_VP9_DECODER
669   // The non-reference frames are expected to be mismatched frames as the
670   // encoder will avoid loopfilter on these frames.
671   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
672 #endif
673 }
674
675 // Check basic rate targeting for 1 pass CBR SVC with 3 spatial and 3 temporal
676 // layers, for inter_layer_pred=OffKey (K-SVC) and on the fly switching
677 // of denoiser from off to on (on at frame = 100). Key frame period is set to
678 // 1000 so denoise is enabled on non-key.
679 TEST_P(DatarateOnePassCbrSvcSingleBR,
680        OnePassCbrSvc3SL3TL_DenoiserOffOnFixedLayers) {
681   SetSvcConfig(3, 3);
682   cfg_.rc_buf_initial_sz = 500;
683   cfg_.rc_buf_optimal_sz = 500;
684   cfg_.rc_buf_sz = 1000;
685   cfg_.rc_min_quantizer = 0;
686   cfg_.rc_max_quantizer = 63;
687   cfg_.g_threads = 1;
688   cfg_.rc_dropframe_thresh = 30;
689   cfg_.kf_max_dist = 1000;
690   ::libvpx_test::I420VideoSource video("desktop_office1.1280_720-020.yuv", 1280,
691                                        720, 30, 1, 0, 300);
692   top_sl_width_ = 1280;
693   top_sl_height_ = 720;
694   cfg_.rc_target_bitrate = 1000;
695   ResetModel();
696   denoiser_off_on_ = true;
697   denoiser_enable_layers_ = false;
698   AssignLayerBitrates();
699   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
700   // Don't check rate targeting on two top spatial layer since they will be
701   // skipped for part of the sequence.
702   CheckLayerRateTargeting(number_spatial_layers_ - 2, number_temporal_layers_,
703                           0.78, 1.15);
704 #if CONFIG_VP9_DECODER
705   // The non-reference frames are expected to be mismatched frames as the
706   // encoder will avoid loopfilter on these frames.
707   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
708 #endif
709 }
710
711 // Check basic rate targeting for 1 pass CBR SVC with 3 spatial and 3 temporal
712 // layers, for inter_layer_pred=OffKey (K-SVC) and on the fly switching
713 // of denoiser from off to on, for dynamic layers. Start at 2 spatial layers
714 // and enable 3rd spatial layer at frame = 100. Use periodic key frame with
715 // period 100 so enabling of spatial layer occurs at key frame. Enable denoiser
716 // at frame > 100, after the key frame sync.
717 TEST_P(DatarateOnePassCbrSvcSingleBR,
718        OnePassCbrSvc3SL3TL_DenoiserOffOnEnableLayers) {
719   SetSvcConfig(3, 3);
720   cfg_.rc_buf_initial_sz = 500;
721   cfg_.rc_buf_optimal_sz = 500;
722   cfg_.rc_buf_sz = 1000;
723   cfg_.rc_min_quantizer = 0;
724   cfg_.rc_max_quantizer = 63;
725   cfg_.g_threads = 1;
726   cfg_.rc_dropframe_thresh = 0;
727   cfg_.kf_max_dist = 100;
728   ::libvpx_test::I420VideoSource video("desktop_office1.1280_720-020.yuv", 1280,
729                                        720, 30, 1, 0, 300);
730   top_sl_width_ = 1280;
731   top_sl_height_ = 720;
732   cfg_.rc_target_bitrate = 1000;
733   ResetModel();
734   denoiser_off_on_ = true;
735   denoiser_enable_layers_ = true;
736   AssignLayerBitrates();
737   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
738   // Don't check rate targeting on two top spatial layer since they will be
739   // skipped for part of the sequence.
740   CheckLayerRateTargeting(number_spatial_layers_ - 2, number_temporal_layers_,
741                           0.78, 1.15);
742 #if CONFIG_VP9_DECODER
743   // The non-reference frames are expected to be mismatched frames as the
744   // encoder will avoid loopfilter on these frames.
745   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
746 #endif
747 }
748
749 // Check basic rate targeting for 1 pass CBR SVC with 3 spatial layers and on
750 // the fly switching to 1 and then 2 and back to 3 spatial layers. This switch
751 // is done by setting spatial layer bitrates to 0, and then back to non-zero,
752 // during the sequence.
753 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL_DisableEnableLayers) {
754   SetSvcConfig(3, 1);
755   cfg_.rc_buf_initial_sz = 500;
756   cfg_.rc_buf_optimal_sz = 500;
757   cfg_.rc_buf_sz = 1000;
758   cfg_.rc_min_quantizer = 0;
759   cfg_.rc_max_quantizer = 63;
760   cfg_.g_threads = 1;
761   cfg_.temporal_layering_mode = 0;
762   cfg_.rc_dropframe_thresh = 30;
763   cfg_.kf_max_dist = 9999;
764   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
765                                        0, 400);
766   top_sl_width_ = 640;
767   top_sl_height_ = 480;
768   cfg_.rc_target_bitrate = 800;
769   ResetModel();
770   dynamic_drop_layer_ = true;
771   AssignLayerBitrates();
772   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
773   // Don't check rate targeting on two top spatial layer since they will be
774   // skipped for part of the sequence.
775   CheckLayerRateTargeting(number_spatial_layers_ - 2, number_temporal_layers_,
776                           0.78, 1.15);
777 #if CONFIG_VP9_DECODER
778   // The non-reference frames are expected to be mismatched frames as the
779   // encoder will avoid loopfilter on these frames.
780   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
781 #endif
782 }
783
784 // Run SVC encoder for 1 temporal layer, 2 spatial layers, with spatial
785 // downscale 5x5.
786 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc2SL1TL5x5MultipleRuns) {
787   cfg_.rc_buf_initial_sz = 500;
788   cfg_.rc_buf_optimal_sz = 500;
789   cfg_.rc_buf_sz = 1000;
790   cfg_.rc_min_quantizer = 0;
791   cfg_.rc_max_quantizer = 63;
792   cfg_.rc_end_usage = VPX_CBR;
793   cfg_.g_lag_in_frames = 0;
794   cfg_.ss_number_layers = 2;
795   cfg_.ts_number_layers = 1;
796   cfg_.ts_rate_decimator[0] = 1;
797   cfg_.g_error_resilient = 1;
798   cfg_.g_threads = 3;
799   cfg_.temporal_layering_mode = 0;
800   svc_params_.scaling_factor_num[0] = 256;
801   svc_params_.scaling_factor_den[0] = 1280;
802   svc_params_.scaling_factor_num[1] = 1280;
803   svc_params_.scaling_factor_den[1] = 1280;
804   cfg_.rc_dropframe_thresh = 10;
805   cfg_.kf_max_dist = 999999;
806   cfg_.kf_min_dist = 0;
807   cfg_.ss_target_bitrate[0] = 300;
808   cfg_.ss_target_bitrate[1] = 1400;
809   cfg_.layer_target_bitrate[0] = 300;
810   cfg_.layer_target_bitrate[1] = 1400;
811   cfg_.rc_target_bitrate = 1700;
812   number_spatial_layers_ = cfg_.ss_number_layers;
813   number_temporal_layers_ = cfg_.ts_number_layers;
814   ResetModel();
815   layer_target_avg_bandwidth_[0] = cfg_.layer_target_bitrate[0] * 1000 / 30;
816   bits_in_buffer_model_[0] =
817       cfg_.layer_target_bitrate[0] * cfg_.rc_buf_initial_sz;
818   layer_target_avg_bandwidth_[1] = cfg_.layer_target_bitrate[1] * 1000 / 30;
819   bits_in_buffer_model_[1] =
820       cfg_.layer_target_bitrate[1] * cfg_.rc_buf_initial_sz;
821   ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
822   top_sl_width_ = 1280;
823   top_sl_height_ = 720;
824   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
825   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.78,
826                           1.15);
827 #if CONFIG_VP9_DECODER
828   // The non-reference frames are expected to be mismatched frames as the
829   // encoder will avoid loopfilter on these frames.
830   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
831 #endif
832 }
833
834 // Params: speed setting and index for bitrate array.
835 class DatarateOnePassCbrSvcMultiBR
836     : public DatarateOnePassCbrSvc,
837       public ::libvpx_test::CodecTestWith2Params<int, int> {
838  public:
839   DatarateOnePassCbrSvcMultiBR() : DatarateOnePassCbrSvc(GET_PARAM(0)) {
840     memset(&svc_params_, 0, sizeof(svc_params_));
841   }
842   virtual ~DatarateOnePassCbrSvcMultiBR() {}
843
844  protected:
845   virtual void SetUp() {
846     InitializeConfig();
847     SetMode(::libvpx_test::kRealTime);
848     speed_setting_ = GET_PARAM(1);
849     ResetModel();
850   }
851 };
852
853 // Check basic rate targeting for 1 pass CBR SVC: 2 spatial layers and
854 // 3 temporal layers. Run CIF clip with 1 thread.
855 TEST_P(DatarateOnePassCbrSvcMultiBR, OnePassCbrSvc2SL3TL) {
856   SetSvcConfig(2, 3);
857   cfg_.rc_buf_initial_sz = 500;
858   cfg_.rc_buf_optimal_sz = 500;
859   cfg_.rc_buf_sz = 1000;
860   cfg_.rc_min_quantizer = 0;
861   cfg_.rc_max_quantizer = 63;
862   cfg_.g_threads = 1;
863   cfg_.rc_dropframe_thresh = 30;
864   cfg_.kf_max_dist = 9999;
865   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
866                                        0, 400);
867   top_sl_width_ = 640;
868   top_sl_height_ = 480;
869   const int bitrates[3] = { 200, 400, 600 };
870   // TODO(marpan): Check that effective_datarate for each layer hits the
871   // layer target_bitrate.
872   cfg_.rc_target_bitrate = bitrates[GET_PARAM(2)];
873   ResetModel();
874   AssignLayerBitrates();
875   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
876   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.75,
877                           1.2);
878 #if CONFIG_VP9_DECODER
879   // The non-reference frames are expected to be mismatched frames as the
880   // encoder will avoid loopfilter on these frames.
881   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
882 #endif
883 }
884
885 // Params: speed setting, layer framedrop control and index for bitrate array.
886 class DatarateOnePassCbrSvcFrameDropMultiBR
887     : public DatarateOnePassCbrSvc,
888       public ::libvpx_test::CodecTestWith3Params<int, int, int> {
889  public:
890   DatarateOnePassCbrSvcFrameDropMultiBR()
891       : DatarateOnePassCbrSvc(GET_PARAM(0)) {
892     memset(&svc_params_, 0, sizeof(svc_params_));
893   }
894   virtual ~DatarateOnePassCbrSvcFrameDropMultiBR() {}
895
896  protected:
897   virtual void SetUp() {
898     InitializeConfig();
899     SetMode(::libvpx_test::kRealTime);
900     speed_setting_ = GET_PARAM(1);
901     ResetModel();
902   }
903 };
904
905 // Check basic rate targeting for 1 pass CBR SVC: 2 spatial layers and
906 // 3 temporal layers. Run HD clip with 4 threads.
907 TEST_P(DatarateOnePassCbrSvcFrameDropMultiBR, OnePassCbrSvc2SL3TL4Threads) {
908   SetSvcConfig(2, 3);
909   cfg_.rc_buf_initial_sz = 500;
910   cfg_.rc_buf_optimal_sz = 500;
911   cfg_.rc_buf_sz = 1000;
912   cfg_.rc_min_quantizer = 0;
913   cfg_.rc_max_quantizer = 63;
914   cfg_.g_threads = 4;
915   cfg_.rc_dropframe_thresh = 30;
916   cfg_.kf_max_dist = 9999;
917   ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
918   top_sl_width_ = 1280;
919   top_sl_height_ = 720;
920   layer_framedrop_ = 0;
921   const int bitrates[3] = { 200, 400, 600 };
922   cfg_.rc_target_bitrate = bitrates[GET_PARAM(3)];
923   ResetModel();
924   layer_framedrop_ = GET_PARAM(2);
925   AssignLayerBitrates();
926   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
927   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.65,
928                           1.45);
929 #if CONFIG_VP9_DECODER
930   // The non-reference frames are expected to be mismatched frames as the
931   // encoder will avoid loopfilter on these frames.
932   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
933 #endif
934 }
935
936 // Check basic rate targeting for 1 pass CBR SVC: 3 spatial layers and
937 // 3 temporal layers. Run HD clip with 4 threads.
938 TEST_P(DatarateOnePassCbrSvcFrameDropMultiBR, OnePassCbrSvc3SL3TL4Threads) {
939   SetSvcConfig(3, 3);
940   cfg_.rc_buf_initial_sz = 500;
941   cfg_.rc_buf_optimal_sz = 500;
942   cfg_.rc_buf_sz = 1000;
943   cfg_.rc_min_quantizer = 0;
944   cfg_.rc_max_quantizer = 63;
945   cfg_.g_threads = 4;
946   cfg_.rc_dropframe_thresh = 30;
947   cfg_.kf_max_dist = 9999;
948   ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
949   top_sl_width_ = 1280;
950   top_sl_height_ = 720;
951   layer_framedrop_ = 0;
952   const int bitrates[3] = { 200, 400, 600 };
953   cfg_.rc_target_bitrate = bitrates[GET_PARAM(3)];
954   ResetModel();
955   layer_framedrop_ = GET_PARAM(2);
956   AssignLayerBitrates();
957   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
958   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.58,
959                           1.2);
960 #if CONFIG_VP9_DECODER
961   // The non-reference frames are expected to be mismatched frames as the
962   // encoder will avoid loopfilter on these frames.
963   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
964 #endif
965 }
966
967 // Params: speed setting, inter-layer prediction mode.
968 class DatarateOnePassCbrSvcInterLayerPredSingleBR
969     : public DatarateOnePassCbrSvc,
970       public ::libvpx_test::CodecTestWith2Params<int, int> {
971  public:
972   DatarateOnePassCbrSvcInterLayerPredSingleBR()
973       : DatarateOnePassCbrSvc(GET_PARAM(0)) {
974     memset(&svc_params_, 0, sizeof(svc_params_));
975   }
976   virtual ~DatarateOnePassCbrSvcInterLayerPredSingleBR() {}
977
978  protected:
979   virtual void SetUp() {
980     InitializeConfig();
981     SetMode(::libvpx_test::kRealTime);
982     speed_setting_ = GET_PARAM(1);
983     inter_layer_pred_mode_ = GET_PARAM(2);
984     ResetModel();
985   }
986 };
987
988 // Check basic rate targeting with different inter-layer prediction modes for 1
989 // pass CBR SVC: 3 spatial layers and 3 temporal layers. Run CIF clip with 1
990 // thread.
991 TEST_P(DatarateOnePassCbrSvcInterLayerPredSingleBR, OnePassCbrSvc3SL3TL) {
992   SetSvcConfig(3, 3);
993   cfg_.rc_buf_initial_sz = 500;
994   cfg_.rc_buf_optimal_sz = 500;
995   cfg_.rc_buf_sz = 1000;
996   cfg_.rc_min_quantizer = 0;
997   cfg_.rc_max_quantizer = 63;
998   cfg_.g_threads = 1;
999   cfg_.temporal_layering_mode = 3;
1000   cfg_.rc_dropframe_thresh = 30;
1001   cfg_.kf_max_dist = 9999;
1002   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1003                                        0, 400);
1004   top_sl_width_ = 640;
1005   top_sl_height_ = 480;
1006   cfg_.rc_target_bitrate = 800;
1007   ResetModel();
1008   AssignLayerBitrates();
1009   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1010   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.78,
1011                           1.15);
1012 #if CONFIG_VP9_DECODER
1013   // The non-reference frames are expected to be mismatched frames as the
1014   // encoder will avoid loopfilter on these frames.
1015   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
1016 #endif
1017 }
1018
1019 // Check rate targeting with different inter-layer prediction modes for 1 pass
1020 // CBR SVC: 3 spatial layers and 3 temporal layers, changing the target bitrate
1021 // at the middle of encoding.
1022 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL3TLDynamicBitrateChange) {
1023   SetSvcConfig(3, 3);
1024   cfg_.rc_buf_initial_sz = 500;
1025   cfg_.rc_buf_optimal_sz = 500;
1026   cfg_.rc_buf_sz = 1000;
1027   cfg_.rc_min_quantizer = 0;
1028   cfg_.rc_max_quantizer = 63;
1029   cfg_.g_threads = 1;
1030   cfg_.rc_dropframe_thresh = 30;
1031   cfg_.kf_max_dist = 9999;
1032   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1033                                        0, 400);
1034   top_sl_width_ = 640;
1035   top_sl_height_ = 480;
1036   cfg_.rc_target_bitrate = 800;
1037   ResetModel();
1038   change_bitrate_ = true;
1039   AssignLayerBitrates();
1040   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1041   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.78,
1042                           1.15);
1043 #if CONFIG_VP9_DECODER
1044   // The non-reference frames are expected to be mismatched frames as the
1045   // encoder will avoid loopfilter on these frames.
1046   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
1047 #endif
1048 }
1049
1050 #if CONFIG_VP9_TEMPORAL_DENOISING
1051 // Params: speed setting, noise sensitivity and index for bitrate array.
1052 class DatarateOnePassCbrSvcDenoiser
1053     : public DatarateOnePassCbrSvc,
1054       public ::libvpx_test::CodecTestWith3Params<int, int, int> {
1055  public:
1056   DatarateOnePassCbrSvcDenoiser() : DatarateOnePassCbrSvc(GET_PARAM(0)) {
1057     memset(&svc_params_, 0, sizeof(svc_params_));
1058   }
1059   virtual ~DatarateOnePassCbrSvcDenoiser() {}
1060
1061  protected:
1062   virtual void SetUp() {
1063     InitializeConfig();
1064     SetMode(::libvpx_test::kRealTime);
1065     speed_setting_ = GET_PARAM(1);
1066     ResetModel();
1067   }
1068 };
1069
1070 // Check basic rate targeting for 1 pass CBR SVC with denoising.
1071 // 2 spatial layers and 3 temporal layer. Run HD clip with 2 threads.
1072 TEST_P(DatarateOnePassCbrSvcDenoiser, OnePassCbrSvc2SL3TLDenoiserOn) {
1073   SetSvcConfig(2, 3);
1074   cfg_.rc_buf_initial_sz = 500;
1075   cfg_.rc_buf_optimal_sz = 500;
1076   cfg_.rc_buf_sz = 1000;
1077   cfg_.rc_min_quantizer = 0;
1078   cfg_.rc_max_quantizer = 63;
1079   cfg_.g_threads = 2;
1080   cfg_.rc_dropframe_thresh = 30;
1081   cfg_.kf_max_dist = 9999;
1082   number_spatial_layers_ = cfg_.ss_number_layers;
1083   number_temporal_layers_ = cfg_.ts_number_layers;
1084   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1085                                        0, 400);
1086   top_sl_width_ = 640;
1087   top_sl_height_ = 480;
1088   const int bitrates[3] = { 600, 800, 1000 };
1089   // TODO(marpan): Check that effective_datarate for each layer hits the
1090   // layer target_bitrate.
1091   // For SVC, noise_sen = 1 means denoising only the top spatial layer
1092   // noise_sen = 2 means denoising the two top spatial layers.
1093   cfg_.rc_target_bitrate = bitrates[GET_PARAM(3)];
1094   ResetModel();
1095   denoiser_on_ = GET_PARAM(2);
1096   AssignLayerBitrates();
1097   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1098   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.78,
1099                           1.15);
1100 #if CONFIG_VP9_DECODER
1101   // The non-reference frames are expected to be mismatched frames as the
1102   // encoder will avoid loopfilter on these frames.
1103   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
1104 #endif
1105 }
1106 #endif
1107
1108 // Params: speed setting, key frame dist.
1109 class DatarateOnePassCbrSvcSmallKF
1110     : public DatarateOnePassCbrSvc,
1111       public ::libvpx_test::CodecTestWith2Params<int, int> {
1112  public:
1113   DatarateOnePassCbrSvcSmallKF() : DatarateOnePassCbrSvc(GET_PARAM(0)) {
1114     memset(&svc_params_, 0, sizeof(svc_params_));
1115   }
1116   virtual ~DatarateOnePassCbrSvcSmallKF() {}
1117
1118  protected:
1119   virtual void SetUp() {
1120     InitializeConfig();
1121     SetMode(::libvpx_test::kRealTime);
1122     speed_setting_ = GET_PARAM(1);
1123     ResetModel();
1124   }
1125 };
1126
1127 // Check basic rate targeting for 1 pass CBR SVC: 3 spatial layers and 3
1128 // temporal layers. Run CIF clip with 1 thread, and few short key frame periods.
1129 TEST_P(DatarateOnePassCbrSvcSmallKF, OnePassCbrSvc3SL3TLSmallKf) {
1130   SetSvcConfig(3, 3);
1131   cfg_.rc_buf_initial_sz = 500;
1132   cfg_.rc_buf_optimal_sz = 500;
1133   cfg_.rc_buf_sz = 1000;
1134   cfg_.rc_min_quantizer = 0;
1135   cfg_.rc_max_quantizer = 63;
1136   cfg_.g_threads = 1;
1137   cfg_.rc_dropframe_thresh = 10;
1138   cfg_.rc_target_bitrate = 800;
1139   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1140                                        0, 400);
1141   top_sl_width_ = 640;
1142   top_sl_height_ = 480;
1143   // For this 3 temporal layer case, pattern repeats every 4 frames, so choose
1144   // 4 key neighboring key frame periods (so key frame will land on 0-2-1-2).
1145   const int kf_dist = GET_PARAM(2);
1146   cfg_.kf_max_dist = kf_dist;
1147   key_frame_spacing_ = kf_dist;
1148   ResetModel();
1149   AssignLayerBitrates();
1150   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1151   // TODO(jianj): webm:1554
1152   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.70,
1153                           1.15);
1154 #if CONFIG_VP9_DECODER
1155   // The non-reference frames are expected to be mismatched frames as the
1156   // encoder will avoid loopfilter on these frames.
1157   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
1158 #endif
1159 }
1160
1161 // Check basic rate targeting for 1 pass CBR SVC: 2 spatial layers and 3
1162 // temporal layers. Run CIF clip with 1 thread, and few short key frame periods.
1163 TEST_P(DatarateOnePassCbrSvcSmallKF, OnePassCbrSvc2SL3TLSmallKf) {
1164   SetSvcConfig(2, 3);
1165   cfg_.rc_buf_initial_sz = 500;
1166   cfg_.rc_buf_optimal_sz = 500;
1167   cfg_.rc_buf_sz = 1000;
1168   cfg_.rc_min_quantizer = 0;
1169   cfg_.rc_max_quantizer = 63;
1170   cfg_.g_threads = 1;
1171   cfg_.rc_dropframe_thresh = 10;
1172   cfg_.rc_target_bitrate = 400;
1173   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1174                                        0, 400);
1175   top_sl_width_ = 640;
1176   top_sl_height_ = 480;
1177   // For this 3 temporal layer case, pattern repeats every 4 frames, so choose
1178   // 4 key neighboring key frame periods (so key frame will land on 0-2-1-2).
1179   const int kf_dist = GET_PARAM(2) + 32;
1180   cfg_.kf_max_dist = kf_dist;
1181   key_frame_spacing_ = kf_dist;
1182   ResetModel();
1183   AssignLayerBitrates();
1184   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1185   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.78,
1186                           1.15);
1187 #if CONFIG_VP9_DECODER
1188   // The non-reference frames are expected to be mismatched frames as the
1189   // encoder will avoid loopfilter on these frames.
1190   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
1191 #endif
1192 }
1193
1194 // Check basic rate targeting for 1 pass CBR SVC: 3 spatial layers and 3
1195 // temporal layers. Run VGA clip with 1 thread, and place layer sync frames:
1196 // one at middle layer first, then another one for top layer, and another
1197 // insert for base spatial layer (which forces key frame).
1198 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL3TLSyncFrames) {
1199   SetSvcConfig(3, 3);
1200   cfg_.rc_buf_initial_sz = 500;
1201   cfg_.rc_buf_optimal_sz = 500;
1202   cfg_.rc_buf_sz = 1000;
1203   cfg_.rc_min_quantizer = 0;
1204   cfg_.rc_max_quantizer = 63;
1205   cfg_.g_threads = 1;
1206   cfg_.kf_max_dist = 9999;
1207   cfg_.rc_dropframe_thresh = 10;
1208   cfg_.rc_target_bitrate = 400;
1209   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1210                                        0, 400);
1211   top_sl_width_ = 640;
1212   top_sl_height_ = 480;
1213   ResetModel();
1214   insert_layer_sync_ = 1;
1215   AssignLayerBitrates();
1216   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1217   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.78,
1218                           1.15);
1219 #if CONFIG_VP9_DECODER
1220   // The non-reference frames are expected to be mismatched frames as the
1221   // encoder will avoid loopfilter on these frames.
1222   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
1223 #endif
1224 }
1225
1226 // Run SVC encoder for 3 spatial layers, 1 temporal layer, with
1227 // intra-only frame as sync frame on base spatial layer.
1228 // Intra_only is inserted at start and in middle of sequence.
1229 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc3SL1TLSyncWithIntraOnly) {
1230   SetSvcConfig(3, 1);
1231   cfg_.rc_buf_initial_sz = 500;
1232   cfg_.rc_buf_optimal_sz = 500;
1233   cfg_.rc_buf_sz = 1000;
1234   cfg_.rc_min_quantizer = 0;
1235   cfg_.rc_max_quantizer = 63;
1236   cfg_.g_threads = 4;
1237   cfg_.rc_dropframe_thresh = 30;
1238   cfg_.kf_max_dist = 9999;
1239   cfg_.rc_target_bitrate = 400;
1240   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1241                                        0, 400);
1242   top_sl_width_ = 640;
1243   top_sl_height_ = 480;
1244   ResetModel();
1245   insert_layer_sync_ = 1;
1246   // Use intra_only frame for sync on base layer.
1247   force_intra_only_frame_ = 1;
1248   AssignLayerBitrates();
1249   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1250   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.73,
1251                           1.2);
1252 #if CONFIG_VP9_DECODER
1253   // The non-reference frames are expected to be mismatched frames as the
1254   // encoder will avoid loopfilter on these frames.
1255   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
1256 #endif
1257 }
1258
1259 // Run SVC encoder for 2 quality layers (same resolution different,
1260 // bitrates), 1 temporal layer, with screen content mode.
1261 TEST_P(DatarateOnePassCbrSvcSingleBR, OnePassCbrSvc2QL1TLScreen) {
1262   cfg_.rc_buf_initial_sz = 500;
1263   cfg_.rc_buf_optimal_sz = 500;
1264   cfg_.rc_buf_sz = 1000;
1265   cfg_.rc_min_quantizer = 0;
1266   cfg_.rc_max_quantizer = 56;
1267   cfg_.rc_end_usage = VPX_CBR;
1268   cfg_.g_lag_in_frames = 0;
1269   cfg_.ss_number_layers = 2;
1270   cfg_.ts_number_layers = 1;
1271   cfg_.ts_rate_decimator[0] = 1;
1272   cfg_.temporal_layering_mode = 0;
1273   cfg_.g_error_resilient = 1;
1274   cfg_.g_threads = 2;
1275   svc_params_.scaling_factor_num[0] = 1;
1276   svc_params_.scaling_factor_den[0] = 1;
1277   svc_params_.scaling_factor_num[1] = 1;
1278   svc_params_.scaling_factor_den[1] = 1;
1279   cfg_.rc_dropframe_thresh = 30;
1280   cfg_.kf_max_dist = 9999;
1281   number_spatial_layers_ = cfg_.ss_number_layers;
1282   number_temporal_layers_ = cfg_.ts_number_layers;
1283   ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
1284                                        0, 400);
1285   top_sl_width_ = 640;
1286   top_sl_height_ = 480;
1287   ResetModel();
1288   tune_content_ = 1;
1289   // Set the layer bitrates, for 2 spatial layers, 1 temporal.
1290   cfg_.rc_target_bitrate = 400;
1291   cfg_.ss_target_bitrate[0] = 100;
1292   cfg_.ss_target_bitrate[1] = 300;
1293   cfg_.layer_target_bitrate[0] = 100;
1294   cfg_.layer_target_bitrate[1] = 300;
1295   for (int sl = 0; sl < 2; ++sl) {
1296     float layer_framerate = 30.0;
1297     layer_target_avg_bandwidth_[sl] = static_cast<int>(
1298         cfg_.layer_target_bitrate[sl] * 1000.0 / layer_framerate);
1299     bits_in_buffer_model_[sl] =
1300         cfg_.layer_target_bitrate[sl] * cfg_.rc_buf_initial_sz;
1301   }
1302   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1303   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.73,
1304                           1.25);
1305 #if CONFIG_VP9_DECODER
1306   // The non-reference frames are expected to be mismatched frames as the
1307   // encoder will avoid loopfilter on these frames.
1308   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
1309 #endif
1310 }
1311
1312 // Params: speed setting.
1313 class DatarateOnePassCbrSvcPostencodeDrop
1314     : public DatarateOnePassCbrSvc,
1315       public ::libvpx_test::CodecTestWithParam<int> {
1316  public:
1317   DatarateOnePassCbrSvcPostencodeDrop() : DatarateOnePassCbrSvc(GET_PARAM(0)) {
1318     memset(&svc_params_, 0, sizeof(svc_params_));
1319   }
1320   virtual ~DatarateOnePassCbrSvcPostencodeDrop() {}
1321
1322  protected:
1323   virtual void SetUp() {
1324     InitializeConfig();
1325     SetMode(::libvpx_test::kRealTime);
1326     speed_setting_ = GET_PARAM(1);
1327     ResetModel();
1328   }
1329 };
1330
1331 // Run SVC encoder for 2 quality layers (same resolution different,
1332 // bitrates), 1 temporal layer, with screen content mode.
1333 TEST_P(DatarateOnePassCbrSvcPostencodeDrop, OnePassCbrSvc2QL1TLScreen) {
1334   cfg_.rc_buf_initial_sz = 200;
1335   cfg_.rc_buf_optimal_sz = 200;
1336   cfg_.rc_buf_sz = 400;
1337   cfg_.rc_min_quantizer = 0;
1338   cfg_.rc_max_quantizer = 52;
1339   cfg_.rc_end_usage = VPX_CBR;
1340   cfg_.g_lag_in_frames = 0;
1341   cfg_.ss_number_layers = 2;
1342   cfg_.ts_number_layers = 1;
1343   cfg_.ts_rate_decimator[0] = 1;
1344   cfg_.temporal_layering_mode = 0;
1345   cfg_.g_error_resilient = 1;
1346   cfg_.g_threads = 2;
1347   svc_params_.scaling_factor_num[0] = 1;
1348   svc_params_.scaling_factor_den[0] = 1;
1349   svc_params_.scaling_factor_num[1] = 1;
1350   svc_params_.scaling_factor_den[1] = 1;
1351   cfg_.rc_dropframe_thresh = 30;
1352   cfg_.kf_max_dist = 9999;
1353   number_spatial_layers_ = cfg_.ss_number_layers;
1354   number_temporal_layers_ = cfg_.ts_number_layers;
1355   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
1356                                        30, 1, 0, 300);
1357   top_sl_width_ = 352;
1358   top_sl_height_ = 288;
1359   ResetModel();
1360   base_speed_setting_ = speed_setting_;
1361   tune_content_ = 1;
1362   use_post_encode_drop_ = 1;
1363   // Set the layer bitrates, for 2 spatial layers, 1 temporal.
1364   cfg_.rc_target_bitrate = 400;
1365   cfg_.ss_target_bitrate[0] = 100;
1366   cfg_.ss_target_bitrate[1] = 300;
1367   cfg_.layer_target_bitrate[0] = 100;
1368   cfg_.layer_target_bitrate[1] = 300;
1369   for (int sl = 0; sl < 2; ++sl) {
1370     float layer_framerate = 30.0;
1371     layer_target_avg_bandwidth_[sl] = static_cast<int>(
1372         cfg_.layer_target_bitrate[sl] * 1000.0 / layer_framerate);
1373     bits_in_buffer_model_[sl] =
1374         cfg_.layer_target_bitrate[sl] * cfg_.rc_buf_initial_sz;
1375   }
1376   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
1377   CheckLayerRateTargeting(number_spatial_layers_, number_temporal_layers_, 0.73,
1378                           1.25);
1379 #if CONFIG_VP9_DECODER
1380   // The non-reference frames are expected to be mismatched frames as the
1381   // encoder will avoid loopfilter on these frames.
1382   EXPECT_EQ(GetNonRefFrames(), GetMismatchFrames());
1383 #endif
1384 }
1385
1386 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcSingleBR,
1387                           ::testing::Range(5, 10));
1388
1389 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcPostencodeDrop,
1390                           ::testing::Range(4, 5));
1391
1392 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcInterLayerPredSingleBR,
1393                           ::testing::Range(5, 10), ::testing::Range(0, 3));
1394
1395 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcMultiBR, ::testing::Range(5, 10),
1396                           ::testing::Range(0, 3));
1397
1398 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcFrameDropMultiBR,
1399                           ::testing::Range(5, 10), ::testing::Range(0, 2),
1400                           ::testing::Range(0, 3));
1401
1402 #if CONFIG_VP9_TEMPORAL_DENOISING
1403 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcDenoiser,
1404                           ::testing::Range(5, 10), ::testing::Range(1, 3),
1405                           ::testing::Range(0, 3));
1406 #endif
1407
1408 VP9_INSTANTIATE_TEST_CASE(DatarateOnePassCbrSvcSmallKF, ::testing::Range(5, 10),
1409                           ::testing::Range(32, 36));
1410 }  // namespace
1411 }  // namespace svc_test