From 467f9418ef2c70994ea40cb0bf619bddbf2def64 Mon Sep 17 00:00:00 2001 From: foo86 Date: Fri, 17 Apr 2015 18:13:05 +0300 Subject: [PATCH] Add API to calculate padded frame buffer size. --- libdcadec/dca_frame.c | 8 ++++++++ libdcadec/dca_frame.h | 17 +++++++++++++++++ libdcadec/dca_stream.c | 6 ++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/libdcadec/dca_frame.c b/libdcadec/dca_frame.c index 223b1f5..12f4c37 100644 --- a/libdcadec/dca_frame.c +++ b/libdcadec/dca_frame.c @@ -142,3 +142,11 @@ DCADEC_API int dcadec_frame_parse_header(const uint8_t *data, size_t *size) return -DCADEC_ENOSYNC; } } + +DCADEC_API size_t dcadec_frame_buffer_size(size_t size) +{ + size_t padding = -size & (DCADEC_FRAME_BUFFER_ALIGN - 1); + if (padding < DCADEC_BUFFER_PADDING) + padding = DCADEC_BUFFER_PADDING; + return size + padding; +} diff --git a/libdcadec/dca_frame.h b/libdcadec/dca_frame.h index 882580b..4203a5d 100644 --- a/libdcadec/dca_frame.h +++ b/libdcadec/dca_frame.h @@ -85,4 +85,21 @@ DCADEC_API int dcadec_frame_convert_bitstream(uint8_t *dst, size_t *dst_size, */ DCADEC_API int dcadec_frame_parse_header(const uint8_t *data, size_t *size); +/** + * Given the raw frame size returned by dcadec_frame_parse_header(), calculate + * minimum required buffer size for performing bitstream format conversion and + * (possibly) parsing the frame. + * + * It is recommended to use this function instead of calculating buffer size + * manually based on DCADEC_FRAME_BUFFER_ALIGN and DCADEC_BUFFER_PADDING + * constants since alignment requirements may change in the future. + * + * @param size Raw frame size, in bytes. + * + * @return Buffer size, in bytes, padded to the minimum value that + * satisfies both dcadec_frame_convert_bitstream() alignment + * requirement and dcadec_context_parse() padding requirement. + */ +DCADEC_API size_t dcadec_frame_buffer_size(size_t size); + #endif diff --git a/libdcadec/dca_stream.c b/libdcadec/dca_stream.c index b5830c0..629c250 100644 --- a/libdcadec/dca_stream.c +++ b/libdcadec/dca_stream.c @@ -202,9 +202,7 @@ DCADEC_API void dcadec_stream_close(struct dcadec_stream *stream) static uint8_t *prepare_packet_buffer(struct dcadec_stream *stream, size_t size) { - size = DCA_ALIGN(size, DCADEC_FRAME_BUFFER_ALIGN); - size += stream->packet_size + DCADEC_BUFFER_PADDING; - size = DCA_ALIGN(size, BUFFER_ALIGN); + size = DCA_ALIGN(stream->packet_size + size, BUFFER_ALIGN); uint8_t *buf = ta_realloc_size(stream, stream->buffer, size); if (buf) { @@ -265,7 +263,7 @@ static int read_frame(struct dcadec_stream *stream, uint32_t *sync_p) return ret; // Reallocate packet buffer - if (!(buf = prepare_packet_buffer(stream, frame_size))) + if (!(buf = prepare_packet_buffer(stream, dcadec_frame_buffer_size(frame_size)))) return -DCADEC_ENOMEM; // Restore frame header