From 9388f69f679f0eabde23271944c8f2f35a94dee7 Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 10 Nov 2014 10:43:15 +0100 Subject: audio: use AVBufferRef to allocate audio frames A first step towards refcounted audio frames. Amazingly, the API just does what we want, and the code becomes simpler. We will need to NIH allocation from a pool, though. --- audio/audio.c | 28 +++++++++------------------- audio/audio.h | 2 +- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 8589104137..161546a9a3 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -20,7 +20,7 @@ #include #include -#include +#include #include "talloc.h" #include "common/common.h" @@ -124,11 +124,8 @@ void mp_audio_set_null_data(struct mp_audio *mpa) static void mp_audio_destructor(void *ptr) { struct mp_audio *mpa = ptr; - for (int n = 0; n < MP_NUM_CHANNELS; n++) { - // Note: don't free if not allocated by mp_audio_realloc - if (mpa->allocated[n]) - av_free(mpa->planes[n]); - } + for (int n = 0; n < MP_NUM_CHANNELS; n++) + av_buffer_unref(&mpa->allocated[n]); } /* Reallocate the data stored in mpa->planes[n] so that enough samples are @@ -152,22 +149,15 @@ void mp_audio_realloc(struct mp_audio *mpa, int samples) abort(); // oom int size = MPMAX(samples * mpa->sstride, 1); for (int n = 0; n < mpa->num_planes; n++) { - if (size != mpa->allocated[n]) { - // Note: av_realloc() can't be used (see libavutil doxygen) - void *new = av_malloc(size); - if (!new) - abort(); - if (mpa->allocated[n]) - memcpy(new, mpa->planes[n], MPMIN(mpa->allocated[n], size)); - av_free(mpa->planes[n]); - mpa->planes[n] = new; - mpa->allocated[n] = size; + if (!mpa->allocated[n] || size != mpa->allocated[n]->size) { + if (av_buffer_realloc(&mpa->allocated[n], size) < 0) + abort(); // OOM + mpa->planes[n] = mpa->allocated[n]->data; } } for (int n = mpa->num_planes; n < MP_NUM_CHANNELS; n++) { - av_free(mpa->planes[n]); + av_buffer_unref(&mpa->allocated[n]); mpa->planes[n] = NULL; - mpa->allocated[n] = 0; } talloc_set_destructor(mpa, mp_audio_destructor); } @@ -194,7 +184,7 @@ int mp_audio_get_allocated_size(struct mp_audio *mpa) { int size = 0; for (int n = 0; n < mpa->num_planes; n++) { - int s = mpa->allocated[n] / mpa->sstride; + int s = mpa->allocated[n] ? mpa->allocated[n]->size / mpa->sstride : 0; size = n == 0 ? s : MPMIN(size, s); } return size; diff --git a/audio/audio.h b/audio/audio.h index 2fe4da92b1..aa03f2450c 100644 --- a/audio/audio.h +++ b/audio/audio.h @@ -38,7 +38,7 @@ struct mp_audio { int bps; // size of sub-samples (af_fmt2bps(format)) // private - int allocated[MP_NUM_CHANNELS]; // use mp_audio_get_allocated_size() + struct AVBufferRef *allocated[MP_NUM_CHANNELS]; }; void mp_audio_set_format(struct mp_audio *mpa, int format); -- cgit v1.2.3