2 * Copyright (c) 2018 The WebM project authors. All Rights Reserved.
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.
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 "vpx/vpx_codec.h"
19 #include "vpx_ports/bitops.h"
23 // Params: Inter layer prediction modes.
24 class SyncFrameOnePassCbrSvc : public ::svc_test::OnePassCbrSvc,
25 public ::libvpx_test::CodecTestWithParam<int> {
27 SyncFrameOnePassCbrSvc()
28 : OnePassCbrSvc(GET_PARAM(0)), current_video_frame_(0),
29 frame_to_start_decode_(0), frame_to_sync_(0), mismatch_nframes_(0),
30 num_nonref_frames_(0), inter_layer_pred_mode_(GET_PARAM(1)),
31 decode_to_layer_before_sync_(-1), decode_to_layer_after_sync_(-1),
32 denoiser_on_(0), intra_only_test_(false) {
33 SetMode(::libvpx_test::kRealTime);
34 memset(&svc_layer_sync_, 0, sizeof(svc_layer_sync_));
38 virtual ~SyncFrameOnePassCbrSvc() {}
40 virtual void SetUp() {
45 virtual bool DoDecode() const {
46 return current_video_frame_ >= frame_to_start_decode_;
49 virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
50 ::libvpx_test::Encoder *encoder) {
51 current_video_frame_ = video->frame();
52 PreEncodeFrameHookSetup(video, encoder);
53 if (video->frame() == 0) {
54 encoder->Control(VP9E_SET_SVC_INTER_LAYER_PRED, inter_layer_pred_mode_);
55 encoder->Control(VP9E_SET_NOISE_SENSITIVITY, denoiser_on_);
57 // Decoder sets the color_space for Intra-only frames
58 // to BT_601 (see line 1810 in vp9_decodeframe.c).
59 // So set it here in these tess to avoid encoder-decoder
60 // mismatch check on color space setting.
61 encoder->Control(VP9E_SET_COLOR_SPACE, VPX_CS_BT_601);
63 if (video->frame() == frame_to_sync_) {
64 encoder->Control(VP9E_SET_SVC_SPATIAL_LAYER_SYNC, &svc_layer_sync_);
68 #if CONFIG_VP9_DECODER
69 virtual void PreDecodeFrameHook(::libvpx_test::VideoSource *video,
70 ::libvpx_test::Decoder *decoder) {
71 if (video->frame() < frame_to_sync_) {
72 if (decode_to_layer_before_sync_ >= 0)
73 decoder->Control(VP9_DECODE_SVC_SPATIAL_LAYER,
74 decode_to_layer_before_sync_);
76 if (decode_to_layer_after_sync_ >= 0)
77 decoder->Control(VP9_DECODE_SVC_SPATIAL_LAYER,
78 decode_to_layer_after_sync_);
83 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
84 // Keep track of number of non-reference frames, needed for mismatch check.
85 // Non-reference frames are top spatial and temporal layer frames,
87 if (temporal_layer_id_ == number_temporal_layers_ - 1 &&
88 temporal_layer_id_ > 0 &&
89 pkt->data.frame.spatial_layer_encoded[number_spatial_layers_ - 1] &&
90 current_video_frame_ >= frame_to_sync_)
93 if (intra_only_test_ && current_video_frame_ == frame_to_sync_) {
94 // Intra-only frame is only generated for spatial layers > 1 and <= 3,
95 // among other conditions (see constraint in set_intra_only_frame(). If
96 // intra-only is no allowed then encoder will insert key frame instead.
97 const bool key_frame =
98 (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
99 if (number_spatial_layers_ == 1 || number_spatial_layers_ > 3)
100 ASSERT_TRUE(key_frame);
102 ASSERT_FALSE(key_frame);
106 virtual void MismatchHook(const vpx_image_t * /*img1*/,
107 const vpx_image_t * /*img2*/) {
108 if (current_video_frame_ >= frame_to_sync_) ++mismatch_nframes_;
111 unsigned int GetMismatchFrames() const { return mismatch_nframes_; }
113 unsigned int current_video_frame_;
114 unsigned int frame_to_start_decode_;
115 unsigned int frame_to_sync_;
116 unsigned int mismatch_nframes_;
117 unsigned int num_nonref_frames_;
118 int inter_layer_pred_mode_;
119 int decode_to_layer_before_sync_;
120 int decode_to_layer_after_sync_;
122 bool intra_only_test_;
123 vpx_svc_spatial_layer_sync_t svc_layer_sync_;
126 virtual void SetConfig(const int num_temporal_layer) {
127 cfg_.rc_buf_initial_sz = 500;
128 cfg_.rc_buf_optimal_sz = 500;
129 cfg_.rc_buf_sz = 1000;
130 cfg_.rc_min_quantizer = 0;
131 cfg_.rc_max_quantizer = 63;
132 cfg_.rc_end_usage = VPX_CBR;
133 cfg_.g_lag_in_frames = 0;
134 cfg_.g_error_resilient = 1;
136 cfg_.rc_dropframe_thresh = 30;
137 cfg_.kf_max_dist = 9999;
138 if (num_temporal_layer == 3) {
139 cfg_.ts_rate_decimator[0] = 4;
140 cfg_.ts_rate_decimator[1] = 2;
141 cfg_.ts_rate_decimator[2] = 1;
142 cfg_.temporal_layering_mode = 3;
143 } else if (num_temporal_layer == 2) {
144 cfg_.ts_rate_decimator[0] = 2;
145 cfg_.ts_rate_decimator[1] = 1;
146 cfg_.temporal_layering_mode = 2;
147 } else if (num_temporal_layer == 1) {
148 cfg_.ts_rate_decimator[0] = 1;
149 cfg_.temporal_layering_mode = 1;
154 // Test for sync layer for 1 pass CBR SVC: 3 spatial layers and
155 // 3 temporal layers. Only start decoding on the sync layer.
156 // Full sync: insert key frame on base layer.
157 TEST_P(SyncFrameOnePassCbrSvc, OnePassCbrSvc3SL3TLFullSync) {
159 // Sync is on base layer so the frame to sync and the frame to start decoding
161 frame_to_start_decode_ = 20;
163 decode_to_layer_before_sync_ = -1;
164 decode_to_layer_after_sync_ = 2;
166 // Set up svc layer sync structure.
167 svc_layer_sync_.base_layer_intra_only = 0;
168 svc_layer_sync_.spatial_layer_sync[0] = 1;
170 ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
172 cfg_.rc_target_bitrate = 600;
173 AssignLayerBitrates();
174 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
175 #if CONFIG_VP9_DECODER
176 // The non-reference frames are expected to be mismatched frames as the
177 // encoder will avoid loopfilter on these frames.
178 EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
182 // Test for sync layer for 1 pass CBR SVC: 2 spatial layers and
183 // 3 temporal layers. Decoding QVGA before sync frame and decode up to
184 // VGA on and after sync.
185 TEST_P(SyncFrameOnePassCbrSvc, OnePassCbrSvc2SL3TLSyncToVGA) {
187 frame_to_start_decode_ = 0;
188 frame_to_sync_ = 100;
189 decode_to_layer_before_sync_ = 0;
190 decode_to_layer_after_sync_ = 1;
192 // Set up svc layer sync structure.
193 svc_layer_sync_.base_layer_intra_only = 0;
194 svc_layer_sync_.spatial_layer_sync[0] = 0;
195 svc_layer_sync_.spatial_layer_sync[1] = 1;
197 ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
199 cfg_.rc_target_bitrate = 400;
200 AssignLayerBitrates();
201 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
202 #if CONFIG_VP9_DECODER
203 // The non-reference frames are expected to be mismatched frames as the
204 // encoder will avoid loopfilter on these frames.
205 EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
209 // Test for sync layer for 1 pass CBR SVC: 3 spatial layers and
210 // 3 temporal layers. Decoding QVGA and VGA before sync frame and decode up to
211 // HD on and after sync.
212 TEST_P(SyncFrameOnePassCbrSvc, OnePassCbrSvc3SL3TLSyncToHD) {
214 frame_to_start_decode_ = 0;
216 decode_to_layer_before_sync_ = 1;
217 decode_to_layer_after_sync_ = 2;
219 // Set up svc layer sync structure.
220 svc_layer_sync_.base_layer_intra_only = 0;
221 svc_layer_sync_.spatial_layer_sync[0] = 0;
222 svc_layer_sync_.spatial_layer_sync[1] = 0;
223 svc_layer_sync_.spatial_layer_sync[2] = 1;
225 ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
226 cfg_.rc_target_bitrate = 600;
227 AssignLayerBitrates();
228 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
229 #if CONFIG_VP9_DECODER
230 // The non-reference frames are expected to be mismatched frames as the
231 // encoder will avoid loopfilter on these frames.
232 EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
236 // Test for sync layer for 1 pass CBR SVC: 3 spatial layers and
237 // 3 temporal layers. Decoding QVGA before sync frame and decode up to
238 // HD on and after sync.
239 TEST_P(SyncFrameOnePassCbrSvc, OnePassCbrSvc3SL3TLSyncToVGAHD) {
241 frame_to_start_decode_ = 0;
243 decode_to_layer_before_sync_ = 0;
244 decode_to_layer_after_sync_ = 2;
246 // Set up svc layer sync structure.
247 svc_layer_sync_.base_layer_intra_only = 0;
248 svc_layer_sync_.spatial_layer_sync[0] = 0;
249 svc_layer_sync_.spatial_layer_sync[1] = 1;
250 svc_layer_sync_.spatial_layer_sync[2] = 1;
252 ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
253 cfg_.rc_target_bitrate = 600;
254 AssignLayerBitrates();
255 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
256 #if CONFIG_VP9_DECODER
257 // The non-reference frames are expected to be mismatched frames as the
258 // encoder will avoid loopfilter on these frames.
259 EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
263 #if CONFIG_VP9_TEMPORAL_DENOISING
264 // Test for sync layer for 1 pass CBR SVC: 2 spatial layers and
265 // 3 temporal layers. Decoding QVGA before sync frame and decode up to
266 // VGA on and after sync.
267 TEST_P(SyncFrameOnePassCbrSvc, OnePassCbrSvc2SL3TLSyncFrameVGADenoise) {
269 frame_to_start_decode_ = 0;
270 frame_to_sync_ = 100;
271 decode_to_layer_before_sync_ = 0;
272 decode_to_layer_after_sync_ = 1;
275 // Set up svc layer sync structure.
276 svc_layer_sync_.base_layer_intra_only = 0;
277 svc_layer_sync_.spatial_layer_sync[0] = 0;
278 svc_layer_sync_.spatial_layer_sync[1] = 1;
280 ::libvpx_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30, 1,
282 cfg_.rc_target_bitrate = 400;
283 AssignLayerBitrates();
284 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
285 #if CONFIG_VP9_DECODER
286 // The non-reference frames are expected to be mismatched frames as the
287 // encoder will avoid loopfilter on these frames.
288 EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
293 // Start decoding from beginning of sequence, during sequence insert intra-only
294 // on base/qvga layer. Decode all layers.
295 TEST_P(SyncFrameOnePassCbrSvc, OnePassCbrSvc3SL3TLSyncFrameIntraOnlyQVGA) {
297 frame_to_start_decode_ = 0;
299 decode_to_layer_before_sync_ = 2;
300 // The superframe containing intra-only layer will have 4 frames. Thus set the
301 // layer to decode after sync frame to 3.
302 decode_to_layer_after_sync_ = 3;
303 intra_only_test_ = true;
305 // Set up svc layer sync structure.
306 svc_layer_sync_.base_layer_intra_only = 1;
307 svc_layer_sync_.spatial_layer_sync[0] = 1;
308 svc_layer_sync_.spatial_layer_sync[1] = 0;
309 svc_layer_sync_.spatial_layer_sync[2] = 0;
311 ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
312 cfg_.rc_target_bitrate = 600;
313 AssignLayerBitrates();
314 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
315 #if CONFIG_VP9_DECODER
316 // The non-reference frames are expected to be mismatched frames as the
317 // encoder will avoid loopfilter on these frames.
318 EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
322 // Start decoding from beginning of sequence, during sequence insert intra-only
323 // on base/qvga layer and sync_layer on middle/VGA layer. Decode all layers.
324 TEST_P(SyncFrameOnePassCbrSvc, OnePassCbrSvc3SL3TLSyncFrameIntraOnlyVGA) {
326 frame_to_start_decode_ = 0;
328 decode_to_layer_before_sync_ = 2;
329 // The superframe containing intra-only layer will have 4 frames. Thus set the
330 // layer to decode after sync frame to 3.
331 decode_to_layer_after_sync_ = 3;
332 intra_only_test_ = true;
334 // Set up svc layer sync structure.
335 svc_layer_sync_.base_layer_intra_only = 1;
336 svc_layer_sync_.spatial_layer_sync[0] = 1;
337 svc_layer_sync_.spatial_layer_sync[1] = 1;
338 svc_layer_sync_.spatial_layer_sync[2] = 0;
340 ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
341 cfg_.rc_target_bitrate = 600;
342 AssignLayerBitrates();
343 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
344 #if CONFIG_VP9_DECODER
345 // The non-reference frames are expected to be mismatched frames as the
346 // encoder will avoid loopfilter on these frames.
347 EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
351 // Start decoding from sync frame, insert intra-only on base/qvga layer. Decode
352 // all layers. For 1 spatial layer, it inserts a key frame.
353 TEST_P(SyncFrameOnePassCbrSvc, OnePassCbrSvc1SL3TLSyncFrameIntraOnlyQVGA) {
355 frame_to_start_decode_ = 20;
357 decode_to_layer_before_sync_ = 0;
358 decode_to_layer_after_sync_ = 0;
359 intra_only_test_ = true;
361 // Set up svc layer sync structure.
362 svc_layer_sync_.base_layer_intra_only = 1;
363 svc_layer_sync_.spatial_layer_sync[0] = 1;
365 ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
366 cfg_.rc_target_bitrate = 600;
367 AssignLayerBitrates();
368 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
369 #if CONFIG_VP9_DECODER
370 // The non-reference frames are expected to be mismatched frames as the
371 // encoder will avoid loopfilter on these frames.
372 EXPECT_EQ(num_nonref_frames_, GetMismatchFrames());
376 VP9_INSTANTIATE_TEST_CASE(SyncFrameOnePassCbrSvc, ::testing::Range(0, 3));