mirror of
https://github.com/mpv-player/mpv
synced 2025-03-21 18:57:35 +00:00
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.
This commit is contained in:
parent
5f175b0bdc
commit
9388f69f67
@ -20,7 +20,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include <libavutil/mem.h>
|
#include <libavutil/buffer.h>
|
||||||
|
|
||||||
#include "talloc.h"
|
#include "talloc.h"
|
||||||
#include "common/common.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)
|
static void mp_audio_destructor(void *ptr)
|
||||||
{
|
{
|
||||||
struct mp_audio *mpa = ptr;
|
struct mp_audio *mpa = ptr;
|
||||||
for (int n = 0; n < MP_NUM_CHANNELS; n++) {
|
for (int n = 0; n < MP_NUM_CHANNELS; n++)
|
||||||
// Note: don't free if not allocated by mp_audio_realloc
|
av_buffer_unref(&mpa->allocated[n]);
|
||||||
if (mpa->allocated[n])
|
|
||||||
av_free(mpa->planes[n]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Reallocate the data stored in mpa->planes[n] so that enough samples are
|
/* 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
|
abort(); // oom
|
||||||
int size = MPMAX(samples * mpa->sstride, 1);
|
int size = MPMAX(samples * mpa->sstride, 1);
|
||||||
for (int n = 0; n < mpa->num_planes; n++) {
|
for (int n = 0; n < mpa->num_planes; n++) {
|
||||||
if (size != mpa->allocated[n]) {
|
if (!mpa->allocated[n] || size != mpa->allocated[n]->size) {
|
||||||
// Note: av_realloc() can't be used (see libavutil doxygen)
|
if (av_buffer_realloc(&mpa->allocated[n], size) < 0)
|
||||||
void *new = av_malloc(size);
|
abort(); // OOM
|
||||||
if (!new)
|
mpa->planes[n] = mpa->allocated[n]->data;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int n = mpa->num_planes; n < MP_NUM_CHANNELS; n++) {
|
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->planes[n] = NULL;
|
||||||
mpa->allocated[n] = 0;
|
|
||||||
}
|
}
|
||||||
talloc_set_destructor(mpa, mp_audio_destructor);
|
talloc_set_destructor(mpa, mp_audio_destructor);
|
||||||
}
|
}
|
||||||
@ -194,7 +184,7 @@ int mp_audio_get_allocated_size(struct mp_audio *mpa)
|
|||||||
{
|
{
|
||||||
int size = 0;
|
int size = 0;
|
||||||
for (int n = 0; n < mpa->num_planes; n++) {
|
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);
|
size = n == 0 ? s : MPMIN(size, s);
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
|
@ -38,7 +38,7 @@ struct mp_audio {
|
|||||||
int bps; // size of sub-samples (af_fmt2bps(format))
|
int bps; // size of sub-samples (af_fmt2bps(format))
|
||||||
|
|
||||||
// private
|
// 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);
|
void mp_audio_set_format(struct mp_audio *mpa, int format);
|
||||||
|
Loading…
Reference in New Issue
Block a user