[spatial svc]Add a few different encode frame tests.

1. Clean the code for encode frame tests
2. Add encode w/ and w/o alt reference frame test
3. Add encode SNR layers test
4. Add encode multiple layers but decode partial layers test

Change-Id: Ibd2c9bc02525db584a6f931a98405f2d851b3cd6
This commit is contained in:
Minghai Shang 2014-08-13 13:12:00 -07:00
parent 1d9e2b5003
commit e1b5d24837
4 changed files with 409 additions and 325 deletions

View File

@ -13,6 +13,9 @@
#include "test/codec_factory.h" #include "test/codec_factory.h"
#include "test/decode_test_driver.h" #include "test/decode_test_driver.h"
#include "test/i420_video_source.h" #include "test/i420_video_source.h"
#include "vp9/decoder/vp9_decoder.h"
#include "vpx/svc_context.h" #include "vpx/svc_context.h"
#include "vpx/vp8cx.h" #include "vpx/vp8cx.h"
#include "vpx/vpx_encoder.h" #include "vpx/vpx_encoder.h"
@ -21,6 +24,7 @@ namespace {
using libvpx_test::CodecFactory; using libvpx_test::CodecFactory;
using libvpx_test::Decoder; using libvpx_test::Decoder;
using libvpx_test::DxDataIterator;
using libvpx_test::VP9CodecFactory; using libvpx_test::VP9CodecFactory;
class SvcTest : public ::testing::Test { class SvcTest : public ::testing::Test {
@ -62,9 +66,213 @@ class SvcTest : public ::testing::Test {
} }
virtual void TearDown() { virtual void TearDown() {
vpx_svc_release(&svc_); ReleaseEncoder();
delete(decoder_); delete(decoder_);
}
void InitializeEncoder() {
const vpx_codec_err_t res =
vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
}
void ReleaseEncoder() {
vpx_svc_release(&svc_);
if (codec_initialized_) vpx_codec_destroy(&codec_); if (codec_initialized_) vpx_codec_destroy(&codec_);
codec_initialized_ = false;
}
void Pass1EncodeNFrames(const int n, const int layers,
std::string *const stats_buf) {
vpx_codec_err_t res;
size_t stats_size = 0;
const char *stats_data = NULL;
ASSERT_GT(n, 0);
ASSERT_GT(layers, 0);
svc_.spatial_layers = layers;
codec_enc_.g_pass = VPX_RC_FIRST_PASS;
InitializeEncoder();
libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight,
codec_enc_.g_timebase.den,
codec_enc_.g_timebase.num, 0, 30);
video.Begin();
for (int i = 0; i < n; ++i) {
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
ASSERT_EQ(VPX_CODEC_OK, res);
stats_size = vpx_svc_get_rc_stats_buffer_size(&svc_);
EXPECT_GT(stats_size, 0U);
stats_data = vpx_svc_get_rc_stats_buffer(&svc_);
ASSERT_TRUE(stats_data != NULL);
stats_buf->append(stats_data, stats_size);
video.Next();
}
// Flush encoder and test EOS packet
res = vpx_svc_encode(&svc_, &codec_, NULL, video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
stats_size = vpx_svc_get_rc_stats_buffer_size(&svc_);
EXPECT_GT(stats_size, 0U);
stats_data = vpx_svc_get_rc_stats_buffer(&svc_);
ASSERT_TRUE(stats_data != NULL);
stats_buf->append(stats_data, stats_size);
ReleaseEncoder();
}
void StoreFrames(const size_t max_frame_received,
struct vpx_fixed_buf *const outputs,
size_t *const frame_received) {
size_t frame_size;
while ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
ASSERT_LT(*frame_received, max_frame_received);
if (*frame_received == 0) {
EXPECT_EQ(1, vpx_svc_is_keyframe(&svc_));
}
outputs[*frame_received].buf = malloc(frame_size);
ASSERT_TRUE(outputs[*frame_received].buf != NULL);
memcpy(outputs[*frame_received].buf, vpx_svc_get_buffer(&svc_),
frame_size);
outputs[*frame_received].sz = frame_size;
++(*frame_received);
}
}
void Pass2EncodeNFrames(std::string *const stats_buf,
const int n, const int layers,
struct vpx_fixed_buf *const outputs) {
vpx_codec_err_t res;
size_t frame_received = 0;
ASSERT_TRUE(outputs != NULL);
ASSERT_GT(n, 0);
ASSERT_GT(layers, 0);
svc_.spatial_layers = layers;
codec_enc_.rc_target_bitrate = 500;
if (codec_enc_.g_pass == VPX_RC_LAST_PASS) {
ASSERT_TRUE(stats_buf != NULL);
ASSERT_GT(stats_buf->size(), 0U);
codec_enc_.rc_twopass_stats_in.buf = &(*stats_buf)[0];
codec_enc_.rc_twopass_stats_in.sz = stats_buf->size();
}
InitializeEncoder();
libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight,
codec_enc_.g_timebase.den,
codec_enc_.g_timebase.num, 0, 30);
video.Begin();
for (int i = 0; i < n; ++i) {
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
ASSERT_EQ(VPX_CODEC_OK, res);
StoreFrames(n, outputs, &frame_received);
video.Next();
}
// Flush Encoder
res = vpx_svc_encode(&svc_, &codec_, NULL, 0,
video.duration(), VPX_DL_GOOD_QUALITY);
EXPECT_EQ(VPX_CODEC_OK, res);
StoreFrames(n, outputs, &frame_received);
EXPECT_EQ(frame_received, (size_t)n);
ReleaseEncoder();
}
void DecodeNFrames(const struct vpx_fixed_buf *const inputs, const int n) {
int decoded_frames = 0;
int received_frames = 0;
ASSERT_TRUE(inputs != NULL);
ASSERT_GT(n, 0);
for (int i = 0; i < n; ++i) {
ASSERT_TRUE(inputs[i].buf != NULL);
ASSERT_GT(inputs[i].sz, 0U);
const vpx_codec_err_t res_dec =
decoder_->DecodeFrame(static_cast<const uint8_t *>(inputs[i].buf),
inputs[i].sz);
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
++decoded_frames;
DxDataIterator dec_iter = decoder_->GetDxData();
while (dec_iter.Next()) {
++received_frames;
}
}
EXPECT_EQ(decoded_frames, n);
EXPECT_EQ(received_frames, n);
}
void DropEnhancementLayers(struct vpx_fixed_buf *const inputs,
const int num_super_frames,
const int remained_layers) {
ASSERT_TRUE(inputs != NULL);
ASSERT_GT(num_super_frames, 0);
ASSERT_GT(remained_layers, 0);
for (int i = 0; i < num_super_frames; ++i) {
uint32_t frame_sizes[8] = {0};
int frame_count = 0;
int frames_found = 0;
int frame;
ASSERT_TRUE(inputs[i].buf != NULL);
ASSERT_GT(inputs[i].sz, 0U);
vpx_codec_err_t res =
vp9_parse_superframe_index(static_cast<const uint8_t*>(inputs[i].buf),
inputs[i].sz, frame_sizes, &frame_count,
NULL, NULL);
ASSERT_EQ(VPX_CODEC_OK, res);
uint8_t *frame_data = static_cast<uint8_t *>(inputs[i].buf);
uint8_t *frame_start = frame_data;
for (frame = 0; frame < frame_count; ++frame) {
// Looking for a visible frame
if (frame_data[0] & 0x02) {
++frames_found;
if (frames_found == remained_layers)
break;
}
frame_data += frame_sizes[frame];
}
ASSERT_LT(frame, frame_count);
if (frame == frame_count - 1)
continue;
frame_data += frame_sizes[frame];
uint8_t marker =
static_cast<const uint8_t *>(inputs[i].buf)[inputs[i].sz - 1];
const uint32_t mag = ((marker >> 3) & 0x3) + 1;
const size_t index_sz = 2 + mag * frame_count;
const size_t new_index_sz = 2 + mag * (frame + 1);
marker &= 0x0f8;
marker |= frame;
frame_data[0] = marker;
memcpy(frame_data + 1, frame_start + inputs[i].sz - index_sz + 1,
new_index_sz - 2);
frame_data[new_index_sz - 1] = marker;
inputs[i].sz = frame_data - frame_start + new_index_sz;
}
}
void FreeBitstreamBuffers(struct vpx_fixed_buf *const inputs, const int n) {
ASSERT_TRUE(inputs != NULL);
ASSERT_GT(n, 0);
for (int i = 0; i < n; ++i) {
free(inputs[i].buf);
inputs[i].buf = NULL;
inputs[i].sz = 0;
}
} }
SvcContext svc_; SvcContext svc_;
@ -93,9 +301,7 @@ TEST_F(SvcTest, SvcInit) {
EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res); EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
svc_.spatial_layers = 0; // use default layers svc_.spatial_layers = 0; // use default layers
res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_); InitializeEncoder();
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
EXPECT_EQ(VPX_SS_DEFAULT_LAYERS, svc_.spatial_layers); EXPECT_EQ(VPX_SS_DEFAULT_LAYERS, svc_.spatial_layers);
} }
@ -106,9 +312,7 @@ TEST_F(SvcTest, InitTwoLayers) {
EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res); EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
vpx_svc_set_scale_factors(&svc_, "4/16,16/16"); // valid scale values vpx_svc_set_scale_factors(&svc_, "4/16,16/16"); // valid scale values
res = vpx_svc_init(&svc_, &codec_, codec_iface_, &codec_enc_); InitializeEncoder();
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
} }
TEST_F(SvcTest, InvalidOptions) { TEST_F(SvcTest, InvalidOptions) {
@ -124,18 +328,15 @@ TEST_F(SvcTest, InvalidOptions) {
TEST_F(SvcTest, SetLayersOption) { TEST_F(SvcTest, SetLayersOption) {
vpx_codec_err_t res = vpx_svc_set_options(&svc_, "layers=3"); vpx_codec_err_t res = vpx_svc_set_options(&svc_, "layers=3");
EXPECT_EQ(VPX_CODEC_OK, res); EXPECT_EQ(VPX_CODEC_OK, res);
res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_); InitializeEncoder();
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
EXPECT_EQ(3, svc_.spatial_layers); EXPECT_EQ(3, svc_.spatial_layers);
} }
TEST_F(SvcTest, SetMultipleOptions) { TEST_F(SvcTest, SetMultipleOptions) {
vpx_codec_err_t res = vpx_codec_err_t res =
vpx_svc_set_options(&svc_, "layers=2 scale-factors=1/3,2/3"); vpx_svc_set_options(&svc_, "layers=2 scale-factors=1/3,2/3");
res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
EXPECT_EQ(VPX_CODEC_OK, res); EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true; InitializeEncoder();
EXPECT_EQ(2, svc_.spatial_layers); EXPECT_EQ(2, svc_.spatial_layers);
} }
@ -149,9 +350,7 @@ TEST_F(SvcTest, SetScaleFactorsOption) {
res = vpx_svc_set_options(&svc_, "scale-factors=1/3,2/3"); res = vpx_svc_set_options(&svc_, "scale-factors=1/3,2/3");
EXPECT_EQ(VPX_CODEC_OK, res); EXPECT_EQ(VPX_CODEC_OK, res);
res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_); InitializeEncoder();
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
} }
TEST_F(SvcTest, SetQuantizersOption) { TEST_F(SvcTest, SetQuantizersOption) {
@ -162,9 +361,7 @@ TEST_F(SvcTest, SetQuantizersOption) {
EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res); EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
vpx_svc_set_options(&svc_, "quantizers=40,45"); vpx_svc_set_options(&svc_, "quantizers=40,45");
res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_); InitializeEncoder();
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
} }
TEST_F(SvcTest, SetAutoAltRefOption) { TEST_F(SvcTest, SetAutoAltRefOption) {
@ -180,9 +377,7 @@ TEST_F(SvcTest, SetAutoAltRefOption) {
EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res); EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
vpx_svc_set_options(&svc_, "auto-alt-refs=0,1,1,1,0"); vpx_svc_set_options(&svc_, "auto-alt-refs=0,1,1,1,0");
res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_); InitializeEncoder();
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
} }
TEST_F(SvcTest, SetQuantizers) { TEST_F(SvcTest, SetQuantizers) {
@ -200,9 +395,7 @@ TEST_F(SvcTest, SetQuantizers) {
res = vpx_svc_set_quantizers(&svc_, "40,30"); res = vpx_svc_set_quantizers(&svc_, "40,30");
EXPECT_EQ(VPX_CODEC_OK, res); EXPECT_EQ(VPX_CODEC_OK, res);
res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_); InitializeEncoder();
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
} }
TEST_F(SvcTest, SetScaleFactors) { TEST_F(SvcTest, SetScaleFactors) {
@ -220,121 +413,25 @@ TEST_F(SvcTest, SetScaleFactors) {
res = vpx_svc_set_scale_factors(&svc_, "4/16,16/16"); res = vpx_svc_set_scale_factors(&svc_, "4/16,16/16");
EXPECT_EQ(VPX_CODEC_OK, res); EXPECT_EQ(VPX_CODEC_OK, res);
res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_); InitializeEncoder();
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
} }
// Test that decoder can handle an SVC frame as the first frame in a sequence. // Test that decoder can handle an SVC frame as the first frame in a sequence.
TEST_F(SvcTest, FirstFrameHasLayers) { TEST_F(SvcTest, OnePassEncodeOneFrame) {
svc_.spatial_layers = 2; codec_enc_.g_pass = VPX_RC_ONE_PASS;
vpx_svc_set_scale_factors(&svc_, "4/16,16/16"); vpx_fixed_buf output = {0};
vpx_svc_set_quantizers(&svc_, "40,30"); Pass2EncodeNFrames(NULL, 1, 2, &output);
DecodeNFrames(&output, 1);
vpx_codec_err_t res = FreeBitstreamBuffers(&output, 1);
vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight,
codec_enc_.g_timebase.den,
codec_enc_.g_timebase.num, 0, 30);
video.Begin();
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
EXPECT_EQ(VPX_CODEC_OK, res);
if (vpx_svc_get_frame_size(&svc_) == 0) {
// Flush encoder
res = vpx_svc_encode(&svc_, &codec_, NULL, 0,
video.duration(), VPX_DL_GOOD_QUALITY);
EXPECT_EQ(VPX_CODEC_OK, res);
}
int frame_size = vpx_svc_get_frame_size(&svc_);
EXPECT_GT(frame_size, 0);
const vpx_codec_err_t res_dec = decoder_->DecodeFrame(
static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
// this test fails with a decoder error
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
} }
TEST_F(SvcTest, EncodeThreeFrames) { TEST_F(SvcTest, OnePassEncodeThreeFrames) {
svc_.spatial_layers = 2; codec_enc_.g_pass = VPX_RC_ONE_PASS;
vpx_svc_set_scale_factors(&svc_, "4/16,16/16"); vpx_fixed_buf outputs[3];
vpx_svc_set_quantizers(&svc_, "40,30"); memset(&outputs[0], 0, sizeof(outputs));
int decoded_frames = 0; Pass2EncodeNFrames(NULL, 3, 2, &outputs[0]);
vpx_codec_err_t res_dec; DecodeNFrames(&outputs[0], 3);
int frame_size; FreeBitstreamBuffers(&outputs[0], 3);
vpx_codec_err_t res =
vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
ASSERT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight,
codec_enc_.g_timebase.den,
codec_enc_.g_timebase.num, 0, 30);
// FRAME 0
video.Begin();
// This frame is a keyframe.
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
res_dec = decoder_->DecodeFrame(
static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
++decoded_frames;
}
// FRAME 1
video.Next();
// This is a P-frame.
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
ASSERT_EQ(VPX_CODEC_OK, res);
if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
res_dec = decoder_->DecodeFrame(
static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
++decoded_frames;
}
// FRAME 2
video.Next();
// This is a P-frame.
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
ASSERT_EQ(VPX_CODEC_OK, res);
if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
res_dec = decoder_->DecodeFrame(
static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
++decoded_frames;
}
// Flush encoder
res = vpx_svc_encode(&svc_, &codec_, NULL, 0,
video.duration(), VPX_DL_GOOD_QUALITY);
EXPECT_EQ(VPX_CODEC_OK, res);
while ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) {
EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_));
res_dec = decoder_->DecodeFrame(
static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size);
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
++decoded_frames;
}
EXPECT_EQ(decoded_frames, 3);
} }
TEST_F(SvcTest, GetLayerResolution) { TEST_F(SvcTest, GetLayerResolution) {
@ -342,14 +439,11 @@ TEST_F(SvcTest, GetLayerResolution) {
vpx_svc_set_scale_factors(&svc_, "4/16,8/16"); vpx_svc_set_scale_factors(&svc_, "4/16,8/16");
vpx_svc_set_quantizers(&svc_, "40,30"); vpx_svc_set_quantizers(&svc_, "40,30");
vpx_codec_err_t res = InitializeEncoder();
vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_);
EXPECT_EQ(VPX_CODEC_OK, res);
codec_initialized_ = true;
// ensure that requested layer is a valid layer // ensure that requested layer is a valid layer
uint32_t layer_width, layer_height; uint32_t layer_width, layer_height;
res = vpx_svc_get_layer_resolution(&svc_, svc_.spatial_layers, vpx_codec_err_t res = vpx_svc_get_layer_resolution(&svc_, svc_.spatial_layers,
&layer_width, &layer_height); &layer_width, &layer_height);
EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res); EXPECT_EQ(VPX_CODEC_INVALID_PARAM, res);
@ -373,131 +467,113 @@ TEST_F(SvcTest, GetLayerResolution) {
EXPECT_EQ(kHeight * 8 / 16, layer_height); EXPECT_EQ(kHeight * 8 / 16, layer_height);
} }
TEST_F(SvcTest, TwoPassEncode) { TEST_F(SvcTest, TwoPassEncode10Frames) {
// First pass encode // First pass encode
std::string stats_buf; std::string stats_buf;
svc_.spatial_layers = 2; Pass1EncodeNFrames(10, 2, &stats_buf);
codec_enc_.g_pass = VPX_RC_FIRST_PASS;
vpx_svc_set_scale_factors(&svc_, "4/16,16/16"); // Second pass encode
vpx_svc_set_quantizers(&svc_, "40,30"); codec_enc_.g_pass = VPX_RC_LAST_PASS;
vpx_svc_set_options(&svc_, "auto-alt-refs=1,1"); vpx_fixed_buf outputs[10];
memset(&outputs[0], 0, sizeof(outputs));
vpx_codec_err_t res = Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_); DecodeNFrames(&outputs[0], 10);
ASSERT_EQ(VPX_CODEC_OK, res); FreeBitstreamBuffers(&outputs[0], 10);
codec_initialized_ = true; }
libvpx_test::I420VideoSource video(test_file_name_, kWidth, kHeight, TEST_F(SvcTest, TwoPassEncode20FramesWithAltRef) {
codec_enc_.g_timebase.den, // First pass encode
codec_enc_.g_timebase.num, 0, 30); std::string stats_buf;
// FRAME 0 Pass1EncodeNFrames(20, 2, &stats_buf);
video.Begin();
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
ASSERT_EQ(VPX_CODEC_OK, res);
size_t stats_size = vpx_svc_get_rc_stats_buffer_size(&svc_);
EXPECT_GT(stats_size, 0U);
const char *stats_data = vpx_svc_get_rc_stats_buffer(&svc_);
ASSERT_TRUE(stats_data != NULL);
stats_buf.append(stats_data, stats_size);
// FRAME 1
video.Next();
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
stats_size = vpx_svc_get_rc_stats_buffer_size(&svc_);
EXPECT_GT(stats_size, 0U);
stats_data = vpx_svc_get_rc_stats_buffer(&svc_);
ASSERT_TRUE(stats_data != NULL);
stats_buf.append(stats_data, stats_size);
// Flush encoder and test EOS packet
res = vpx_svc_encode(&svc_, &codec_, NULL, video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
stats_size = vpx_svc_get_rc_stats_buffer_size(&svc_);
EXPECT_GT(stats_size, 0U);
stats_data = vpx_svc_get_rc_stats_buffer(&svc_);
ASSERT_TRUE(stats_data != NULL);
stats_buf.append(stats_data, stats_size);
// Tear down encoder
vpx_svc_release(&svc_);
vpx_codec_destroy(&codec_);
// Second pass encode // Second pass encode
int decoded_frames = 0;
vpx_codec_err_t res_dec;
int frame_size;
codec_enc_.g_pass = VPX_RC_LAST_PASS; codec_enc_.g_pass = VPX_RC_LAST_PASS;
vpx_svc_set_scale_factors(&svc_, "4/16,16/16");
vpx_svc_set_quantizers(&svc_, "40,30");
vpx_svc_set_options(&svc_, "auto-alt-refs=1,1"); vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
codec_enc_.rc_twopass_stats_in.buf = &stats_buf[0]; vpx_fixed_buf outputs[20];
codec_enc_.rc_twopass_stats_in.sz = stats_buf.size(); memset(&outputs[0], 0, sizeof(outputs));
Pass2EncodeNFrames(&stats_buf, 20, 2, &outputs[0]);
DecodeNFrames(&outputs[0], 20);
FreeBitstreamBuffers(&outputs[0], 20);
}
res = vpx_svc_init(&svc_, &codec_, vpx_codec_vp9_cx(), &codec_enc_); TEST_F(SvcTest, TwoPassEncode2LayersDecodeBaseLayerOnly) {
ASSERT_EQ(VPX_CODEC_OK, res); // First pass encode
codec_initialized_ = true; std::string stats_buf;
Pass1EncodeNFrames(10, 2, &stats_buf);
// FRAME 0 // Second pass encode
video.Begin(); codec_enc_.g_pass = VPX_RC_LAST_PASS;
// This frame is a keyframe. vpx_svc_set_options(&svc_, "auto-alt-refs=1,1");
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(), vpx_fixed_buf outputs[10];
video.duration(), VPX_DL_GOOD_QUALITY); memset(&outputs[0], 0, sizeof(outputs));
ASSERT_EQ(VPX_CODEC_OK, res); Pass2EncodeNFrames(&stats_buf, 10, 2, &outputs[0]);
DropEnhancementLayers(&outputs[0], 10, 1);
DecodeNFrames(&outputs[0], 10);
FreeBitstreamBuffers(&outputs[0], 10);
}
if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) { TEST_F(SvcTest, TwoPassEncode5LayersDecode54321Layers) {
EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_)); // First pass encode
res_dec = decoder_->DecodeFrame( std::string stats_buf;
static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size); Pass1EncodeNFrames(10, 5, &stats_buf);
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError();
++decoded_frames;
}
// FRAME 1 // Second pass encode
video.Next(); codec_enc_.g_pass = VPX_RC_LAST_PASS;
// This is a P-frame. vpx_svc_set_options(&svc_, "auto-alt-refs=0,1,1,1,0");
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(), vpx_fixed_buf outputs[10];
video.duration(), VPX_DL_GOOD_QUALITY); memset(&outputs[0], 0, sizeof(outputs));
ASSERT_EQ(VPX_CODEC_OK, res); Pass2EncodeNFrames(&stats_buf, 10, 5, &outputs[0]);
if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) { DecodeNFrames(&outputs[0], 10);
EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_)); DropEnhancementLayers(&outputs[0], 10, 4);
res_dec = decoder_->DecodeFrame( DecodeNFrames(&outputs[0], 10);
static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size); DropEnhancementLayers(&outputs[0], 10, 3);
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError(); DecodeNFrames(&outputs[0], 10);
++decoded_frames; DropEnhancementLayers(&outputs[0], 10, 2);
} DecodeNFrames(&outputs[0], 10);
DropEnhancementLayers(&outputs[0], 10, 1);
DecodeNFrames(&outputs[0], 10);
// FRAME 2 FreeBitstreamBuffers(&outputs[0], 10);
video.Next(); }
// This is a P-frame.
res = vpx_svc_encode(&svc_, &codec_, video.img(), video.pts(),
video.duration(), VPX_DL_GOOD_QUALITY);
ASSERT_EQ(VPX_CODEC_OK, res);
if ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) { TEST_F(SvcTest, TwoPassEncode2SNRLayers) {
EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_)); // First pass encode
res_dec = decoder_->DecodeFrame( std::string stats_buf;
static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size); vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1");
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError(); Pass1EncodeNFrames(20, 2, &stats_buf);
++decoded_frames;
}
// Flush encoder // Second pass encode
res = vpx_svc_encode(&svc_, &codec_, NULL, 0, codec_enc_.g_pass = VPX_RC_LAST_PASS;
video.duration(), VPX_DL_GOOD_QUALITY); vpx_svc_set_options(&svc_,
EXPECT_EQ(VPX_CODEC_OK, res); "auto-alt-refs=1,1 scale-factors=1/1,1/1");
vpx_fixed_buf outputs[20];
memset(&outputs[0], 0, sizeof(outputs));
Pass2EncodeNFrames(&stats_buf, 20, 2, &outputs[0]);
DecodeNFrames(&outputs[0], 20);
FreeBitstreamBuffers(&outputs[0], 20);
}
while ((frame_size = vpx_svc_get_frame_size(&svc_)) > 0) { TEST_F(SvcTest, TwoPassEncode3SNRLayersDecode321Layers) {
EXPECT_EQ((decoded_frames == 0), vpx_svc_is_keyframe(&svc_)); // First pass encode
res_dec = decoder_->DecodeFrame( std::string stats_buf;
static_cast<const uint8_t *>(vpx_svc_get_buffer(&svc_)), frame_size); vpx_svc_set_options(&svc_, "scale-factors=1/1,1/1,1/1");
ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder_->DecodeError(); Pass1EncodeNFrames(20, 3, &stats_buf);
++decoded_frames;
}
EXPECT_EQ(decoded_frames, 3); // Second pass encode
codec_enc_.g_pass = VPX_RC_LAST_PASS;
vpx_svc_set_options(&svc_,
"auto-alt-refs=1,1,1 scale-factors=1/1,1/1,1/1");
vpx_fixed_buf outputs[20];
memset(&outputs[0], 0, sizeof(outputs));
Pass2EncodeNFrames(&stats_buf, 20, 3, &outputs[0]);
DecodeNFrames(&outputs[0], 20);
DropEnhancementLayers(&outputs[0], 20, 2);
DecodeNFrames(&outputs[0], 20);
DropEnhancementLayers(&outputs[0], 20, 1);
DecodeNFrames(&outputs[0], 20);
FreeBitstreamBuffers(&outputs[0], 20);
} }
} // namespace } // namespace

