11b362b15af34006e6a11974088a46d42b903418eJohann/* 21b362b15af34006e6a11974088a46d42b903418eJohann * Copyright (c) 2012 The WebM project authors. All Rights Reserved. 31b362b15af34006e6a11974088a46d42b903418eJohann * 41b362b15af34006e6a11974088a46d42b903418eJohann * Use of this source code is governed by a BSD-style license 51b362b15af34006e6a11974088a46d42b903418eJohann * that can be found in the LICENSE file in the root of the source 61b362b15af34006e6a11974088a46d42b903418eJohann * tree. An additional intellectual property rights grant can be found 71b362b15af34006e6a11974088a46d42b903418eJohann * in the file PATENTS. All contributing project authors may 81b362b15af34006e6a11974088a46d42b903418eJohann * be found in the AUTHORS file in the root of the source tree. 91b362b15af34006e6a11974088a46d42b903418eJohann */ 10ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 113df0563f1b24dac6c0bd122fc922a48211269061hkuang#include "./vpx_config.h" 12ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "test/codec_factory.h" 131b362b15af34006e6a11974088a46d42b903418eJohann#include "test/encode_test_driver.h" 141b362b15af34006e6a11974088a46d42b903418eJohann#include "test/decode_test_driver.h" 15ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "test/register_state_check.h" 161b362b15af34006e6a11974088a46d42b903418eJohann#include "test/video_source.h" 171b362b15af34006e6a11974088a46d42b903418eJohann#include "third_party/googletest/src/include/gtest/gtest.h" 181b362b15af34006e6a11974088a46d42b903418eJohann 191b362b15af34006e6a11974088a46d42b903418eJohannnamespace libvpx_test { 201b362b15af34006e6a11974088a46d42b903418eJohannvoid Encoder::EncodeFrame(VideoSource *video, const unsigned long frame_flags) { 211b362b15af34006e6a11974088a46d42b903418eJohann if (video->img()) 221b362b15af34006e6a11974088a46d42b903418eJohann EncodeFrameInternal(*video, frame_flags); 231b362b15af34006e6a11974088a46d42b903418eJohann else 241b362b15af34006e6a11974088a46d42b903418eJohann Flush(); 251b362b15af34006e6a11974088a46d42b903418eJohann 261b362b15af34006e6a11974088a46d42b903418eJohann // Handle twopass stats 271b362b15af34006e6a11974088a46d42b903418eJohann CxDataIterator iter = GetCxData(); 281b362b15af34006e6a11974088a46d42b903418eJohann 291b362b15af34006e6a11974088a46d42b903418eJohann while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) { 301b362b15af34006e6a11974088a46d42b903418eJohann if (pkt->kind != VPX_CODEC_STATS_PKT) 311b362b15af34006e6a11974088a46d42b903418eJohann continue; 321b362b15af34006e6a11974088a46d42b903418eJohann 331b362b15af34006e6a11974088a46d42b903418eJohann stats_->Append(*pkt); 341b362b15af34006e6a11974088a46d42b903418eJohann } 351b362b15af34006e6a11974088a46d42b903418eJohann} 361b362b15af34006e6a11974088a46d42b903418eJohann 371b362b15af34006e6a11974088a46d42b903418eJohannvoid Encoder::EncodeFrameInternal(const VideoSource &video, 381b362b15af34006e6a11974088a46d42b903418eJohann const unsigned long frame_flags) { 391b362b15af34006e6a11974088a46d42b903418eJohann vpx_codec_err_t res; 401b362b15af34006e6a11974088a46d42b903418eJohann const vpx_image_t *img = video.img(); 411b362b15af34006e6a11974088a46d42b903418eJohann 421b362b15af34006e6a11974088a46d42b903418eJohann // Handle first frame initialization 431b362b15af34006e6a11974088a46d42b903418eJohann if (!encoder_.priv) { 441b362b15af34006e6a11974088a46d42b903418eJohann cfg_.g_w = img->d_w; 451b362b15af34006e6a11974088a46d42b903418eJohann cfg_.g_h = img->d_h; 461b362b15af34006e6a11974088a46d42b903418eJohann cfg_.g_timebase = video.timebase(); 471b362b15af34006e6a11974088a46d42b903418eJohann cfg_.rc_twopass_stats_in = stats_->buf(); 48ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_, 491b362b15af34006e6a11974088a46d42b903418eJohann init_flags_); 501b362b15af34006e6a11974088a46d42b903418eJohann ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); 511b362b15af34006e6a11974088a46d42b903418eJohann } 521b362b15af34006e6a11974088a46d42b903418eJohann 531b362b15af34006e6a11974088a46d42b903418eJohann // Handle frame resizing 541b362b15af34006e6a11974088a46d42b903418eJohann if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) { 551b362b15af34006e6a11974088a46d42b903418eJohann cfg_.g_w = img->d_w; 561b362b15af34006e6a11974088a46d42b903418eJohann cfg_.g_h = img->d_h; 571b362b15af34006e6a11974088a46d42b903418eJohann res = vpx_codec_enc_config_set(&encoder_, &cfg_); 581b362b15af34006e6a11974088a46d42b903418eJohann ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); 591b362b15af34006e6a11974088a46d42b903418eJohann } 601b362b15af34006e6a11974088a46d42b903418eJohann 611b362b15af34006e6a11974088a46d42b903418eJohann // Encode the frame 62ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang REGISTER_STATE_CHECK( 63ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang res = vpx_codec_encode(&encoder_, 64ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang video.img(), video.pts(), video.duration(), 65ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang frame_flags, deadline_)); 661b362b15af34006e6a11974088a46d42b903418eJohann ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); 671b362b15af34006e6a11974088a46d42b903418eJohann} 681b362b15af34006e6a11974088a46d42b903418eJohann 691b362b15af34006e6a11974088a46d42b903418eJohannvoid Encoder::Flush() { 701b362b15af34006e6a11974088a46d42b903418eJohann const vpx_codec_err_t res = vpx_codec_encode(&encoder_, NULL, 0, 0, 0, 711b362b15af34006e6a11974088a46d42b903418eJohann deadline_); 721b362b15af34006e6a11974088a46d42b903418eJohann ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); 731b362b15af34006e6a11974088a46d42b903418eJohann} 741b362b15af34006e6a11974088a46d42b903418eJohann 75ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangvoid EncoderTest::InitializeConfig() { 76ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const vpx_codec_err_t res = codec_->DefaultEncoderConfig(&cfg_, 0); 77ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ASSERT_EQ(VPX_CODEC_OK, res); 78ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 79ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 801b362b15af34006e6a11974088a46d42b903418eJohannvoid EncoderTest::SetMode(TestMode mode) { 811b362b15af34006e6a11974088a46d42b903418eJohann switch (mode) { 821b362b15af34006e6a11974088a46d42b903418eJohann case kRealTime: 831b362b15af34006e6a11974088a46d42b903418eJohann deadline_ = VPX_DL_REALTIME; 841b362b15af34006e6a11974088a46d42b903418eJohann break; 851b362b15af34006e6a11974088a46d42b903418eJohann 861b362b15af34006e6a11974088a46d42b903418eJohann case kOnePassGood: 871b362b15af34006e6a11974088a46d42b903418eJohann case kTwoPassGood: 881b362b15af34006e6a11974088a46d42b903418eJohann deadline_ = VPX_DL_GOOD_QUALITY; 891b362b15af34006e6a11974088a46d42b903418eJohann break; 901b362b15af34006e6a11974088a46d42b903418eJohann 911b362b15af34006e6a11974088a46d42b903418eJohann case kOnePassBest: 921b362b15af34006e6a11974088a46d42b903418eJohann case kTwoPassBest: 931b362b15af34006e6a11974088a46d42b903418eJohann deadline_ = VPX_DL_BEST_QUALITY; 941b362b15af34006e6a11974088a46d42b903418eJohann break; 951b362b15af34006e6a11974088a46d42b903418eJohann 961b362b15af34006e6a11974088a46d42b903418eJohann default: 971b362b15af34006e6a11974088a46d42b903418eJohann ASSERT_TRUE(false) << "Unexpected mode " << mode; 981b362b15af34006e6a11974088a46d42b903418eJohann } 991b362b15af34006e6a11974088a46d42b903418eJohann 1001b362b15af34006e6a11974088a46d42b903418eJohann if (mode == kTwoPassGood || mode == kTwoPassBest) 1011b362b15af34006e6a11974088a46d42b903418eJohann passes_ = 2; 1021b362b15af34006e6a11974088a46d42b903418eJohann else 1031b362b15af34006e6a11974088a46d42b903418eJohann passes_ = 1; 1041b362b15af34006e6a11974088a46d42b903418eJohann} 1051b362b15af34006e6a11974088a46d42b903418eJohann// The function should return "true" most of the time, therefore no early 1061b362b15af34006e6a11974088a46d42b903418eJohann// break-out is implemented within the match checking process. 1071b362b15af34006e6a11974088a46d42b903418eJohannstatic bool compare_img(const vpx_image_t *img1, 1081b362b15af34006e6a11974088a46d42b903418eJohann const vpx_image_t *img2) { 1091b362b15af34006e6a11974088a46d42b903418eJohann bool match = (img1->fmt == img2->fmt) && 1101b362b15af34006e6a11974088a46d42b903418eJohann (img1->d_w == img2->d_w) && 1111b362b15af34006e6a11974088a46d42b903418eJohann (img1->d_h == img2->d_h); 1121b362b15af34006e6a11974088a46d42b903418eJohann 1131b362b15af34006e6a11974088a46d42b903418eJohann const unsigned int width_y = img1->d_w; 1141b362b15af34006e6a11974088a46d42b903418eJohann const unsigned int height_y = img1->d_h; 1151b362b15af34006e6a11974088a46d42b903418eJohann unsigned int i; 1161b362b15af34006e6a11974088a46d42b903418eJohann for (i = 0; i < height_y; ++i) 1173df0563f1b24dac6c0bd122fc922a48211269061hkuang match = (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y], 1183df0563f1b24dac6c0bd122fc922a48211269061hkuang img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y], 1193df0563f1b24dac6c0bd122fc922a48211269061hkuang width_y) == 0) && match; 1201b362b15af34006e6a11974088a46d42b903418eJohann const unsigned int width_uv = (img1->d_w + 1) >> 1; 1211b362b15af34006e6a11974088a46d42b903418eJohann const unsigned int height_uv = (img1->d_h + 1) >> 1; 1221b362b15af34006e6a11974088a46d42b903418eJohann for (i = 0; i < height_uv; ++i) 1233df0563f1b24dac6c0bd122fc922a48211269061hkuang match = (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U], 1243df0563f1b24dac6c0bd122fc922a48211269061hkuang img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U], 1253df0563f1b24dac6c0bd122fc922a48211269061hkuang width_uv) == 0) && match; 1261b362b15af34006e6a11974088a46d42b903418eJohann for (i = 0; i < height_uv; ++i) 1273df0563f1b24dac6c0bd122fc922a48211269061hkuang match = (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V], 1283df0563f1b24dac6c0bd122fc922a48211269061hkuang img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V], 1293df0563f1b24dac6c0bd122fc922a48211269061hkuang width_uv) == 0) && match; 1301b362b15af34006e6a11974088a46d42b903418eJohann return match; 1311b362b15af34006e6a11974088a46d42b903418eJohann} 1321b362b15af34006e6a11974088a46d42b903418eJohann 133ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangvoid EncoderTest::MismatchHook(const vpx_image_t *img1, 134ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const vpx_image_t *img2) { 135ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ASSERT_TRUE(0) << "Encode/Decode mismatch found"; 136ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 137ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 1381b362b15af34006e6a11974088a46d42b903418eJohannvoid EncoderTest::RunLoop(VideoSource *video) { 1391b362b15af34006e6a11974088a46d42b903418eJohann vpx_codec_dec_cfg_t dec_cfg = {0}; 1401b362b15af34006e6a11974088a46d42b903418eJohann 1411b362b15af34006e6a11974088a46d42b903418eJohann stats_.Reset(); 1421b362b15af34006e6a11974088a46d42b903418eJohann 143ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ASSERT_TRUE(passes_ == 1 || passes_ == 2); 1441b362b15af34006e6a11974088a46d42b903418eJohann for (unsigned int pass = 0; pass < passes_; pass++) { 1451b362b15af34006e6a11974088a46d42b903418eJohann last_pts_ = 0; 1461b362b15af34006e6a11974088a46d42b903418eJohann 1471b362b15af34006e6a11974088a46d42b903418eJohann if (passes_ == 1) 1481b362b15af34006e6a11974088a46d42b903418eJohann cfg_.g_pass = VPX_RC_ONE_PASS; 1491b362b15af34006e6a11974088a46d42b903418eJohann else if (pass == 0) 1501b362b15af34006e6a11974088a46d42b903418eJohann cfg_.g_pass = VPX_RC_FIRST_PASS; 1511b362b15af34006e6a11974088a46d42b903418eJohann else 1521b362b15af34006e6a11974088a46d42b903418eJohann cfg_.g_pass = VPX_RC_LAST_PASS; 1531b362b15af34006e6a11974088a46d42b903418eJohann 1541b362b15af34006e6a11974088a46d42b903418eJohann BeginPassHook(pass); 155ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang Encoder* const encoder = codec_->CreateEncoder(cfg_, deadline_, init_flags_, 156ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang &stats_); 157ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ASSERT_TRUE(encoder != NULL); 158ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang Decoder* const decoder = codec_->CreateDecoder(dec_cfg, 0); 1591b362b15af34006e6a11974088a46d42b903418eJohann bool again; 1601b362b15af34006e6a11974088a46d42b903418eJohann for (again = true, video->Begin(); again; video->Next()) { 1613df0563f1b24dac6c0bd122fc922a48211269061hkuang again = (video->img() != NULL); 1621b362b15af34006e6a11974088a46d42b903418eJohann 1631b362b15af34006e6a11974088a46d42b903418eJohann PreEncodeFrameHook(video); 164ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang PreEncodeFrameHook(video, encoder); 165ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang encoder->EncodeFrame(video, frame_flags_); 1661b362b15af34006e6a11974088a46d42b903418eJohann 167ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang CxDataIterator iter = encoder->GetCxData(); 1681b362b15af34006e6a11974088a46d42b903418eJohann 169ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang bool has_cxdata = false; 170ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang bool has_dxdata = false; 1711b362b15af34006e6a11974088a46d42b903418eJohann while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) { 172ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang pkt = MutateEncoderOutputHook(pkt); 1731b362b15af34006e6a11974088a46d42b903418eJohann again = true; 1741b362b15af34006e6a11974088a46d42b903418eJohann switch (pkt->kind) { 1751b362b15af34006e6a11974088a46d42b903418eJohann case VPX_CODEC_CX_FRAME_PKT: 1761b362b15af34006e6a11974088a46d42b903418eJohann has_cxdata = true; 177ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (decoder && DoDecode()) { 178ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang vpx_codec_err_t res_dec = decoder->DecodeFrame( 179ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang (const uint8_t*)pkt->data.frame.buf, pkt->data.frame.sz); 180ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError(); 181ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang has_dxdata = true; 182ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } 1831b362b15af34006e6a11974088a46d42b903418eJohann ASSERT_GE(pkt->data.frame.pts, last_pts_); 1841b362b15af34006e6a11974088a46d42b903418eJohann last_pts_ = pkt->data.frame.pts; 1851b362b15af34006e6a11974088a46d42b903418eJohann FramePktHook(pkt); 1861b362b15af34006e6a11974088a46d42b903418eJohann break; 1871b362b15af34006e6a11974088a46d42b903418eJohann 1881b362b15af34006e6a11974088a46d42b903418eJohann case VPX_CODEC_PSNR_PKT: 1891b362b15af34006e6a11974088a46d42b903418eJohann PSNRPktHook(pkt); 1901b362b15af34006e6a11974088a46d42b903418eJohann break; 1911b362b15af34006e6a11974088a46d42b903418eJohann 1921b362b15af34006e6a11974088a46d42b903418eJohann default: 1931b362b15af34006e6a11974088a46d42b903418eJohann break; 1941b362b15af34006e6a11974088a46d42b903418eJohann } 1951b362b15af34006e6a11974088a46d42b903418eJohann } 1961b362b15af34006e6a11974088a46d42b903418eJohann 197ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (has_dxdata && has_cxdata) { 198ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const vpx_image_t *img_enc = encoder->GetPreviewFrame(); 199ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang DxDataIterator dec_iter = decoder->GetDxData(); 2001b362b15af34006e6a11974088a46d42b903418eJohann const vpx_image_t *img_dec = dec_iter.Next(); 201ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (img_enc && img_dec) { 2021b362b15af34006e6a11974088a46d42b903418eJohann const bool res = compare_img(img_enc, img_dec); 203ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (!res) { // Mismatch 204ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang MismatchHook(img_enc, img_dec); 205ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } 2061b362b15af34006e6a11974088a46d42b903418eJohann } 207ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (img_dec) 208ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang DecompressedFrameHook(*img_dec, video->pts()); 2091b362b15af34006e6a11974088a46d42b903418eJohann } 2101b362b15af34006e6a11974088a46d42b903418eJohann if (!Continue()) 2111b362b15af34006e6a11974088a46d42b903418eJohann break; 2121b362b15af34006e6a11974088a46d42b903418eJohann } 2131b362b15af34006e6a11974088a46d42b903418eJohann 2141b362b15af34006e6a11974088a46d42b903418eJohann EndPassHook(); 2151b362b15af34006e6a11974088a46d42b903418eJohann 216ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (decoder) 217ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang delete decoder; 218ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang delete encoder; 219ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 2201b362b15af34006e6a11974088a46d42b903418eJohann if (!Continue()) 2211b362b15af34006e6a11974088a46d42b903418eJohann break; 2221b362b15af34006e6a11974088a46d42b903418eJohann } 2231b362b15af34006e6a11974088a46d42b903418eJohann} 224ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 2251b362b15af34006e6a11974088a46d42b903418eJohann} // namespace libvpx_test 226