[FFmpeg-devel] [PATCH 3/3] af_amerge: use the buferqueue API.
Nicolas George
nicolas.george at normalesup.org
Thu May 31 21:59:18 CEST 2012
Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
libavfilter/af_amerge.c | 65 +++++++++++++++++------------------------------
1 file changed, 24 insertions(+), 41 deletions(-)
diff --git a/libavfilter/af_amerge.c b/libavfilter/af_amerge.c
index 27a35a8..84ffc1f 100644
--- a/libavfilter/af_amerge.c
+++ b/libavfilter/af_amerge.c
@@ -28,26 +28,25 @@
#include "audio.h"
#include "internal.h"
-#define QUEUE_SIZE 16
+#define FF_BUFQUEUE_EXTRA_FIELDS \
+ int nb_samples; \
+ int pos;
+#include "bufferqueue.h"
typedef struct {
int nb_in_ch[2]; /**< number of channels for each input */
int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
int bps;
- struct amerge_queue {
- AVFilterBufferRef *buf[QUEUE_SIZE];
- int nb_buf, nb_samples, pos;
- } queue[2];
+ struct FFBufQueue queue[2];
} AMergeContext;
static av_cold void uninit(AVFilterContext *ctx)
{
AMergeContext *am = ctx->priv;
- int i, j;
+ int i;
for (i = 0; i < 2; i++)
- for (j = 0; j < am->queue[i].nb_buf; j++)
- avfilter_unref_buffer(am->queue[i].buf[j]);
+ ff_bufqueue_discard_all(&am->queue[i]);
}
static int query_formats(AVFilterContext *ctx)
@@ -189,47 +188,39 @@ static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
AMergeContext *am = ctx->priv;
AVFilterLink *const outlink = ctx->outputs[0];
int input_number = inlink == ctx->inputs[1];
- struct amerge_queue *inq = &am->queue[input_number];
+ struct FFBufQueue *inq = &am->queue[input_number];
int nb_samples, ns, i;
- AVFilterBufferRef *outbuf, **inbuf[2];
+ AVFilterBufferRef *outbuf, *inbuf[2];
uint8_t *ins[2], *outs;
- if (inq->nb_buf == QUEUE_SIZE) {
- av_log(ctx, AV_LOG_ERROR, "Packet queue overflow; dropped\n");
- avfilter_unref_buffer(insamples);
- return;
- }
- inq->buf[inq->nb_buf++] = avfilter_ref_buffer(insamples, AV_PERM_READ |
- AV_PERM_PRESERVE);
+ ff_bufqueue_add(ctx, inq, insamples);
inq->nb_samples += insamples->audio->nb_samples;
- avfilter_unref_buffer(insamples);
if (!am->queue[!input_number].nb_samples)
return;
nb_samples = FFMIN(am->queue[0].nb_samples,
am->queue[1].nb_samples);
- outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE,
- nb_samples);
+ outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE, nb_samples);
outs = outbuf->data[0];
for (i = 0; i < 2; i++) {
- inbuf[i] = am->queue[i].buf;
- ins[i] = (*inbuf[i])->data[0] +
+ inbuf[i] = ff_bufqueue_peek(&am->queue[i], 0);
+ ins[i] = inbuf[i]->data[0] +
am->queue[i].pos * am->nb_in_ch[i] * am->bps;
}
- outbuf->pts = (*inbuf[0])->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
- (*inbuf[0])->pts +
+ outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
+ inbuf[0]->pts +
av_rescale_q(am->queue[0].pos,
(AVRational){ 1, ctx->inputs[0]->sample_rate },
ctx->outputs[0]->time_base);
- avfilter_copy_buffer_ref_props(outbuf, *inbuf[0]);
+ avfilter_copy_buffer_ref_props(outbuf, inbuf[0]);
outbuf->audio->nb_samples = nb_samples;
outbuf->audio->channel_layout = outlink->channel_layout;
while (nb_samples) {
ns = nb_samples;
for (i = 0; i < 2; i++)
- ns = FFMIN(ns, (*inbuf[i])->audio->nb_samples - am->queue[i].pos);
+ ns = FFMIN(ns, inbuf[i]->audio->nb_samples - am->queue[i].pos);
/* Unroll the most common sample formats: speed +~350% for the loop,
+~13% overall (including two common decoders) */
switch (am->bps) {
@@ -251,23 +242,15 @@ static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
for (i = 0; i < 2; i++) {
am->queue[i].nb_samples -= ns;
am->queue[i].pos += ns;
- if (am->queue[i].pos == (*inbuf[i])->audio->nb_samples) {
+ if (am->queue[i].pos == inbuf[i]->audio->nb_samples) {
am->queue[i].pos = 0;
- avfilter_unref_buffer(*inbuf[i]);
- *inbuf[i] = NULL;
- inbuf[i]++;
- ins[i] = *inbuf[i] ? (*inbuf[i])->data[0] : NULL;
+ avfilter_unref_buffer(inbuf[i]);
+ ff_bufqueue_get(&am->queue[i]);
+ inbuf[i] = ff_bufqueue_peek(&am->queue[i], 0);
+ ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
}
}
}
- for (i = 0; i < 2; i++) {
- int nbufused = inbuf[i] - am->queue[i].buf;
- if (nbufused) {
- am->queue[i].nb_buf -= nbufused;
- memmove(am->queue[i].buf, inbuf[i],
- am->queue[i].nb_buf * sizeof(**inbuf));
- }
- }
ff_filter_samples(ctx->outputs[0], outbuf);
}
@@ -283,11 +266,11 @@ AVFilter avfilter_af_amerge = {
{ .name = "in1",
.type = AVMEDIA_TYPE_AUDIO,
.filter_samples = filter_samples,
- .min_perms = AV_PERM_READ, },
+ .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
{ .name = "in2",
.type = AVMEDIA_TYPE_AUDIO,
.filter_samples = filter_samples,
- .min_perms = AV_PERM_READ, },
+ .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
{ .name = NULL }
},
.outputs = (const AVFilterPad[]) {
--
1.7.10
More information about the ffmpeg-devel
mailing list