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.

502 lines
16KB

  1. /*
  2. * Copyright (c) 2008 Vitor Sessak
  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. * memory buffer source filter
  23. */
  24. #include <float.h>
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/frame.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/internal.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/timestamp.h"
  34. #include "audio.h"
  35. #include "avfilter.h"
  36. #include "buffersrc.h"
  37. #include "formats.h"
  38. #include "internal.h"
  39. #include "video.h"
  40. typedef struct BufferSourceContext {
  41. const AVClass *class;
  42. AVRational time_base; ///< time_base to set in the output link
  43. AVRational frame_rate; ///< frame_rate to set in the output link
  44. unsigned nb_failed_requests;
  45. /* video only */
  46. int w, h;
  47. enum AVPixelFormat pix_fmt;
  48. AVRational pixel_aspect;
  49. #if FF_API_SWS_PARAM_OPTION
  50. char *sws_param;
  51. #endif
  52. AVBufferRef *hw_frames_ctx;
  53. /* audio only */
  54. int sample_rate;
  55. enum AVSampleFormat sample_fmt;
  56. int channels;
  57. uint64_t channel_layout;
  58. char *channel_layout_str;
  59. int eof;
  60. } BufferSourceContext;
  61. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format, pts)\
  62. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  63. av_log(s, AV_LOG_INFO, "filter context - w: %d h: %d fmt: %d, incoming frame - w: %d h: %d fmt: %d pts_time: %s\n",\
  64. c->w, c->h, c->pix_fmt, width, height, format, av_ts2timestr(pts, &s->outputs[0]->time_base));\
  65. av_log(s, AV_LOG_WARNING, "Changing video frame properties on the fly is not supported by all filters.\n");\
  66. }
  67. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, ch_count, format, pts)\
  68. if (c->sample_fmt != format || c->sample_rate != srate ||\
  69. c->channel_layout != ch_layout || c->channels != ch_count) {\
  70. av_log(s, AV_LOG_INFO, "filter context - fmt: %s r: %d layout: %"PRIX64" ch: %d, incoming frame - fmt: %s r: %d layout: %"PRIX64" ch: %d pts_time: %s\n",\
  71. av_get_sample_fmt_name(c->sample_fmt), c->sample_rate, c->channel_layout, c->channels,\
  72. av_get_sample_fmt_name(format), srate, ch_layout, ch_count, av_ts2timestr(pts, &s->outputs[0]->time_base));\
  73. av_log(s, AV_LOG_ERROR, "Changing audio frame properties on the fly is not supported.\n");\
  74. return AVERROR(EINVAL);\
  75. }
  76. AVBufferSrcParameters *av_buffersrc_parameters_alloc(void)
  77. {
  78. AVBufferSrcParameters *par = av_mallocz(sizeof(*par));
  79. if (!par)
  80. return NULL;
  81. par->format = -1;
  82. return par;
  83. }
  84. int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param)
  85. {
  86. BufferSourceContext *s = ctx->priv;
  87. if (param->time_base.num > 0 && param->time_base.den > 0)
  88. s->time_base = param->time_base;
  89. switch (ctx->filter->outputs[0].type) {
  90. case AVMEDIA_TYPE_VIDEO:
  91. if (param->format != AV_PIX_FMT_NONE) {
  92. s->pix_fmt = param->format;
  93. }
  94. if (param->width > 0)
  95. s->w = param->width;
  96. if (param->height > 0)
  97. s->h = param->height;
  98. if (param->sample_aspect_ratio.num > 0 && param->sample_aspect_ratio.den > 0)
  99. s->pixel_aspect = param->sample_aspect_ratio;
  100. if (param->frame_rate.num > 0 && param->frame_rate.den > 0)
  101. s->frame_rate = param->frame_rate;
  102. if (param->hw_frames_ctx) {
  103. av_buffer_unref(&s->hw_frames_ctx);
  104. s->hw_frames_ctx = av_buffer_ref(param->hw_frames_ctx);
  105. if (!s->hw_frames_ctx)
  106. return AVERROR(ENOMEM);
  107. }
  108. break;
  109. case AVMEDIA_TYPE_AUDIO:
  110. if (param->format != AV_SAMPLE_FMT_NONE) {
  111. s->sample_fmt = param->format;
  112. }
  113. if (param->sample_rate > 0)
  114. s->sample_rate = param->sample_rate;
  115. if (param->channel_layout)
  116. s->channel_layout = param->channel_layout;
  117. break;
  118. default:
  119. return AVERROR_BUG;
  120. }
  121. return 0;
  122. }
  123. int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  124. {
  125. return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
  126. AV_BUFFERSRC_FLAG_KEEP_REF);
  127. }
  128. int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
  129. {
  130. return av_buffersrc_add_frame_flags(ctx, frame, 0);
  131. }
  132. static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  133. AVFrame *frame, int flags);
  134. int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  135. {
  136. AVFrame *copy = NULL;
  137. int ret = 0;
  138. if (frame && frame->channel_layout &&
  139. av_get_channel_layout_nb_channels(frame->channel_layout) != frame->channels) {
  140. av_log(ctx, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  141. return AVERROR(EINVAL);
  142. }
  143. if (!(flags & AV_BUFFERSRC_FLAG_KEEP_REF) || !frame)
  144. return av_buffersrc_add_frame_internal(ctx, frame, flags);
  145. if (!(copy = av_frame_alloc()))
  146. return AVERROR(ENOMEM);
  147. ret = av_frame_ref(copy, frame);
  148. if (ret >= 0)
  149. ret = av_buffersrc_add_frame_internal(ctx, copy, flags);
  150. av_frame_free(&copy);
  151. return ret;
  152. }
  153. static int push_frame(AVFilterGraph *graph)
  154. {
  155. int ret;
  156. while (1) {
  157. ret = ff_filter_graph_run_once(graph);
  158. if (ret == AVERROR(EAGAIN))
  159. break;
  160. if (ret < 0)
  161. return ret;
  162. }
  163. return 0;
  164. }
  165. static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  166. AVFrame *frame, int flags)
  167. {
  168. BufferSourceContext *s = ctx->priv;
  169. AVFrame *copy;
  170. int refcounted, ret;
  171. s->nb_failed_requests = 0;
  172. if (!frame)
  173. return av_buffersrc_close(ctx, AV_NOPTS_VALUE, flags);
  174. if (s->eof)
  175. return AVERROR(EINVAL);
  176. refcounted = !!frame->buf[0];
  177. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  178. switch (ctx->outputs[0]->type) {
  179. case AVMEDIA_TYPE_VIDEO:
  180. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  181. frame->format, frame->pts);
  182. break;
  183. case AVMEDIA_TYPE_AUDIO:
  184. /* For layouts unknown on input but known on link after negotiation. */
  185. if (!frame->channel_layout)
  186. frame->channel_layout = s->channel_layout;
  187. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  188. frame->channels, frame->format, frame->pts);
  189. break;
  190. default:
  191. return AVERROR(EINVAL);
  192. }
  193. }
  194. if (!(copy = av_frame_alloc()))
  195. return AVERROR(ENOMEM);
  196. if (refcounted) {
  197. av_frame_move_ref(copy, frame);
  198. } else {
  199. ret = av_frame_ref(copy, frame);
  200. if (ret < 0) {
  201. av_frame_free(&copy);
  202. return ret;
  203. }
  204. }
  205. ret = ff_filter_frame(ctx->outputs[0], copy);
  206. if (ret < 0) {
  207. av_frame_free(&copy);
  208. return ret;
  209. }
  210. if ((flags & AV_BUFFERSRC_FLAG_PUSH)) {
  211. ret = push_frame(ctx->graph);
  212. if (ret < 0)
  213. return ret;
  214. }
  215. return 0;
  216. }
  217. int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
  218. {
  219. BufferSourceContext *s = ctx->priv;
  220. s->eof = 1;
  221. ff_avfilter_link_set_in_status(ctx->outputs[0], AVERROR_EOF, pts);
  222. return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
  223. }
  224. static av_cold int init_video(AVFilterContext *ctx)
  225. {
  226. BufferSourceContext *c = ctx->priv;
  227. if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h ||
  228. av_q2d(c->time_base) <= 0) {
  229. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  230. return AVERROR(EINVAL);
  231. }
  232. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d\n",
  233. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  234. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  235. c->pixel_aspect.num, c->pixel_aspect.den);
  236. #if FF_API_SWS_PARAM_OPTION
  237. if (c->sws_param)
  238. av_log(ctx, AV_LOG_WARNING, "sws_param option is deprecated and ignored\n");
  239. #endif
  240. return 0;
  241. }
  242. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  243. {
  244. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  245. }
  246. #define OFFSET(x) offsetof(BufferSourceContext, x)
  247. #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  248. #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  249. static const AVOption buffer_options[] = {
  250. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  251. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
  252. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  253. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_NONE }, .min = AV_PIX_FMT_NONE, .max = INT_MAX, .flags = V },
  254. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  255. { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  256. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  257. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  258. #if FF_API_SWS_PARAM_OPTION
  259. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
  260. #endif
  261. { NULL },
  262. };
  263. AVFILTER_DEFINE_CLASS(buffer);
  264. static const AVOption abuffer_options[] = {
  265. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  266. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  267. { "sample_fmt", NULL, OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_NONE }, .min = AV_SAMPLE_FMT_NONE, .max = INT_MAX, .flags = A },
  268. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  269. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  270. { NULL },
  271. };
  272. AVFILTER_DEFINE_CLASS(abuffer);
  273. static av_cold int init_audio(AVFilterContext *ctx)
  274. {
  275. BufferSourceContext *s = ctx->priv;
  276. int ret = 0;
  277. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  278. av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
  279. return AVERROR(EINVAL);
  280. }
  281. if (s->channel_layout_str || s->channel_layout) {
  282. int n;
  283. if (!s->channel_layout) {
  284. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  285. if (!s->channel_layout) {
  286. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  287. s->channel_layout_str);
  288. return AVERROR(EINVAL);
  289. }
  290. }
  291. n = av_get_channel_layout_nb_channels(s->channel_layout);
  292. if (s->channels) {
  293. if (n != s->channels) {
  294. av_log(ctx, AV_LOG_ERROR,
  295. "Mismatching channel count %d and layout '%s' "
  296. "(%d channels)\n",
  297. s->channels, s->channel_layout_str, n);
  298. return AVERROR(EINVAL);
  299. }
  300. }
  301. s->channels = n;
  302. } else if (!s->channels) {
  303. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  304. "channel layout specified\n");
  305. return AVERROR(EINVAL);
  306. }
  307. if (!s->time_base.num)
  308. s->time_base = (AVRational){1, s->sample_rate};
  309. av_log(ctx, AV_LOG_VERBOSE,
  310. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  311. s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
  312. s->sample_rate, s->channel_layout_str);
  313. return ret;
  314. }
  315. static av_cold void uninit(AVFilterContext *ctx)
  316. {
  317. BufferSourceContext *s = ctx->priv;
  318. av_buffer_unref(&s->hw_frames_ctx);
  319. }
  320. static int query_formats(AVFilterContext *ctx)
  321. {
  322. BufferSourceContext *c = ctx->priv;
  323. AVFilterChannelLayouts *channel_layouts = NULL;
  324. AVFilterFormats *formats = NULL;
  325. AVFilterFormats *samplerates = NULL;
  326. int ret;
  327. switch (ctx->outputs[0]->type) {
  328. case AVMEDIA_TYPE_VIDEO:
  329. if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
  330. (ret = ff_set_common_formats (ctx , formats )) < 0)
  331. return ret;
  332. break;
  333. case AVMEDIA_TYPE_AUDIO:
  334. if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
  335. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  336. (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
  337. (ret = ff_set_common_samplerates (ctx , samplerates )) < 0)
  338. return ret;
  339. if ((ret = ff_add_channel_layout(&channel_layouts,
  340. c->channel_layout ? c->channel_layout :
  341. FF_COUNT2LAYOUT(c->channels))) < 0)
  342. return ret;
  343. if ((ret = ff_set_common_channel_layouts(ctx, channel_layouts)) < 0)
  344. return ret;
  345. break;
  346. default:
  347. return AVERROR(EINVAL);
  348. }
  349. return 0;
  350. }
  351. static int config_props(AVFilterLink *link)
  352. {
  353. BufferSourceContext *c = link->src->priv;
  354. switch (link->type) {
  355. case AVMEDIA_TYPE_VIDEO:
  356. link->w = c->w;
  357. link->h = c->h;
  358. link->sample_aspect_ratio = c->pixel_aspect;
  359. if (c->hw_frames_ctx) {
  360. link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
  361. if (!link->hw_frames_ctx)
  362. return AVERROR(ENOMEM);
  363. }
  364. break;
  365. case AVMEDIA_TYPE_AUDIO:
  366. if (!c->channel_layout)
  367. c->channel_layout = link->channel_layout;
  368. break;
  369. default:
  370. return AVERROR(EINVAL);
  371. }
  372. link->time_base = c->time_base;
  373. link->frame_rate = c->frame_rate;
  374. return 0;
  375. }
  376. static int request_frame(AVFilterLink *link)
  377. {
  378. BufferSourceContext *c = link->src->priv;
  379. if (c->eof)
  380. return AVERROR_EOF;
  381. c->nb_failed_requests++;
  382. return AVERROR(EAGAIN);
  383. }
  384. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  385. {
  386. .name = "default",
  387. .type = AVMEDIA_TYPE_VIDEO,
  388. .request_frame = request_frame,
  389. .config_props = config_props,
  390. },
  391. { NULL }
  392. };
  393. AVFilter ff_vsrc_buffer = {
  394. .name = "buffer",
  395. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  396. .priv_size = sizeof(BufferSourceContext),
  397. .query_formats = query_formats,
  398. .init = init_video,
  399. .uninit = uninit,
  400. .inputs = NULL,
  401. .outputs = avfilter_vsrc_buffer_outputs,
  402. .priv_class = &buffer_class,
  403. };
  404. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  405. {
  406. .name = "default",
  407. .type = AVMEDIA_TYPE_AUDIO,
  408. .request_frame = request_frame,
  409. .config_props = config_props,
  410. },
  411. { NULL }
  412. };
  413. AVFilter ff_asrc_abuffer = {
  414. .name = "abuffer",
  415. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  416. .priv_size = sizeof(BufferSourceContext),
  417. .query_formats = query_formats,
  418. .init = init_audio,
  419. .uninit = uninit,
  420. .inputs = NULL,
  421. .outputs = avfilter_asrc_abuffer_outputs,
  422. .priv_class = &abuffer_class,
  423. };