View File

@ -310,3 +310,67 @@ int vp9_get_raw_frame(VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
vp9_clear_system_state(); vp9_clear_system_state();
return ret; return ret;
} }
vpx_codec_err_t vp9_parse_superframe_index(const uint8_t *data,
size_t data_sz,
uint32_t sizes[8], int *count,
vpx_decrypt_cb decrypt_cb,
void *decrypt_state) {
// A chunk ending with a byte matching 0xc0 is an invalid chunk unless
// it is a super frame index. If the last byte of real video compression
// data is 0xc0 the encoder must add a 0 byte. If we have the marker but
// not the associated matching marker byte at the front of the index we have
// an invalid bitstream and need to return an error.
uint8_t marker;
assert(data_sz);
marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1);
*count = 0;
if ((marker & 0xe0) == 0xc0) {
const uint32_t frames = (marker & 0x7) + 1;
const uint32_t mag = ((marker >> 3) & 0x3) + 1;
const size_t index_sz = 2 + mag * frames;
// This chunk is marked as having a superframe index but doesn't have
// enough data for it, thus it's an invalid superframe index.
if (data_sz < index_sz)
return VPX_CODEC_CORRUPT_FRAME;
{
const uint8_t marker2 = read_marker(decrypt_cb, decrypt_state,
data + data_sz - index_sz);
// This chunk is marked as having a superframe index but doesn't have
// the matching marker byte at the front of the index therefore it's an
// invalid chunk.
if (marker != marker2)
return VPX_CODEC_CORRUPT_FRAME;
}
{
// Found a valid superframe index.
uint32_t i, j;
const uint8_t *x = &data[data_sz - index_sz + 1];
// Frames has a maximum of 8 and mag has a maximum of 4.
uint8_t clear_buffer[32];
assert(sizeof(clear_buffer) >= frames * mag);
if (decrypt_cb) {
decrypt_cb(decrypt_state, x, clear_buffer, frames * mag);
x = clear_buffer;
}
for (i = 0; i < frames; ++i) {
uint32_t this_sz = 0;
for (j = 0; j < mag; ++j)
this_sz |= (*x++) << (j * 8);
sizes[i] = this_sz;
}
*count = frames;
}
}
return VPX_CODEC_OK;
}

