Commit d0ee1fd7 authored by Frank Galligan's avatar Frank Galligan Committed by Gerrit Code Review

Merge "Add support to pass in external frame buffers."

parents c5aaf923 10f89169
......@@ -183,6 +183,7 @@ CODEC_EXPORTS-$(CONFIG_DECODERS) += vpx/exports_dec
INSTALL-LIBS-yes += include/vpx/vpx_codec.h
INSTALL-LIBS-yes += include/vpx/vpx_image.h
INSTALL-LIBS-yes += include/vpx/vpx_external_frame_buffer.h
INSTALL-LIBS-yes += include/vpx/vpx_integer.h
INSTALL-LIBS-$(CONFIG_DECODERS) += include/vpx/vpx_decoder.h
INSTALL-LIBS-$(CONFIG_ENCODERS) += include/vpx/vpx_encoder.h
......
......@@ -26,6 +26,8 @@ extern "C" {
#include "test/encode_test_driver.h"
namespace libvpx_test {
const int kCodecFactoryParam = 0;
class CodecFactory {
public:
CodecFactory() {}
......
......@@ -76,6 +76,16 @@ class Decoder {
return detail ? detail : vpx_codec_error(&decoder_);
}
// Passes the external frame buffer information to libvpx.
vpx_codec_err_t SetExternalFrameBuffers(
vpx_codec_frame_buffer_t *fb_list, int fb_count,
vpx_realloc_frame_buffer_cb_fn_t cb, void *user_priv) {
InitOnce();
return vpx_codec_set_frame_buffers(&decoder_,
fb_list, fb_count,
cb, user_priv);
}
protected:
virtual const vpx_codec_iface_t* CodecInterface() const = 0;
......
/*
* Copyright (c) 2013 The WebM project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <string>
#include "test/codec_factory.h"
#include "test/decode_test_driver.h"
#include "test/ivf_video_source.h"
#include "test/md5_helper.h"
#include "test/test_vectors.h"
#include "test/util.h"
#include "test/webm_video_source.h"
namespace {
const int kVideoNameParam = 1;
const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
// Callback used by libvpx to request the application to allocate a frame
// buffer of at least |new_size| in bytes.
int realloc_vp9_frame_buffer(void *user_priv, size_t new_size,
vpx_codec_frame_buffer_t *fb) {
(void)user_priv;
if (fb == NULL)
return -1;
delete [] fb->data;
fb->data = new uint8_t[new_size];
fb->size = new_size;
return 0;
}
// Callback will not allocate data for frame buffer.
int zero_realloc_vp9_frame_buffer(void *user_priv, size_t new_size,
vpx_codec_frame_buffer_t *fb) {
(void)user_priv;
if (fb == NULL)
return -1;
delete [] fb->data;
fb->data = NULL;
fb->size = new_size;
return 0;
}
// Callback will allocate one less byte.
int one_less_byte_realloc_vp9_frame_buffer(void *user_priv, size_t new_size,
vpx_codec_frame_buffer_t *fb) {
(void)user_priv;
if (fb == NULL)
return -1;
delete [] fb->data;
const size_t error_size = new_size - 1;
fb->data = new uint8_t[error_size];
fb->size = error_size;
return 0;
}
// Class for testing passing in external frame buffers to libvpx.
class ExternalFrameBufferMD5Test
: public ::libvpx_test::DecoderTest,
public ::libvpx_test::CodecTestWithParam<const char*> {
protected:
ExternalFrameBufferMD5Test()
: DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
md5_file_(NULL),
num_buffers_(0),
frame_buffers_(NULL) {}
virtual ~ExternalFrameBufferMD5Test() {
for (int i = 0; i < num_buffers_; ++i) {
delete [] frame_buffers_[i].data;
}
delete [] frame_buffers_;
if (md5_file_ != NULL)
fclose(md5_file_);
}
virtual void PreDecodeFrameHook(
const libvpx_test::CompressedVideoSource &video,
libvpx_test::Decoder *decoder) {
if (num_buffers_ > 0 && video.frame_number() == 0) {
// Have libvpx use frame buffers we create.
frame_buffers_ = new vpx_codec_frame_buffer_t[num_buffers_];
memset(frame_buffers_, 0, sizeof(frame_buffers_[0]) * num_buffers_);
ASSERT_EQ(VPX_CODEC_OK,
decoder->SetExternalFrameBuffers(
frame_buffers_, num_buffers_,
realloc_vp9_frame_buffer, NULL));
}
}
void OpenMD5File(const std::string &md5_file_name_) {
md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
ASSERT_TRUE(md5_file_ != NULL) << "Md5 file open failed. Filename: "
<< md5_file_name_;
}
virtual void DecompressedFrameHook(const vpx_image_t &img,
const unsigned int frame_number) {
ASSERT_TRUE(md5_file_ != NULL);
char expected_md5[33];
char junk[128];
// Read correct md5 checksums.
const int res = fscanf(md5_file_, "%s %s", expected_md5, junk);
ASSERT_NE(EOF, res) << "Read md5 data failed";
expected_md5[32] = '\0';
::libvpx_test::MD5 md5_res;
md5_res.Add(&img);
const char *const actual_md5 = md5_res.Get();
// Check md5 match.
ASSERT_STREQ(expected_md5, actual_md5)
<< "Md5 checksums don't match: frame number = " << frame_number;
}
void set_num_buffers(int num_buffers) { num_buffers_ = num_buffers; }
int num_buffers() const { return num_buffers_; }
private:
FILE *md5_file_;
int num_buffers_;
vpx_codec_frame_buffer_t *frame_buffers_;
};
class ExternalFrameBufferTest : public ::testing::Test {
protected:
ExternalFrameBufferTest()
: video_(NULL),
decoder_(NULL),
num_buffers_(0),
frame_buffers_(NULL) {}
virtual void SetUp() {
video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
video_->Init();
video_->Begin();
vpx_codec_dec_cfg_t cfg = {0};
decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
}
virtual void TearDown() {
for (int i = 0; i < num_buffers_; ++i) {
delete [] frame_buffers_[i].data;
}
delete [] frame_buffers_;
delete decoder_;
delete video_;
}
// Passes the external frame buffer information to libvpx.
vpx_codec_err_t SetExternalFrameBuffers(
int num_buffers,
vpx_realloc_frame_buffer_cb_fn_t cb) {
if (num_buffers > 0) {
num_buffers_ = num_buffers;
// Have libvpx use frame buffers we create.
frame_buffers_ = new vpx_codec_frame_buffer_t[num_buffers_];
memset(frame_buffers_, 0, sizeof(frame_buffers_[0]) * num_buffers_);
}
return decoder_->SetExternalFrameBuffers(frame_buffers_, num_buffers_,
cb, NULL);
}
// Pass Null frame buffer list to libvpx.
vpx_codec_err_t SetNullFrameBuffers(
int num_buffers,
vpx_realloc_frame_buffer_cb_fn_t cb) {
return decoder_->SetExternalFrameBuffers(NULL, num_buffers,
cb, NULL);
}
vpx_codec_err_t DecodeOneFrame() {
const vpx_codec_err_t res =
decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
if (res == VPX_CODEC_OK)
video_->Next();
return res;
}
vpx_codec_err_t DecodeRemainingFrames() {
for (; video_->cxdata(); video_->Next()) {
const vpx_codec_err_t res =
decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
if (res != VPX_CODEC_OK)
return res;
libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
const vpx_image_t *img = NULL;
// Get decompressed data
while ((img = dec_iter.Next())) {
}
}
return VPX_CODEC_OK;
}
libvpx_test::WebMVideoSource *video_;
libvpx_test::VP9Decoder *decoder_;
int num_buffers_;
vpx_codec_frame_buffer_t *frame_buffers_;
};
// This test runs through the set of test vectors, and decodes them.
// Libvpx will call into the application to allocate a frame buffer when
// needed. The md5 checksums are computed for each frame in the video file.
// If md5 checksums match the correct md5 data, then the test is passed.
// Otherwise, the test failed.
TEST_P(ExternalFrameBufferMD5Test, ExtFBMD5Match) {
const std::string filename = GET_PARAM(kVideoNameParam);
libvpx_test::CompressedVideoSource *video = NULL;
// Number of buffers equals number of possible reference buffers(8), plus
// one working buffer, plus four jitter buffers.
const int num_buffers = 13;
set_num_buffers(num_buffers);
// Tell compiler we are not using kVP8TestVectors.
(void)libvpx_test::kVP8TestVectors;
// Open compressed video file.
if (filename.substr(filename.length() - 3, 3) == "ivf") {
video = new libvpx_test::IVFVideoSource(filename);
} else if (filename.substr(filename.length() - 4, 4) == "webm") {
video = new libvpx_test::WebMVideoSource(filename);
}
video->Init();
// Construct md5 file name.
const std::string md5_filename = filename + ".md5";
OpenMD5File(md5_filename);
// Decode frame, and check the md5 matching.
ASSERT_NO_FATAL_FAILURE(RunLoop(video));
delete video;
}
TEST_F(ExternalFrameBufferTest, EightFrameBuffers) {
// Minimum number of reference buffers for VP9 is 8.
const int num_buffers = 8;
ASSERT_EQ(VPX_CODEC_OK,
SetExternalFrameBuffers(num_buffers, realloc_vp9_frame_buffer));
ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
}
TEST_F(ExternalFrameBufferTest, EightJitterBuffers) {
// Number of buffers equals number of possible reference buffers(8), plus
// one working buffer, plus eight jitter buffers.
const int num_buffers = 17;
ASSERT_EQ(VPX_CODEC_OK,
SetExternalFrameBuffers(num_buffers, realloc_vp9_frame_buffer));
ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
}
TEST_F(ExternalFrameBufferTest, NotEnoughBuffers) {
// Minimum number of reference buffers for VP9 is 8.
const int num_buffers = 7;
ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
SetExternalFrameBuffers(num_buffers, realloc_vp9_frame_buffer));
}
TEST_F(ExternalFrameBufferTest, NullFrameBufferList) {
// Number of buffers equals number of possible reference buffers(8), plus
// one working buffer, plus four jitter buffers.
const int num_buffers = 13;
ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
SetNullFrameBuffers(num_buffers, realloc_vp9_frame_buffer));
}
TEST_F(ExternalFrameBufferTest, NullRealloc) {
// Number of buffers equals number of possible reference buffers(8), plus
// one working buffer, plus four jitter buffers.
const int num_buffers = 13;
ASSERT_EQ(VPX_CODEC_OK,
SetExternalFrameBuffers(num_buffers,
zero_realloc_vp9_frame_buffer));
ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
}
TEST_F(ExternalFrameBufferTest, ReallocOneLessByte) {
// Number of buffers equals number of possible reference buffers(8), plus
// one working buffer, plus four jitter buffers.
const int num_buffers = 13;
ASSERT_EQ(VPX_CODEC_OK,
SetExternalFrameBuffers(num_buffers,
one_less_byte_realloc_vp9_frame_buffer));
ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
}
VP9_INSTANTIATE_TEST_CASE(ExternalFrameBufferMD5Test,
::testing::ValuesIn(libvpx_test::kVP9TestVectors));
} // namespace
......@@ -34,6 +34,7 @@ LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += ../md5_utils.h ../md5_utils.c
LIBVPX_TEST_SRCS-yes += decode_test_driver.cc
LIBVPX_TEST_SRCS-yes += decode_test_driver.h
LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += ivf_video_source.h
LIBVPX_TEST_SRCS-$(CONFIG_VP9_DECODER) += external_frame_buffer_test.cc
## WebM Parsing
NESTEGG_SRCS += ../nestegg/halloc/halloc.h
......
......@@ -929,6 +929,7 @@ CODEC_INTERFACE(vpx_codec_vp8_dx) =
vp8_get_si, /* vpx_codec_get_si_fn_t get_si; */
vp8_decode, /* vpx_codec_decode_fn_t decode; */
vp8_get_frame, /* vpx_codec_frame_get_fn_t frame_get; */
NOT_IMPLEMENTED,
},
{ /* encoder functions */
NOT_IMPLEMENTED,
......
......@@ -34,7 +34,7 @@ void vp9_update_mode_info_border(VP9_COMMON *cm, MODE_INFO *mi) {
void vp9_free_frame_buffers(VP9_COMMON *cm) {
int i;
for (i = 0; i < FRAME_BUFFERS; i++)
for (i = 0; i < cm->fb_count; i++)
vp9_free_frame_buffer(&cm->yv12_fb[i]);
vp9_free_frame_buffer(&cm->post_proc_buffer);
......@@ -86,7 +86,7 @@ int vp9_resize_frame_buffers(VP9_COMMON *cm, int width, int height) {
int mi_size;
if (vp9_realloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
VP9BORDERINPIXELS) < 0)
VP9BORDERINPIXELS, NULL, NULL, NULL) < 0)
goto fail;
set_mb_mi(cm, aligned_width, aligned_height);
......@@ -138,16 +138,24 @@ int vp9_alloc_frame_buffers(VP9_COMMON *cm, int width, int height) {
const int ss_y = cm->subsampling_y;
int mi_size;
if (cm->fb_count == 0) {
cm->fb_count = FRAME_BUFFERS;
CHECK_MEM_ERROR(cm, cm->yv12_fb,
vpx_calloc(cm->fb_count, sizeof(*cm->yv12_fb)));
CHECK_MEM_ERROR(cm, cm->fb_idx_ref_cnt,
vpx_calloc(cm->fb_count, sizeof(*cm->fb_idx_ref_cnt)));
}
vp9_free_frame_buffers(cm);
for (i = 0; i < FRAME_BUFFERS; i++) {
for (i = 0; i < cm->fb_count; i++) {
cm->fb_idx_ref_cnt[i] = 0;
if (vp9_alloc_frame_buffer(&cm->yv12_fb[i], width, height, ss_x, ss_y,
VP9BORDERINPIXELS) < 0)
goto fail;
}
cm->new_fb_idx = FRAME_BUFFERS - 1;
cm->new_fb_idx = cm->fb_count - 1;
cm->fb_idx_ref_cnt[cm->new_fb_idx] = 1;
for (i = 0; i < REFS_PER_FRAME; i++)
......@@ -203,6 +211,12 @@ void vp9_create_common(VP9_COMMON *cm) {
void vp9_remove_common(VP9_COMMON *cm) {
vp9_free_frame_buffers(cm);
vpx_free(cm->yv12_fb);
vpx_free(cm->fb_idx_ref_cnt);
cm->yv12_fb = NULL;
cm->fb_idx_ref_cnt = NULL;
}
void vp9_initialize_common() {
......
......@@ -113,8 +113,8 @@ typedef struct VP9Common {
YV12_BUFFER_CONFIG *frame_to_show;
YV12_BUFFER_CONFIG yv12_fb[FRAME_BUFFERS];
int fb_idx_ref_cnt[FRAME_BUFFERS]; /* reference counts */
YV12_BUFFER_CONFIG *yv12_fb;
int *fb_idx_ref_cnt; /* reference counts */
int ref_frame_map[REF_FRAMES]; /* maps fb_idx to reference slot */
// TODO(jkoleszar): could expand active_ref_idx to 4, with 0 as intra, and
......@@ -213,6 +213,11 @@ typedef struct VP9Common {
int frame_parallel_decoding_mode;
int log2_tile_cols, log2_tile_rows;
vpx_codec_frame_buffer_t *fb_list; // External frame buffers
int fb_count; // Total number of frame buffers
vpx_realloc_frame_buffer_cb_fn_t realloc_fb_cb;
void *user_priv; // Private data associated with the external frame buffers.
} VP9_COMMON;
// ref == 0 => LAST_FRAME
......@@ -228,11 +233,11 @@ static YV12_BUFFER_CONFIG *get_frame_new_buffer(VP9_COMMON *cm) {
static int get_free_fb(VP9_COMMON *cm) {
int i;
for (i = 0; i < FRAME_BUFFERS; i++)
for (i = 0; i < cm->fb_count; i++)
if (cm->fb_idx_ref_cnt[i] == 0)
break;
assert(i < FRAME_BUFFERS);
assert(i < cm->fb_count);
cm->fb_idx_ref_cnt[i] = 1;
return i;
}
......
......@@ -397,7 +397,7 @@ void vp9_setup_scale_factors(VP9_COMMON *cm, int i) {
const int ref = cm->active_ref_idx[i];
struct scale_factors *const sf = &cm->active_ref_scale[i];
struct scale_factors_common *const sfc = &cm->active_ref_scale_comm[i];
if (ref >= FRAME_BUFFERS) {
if (ref >= cm->fb_count) {
vp9_zero(*sf);
vp9_zero(*sfc);
} else {
......
......@@ -707,9 +707,21 @@ static void apply_frame_size(VP9D_COMP *pbi, int width, int height) {
vp9_update_frame_size(cm);
}
vp9_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
cm->subsampling_x, cm->subsampling_y,
VP9BORDERINPIXELS);
if (cm->fb_list != NULL) {
vpx_codec_frame_buffer_t *const ext_fb = &cm->fb_list[cm->new_fb_idx];
if (vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
cm->width, cm->height,
cm->subsampling_x, cm->subsampling_y,
VP9BORDERINPIXELS, ext_fb,
cm->realloc_fb_cb, cm->user_priv)) {
vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
"Failed to allocate external frame buffer");
}
} else {
vp9_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
cm->subsampling_x, cm->subsampling_y,
VP9BORDERINPIXELS, NULL, NULL, NULL);
}
}
static void setup_frame_size(VP9D_COMP *pbi,
......
......@@ -926,7 +926,7 @@ static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
cpi->oxcf.width, cpi->oxcf.height,
cm->subsampling_x, cm->subsampling_y,
VP9BORDERINPIXELS))
VP9BORDERINPIXELS, NULL, NULL, NULL))
vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
"Failed to allocate altref buffer");
}
......@@ -994,14 +994,14 @@ static void update_frame_size(VP9_COMP *cpi) {
if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
cm->width, cm->height,
cm->subsampling_x, cm->subsampling_y,
VP9BORDERINPIXELS))
VP9BORDERINPIXELS, NULL, NULL, NULL))
vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
"Failed to reallocate last frame buffer");
if (vp9_realloc_frame_buffer(&cpi->scaled_source,
cm->width, cm->height,
cm->subsampling_x, cm->subsampling_y,
VP9BORDERINPIXELS))
VP9BORDERINPIXELS, NULL, NULL, NULL))
vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
"Failed to reallocate scaled source buffer");
......@@ -2569,7 +2569,7 @@ static void scale_references(VP9_COMP *cpi) {
vp9_realloc_frame_buffer(&cm->yv12_fb[new_fb],
cm->width, cm->height,
cm->subsampling_x, cm->subsampling_y,
VP9BORDERINPIXELS);
VP9BORDERINPIXELS, NULL, NULL, NULL);
scale_and_extend_frame(ref, &cm->yv12_fb[new_fb]);
cpi->scaled_ref_idx[i] = new_fb;
} else {
......@@ -3560,7 +3560,7 @@ int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags,
vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
cm->width, cm->height,
cm->subsampling_x, cm->subsampling_y,
VP9BORDERINPIXELS);
VP9BORDERINPIXELS, NULL, NULL, NULL);
// Calculate scaling factors for each of the 3 available references
for (i = 0; i < REFS_PER_FRAME; ++i) {
......
......@@ -59,6 +59,12 @@ struct vpx_codec_alg_priv {
int img_setup;
int img_avail;
int invert_tile_order;
/* External buffer info to save for VP9 common. */
vpx_codec_frame_buffer_t *fb_list; // External frame buffers
int fb_count; // Total number of frame buffers
vpx_realloc_frame_buffer_cb_fn_t realloc_fb_cb;
void *user_priv; // Private data associated with the external frame buffers.
};
static unsigned long priv_sz(const vpx_codec_dec_cfg_t *si,
......@@ -307,10 +313,26 @@ static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
ctx->postproc_cfg.noise_level = 0;
}
if (!optr)
if (!optr) {
res = VPX_CODEC_ERROR;
else
} else {
VP9D_COMP *const pbi = (VP9D_COMP*)optr;
VP9_COMMON *const cm = &pbi->common;
if (ctx->fb_list != NULL && ctx->realloc_fb_cb != NULL &&
ctx->fb_count > 0) {
cm->fb_list = ctx->fb_list;
cm->fb_count = ctx->fb_count;
cm->realloc_fb_cb = ctx->realloc_fb_cb;
cm->user_priv = ctx->user_priv;
} else {
cm->fb_count = FRAME_BUFFERS;
}
CHECK_MEM_ERROR(cm, cm->yv12_fb,
vpx_calloc(cm->fb_count, sizeof(*cm->yv12_fb)));
CHECK_MEM_ERROR(cm, cm->fb_idx_ref_cnt,
vpx_calloc(cm->fb_count, sizeof(*cm->fb_idx_ref_cnt)));
ctx->pbi = optr;
}
}
ctx->decoder_init = 1;
......@@ -347,7 +369,7 @@ static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
}
if (vp9_receive_compressed_data(ctx->pbi, data_sz, data, deadline)) {
VP9D_COMP *pbi = (VP9D_COMP *)ctx->pbi;
VP9D_COMP *pbi = (VP9D_COMP*)ctx->pbi;
res = update_error_state(ctx, &pbi->common.error);
}
......@@ -475,6 +497,27 @@ static vpx_image_t *vp9_get_frame(vpx_codec_alg_priv_t *ctx,
return img;
}
static vpx_codec_err_t vp9_set_frame_buffers(
vpx_codec_alg_priv_t *ctx,
vpx_codec_frame_buffer_t *fb_list, int fb_count,
vpx_realloc_frame_buffer_cb_fn_t cb, void *user_priv) {
if (fb_count < REF_FRAMES) {
/* The application must pass in at least REF_FRAMES frame buffers. */
return VPX_CODEC_INVALID_PARAM;
} else if (!ctx->pbi) {
/* If the decoder has already been initialized, do not accept external
* frame buffers.
*/
ctx->fb_list = fb_list;
ctx->fb_count = fb_count;
ctx->realloc_fb_cb = cb;
ctx->user_priv = user_priv;
return VPX_CODEC_OK;
}