7873c0aebd3fafb0c7e27e201b0fb41f03a87594
[ffmpeg.git] / libavfilter / src_buffer.c
1 /*
2  * Copyright (c) 2008 Vitor Sessak
3  * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
4  * Copyright (c) 2011 Mina Nagy Zaki
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * memory buffer source filter
26  */
27
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "avcodec.h"
31 #include "buffersrc.h"
32 #include "vsrc_buffer.h"
33 #include "asrc_abuffer.h"
34 #include "libavutil/audioconvert.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/fifo.h"
37 #include "libavutil/imgutils.h"
38
39 typedef struct {
40     AVFifoBuffer     *fifo;
41     AVRational        time_base;     ///< time_base to set in the output link
42     int eof;
43     unsigned          nb_failed_requests;
44
45     /* Video only */
46     AVFilterContext  *scale;
47     int               h, w;
48     enum PixelFormat  pix_fmt;
49     AVRational        sample_aspect_ratio;
50     char              sws_param[256];
51
52     /* Audio only */
53     // Audio format of incoming buffers
54     int sample_rate;
55     unsigned int sample_format;
56     int64_t channel_layout;
57     int packing_format;
58
59     // Normalization filters
60     AVFilterContext *aconvert;
61     AVFilterContext *aresample;
62 } BufferSourceContext;
63
64 #define FIFO_SIZE 8
65
66 #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
67     if (c->w != width || c->h != height || c->pix_fmt != format) {\
68         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
69         return AVERROR(EINVAL);\
70     }
71
72 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
73                                         AVFilterBufferRef *picref, int flags)
74 {
75     BufferSourceContext *c = buffer_filter->priv;
76     AVFilterLink *outlink = buffer_filter->outputs[0];
77     AVFilterBufferRef *buf;
78     int ret;
79
80     if (!picref) {
81         c->eof = 1;
82         return 0;
83     } else if (c->eof)
84         return AVERROR(EINVAL);
85
86     if (!av_fifo_space(c->fifo) &&
87         (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
88                                          sizeof(buf))) < 0)
89         return ret;
90
91     if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
92         AVFilterContext *scale = buffer_filter->outputs[0]->dst;
93         AVFilterLink *link;
94         char scale_param[1024];
95
96         av_log(buffer_filter, AV_LOG_INFO,
97                "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
98                c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
99                picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
100
101         if (!scale || strcmp(scale->filter->name, "scale")) {
102             AVFilter *f = avfilter_get_by_name("scale");
103
104             av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
105             if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
106                 return ret;
107
108             c->scale = scale;
109
110             snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
111             if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
112                 return ret;
113             }
114
115             if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
116                 return ret;
117             }
118             scale->outputs[0]->time_base = scale->inputs[0]->time_base;
119
120             scale->outputs[0]->format= c->pix_fmt;
121         } else if (!strcmp(scale->filter->name, "scale")) {
122             snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
123                      scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
124             scale->filter->init(scale, scale_param, NULL);
125         }
126
127         c->pix_fmt = scale->inputs[0]->format = picref->format;
128         c->w       = scale->inputs[0]->w      = picref->video->w;
129         c->h       = scale->inputs[0]->h      = picref->video->h;
130
131         link = scale->outputs[0];
132         if ((ret =  link->srcpad->config_props(link)) < 0)
133             return ret;
134     }
135
136     buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
137                                     picref->video->w, picref->video->h);
138     av_image_copy(buf->data, buf->linesize,
139                   (void*)picref->data, picref->linesize,
140                   picref->format, picref->video->w, picref->video->h);
141     avfilter_copy_buffer_ref_props(buf, picref);
142
143     if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
144         avfilter_unref_buffer(buf);
145         return ret;
146     }
147     c->nb_failed_requests = 0;
148
149     return 0;
150 }
151
152 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
153 {
154     BufferSourceContext *c = s->priv;
155     int ret;
156
157     if (!buf) {
158         c->eof = 1;
159         return 0;
160     } else if (c->eof)
161         return AVERROR(EINVAL);
162
163     if (!av_fifo_space(c->fifo) &&
164         (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
165                                          sizeof(buf))) < 0)
166         return ret;
167
168 //     CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
169
170     if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0)
171         return ret;
172     c->nb_failed_requests = 0;
173
174     return 0;
175 }
176
177 #if CONFIG_AVCODEC
178 #include "avcodec.h"
179
180 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
181                              const AVFrame *frame, int flags)
182 {
183     BufferSourceContext *c = buffer_src->priv;
184     AVFilterBufferRef *picref;
185     int ret;
186
187     if (!frame) {
188         c->eof = 1;
189         return 0;
190     } else if (c->eof)
191         return AVERROR(EINVAL);
192
193     picref = avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
194     if (!picref)
195         return AVERROR(ENOMEM);
196     ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
197     picref->buf->data[0] = NULL;
198     avfilter_unref_buffer(picref);
199
200     return ret;
201 }
202 #endif
203
204 unsigned av_vsrc_buffer_get_nb_failed_requests(AVFilterContext *buffer_src)
205 {
206     return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
207 }
208
209 static av_cold int init_video(AVFilterContext *ctx, const char *args, void *opaque)
210 {
211     BufferSourceContext *c = ctx->priv;
212     char pix_fmt_str[128];
213     int ret, n = 0;
214     *c->sws_param = 0;
215
216     if (!args ||
217         (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
218                     &c->time_base.num, &c->time_base.den,
219                     &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
220         av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
221         return AVERROR(EINVAL);
222     }
223
224     if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
225         return ret;
226
227     if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
228         return AVERROR(ENOMEM);
229
230     av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
231            c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
232            c->time_base.num, c->time_base.den,
233            c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
234     return 0;
235 }
236
237 static av_cold int init_audio(AVFilterContext *ctx, const char *args0, void *opaque)
238 {
239     BufferSourceContext *abuffer = ctx->priv;
240     char *arg = NULL, *ptr, chlayout_str[16];
241     char *args = av_strdup(args0);
242     int ret;
243
244     arg = av_strtok(args, ":", &ptr);
245
246 #define ADD_FORMAT(fmt_name)                                            \
247     if (!arg)                                                           \
248         goto arg_fail;                                                  \
249     if ((ret = ff_parse_##fmt_name(&abuffer->fmt_name, arg, ctx)) < 0) { \
250         av_freep(&args);                                                \
251         return ret;                                                     \
252     }                                                                   \
253     if (*args)                                                          \
254         arg = av_strtok(NULL, ":", &ptr)
255
256     ADD_FORMAT(sample_rate);
257     ADD_FORMAT(sample_format);
258     ADD_FORMAT(channel_layout);
259     ADD_FORMAT(packing_format);
260
261     abuffer->fifo = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
262     if (!abuffer->fifo) {
263         av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo, filter init failed.\n");
264         return AVERROR(ENOMEM);
265     }
266
267     av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str),
268                                  -1, abuffer->channel_layout);
269     av_log(ctx, AV_LOG_INFO, "format:%s layout:%s rate:%d\n",
270            av_get_sample_fmt_name(abuffer->sample_format), chlayout_str,
271            abuffer->sample_rate);
272     av_freep(&args);
273
274     return 0;
275
276 arg_fail:
277     av_log(ctx, AV_LOG_ERROR, "Invalid arguments, must be of the form "
278                               "sample_rate:sample_fmt:channel_layout:packing\n");
279     av_freep(&args);
280     return AVERROR(EINVAL);
281 }
282
283 static av_cold void uninit(AVFilterContext *ctx)
284 {
285     BufferSourceContext *s = ctx->priv;
286     while (s->fifo && av_fifo_size(s->fifo)) {
287         AVFilterBufferRef *buf;
288         av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
289         avfilter_unref_buffer(buf);
290     }
291     av_fifo_free(s->fifo);
292     s->fifo = NULL;
293     avfilter_free(s->scale);
294     s->scale = NULL;
295 }
296
297 static int query_formats_video(AVFilterContext *ctx)
298 {
299     BufferSourceContext *c = ctx->priv;
300     enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
301
302     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
303     return 0;
304 }
305
306 static int query_formats_audio(AVFilterContext *ctx)
307 {
308     BufferSourceContext *abuffer = ctx->priv;
309     AVFilterFormats *formats;
310
311     formats = NULL;
312     avfilter_add_format(&formats, abuffer->sample_format);
313     avfilter_set_common_sample_formats(ctx, formats);
314
315     formats = NULL;
316     avfilter_add_format(&formats, abuffer->channel_layout);
317     avfilter_set_common_channel_layouts(ctx, formats);
318
319     formats = NULL;
320     avfilter_add_format(&formats, abuffer->packing_format);
321     avfilter_set_common_packing_formats(ctx, formats);
322
323     return 0;
324 }
325
326 static int config_output_video(AVFilterLink *link)
327 {
328     BufferSourceContext *c = link->src->priv;
329
330     link->w = c->w;
331     link->h = c->h;
332     link->sample_aspect_ratio = c->sample_aspect_ratio;
333     link->time_base = c->time_base;
334
335     return 0;
336 }
337
338 static int config_output_audio(AVFilterLink *outlink)
339 {
340     BufferSourceContext *abuffer = outlink->src->priv;
341     outlink->sample_rate = abuffer->sample_rate;
342     return 0;
343 }
344
345 static int request_frame(AVFilterLink *link)
346 {
347     BufferSourceContext *c = link->src->priv;
348     AVFilterBufferRef *buf;
349
350     if (!av_fifo_size(c->fifo)) {
351         if (c->eof)
352             return AVERROR_EOF;
353         c->nb_failed_requests++;
354         return AVERROR(EAGAIN);
355     }
356     av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
357
358     switch (link->type) {
359     case AVMEDIA_TYPE_VIDEO:
360         /* TODO reindent */
361     avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0));
362     avfilter_draw_slice(link, 0, link->h, 1);
363     avfilter_end_frame(link);
364     avfilter_unref_buffer(buf);
365         break;
366     case AVMEDIA_TYPE_AUDIO:
367         avfilter_filter_samples(link, avfilter_ref_buffer(buf, ~0));
368         avfilter_unref_buffer(buf);
369         break;
370     default:
371         return AVERROR(ENOSYS);
372     }
373     return 0;
374 }
375
376 static int poll_frame(AVFilterLink *link)
377 {
378     BufferSourceContext *c = link->src->priv;
379     int size = av_fifo_size(c->fifo);
380     if (!size && c->eof)
381         return AVERROR_EOF;
382     return size/sizeof(AVFilterBufferRef*);
383 }
384
385 static void buf_free(AVFilterBuffer *ptr)
386 {
387     av_free(ptr);
388     return;
389 }
390
391 static void set_link_source(AVFilterContext *src, AVFilterLink *link)
392 {
393     link->src       = src;
394     link->srcpad    = &(src->output_pads[0]);
395     src->outputs[0] = link;
396 }
397
398 static int reconfigure_filter(BufferSourceContext *abuffer, AVFilterContext *filt_ctx)
399 {
400     int ret;
401     AVFilterLink * const inlink  = filt_ctx->inputs[0];
402     AVFilterLink * const outlink = filt_ctx->outputs[0];
403
404     inlink->format         = abuffer->sample_format;
405     inlink->channel_layout = abuffer->channel_layout;
406     inlink->planar         = abuffer->packing_format;
407     inlink->sample_rate    = abuffer->sample_rate;
408
409     filt_ctx->filter->uninit(filt_ctx);
410     memset(filt_ctx->priv, 0, filt_ctx->filter->priv_size);
411     if ((ret = filt_ctx->filter->init(filt_ctx, NULL , NULL)) < 0)
412         return ret;
413     if ((ret = inlink->srcpad->config_props(inlink)) < 0)
414         return ret;
415     return outlink->srcpad->config_props(outlink);
416 }
417
418 static int insert_filter(BufferSourceContext *abuffer,
419                          AVFilterLink *link, AVFilterContext **filt_ctx,
420                          const char *filt_name)
421 {
422     int ret;
423
424     if ((ret = avfilter_open(filt_ctx, avfilter_get_by_name(filt_name), NULL)) < 0)
425         return ret;
426
427     link->src->outputs[0] = NULL;
428     if ((ret = avfilter_link(link->src, 0, *filt_ctx, 0)) < 0) {
429         link->src->outputs[0] = link;
430         return ret;
431     }
432
433     set_link_source(*filt_ctx, link);
434
435     if ((ret = reconfigure_filter(abuffer, *filt_ctx)) < 0) {
436         avfilter_free(*filt_ctx);
437         return ret;
438     }
439
440     return 0;
441 }
442
443 static void remove_filter(AVFilterContext **filt_ctx)
444 {
445     AVFilterLink *outlink = (*filt_ctx)->outputs[0];
446     AVFilterContext *src  = (*filt_ctx)->inputs[0]->src;
447
448     (*filt_ctx)->outputs[0] = NULL;
449     avfilter_free(*filt_ctx);
450     *filt_ctx = NULL;
451
452     set_link_source(src, outlink);
453 }
454
455 static inline void log_input_change(void *ctx, AVFilterLink *link, AVFilterBufferRef *ref)
456 {
457     char old_layout_str[16], new_layout_str[16];
458     av_get_channel_layout_string(old_layout_str, sizeof(old_layout_str),
459                                  -1, link->channel_layout);
460     av_get_channel_layout_string(new_layout_str, sizeof(new_layout_str),
461                                  -1, ref->audio->channel_layout);
462     av_log(ctx, AV_LOG_INFO,
463            "Audio input format changed: "
464            "%s:%s:%d -> %s:%s:%d, normalizing\n",
465            av_get_sample_fmt_name(link->format),
466            old_layout_str, (int)link->sample_rate,
467            av_get_sample_fmt_name(ref->format),
468            new_layout_str, ref->audio->sample_rate);
469 }
470
471 int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
472                                         AVFilterBufferRef *samplesref,
473                                         int av_unused flags)
474 {
475     BufferSourceContext *abuffer = ctx->priv;
476     AVFilterLink *link;
477     int ret, logged = 0;
478
479     if (av_fifo_space(abuffer->fifo) < sizeof(samplesref)) {
480         av_log(ctx, AV_LOG_ERROR,
481                "Buffering limit reached. Please consume some available frames "
482                "before adding new ones.\n");
483         return AVERROR(EINVAL);
484     }
485
486     // Normalize input
487
488     link = ctx->outputs[0];
489     if (samplesref->audio->sample_rate != link->sample_rate) {
490
491         log_input_change(ctx, link, samplesref);
492         logged = 1;
493
494         abuffer->sample_rate = samplesref->audio->sample_rate;
495
496         if (!abuffer->aresample) {
497             ret = insert_filter(abuffer, link, &abuffer->aresample, "aresample");
498             if (ret < 0) return ret;
499         } else {
500             link = abuffer->aresample->outputs[0];
501             if (samplesref->audio->sample_rate == link->sample_rate)
502                 remove_filter(&abuffer->aresample);
503             else
504                 if ((ret = reconfigure_filter(abuffer, abuffer->aresample)) < 0)
505                     return ret;
506         }
507     }
508
509     link = ctx->outputs[0];
510     if (samplesref->format                != link->format         ||
511         samplesref->audio->channel_layout != link->channel_layout ||
512         samplesref->audio->planar         != link->planar) {
513
514         if (!logged) log_input_change(ctx, link, samplesref);
515
516         abuffer->sample_format  = samplesref->format;
517         abuffer->channel_layout = samplesref->audio->channel_layout;
518         abuffer->packing_format = samplesref->audio->planar;
519
520         if (!abuffer->aconvert) {
521             ret = insert_filter(abuffer, link, &abuffer->aconvert, "aconvert");
522             if (ret < 0) return ret;
523         } else {
524             link = abuffer->aconvert->outputs[0];
525             if (samplesref->format                == link->format         &&
526                 samplesref->audio->channel_layout == link->channel_layout &&
527                 samplesref->audio->planar         == link->planar
528                )
529                 remove_filter(&abuffer->aconvert);
530             else
531                 if ((ret = reconfigure_filter(abuffer, abuffer->aconvert)) < 0)
532                     return ret;
533         }
534     }
535
536     if (sizeof(samplesref) != av_fifo_generic_write(abuffer->fifo, &samplesref,
537                                                     sizeof(samplesref), NULL)) {
538         av_log(ctx, AV_LOG_ERROR, "Error while writing to FIFO\n");
539         return AVERROR(EINVAL);
540     }
541
542     return 0;
543 }
544
545 int av_asrc_buffer_add_samples(AVFilterContext *ctx,
546                                uint8_t *data[8], int linesize[8],
547                                int nb_samples, int sample_rate,
548                                int sample_fmt, int64_t channel_layout, int planar,
549                                int64_t pts, int av_unused flags)
550 {
551     AVFilterBufferRef *samplesref;
552
553     samplesref = avfilter_get_audio_buffer_ref_from_arrays(
554                      data, linesize, AV_PERM_WRITE,
555                      nb_samples,
556                      sample_fmt, channel_layout, planar);
557     if (!samplesref)
558         return AVERROR(ENOMEM);
559
560     samplesref->buf->free  = buf_free;
561     samplesref->pts = pts;
562     samplesref->audio->sample_rate = sample_rate;
563
564     return av_asrc_buffer_add_audio_buffer_ref(ctx, samplesref, 0);
565 }
566
567 int av_asrc_buffer_add_buffer(AVFilterContext *ctx,
568                               uint8_t *buf, int buf_size, int sample_rate,
569                               int sample_fmt, int64_t channel_layout, int planar,
570                               int64_t pts, int av_unused flags)
571 {
572     uint8_t *data[8] = {0};
573     int linesize[8];
574     int nb_channels = av_get_channel_layout_nb_channels(channel_layout),
575         nb_samples  = buf_size / nb_channels / av_get_bytes_per_sample(sample_fmt);
576
577     av_samples_fill_arrays(data, linesize,
578                            buf, nb_channels, nb_samples,
579                            sample_fmt, 16);
580
581     return av_asrc_buffer_add_samples(ctx,
582                                       data, linesize, nb_samples,
583                                       sample_rate,
584                                       sample_fmt, channel_layout, planar,
585                                       pts, flags);
586 }
587
588 AVFilter avfilter_vsrc_buffer = {
589     .name      = "buffer",
590     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
591     .priv_size = sizeof(BufferSourceContext),
592     .query_formats = query_formats_video,
593
594     .init      = init_video,
595     .uninit    = uninit,
596
597     .inputs    = (const AVFilterPad[]) {{ .name = NULL }},
598     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
599                                     .type            = AVMEDIA_TYPE_VIDEO,
600                                     .request_frame   = request_frame,
601                                     .poll_frame      = poll_frame,
602                                     .config_props    = config_output_video, },
603                                   { .name = NULL}},
604 };
605
606 #ifdef CONFIG_ABUFFER_FILTER
607
608 AVFilter avfilter_asrc_abuffer = {
609     .name        = "abuffer",
610     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
611     .priv_size   = sizeof(BufferSourceContext),
612     .query_formats = query_formats_audio,
613
614     .init        = init_audio,
615     .uninit      = uninit,
616
617     .inputs      = (const AVFilterPad[]) {{ .name = NULL }},
618     .outputs     = (const AVFilterPad[]) {{ .name      = "default",
619                                       .type            = AVMEDIA_TYPE_AUDIO,
620                                       .request_frame   = request_frame,
621                                       .poll_frame      = poll_frame,
622                                       .config_props    = config_output_audio, },
623                                     { .name = NULL}},
624 };
625
626 #endif