View File

@ -78,6 +78,25 @@ struct VP9Decoder *vp9_decoder_create();
void vp9_decoder_remove(struct VP9Decoder *pbi); void vp9_decoder_remove(struct VP9Decoder *pbi);
static INLINE uint8_t read_marker(vpx_decrypt_cb decrypt_cb,
void *decrypt_state,
const uint8_t *data) {
if (decrypt_cb) {
uint8_t marker;
decrypt_cb(decrypt_state, data, &marker, 1);
return marker;
}
return *data;
}
// This function is exposed for use in tests, as well as the inlined function
// "read_marker".
vpx_codec_err_t vp9_parse_superframe_index(const uint8_t *data,
size_t data_sz,
uint32_t sizes[8], int *count,
vpx_decrypt_cb decrypt_cb,
void *decrypt_state);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif

View File

@ -332,81 +332,6 @@ static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
return VPX_CODEC_OK; return VPX_CODEC_OK;
} }
static INLINE uint8_t read_marker(vpx_decrypt_cb decrypt_cb,
void *decrypt_state,
const uint8_t *data) {
if (decrypt_cb) {
uint8_t marker;
decrypt_cb(decrypt_state, data, &marker, 1);
return marker;
}
return *data;
}
static vpx_codec_err_t parse_superframe_index(const uint8_t *data,
size_t data_sz,
uint32_t sizes[8], int *count,
vpx_decrypt_cb decrypt_cb,
void *decrypt_state) {
// A chunk ending with a byte matching 0xc0 is an invalid chunk unless
// it is a super frame index. If the last byte of real video compression
// data is 0xc0 the encoder must add a 0 byte. If we have the marker but
// not the associated matching marker byte at the front of the index we have
// an invalid bitstream and need to return an error.
uint8_t marker;
assert(data_sz);
marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1);
*count = 0;
if ((marker & 0xe0) == 0xc0) {
const uint32_t frames = (marker & 0x7) + 1;
const uint32_t mag = ((marker >> 3) & 0x3) + 1;
const size_t index_sz = 2 + mag * frames;
// This chunk is marked as having a superframe index but doesn't have
// enough data for it, thus it's an invalid superframe index.
if (data_sz < index_sz)
return VPX_CODEC_CORRUPT_FRAME;
{
const uint8_t marker2 = read_marker(decrypt_cb, decrypt_state,
data + data_sz - index_sz);
// This chunk is marked as having a superframe index but doesn't have
// the matching marker byte at the front of the index therefore it's an
// invalid chunk.
if (marker != marker2)
return VPX_CODEC_CORRUPT_FRAME;
}
{
// Found a valid superframe index.
uint32_t i, j;
const uint8_t *x = &data[data_sz - index_sz + 1];
// Frames has a maximum of 8 and mag has a maximum of 4.
uint8_t clear_buffer[32];
assert(sizeof(clear_buffer) >= frames * mag);
if (decrypt_cb) {
decrypt_cb(decrypt_state, x, clear_buffer, frames * mag);
x = clear_buffer;
}
for (i = 0; i < frames; ++i) {
uint32_t this_sz = 0;
for (j = 0; j < mag; ++j)
this_sz |= (*x++) << (j * 8);
sizes[i] = this_sz;
}
*count = frames;
}
}
return VPX_CODEC_OK;
}
static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx, static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
const uint8_t *data, unsigned int data_sz, const uint8_t *data, unsigned int data_sz,
void *user_priv, long deadline) { void *user_priv, long deadline) {
@ -424,8 +349,8 @@ static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
// Reset flushed when receiving a valid frame. // Reset flushed when receiving a valid frame.
ctx->flushed = 0; ctx->flushed = 0;
res = parse_superframe_index(data, data_sz, frame_sizes, &frame_count, res = vp9_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
ctx->decrypt_cb, ctx->decrypt_state); ctx->decrypt_cb, ctx->decrypt_state);
if (res != VPX_CODEC_OK) if (res != VPX_CODEC_OK)
return res; return res;