Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 1 | /* |
Frank Galligan | 38536f6 | 2013-12-12 08:36:34 -0800 | [diff] [blame] | 2 | * Copyright (c) 2013 The WebM project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 10 | |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 11 | #include "third_party/googletest/src/include/gtest/gtest.h" |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 12 | #include "test/codec_factory.h" |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 13 | #include "test/encode_test_driver.h" |
| 14 | #include "test/i420_video_source.h" |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 15 | #include "test/util.h" |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 16 | |
| 17 | namespace { |
| 18 | |
Marco Paniconi | f61b962 | 2014-02-24 18:14:50 -0800 | [diff] [blame] | 19 | const int kMaxErrorFrames = 12; |
| 20 | const int kMaxDroppableFrames = 12; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 21 | |
Jim Bankoski | a0b5ed6 | 2014-03-12 08:13:16 -0700 | [diff] [blame] | 22 | class ErrorResilienceTestLarge : public ::libvpx_test::EncoderTest, |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 23 | public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> { |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 24 | protected: |
Jim Bankoski | a0b5ed6 | 2014-03-12 08:13:16 -0700 | [diff] [blame] | 25 | ErrorResilienceTestLarge() |
| 26 | : EncoderTest(GET_PARAM(0)), |
| 27 | psnr_(0.0), |
| 28 | nframes_(0), |
| 29 | mismatch_psnr_(0.0), |
| 30 | mismatch_nframes_(0), |
| 31 | encoding_mode_(GET_PARAM(1)) { |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 32 | Reset(); |
| 33 | } |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 34 | |
Jim Bankoski | a0b5ed6 | 2014-03-12 08:13:16 -0700 | [diff] [blame] | 35 | virtual ~ErrorResilienceTestLarge() {} |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 36 | |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 37 | void Reset() { |
| 38 | error_nframes_ = 0; |
| 39 | droppable_nframes_ = 0; |
| 40 | } |
| 41 | |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 42 | virtual void SetUp() { |
| 43 | InitializeConfig(); |
| 44 | SetMode(encoding_mode_); |
| 45 | } |
| 46 | |
| 47 | virtual void BeginPassHook(unsigned int /*pass*/) { |
| 48 | psnr_ = 0.0; |
| 49 | nframes_ = 0; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 50 | mismatch_psnr_ = 0.0; |
| 51 | mismatch_nframes_ = 0; |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 54 | virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) { |
| 55 | psnr_ += pkt->data.psnr.psnr[0]; |
| 56 | nframes_++; |
| 57 | } |
| 58 | |
Marco | 8fd5525 | 2014-10-29 15:34:18 -0700 | [diff] [blame] | 59 | // |
| 60 | // Frame flags and layer id for temporal layers. |
| 61 | // For two layers, test pattern is: |
| 62 | // 1 3 |
| 63 | // 0 2 ..... |
| 64 | // LAST is updated on base/layer 0, GOLDEN updated on layer 1. |
| 65 | int SetFrameFlags(int frame_num, int num_temp_layers) { |
| 66 | int frame_flags = 0; |
| 67 | if (num_temp_layers == 2) { |
| 68 | if (frame_num % 2 == 0) { |
| 69 | // Layer 0: predict from L and ARF, update L. |
| 70 | frame_flags = VP8_EFLAG_NO_REF_GF | |
| 71 | VP8_EFLAG_NO_UPD_GF | |
| 72 | VP8_EFLAG_NO_UPD_ARF; |
| 73 | } else { |
| 74 | // Layer 1: predict from L, GF, and ARF, and update GF. |
| 75 | frame_flags = VP8_EFLAG_NO_UPD_ARF | |
| 76 | VP8_EFLAG_NO_UPD_LAST; |
| 77 | } |
| 78 | } |
| 79 | return frame_flags; |
| 80 | } |
| 81 | |
| 82 | virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video, |
| 83 | ::libvpx_test::Encoder *encoder) { |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 84 | frame_flags_ &= ~(VP8_EFLAG_NO_UPD_LAST | |
| 85 | VP8_EFLAG_NO_UPD_GF | |
| 86 | VP8_EFLAG_NO_UPD_ARF); |
Marco | 8fd5525 | 2014-10-29 15:34:18 -0700 | [diff] [blame] | 87 | // For temporal layer case. |
| 88 | if (cfg_.ts_number_layers > 1) { |
| 89 | frame_flags_ = SetFrameFlags(video->frame(), cfg_.ts_number_layers); |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 90 | for (unsigned int i = 0; i < droppable_nframes_; ++i) { |
Deb Mukherjee | 0d8723f | 2013-08-19 14:16:26 -0700 | [diff] [blame] | 91 | if (droppable_frames_[i] == video->frame()) { |
Marco | 8fd5525 | 2014-10-29 15:34:18 -0700 | [diff] [blame] | 92 | std::cout << "Encoding droppable frame: " |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 93 | << droppable_frames_[i] << "\n"; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
Marco | 8fd5525 | 2014-10-29 15:34:18 -0700 | [diff] [blame] | 96 | } else { |
| 97 | if (droppable_nframes_ > 0 && |
| 98 | (cfg_.g_pass == VPX_RC_LAST_PASS || cfg_.g_pass == VPX_RC_ONE_PASS)) { |
| 99 | for (unsigned int i = 0; i < droppable_nframes_; ++i) { |
| 100 | if (droppable_frames_[i] == video->frame()) { |
| 101 | std::cout << "Encoding droppable frame: " |
| 102 | << droppable_frames_[i] << "\n"; |
| 103 | frame_flags_ |= (VP8_EFLAG_NO_UPD_LAST | |
| 104 | VP8_EFLAG_NO_UPD_GF | |
| 105 | VP8_EFLAG_NO_UPD_ARF); |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | } |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 113 | double GetAveragePsnr() const { |
| 114 | if (nframes_) |
| 115 | return psnr_ / nframes_; |
| 116 | return 0.0; |
| 117 | } |
| 118 | |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 119 | double GetAverageMismatchPsnr() const { |
| 120 | if (mismatch_nframes_) |
| 121 | return mismatch_psnr_ / mismatch_nframes_; |
| 122 | return 0.0; |
| 123 | } |
| 124 | |
| 125 | virtual bool DoDecode() const { |
| 126 | if (error_nframes_ > 0 && |
| 127 | (cfg_.g_pass == VPX_RC_LAST_PASS || cfg_.g_pass == VPX_RC_ONE_PASS)) { |
| 128 | for (unsigned int i = 0; i < error_nframes_; ++i) { |
| 129 | if (error_frames_[i] == nframes_ - 1) { |
| 130 | std::cout << " Skipping decoding frame: " |
| 131 | << error_frames_[i] << "\n"; |
| 132 | return 0; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | virtual void MismatchHook(const vpx_image_t *img1, |
| 140 | const vpx_image_t *img2) { |
| 141 | double mismatch_psnr = compute_psnr(img1, img2); |
| 142 | mismatch_psnr_ += mismatch_psnr; |
| 143 | ++mismatch_nframes_; |
| 144 | // std::cout << "Mismatch frame psnr: " << mismatch_psnr << "\n"; |
| 145 | } |
| 146 | |
| 147 | void SetErrorFrames(int num, unsigned int *list) { |
| 148 | if (num > kMaxErrorFrames) |
| 149 | num = kMaxErrorFrames; |
| 150 | else if (num < 0) |
| 151 | num = 0; |
| 152 | error_nframes_ = num; |
| 153 | for (unsigned int i = 0; i < error_nframes_; ++i) |
| 154 | error_frames_[i] = list[i]; |
| 155 | } |
| 156 | |
| 157 | void SetDroppableFrames(int num, unsigned int *list) { |
| 158 | if (num > kMaxDroppableFrames) |
| 159 | num = kMaxDroppableFrames; |
| 160 | else if (num < 0) |
| 161 | num = 0; |
| 162 | droppable_nframes_ = num; |
| 163 | for (unsigned int i = 0; i < droppable_nframes_; ++i) |
| 164 | droppable_frames_[i] = list[i]; |
| 165 | } |
| 166 | |
| 167 | unsigned int GetMismatchFrames() { |
| 168 | return mismatch_nframes_; |
| 169 | } |
| 170 | |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 171 | private: |
| 172 | double psnr_; |
| 173 | unsigned int nframes_; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 174 | unsigned int error_nframes_; |
| 175 | unsigned int droppable_nframes_; |
| 176 | double mismatch_psnr_; |
| 177 | unsigned int mismatch_nframes_; |
| 178 | unsigned int error_frames_[kMaxErrorFrames]; |
| 179 | unsigned int droppable_frames_[kMaxDroppableFrames]; |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 180 | libvpx_test::TestMode encoding_mode_; |
| 181 | }; |
| 182 | |
Jim Bankoski | a0b5ed6 | 2014-03-12 08:13:16 -0700 | [diff] [blame] | 183 | TEST_P(ErrorResilienceTestLarge, OnVersusOff) { |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 184 | const vpx_rational timebase = { 33333333, 1000000000 }; |
| 185 | cfg_.g_timebase = timebase; |
| 186 | cfg_.rc_target_bitrate = 2000; |
Deb Mukherjee | 0d8723f | 2013-08-19 14:16:26 -0700 | [diff] [blame] | 187 | cfg_.g_lag_in_frames = 10; |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 188 | |
| 189 | init_flags_ = VPX_CODEC_USE_PSNR; |
| 190 | |
| 191 | libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
| 192 | timebase.den, timebase.num, 0, 30); |
| 193 | |
| 194 | // Error resilient mode OFF. |
| 195 | cfg_.g_error_resilient = 0; |
| 196 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 197 | const double psnr_resilience_off = GetAveragePsnr(); |
| 198 | EXPECT_GT(psnr_resilience_off, 25.0); |
| 199 | |
| 200 | // Error resilient mode ON. |
| 201 | cfg_.g_error_resilient = 1; |
| 202 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 203 | const double psnr_resilience_on = GetAveragePsnr(); |
| 204 | EXPECT_GT(psnr_resilience_on, 25.0); |
| 205 | |
| 206 | // Test that turning on error resilient mode hurts by 10% at most. |
| 207 | if (psnr_resilience_off > 0.0) { |
| 208 | const double psnr_ratio = psnr_resilience_on / psnr_resilience_off; |
| 209 | EXPECT_GE(psnr_ratio, 0.9); |
| 210 | EXPECT_LE(psnr_ratio, 1.1); |
| 211 | } |
| 212 | } |
| 213 | |
Marco Paniconi | f61b962 | 2014-02-24 18:14:50 -0800 | [diff] [blame] | 214 | // Check for successful decoding and no encoder/decoder mismatch |
| 215 | // if we lose (i.e., drop before decoding) a set of droppable |
| 216 | // frames (i.e., frames that don't update any reference buffers). |
| 217 | // Check both isolated and consecutive loss. |
Jim Bankoski | a0b5ed6 | 2014-03-12 08:13:16 -0700 | [diff] [blame] | 218 | TEST_P(ErrorResilienceTestLarge, DropFramesWithoutRecovery) { |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 219 | const vpx_rational timebase = { 33333333, 1000000000 }; |
| 220 | cfg_.g_timebase = timebase; |
John Koleszar | c0490a5 | 2013-05-07 12:58:32 -0700 | [diff] [blame] | 221 | cfg_.rc_target_bitrate = 500; |
Deb Mukherjee | 0d8723f | 2013-08-19 14:16:26 -0700 | [diff] [blame] | 222 | // FIXME(debargha): Fix this to work for any lag. |
| 223 | // Currently this test only works for lag = 0 |
| 224 | cfg_.g_lag_in_frames = 0; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 225 | |
| 226 | init_flags_ = VPX_CODEC_USE_PSNR; |
| 227 | |
| 228 | libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
Marco Paniconi | f61b962 | 2014-02-24 18:14:50 -0800 | [diff] [blame] | 229 | timebase.den, timebase.num, 0, 40); |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 230 | |
| 231 | // Error resilient mode ON. |
| 232 | cfg_.g_error_resilient = 1; |
Marco Paniconi | f61b962 | 2014-02-24 18:14:50 -0800 | [diff] [blame] | 233 | cfg_.kf_mode = VPX_KF_DISABLED; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 234 | |
Marco Paniconi | f61b962 | 2014-02-24 18:14:50 -0800 | [diff] [blame] | 235 | // Set an arbitrary set of error frames same as droppable frames. |
| 236 | // In addition to isolated loss/drop, add a long consecutive series |
| 237 | // (of size 9) of dropped frames. |
| 238 | unsigned int num_droppable_frames = 11; |
| 239 | unsigned int droppable_frame_list[] = {5, 16, 22, 23, 24, 25, 26, 27, 28, |
| 240 | 29, 30}; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 241 | SetDroppableFrames(num_droppable_frames, droppable_frame_list); |
| 242 | SetErrorFrames(num_droppable_frames, droppable_frame_list); |
| 243 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 244 | // Test that no mismatches have been found |
| 245 | std::cout << " Mismatch frames: " |
| 246 | << GetMismatchFrames() << "\n"; |
| 247 | EXPECT_EQ(GetMismatchFrames(), (unsigned int) 0); |
| 248 | |
Marco Paniconi | f61b962 | 2014-02-24 18:14:50 -0800 | [diff] [blame] | 249 | // Reset previously set of error/droppable frames. |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 250 | Reset(); |
| 251 | |
John Koleszar | 9fba034 | 2013-05-07 13:02:45 -0700 | [diff] [blame] | 252 | #if 0 |
| 253 | // TODO(jkoleszar): This test is disabled for the time being as too |
| 254 | // sensitive. It's not clear how to set a reasonable threshold for |
| 255 | // this behavior. |
| 256 | |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 257 | // Now set an arbitrary set of error frames that are non-droppable |
| 258 | unsigned int num_error_frames = 3; |
| 259 | unsigned int error_frame_list[] = {3, 10, 20}; |
| 260 | SetErrorFrames(num_error_frames, error_frame_list); |
| 261 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
John Koleszar | 9fba034 | 2013-05-07 13:02:45 -0700 | [diff] [blame] | 262 | |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 263 | // Test that dropping an arbitrary set of inter frames does not hurt too much |
| 264 | // Note the Average Mismatch PSNR is the average of the PSNR between |
| 265 | // decoded frame and encoder's version of the same frame for all frames |
| 266 | // with mismatch. |
| 267 | const double psnr_resilience_mismatch = GetAverageMismatchPsnr(); |
| 268 | std::cout << " Mismatch PSNR: " |
| 269 | << psnr_resilience_mismatch << "\n"; |
| 270 | EXPECT_GT(psnr_resilience_mismatch, 20.0); |
John Koleszar | 9fba034 | 2013-05-07 13:02:45 -0700 | [diff] [blame] | 271 | #endif |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Marco | 8fd5525 | 2014-10-29 15:34:18 -0700 | [diff] [blame] | 274 | // Check for successful decoding and no encoder/decoder mismatch |
| 275 | // if we lose (i.e., drop before decoding) the enhancement layer frames for a |
| 276 | // two layer temporal pattern. The base layer does not predict from the top |
| 277 | // layer, so successful decoding is expected. |
| 278 | TEST_P(ErrorResilienceTestLarge, 2LayersDropEnhancement) { |
| 279 | const vpx_rational timebase = { 33333333, 1000000000 }; |
| 280 | cfg_.g_timebase = timebase; |
| 281 | cfg_.rc_target_bitrate = 500; |
| 282 | cfg_.g_lag_in_frames = 0; |
| 283 | |
| 284 | cfg_.rc_end_usage = VPX_CBR; |
| 285 | // 2 Temporal layers, no spatial layers, CBR mode. |
| 286 | cfg_.ss_number_layers = 1; |
| 287 | cfg_.ts_number_layers = 2; |
| 288 | cfg_.ts_rate_decimator[0] = 2; |
| 289 | cfg_.ts_rate_decimator[1] = 1; |
| 290 | cfg_.ts_periodicity = 2; |
| 291 | cfg_.ts_target_bitrate[0] = 60 * cfg_.rc_target_bitrate / 100; |
| 292 | cfg_.ts_target_bitrate[1] = cfg_.rc_target_bitrate; |
| 293 | |
| 294 | init_flags_ = VPX_CODEC_USE_PSNR; |
| 295 | |
| 296 | libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
| 297 | timebase.den, timebase.num, 0, 40); |
| 298 | |
| 299 | // Error resilient mode ON. |
| 300 | cfg_.g_error_resilient = 1; |
| 301 | cfg_.kf_mode = VPX_KF_DISABLED; |
| 302 | |
| 303 | // The odd frames are the enhancement layer for 2 layer pattern, so set |
| 304 | // those frames as droppable. Drop the last 7 frames. |
| 305 | unsigned int num_droppable_frames = 7; |
| 306 | unsigned int droppable_frame_list[] = {27, 29, 31, 33, 35, 37, 39}; |
| 307 | SetDroppableFrames(num_droppable_frames, droppable_frame_list); |
| 308 | SetErrorFrames(num_droppable_frames, droppable_frame_list); |
| 309 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 310 | // Test that no mismatches have been found |
| 311 | std::cout << " Mismatch frames: " |
| 312 | << GetMismatchFrames() << "\n"; |
| 313 | EXPECT_EQ(GetMismatchFrames(), (unsigned int) 0); |
| 314 | |
| 315 | // Reset previously set of error/droppable frames. |
| 316 | Reset(); |
| 317 | } |
| 318 | |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 319 | class ErrorResilienceTestLargeCodecControls : public ::libvpx_test::EncoderTest, |
| 320 | public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> { |
| 321 | protected: |
| 322 | ErrorResilienceTestLargeCodecControls() |
| 323 | : EncoderTest(GET_PARAM(0)), |
| 324 | encoding_mode_(GET_PARAM(1)) { |
| 325 | Reset(); |
| 326 | } |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 327 | |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 328 | virtual ~ErrorResilienceTestLargeCodecControls() {} |
| 329 | |
| 330 | void Reset() { |
| 331 | last_pts_ = 0; |
| 332 | tot_frame_number_ = 0; |
| 333 | // For testing up to 3 layers. |
| 334 | for (int i = 0; i < 3; ++i) { |
| 335 | bits_total_[i] = 0; |
| 336 | } |
| 337 | duration_ = 0.0; |
| 338 | } |
| 339 | |
| 340 | virtual void SetUp() { |
| 341 | InitializeConfig(); |
| 342 | SetMode(encoding_mode_); |
| 343 | } |
| 344 | |
| 345 | // |
| 346 | // Frame flags and layer id for temporal layers. |
| 347 | // |
| 348 | |
| 349 | // For two layers, test pattern is: |
| 350 | // 1 3 |
| 351 | // 0 2 ..... |
| 352 | // For three layers, test pattern is: |
| 353 | // 1 3 5 7 |
| 354 | // 2 6 |
| 355 | // 0 4 .... |
| 356 | // LAST is always update on base/layer 0, GOLDEN is updated on layer 1, |
| 357 | // and ALTREF is updated on top layer for 3 layer pattern. |
| 358 | int SetFrameFlags(int frame_num, int num_temp_layers) { |
| 359 | int frame_flags = 0; |
| 360 | if (num_temp_layers == 2) { |
| 361 | if (frame_num % 2 == 0) { |
| 362 | // Layer 0: predict from L and ARF, update L. |
| 363 | frame_flags = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF | |
| 364 | VP8_EFLAG_NO_UPD_ARF; |
| 365 | } else { |
| 366 | // Layer 1: predict from L, G and ARF, and update G. |
| 367 | frame_flags = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST | |
| 368 | VP8_EFLAG_NO_UPD_ENTROPY; |
| 369 | } |
| 370 | } else if (num_temp_layers == 3) { |
| 371 | if (frame_num % 4 == 0) { |
| 372 | // Layer 0: predict from L, update L. |
| 373 | frame_flags = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF | |
| 374 | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF; |
| 375 | } else if ((frame_num - 2) % 4 == 0) { |
| 376 | // Layer 1: predict from L, G, update G. |
| 377 | frame_flags = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST | |
| 378 | VP8_EFLAG_NO_REF_ARF; |
| 379 | } else if ((frame_num - 1) % 2 == 0) { |
| 380 | // Layer 2: predict from L, G, ARF; update ARG. |
| 381 | frame_flags = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST; |
| 382 | } |
| 383 | } |
| 384 | return frame_flags; |
| 385 | } |
| 386 | |
| 387 | int SetLayerId(int frame_num, int num_temp_layers) { |
| 388 | int layer_id = 0; |
| 389 | if (num_temp_layers == 2) { |
| 390 | if (frame_num % 2 == 0) { |
| 391 | layer_id = 0; |
| 392 | } else { |
| 393 | layer_id = 1; |
| 394 | } |
| 395 | } else if (num_temp_layers == 3) { |
| 396 | if (frame_num % 4 == 0) { |
| 397 | layer_id = 0; |
| 398 | } else if ((frame_num - 2) % 4 == 0) { |
| 399 | layer_id = 1; |
| 400 | } else if ((frame_num - 1) % 2 == 0) { |
| 401 | layer_id = 2; |
| 402 | } |
| 403 | } |
| 404 | return layer_id; |
| 405 | } |
| 406 | |
| 407 | virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video, |
| 408 | libvpx_test::Encoder *encoder) { |
| 409 | if (cfg_.ts_number_layers > 1) { |
| 410 | int layer_id = SetLayerId(video->frame(), cfg_.ts_number_layers); |
| 411 | int frame_flags = SetFrameFlags(video->frame(), cfg_.ts_number_layers); |
| 412 | if (video->frame() > 0) { |
| 413 | encoder->Control(VP8E_SET_TEMPORAL_LAYER_ID, layer_id); |
| 414 | encoder->Control(VP8E_SET_FRAME_FLAGS, frame_flags); |
| 415 | } |
| 416 | const vpx_rational_t tb = video->timebase(); |
| 417 | timebase_ = static_cast<double>(tb.num) / tb.den; |
| 418 | duration_ = 0; |
| 419 | return; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) { |
| 424 | // Time since last timestamp = duration. |
| 425 | vpx_codec_pts_t duration = pkt->data.frame.pts - last_pts_; |
| 426 | if (duration > 1) { |
| 427 | // Update counter for total number of frames (#frames input to encoder). |
| 428 | // Needed for setting the proper layer_id below. |
| 429 | tot_frame_number_ += static_cast<int>(duration - 1); |
| 430 | } |
| 431 | int layer = SetLayerId(tot_frame_number_, cfg_.ts_number_layers); |
| 432 | const size_t frame_size_in_bits = pkt->data.frame.sz * 8; |
| 433 | // Update the total encoded bits. For temporal layers, update the cumulative |
| 434 | // encoded bits per layer. |
| 435 | for (int i = layer; i < static_cast<int>(cfg_.ts_number_layers); ++i) { |
| 436 | bits_total_[i] += frame_size_in_bits; |
| 437 | } |
| 438 | // Update the most recent pts. |
| 439 | last_pts_ = pkt->data.frame.pts; |
| 440 | ++tot_frame_number_; |
| 441 | } |
| 442 | |
| 443 | virtual void EndPassHook(void) { |
| 444 | duration_ = (last_pts_ + 1) * timebase_; |
| 445 | if (cfg_.ts_number_layers > 1) { |
| 446 | for (int layer = 0; layer < static_cast<int>(cfg_.ts_number_layers); |
| 447 | ++layer) { |
| 448 | if (bits_total_[layer]) { |
| 449 | // Effective file datarate: |
| 450 | effective_datarate_[layer] = (bits_total_[layer] / 1000.0) / duration_; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | double effective_datarate_[3]; |
| 457 | private: |
| 458 | libvpx_test::TestMode encoding_mode_; |
| 459 | vpx_codec_pts_t last_pts_; |
| 460 | double timebase_; |
| 461 | int64_t bits_total_[3]; |
| 462 | double duration_; |
| 463 | int tot_frame_number_; |
| 464 | }; |
| 465 | |
| 466 | // Check two codec controls used for: |
| 467 | // (1) for setting temporal layer id, and (2) for settings encoder flags. |
| 468 | // This test invokes those controls for each frame, and verifies encoder/decoder |
| 469 | // mismatch and basic rate control response. |
| 470 | // TODO(marpan): Maybe move this test to datarate_test.cc. |
| 471 | TEST_P(ErrorResilienceTestLargeCodecControls, CodecControl3TemporalLayers) { |
| 472 | cfg_.rc_buf_initial_sz = 500; |
| 473 | cfg_.rc_buf_optimal_sz = 500; |
| 474 | cfg_.rc_buf_sz = 1000; |
| 475 | cfg_.rc_dropframe_thresh = 1; |
| 476 | cfg_.rc_min_quantizer = 2; |
| 477 | cfg_.rc_max_quantizer = 56; |
| 478 | cfg_.rc_end_usage = VPX_CBR; |
| 479 | cfg_.rc_dropframe_thresh = 1; |
| 480 | cfg_.g_lag_in_frames = 0; |
| 481 | cfg_.kf_mode = VPX_KF_DISABLED; |
| 482 | cfg_.g_error_resilient = 1; |
| 483 | |
| 484 | // 3 Temporal layers. Framerate decimation (4, 2, 1). |
| 485 | cfg_.ts_number_layers = 3; |
| 486 | cfg_.ts_rate_decimator[0] = 4; |
| 487 | cfg_.ts_rate_decimator[1] = 2; |
| 488 | cfg_.ts_rate_decimator[2] = 1; |
| 489 | cfg_.ts_periodicity = 4; |
| 490 | cfg_.ts_layer_id[0] = 0; |
| 491 | cfg_.ts_layer_id[1] = 2; |
| 492 | cfg_.ts_layer_id[2] = 1; |
| 493 | cfg_.ts_layer_id[3] = 2; |
| 494 | |
| 495 | ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
| 496 | 30, 1, 0, 200); |
| 497 | for (int i = 200; i <= 800; i += 200) { |
| 498 | cfg_.rc_target_bitrate = i; |
| 499 | Reset(); |
| 500 | // 40-20-40 bitrate allocation for 3 temporal layers. |
| 501 | cfg_.ts_target_bitrate[0] = 40 * cfg_.rc_target_bitrate / 100; |
| 502 | cfg_.ts_target_bitrate[1] = 60 * cfg_.rc_target_bitrate / 100; |
| 503 | cfg_.ts_target_bitrate[2] = cfg_.rc_target_bitrate; |
| 504 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 505 | for (int j = 0; j < static_cast<int>(cfg_.ts_number_layers); ++j) { |
| 506 | ASSERT_GE(effective_datarate_[j], cfg_.ts_target_bitrate[j] * 0.75) |
| 507 | << " The datarate for the file is lower than target by too much, " |
| 508 | "for layer: " << j; |
| 509 | ASSERT_LE(effective_datarate_[j], cfg_.ts_target_bitrate[j] * 1.25) |
| 510 | << " The datarate for the file is greater than target by too much, " |
| 511 | "for layer: " << j; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | VP8_INSTANTIATE_TEST_CASE(ErrorResilienceTestLarge, ONE_PASS_TEST_MODES); |
| 517 | VP8_INSTANTIATE_TEST_CASE(ErrorResilienceTestLargeCodecControls, |
| 518 | ONE_PASS_TEST_MODES); |
| 519 | VP9_INSTANTIATE_TEST_CASE(ErrorResilienceTestLarge, ONE_PASS_TEST_MODES); |
Adrian Grange | cc017ca | 2012-10-02 12:16:27 -0700 | [diff] [blame] | 520 | } // namespace |