You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

377 lines
13KB

  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. * @file
  22. * libavfilter virtual input device
  23. */
  24. /* #define DEBUG */
  25. #include "float.h" /* DBL_MIN, DBL_MAX */
  26. #include "libavutil/log.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/parseutils.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavfilter/avfilter.h"
  33. #include "libavfilter/avfiltergraph.h"
  34. #include "libavfilter/buffersink.h"
  35. #include "libavformat/internal.h"
  36. #include "avdevice.h"
  37. typedef struct {
  38. AVClass *class; ///< class for private options
  39. char *graph_str;
  40. char *dump_graph;
  41. AVFilterGraph *graph;
  42. AVFilterContext **sinks;
  43. int *sink_stream_map;
  44. int *sink_eof;
  45. int *stream_sink_map;
  46. } LavfiContext;
  47. static int *create_all_formats(int n)
  48. {
  49. int i, j, *fmts, count = 0;
  50. for (i = 0; i < n; i++) {
  51. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
  52. if (!(desc->flags & PIX_FMT_HWACCEL))
  53. count++;
  54. }
  55. if (!(fmts = av_malloc((count+1) * sizeof(int))))
  56. return NULL;
  57. for (j = 0, i = 0; i < n; i++) {
  58. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
  59. if (!(desc->flags & PIX_FMT_HWACCEL))
  60. fmts[j++] = i;
  61. }
  62. fmts[j] = -1;
  63. return fmts;
  64. }
  65. av_cold static int lavfi_read_close(AVFormatContext *avctx)
  66. {
  67. LavfiContext *lavfi = avctx->priv_data;
  68. av_freep(&lavfi->sink_stream_map);
  69. av_freep(&lavfi->sink_eof);
  70. av_freep(&lavfi->stream_sink_map);
  71. av_freep(&lavfi->sinks);
  72. avfilter_graph_free(&lavfi->graph);
  73. return 0;
  74. }
  75. av_cold static int lavfi_read_header(AVFormatContext *avctx)
  76. {
  77. LavfiContext *lavfi = avctx->priv_data;
  78. AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
  79. AVFilter *buffersink, *abuffersink;
  80. int *pix_fmts = create_all_formats(AV_PIX_FMT_NB);
  81. enum AVMediaType type;
  82. int ret = 0, i, n;
  83. #define FAIL(ERR) { ret = ERR; goto end; }
  84. if (!pix_fmts)
  85. FAIL(AVERROR(ENOMEM));
  86. avfilter_register_all();
  87. buffersink = avfilter_get_by_name("ffbuffersink");
  88. abuffersink = avfilter_get_by_name("ffabuffersink");
  89. if (!lavfi->graph_str)
  90. lavfi->graph_str = av_strdup(avctx->filename);
  91. /* parse the graph, create a stream for each open output */
  92. if (!(lavfi->graph = avfilter_graph_alloc()))
  93. FAIL(AVERROR(ENOMEM));
  94. if ((ret = avfilter_graph_parse(lavfi->graph, lavfi->graph_str,
  95. &input_links, &output_links, avctx)) < 0)
  96. FAIL(ret);
  97. if (input_links) {
  98. av_log(avctx, AV_LOG_ERROR,
  99. "Open inputs in the filtergraph are not acceptable\n");
  100. FAIL(AVERROR(EINVAL));
  101. }
  102. /* count the outputs */
  103. for (n = 0, inout = output_links; inout; n++, inout = inout->next);
  104. if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n)))
  105. FAIL(AVERROR(ENOMEM));
  106. if (!(lavfi->sink_eof = av_mallocz(sizeof(int) * n)))
  107. FAIL(AVERROR(ENOMEM));
  108. if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n)))
  109. FAIL(AVERROR(ENOMEM));
  110. for (i = 0; i < n; i++)
  111. lavfi->stream_sink_map[i] = -1;
  112. /* parse the output link names - they need to be of the form out0, out1, ...
  113. * create a mapping between them and the streams */
  114. for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
  115. int stream_idx;
  116. if (!strcmp(inout->name, "out"))
  117. stream_idx = 0;
  118. else if (sscanf(inout->name, "out%d\n", &stream_idx) != 1) {
  119. av_log(avctx, AV_LOG_ERROR,
  120. "Invalid outpad name '%s'\n", inout->name);
  121. FAIL(AVERROR(EINVAL));
  122. }
  123. if ((unsigned)stream_idx >= n) {
  124. av_log(avctx, AV_LOG_ERROR,
  125. "Invalid index was specified in output '%s', "
  126. "must be a non-negative value < %d\n",
  127. inout->name, n);
  128. FAIL(AVERROR(EINVAL));
  129. }
  130. /* is an audio or video output? */
  131. type = inout->filter_ctx->output_pads[inout->pad_idx].type;
  132. if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
  133. av_log(avctx, AV_LOG_ERROR,
  134. "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
  135. FAIL(AVERROR(EINVAL));
  136. }
  137. if (lavfi->stream_sink_map[stream_idx] != -1) {
  138. av_log(avctx, AV_LOG_ERROR,
  139. "An output with stream index %d was already specified\n",
  140. stream_idx);
  141. FAIL(AVERROR(EINVAL));
  142. }
  143. lavfi->sink_stream_map[i] = stream_idx;
  144. lavfi->stream_sink_map[stream_idx] = i;
  145. }
  146. /* for each open output create a corresponding stream */
  147. for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
  148. AVStream *st;
  149. if (!(st = avformat_new_stream(avctx, NULL)))
  150. FAIL(AVERROR(ENOMEM));
  151. st->id = i;
  152. }
  153. /* create a sink for each output and connect them to the graph */
  154. lavfi->sinks = av_malloc(sizeof(AVFilterContext *) * avctx->nb_streams);
  155. if (!lavfi->sinks)
  156. FAIL(AVERROR(ENOMEM));
  157. for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
  158. AVFilterContext *sink;
  159. type = inout->filter_ctx->output_pads[inout->pad_idx].type;
  160. if (type == AVMEDIA_TYPE_VIDEO && ! buffersink ||
  161. type == AVMEDIA_TYPE_AUDIO && ! abuffersink) {
  162. av_log(avctx, AV_LOG_ERROR, "Missing required buffersink filter, aborting.\n");
  163. FAIL(AVERROR_FILTER_NOT_FOUND);
  164. }
  165. if (type == AVMEDIA_TYPE_VIDEO) {
  166. AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
  167. buffersink_params->pixel_fmts = pix_fmts;
  168. ret = avfilter_graph_create_filter(&sink, buffersink,
  169. inout->name, NULL,
  170. buffersink_params, lavfi->graph);
  171. av_freep(&buffersink_params);
  172. if (ret < 0)
  173. goto end;
  174. } else if (type == AVMEDIA_TYPE_AUDIO) {
  175. enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_U8,
  176. AV_SAMPLE_FMT_S16,
  177. AV_SAMPLE_FMT_S32,
  178. AV_SAMPLE_FMT_FLT,
  179. AV_SAMPLE_FMT_DBL, -1 };
  180. AVABufferSinkParams *abuffersink_params = av_abuffersink_params_alloc();
  181. abuffersink_params->sample_fmts = sample_fmts;
  182. ret = avfilter_graph_create_filter(&sink, abuffersink,
  183. inout->name, NULL,
  184. abuffersink_params, lavfi->graph);
  185. av_free(abuffersink_params);
  186. if (ret < 0)
  187. goto end;
  188. }
  189. lavfi->sinks[i] = sink;
  190. if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
  191. FAIL(ret);
  192. }
  193. /* configure the graph */
  194. if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
  195. FAIL(ret);
  196. if (lavfi->dump_graph) {
  197. char *dump = avfilter_graph_dump(lavfi->graph, lavfi->dump_graph);
  198. fputs(dump, stderr);
  199. fflush(stderr);
  200. av_free(dump);
  201. }
  202. /* fill each stream with the information in the corresponding sink */
  203. for (i = 0; i < avctx->nb_streams; i++) {
  204. AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0];
  205. AVStream *st = avctx->streams[i];
  206. st->codec->codec_type = link->type;
  207. avpriv_set_pts_info(st, 64, link->time_base.num, link->time_base.den);
  208. if (link->type == AVMEDIA_TYPE_VIDEO) {
  209. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  210. st->codec->pix_fmt = link->format;
  211. st->codec->time_base = link->time_base;
  212. st->codec->width = link->w;
  213. st->codec->height = link->h;
  214. st ->sample_aspect_ratio =
  215. st->codec->sample_aspect_ratio = link->sample_aspect_ratio;
  216. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  217. st->codec->codec_id = av_get_pcm_codec(link->format, -1);
  218. st->codec->channels = av_get_channel_layout_nb_channels(link->channel_layout);
  219. st->codec->sample_fmt = link->format;
  220. st->codec->sample_rate = link->sample_rate;
  221. st->codec->time_base = link->time_base;
  222. st->codec->channel_layout = link->channel_layout;
  223. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  224. av_log(avctx, AV_LOG_ERROR,
  225. "Could not find PCM codec for sample format %s.\n",
  226. av_get_sample_fmt_name(link->format));
  227. }
  228. }
  229. end:
  230. av_free(pix_fmts);
  231. avfilter_inout_free(&input_links);
  232. avfilter_inout_free(&output_links);
  233. if (ret < 0)
  234. lavfi_read_close(avctx);
  235. return ret;
  236. }
  237. static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  238. {
  239. LavfiContext *lavfi = avctx->priv_data;
  240. double min_pts = DBL_MAX;
  241. int stream_idx, min_pts_sink_idx = 0;
  242. AVFilterBufferRef *ref;
  243. AVPicture pict;
  244. int ret, i;
  245. int size = 0;
  246. /* iterate through all the graph sinks. Select the sink with the
  247. * minimum PTS */
  248. for (i = 0; i < avctx->nb_streams; i++) {
  249. AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;
  250. double d;
  251. int ret;
  252. if (lavfi->sink_eof[i])
  253. continue;
  254. ret = av_buffersink_get_buffer_ref(lavfi->sinks[i],
  255. &ref, AV_BUFFERSINK_FLAG_PEEK);
  256. if (ret == AVERROR_EOF) {
  257. av_dlog(avctx, "EOF sink_idx:%d\n", i);
  258. lavfi->sink_eof[i] = 1;
  259. continue;
  260. } else if (ret < 0)
  261. return ret;
  262. d = av_rescale_q(ref->pts, tb, AV_TIME_BASE_Q);
  263. av_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
  264. if (d < min_pts) {
  265. min_pts = d;
  266. min_pts_sink_idx = i;
  267. }
  268. }
  269. if (min_pts == DBL_MAX)
  270. return AVERROR_EOF;
  271. av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
  272. av_buffersink_get_buffer_ref(lavfi->sinks[min_pts_sink_idx], &ref, 0);
  273. stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];
  274. if (ref->video) {
  275. size = avpicture_get_size(ref->format, ref->video->w, ref->video->h);
  276. if ((ret = av_new_packet(pkt, size)) < 0)
  277. return ret;
  278. memcpy(pict.data, ref->data, 4*sizeof(ref->data[0]));
  279. memcpy(pict.linesize, ref->linesize, 4*sizeof(ref->linesize[0]));
  280. avpicture_layout(&pict, ref->format, ref->video->w,
  281. ref->video->h, pkt->data, size);
  282. } else if (ref->audio) {
  283. size = ref->audio->nb_samples *
  284. av_get_bytes_per_sample(ref->format) *
  285. av_get_channel_layout_nb_channels(ref->audio->channel_layout);
  286. if ((ret = av_new_packet(pkt, size)) < 0)
  287. return ret;
  288. memcpy(pkt->data, ref->data[0], size);
  289. }
  290. pkt->stream_index = stream_idx;
  291. pkt->pts = ref->pts;
  292. pkt->pos = ref->pos;
  293. pkt->size = size;
  294. avfilter_unref_buffer(ref);
  295. return size;
  296. }
  297. #define OFFSET(x) offsetof(LavfiContext, x)
  298. #define DEC AV_OPT_FLAG_DECODING_PARAM
  299. static const AVOption options[] = {
  300. { "graph", "set libavfilter graph", OFFSET(graph_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  301. { "dumpgraph", "dump graph to stderr", OFFSET(dump_graph), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  302. { NULL },
  303. };
  304. static const AVClass lavfi_class = {
  305. .class_name = "lavfi indev",
  306. .item_name = av_default_item_name,
  307. .option = options,
  308. .version = LIBAVUTIL_VERSION_INT,
  309. };
  310. AVInputFormat ff_lavfi_demuxer = {
  311. .name = "lavfi",
  312. .long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
  313. .priv_data_size = sizeof(LavfiContext),
  314. .read_header = lavfi_read_header,
  315. .read_packet = lavfi_read_packet,
  316. .read_close = lavfi_read_close,
  317. .flags = AVFMT_NOFILE,
  318. .priv_class = &lavfi_class,
  319. };