LCOV - code coverage report
Current view: top level - libavdevice - lavfi.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 216 273 79.1 %
Date: 2018-01-22 05:20:11 Functions: 6 6 100.0 %

          Line data    Source code
       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/avio_internal.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             :     AVFrame *decoded_frame;
      58             :     int nb_sinks;
      59             :     AVPacket subcc_packet;
      60             : } LavfiContext;
      61             : 
      62          19 : static int *create_all_formats(int n)
      63             : {
      64          19 :     int i, j, *fmts, count = 0;
      65             : 
      66        3496 :     for (i = 0; i < n; i++) {
      67        3477 :         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
      68        3477 :         if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
      69        3192 :             count++;
      70             :     }
      71             : 
      72          19 :     if (!(fmts = av_malloc((count+1) * sizeof(int))))
      73           0 :         return NULL;
      74        3496 :     for (j = 0, i = 0; i < n; i++) {
      75        3477 :         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
      76        3477 :         if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
      77        3192 :             fmts[j++] = i;
      78             :     }
      79          19 :     fmts[j] = -1;
      80          19 :     return fmts;
      81             : }
      82             : 
      83          19 : av_cold static int lavfi_read_close(AVFormatContext *avctx)
      84             : {
      85          19 :     LavfiContext *lavfi = avctx->priv_data;
      86             : 
      87          19 :     av_freep(&lavfi->sink_stream_map);
      88          19 :     av_freep(&lavfi->sink_eof);
      89          19 :     av_freep(&lavfi->stream_sink_map);
      90          19 :     av_freep(&lavfi->sink_stream_subcc_map);
      91          19 :     av_freep(&lavfi->sinks);
      92          19 :     avfilter_graph_free(&lavfi->graph);
      93          19 :     av_frame_free(&lavfi->decoded_frame);
      94             : 
      95          19 :     return 0;
      96             : }
      97             : 
      98          19 : static int create_subcc_streams(AVFormatContext *avctx)
      99             : {
     100          19 :     LavfiContext *lavfi = avctx->priv_data;
     101             :     AVStream *st;
     102             :     int stream_idx, sink_idx;
     103             : 
     104          40 :     for (stream_idx = 0; stream_idx < lavfi->nb_sinks; stream_idx++) {
     105          21 :         sink_idx = lavfi->stream_sink_map[stream_idx];
     106          21 :         if (lavfi->sink_stream_subcc_map[sink_idx]) {
     107           3 :             lavfi->sink_stream_subcc_map[sink_idx] = avctx->nb_streams;
     108           3 :             if (!(st = avformat_new_stream(avctx, NULL)))
     109           0 :                 return AVERROR(ENOMEM);
     110           3 :             st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
     111           3 :             st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
     112             :         } else {
     113          18 :             lavfi->sink_stream_subcc_map[sink_idx] = -1;
     114             :         }
     115             :     }
     116          19 :     return 0;
     117             : }
     118             : 
     119          19 : av_cold static int lavfi_read_header(AVFormatContext *avctx)
     120             : {
     121          19 :     LavfiContext *lavfi = avctx->priv_data;
     122          19 :     AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
     123             :     const AVFilter *buffersink, *abuffersink;
     124          19 :     int *pix_fmts = create_all_formats(AV_PIX_FMT_NB);
     125             :     enum AVMediaType type;
     126          19 :     int ret = 0, i, n;
     127             : 
     128             : #define FAIL(ERR) { ret = ERR; goto end; }
     129             : 
     130          19 :     if (!pix_fmts)
     131           0 :         FAIL(AVERROR(ENOMEM));
     132             : 
     133          19 :     avfilter_register_all();
     134             : 
     135          19 :     buffersink = avfilter_get_by_name("buffersink");
     136          19 :     abuffersink = avfilter_get_by_name("abuffersink");
     137             : 
     138          19 :     if (lavfi->graph_filename && lavfi->graph_str) {
     139           0 :         av_log(avctx, AV_LOG_ERROR,
     140             :                "Only one of the graph or graph_file options must be specified\n");
     141           0 :         FAIL(AVERROR(EINVAL));
     142             :     }
     143             : 
     144          19 :     if (lavfi->graph_filename) {
     145             :         AVBPrint graph_file_pb;
     146           1 :         AVIOContext *avio = NULL;
     147           1 :         AVDictionary *options = NULL;
     148           1 :         if (avctx->protocol_whitelist && (ret = av_dict_set(&options, "protocol_whitelist", avctx->protocol_whitelist, 0)) < 0)
     149           0 :             goto end;
     150           1 :         ret = avio_open2(&avio, lavfi->graph_filename, AVIO_FLAG_READ, &avctx->interrupt_callback, &options);
     151           1 :         av_dict_set(&options, "protocol_whitelist", NULL, 0);
     152           1 :         if (ret < 0)
     153           0 :             goto end;
     154           1 :         av_bprint_init(&graph_file_pb, 0, AV_BPRINT_SIZE_UNLIMITED);
     155           1 :         ret = avio_read_to_bprint(avio, &graph_file_pb, INT_MAX);
     156           1 :         avio_closep(&avio);
     157           1 :         av_bprint_chars(&graph_file_pb, '\0', 1);
     158           1 :         if (!ret && !av_bprint_is_complete(&graph_file_pb))
     159           0 :             ret = AVERROR(ENOMEM);
     160           1 :         if (ret) {
     161           0 :             av_bprint_finalize(&graph_file_pb, NULL);
     162           0 :             goto end;
     163             :         }
     164           1 :         if ((ret = av_bprint_finalize(&graph_file_pb, &lavfi->graph_str)))
     165           0 :             goto end;
     166             :     }
     167             : 
     168          19 :     if (!lavfi->graph_str)
     169          18 :         lavfi->graph_str = av_strdup(avctx->filename);
     170             : 
     171             :     /* parse the graph, create a stream for each open output */
     172          19 :     if (!(lavfi->graph = avfilter_graph_alloc()))
     173           0 :         FAIL(AVERROR(ENOMEM));
     174             : 
     175          19 :     if ((ret = avfilter_graph_parse_ptr(lavfi->graph, lavfi->graph_str,
     176             :                                     &input_links, &output_links, avctx)) < 0)
     177           0 :         goto end;
     178             : 
     179          19 :     if (input_links) {
     180           0 :         av_log(avctx, AV_LOG_ERROR,
     181             :                "Open inputs in the filtergraph are not acceptable\n");
     182           0 :         FAIL(AVERROR(EINVAL));
     183             :     }
     184             : 
     185             :     /* count the outputs */
     186          19 :     for (n = 0, inout = output_links; inout; n++, inout = inout->next);
     187          19 :     lavfi->nb_sinks = n;
     188             : 
     189          19 :     if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n)))
     190           0 :         FAIL(AVERROR(ENOMEM));
     191          19 :     if (!(lavfi->sink_eof = av_mallocz(sizeof(int) * n)))
     192           0 :         FAIL(AVERROR(ENOMEM));
     193          19 :     if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n)))
     194           0 :         FAIL(AVERROR(ENOMEM));
     195          19 :     if (!(lavfi->sink_stream_subcc_map = av_malloc(sizeof(int) * n)))
     196           0 :         FAIL(AVERROR(ENOMEM));
     197             : 
     198          40 :     for (i = 0; i < n; i++)
     199          21 :         lavfi->stream_sink_map[i] = -1;
     200             : 
     201             :     /* parse the output link names - they need to be of the form out0, out1, ...
     202             :      * create a mapping between them and the streams */
     203          80 :     for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
     204          21 :         int stream_idx = 0, suffix = 0, use_subcc = 0;
     205          21 :         sscanf(inout->name, "out%n%d%n", &suffix, &stream_idx, &suffix);
     206          21 :         if (!suffix) {
     207           0 :             av_log(avctx,  AV_LOG_ERROR,
     208             :                    "Invalid outpad name '%s'\n", inout->name);
     209           0 :             FAIL(AVERROR(EINVAL));
     210             :         }
     211          21 :         if (inout->name[suffix]) {
     212           3 :             if (!strcmp(inout->name + suffix, "+subcc")) {
     213           3 :                 use_subcc = 1;
     214             :             } else {
     215           0 :                 av_log(avctx,  AV_LOG_ERROR,
     216             :                        "Invalid outpad suffix '%s'\n", inout->name);
     217           0 :                 FAIL(AVERROR(EINVAL));
     218             :             }
     219             :         }
     220             : 
     221          21 :         if ((unsigned)stream_idx >= n) {
     222           0 :             av_log(avctx, AV_LOG_ERROR,
     223             :                    "Invalid index was specified in output '%s', "
     224             :                    "must be a non-negative value < %d\n",
     225             :                    inout->name, n);
     226           0 :             FAIL(AVERROR(EINVAL));
     227             :         }
     228             : 
     229          21 :         if (lavfi->stream_sink_map[stream_idx] != -1) {
     230           0 :             av_log(avctx,  AV_LOG_ERROR,
     231             :                    "An output with stream index %d was already specified\n",
     232             :                    stream_idx);
     233           0 :             FAIL(AVERROR(EINVAL));
     234             :         }
     235          21 :         lavfi->sink_stream_map[i] = stream_idx;
     236          21 :         lavfi->stream_sink_map[stream_idx] = i;
     237          21 :         lavfi->sink_stream_subcc_map[i] = !!use_subcc;
     238             :     }
     239             : 
     240             :     /* for each open output create a corresponding stream */
     241          40 :     for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
     242             :         AVStream *st;
     243          21 :         if (!(st = avformat_new_stream(avctx, NULL)))
     244           0 :             FAIL(AVERROR(ENOMEM));
     245          21 :         st->id = i;
     246             :     }
     247             : 
     248             :     /* create a sink for each output and connect them to the graph */
     249          19 :     lavfi->sinks = av_malloc_array(lavfi->nb_sinks, sizeof(AVFilterContext *));
     250          19 :     if (!lavfi->sinks)
     251           0 :         FAIL(AVERROR(ENOMEM));
     252             : 
     253          80 :     for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
     254             :         AVFilterContext *sink;
     255             : 
     256          21 :         type = avfilter_pad_get_type(inout->filter_ctx->output_pads, inout->pad_idx);
     257             : 
     258          21 :         if (type == AVMEDIA_TYPE_VIDEO && ! buffersink ||
     259           8 :             type == AVMEDIA_TYPE_AUDIO && ! abuffersink) {
     260           0 :                 av_log(avctx, AV_LOG_ERROR, "Missing required buffersink filter, aborting.\n");
     261           0 :                 FAIL(AVERROR_FILTER_NOT_FOUND);
     262             :         }
     263             : 
     264          21 :         if (type == AVMEDIA_TYPE_VIDEO) {
     265          26 :             ret = avfilter_graph_create_filter(&sink, buffersink,
     266          13 :                                                inout->name, NULL,
     267             :                                                NULL, lavfi->graph);
     268          13 :             if (ret >= 0)
     269          13 :                 ret = av_opt_set_int_list(sink, "pix_fmts", pix_fmts,  AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
     270          13 :             if (ret < 0)
     271           0 :                 goto end;
     272           8 :         } else if (type == AVMEDIA_TYPE_AUDIO) {
     273           8 :             enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_U8,
     274             :                                                   AV_SAMPLE_FMT_S16,
     275             :                                                   AV_SAMPLE_FMT_S32,
     276             :                                                   AV_SAMPLE_FMT_FLT,
     277             :                                                   AV_SAMPLE_FMT_DBL, -1 };
     278             : 
     279          16 :             ret = avfilter_graph_create_filter(&sink, abuffersink,
     280           8 :                                                inout->name, NULL,
     281             :                                                NULL, lavfi->graph);
     282           8 :             if (ret >= 0)
     283           8 :                 ret = av_opt_set_int_list(sink, "sample_fmts", sample_fmts,  AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
     284           8 :             if (ret < 0)
     285           0 :                 goto end;
     286           8 :             ret = av_opt_set_int(sink, "all_channel_counts", 1,
     287             :                                  AV_OPT_SEARCH_CHILDREN);
     288           8 :             if (ret < 0)
     289           0 :                 goto end;
     290             :         } else {
     291           0 :             av_log(avctx,  AV_LOG_ERROR,
     292             :                    "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
     293           0 :             FAIL(AVERROR(EINVAL));
     294             :         }
     295             : 
     296          21 :         lavfi->sinks[i] = sink;
     297          21 :         if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
     298           0 :             goto end;
     299             :     }
     300             : 
     301             :     /* configure the graph */
     302          19 :     if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
     303           0 :         goto end;
     304             : 
     305          19 :     if (lavfi->dump_graph) {
     306           0 :         char *dump = avfilter_graph_dump(lavfi->graph, lavfi->dump_graph);
     307           0 :         fputs(dump, stderr);
     308           0 :         fflush(stderr);
     309           0 :         av_free(dump);
     310             :     }
     311             : 
     312             :     /* fill each stream with the information in the corresponding sink */
     313          40 :     for (i = 0; i < lavfi->nb_sinks; i++) {
     314          21 :         AVFilterContext *sink = lavfi->sinks[lavfi->stream_sink_map[i]];
     315          21 :         AVRational time_base = av_buffersink_get_time_base(sink);
     316          21 :         AVStream *st = avctx->streams[i];
     317          21 :         st->codecpar->codec_type = av_buffersink_get_type(sink);
     318          21 :         avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
     319          21 :         if (av_buffersink_get_type(sink) == AVMEDIA_TYPE_VIDEO) {
     320          13 :             st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
     321          13 :             st->codecpar->format     = av_buffersink_get_format(sink);
     322          13 :             st->codecpar->width      = av_buffersink_get_w(sink);
     323          13 :             st->codecpar->height     = av_buffersink_get_h(sink);
     324          13 :             st       ->sample_aspect_ratio =
     325          13 :             st->codecpar->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink);
     326          13 :             avctx->probesize = FFMAX(avctx->probesize,
     327             :                                      av_buffersink_get_w(sink) * av_buffersink_get_h(sink) *
     328             :                                      av_get_padded_bits_per_pixel(av_pix_fmt_desc_get(av_buffersink_get_format(sink))) *
     329             :                                      30);
     330           8 :         } else if (av_buffersink_get_type(sink) == AVMEDIA_TYPE_AUDIO) {
     331           8 :             st->codecpar->codec_id    = av_get_pcm_codec(av_buffersink_get_format(sink), -1);
     332           8 :             st->codecpar->channels    = av_buffersink_get_channels(sink);
     333           8 :             st->codecpar->format      = av_buffersink_get_format(sink);
     334           8 :             st->codecpar->sample_rate = av_buffersink_get_sample_rate(sink);
     335           8 :             st->codecpar->channel_layout = av_buffersink_get_channel_layout(sink);
     336           8 :             if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
     337           0 :                 av_log(avctx, AV_LOG_ERROR,
     338             :                        "Could not find PCM codec for sample format %s.\n",
     339           0 :                        av_get_sample_fmt_name(av_buffersink_get_format(sink)));
     340             :         }
     341             :     }
     342             : 
     343          19 :     if ((ret = create_subcc_streams(avctx)) < 0)
     344           0 :         goto end;
     345             : 
     346          19 :     if (!(lavfi->decoded_frame = av_frame_alloc()))
     347           0 :         FAIL(AVERROR(ENOMEM));
     348             : 
     349          38 : end:
     350          19 :     av_free(pix_fmts);
     351          19 :     avfilter_inout_free(&input_links);
     352          19 :     avfilter_inout_free(&output_links);
     353          19 :     if (ret < 0)
     354           0 :         lavfi_read_close(avctx);
     355          19 :     return ret;
     356             : }
     357             : 
     358        4205 : static int create_subcc_packet(AVFormatContext *avctx, AVFrame *frame,
     359             :                                int sink_idx)
     360             : {
     361        4205 :     LavfiContext *lavfi = avctx->priv_data;
     362             :     AVFrameSideData *sd;
     363             :     int stream_idx, i, ret;
     364             : 
     365        4205 :     if ((stream_idx = lavfi->sink_stream_subcc_map[sink_idx]) < 0)
     366        3733 :         return 0;
     367         944 :     for (i = 0; i < frame->nb_side_data; i++)
     368         944 :         if (frame->side_data[i]->type == AV_FRAME_DATA_A53_CC)
     369         472 :             break;
     370         472 :     if (i >= frame->nb_side_data)
     371           0 :         return 0;
     372         472 :     sd = frame->side_data[i];
     373         472 :     if ((ret = av_new_packet(&lavfi->subcc_packet, sd->size)) < 0)
     374           0 :         return ret;
     375         472 :     memcpy(lavfi->subcc_packet.data, sd->data, sd->size);
     376         472 :     lavfi->subcc_packet.stream_index = stream_idx;
     377         472 :     lavfi->subcc_packet.pts = frame->pts;
     378         472 :     lavfi->subcc_packet.pos = frame->pkt_pos;
     379         472 :     return 0;
     380             : }
     381             : 
     382        4697 : static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
     383             : {
     384        4697 :     LavfiContext *lavfi = avctx->priv_data;
     385        4697 :     double min_pts = DBL_MAX;
     386        4697 :     int stream_idx, min_pts_sink_idx = 0;
     387        4697 :     AVFrame *frame = lavfi->decoded_frame;
     388             :     AVDictionary *frame_metadata;
     389             :     int ret, i;
     390        4697 :     int size = 0;
     391             : 
     392        4697 :     if (lavfi->subcc_packet.size) {
     393         472 :         *pkt = lavfi->subcc_packet;
     394         472 :         av_init_packet(&lavfi->subcc_packet);
     395         472 :         lavfi->subcc_packet.size = 0;
     396         472 :         lavfi->subcc_packet.data = NULL;
     397         472 :         return pkt->size;
     398             :     }
     399             : 
     400             :     /* iterate through all the graph sinks. Select the sink with the
     401             :      * minimum PTS */
     402        8480 :     for (i = 0; i < lavfi->nb_sinks; i++) {
     403        4255 :         AVRational tb = av_buffersink_get_time_base(lavfi->sinks[i]);
     404             :         double d;
     405             :         int ret;
     406             : 
     407        4255 :         if (lavfi->sink_eof[i])
     408          30 :             continue;
     409             : 
     410        4250 :         ret = av_buffersink_get_frame_flags(lavfi->sinks[i], frame,
     411             :                                             AV_BUFFERSINK_FLAG_PEEK);
     412        4250 :         if (ret == AVERROR_EOF) {
     413             :             ff_dlog(avctx, "EOF sink_idx:%d\n", i);
     414          20 :             lavfi->sink_eof[i] = 1;
     415          20 :             continue;
     416        4230 :         } else if (ret < 0)
     417           0 :             return ret;
     418        4230 :         d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
     419             :         ff_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
     420        4230 :         av_frame_unref(frame);
     421             : 
     422        4230 :         if (d < min_pts) {
     423        4214 :             min_pts = d;
     424        4214 :             min_pts_sink_idx = i;
     425             :         }
     426             :     }
     427        4225 :     if (min_pts == DBL_MAX)
     428          20 :         return AVERROR_EOF;
     429             : 
     430             :     ff_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
     431             : 
     432        4205 :     av_buffersink_get_frame_flags(lavfi->sinks[min_pts_sink_idx], frame, 0);
     433        4205 :     stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];
     434             : 
     435        4205 :     if (frame->width /* FIXME best way of testing a video */) {
     436         763 :         size = av_image_get_buffer_size(frame->format, frame->width, frame->height, 1);
     437         763 :         if ((ret = av_new_packet(pkt, size)) < 0)
     438           0 :             return ret;
     439             : 
     440        1526 :         av_image_copy_to_buffer(pkt->data, size, (const uint8_t **)frame->data, frame->linesize,
     441         763 :                                 frame->format, frame->width, frame->height, 1);
     442        3442 :     } else if (frame->channels /* FIXME test audio */) {
     443        6884 :         size = frame->nb_samples * av_get_bytes_per_sample(frame->format) *
     444        3442 :                                    frame->channels;
     445        3442 :         if ((ret = av_new_packet(pkt, size)) < 0)
     446           0 :             return ret;
     447        3442 :         memcpy(pkt->data, frame->data[0], size);
     448             :     }
     449             : 
     450        4205 :     frame_metadata = frame->metadata;
     451        4205 :     if (frame_metadata) {
     452             :         uint8_t *metadata;
     453         403 :         AVDictionaryEntry *e = NULL;
     454             :         AVBPrint meta_buf;
     455             : 
     456         403 :         av_bprint_init(&meta_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
     457        2831 :         while ((e = av_dict_get(frame_metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
     458        2025 :             av_bprintf(&meta_buf, "%s", e->key);
     459        2025 :             av_bprint_chars(&meta_buf, '\0', 1);
     460        2025 :             av_bprintf(&meta_buf, "%s", e->value);
     461        2025 :             av_bprint_chars(&meta_buf, '\0', 1);
     462             :         }
     463         806 :         if (!av_bprint_is_complete(&meta_buf) ||
     464         403 :             !(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA,
     465         403 :                                                  meta_buf.len))) {
     466           0 :             av_bprint_finalize(&meta_buf, NULL);
     467           0 :             return AVERROR(ENOMEM);
     468             :         }
     469         403 :         memcpy(metadata, meta_buf.str, meta_buf.len);
     470         403 :         av_bprint_finalize(&meta_buf, NULL);
     471             :     }
     472             : 
     473        4205 :     if ((ret = create_subcc_packet(avctx, frame, min_pts_sink_idx)) < 0) {
     474           0 :         av_frame_unref(frame);
     475           0 :         av_packet_unref(pkt);
     476           0 :         return ret;
     477             :     }
     478             : 
     479        4205 :     pkt->stream_index = stream_idx;
     480        4205 :     pkt->pts = frame->pts;
     481        4205 :     pkt->pos = frame->pkt_pos;
     482        4205 :     pkt->size = size;
     483        4205 :     av_frame_unref(frame);
     484        4205 :     return size;
     485             : }
     486             : 
     487             : #define OFFSET(x) offsetof(LavfiContext, x)
     488             : 
     489             : #define DEC AV_OPT_FLAG_DECODING_PARAM
     490             : 
     491             : static const AVOption options[] = {
     492             :     { "graph",     "set libavfilter graph", OFFSET(graph_str),  AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
     493             :     { "graph_file","set libavfilter graph filename", OFFSET(graph_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
     494             :     { "dumpgraph", "dump graph to stderr",  OFFSET(dump_graph), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
     495             :     { NULL },
     496             : };
     497             : 
     498             : static const AVClass lavfi_class = {
     499             :     .class_name = "lavfi indev",
     500             :     .item_name  = av_default_item_name,
     501             :     .option     = options,
     502             :     .version    = LIBAVUTIL_VERSION_INT,
     503             :     .category   = AV_CLASS_CATEGORY_DEVICE_INPUT,
     504             : };
     505             : 
     506             : AVInputFormat ff_lavfi_demuxer = {
     507             :     .name           = "lavfi",
     508             :     .long_name      = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
     509             :     .priv_data_size = sizeof(LavfiContext),
     510             :     .read_header    = lavfi_read_header,
     511             :     .read_packet    = lavfi_read_packet,
     512             :     .read_close     = lavfi_read_close,
     513             :     .flags          = AVFMT_NOFILE,
     514             :     .priv_class     = &lavfi_class,
     515             : };

Generated by: LCOV version 1.13