]> git.sesse.net Git - ffmpeg/blob - libavfilter/buffersink.c
avfilter/buffersink: remove unused macros
[ffmpeg] / libavfilter / buffersink.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
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  * buffer sink
24  */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/opt.h"
31
32 #define FF_INTERNAL_FIELDS 1
33 #include "framequeue.h"
34
35 #include "audio.h"
36 #include "avfilter.h"
37 #include "buffersink.h"
38 #include "filters.h"
39 #include "internal.h"
40
41 typedef struct BufferSinkContext {
42     const AVClass *class;
43     unsigned warning_limit;
44
45     /* only used for video */
46     enum AVPixelFormat *pixel_fmts;           ///< list of accepted pixel formats, must be terminated with -1
47     int pixel_fmts_size;
48
49     /* only used for audio */
50     enum AVSampleFormat *sample_fmts;       ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
51     int sample_fmts_size;
52     int64_t *channel_layouts;               ///< list of accepted channel layouts, terminated by -1
53     int channel_layouts_size;
54     int *channel_counts;                    ///< list of accepted channel counts, terminated by -1
55     int channel_counts_size;
56     int all_channel_counts;
57     int *sample_rates;                      ///< list of accepted sample rates, terminated by -1
58     int sample_rates_size;
59
60     AVFrame *peeked_frame;
61 } BufferSinkContext;
62
63 #define NB_ITEMS(list) (list ## _size / sizeof(*list))
64
65 int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
66 {
67     return av_buffersink_get_frame_flags(ctx, frame, 0);
68 }
69
70 static int return_or_keep_frame(BufferSinkContext *buf, AVFrame *out, AVFrame *in, int flags)
71 {
72     if ((flags & AV_BUFFERSINK_FLAG_PEEK)) {
73         buf->peeked_frame = in;
74         return out ? av_frame_ref(out, in) : 0;
75     } else {
76         av_assert1(out);
77         buf->peeked_frame = NULL;
78         av_frame_move_ref(out, in);
79         av_frame_free(&in);
80         return 0;
81     }
82 }
83
84 static int get_frame_internal(AVFilterContext *ctx, AVFrame *frame, int flags, int samples)
85 {
86     BufferSinkContext *buf = ctx->priv;
87     AVFilterLink *inlink = ctx->inputs[0];
88     int status, ret;
89     AVFrame *cur_frame;
90     int64_t pts;
91
92     if (buf->peeked_frame)
93         return return_or_keep_frame(buf, frame, buf->peeked_frame, flags);
94
95     while (1) {
96         ret = samples ? ff_inlink_consume_samples(inlink, samples, samples, &cur_frame) :
97                         ff_inlink_consume_frame(inlink, &cur_frame);
98         if (ret < 0) {
99             return ret;
100         } else if (ret) {
101             /* TODO return the frame instead of copying it */
102             return return_or_keep_frame(buf, frame, cur_frame, flags);
103         } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
104             return status;
105         } else if ((flags & AV_BUFFERSINK_FLAG_NO_REQUEST)) {
106             return AVERROR(EAGAIN);
107         } else if (inlink->frame_wanted_out) {
108             ret = ff_filter_graph_run_once(ctx->graph);
109             if (ret < 0)
110                 return ret;
111         } else {
112             ff_inlink_request_frame(inlink);
113         }
114     }
115 }
116
117 int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
118 {
119     return get_frame_internal(ctx, frame, flags, ctx->inputs[0]->min_samples);
120 }
121
122 int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
123                                                   AVFrame *frame, int nb_samples)
124 {
125     return get_frame_internal(ctx, frame, 0, nb_samples);
126 }
127
128 AVBufferSinkParams *av_buffersink_params_alloc(void)
129 {
130     static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
131     AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
132     if (!params)
133         return NULL;
134
135     params->pixel_fmts = pixel_fmts;
136     return params;
137 }
138
139 AVABufferSinkParams *av_abuffersink_params_alloc(void)
140 {
141     AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
142
143     if (!params)
144         return NULL;
145     return params;
146 }
147
148 static av_cold int common_init(AVFilterContext *ctx)
149 {
150     BufferSinkContext *buf = ctx->priv;
151
152     buf->warning_limit = 100;
153     return 0;
154 }
155
156 static int activate(AVFilterContext *ctx)
157 {
158     BufferSinkContext *buf = ctx->priv;
159
160     if (buf->warning_limit &&
161         ff_framequeue_queued_frames(&ctx->inputs[0]->fifo) >= buf->warning_limit) {
162         av_log(ctx, AV_LOG_WARNING,
163                "%d buffers queued in %s, something may be wrong.\n",
164                buf->warning_limit,
165                (char *)av_x_if_null(ctx->name, ctx->filter->name));
166         buf->warning_limit *= 10;
167     }
168
169     /* The frame is queued, the rest is up to get_frame_internal */
170     return 0;
171 }
172
173 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
174 {
175     AVFilterLink *inlink = ctx->inputs[0];
176
177     inlink->min_samples = inlink->max_samples =
178     inlink->partial_buf_size = frame_size;
179 }
180
181 #define MAKE_AVFILTERLINK_ACCESSOR(type, field) \
182 type av_buffersink_get_##field(const AVFilterContext *ctx) { \
183     av_assert0(ctx->filter->activate == activate); \
184     return ctx->inputs[0]->field; \
185 }
186
187 MAKE_AVFILTERLINK_ACCESSOR(enum AVMediaType , type               )
188 MAKE_AVFILTERLINK_ACCESSOR(AVRational       , time_base          )
189 MAKE_AVFILTERLINK_ACCESSOR(int              , format             )
190
191 MAKE_AVFILTERLINK_ACCESSOR(AVRational       , frame_rate         )
192 MAKE_AVFILTERLINK_ACCESSOR(int              , w                  )
193 MAKE_AVFILTERLINK_ACCESSOR(int              , h                  )
194 MAKE_AVFILTERLINK_ACCESSOR(AVRational       , sample_aspect_ratio)
195
196 MAKE_AVFILTERLINK_ACCESSOR(int              , channels           )
197 MAKE_AVFILTERLINK_ACCESSOR(uint64_t         , channel_layout     )
198 MAKE_AVFILTERLINK_ACCESSOR(int              , sample_rate        )
199
200 MAKE_AVFILTERLINK_ACCESSOR(AVBufferRef *    , hw_frames_ctx      )
201
202 static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
203 {
204     BufferSinkContext *buf = ctx->priv;
205     AVBufferSinkParams *params = opaque;
206     int ret;
207
208     if (params) {
209         if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
210             return ret;
211     }
212
213     return common_init(ctx);
214 }
215
216 #define CHECK_LIST_SIZE(field) \
217         if (buf->field ## _size % sizeof(*buf->field)) { \
218             av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
219                    "should be multiple of %d\n", \
220                    buf->field ## _size, (int)sizeof(*buf->field)); \
221             return AVERROR(EINVAL); \
222         }
223 static int vsink_query_formats(AVFilterContext *ctx)
224 {
225     BufferSinkContext *buf = ctx->priv;
226     AVFilterFormats *formats = NULL;
227     unsigned i;
228     int ret;
229
230     CHECK_LIST_SIZE(pixel_fmts)
231     if (buf->pixel_fmts_size) {
232         for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
233             if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0)
234                 return ret;
235         if ((ret = ff_set_common_formats(ctx, formats)) < 0)
236             return ret;
237     } else {
238         if ((ret = ff_default_query_formats(ctx)) < 0)
239             return ret;
240     }
241
242     return 0;
243 }
244
245 static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
246 {
247     BufferSinkContext *buf = ctx->priv;
248     AVABufferSinkParams *params = opaque;
249     int ret;
250
251     if (params) {
252         if ((ret = av_opt_set_int_list(buf, "sample_fmts",     params->sample_fmts,  AV_SAMPLE_FMT_NONE, 0)) < 0 ||
253             (ret = av_opt_set_int_list(buf, "sample_rates",    params->sample_rates,    -1, 0)) < 0 ||
254             (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
255             (ret = av_opt_set_int_list(buf, "channel_counts",  params->channel_counts,  -1, 0)) < 0 ||
256             (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
257             return ret;
258     }
259     return common_init(ctx);
260 }
261
262 static int asink_query_formats(AVFilterContext *ctx)
263 {
264     BufferSinkContext *buf = ctx->priv;
265     AVFilterFormats *formats = NULL;
266     AVFilterChannelLayouts *layouts = NULL;
267     unsigned i;
268     int ret;
269
270     CHECK_LIST_SIZE(sample_fmts)
271     CHECK_LIST_SIZE(sample_rates)
272     CHECK_LIST_SIZE(channel_layouts)
273     CHECK_LIST_SIZE(channel_counts)
274
275     if (buf->sample_fmts_size) {
276         for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
277             if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
278                 return ret;
279         if ((ret = ff_set_common_formats(ctx, formats)) < 0)
280             return ret;
281     }
282
283     if (buf->channel_layouts_size || buf->channel_counts_size ||
284         buf->all_channel_counts) {
285         for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
286             if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0)
287                 return ret;
288         for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
289             if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0)
290                 return ret;
291         if (buf->all_channel_counts) {
292             if (layouts)
293                 av_log(ctx, AV_LOG_WARNING,
294                        "Conflicting all_channel_counts and list in options\n");
295             else if (!(layouts = ff_all_channel_counts()))
296                 return AVERROR(ENOMEM);
297         }
298         if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
299             return ret;
300     }
301
302     if (buf->sample_rates_size) {
303         formats = NULL;
304         for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
305             if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)
306                 return ret;
307         if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
308             return ret;
309     }
310
311     return 0;
312 }
313
314 #define OFFSET(x) offsetof(BufferSinkContext, x)
315 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
316 static const AVOption buffersink_options[] = {
317     { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
318     { NULL },
319 };
320 #undef FLAGS
321 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
322 static const AVOption abuffersink_options[] = {
323     { "sample_fmts",     "set the supported sample formats",  OFFSET(sample_fmts),     AV_OPT_TYPE_BINARY, .flags = FLAGS },
324     { "sample_rates",    "set the supported sample rates",    OFFSET(sample_rates),    AV_OPT_TYPE_BINARY, .flags = FLAGS },
325     { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
326     { "channel_counts",  "set the supported channel counts",  OFFSET(channel_counts),  AV_OPT_TYPE_BINARY, .flags = FLAGS },
327     { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
328     { NULL },
329 };
330 #undef FLAGS
331
332 AVFILTER_DEFINE_CLASS(buffersink);
333 AVFILTER_DEFINE_CLASS(abuffersink);
334
335 static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
336     {
337         .name = "default",
338         .type = AVMEDIA_TYPE_VIDEO,
339     },
340     { NULL }
341 };
342
343 AVFilter ff_vsink_buffer = {
344     .name          = "buffersink",
345     .description   = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
346     .priv_size     = sizeof(BufferSinkContext),
347     .priv_class    = &buffersink_class,
348     .init_opaque   = vsink_init,
349     .query_formats = vsink_query_formats,
350     .activate      = activate,
351     .inputs        = avfilter_vsink_buffer_inputs,
352     .outputs       = NULL,
353 };
354
355 static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
356     {
357         .name = "default",
358         .type = AVMEDIA_TYPE_AUDIO,
359     },
360     { NULL }
361 };
362
363 AVFilter ff_asink_abuffer = {
364     .name          = "abuffersink",
365     .description   = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
366     .priv_class    = &abuffersink_class,
367     .priv_size     = sizeof(BufferSinkContext),
368     .init_opaque   = asink_init,
369     .query_formats = asink_query_formats,
370     .activate      = activate,
371     .inputs        = avfilter_asink_abuffer_inputs,
372     .outputs       = NULL,
373 };