Skip to content

Commit

Permalink
Add API to calculate padded frame buffer size.
Browse files Browse the repository at this point in the history
  • Loading branch information
foo86 committed Apr 17, 2015
1 parent 84b77a1 commit 467f941
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
8 changes: 8 additions & 0 deletions libdcadec/dca_frame.c
Expand Up @@ -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;
}
17 changes: 17 additions & 0 deletions libdcadec/dca_frame.h
Expand Up @@ -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
6 changes: 2 additions & 4 deletions libdcadec/dca_stream.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 467f941

Please sign in to comment.