2012-05-17 02:27:00 +04:00
|
|
|
/*
|
2016-03-29 02:03:39 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2012-05-17 02:27:00 +04:00
|
|
|
*
|
2016-03-29 02:03:39 +03:00
|
|
|
* This source code is subject to the terms of the BSD 2 Clause License and
|
|
|
|
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
|
|
|
|
* was not distributed with this source code in the LICENSE file, you can
|
|
|
|
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
|
|
|
|
* Media Patent License 1.0 was not distributed with this source code in the
|
|
|
|
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
|
|
|
|
*/
|
|
|
|
|
2015-03-10 01:52:29 +03:00
|
|
|
#include <string>
|
|
|
|
|
2015-07-30 00:51:36 +03:00
|
|
|
#include "third_party/googletest/src/include/gtest/gtest.h"
|
|
|
|
|
2016-03-25 22:11:05 +03:00
|
|
|
#include "./aom_config.h"
|
2013-01-18 23:51:12 +04:00
|
|
|
#include "test/codec_factory.h"
|
2012-08-31 00:43:15 +04:00
|
|
|
#include "test/decode_test_driver.h"
|
2015-07-30 00:51:36 +03:00
|
|
|
#include "test/encode_test_driver.h"
|
2012-11-28 01:08:05 +04:00
|
|
|
#include "test/register_state_check.h"
|
2012-05-17 02:27:00 +04:00
|
|
|
#include "test/video_source.h"
|
|
|
|
|
2016-03-22 22:33:42 +03:00
|
|
|
namespace libaom_test {
|
2014-12-13 01:34:30 +03:00
|
|
|
void Encoder::InitEncoder(VideoSource *video) {
|
2016-03-25 22:11:05 +03:00
|
|
|
aom_codec_err_t res;
|
|
|
|
const aom_image_t *img = video->img();
|
2014-12-13 01:34:30 +03:00
|
|
|
|
|
|
|
if (video->img() && !encoder_.priv) {
|
|
|
|
cfg_.g_w = img->d_w;
|
|
|
|
cfg_.g_h = img->d_h;
|
|
|
|
cfg_.g_timebase = video->timebase();
|
|
|
|
cfg_.rc_twopass_stats_in = stats_->buf();
|
2015-03-10 01:52:29 +03:00
|
|
|
|
2016-03-25 22:11:05 +03:00
|
|
|
res = aom_codec_enc_init(&encoder_, CodecInterface(), &cfg_, init_flags_);
|
2016-03-25 22:57:08 +03:00
|
|
|
ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
|
2015-03-10 01:52:29 +03:00
|
|
|
|
2016-03-25 22:43:01 +03:00
|
|
|
#if CONFIG_AV1_ENCODER
|
|
|
|
if (CodecInterface() == &aom_codec_av1_cx_algo) {
|
|
|
|
// Default to 1 tile column for AV1.
|
2015-08-20 00:13:18 +03:00
|
|
|
const int log2_tile_columns = 0;
|
2016-03-28 20:04:30 +03:00
|
|
|
res = aom_codec_control_(&encoder_, AV1E_SET_TILE_COLUMNS,
|
2015-08-20 00:13:18 +03:00
|
|
|
log2_tile_columns);
|
2016-03-25 22:57:08 +03:00
|
|
|
ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
|
2015-03-10 01:52:29 +03:00
|
|
|
}
|
2016-01-22 03:46:09 +03:00
|
|
|
#endif
|
2014-12-13 01:34:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:03:09 +04:00
|
|
|
void Encoder::EncodeFrame(VideoSource *video, const unsigned long frame_flags) {
|
2012-05-17 02:27:00 +04:00
|
|
|
if (video->img())
|
2012-10-02 22:03:09 +04:00
|
|
|
EncodeFrameInternal(*video, frame_flags);
|
2012-05-17 02:27:00 +04:00
|
|
|
else
|
|
|
|
Flush();
|
|
|
|
|
|
|
|
// Handle twopass stats
|
|
|
|
CxDataIterator iter = GetCxData();
|
|
|
|
|
2016-03-25 22:11:05 +03:00
|
|
|
while (const aom_codec_cx_pkt_t *pkt = iter.Next()) {
|
2016-03-25 22:57:08 +03:00
|
|
|
if (pkt->kind != AOM_CODEC_STATS_PKT) continue;
|
2012-05-17 02:27:00 +04:00
|
|
|
|
|
|
|
stats_->Append(*pkt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Encoder::EncodeFrameInternal(const VideoSource &video,
|
2012-10-02 22:03:09 +04:00
|
|
|
const unsigned long frame_flags) {
|
2016-03-25 22:11:05 +03:00
|
|
|
aom_codec_err_t res;
|
|
|
|
const aom_image_t *img = video.img();
|
2012-05-17 02:27:00 +04:00
|
|
|
|
|
|
|
// Handle frame resizing
|
|
|
|
if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
|
|
|
|
cfg_.g_w = img->d_w;
|
|
|
|
cfg_.g_h = img->d_h;
|
2016-03-25 22:11:05 +03:00
|
|
|
res = aom_codec_enc_config_set(&encoder_, &cfg_);
|
2016-03-25 22:57:08 +03:00
|
|
|
ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
|
2012-05-17 02:27:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the frame
|
2016-03-25 22:11:05 +03:00
|
|
|
API_REGISTER_STATE_CHECK(res = aom_codec_encode(&encoder_, img, video.pts(),
|
2016-01-27 23:42:45 +03:00
|
|
|
video.duration(), frame_flags,
|
|
|
|
deadline_));
|
2016-03-25 22:57:08 +03:00
|
|
|
ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
|
2012-05-17 02:27:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Encoder::Flush() {
|
2016-03-25 22:11:05 +03:00
|
|
|
const aom_codec_err_t res =
|
|
|
|
aom_codec_encode(&encoder_, NULL, 0, 0, 0, deadline_);
|
2014-07-18 14:06:07 +04:00
|
|
|
if (!encoder_.priv)
|
2016-03-25 22:57:08 +03:00
|
|
|
ASSERT_EQ(AOM_CODEC_ERROR, res) << EncoderError();
|
2014-07-18 14:06:07 +04:00
|
|
|
else
|
2016-03-25 22:57:08 +03:00
|
|
|
ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
|
2012-05-17 02:27:00 +04:00
|
|
|
}
|
|
|
|
|
2013-01-18 23:51:12 +04:00
|
|
|
void EncoderTest::InitializeConfig() {
|
2016-03-25 22:11:05 +03:00
|
|
|
const aom_codec_err_t res = codec_->DefaultEncoderConfig(&cfg_, 0);
|
|
|
|
dec_cfg_ = aom_codec_dec_cfg_t();
|
2016-03-25 22:57:08 +03:00
|
|
|
ASSERT_EQ(AOM_CODEC_OK, res);
|
2013-01-18 23:51:12 +04:00
|
|
|
}
|
|
|
|
|
2012-05-17 02:27:00 +04:00
|
|
|
void EncoderTest::SetMode(TestMode mode) {
|
|
|
|
switch (mode) {
|
2016-03-25 22:57:08 +03:00
|
|
|
case kRealTime: deadline_ = AOM_DL_REALTIME; break;
|
2012-05-17 02:27:00 +04:00
|
|
|
|
|
|
|
case kOnePassGood:
|
2016-03-25 22:57:08 +03:00
|
|
|
case kTwoPassGood: deadline_ = AOM_DL_GOOD_QUALITY; break;
|
2012-05-17 02:27:00 +04:00
|
|
|
|
|
|
|
case kOnePassBest:
|
2016-03-25 22:57:08 +03:00
|
|
|
case kTwoPassBest: deadline_ = AOM_DL_BEST_QUALITY; break;
|
2012-05-17 02:27:00 +04:00
|
|
|
|
2016-01-27 23:42:45 +03:00
|
|
|
default: ASSERT_TRUE(false) << "Unexpected mode " << mode;
|
2012-05-17 02:27:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == kTwoPassGood || mode == kTwoPassBest)
|
|
|
|
passes_ = 2;
|
|
|
|
else
|
|
|
|
passes_ = 1;
|
|
|
|
}
|
2012-08-31 00:43:15 +04:00
|
|
|
// The function should return "true" most of the time, therefore no early
|
|
|
|
// break-out is implemented within the match checking process.
|
2016-03-25 22:11:05 +03:00
|
|
|
static bool compare_img(const aom_image_t *img1, const aom_image_t *img2) {
|
2016-01-27 23:42:45 +03:00
|
|
|
bool match = (img1->fmt == img2->fmt) && (img1->cs == img2->cs) &&
|
|
|
|
(img1->d_w == img2->d_w) && (img1->d_h == img2->d_h);
|
|
|
|
|
|
|
|
const unsigned int width_y = img1->d_w;
|
2012-08-31 00:43:15 +04:00
|
|
|
const unsigned int height_y = img1->d_h;
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < height_y; ++i)
|
2016-03-25 22:57:08 +03:00
|
|
|
match = (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
|
|
|
|
img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
|
2016-01-27 23:42:45 +03:00
|
|
|
width_y) == 0) &&
|
|
|
|
match;
|
|
|
|
const unsigned int width_uv = (img1->d_w + 1) >> 1;
|
2012-08-31 00:43:15 +04:00
|
|
|
const unsigned int height_uv = (img1->d_h + 1) >> 1;
|
2016-01-27 23:42:45 +03:00
|
|
|
for (i = 0; i < height_uv; ++i)
|
2016-03-25 22:57:08 +03:00
|
|
|
match = (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
|
|
|
|
img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
|
2016-01-27 23:42:45 +03:00
|
|
|
width_uv) == 0) &&
|
|
|
|
match;
|
2012-08-31 00:43:15 +04:00
|
|
|
for (i = 0; i < height_uv; ++i)
|
2016-03-25 22:57:08 +03:00
|
|
|
match = (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
|
|
|
|
img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
|
2016-01-27 23:42:45 +03:00
|
|
|
width_uv) == 0) &&
|
|
|
|
match;
|
2012-08-31 00:43:15 +04:00
|
|
|
return match;
|
|
|
|
}
|
2012-05-17 02:27:00 +04:00
|
|
|
|
2016-03-25 22:11:05 +03:00
|
|
|
void EncoderTest::MismatchHook(const aom_image_t * /*img1*/,
|
|
|
|
const aom_image_t * /*img2*/) {
|
2013-01-15 18:43:35 +04:00
|
|
|
ASSERT_TRUE(0) << "Encode/Decode mismatch found";
|
|
|
|
}
|
|
|
|
|
2012-05-17 02:27:00 +04:00
|
|
|
void EncoderTest::RunLoop(VideoSource *video) {
|
2016-03-25 22:11:05 +03:00
|
|
|
aom_codec_dec_cfg_t dec_cfg = aom_codec_dec_cfg_t();
|
2014-11-21 02:39:56 +03:00
|
|
|
|
2012-10-02 20:36:41 +04:00
|
|
|
stats_.Reset();
|
|
|
|
|
2013-03-02 00:43:10 +04:00
|
|
|
ASSERT_TRUE(passes_ == 1 || passes_ == 2);
|
2012-05-17 02:27:00 +04:00
|
|
|
for (unsigned int pass = 0; pass < passes_; pass++) {
|
2012-08-09 01:16:08 +04:00
|
|
|
last_pts_ = 0;
|
|
|
|
|
2012-05-17 02:27:00 +04:00
|
|
|
if (passes_ == 1)
|
2016-03-25 22:57:08 +03:00
|
|
|
cfg_.g_pass = AOM_RC_ONE_PASS;
|
2012-05-17 02:27:00 +04:00
|
|
|
else if (pass == 0)
|
2016-03-25 22:57:08 +03:00
|
|
|
cfg_.g_pass = AOM_RC_FIRST_PASS;
|
2012-05-17 02:27:00 +04:00
|
|
|
else
|
2016-03-25 22:57:08 +03:00
|
|
|
cfg_.g_pass = AOM_RC_LAST_PASS;
|
2012-05-17 02:27:00 +04:00
|
|
|
|
|
|
|
BeginPassHook(pass);
|
2016-01-27 23:42:45 +03:00
|
|
|
Encoder *const encoder =
|
|
|
|
codec_->CreateEncoder(cfg_, deadline_, init_flags_, &stats_);
|
2013-01-18 23:51:12 +04:00
|
|
|
ASSERT_TRUE(encoder != NULL);
|
2014-11-21 02:39:56 +03:00
|
|
|
|
2014-12-13 01:34:30 +03:00
|
|
|
video->Begin();
|
|
|
|
encoder->InitEncoder(video);
|
2015-08-28 02:05:52 +03:00
|
|
|
ASSERT_FALSE(::testing::Test::HasFatalFailure());
|
2014-12-13 01:34:30 +03:00
|
|
|
|
2014-11-21 02:39:56 +03:00
|
|
|
unsigned long dec_init_flags = 0; // NOLINT
|
|
|
|
// Use fragment decoder if encoder outputs partitions.
|
2016-03-26 00:24:04 +03:00
|
|
|
// NOTE: fragment decoder and partition encoder are only supported by AOM.
|
2016-03-25 22:57:08 +03:00
|
|
|
if (init_flags_ & AOM_CODEC_USE_OUTPUT_PARTITION)
|
|
|
|
dec_init_flags |= AOM_CODEC_USE_INPUT_FRAGMENTS;
|
2016-01-27 23:42:45 +03:00
|
|
|
Decoder *const decoder = codec_->CreateDecoder(dec_cfg, dec_init_flags, 0);
|
2012-05-17 02:27:00 +04:00
|
|
|
bool again;
|
2014-12-13 01:34:30 +03:00
|
|
|
for (again = true; again; video->Next()) {
|
2013-08-20 01:16:26 +04:00
|
|
|
again = (video->img() != NULL);
|
2012-05-17 02:27:00 +04:00
|
|
|
|
|
|
|
PreEncodeFrameHook(video);
|
2013-01-18 23:51:12 +04:00
|
|
|
PreEncodeFrameHook(video, encoder);
|
|
|
|
encoder->EncodeFrame(video, frame_flags_);
|
2012-05-17 02:27:00 +04:00
|
|
|
|
2013-01-18 23:51:12 +04:00
|
|
|
CxDataIterator iter = encoder->GetCxData();
|
2012-05-17 02:27:00 +04:00
|
|
|
|
2013-01-15 18:43:35 +04:00
|
|
|
bool has_cxdata = false;
|
|
|
|
bool has_dxdata = false;
|
2016-03-25 22:11:05 +03:00
|
|
|
while (const aom_codec_cx_pkt_t *pkt = iter.Next()) {
|
2013-03-06 00:23:34 +04:00
|
|
|
pkt = MutateEncoderOutputHook(pkt);
|
2012-05-17 02:27:00 +04:00
|
|
|
again = true;
|
2012-10-02 22:27:29 +04:00
|
|
|
switch (pkt->kind) {
|
2016-03-25 22:57:08 +03:00
|
|
|
case AOM_CODEC_CX_FRAME_PKT:
|
2012-10-02 22:27:29 +04:00
|
|
|
has_cxdata = true;
|
2013-01-15 18:43:35 +04:00
|
|
|
if (decoder && DoDecode()) {
|
2016-03-25 22:11:05 +03:00
|
|
|
aom_codec_err_t res_dec = decoder->DecodeFrame(
|
2016-01-27 23:42:45 +03:00
|
|
|
(const uint8_t *)pkt->data.frame.buf, pkt->data.frame.sz);
|
2014-07-17 17:31:50 +04:00
|
|
|
|
2016-01-27 23:42:45 +03:00
|
|
|
if (!HandleDecodeResult(res_dec, *video, decoder)) break;
|
2014-07-17 17:31:50 +04:00
|
|
|
|
2013-01-15 18:43:35 +04:00
|
|
|
has_dxdata = true;
|
|
|
|
}
|
2012-10-02 22:27:29 +04:00
|
|
|
ASSERT_GE(pkt->data.frame.pts, last_pts_);
|
|
|
|
last_pts_ = pkt->data.frame.pts;
|
|
|
|
FramePktHook(pkt);
|
|
|
|
break;
|
|
|
|
|
2016-03-25 22:57:08 +03:00
|
|
|
case AOM_CODEC_PSNR_PKT: PSNRPktHook(pkt); break;
|
2012-10-02 22:27:29 +04:00
|
|
|
|
2016-01-27 23:42:45 +03:00
|
|
|
default: break;
|
2012-10-02 22:27:29 +04:00
|
|
|
}
|
2012-05-17 02:27:00 +04:00
|
|
|
}
|
|
|
|
|
2014-11-21 02:39:56 +03:00
|
|
|
// Flush the decoder when there are no more fragments.
|
2016-03-25 22:57:08 +03:00
|
|
|
if ((init_flags_ & AOM_CODEC_USE_OUTPUT_PARTITION) && has_dxdata) {
|
2016-03-25 22:11:05 +03:00
|
|
|
const aom_codec_err_t res_dec = decoder->DecodeFrame(NULL, 0);
|
2016-01-27 23:42:45 +03:00
|
|
|
if (!HandleDecodeResult(res_dec, *video, decoder)) break;
|
2014-11-21 02:39:56 +03:00
|
|
|
}
|
|
|
|
|
2013-01-15 18:43:35 +04:00
|
|
|
if (has_dxdata && has_cxdata) {
|
2016-03-25 22:11:05 +03:00
|
|
|
const aom_image_t *img_enc = encoder->GetPreviewFrame();
|
2013-01-18 23:51:12 +04:00
|
|
|
DxDataIterator dec_iter = decoder->GetDxData();
|
2016-03-25 22:11:05 +03:00
|
|
|
const aom_image_t *img_dec = dec_iter.Next();
|
2013-01-15 18:43:35 +04:00
|
|
|
if (img_enc && img_dec) {
|
2012-08-31 00:43:15 +04:00
|
|
|
const bool res = compare_img(img_enc, img_dec);
|
2013-01-15 18:43:35 +04:00
|
|
|
if (!res) { // Mismatch
|
|
|
|
MismatchHook(img_enc, img_dec);
|
|
|
|
}
|
2012-08-31 00:43:15 +04:00
|
|
|
}
|
2016-01-27 23:42:45 +03:00
|
|
|
if (img_dec) DecompressedFrameHook(*img_dec, video->pts());
|
2012-08-31 00:43:15 +04:00
|
|
|
}
|
2016-01-27 23:42:45 +03:00
|
|
|
if (!Continue()) break;
|
2012-05-17 02:27:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
EndPassHook();
|
|
|
|
|
2016-01-27 23:42:45 +03:00
|
|
|
if (decoder) delete decoder;
|
2013-01-18 23:51:12 +04:00
|
|
|
delete encoder;
|
|
|
|
|
2016-01-27 23:42:45 +03:00
|
|
|
if (!Continue()) break;
|
2012-05-17 02:27:00 +04:00
|
|
|
}
|
|
|
|
}
|
2013-01-15 18:43:35 +04:00
|
|
|
|
2016-03-22 22:33:42 +03:00
|
|
|
} // namespace libaom_test
|