lavfi: add error handling to draw_slice().
[ffmpeg.git] / libavfilter / fifo.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
76 {
77     FifoContext *fifo = inlink->dst->priv;
78
79     inlink->cur_buf = NULL;
80     fifo->last->next = av_mallocz(sizeof(Buf));
81     if (!fifo->last->next) {
82         avfilter_unref_buffer(buf);
83         return AVERROR(ENOMEM);
84     }
85
86     fifo->last = fifo->last->next;
87     fifo->last->buf = buf;
88
89     return 0;
90 }
91
92 static void queue_pop(FifoContext *s)
93 {
94     Buf *tmp = s->root.next->next;
95     if (s->last == s->root.next)
96         s->last = &s->root;
97     av_freep(&s->root.next);
98     s->root.next = tmp;
99 }
100
101 static void end_frame(AVFilterLink *inlink) { }
102
103 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
104 {
105     return 0;
106 }
107
108 /**
109  * Move data pointers and pts offset samples forward.
110  */
111 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
112                           int offset)
113 {
114     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
115     int planar = av_sample_fmt_is_planar(link->format);
116     int planes = planar ? nb_channels : 1;
117     int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
118     int i;
119
120     av_assert0(buf->audio->nb_samples > offset);
121
122     for (i = 0; i < planes; i++)
123         buf->extended_data[i] += block_align*offset;
124     if (buf->data != buf->extended_data)
125         memcpy(buf->data, buf->extended_data,
126                FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
127     buf->linesize[0] -= block_align*offset;
128     buf->audio->nb_samples -= offset;
129
130     if (buf->pts != AV_NOPTS_VALUE) {
131         buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
132                                  link->time_base);
133     }
134 }
135
136 static int calc_ptr_alignment(AVFilterBufferRef *buf)
137 {
138     int planes = av_sample_fmt_is_planar(buf->format) ?
139                  av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
140     int min_align = 128;
141     int p;
142
143     for (p = 0; p < planes; p++) {
144         int cur_align = 128;
145         while ((intptr_t)buf->extended_data[p] % cur_align)
146             cur_align >>= 1;
147         if (cur_align < min_align)
148             min_align = cur_align;
149     }
150     return min_align;
151 }
152
153 static int return_audio_frame(AVFilterContext *ctx)
154 {
155     AVFilterLink *link = ctx->outputs[0];
156     FifoContext *s = ctx->priv;
157     AVFilterBufferRef *head = s->root.next->buf;
158     AVFilterBufferRef *buf_out;
159     int ret;
160
161     if (!s->buf_out &&
162         head->audio->nb_samples >= link->request_samples &&
163         calc_ptr_alignment(head) >= 32) {
164         if (head->audio->nb_samples == link->request_samples) {
165             buf_out = head;
166             queue_pop(s);
167         } else {
168             buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
169             buf_out->audio->nb_samples = link->request_samples;
170             buffer_offset(link, head, link->request_samples);
171         }
172     } else {
173         int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
174
175         if (!s->buf_out) {
176             s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
177                                              link->request_samples);
178             if (!s->buf_out)
179                 return AVERROR(ENOMEM);
180
181             s->buf_out->audio->nb_samples = 0;
182             s->buf_out->pts               = head->pts;
183             s->allocated_samples          = link->request_samples;
184         } else if (link->request_samples != s->allocated_samples) {
185             av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
186                    "buffer was returned.\n");
187             return AVERROR(EINVAL);
188         }
189
190         while (s->buf_out->audio->nb_samples < s->allocated_samples) {
191             int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
192                             head->audio->nb_samples);
193
194             av_samples_copy(s->buf_out->extended_data, head->extended_data,
195                             s->buf_out->audio->nb_samples, 0, len, nb_channels,
196                             link->format);
197             s->buf_out->audio->nb_samples += len;
198
199             if (len == head->audio->nb_samples) {
200                 avfilter_unref_buffer(head);
201                 queue_pop(s);
202
203                 if (!s->root.next &&
204                     (ret = ff_request_frame(ctx->inputs[0])) < 0) {
205                     if (ret == AVERROR_EOF) {
206                         av_samples_set_silence(s->buf_out->extended_data,
207                                                s->buf_out->audio->nb_samples,
208                                                s->allocated_samples -
209                                                s->buf_out->audio->nb_samples,
210                                                nb_channels, link->format);
211                         s->buf_out->audio->nb_samples = s->allocated_samples;
212                         break;
213                     }
214                     return ret;
215                 }
216                 head = s->root.next->buf;
217             } else {
218                 buffer_offset(link, head, len);
219             }
220         }
221         buf_out = s->buf_out;
222         s->buf_out = NULL;
223     }
224     return ff_filter_samples(link, buf_out);
225 }
226
227 static int request_frame(AVFilterLink *outlink)
228 {
229     FifoContext *fifo = outlink->src->priv;
230     int ret = 0;
231
232     if (!fifo->root.next) {
233         if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
234             return ret;
235     }
236
237     /* by doing this, we give ownership of the reference to the next filter,
238      * so we don't have to worry about dereferencing it ourselves. */
239     switch (outlink->type) {
240     case AVMEDIA_TYPE_VIDEO:
241         ff_start_frame(outlink, fifo->root.next->buf);
242         ff_draw_slice (outlink, 0, outlink->h, 1);
243         ff_end_frame  (outlink);
244         queue_pop(fifo);
245         break;
246     case AVMEDIA_TYPE_AUDIO:
247         if (outlink->request_samples) {
248             return return_audio_frame(outlink->src);
249         } else {
250             ret = ff_filter_samples(outlink, fifo->root.next->buf);
251             queue_pop(fifo);
252         }
253         break;
254     default:
255         return AVERROR(EINVAL);
256     }
257
258     return ret;
259 }
260
261 AVFilter avfilter_vf_fifo = {
262     .name      = "fifo",
263     .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
264
265     .init      = init,
266     .uninit    = uninit,
267
268     .priv_size = sizeof(FifoContext),
269
270     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
271                                           .type            = AVMEDIA_TYPE_VIDEO,
272                                           .get_video_buffer= ff_null_get_video_buffer,
273                                           .start_frame     = add_to_queue,
274                                           .draw_slice      = draw_slice,
275                                           .end_frame       = end_frame,
276                                           .rej_perms       = AV_PERM_REUSE2, },
277                                         { .name = NULL}},
278     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
279                                           .type            = AVMEDIA_TYPE_VIDEO,
280                                           .request_frame   = request_frame, },
281                                         { .name = NULL}},
282 };
283
284 AVFilter avfilter_af_afifo = {
285     .name        = "afifo",
286     .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
287
288     .init      = init,
289     .uninit    = uninit,
290
291     .priv_size = sizeof(FifoContext),
292
293     .inputs    = (const AVFilterPad[]) {{ .name             = "default",
294                                           .type             = AVMEDIA_TYPE_AUDIO,
295                                           .get_audio_buffer = ff_null_get_audio_buffer,
296                                           .filter_samples   = add_to_queue,
297                                           .rej_perms        = AV_PERM_REUSE2, },
298                                         { .name = NULL}},
299     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
300                                           .type             = AVMEDIA_TYPE_AUDIO,
301                                           .request_frame    = request_frame, },
302                                         { .name = NULL}},
303 };