af: don't require filters to allocate af_instance->data, redo buffers

Allocate af_instance->data in generic code before filter initialization.
Every filter needs af->data (since it contains the output
configuration), so there's no reason why every filter should allocate
and free it.

Remove RESIZE_LOCAL_BUFFER(), and replace it with mp_audio_realloc_min().
Interestingly, most code becomes simpler, because the new function takes
the size in samples, and not in bytes. There are larger change in
af_scaletempo.c and af_lavcac3enc.c, because these had copied and
modified versions of the RESIZE_LOCAL_BUFFER macro/function.
This commit is contained in:
wm4 2013-11-10 23:20:06 +01:00
parent e763d528e2
commit d115fb3b0e
27 changed files with 36 additions and 211 deletions

View File

@ -201,6 +201,7 @@ static struct af_instance *af_create(struct af_stream *s, char *name,
*af = (struct af_instance) {
.info = info,
.mul = 1,
.data = talloc_zero(af, struct mp_audio),
};
struct m_config *config = m_config_from_obj_desc(af, &desc);
if (m_config_initialize_obj(config, &desc, &af->priv, &args) < 0)
@ -730,42 +731,6 @@ double af_calc_delay(struct af_stream *s)
return delay;
}
/* I a local buffer is used (i.e. if the filter doesn't operate on the incoming
* buffer), this macro must be called to ensure the buffer is big enough. */
int af_resize_local_buffer(struct af_instance *af, struct mp_audio *data)
{
assert(data->format);
if (!af->data->format && !af->data->planes[0]) {
// Dummy initialization
mp_audio_set_format(af->data, AF_FORMAT_U8);
}
int oldlen = af->data->samples * af->data->sstride;
/* Calculate the minimum output buffer size for given input data d
* when using the af_resize_local_buffer function. The +x part ensures
* the value is >= len*mul rounded upwards to whole samples even if the
* double 'mul' is inexact. */
int newlen = data->samples * data->sstride * af->mul + data->sstride + 1;
if (oldlen >= newlen)
return AF_OK;
mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
"old len = %i, new len = %i\n", af->info->name, oldlen, newlen);
// If there is a buffer free it
free(af->data->planes[0]);
// Create new buffer and check that it is OK
af->data->planes[0] = malloc(newlen);
if (!af->data->planes[0]) {
mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
return AF_ERROR;
}
af->data->samples = newlen / af->data->sstride;
return AF_OK;
}
// documentation in af.h
struct af_instance *af_control_any_rev(struct af_stream *s, int cmd, void *arg)
{

View File

@ -61,7 +61,7 @@ struct af_instance {
struct mp_audio * (*play)(struct af_instance *af, struct mp_audio *data);
void *setup; // old field for priv structs
void *priv;
struct mp_audio *data; // configuration for outgoing data stream
struct mp_audio *data; // configuration and buffer for outgoing data stream
struct af_instance *next;
struct af_instance *prev;
double delay; /* Delay caused by the filter, in units of bytes read without
@ -182,10 +182,6 @@ double af_calc_delay(struct af_stream *s);
* \{
*/
int af_resize_local_buffer(struct af_instance *af, struct mp_audio *data);
#define RESIZE_LOCAL_BUFFER af_resize_local_buffer
/**
* \brief convert dB to gain value
* \param n number of values to convert

View File

@ -161,7 +161,6 @@ static int control(struct af_instance *af, int cmd, void *arg)
static void uninit(struct af_instance *af)
{
struct af_bs2b *s = af->priv;
free(af->data);
if (s->filter)
bs2b_close(s->filter);
}
@ -173,12 +172,9 @@ static int af_open(struct af_instance *af)
af->control = control;
af->uninit = uninit;
af->mul = 1;
if (!(af->data = calloc(1, sizeof(struct mp_audio))))
return AF_ERROR;
// NULL means failed initialization
if (!(s->filter = bs2b_open())) {
free(af->data);
return AF_ERROR;
}

View File

@ -78,7 +78,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
// Deallocate memory
static void uninit(struct af_instance* af)
{
free(af->data);
free(af->setup);
}
@ -109,9 +108,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=s=calloc(1,sizeof(af_center_t));
if(af->data == NULL || af->setup == NULL)
if(af->setup == NULL)
return AF_ERROR;
// Set default values
s->ch = 1; // Channel nr 2

View File

@ -222,9 +222,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
static void uninit(struct af_instance* af)
{
free(af->setup);
if (af->data)
free(af->data->planes[0]);
free(af->data);
}
// Filter data through filter
@ -235,8 +232,7 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data)
af_channels_t* s = af->setup;
int i;
if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
return NULL;
mp_audio_realloc_min(af->data, data->samples);
// Reset unused channels
memset(l->planes[0],0,mp_audio_psize(c) / c->nch * l->nch);
@ -260,9 +256,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=calloc(1,sizeof(af_channels_t));
if((af->data == NULL) || (af->setup == NULL))
if(af->setup == NULL)
return AF_ERROR;
return AF_OK;
}

View File

@ -74,8 +74,7 @@ static int control(struct af_instance *af, int cmd, void *arg)
static struct mp_audio *play(struct af_instance *af, struct mp_audio *data)
{
if (RESIZE_LOCAL_BUFFER(af, data) != AF_OK)
return NULL;
mp_audio_realloc_min(af->data, data->samples);
struct mp_audio *out = af->data;
size_t len = mp_audio_psize(data) / data->bps;
@ -104,18 +103,10 @@ static struct mp_audio *play(struct af_instance *af, struct mp_audio *data)
return data;
}
static void uninit(struct af_instance* af)
{
if (af->data)
free(af->data->planes[0]);
}
static int af_open(struct af_instance *af)
{
af->control = control;
af->play = play;
af->uninit = uninit;
af->data = talloc_zero(af, struct mp_audio);
return AF_OK;
}

View File

@ -121,7 +121,6 @@ static int af_open(struct af_instance *af)
af->control = control;
af->play = play;
af->mul = 1;
af->data = talloc_zero(af, struct mp_audio);
return AF_OK;
}

View File

@ -112,7 +112,6 @@ static void uninit(struct af_instance* af)
{
int i;
free(af->data);
for(i=0;i<AF_NCH;i++)
free(((af_delay_t*)(af->setup))->q[i]);
free(af->setup);
@ -183,9 +182,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=calloc(1,sizeof(af_delay_t));
if(af->data == NULL || af->setup == NULL)
if(af->setup == NULL)
return AF_ERROR;
return AF_OK;
}

View File

@ -113,7 +113,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
// Deallocate memory
static void uninit(struct af_instance* af)
{
free(af->data);
free(af->setup);
}
@ -321,9 +320,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=calloc(1,sizeof(af_drc_t));
if(af->data == NULL || af->setup == NULL)
if(af->setup == NULL)
return AF_ERROR;
((af_drc_t*)af->setup)->mul = MUL_INIT;

View File

@ -40,12 +40,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
return AF_UNKNOWN;
}
// Deallocate memory
static void uninit(struct af_instance* af)
{
free(af->data);
}
// Filter data through filter
static struct mp_audio* play(struct af_instance* af, struct mp_audio* data)
{
@ -58,12 +52,8 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data)
// Allocate memory and set function pointers
static int af_open(struct af_instance* af){
af->control=control;
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=malloc(sizeof(struct mp_audio));
if(af->data == NULL)
return AF_ERROR;
return AF_OK;
}

View File

@ -156,7 +156,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
// Deallocate memory
static void uninit(struct af_instance* af)
{
free(af->data);
free(af->setup);
}
@ -205,9 +204,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=calloc(1,sizeof(af_equalizer_t));
if(af->data == NULL || af->setup == NULL)
if(af->setup == NULL)
return AF_ERROR;
return AF_OK;
}

View File

@ -183,9 +183,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
*/
static void uninit( struct af_instance* af )
{
free(af->data);
af->data = NULL;
if(af->setup){
af_export_t* s = af->setup;
if (s->buf)
@ -260,9 +257,8 @@ static int af_open( struct af_instance* af )
af->uninit = uninit;
af->play = play;
af->mul=1;
af->data = calloc(1, sizeof(struct mp_audio));
af->setup = calloc(1, sizeof(af_export_t));
if((af->data == NULL) || (af->setup == NULL))
if(af->setup == NULL)
return AF_ERROR;
((af_export_t *)af->setup)->filename = mp_find_user_config_file(SHARED_FILE);

View File

@ -75,7 +75,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
// Deallocate memory
static void uninit(struct af_instance* af)
{
free(af->data);
free(af->setup);
}
@ -130,9 +129,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play_s16;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=calloc(1,sizeof(af_extrastereo_t));
if(af->data == NULL || af->setup == NULL)
if(af->setup == NULL)
return AF_ERROR;
((af_extrastereo_t*)af->setup)->mul = 2.5;

View File

@ -35,9 +35,6 @@ struct priv {
struct mp_chmap out_channels;
int fail;
struct mp_audio data;
struct mp_audio temp;
};
static void force_in_params(struct af_instance *af, struct mp_audio *in)
@ -101,15 +98,8 @@ static int control(struct af_instance *af, int cmd, void *arg)
static struct mp_audio *play(struct af_instance *af, struct mp_audio *data)
{
struct priv *priv = af->priv;
struct mp_audio *r = &priv->temp;
*r = *af->data;
for (int n = 0; n < r->nch; n++)
r->planes[n] = data->planes[n];
r->samples = data->samples;
return r;
mp_audio_copy_config(data, af->data);
return data;
}
static int af_open(struct af_instance *af)
@ -117,8 +107,6 @@ static int af_open(struct af_instance *af)
af->control = control;
af->play = play;
af->mul = 1;
struct priv *priv = af->priv;
af->data = &priv->data;
force_in_params(af, af->data);
force_out_params(af, af->data);

View File

@ -366,9 +366,6 @@ static void uninit(struct af_instance *af)
free(s->fwrbuf_rr);
free(af->setup);
}
if(af->data)
free(af->data->planes[0]);
free(af->data);
}
/* Filter data through filter
@ -391,8 +388,7 @@ static struct mp_audio* play(struct af_instance *af, struct mp_audio *data)
float common, left, right, diff, left_b, right_b;
const int dblen = s->dlbuflen, hlen = s->hrflen, blen = s->basslen;
if(AF_OK != RESIZE_LOCAL_BUFFER(af, data))
return NULL;
mp_audio_realloc_min(af->data, data->samples);
if(s->print_flag) {
s->print_flag = 0;
@ -603,9 +599,8 @@ static int af_open(struct af_instance* af)
af->uninit = uninit;
af->play = play;
af->mul = 1;
af->data = calloc(1, sizeof(struct mp_audio));
af->setup = calloc(1, sizeof(af_hrtf_t));
if((af->data == NULL) || (af->setup == NULL))
if(af->setup == NULL)
return AF_ERROR;
s = af->setup;

View File

@ -41,12 +41,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
return AF_UNKNOWN;
}
// Deallocate memory
static void uninit(struct af_instance* af)
{
free(af->data);
}
// Filter data through filter
static struct mp_audio* play(struct af_instance* af, struct mp_audio* data)
{
@ -74,14 +68,8 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data)
// Allocate memory and set function pointers
static int af_open(struct af_instance* af){
af->control = control;
af->uninit = uninit;
af->play = play;
af->mul = 1;
af->data = calloc(1,sizeof(struct mp_audio));
if(af->data == NULL)
return AF_ERROR;
return AF_OK;
}

View File

@ -646,7 +646,6 @@ static int control(struct af_instance *af, int cmd, void *arg) {
*/
static void uninit(struct af_instance *af) {
free(af->data);
if (af->setup) {
af_ladspa_t *setup = (af_ladspa_t*) af->setup;
const LADSPA_Descriptor *pdes = setup->plugin_descriptor;
@ -878,14 +877,8 @@ static int af_open(struct af_instance *af) {
af->play=play;
af->mul=1;
af->data = calloc(1, sizeof(struct mp_audio));
if (af->data == NULL)
return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
af->setup = calloc(1, sizeof(af_ladspa_t));
if (af->setup == NULL) {
free(af->data);
af->data=NULL;
return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
}

View File

@ -160,9 +160,6 @@ static void uninit(struct af_instance* af)
{
af_ac3enc_t *s = af->setup;
if (af->data)
free(af->data->planes[0]);
free(af->data);
if (s) {
av_free_packet(&s->pkt);
if(s->lavc_actx) {
@ -191,18 +188,8 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* audio)
else
max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
if (mp_audio_psize(af->data) < max_output_len) {
mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
"old len = %i, new len = %i\n", af->info->name,
mp_audio_psize(af->data), max_output_len);
free(af->data->planes[0]);
af->data->planes[0] = malloc(max_output_len);
if (!af->data->planes[0]) {
mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
return NULL;
}
af->data->samples = max_output_len / af->data->sstride;
}
mp_audio_realloc_min(af->data, max_output_len / af->data->sstride);
af->data->samples = max_output_len / af->data->sstride;
l = af->data; // Local data
buf = l->planes[0];
@ -320,7 +307,6 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=s;
s->lavc_acodec = avcodec_find_encoder_by_name("ac3");

View File

@ -58,11 +58,6 @@ struct priv {
AVFilterContext *in;
AVFilterContext *out;
// Guarantee that the data stays valid until next filter call
char *out_buffer;
struct mp_audio temp;
int64_t samples_in;
AVRational timebase_out;
@ -278,8 +273,8 @@ static struct mp_audio *play(struct af_instance *af, struct mp_audio *data)
af->delay = (in_time - out_time) * r->rate * r->sstride;
}
p->temp = *r;
return &p->temp;
*data = *r;
return data;
}
static void uninit(struct af_instance *af)

View File

@ -381,9 +381,6 @@ static int af_open(struct af_instance *af)
af->uninit = uninit;
af->play = play;
af->mul = 1;
af->data = talloc_zero(s, struct mp_audio);
af->data->rate = 0;
if (s->opts.cutoff <= 0.0)
s->opts.cutoff = af_resample_default_cutoff(s->opts.filter_size);

View File

@ -146,9 +146,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
// Deallocate memory
static void uninit(struct af_instance* af)
{
if(af->data)
free(af->data->planes[0]);
free(af->data);
free(af->setup);
}
@ -165,8 +162,7 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data)
int ncho = l->nch; // Number of output channels
register int j,k;
if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
return NULL;
mp_audio_realloc_min(af->data, data->samples);
out = l->planes[0];
// Execute panning
@ -196,9 +192,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=calloc(1,sizeof(af_pan_t));
if(af->data == NULL || af->setup == NULL)
if(af->setup == NULL)
return AF_ERROR;
return AF_OK;
}

View File

@ -47,11 +47,11 @@ typedef struct af_scaletempo_s
// stride
float scale;
float speed;
int frames_stride;
float frames_stride_scaled;
float frames_stride_error;
int bytes_per_frame;
int bytes_stride;
float bytes_stride_scaled;
int bytes_queue;
int bytes_queued;
int bytes_to_slide;
@ -220,21 +220,8 @@ static struct mp_audio *play(struct af_instance *af, struct mp_audio *data)
return data;
}
// RESIZE_LOCAL_BUFFER - can't use macro
int max_bytes_out = ((int)(mp_audio_psize(data) /
s->bytes_stride_scaled) + 1) * s->bytes_stride;
if (max_bytes_out > mp_audio_psize(af->data)) {
mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
"old len = %i, new len = %i\n", af->info->name,
mp_audio_psize(af->data), max_bytes_out);
af->data->planes[0] = realloc(af->data->planes[0], max_bytes_out);
if (!af->data->planes[0]) {
mp_msg(MSGT_AFILTER, MSGL_FATAL,
"[libaf] Could not allocate memory\n");
return NULL;
}
af->data->samples = max_bytes_out / af->data->sstride;
}
mp_audio_realloc_min(af->data,
((int)(data->samples / s->frames_stride_scaled) + 1) * s->frames_stride);
int offset_in = fill_queue(af, data, 0);
int8_t *pout = af->data->planes[0];
@ -309,15 +296,15 @@ static int control(struct af_instance *af, int cmd, void *arg)
}
int bps = af->data->bps;
int frames_stride = srate * s->ms_stride;
s->bytes_stride = frames_stride * bps * nch;
s->bytes_stride_scaled = s->scale * s->bytes_stride;
s->frames_stride_scaled = s->scale * frames_stride;
s->frames_stride = srate * s->ms_stride;
s->bytes_stride = s->frames_stride * bps * nch;
s->frames_stride_scaled = s->scale * s->frames_stride;
int bytes_stride_scaled = s->scale * s->bytes_stride;
s->frames_stride_error = 0;
af->mul = (double)s->bytes_stride / s->bytes_stride_scaled;
af->mul = (double)s->bytes_stride / bytes_stride_scaled;
af->delay = 0;
int frames_overlap = frames_stride * s->percent_overlap;
int frames_overlap = s->frames_stride * s->percent_overlap;
if (frames_overlap <= 0) {
s->bytes_standing = s->bytes_stride;
s->samples_standing = s->bytes_standing / bps;
@ -402,8 +389,8 @@ static int control(struct af_instance *af, int cmd, void *arg)
s->bytes_per_frame = bps * nch;
s->num_channels = nch;
s->bytes_queue
= (s->frames_search + frames_stride + frames_overlap) * bps * nch;
s->bytes_queue = (s->frames_search + s->frames_stride + frames_overlap)
* bps * nch;
s->buf_queue = realloc(s->buf_queue, s->bytes_queue + UNROLL_PADDING);
if (!s->buf_queue) {
mp_msg(MSGT_AFILTER, MSGL_FATAL, "[scaletempo] Out of memory\n");
@ -457,8 +444,6 @@ static int control(struct af_instance *af, int cmd, void *arg)
static void uninit(struct af_instance *af)
{
af_scaletempo_t *s = af->priv;
free(af->data->planes[0]);
free(af->data);
free(s->buf_queue);
free(s->buf_overlap);
free(s->buf_pre_corr);
@ -478,9 +463,6 @@ static int af_open(struct af_instance *af)
af->uninit = uninit;
af->play = play;
af->mul = 1;
af->data = calloc(1, sizeof(struct mp_audio));
if (af->data == NULL)
return AF_ERROR;
s->speed_tempo = !!(s->speed_opt & SCALE_TEMPO);
s->speed_pitch = !!(s->speed_opt & SCALE_PITCH);

View File

@ -85,7 +85,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
// Deallocate memory
static void uninit(struct af_instance* af)
{
free(af->data);
free(af->setup);
}
@ -150,9 +149,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play_s16;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=calloc(1,sizeof(af_sinesuppress_t));
if(af->data == NULL || af->setup == NULL)
if(af->setup == NULL)
return AF_ERROR;
((af_sinesuppress_t*)af->setup)->freq = 50.0;

View File

@ -123,7 +123,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
// Deallocate memory
static void uninit(struct af_instance* af)
{
free(af->data);
free(af->setup);
}
@ -167,9 +166,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=s=calloc(1,sizeof(af_sub_t));
if(af->data == NULL || af->setup == NULL)
if(af->setup == NULL)
return AF_ERROR;
// Set default values
s->ch = 5; // Channel nr 6

View File

@ -144,9 +144,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
// Deallocate memory
static void uninit(struct af_instance* af)
{
if(af->data)
free(af->data->planes[0]);
free(af->data);
free(af->setup);
}
@ -172,8 +169,7 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data){
int ri = s->ri; // Read index for delay queue
int wi = s->wi; // Write index for delay queue
if (AF_OK != RESIZE_LOCAL_BUFFER(af, data))
return NULL;
mp_audio_realloc_min(af->data, data->samples);
out = af->data->planes[0];
@ -248,9 +244,8 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=2;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=calloc(1,sizeof(af_surround_t));
if(af->data == NULL || af->setup == NULL)
if(af->setup == NULL)
return AF_ERROR;
((af_surround_t*)af->setup)->d = 20;
return AF_OK;

View File

@ -58,7 +58,6 @@ static int control(struct af_instance* af, int cmd, void* arg)
// Deallocate memory
static void uninit(struct af_instance* af)
{
free(af->data);
free(af->setup);
}
@ -86,7 +85,6 @@ static int af_open(struct af_instance* af){
af->uninit=uninit;
af->play=play;
af->mul=1;
af->data=calloc(1,sizeof(struct mp_audio));
af->setup=calloc(1,sizeof(af_sweept));
return AF_OK;
}

View File

@ -105,7 +105,6 @@ static int af_open(struct af_instance *af)
af->control = control;
af->play = play;
af->mul = 1;
af->data = talloc_zero(af, struct mp_audio);
af_from_dB(1, &s->cfg_volume, &s->level, 20.0, -200.0, 60.0);
return AF_OK;
}