GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/buffersink.c Lines: 89 123 72.4 %
Date: 2020-08-13 15:06:06 Branches: 61 108 56.5 %

Line Branch Exec Source
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
466883
static int return_or_keep_frame(BufferSinkContext *buf, AVFrame *out, AVFrame *in, int flags)
71
{
72
466883
    if ((flags & AV_BUFFERSINK_FLAG_PEEK)) {
73
11361
        buf->peeked_frame = in;
74
11361
        return out ? av_frame_ref(out, in) : 0;
75
    } else {
76
        av_assert1(out);
77
455522
        buf->peeked_frame = NULL;
78
455522
        av_frame_move_ref(out, in);
79
455522
        av_frame_free(&in);
80
455522
        return 0;
81
    }
82
}
83
84
1298302
static int get_frame_internal(AVFilterContext *ctx, AVFrame *frame, int flags, int samples)
85
{
86
1298302
    BufferSinkContext *buf = ctx->priv;
87
1298302
    AVFilterLink *inlink = ctx->inputs[0];
88
    int status, ret;
89
    AVFrame *cur_frame;
90
    int64_t pts;
91
92
1298302
    if (buf->peeked_frame)
93
11361
        return return_or_keep_frame(buf, frame, buf->peeked_frame, flags);
94
95
    while (1) {
96
2798478
        ret = samples ? ff_inlink_consume_samples(inlink, samples, samples, &cur_frame) :
97
2497795
                        ff_inlink_consume_frame(inlink, &cur_frame);
98
2798478
        if (ret < 0) {
99
            return ret;
100
2798478
        } else if (ret) {
101
            /* TODO return the frame instead of copying it */
102
455522
            return return_or_keep_frame(buf, frame, cur_frame, flags);
103
2342956
        } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
104
7434
            return status;
105
2335522
        } else if ((flags & AV_BUFFERSINK_FLAG_NO_REQUEST)) {
106
417550
            return AVERROR(EAGAIN);
107
1917972
        } else if (inlink->frame_wanted_out) {
108
1515599
            ret = ff_filter_graph_run_once(ctx->graph);
109
1515599
            if (ret < 0)
110
406435
                return ret;
111
        } else {
112
402373
            ff_inlink_request_frame(inlink);
113
        }
114
    }
115
}
116
117
1298302
int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
118
{
119
1298302
    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
#if FF_API_NEXT
129
AVBufferSinkParams *av_buffersink_params_alloc(void)
130
{
131
    static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
132
    AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
133
    if (!params)
134
        return NULL;
135
136
    params->pixel_fmts = pixel_fmts;
137
    return params;
138
}
139
140
AVABufferSinkParams *av_abuffersink_params_alloc(void)
141
{
142
    AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
143
144
    if (!params)
145
        return NULL;
146
    return params;
147
}
148
#endif
149
150
5849
static av_cold int common_init(AVFilterContext *ctx)
151
{
152
5849
    BufferSinkContext *buf = ctx->priv;
153
154
5849
    buf->warning_limit = 100;
155
5849
    return 0;
156
}
157
158
440637
static int activate(AVFilterContext *ctx)
159
{
160
440637
    BufferSinkContext *buf = ctx->priv;
161
162
440637
    if (buf->warning_limit &&
163
440637
        ff_framequeue_queued_frames(&ctx->inputs[0]->fifo) >= buf->warning_limit) {
164
5
        av_log(ctx, AV_LOG_WARNING,
165
               "%d buffers queued in %s, something may be wrong.\n",
166
               buf->warning_limit,
167
5
               (char *)av_x_if_null(ctx->name, ctx->filter->name));
168
5
        buf->warning_limit *= 10;
169
    }
170
171
    /* The frame is queued, the rest is up to get_frame_internal */
172
440637
    return 0;
173
}
174
175
198
void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
176
{
177
198
    AVFilterLink *inlink = ctx->inputs[0];
178
179
198
    inlink->min_samples = inlink->max_samples =
180
198
    inlink->partial_buf_size = frame_size;
181
198
}
182
183
#define MAKE_AVFILTERLINK_ACCESSOR(type, field) \
184
type av_buffersink_get_##field(const AVFilterContext *ctx) { \
185
    av_assert0(ctx->filter->activate == activate); \
186
    return ctx->inputs[0]->field; \
187
}
188
189
449343
MAKE_AVFILTERLINK_ACCESSOR(enum AVMediaType , type               )
190
455621
MAKE_AVFILTERLINK_ACCESSOR(AVRational       , time_base          )
191
11675
MAKE_AVFILTERLINK_ACCESSOR(int              , format             )
192
193
106441
MAKE_AVFILTERLINK_ACCESSOR(AVRational       , frame_rate         )
194
10525
MAKE_AVFILTERLINK_ACCESSOR(int              , w                  )
195
10525
MAKE_AVFILTERLINK_ACCESSOR(int              , h                  )
196
4678
MAKE_AVFILTERLINK_ACCESSOR(AVRational       , sample_aspect_ratio)
197
198
1134
MAKE_AVFILTERLINK_ACCESSOR(int              , channels           )
199
6950
MAKE_AVFILTERLINK_ACCESSOR(uint64_t         , channel_layout     )
200
6950
MAKE_AVFILTERLINK_ACCESSOR(int              , sample_rate        )
201
202
5779
MAKE_AVFILTERLINK_ACCESSOR(AVBufferRef *    , hw_frames_ctx      )
203
204
#define CHECK_LIST_SIZE(field) \
205
        if (buf->field ## _size % sizeof(*buf->field)) { \
206
            av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
207
                   "should be multiple of %d\n", \
208
                   buf->field ## _size, (int)sizeof(*buf->field)); \
209
            return AVERROR(EINVAL); \
210
        }
