| 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 | * libavfilter virtual input device | ||
| 24 | */ | ||
| 25 | |||
| 26 | /* #define DEBUG */ | ||
| 27 | |||
| 28 | #include <float.h> /* DBL_MIN, DBL_MAX */ | ||
| 29 | |||
| 30 | #include "libavutil/bprint.h" | ||
| 31 | #include "libavutil/channel_layout.h" | ||
| 32 | #include "libavutil/file.h" | ||
| 33 | #include "libavutil/imgutils.h" | ||
| 34 | #include "libavutil/internal.h" | ||
| 35 | #include "libavutil/log.h" | ||
| 36 | #include "libavutil/mem.h" | ||
| 37 | #include "libavutil/opt.h" | ||
| 38 | #include "libavutil/parseutils.h" | ||
| 39 | #include "libavutil/pixdesc.h" | ||
| 40 | #include "libavfilter/avfilter.h" | ||
| 41 | #include "libavfilter/buffersink.h" | ||
| 42 | #include "libavformat/demux.h" | ||
| 43 | #include "libavformat/internal.h" | ||
| 44 | #include "avdevice.h" | ||
| 45 | |||
| 46 | typedef struct { | ||
| 47 | AVClass *class; ///< class for private options | ||
| 48 | char *graph_str; | ||
| 49 | char *graph_filename; | ||
| 50 | char *dump_graph; | ||
| 51 | AVFilterGraph *graph; | ||
| 52 | AVFilterContext **sinks; | ||
| 53 | int *sink_stream_map; | ||
| 54 | int *sink_eof; | ||
| 55 | int *stream_sink_map; | ||
| 56 | int *sink_stream_subcc_map; | ||
| 57 | int nb_sinks; | ||
| 58 | AVPacket subcc_packet; | ||
| 59 | } LavfiContext; | ||
| 60 | |||
| 61 | 59 | av_cold static int lavfi_read_close(AVFormatContext *avctx) | |
| 62 | { | ||
| 63 | 59 | LavfiContext *lavfi = avctx->priv_data; | |
| 64 | |||
| 65 | 59 | av_freep(&lavfi->sink_stream_map); | |
| 66 | 59 | av_freep(&lavfi->sink_eof); | |
| 67 | 59 | av_freep(&lavfi->stream_sink_map); | |
| 68 | 59 | av_freep(&lavfi->sink_stream_subcc_map); | |
| 69 | 59 | av_freep(&lavfi->sinks); | |
| 70 | 59 | avfilter_graph_free(&lavfi->graph); | |
| 71 | |||
| 72 | 59 | return 0; | |
| 73 | } | ||
| 74 | |||
| 75 | 59 | static int create_subcc_streams(AVFormatContext *avctx) | |
| 76 | { | ||
| 77 | 59 | LavfiContext *lavfi = avctx->priv_data; | |
| 78 | AVStream *st; | ||
| 79 | int stream_idx, sink_idx; | ||
| 80 | AVRational *time_base; | ||
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 59 times.
|
121 | for (stream_idx = 0; stream_idx < lavfi->nb_sinks; stream_idx++) { |
| 83 | 62 | sink_idx = lavfi->stream_sink_map[stream_idx]; | |
| 84 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 56 times.
|
62 | if (lavfi->sink_stream_subcc_map[sink_idx]) { |
| 85 | 6 | lavfi->sink_stream_subcc_map[sink_idx] = avctx->nb_streams; | |
| 86 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (!(st = avformat_new_stream(avctx, NULL))) |
| 87 | ✗ | return AVERROR(ENOMEM); | |
| 88 | 6 | st->codecpar->codec_id = AV_CODEC_ID_EIA_608; | |
| 89 | 6 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
| 90 | 6 | time_base = &avctx->streams[stream_idx]->time_base; | |
| 91 | 6 | st->time_base.num = time_base->num; | |
| 92 | 6 | st->time_base.den = time_base->den; | |
| 93 | } else { | ||
| 94 | 56 | lavfi->sink_stream_subcc_map[sink_idx] = -1; | |
| 95 | } | ||
| 96 | } | ||
| 97 | 59 | return 0; | |
| 98 | } | ||
| 99 | |||
| 100 | 59 | av_cold static int lavfi_read_header(AVFormatContext *avctx) | |
| 101 | { | ||
| 102 | 59 | LavfiContext *lavfi = avctx->priv_data; | |
| 103 | 59 | AVFilterInOut *input_links = NULL, *output_links = NULL, *inout; | |
| 104 | const AVFilter *buffersink, *abuffersink; | ||
| 105 | enum AVMediaType type; | ||
| 106 | 59 | int ret = 0, i, n; | |
| 107 | |||
| 108 | #define FAIL(ERR) { ret = ERR; goto end; } | ||
| 109 | |||
| 110 | 59 | buffersink = avfilter_get_by_name("buffersink"); | |
| 111 | 59 | abuffersink = avfilter_get_by_name("abuffersink"); | |
| 112 | |||
| 113 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
59 | if (lavfi->graph_filename && lavfi->graph_str) { |
| 114 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 115 | "Only one of the graph or graph_file options must be specified\n"); | ||
| 116 | ✗ | FAIL(AVERROR(EINVAL)); | |
| 117 | } | ||
| 118 | |||
| 119 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 58 times.
|
59 | if (lavfi->graph_filename) { |
| 120 | AVBPrint graph_file_pb; | ||
| 121 | 1 | AVIOContext *avio = NULL; | |
| 122 | 1 | AVDictionary *options = NULL; | |
| 123 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | if (avctx->protocol_whitelist && (ret = av_dict_set(&options, "protocol_whitelist", avctx->protocol_whitelist, 0)) < 0) |
| 124 | ✗ | goto end; | |
| 125 | 1 | ret = avio_open2(&avio, lavfi->graph_filename, AVIO_FLAG_READ, &avctx->interrupt_callback, &options); | |
| 126 | 1 | av_dict_free(&options); | |
| 127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 128 | ✗ | goto end; | |
| 129 | 1 | av_bprint_init(&graph_file_pb, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 130 | 1 | ret = avio_read_to_bprint(avio, &graph_file_pb, INT_MAX); | |
| 131 | 1 | avio_closep(&avio); | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret) { |
| 133 | ✗ | av_bprint_finalize(&graph_file_pb, NULL); | |
| 134 | ✗ | goto end; | |
| 135 | } | ||
| 136 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = av_bprint_finalize(&graph_file_pb, &lavfi->graph_str))) |
| 137 | ✗ | goto end; | |
| 138 | } | ||
| 139 | |||
| 140 |
2/2✓ Branch 0 taken 57 times.
✓ Branch 1 taken 2 times.
|
59 | if (!lavfi->graph_str) |
| 141 | 57 | lavfi->graph_str = av_strdup(avctx->url); | |
| 142 | |||
| 143 | /* parse the graph, create a stream for each open output */ | ||
| 144 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
|
59 | if (!(lavfi->graph = avfilter_graph_alloc())) |
| 145 | ✗ | FAIL(AVERROR(ENOMEM)); | |
| 146 | |||
| 147 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
|
59 | if ((ret = avfilter_graph_parse_ptr(lavfi->graph, lavfi->graph_str, |
| 148 | &input_links, &output_links, avctx)) < 0) | ||
| 149 | ✗ | goto end; | |
| 150 | |||
| 151 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
|
59 | if (input_links) { |
| 152 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 153 | "Open inputs in the filtergraph are not acceptable\n"); | ||
| 154 | ✗ | FAIL(AVERROR(EINVAL)); | |
| 155 | } | ||
| 156 | |||
| 157 | /* count the outputs */ | ||
| 158 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 59 times.
|
121 | for (n = 0, inout = output_links; inout; n++, inout = inout->next); |
| 159 | 59 | lavfi->nb_sinks = n; | |
| 160 | |||
| 161 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
|
59 | if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n))) |
| 162 | ✗ | FAIL(AVERROR(ENOMEM)); | |
| 163 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
|
59 | if (!(lavfi->sink_eof = av_mallocz(sizeof(int) * n))) |
| 164 | ✗ | FAIL(AVERROR(ENOMEM)); | |
| 165 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
|
59 | if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n))) |
| 166 | ✗ | FAIL(AVERROR(ENOMEM)); | |
| 167 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
|
59 | if (!(lavfi->sink_stream_subcc_map = av_malloc(sizeof(int) * n))) |
| 168 | ✗ | FAIL(AVERROR(ENOMEM)); | |
| 169 | |||
| 170 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 59 times.
|
121 | for (i = 0; i < n; i++) |
| 171 | 62 | lavfi->stream_sink_map[i] = -1; | |
| 172 | |||
| 173 | /* parse the output link names - they need to be of the form out0, out1, ... | ||
| 174 | * create a mapping between them and the streams */ | ||
| 175 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 59 times.
|
121 | for (i = 0, inout = output_links; inout; i++, inout = inout->next) { |
| 176 | 62 | int stream_idx = 0, suffix = 0, use_subcc = 0; | |
| 177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (!inout->name) { |
| 178 | ✗ | av_log(avctx, AV_LOG_ERROR, "Missing %d outpad name\n", i); | |
| 179 | ✗ | FAIL(AVERROR(EINVAL)); | |
| 180 | } | ||
| 181 | 62 | sscanf(inout->name, "out%n%d%n", &suffix, &stream_idx, &suffix); | |
| 182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (!suffix) { |
| 183 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 184 | "Invalid outpad name '%s'\n", inout->name); | ||
| 185 | ✗ | FAIL(AVERROR(EINVAL)); | |
| 186 | } | ||
| 187 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 56 times.
|
62 | if (inout->name[suffix]) { |
| 188 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (!strcmp(inout->name + suffix, "+subcc")) { |
| 189 | 6 | use_subcc = 1; | |
| 190 | } else { | ||
| 191 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 192 | "Invalid outpad suffix '%s'\n", inout->name); | ||
| 193 | ✗ | FAIL(AVERROR(EINVAL)); | |
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if ((unsigned)stream_idx >= n) { |
| 198 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 199 | "Invalid index was specified in output '%s', " | ||
| 200 | "must be a non-negative value < %d\n", | ||
| 201 | inout->name, n); | ||
| 202 | ✗ | FAIL(AVERROR(EINVAL)); | |
| 203 | } | ||
| 204 | |||
| 205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (lavfi->stream_sink_map[stream_idx] != -1) { |
| 206 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 207 | "An output with stream index %d was already specified\n", | ||
| 208 | stream_idx); | ||
| 209 | ✗ | FAIL(AVERROR(EINVAL)); | |
| 210 | } | ||
| 211 | 62 | lavfi->sink_stream_map[i] = stream_idx; | |
| 212 | 62 | lavfi->stream_sink_map[stream_idx] = i; | |
| 213 | 62 | lavfi->sink_stream_subcc_map[i] = !!use_subcc; | |
| 214 | } | ||
| 215 | |||
| 216 | /* for each open output create a corresponding stream */ | ||
| 217 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 59 times.
|
121 | for (i = 0, inout = output_links; inout; i++, inout = inout->next) { |
| 218 | AVStream *st; | ||
| 219 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
|
62 | if (!(st = avformat_new_stream(avctx, NULL))) |
| 220 | ✗ | FAIL(AVERROR(ENOMEM)); | |
| 221 | 62 | st->id = i; | |
| 222 | } | ||
| 223 | |||
| 224 | /* create a sink for each output and connect them to the graph */ | ||
| 225 | 59 | lavfi->sinks = av_malloc_array(lavfi->nb_sinks, sizeof(AVFilterContext *)); | |
| 226 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
|
59 | if (!lavfi->sinks) |
| 227 | ✗ | FAIL(AVERROR(ENOMEM)); | |
| 228 | |||
| 229 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 59 times.
|
121 | for (i = 0, inout = output_links; inout; i++, inout = inout->next) { |
| 230 | AVFilterContext *sink; | ||
| 231 | |||
| 232 | 62 | type = avfilter_pad_get_type(inout->filter_ctx->output_pads, inout->pad_idx); | |
| 233 | |||
| 234 |
5/6✓ Branch 0 taken 35 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 35 times.
|
62 | if (type == AVMEDIA_TYPE_VIDEO && ! buffersink || |
| 235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | type == AVMEDIA_TYPE_AUDIO && ! abuffersink) { |
| 236 | ✗ | av_log(avctx, AV_LOG_ERROR, "Missing required buffersink filter, aborting.\n"); | |
| 237 | ✗ | FAIL(AVERROR_FILTER_NOT_FOUND); | |
| 238 | } | ||
| 239 | |||
| 240 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 27 times.
|
62 | if (type == AVMEDIA_TYPE_VIDEO) { |
| 241 | 35 | ret = avfilter_graph_create_filter(&sink, buffersink, | |
| 242 | 35 | inout->name, NULL, | |
| 243 | NULL, lavfi->graph); | ||
| 244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
|
35 | if (ret < 0) |
| 245 | ✗ | goto end; | |
| 246 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | } else if (type == AVMEDIA_TYPE_AUDIO) { |
| 247 | static const enum AVSampleFormat sample_fmts[] = { | ||
| 248 | AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, | ||
| 249 | AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL, | ||
| 250 | }; | ||
| 251 | |||
| 252 | 27 | sink = avfilter_graph_alloc_filter(lavfi->graph, abuffersink, inout->name); | |
| 253 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (!sink) { |
| 254 | ✗ | ret = AVERROR(ENOMEM); | |
| 255 | ✗ | goto end; | |
| 256 | } | ||
| 257 | |||
| 258 | 27 | ret = av_opt_set_array(sink, "sample_formats", AV_OPT_SEARCH_CHILDREN, 0, | |
| 259 | FF_ARRAY_ELEMS(sample_fmts), AV_OPT_TYPE_SAMPLE_FMT, | ||
| 260 | sample_fmts); | ||
| 261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (ret < 0) |
| 262 | ✗ | goto end; | |
| 263 | |||
| 264 | 27 | ret = avfilter_init_dict(sink, NULL); | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (ret < 0) |
| 266 | ✗ | goto end; | |
| 267 | } else { | ||
| 268 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 269 | "Output '%s' is not a video or audio output, not yet supported\n", inout->name); | ||
| 270 | ✗ | FAIL(AVERROR(EINVAL)); | |
| 271 | } | ||
| 272 | |||
| 273 | 62 | lavfi->sinks[i] = sink; | |
| 274 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
|
62 | if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0) |
| 275 | ✗ | goto end; | |
| 276 | } | ||
| 277 | |||
| 278 | /* configure the graph */ | ||
| 279 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
|
59 | if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0) |
| 280 | ✗ | goto end; | |
| 281 | |||
| 282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
|
59 | if (lavfi->dump_graph) { |
| 283 | ✗ | char *dump = avfilter_graph_dump(lavfi->graph, lavfi->dump_graph); | |
| 284 | ✗ | if (dump != NULL) { | |
| 285 | ✗ | fputs(dump, stderr); | |
| 286 | ✗ | fflush(stderr); | |
| 287 | ✗ | av_free(dump); | |
| 288 | } else { | ||
| 289 | ✗ | FAIL(AVERROR(ENOMEM)); | |
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | /* fill each stream with the information in the corresponding sink */ | ||
| 294 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 59 times.
|
121 | for (i = 0; i < lavfi->nb_sinks; i++) { |
| 295 | 62 | AVFilterContext *sink = lavfi->sinks[lavfi->stream_sink_map[i]]; | |
| 296 | 62 | AVRational time_base = av_buffersink_get_time_base(sink); | |
| 297 | 62 | AVRational frame_rate = av_buffersink_get_frame_rate(sink); | |
| 298 | 62 | AVStream *st = avctx->streams[i]; | |
| 299 | 62 | AVCodecParameters *const par = st->codecpar; | |
| 300 | 62 | avpriv_set_pts_info(st, 64, time_base.num, time_base.den); | |
| 301 | 62 | par->codec_type = av_buffersink_get_type(sink); | |
| 302 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 27 times.
|
62 | if (par->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 303 | 35 | par->codec_id = AV_CODEC_ID_WRAPPED_AVFRAME; | |
| 304 | 35 | par->format = av_buffersink_get_format(sink); | |
| 305 | 35 | par->width = av_buffersink_get_w(sink); | |
| 306 | 35 | par->height = av_buffersink_get_h(sink); | |
| 307 | 35 | avctx->probesize = FFMAX(avctx->probesize, sizeof(AVFrame) * 30); | |
| 308 | 35 | st ->sample_aspect_ratio = | |
| 309 | 35 | par->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink); | |
| 310 |
2/4✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
|
35 | if (frame_rate.num > 0 && frame_rate.den > 0) { |
| 311 | 35 | st->avg_frame_rate = frame_rate; | |
| 312 | 35 | st->r_frame_rate = frame_rate; | |
| 313 | } | ||
| 314 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 315 | 27 | par->sample_rate = av_buffersink_get_sample_rate(sink); | |
| 316 | 27 | ret = av_buffersink_get_ch_layout(sink, &par->ch_layout); | |
| 317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (ret < 0) |
| 318 | ✗ | goto end; | |
| 319 | 27 | par->format = av_buffersink_get_format(sink); | |
| 320 | 27 | par->codec_id = av_get_pcm_codec(par->format, -1); | |
| 321 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (par->codec_id == AV_CODEC_ID_NONE) |
| 322 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 323 | "Could not find PCM codec for sample format %s.\n", | ||
| 324 | ✗ | av_get_sample_fmt_name(par->format)); | |
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 |
1/2✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
|
59 | if ((ret = create_subcc_streams(avctx)) < 0) |
| 329 | ✗ | goto end; | |
| 330 | |||
| 331 | 59 | end: | |
| 332 | 59 | avfilter_inout_free(&input_links); | |
| 333 | 59 | avfilter_inout_free(&output_links); | |
| 334 | 59 | return ret; | |
| 335 | } | ||
| 336 | |||
| 337 | 13565 | static int create_subcc_packet(AVFormatContext *avctx, AVFrame *frame, | |
| 338 | int sink_idx) | ||
| 339 | { | ||
| 340 | 13565 | LavfiContext *lavfi = avctx->priv_data; | |
| 341 | AVFrameSideData *sd; | ||
| 342 | int stream_idx, ret; | ||
| 343 | |||
| 344 |
2/2✓ Branch 0 taken 12621 times.
✓ Branch 1 taken 944 times.
|
13565 | if ((stream_idx = lavfi->sink_stream_subcc_map[sink_idx]) < 0) |
| 345 | 12621 | return 0; | |
| 346 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 944 times.
|
944 | if (!(sd = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC))) |
| 347 | ✗ | return 0; | |
| 348 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 944 times.
|
944 | if ((ret = av_new_packet(&lavfi->subcc_packet, sd->size)) < 0) |
| 349 | ✗ | return ret; | |
| 350 | 944 | memcpy(lavfi->subcc_packet.data, sd->data, sd->size); | |
| 351 | 944 | lavfi->subcc_packet.stream_index = stream_idx; | |
| 352 | 944 | lavfi->subcc_packet.pts = frame->pts; | |
| 353 | 944 | return 0; | |
| 354 | } | ||
| 355 | |||
| 356 | 3293 | static void lavfi_free_frame(void *opaque, uint8_t *data) | |
| 357 | { | ||
| 358 | 3293 | AVFrame *frame = (AVFrame*)data; | |
| 359 | 3293 | av_frame_free(&frame); | |
| 360 | 3293 | } | |
| 361 | |||
| 362 | 14564 | static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt) | |
| 363 | { | ||
| 364 | 14564 | LavfiContext *lavfi = avctx->priv_data; | |
| 365 | 14564 | double min_pts = DBL_MAX; | |
| 366 | 14564 | int stream_idx, min_pts_sink_idx = 0; | |
| 367 | AVFrame *frame, *frame_to_free; | ||
| 368 | AVDictionary *frame_metadata; | ||
| 369 | int ret, i; | ||
| 370 | AVStream *st; | ||
| 371 | |||
| 372 |
2/2✓ Branch 0 taken 944 times.
✓ Branch 1 taken 13620 times.
|
14564 | if (lavfi->subcc_packet.size) { |
| 373 | 944 | av_packet_move_ref(pkt, &lavfi->subcc_packet); | |
| 374 | 944 | return pkt->size; | |
| 375 | } | ||
| 376 | |||
| 377 | 13620 | frame = av_frame_alloc(); | |
| 378 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13620 times.
|
13620 | if (!frame) |
| 379 | ✗ | return AVERROR(ENOMEM); | |
| 380 | 13620 | frame_to_free = frame; | |
| 381 | |||
| 382 | /* iterate through all the graph sinks. Select the sink with the | ||
| 383 | * minimum PTS */ | ||
| 384 |
2/2✓ Branch 0 taken 13677 times.
✓ Branch 1 taken 13620 times.
|
27297 | for (i = 0; i < lavfi->nb_sinks; i++) { |
| 385 | 13677 | AVRational tb = av_buffersink_get_time_base(lavfi->sinks[i]); | |
| 386 | double d; | ||
| 387 | |||
| 388 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 13647 times.
|
13677 | if (lavfi->sink_eof[i]) |
| 389 | 87 | continue; | |
| 390 | |||
| 391 | 13647 | ret = av_buffersink_get_frame_flags(lavfi->sinks[i], frame, | |
| 392 | AV_BUFFERSINK_FLAG_PEEK); | ||
| 393 |
2/2✓ Branch 0 taken 57 times.
✓ Branch 1 taken 13590 times.
|
13647 | if (ret == AVERROR_EOF) { |
| 394 | ff_dlog(avctx, "EOF sink_idx:%d\n", i); | ||
| 395 | 57 | lavfi->sink_eof[i] = 1; | |
| 396 | 57 | continue; | |
| 397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13590 times.
|
13590 | } else if (ret < 0) |
| 398 | ✗ | goto fail; | |
| 399 | 13590 | d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX); | |
| 400 | ff_dlog(avctx, "sink_idx:%d time:%f\n", i, d); | ||
| 401 | 13590 | av_frame_unref(frame); | |
| 402 | |||
| 403 |
2/2✓ Branch 0 taken 13571 times.
✓ Branch 1 taken 19 times.
|
13590 | if (d < min_pts) { |
| 404 | 13571 | min_pts = d; | |
| 405 | 13571 | min_pts_sink_idx = i; | |
| 406 | } | ||
| 407 | } | ||
| 408 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 13565 times.
|
13620 | if (min_pts == DBL_MAX) { |
| 409 | 55 | ret = AVERROR_EOF; | |
| 410 | 55 | goto fail; | |
| 411 | } | ||
| 412 | |||
| 413 | ff_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx); | ||
| 414 | |||
| 415 | 13565 | av_buffersink_get_frame_flags(lavfi->sinks[min_pts_sink_idx], frame, 0); | |
| 416 | 13565 | stream_idx = lavfi->sink_stream_map[min_pts_sink_idx]; | |
| 417 | 13565 | st = avctx->streams[stream_idx]; | |
| 418 | |||
| 419 |
2/2✓ Branch 0 taken 3293 times.
✓ Branch 1 taken 10272 times.
|
13565 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 420 | 3293 | pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame), | |
| 421 | &lavfi_free_frame, NULL, 0); | ||
| 422 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3293 times.
|
3293 | if (!pkt->buf) { |
| 423 | ✗ | ret = AVERROR(ENOMEM); | |
| 424 | ✗ | goto fail; | |
| 425 | } | ||
| 426 | 3293 | frame_to_free = NULL; | |
| 427 | |||
| 428 | 3293 | pkt->data = pkt->buf->data; | |
| 429 | 3293 | pkt->size = pkt->buf->size; | |
| 430 | 3293 | pkt->flags |= AV_PKT_FLAG_TRUSTED; | |
| 431 |
1/2✓ Branch 0 taken 10272 times.
✗ Branch 1 not taken.
|
10272 | } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 432 | 10272 | int size = frame->nb_samples * av_get_bytes_per_sample(frame->format) * | |
| 433 | 10272 | frame->ch_layout.nb_channels; | |
| 434 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10272 times.
|
10272 | if ((ret = av_new_packet(pkt, size)) < 0) |
| 435 | ✗ | goto fail; | |
| 436 | 10272 | memcpy(pkt->data, frame->data[0], size); | |
| 437 | } | ||
| 438 | |||
| 439 | 13565 | frame_metadata = frame->metadata; | |
| 440 |
2/2✓ Branch 0 taken 469 times.
✓ Branch 1 taken 13096 times.
|
13565 | if (frame_metadata) { |
| 441 | size_t size; | ||
| 442 | 469 | uint8_t *metadata = av_packet_pack_dictionary(frame_metadata, &size); | |
| 443 | |||
| 444 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 469 times.
|
469 | if (!metadata) { |
| 445 | ✗ | ret = AVERROR(ENOMEM); | |
| 446 | ✗ | goto fail; | |
| 447 | } | ||
| 448 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 469 times.
|
469 | if ((ret = av_packet_add_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, |
| 449 | metadata, size)) < 0) { | ||
| 450 | ✗ | av_freep(&metadata); | |
| 451 | ✗ | goto fail; | |
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13565 times.
|
13565 | if ((ret = create_subcc_packet(avctx, frame, min_pts_sink_idx)) < 0) { |
| 456 | ✗ | goto fail; | |
| 457 | } | ||
| 458 | |||
| 459 | 13565 | pkt->stream_index = stream_idx; | |
| 460 | 13565 | pkt->pts = frame->pts; | |
| 461 | |||
| 462 | 13565 | av_frame_free(&frame_to_free); | |
| 463 | |||
| 464 | 13565 | return pkt->size; | |
| 465 | 55 | fail: | |
| 466 | 55 | av_frame_free(&frame_to_free); | |
| 467 | 55 | return ret; | |
| 468 | |||
| 469 | } | ||
| 470 | |||
| 471 | #define OFFSET(x) offsetof(LavfiContext, x) | ||
| 472 | |||
| 473 | #define DEC AV_OPT_FLAG_DECODING_PARAM | ||
| 474 | |||
| 475 | static const AVOption options[] = { | ||
| 476 | { "graph", "set libavfilter graph", OFFSET(graph_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, | ||
| 477 | { "graph_file","set libavfilter graph filename", OFFSET(graph_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC}, | ||
| 478 | { "dumpgraph", "dump graph to stderr", OFFSET(dump_graph), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, | ||
| 479 | { NULL }, | ||
| 480 | }; | ||
| 481 | |||
| 482 | static const AVClass lavfi_class = { | ||
| 483 | .class_name = "lavfi indev", | ||
| 484 | .item_name = av_default_item_name, | ||
| 485 | .option = options, | ||
| 486 | .version = LIBAVUTIL_VERSION_INT, | ||
| 487 | .category = AV_CLASS_CATEGORY_DEVICE_INPUT, | ||
| 488 | }; | ||
| 489 | |||
| 490 | const FFInputFormat ff_lavfi_demuxer = { | ||
| 491 | .p.name = "lavfi", | ||
| 492 | .p.long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"), | ||
| 493 | .p.flags = AVFMT_NOFILE, | ||
| 494 | .p.priv_class = &lavfi_class, | ||
| 495 | .priv_data_size = sizeof(LavfiContext), | ||
| 496 | .read_header = lavfi_read_header, | ||
| 497 | .read_packet = lavfi_read_packet, | ||
| 498 | .read_close = lavfi_read_close, | ||
| 499 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 500 | }; | ||
| 501 |