diff options
Diffstat (limited to 'src/chunk.c')
-rw-r--r-- | src/chunk.c | 78 |
1 files changed, 25 insertions, 53 deletions
diff --git a/src/chunk.c b/src/chunk.c index 1eb96f4b..a7f11c3f 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -22,81 +22,53 @@ #include "audio_format.h" #include "tag.h" +#include <libavutil/frame.h> +#include <libavutil/mem.h> + #include <assert.h> void music_chunk_init(struct music_chunk *chunk) { chunk->other = NULL; - chunk->length = 0; + chunk->frame = NULL; chunk->tag = NULL; chunk->replay_gain_serial = 0; } -void -music_chunk_free(struct music_chunk *chunk) +struct music_chunk *music_chunk_alloc(void) { - if (chunk->tag != NULL) - tag_free(chunk->tag); -} + struct music_chunk *ret = av_mallocz(sizeof(*ret)); -#ifndef NDEBUG -bool -music_chunk_check_format(const struct music_chunk *chunk, - const struct audio_format *audio_format) -{ - assert(chunk != NULL); - assert(audio_format != NULL); - assert(audio_format_valid(audio_format)); + if (!ret) + return NULL; - return chunk->length == 0 || - audio_format_equals(&chunk->audio_format, audio_format); + music_chunk_init(ret); + return ret; } -#endif -void * -music_chunk_write(struct music_chunk *chunk, - const struct audio_format *audio_format, - float data_time, uint16_t bit_rate, - size_t *max_length_r) +void music_chunk_free(struct music_chunk *chunk) { - const size_t frame_size = audio_format_frame_size(audio_format); - size_t num_frames; - - assert(music_chunk_check_format(chunk, audio_format)); - assert(chunk->length == 0 || audio_format_valid(&chunk->audio_format)); + if (!chunk) + return; - if (chunk->length == 0) { - /* if the chunk is empty, nobody has set bitRate and - times yet */ + if (chunk->tag != NULL) + tag_free(chunk->tag); - chunk->bit_rate = bit_rate; - chunk->times = data_time; - } - - num_frames = (sizeof(chunk->data) - chunk->length) / frame_size; - if (num_frames == 0) - return NULL; - -#ifndef NDEBUG - chunk->audio_format = *audio_format; -#endif - - *max_length_r = num_frames * frame_size; - return chunk->data + chunk->length; + av_frame_free(&chunk->frame); + av_freep(&chunk); } +#ifndef NDEBUG bool -music_chunk_expand(struct music_chunk *chunk, - const struct audio_format *audio_format, size_t length) +music_chunk_check_format(const struct music_chunk *chunk, + const struct audio_format *audio_format) { - const size_t frame_size = audio_format_frame_size(audio_format); - assert(chunk != NULL); - assert(chunk->length + length <= sizeof(chunk->data)); - assert(audio_format_equals(&chunk->audio_format, audio_format)); - - chunk->length += length; + assert(audio_format != NULL); + assert(audio_format_valid(audio_format)); - return chunk->length + frame_size > sizeof(chunk->data); + return !chunk->frame || + audio_format_equals(&chunk->audio_format, audio_format); } +#endif |