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.

352 lines
12KB

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