00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026
00027
00028 #include "float.h"
00029
00030 #include "libavutil/log.h"
00031 #include "libavutil/mem.h"
00032 #include "libavutil/opt.h"
00033 #include "libavutil/parseutils.h"
00034 #include "libavutil/pixdesc.h"
00035 #include "libavutil/audioconvert.h"
00036 #include "libavfilter/avfilter.h"
00037 #include "libavfilter/avfiltergraph.h"
00038 #include "libavfilter/buffersink.h"
00039 #include "libavformat/internal.h"
00040 #include "avdevice.h"
00041
00042 typedef struct {
00043 AVClass *class;
00044 char *graph_str;
00045 char *dump_graph;
00046 AVFilterGraph *graph;
00047 AVFilterContext **sinks;
00048 int *sink_stream_map;
00049 int *stream_sink_map;
00050 } LavfiContext;
00051
00052 static int *create_all_formats(int n)
00053 {
00054 int i, j, *fmts, count = 0;
00055
00056 for (i = 0; i < n; i++)
00057 if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL))
00058 count++;
00059
00060 if (!(fmts = av_malloc((count+1) * sizeof(int))))
00061 return NULL;
00062 for (j = 0, i = 0; i < n; i++) {
00063 if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL))
00064 fmts[j++] = i;
00065 }
00066 fmts[j] = -1;
00067 return fmts;
00068 }
00069
00070 av_cold static int lavfi_read_close(AVFormatContext *avctx)
00071 {
00072 LavfiContext *lavfi = avctx->priv_data;
00073
00074 av_freep(&lavfi->sink_stream_map);
00075 av_freep(&lavfi->stream_sink_map);
00076 av_freep(&lavfi->sinks);
00077 avfilter_graph_free(&lavfi->graph);
00078
00079 return 0;
00080 }
00081
00082 av_cold static int lavfi_read_header(AVFormatContext *avctx)
00083 {
00084 LavfiContext *lavfi = avctx->priv_data;
00085 AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
00086 AVFilter *buffersink, *abuffersink;
00087 int *pix_fmts = create_all_formats(PIX_FMT_NB);
00088 enum AVMediaType type;
00089 int ret = 0, i, n;
00090
00091 #define FAIL(ERR) { ret = ERR; goto end; }
00092
00093 if (!pix_fmts)
00094 FAIL(AVERROR(ENOMEM));
00095
00096 avfilter_register_all();
00097
00098 buffersink = avfilter_get_by_name("buffersink");
00099 abuffersink = avfilter_get_by_name("abuffersink");
00100
00101 if (!lavfi->graph_str)
00102 lavfi->graph_str = av_strdup(avctx->filename);
00103
00104
00105 if (!(lavfi->graph = avfilter_graph_alloc()))
00106 FAIL(AVERROR(ENOMEM));
00107
00108 if ((ret = avfilter_graph_parse(lavfi->graph, lavfi->graph_str,
00109 &input_links, &output_links, avctx)) < 0)
00110 FAIL(ret);
00111
00112 if (input_links) {
00113 av_log(avctx, AV_LOG_ERROR,
00114 "Open inputs in the filtergraph are not acceptable\n");
00115 FAIL(AVERROR(EINVAL));
00116 }
00117
00118
00119 for (n = 0, inout = output_links; inout; n++, inout = inout->next);
00120
00121 if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n)))
00122 FAIL(AVERROR(ENOMEM));
00123 if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n)))
00124 FAIL(AVERROR(ENOMEM));
00125
00126 for (i = 0; i < n; i++)
00127 lavfi->stream_sink_map[i] = -1;
00128
00129
00130
00131 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
00132 int stream_idx;
00133 if (!strcmp(inout->name, "out"))
00134 stream_idx = 0;
00135 else if (sscanf(inout->name, "out%d\n", &stream_idx) != 1) {
00136 av_log(avctx, AV_LOG_ERROR,
00137 "Invalid outpad name '%s'\n", inout->name);
00138 FAIL(AVERROR(EINVAL));
00139 }
00140
00141 if ((unsigned)stream_idx >= n) {
00142 av_log(avctx, AV_LOG_ERROR,
00143 "Invalid index was specified in output '%s', "
00144 "must be a non-negative value < %d\n",
00145 inout->name, n);
00146 FAIL(AVERROR(EINVAL));
00147 }
00148
00149
00150 type = inout->filter_ctx->output_pads[inout->pad_idx].type;
00151 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
00152 av_log(avctx, AV_LOG_ERROR,
00153 "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
00154 FAIL(AVERROR(EINVAL));
00155 }
00156
00157 if (lavfi->stream_sink_map[stream_idx] != -1) {
00158 av_log(avctx, AV_LOG_ERROR,
00159 "An output with stream index %d was already specified\n",
00160 stream_idx);
00161 FAIL(AVERROR(EINVAL));
00162 }
00163 lavfi->sink_stream_map[i] = stream_idx;
00164 lavfi->stream_sink_map[stream_idx] = i;
00165 }
00166
00167
00168 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
00169 AVStream *st;
00170 if (!(st = avformat_new_stream(avctx, NULL)))
00171 FAIL(AVERROR(ENOMEM));
00172 st->id = i;
00173 }
00174
00175
00176 lavfi->sinks = av_malloc(sizeof(AVFilterContext *) * avctx->nb_streams);
00177 if (!lavfi->sinks)
00178 FAIL(AVERROR(ENOMEM));
00179
00180 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
00181 AVFilterContext *sink;
00182
00183 type = inout->filter_ctx->output_pads[inout->pad_idx].type;
00184
00185 if (type == AVMEDIA_TYPE_VIDEO && ! buffersink ||
00186 type == AVMEDIA_TYPE_AUDIO && ! abuffersink) {
00187 av_log(avctx, AV_LOG_ERROR, "Missing required buffersink filter, aborting.\n");
00188 FAIL(AVERROR_FILTER_NOT_FOUND);
00189 }
00190
00191 if (type == AVMEDIA_TYPE_VIDEO) {
00192 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
00193
00194 #if FF_API_OLD_VSINK_API
00195 ret = avfilter_graph_create_filter(&sink, buffersink,
00196 inout->name, NULL,
00197 pix_fmts, lavfi->graph);
00198 #else
00199 buffersink_params->pixel_fmts = pix_fmts;
00200 ret = avfilter_graph_create_filter(&sink, buffersink,
00201 inout->name, NULL,
00202 buffersink_params, lavfi->graph);
00203 #endif
00204 av_freep(&buffersink_params);
00205
00206 if (ret < 0)
00207 goto end;
00208 } else if (type == AVMEDIA_TYPE_AUDIO) {
00209 enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_U8,
00210 AV_SAMPLE_FMT_S16,
00211 AV_SAMPLE_FMT_S32,
00212 AV_SAMPLE_FMT_FLT,
00213 AV_SAMPLE_FMT_DBL, -1 };
00214 const int64_t *chlayouts = avfilter_all_channel_layouts;
00215 AVABufferSinkParams *abuffersink_params = av_abuffersink_params_alloc();
00216 abuffersink_params->sample_fmts = sample_fmts;
00217 abuffersink_params->channel_layouts = chlayouts;
00218
00219 ret = avfilter_graph_create_filter(&sink, abuffersink,
00220 inout->name, NULL,
00221 abuffersink_params, lavfi->graph);
00222 av_free(abuffersink_params);
00223 if (ret < 0)
00224 goto end;
00225 }
00226
00227 lavfi->sinks[i] = sink;
00228 if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
00229 FAIL(ret);
00230 }
00231
00232
00233 if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
00234 FAIL(ret);
00235
00236 if (lavfi->dump_graph) {
00237 char *dump = avfilter_graph_dump(lavfi->graph, lavfi->dump_graph);
00238 fputs(dump, stderr);
00239 fflush(stderr);
00240 av_free(dump);
00241 }
00242
00243
00244 for (i = 0; i < avctx->nb_streams; i++) {
00245 AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0];
00246 AVStream *st = avctx->streams[i];
00247 st->codec->codec_type = link->type;
00248 avpriv_set_pts_info(st, 64, link->time_base.num, link->time_base.den);
00249 if (link->type == AVMEDIA_TYPE_VIDEO) {
00250 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00251 st->codec->pix_fmt = link->format;
00252 st->codec->time_base = link->time_base;
00253 st->codec->width = link->w;
00254 st->codec->height = link->h;
00255 st ->sample_aspect_ratio =
00256 st->codec->sample_aspect_ratio = link->sample_aspect_ratio;
00257 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
00258 st->codec->codec_id = av_get_pcm_codec(link->format, -1);
00259 st->codec->channels = av_get_channel_layout_nb_channels(link->channel_layout);
00260 st->codec->sample_fmt = link->format;
00261 st->codec->sample_rate = link->sample_rate;
00262 st->codec->time_base = link->time_base;
00263 st->codec->channel_layout = link->channel_layout;
00264 if (st->codec->codec_id == CODEC_ID_NONE)
00265 av_log(avctx, AV_LOG_ERROR,
00266 "Could not find PCM codec for sample format %s.\n",
00267 av_get_sample_fmt_name(link->format));
00268 }
00269 }
00270
00271 end:
00272 av_free(pix_fmts);
00273 avfilter_inout_free(&input_links);
00274 avfilter_inout_free(&output_links);
00275 if (ret < 0)
00276 lavfi_read_close(avctx);
00277 return ret;
00278 }
00279
00280 static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
00281 {
00282 LavfiContext *lavfi = avctx->priv_data;
00283 double min_pts = DBL_MAX;
00284 int stream_idx, min_pts_sink_idx = 0;
00285 AVFilterBufferRef *ref;
00286 AVPicture pict;
00287 int ret, i;
00288 int size = 0;
00289
00290
00291
00292 for (i = 0; i < avctx->nb_streams; i++) {
00293 AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;
00294 double d;
00295 int ret = av_buffersink_get_buffer_ref(lavfi->sinks[i],
00296 &ref, AV_BUFFERSINK_FLAG_PEEK);
00297 if (ret < 0)
00298 return ret;
00299 d = av_rescale_q(ref->pts, tb, AV_TIME_BASE_Q);
00300 av_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
00301
00302 if (d < min_pts) {
00303 min_pts = d;
00304 min_pts_sink_idx = i;
00305 }
00306 }
00307 av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
00308
00309 av_buffersink_get_buffer_ref(lavfi->sinks[min_pts_sink_idx], &ref, 0);
00310 stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];
00311
00312 if (ref->video) {
00313 size = avpicture_get_size(ref->format, ref->video->w, ref->video->h);
00314 if ((ret = av_new_packet(pkt, size)) < 0)
00315 return ret;
00316
00317 memcpy(pict.data, ref->data, 4*sizeof(ref->data[0]));
00318 memcpy(pict.linesize, ref->linesize, 4*sizeof(ref->linesize[0]));
00319
00320 avpicture_layout(&pict, ref->format, ref->video->w,
00321 ref->video->h, pkt->data, size);
00322 } else if (ref->audio) {
00323 size = ref->audio->nb_samples *
00324 av_get_bytes_per_sample(ref->format) *
00325 av_get_channel_layout_nb_channels(ref->audio->channel_layout);
00326 if ((ret = av_new_packet(pkt, size)) < 0)
00327 return ret;
00328 memcpy(pkt->data, ref->data[0], size);
00329 }
00330
00331 pkt->stream_index = stream_idx;
00332 pkt->pts = ref->pts;
00333 pkt->pos = ref->pos;
00334 pkt->size = size;
00335 avfilter_unref_buffer(ref);
00336
00337 return size;
00338 }
00339
00340 #define OFFSET(x) offsetof(LavfiContext, x)
00341
00342 #define DEC AV_OPT_FLAG_DECODING_PARAM
00343
00344 static const AVOption options[] = {
00345 { "graph", "Libavfilter graph", OFFSET(graph_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
00346 { "dumpgraph", "Dump graph to stderr", OFFSET(dump_graph), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00347 { NULL },
00348 };
00349
00350 static const AVClass lavfi_class = {
00351 .class_name = "lavfi indev",
00352 .item_name = av_default_item_name,
00353 .option = options,
00354 .version = LIBAVUTIL_VERSION_INT,
00355 };
00356
00357 AVInputFormat ff_lavfi_demuxer = {
00358 .name = "lavfi",
00359 .long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
00360 .priv_data_size = sizeof(LavfiContext),
00361 .read_header = lavfi_read_header,
00362 .read_packet = lavfi_read_packet,
00363 .read_close = lavfi_read_close,
00364 .flags = AVFMT_NOFILE,
00365 .priv_class = &lavfi_class,
00366 };