diff --git a/test/byte_alignment_test.cc b/test/byte_alignment_test.cc new file mode 100644 index 000000000..aa4b78b9a --- /dev/null +++ b/test/byte_alignment_test.cc @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2014 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 + +#include "./vpx_config.h" +#include "test/codec_factory.h" +#include "test/decode_test_driver.h" +#include "test/md5_helper.h" +#include "test/util.h" +#if CONFIG_WEBM_IO +#include "test/webm_video_source.h" +#endif + +namespace { + +const int kLegacyByteAlignment = 0; +const int kLegacyYPlaneByteAlignment = 32; +const int kNumPlanesToCheck = 3; +const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm"; +const char kVP9Md5File[] = "vp90-2-02-size-lf-1920x1080.webm.md5"; + +#if CONFIG_WEBM_IO + +struct ByteAlignmentTestParam { + int byte_alignment; + vpx_codec_err_t expected_value; + bool decode_remaining; +}; + +const ByteAlignmentTestParam kBaTestParams[] = { + {kLegacyByteAlignment, VPX_CODEC_OK, true}, + {32, VPX_CODEC_OK, true}, + {64, VPX_CODEC_OK, true}, + {128, VPX_CODEC_OK, true}, + {256, VPX_CODEC_OK, true}, + {512, VPX_CODEC_OK, true}, + {1024, VPX_CODEC_OK, true}, + {1, VPX_CODEC_INVALID_PARAM, false}, + {-2, VPX_CODEC_INVALID_PARAM, false}, + {4, VPX_CODEC_INVALID_PARAM, false}, + {16, VPX_CODEC_INVALID_PARAM, false}, + {255, VPX_CODEC_INVALID_PARAM, false}, + {2048, VPX_CODEC_INVALID_PARAM, false}, +}; + +// Class for testing byte alignment of reference buffers. +class ByteAlignmentTest + : public ::testing::TestWithParam { + protected: + ByteAlignmentTest() + : video_(NULL), + decoder_(NULL), + md5_file_(NULL) {} + + virtual void SetUp() { + video_ = new libvpx_test::WebMVideoSource(kVP9TestFile); + ASSERT_TRUE(video_ != NULL); + video_->Init(); + video_->Begin(); + + const vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t(); + decoder_ = new libvpx_test::VP9Decoder(cfg, 0); + ASSERT_TRUE(decoder_ != NULL); + + OpenMd5File(kVP9Md5File); + } + + virtual void TearDown() { + if (md5_file_ != NULL) + fclose(md5_file_); + + delete decoder_; + delete video_; + } + + void SetByteAlignment(int byte_alignment, vpx_codec_err_t expected_value) { + decoder_->Control(VP9_SET_BYTE_ALIGNMENT, byte_alignment, expected_value); + } + + vpx_codec_err_t DecodeOneFrame(int byte_alignment_to_check) { + const vpx_codec_err_t res = + decoder_->DecodeFrame(video_->cxdata(), video_->frame_size()); + CheckDecodedFrames(byte_alignment_to_check); + if (res == VPX_CODEC_OK) + video_->Next(); + return res; + } + + vpx_codec_err_t DecodeRemainingFrames(int byte_alignment_to_check) { + for (; video_->cxdata() != NULL; video_->Next()) { + const vpx_codec_err_t res = + decoder_->DecodeFrame(video_->cxdata(), video_->frame_size()); + if (res != VPX_CODEC_OK) + return res; + CheckDecodedFrames(byte_alignment_to_check); + } + return VPX_CODEC_OK; + } + + private: + // Check if |data| is aligned to |byte_alignment_to_check|. + // |byte_alignment_to_check| must be a power of 2. + void CheckByteAlignment(const uint8_t *data, int byte_alignment_to_check) { + ASSERT_EQ(0u, reinterpret_cast(data) % byte_alignment_to_check); + } + + // Iterate through the planes of the decoded frames and check for + // alignment based off |byte_alignment_to_check|. + void CheckDecodedFrames(int byte_alignment_to_check) { + libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData(); + const vpx_image_t *img; + + // Get decompressed data + while ((img = dec_iter.Next()) != NULL) { + if (byte_alignment_to_check == kLegacyByteAlignment) { + CheckByteAlignment(img->planes[0], kLegacyYPlaneByteAlignment); + } else { + for (int i = 0; i < kNumPlanesToCheck; ++i) { + CheckByteAlignment(img->planes[i], byte_alignment_to_check); + } + } + CheckMd5(*img); + } + } + + // TODO(fgalligan): Move the MD5 testing code into another class. + 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_; + } + + void CheckMd5(const vpx_image_t &img) { + 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"; + } + + libvpx_test::WebMVideoSource *video_; + libvpx_test::VP9Decoder *decoder_; + FILE *md5_file_; +}; + +TEST_F(ByteAlignmentTest, SwitchByteAlignment) { + const int num_elements = 14; + const int byte_alignments[] = { 0, 32, 64, 128, 256, 512, 1024, + 0, 1024, 32, 512, 64, 256, 128 }; + + for (int i = 0; i < num_elements; ++i) { + SetByteAlignment(byte_alignments[i], VPX_CODEC_OK); + ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame(byte_alignments[i])); + } + SetByteAlignment(byte_alignments[0], VPX_CODEC_OK); + ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames(byte_alignments[0])); +} + +TEST_P(ByteAlignmentTest, TestAlignment) { + const ByteAlignmentTestParam t = GetParam(); + SetByteAlignment(t.byte_alignment, t.expected_value); + if (t.decode_remaining) + ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames(t.byte_alignment)); +} + +INSTANTIATE_TEST_CASE_P(Alignments, ByteAlignmentTest, + ::testing::ValuesIn(kBaTestParams)); + +#endif // CONFIG_WEBM_IO + +} // namespace diff --git a/test/decode_test_driver.h b/test/decode_test_driver.h index 76cd12e02..72a279f00 100644 --- a/test/decode_test_driver.h +++ b/test/decode_test_driver.h @@ -66,9 +66,7 @@ class Decoder { } void Control(int ctrl_id, int arg) { - InitOnce(); - const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg); - ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError(); + Control(ctrl_id, arg, VPX_CODEC_OK); } void Control(int ctrl_id, const void *arg) { @@ -77,6 +75,12 @@ class Decoder { ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError(); } + void Control(int ctrl_id, int arg, vpx_codec_err_t expected_value) { + InitOnce(); + const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg); + ASSERT_EQ(expected_value, res) << DecodeError(); + } + const char* DecodeError() { const char *detail = vpx_codec_error_detail(&decoder_); return detail ? detail : vpx_codec_error(&decoder_); diff --git a/test/test.mk b/test/test.mk index d94329924..f5c9c37a7 100644 --- a/test/test.mk +++ b/test/test.mk @@ -31,6 +31,7 @@ LIBVPX_TEST_SRCS-$(CONFIG_VP8_ENCODER) += config_test.cc LIBVPX_TEST_SRCS-$(CONFIG_VP8_ENCODER) += cq_test.cc LIBVPX_TEST_SRCS-$(CONFIG_VP8_ENCODER) += keyframe_test.cc +LIBVPX_TEST_SRCS-$(CONFIG_VP9_DECODER) += byte_alignment_test.cc LIBVPX_TEST_SRCS-$(CONFIG_VP9_DECODER) += external_frame_buffer_test.cc LIBVPX_TEST_SRCS-$(CONFIG_VP9_DECODER) += invalid_file_test.cc LIBVPX_TEST_SRCS-$(CONFIG_VP9_DECODER) += user_priv_test.cc diff --git a/vp9/common/vp9_alloccommon.c b/vp9/common/vp9_alloccommon.c index cb299f9f7..e87f42419 100644 --- a/vp9/common/vp9_alloccommon.c +++ b/vp9/common/vp9_alloccommon.c @@ -110,7 +110,8 @@ int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) { #if CONFIG_VP9_HIGHBITDEPTH cm->use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS) < 0) + VP9_ENC_BORDER_IN_PIXELS, + cm->byte_alignment) < 0) goto fail; if (cm->frame_bufs[i].mvs == NULL) { cm->frame_bufs[i].mvs = diff --git a/vp9/common/vp9_onyxc_int.h b/vp9/common/vp9_onyxc_int.h index 55a1f86c7..a7e7ab16d 100644 --- a/vp9/common/vp9_onyxc_int.h +++ b/vp9/common/vp9_onyxc_int.h @@ -207,6 +207,7 @@ typedef struct VP9Common { int frame_parallel_decoding_mode; int log2_tile_cols, log2_tile_rows; + int byte_alignment; // Private data associated with the frame buffer callbacks. void *cb_priv; diff --git a/vp9/decoder/vp9_decodeframe.c b/vp9/decoder/vp9_decodeframe.c index 58df87d0c..6eb9026a9 100644 --- a/vp9/decoder/vp9_decodeframe.c +++ b/vp9/decoder/vp9_decodeframe.c @@ -719,6 +719,7 @@ static void setup_frame_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { cm->use_highbitdepth, #endif VP9_DEC_BORDER_IN_PIXELS, + cm->byte_alignment, &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer, cm->get_fb_cb, cm->cb_priv)) { vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, @@ -793,6 +794,7 @@ static void setup_frame_size_with_refs(VP9_COMMON *cm, cm->use_highbitdepth, #endif VP9_DEC_BORDER_IN_PIXELS, + cm->byte_alignment, &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer, cm->get_fb_cb, cm->cb_priv)) { vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, diff --git a/vp9/encoder/vp9_denoiser.c b/vp9/encoder/vp9_denoiser.c index 4deeed217..56ec6b335 100644 --- a/vp9/encoder/vp9_denoiser.c +++ b/vp9/encoder/vp9_denoiser.c @@ -425,6 +425,7 @@ int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height, #endif int border) { int i, fail; + const int legacy_byte_alignment = 0; assert(denoiser != NULL); for (i = 0; i < MAX_REF_FRAMES; ++i) { @@ -433,7 +434,7 @@ int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height, #if CONFIG_VP9_HIGHBITDEPTH use_highbitdepth, #endif - border); + border, legacy_byte_alignment); if (fail) { vp9_denoiser_free(denoiser); return 1; @@ -448,7 +449,7 @@ int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height, #if CONFIG_VP9_HIGHBITDEPTH use_highbitdepth, #endif - border); + border, legacy_byte_alignment); if (fail) { vp9_denoiser_free(denoiser); return 1; diff --git a/vp9/encoder/vp9_encoder.c b/vp9/encoder/vp9_encoder.c index aaa6b238d..a7f34a2e4 100644 --- a/vp9/encoder/vp9_encoder.c +++ b/vp9/encoder/vp9_encoder.c @@ -491,7 +491,8 @@ static void alloc_raw_frame_buffers(VP9_COMP *cpi) { #if CONFIG_VP9_HIGHBITDEPTH cm->use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) + VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, + NULL, NULL, NULL)) vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, "Failed to allocate altref buffer"); } @@ -511,7 +512,8 @@ static void alloc_util_frame_buffers(VP9_COMP *cpi) { #if CONFIG_VP9_HIGHBITDEPTH cm->use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) + VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, + NULL, NULL, NULL)) vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, "Failed to allocate last frame buffer"); @@ -521,7 +523,8 @@ static void alloc_util_frame_buffers(VP9_COMP *cpi) { #if CONFIG_VP9_HIGHBITDEPTH cm->use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) + VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, + NULL, NULL, NULL)) vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, "Failed to allocate scaled source buffer"); @@ -531,7 +534,8 @@ static void alloc_util_frame_buffers(VP9_COMP *cpi) { #if CONFIG_VP9_HIGHBITDEPTH cm->use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) + VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, + NULL, NULL, NULL)) vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, "Failed to allocate scaled last source buffer"); } @@ -567,7 +571,8 @@ static void update_frame_size(VP9_COMP *cpi) { #if CONFIG_VP9_HIGHBITDEPTH cm->use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) + VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, + NULL, NULL, NULL)) vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, "Failed to reallocate alt_ref_buffer"); } @@ -2473,7 +2478,8 @@ void vp9_scale_references(VP9_COMP *cpi) { cm->width, cm->height, cm->subsampling_x, cm->subsampling_y, cm->use_highbitdepth, - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL); + VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, + NULL, NULL, NULL); scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf, (int)cm->bit_depth); #else @@ -2482,7 +2488,8 @@ void vp9_scale_references(VP9_COMP *cpi) { vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf, cm->width, cm->height, cm->subsampling_x, cm->subsampling_y, - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL); + VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, + NULL, NULL, NULL); scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf); #endif // CONFIG_VP9_HIGHBITDEPTH cpi->scaled_ref_idx[ref_frame - 1] = new_fb; @@ -2721,7 +2728,8 @@ void set_frame_size(VP9_COMP *cpi) { #if CONFIG_VP9_HIGHBITDEPTH cm->use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL); + VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, + NULL, NULL, NULL); alloc_util_frame_buffers(cpi); init_motion_estimation(cpi); diff --git a/vp9/encoder/vp9_lookahead.c b/vp9/encoder/vp9_lookahead.c index 823e7a162..708072ee2 100644 --- a/vp9/encoder/vp9_lookahead.c +++ b/vp9/encoder/vp9_lookahead.c @@ -65,6 +65,7 @@ struct lookahead_ctx *vp9_lookahead_init(unsigned int width, // Allocate the lookahead structures ctx = calloc(1, sizeof(*ctx)); if (ctx) { + const int legacy_byte_alignment = 0; unsigned int i; ctx->max_sz = depth; ctx->buf = calloc(depth, sizeof(*ctx->buf)); @@ -76,7 +77,8 @@ struct lookahead_ctx *vp9_lookahead_init(unsigned int width, #if CONFIG_VP9_HIGHBITDEPTH use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS)) + VP9_ENC_BORDER_IN_PIXELS, + legacy_byte_alignment)) goto bail; } return ctx; diff --git a/vp9/encoder/vp9_svc_layercontext.c b/vp9/encoder/vp9_svc_layercontext.c index 184322f4f..31e93be65 100644 --- a/vp9/encoder/vp9_svc_layercontext.c +++ b/vp9/encoder/vp9_svc_layercontext.c @@ -39,7 +39,9 @@ void vp9_init_layer_context(VP9_COMP *const cpi) { #if CONFIG_VP9_HIGHBITDEPTH cpi->common.use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) + VP9_ENC_BORDER_IN_PIXELS, + cpi->common.byte_alignment, + NULL, NULL, NULL)) vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, "Failed to allocate empty frame for multiple frame " "contexts"); diff --git a/vp9/encoder/vp9_temporal_filter.c b/vp9/encoder/vp9_temporal_filter.c index a4051f05e..424cc0843 100644 --- a/vp9/encoder/vp9_temporal_filter.c +++ b/vp9/encoder/vp9_temporal_filter.c @@ -710,8 +710,9 @@ void vp9_temporal_filter(VP9_COMP *cpi, int distance) { #if CONFIG_VP9_HIGHBITDEPTH cm->use_highbitdepth, #endif - VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, - NULL)) { + VP9_ENC_BORDER_IN_PIXELS, + cm->byte_alignment, + NULL, NULL, NULL)) { vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, "Failed to reallocate alt_ref_buffer"); } diff --git a/vp9/vp9_dx_iface.c b/vp9/vp9_dx_iface.c index 809514001..43bf35f9c 100644 --- a/vp9/vp9_dx_iface.c +++ b/vp9/vp9_dx_iface.c @@ -44,6 +44,7 @@ struct vpx_codec_alg_priv { int flushed; int invert_tile_order; int frame_parallel_decode; // frame-based threading. + int byte_alignment; // External frame buffer info to save for VP9 common. void *ext_priv; // Private data associated with the external frame buffers. @@ -219,6 +220,7 @@ static void init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) { VP9_COMMON *const cm = &ctx->pbi->common; cm->new_fb_idx = -1; + cm->byte_alignment = ctx->byte_alignment; if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) { cm->get_fb_cb = ctx->get_ext_fb_cb; @@ -617,6 +619,27 @@ static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx, return VPX_CODEC_OK; } +static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx, + va_list args) { + const int legacy_byte_alignment = 0; + const int min_byte_alignment = 32; + const int max_byte_alignment = 1024; + const int byte_alignment = va_arg(args, int); + + if (byte_alignment != legacy_byte_alignment && + (byte_alignment < min_byte_alignment || + byte_alignment > max_byte_alignment || + (byte_alignment & (byte_alignment - 1)) != 0)) + return VPX_CODEC_INVALID_PARAM; + + ctx->byte_alignment = byte_alignment; + if (ctx->pbi != NULL) { + VP9_COMMON *const cm = &ctx->pbi->common; + cm->byte_alignment = byte_alignment; + } + return VPX_CODEC_OK; +} + static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = { {VP8_COPY_REFERENCE, ctrl_copy_reference}, @@ -629,6 +652,7 @@ static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = { {VP8_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options}, {VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order}, {VPXD_SET_DECRYPTOR, ctrl_set_decryptor}, + {VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment}, // Getters {VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates}, diff --git a/vpx/vp8dx.h b/vpx/vp8dx.h index 379b30620..5cc25cd6a 100644 --- a/vpx/vp8dx.h +++ b/vpx/vp8dx.h @@ -78,6 +78,13 @@ enum vp8_dec_control_id { /** control function to get the bit depth of the stream. */ VP9D_GET_BIT_DEPTH, + /** control function to set the byte alignment of the planes in the reference + * buffers. Valid values are power of 2, from 32 to 1024. A value of 0 sets + * legacy alignment. I.e. Y plane is aligned to 32 bytes, U plane directly + * follows Y plane, and V plane directly follows U plane. Default value is 0. + */ + VP9_SET_BYTE_ALIGNMENT, + /** For testing. */ VP9_INVERT_TILE_DECODE_ORDER, diff --git a/vpx_scale/generic/yv12config.c b/vpx_scale/generic/yv12config.c index 00a8c163a..ff49ffb95 100644 --- a/vpx_scale/generic/yv12config.c +++ b/vpx_scale/generic/yv12config.c @@ -143,22 +143,25 @@ int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int use_highbitdepth, #endif int border, + int byte_alignment, vpx_codec_frame_buffer_t *fb, vpx_get_frame_buffer_cb_fn_t cb, void *cb_priv) { if (ybf) { + const int vp9_byte_align = (byte_alignment == 0) ? 1 : byte_alignment; const int aligned_width = (width + 7) & ~7; const int aligned_height = (height + 7) & ~7; const int y_stride = ((aligned_width + 2 * border) + 31) & ~31; const uint64_t yplane_size = (aligned_height + 2 * border) * - (uint64_t)y_stride; + (uint64_t)y_stride + byte_alignment; const int uv_width = aligned_width >> ss_x; const int uv_height = aligned_height >> ss_y; const int uv_stride = y_stride >> ss_x; const int uv_border_w = border >> ss_x; const int uv_border_h = border >> ss_y; const uint64_t uvplane_size = (uv_height + 2 * uv_border_h) * - (uint64_t)uv_stride; + (uint64_t)uv_stride + byte_alignment; + #if CONFIG_ALPHA const int alpha_width = aligned_width; const int alpha_height = aligned_height; @@ -166,7 +169,7 @@ int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, const int alpha_border_w = border; const int alpha_border_h = border; const uint64_t alpha_plane_size = (alpha_height + 2 * alpha_border_h) * - (uint64_t)alpha_stride; + (uint64_t)alpha_stride + byte_alignment; #if CONFIG_VP9_HIGHBITDEPTH const uint64_t frame_size = (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size + alpha_plane_size); @@ -182,6 +185,9 @@ int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, const uint64_t frame_size = yplane_size + 2 * uvplane_size; #endif // CONFIG_VP9_HIGHBITDEPTH #endif // CONFIG_ALPHA + + uint8_t *buf = NULL; + if (cb != NULL) { const int align_addr_extra_size = 31; const uint64_t external_frame_size = frame_size + align_addr_extra_size; @@ -244,38 +250,33 @@ int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, ybf->subsampling_x = ss_x; ybf->subsampling_y = ss_y; + buf = ybf->buffer_alloc; #if CONFIG_VP9_HIGHBITDEPTH if (use_highbitdepth) { // Store uint16 addresses when using 16bit framebuffers - uint8_t *p = CONVERT_TO_BYTEPTR(ybf->buffer_alloc); - ybf->y_buffer = p + (border * y_stride) + border; - ybf->u_buffer = p + yplane_size + - (uv_border_h * uv_stride) + uv_border_w; - ybf->v_buffer = p + yplane_size + uvplane_size + - (uv_border_h * uv_stride) + uv_border_w; + buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc); ybf->flags = YV12_FLAG_HIGHBITDEPTH; } else { - ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border; - ybf->u_buffer = ybf->buffer_alloc + yplane_size + - (uv_border_h * uv_stride) + uv_border_w; - ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size + - (uv_border_h * uv_stride) + uv_border_w; ybf->flags = 0; } -#else - ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border; - ybf->u_buffer = ybf->buffer_alloc + yplane_size + - (uv_border_h * uv_stride) + uv_border_w; - ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size + - (uv_border_h * uv_stride) + uv_border_w; #endif // CONFIG_VP9_HIGHBITDEPTH + ybf->y_buffer = (uint8_t *)yv12_align_addr( + buf + (border * y_stride) + border, vp9_byte_align); + ybf->u_buffer = (uint8_t *)yv12_align_addr( + buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w, + vp9_byte_align); + ybf->v_buffer = (uint8_t *)yv12_align_addr( + buf + yplane_size + uvplane_size + (uv_border_h * uv_stride) + + uv_border_w, vp9_byte_align); + #if CONFIG_ALPHA ybf->alpha_width = alpha_width; ybf->alpha_height = alpha_height; ybf->alpha_stride = alpha_stride; - ybf->alpha_buffer = ybf->buffer_alloc + yplane_size + 2 * uvplane_size + - (alpha_border_h * alpha_stride) + alpha_border_w; + ybf->alpha_buffer = (uint8_t *)yv12_align_addr( + buf + yplane_size + 2 * uvplane_size + + (alpha_border_h * alpha_stride) + alpha_border_w, vp9_byte_align); #endif ybf->corrupted = 0; /* assume not corrupted by errors */ return 0; @@ -289,14 +290,15 @@ int vp9_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, #if CONFIG_VP9_HIGHBITDEPTH int use_highbitdepth, #endif - int border) { + int border, + int byte_alignment) { if (ybf) { vp9_free_frame_buffer(ybf); return vp9_realloc_frame_buffer(ybf, width, height, ss_x, ss_y, #if CONFIG_VP9_HIGHBITDEPTH use_highbitdepth, #endif - border, NULL, NULL, NULL); + border, byte_alignment, NULL, NULL, NULL); } return -2; } diff --git a/vpx_scale/yv12config.h b/vpx_scale/yv12config.h index b9f13fd89..f04dee1e8 100644 --- a/vpx_scale/yv12config.h +++ b/vpx_scale/yv12config.h @@ -73,9 +73,10 @@ int vp9_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, #if CONFIG_VP9_HIGHBITDEPTH int use_highbitdepth, #endif - int border); + int border, int byte_alignment); -// Updates the yv12 buffer config with the frame buffer. If cb is not +// Updates the yv12 buffer config with the frame buffer. |byte_alignment| must +// be a power of 2, from 32 to 1024. 0 sets legacy alignment. If cb is not // NULL, then libvpx is using the frame buffer callbacks to handle memory. // If cb is not NULL, libvpx will call cb with minimum size in bytes needed // to decode the current frame. If cb is NULL, libvpx will allocate memory @@ -87,6 +88,7 @@ int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int use_highbitdepth, #endif int border, + int byte_alignment, vpx_codec_frame_buffer_t *fb, vpx_get_frame_buffer_cb_fn_t cb, void *cb_priv);