]> git.sesse.net Git - ffmpeg/blob - libavfilter/fifo.c
swr: MMX2 & SSSE3 int16 resample core
[ffmpeg] / libavfilter / fifo.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * FIFO buffering filter
24  */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/samplefmt.h"
30
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "video.h"
35
36 typedef struct Buf {
37     AVFilterBufferRef *buf;
38     struct Buf        *next;
39 } Buf;
40
41 typedef struct {
42     Buf  root;
43     Buf *last;   ///< last buffered frame
44
45     /**
46      * When a specific number of output samples is requested, the partial
47      * buffer is stored here
48      */
49     AVFilterBufferRef *buf_out;
50     int allocated_samples;      ///< number of samples buf_out was allocated for
51 } FifoContext;
52
53 static av_cold int init(AVFilterContext *ctx, const char *args)
54 {
55     FifoContext *fifo = ctx->priv;
56     fifo->last = &fifo->root;
57
58     return 0;
59 }
60
61 static av_cold void uninit(AVFilterContext *ctx)
62 {
63     FifoContext *fifo = ctx->priv;
64     Buf *buf, *tmp;
65
66     for (buf = fifo->root.next; buf; buf = tmp) {
67         tmp = buf->next;
68         avfilter_unref_buffer(buf->buf);
69         av_free(buf);
70     }
71
72     avfilter_unref_buffer(fifo->buf_out);
73 }
74
75 static void add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
76 {
77     FifoContext *fifo = inlink->dst->priv;
78
79     fifo->last->next = av_mallocz(sizeof(Buf));
80     fifo->last = fifo->last->next;
81     fifo->last->buf = buf;
82 }
83
84 static void queue_pop(FifoContext *s)
85 {
86     Buf *tmp = s->root.next->next;
87     if (s->last == s->root.next)
88         s->last = &s->root;
89     av_freep(&s->root.next);
90     s->root.next = tmp;
91 }
92
93 static void end_frame(AVFilterLink *inlink) { }
94
95 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
96
97 /**
98  * Move data pointers and pts offset samples forward.
99  */
100 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
101                           int offset)
102 {
103     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
104     int planar = av_sample_fmt_is_planar(link->format);
105     int planes = planar ? nb_channels : 1;
106     int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
107     int i;
108
109     av_assert0(buf->audio->nb_samples > offset);
110
111     for (i = 0; i < planes; i++)
112         buf->extended_data[i] += block_align*offset;
113     if (buf->data != buf->extended_data)
114         memcpy(buf->data, buf->extended_data,
115                FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
116     buf->linesize[0] -= block_align*offset;
117     buf->audio->nb_samples -= offset;
118
119     if (buf->pts != AV_NOPTS_VALUE) {
120         buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
121                                  link->time_base);
122     }
123 }
124
125 static int calc_ptr_alignment(AVFilterBufferRef *buf)
126 {
127     int planes = av_sample_fmt_is_planar(buf->format) ?
128                  av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
129     int min_align = 128;
130     int p;
131
132     for (p = 0; p < planes; p++) {
133         int cur_align = 128;
134         while ((intptr_t)buf->extended_data[p] % cur_align)
135             cur_align >>= 1;
136         if (cur_align < min_align)
137             min_align = cur_align;
138     }
139     return min_align;
140 }
141
142 static int return_audio_frame(AVFilterContext *ctx)
143 {
144     AVFilterLink *link = ctx->outputs[0];
145     FifoContext *s = ctx->priv;
146     AVFilterBufferRef *head = s->root.next->buf;
147     AVFilterBufferRef *buf_out;
148     int ret;
149
150     if (!s->buf_out &&
151         head->audio->nb_samples >= link->request_samples &&
152         calc_ptr_alignment(head) >= 32) {
153         if (head->audio->nb_samples == link->request_samples) {
154             buf_out = head;
155             queue_pop(s);
156         } else {
157             buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
158             buf_out->audio->nb_samples = link->request_samples;
159             buffer_offset(link, head, link->request_samples);
160         }
161     } else {
162         int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
163
164         if (!s->buf_out) {
165             s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
166                                              link->request_samples);
167             if (!s->buf_out)
168                 return AVERROR(ENOMEM);
169
170             s->buf_out->audio->nb_samples = 0;
171             s->buf_out->pts               = head->pts;
172             s->allocated_samples          = link->request_samples;
173         } else if (link->request_samples != s->allocated_samples) {
174             av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
175                    "buffer was returned.\n");
176             return AVERROR(EINVAL);
177         }
178
179         while (s->buf_out->audio->nb_samples < s->allocated_samples) {
180             int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
181                             head->audio->nb_samples);
182
183             av_samples_copy(s->buf_out->extended_data, head->extended_data,
184                             s->buf_out->audio->nb_samples, 0, len, nb_channels,
185                             link->format);
186             s->buf_out->audio->nb_samples += len;
187
188             if (len == head->audio->nb_samples) {
189                 avfilter_unref_buffer(head);
190                 queue_pop(s);
191
192                 if (!s->root.next &&
193                     (ret = ff_request_frame(ctx->inputs[0])) < 0) {
194                     if (ret == AVERROR_EOF) {
195                         av_samples_set_silence(s->buf_out->extended_data,
196                                                s->buf_out->audio->nb_samples,
197                                                s->allocated_samples -
198                                                s->buf_out->audio->nb_samples,
199                                                nb_channels, link->format);
200                         s->buf_out->audio->nb_samples = s->allocated_samples;
201                         break;
202                     }
203                     return ret;
204                 }
205                 head = s->root.next->buf;
206             } else {
207                 buffer_offset(link, head, len);
208             }
209         }
210         buf_out = s->buf_out;
211         s->buf_out = NULL;
212     }
213     ff_filter_samples(link, buf_out);
214
215     return 0;
216 }
217
218 static int request_frame(AVFilterLink *outlink)
219 {
220     FifoContext *fifo = outlink->src->priv;
221     int ret;
222
223     if (!fifo->root.next) {
224         if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
225             return ret;
226     }
227
228     /* by doing this, we give ownership of the reference to the next filter,
229      * so we don't have to worry about dereferencing it ourselves. */
230     switch (outlink->type) {
231     case AVMEDIA_TYPE_VIDEO:
232         ff_start_frame(outlink, fifo->root.next->buf);
233         ff_draw_slice (outlink, 0, outlink->h, 1);
234         ff_end_frame  (outlink);
235         queue_pop(fifo);
236         break;
237     case AVMEDIA_TYPE_AUDIO:
238         if (outlink->request_samples) {
239             return return_audio_frame(outlink->src);
240         } else {
241             ff_filter_samples(outlink, fifo->root.next->buf);
242             queue_pop(fifo);
243         }
244         break;
245     default:
246         return AVERROR(EINVAL);
247     }
248
249     return 0;
250 }
251
252 AVFilter avfilter_vf_fifo = {
253     .name      = "fifo",
254     .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
255
256     .init      = init,
257     .uninit    = uninit,
258
259     .priv_size = sizeof(FifoContext),
260
261     .inputs    = (const AVFilterPad[]) {{ .name      = "default",
262                                     .type            = AVMEDIA_TYPE_VIDEO,
263                                     .get_video_buffer= ff_null_get_video_buffer,
264                                     .start_frame     = add_to_queue,
265                                     .draw_slice      = draw_slice,
266                                     .end_frame       = end_frame,
267                                     .rej_perms       = AV_PERM_REUSE2, },
268                                   { .name = NULL}},
269     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
270                                     .type            = AVMEDIA_TYPE_VIDEO,
271                                     .request_frame   = request_frame, },
272                                   { .name = NULL}},
273 };
274
275 AVFilter avfilter_af_afifo = {
276     .name        = "afifo",
277     .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
278
279     .init      = init,
280     .uninit    = uninit,
281
282     .priv_size = sizeof(FifoContext),
283
284     .inputs    = (AVFilterPad[]) {{ .name             = "default",
285                                     .type             = AVMEDIA_TYPE_AUDIO,
286                                     .get_audio_buffer = ff_null_get_audio_buffer,
287                                     .filter_samples   = add_to_queue,
288                                     .rej_perms        = AV_PERM_REUSE2, },
289                                   { .name = NULL}},
290     .outputs   = (AVFilterPad[]) {{ .name             = "default",
291                                     .type             = AVMEDIA_TYPE_AUDIO,
292                                     .request_frame    = request_frame, },
293                                   { .name = NULL}},
294 };