211
4714
static int vsink_query_formats(AVFilterContext *ctx)
212
{
213
4714
    BufferSinkContext *buf = ctx->priv;
214
4714
    AVFilterFormats *formats = NULL;
215
    unsigned i;
216
    int ret;
217
218
4714
    CHECK_LIST_SIZE(pixel_fmts)
219
4714
    if (buf->pixel_fmts_size) {
220
3111
        for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
221
3094
            if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0)
222
                return ret;
223
17
        if ((ret = ff_set_common_formats(ctx, formats)) < 0)
224
            return ret;
225
    } else {
226
4697
        if ((ret = ff_default_query_formats(ctx)) < 0)
227
            return ret;
228
    }
229
230
4714
    return 0;
231
}
232
233
1135
static int asink_query_formats(AVFilterContext *ctx)
234
{
235
1135
    BufferSinkContext *buf = ctx->priv;
236
1135
    AVFilterFormats *formats = NULL;
237
1135
    AVFilterChannelLayouts *layouts = NULL;
238
    unsigned i;
239
    int ret;
240
241
1135
    CHECK_LIST_SIZE(sample_fmts)
242
1135
    CHECK_LIST_SIZE(sample_rates)
243
1135
    CHECK_LIST_SIZE(channel_layouts)
244
1135
    CHECK_LIST_SIZE(channel_counts)
245
246
1135
    if (buf->sample_fmts_size) {
247
96
        for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
248
80
            if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
249
                return ret;
250
16
        if ((ret = ff_set_common_formats(ctx, formats)) < 0)
251
            return ret;
252
    }
253
254

1135
    if (buf->channel_layouts_size || buf->channel_counts_size ||
255
1135
        buf->all_channel_counts) {
256
1135
        for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
257
            if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0)
258
                return ret;
259
1135
        for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
260
            if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0)
261
                return ret;
262
1135
        if (buf->all_channel_counts) {
263
1135
            if (layouts)
264
                av_log(ctx, AV_LOG_WARNING,
265
                       "Conflicting all_channel_counts and list in options\n");
266
1135
            else if (!(layouts = ff_all_channel_counts()))
267
                return AVERROR(ENOMEM);
268
        }
269
1135
        if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
270
            return ret;
271
    }
272
273
1135
    if (buf->sample_rates_size) {
274
        formats = NULL;
275
        for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
276
            if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)
277
                return ret;
278
        if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
279
            return ret;
280
    }
281
282
1135
    return 0;
283
}
284
285
#define OFFSET(x) offsetof(BufferSinkContext, x)
286
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
287
static const AVOption buffersink_options[] = {
288
    { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
289
    { NULL },
290
};
291
#undef FLAGS
292
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
293
static const AVOption abuffersink_options[] = {
294
    { "sample_fmts",     "set the supported sample formats",  OFFSET(sample_fmts),     AV_OPT_TYPE_BINARY, .flags = FLAGS },
295
    { "sample_rates",    "set the supported sample rates",    OFFSET(sample_rates),    AV_OPT_TYPE_BINARY, .flags = FLAGS },
296
    { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
297
    { "channel_counts",  "set the supported channel counts",  OFFSET(channel_counts),  AV_OPT_TYPE_BINARY, .flags = FLAGS },
298
    { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
299
    { NULL },
300
};
301
#undef FLAGS
302
303
AVFILTER_DEFINE_CLASS(buffersink);
304
AVFILTER_DEFINE_CLASS(abuffersink);
305
306
static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
307
    {
308
        .name = "default",
309
        .type = AVMEDIA_TYPE_VIDEO,
310
    },
311
    { NULL }
312
};
313
314
AVFilter ff_vsink_buffer = {
315
    .name          = "buffersink",
316
    .description   = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
317
    .priv_size     = sizeof(BufferSinkContext),
318
    .priv_class    = &buffersink_class,
319
    .init          = common_init,
320
    .query_formats = vsink_query_formats,
321
    .activate      = activate,
322
    .inputs        = avfilter_vsink_buffer_inputs,
323
    .outputs       = NULL,
324
};
325
326
static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
327
    {
328
        .name = "default",
329
        .type = AVMEDIA_TYPE_AUDIO,
330
    },
331
    { NULL }
332
};
333
334
AVFilter ff_asink_abuffer = {
335
    .name          = "abuffersink",
336
    .description   = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
337
    .priv_class    = &abuffersink_class,
338
    .priv_size     = sizeof(BufferSinkContext),
339
    .init          = common_init,
340
    .query_formats = asink_query_formats,
341
    .activate      = activate,
342
    .inputs        = avfilter_asink_abuffer_inputs,
343
    .outputs       = NULL,
344
};