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.

500 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. return ret;
  208. if ((flags & AV_BUFFERSRC_FLAG_PUSH)) {
  209. ret = push_frame(ctx->graph);
  210. if (ret < 0)
  211. return ret;
  212. }
  213. return 0;
  214. }
  215. int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
  216. {
  217. BufferSourceContext *s = ctx->priv;
  218. s->eof = 1;
  219. ff_avfilter_link_set_in_status(ctx->outputs[0], AVERROR_EOF, pts);
  220. return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
  221. }
  222. static av_cold int init_video(AVFilterContext *ctx)
  223. {
  224. BufferSourceContext *c = ctx->priv;
  225. if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h ||
  226. av_q2d(c->time_base) <= 0) {
  227. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  228. return AVERROR(EINVAL);
  229. }
  230. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d\n",
  231. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  232. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  233. c->pixel_aspect.num, c->pixel_aspect.den);
  234. #if FF_API_SWS_PARAM_OPTION
  235. if (c->sws_param)
  236. av_log(ctx, AV_LOG_WARNING, "sws_param option is deprecated and ignored\n");
  237. #endif
  238. return 0;
  239. }
  240. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  241. {
  242. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  243. }
  244. #define OFFSET(x) offsetof(BufferSourceContext, x)
  245. #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  246. #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  247. static const AVOption buffer_options[] = {
  248. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  249. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
  250. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  251. { "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 },
  252. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  253. { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  254. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  255. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  256. #if FF_API_SWS_PARAM_OPTION
  257. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
  258. #endif
  259. { NULL },
  260. };
  261. AVFILTER_DEFINE_CLASS(buffer);
  262. static const AVOption abuffer_options[] = {
  263. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  264. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  265. { "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 },
  266. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  267. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  268. { NULL },
  269. };
  270. AVFILTER_DEFINE_CLASS(abuffer);
  271. static av_cold int init_audio(AVFilterContext *ctx)
  272. {
  273. BufferSourceContext *s = ctx->priv;
  274. int ret = 0;
  275. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  276. av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
  277. return AVERROR(EINVAL);
  278. }
  279. if (s->channel_layout_str || s->channel_layout) {
  280. int n;
  281. if (!s->channel_layout) {
  282. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  283. if (!s->channel_layout) {
  284. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  285. s->channel_layout_str);
  286. return AVERROR(EINVAL);
  287. }
  288. }
  289. n = av_get_channel_layout_nb_channels(s->channel_layout);
  290. if (s->channels) {
  291. if (n != s->channels) {
  292. av_log(ctx, AV_LOG_ERROR,
  293. "Mismatching channel count %d and layout '%s' "
  294. "(%d channels)\n",
  295. s->channels, s->channel_layout_str, n);
  296. return AVERROR(EINVAL);
  297. }
  298. }
  299. s->channels = n;
  300. } else if (!s->channels) {
  301. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  302. "channel layout specified\n");
  303. return AVERROR(EINVAL);
  304. }
  305. if (!s->time_base.num)
  306. s->time_base = (AVRational){1, s->sample_rate};
  307. av_log(ctx, AV_LOG_VERBOSE,
  308. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  309. s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
  310. s->sample_rate, s->channel_layout_str);
  311. return ret;
  312. }
  313. static av_cold void uninit(AVFilterContext *ctx)
  314. {
  315. BufferSourceContext *s = ctx->priv;
  316. av_buffer_unref(&s->hw_frames_ctx);
  317. }
  318. static int query_formats(AVFilterContext *ctx)
  319. {
  320. BufferSourceContext *c = ctx->priv;
  321. AVFilterChannelLayouts *channel_layouts = NULL;
  322. AVFilterFormats *formats = NULL;
  323. AVFilterFormats *samplerates = NULL;
  324. int ret;
  325. switch (ctx->outputs[0]->type) {
  326. case AVMEDIA_TYPE_VIDEO:
  327. if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
  328. (ret = ff_set_common_formats (ctx , formats )) < 0)
  329. return ret;
  330. break;
  331. case AVMEDIA_TYPE_AUDIO:
  332. if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
  333. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  334. (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
  335. (ret = ff_set_common_samplerates (ctx , samplerates )) < 0)
  336. return ret;
  337. if ((ret = ff_add_channel_layout(&channel_layouts,
  338. c->channel_layout ? c->channel_layout :
  339. FF_COUNT2LAYOUT(c->channels))) < 0)
  340. return ret;
  341. if ((ret = ff_set_common_channel_layouts(ctx, channel_layouts)) < 0)
  342. return ret;
  343. break;
  344. default:
  345. return AVERROR(EINVAL);
  346. }
  347. return 0;
  348. }
  349. static int config_props(AVFilterLink *link)
  350. {
  351. BufferSourceContext *c = link->src->priv;
  352. switch (link->type) {
  353. case AVMEDIA_TYPE_VIDEO:
  354. link->w = c->w;
  355. link->h = c->h;
  356. link->sample_aspect_ratio = c->pixel_aspect;
  357. if (c->hw_frames_ctx) {
  358. link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
  359. if (!link->hw_frames_ctx)
  360. return AVERROR(ENOMEM);
  361. }
  362. break;
  363. case AVMEDIA_TYPE_AUDIO:
  364. if (!c->channel_layout)
  365. c->channel_layout = link->channel_layout;
  366. break;
  367. default:
  368. return AVERROR(EINVAL);
  369. }
  370. link->time_base = c->time_base;
  371. link->frame_rate = c->frame_rate;
  372. return 0;
  373. }
  374. static int request_frame(AVFilterLink *link)
  375. {
  376. BufferSourceContext *c = link->src->priv;
  377. if (c->eof)
  378. return AVERROR_EOF;
  379. c->nb_failed_requests++;
  380. return AVERROR(EAGAIN);
  381. }
  382. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  383. {
  384. .name = "default",
  385. .type = AVMEDIA_TYPE_VIDEO,
  386. .request_frame = request_frame,
  387. .config_props = config_props,
  388. },
  389. { NULL }
  390. };
  391. AVFilter ff_vsrc_buffer = {
  392. .name = "buffer",
  393. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  394. .priv_size = sizeof(BufferSourceContext),
  395. .query_formats = query_formats,
  396. .init = init_video,
  397. .uninit = uninit,
  398. .inputs = NULL,
  399. .outputs = avfilter_vsrc_buffer_outputs,
  400. .priv_class = &buffer_class,
  401. };
  402. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  403. {
  404. .name = "default",
  405. .type = AVMEDIA_TYPE_AUDIO,
  406. .request_frame = request_frame,
  407. .config_props = config_props,
  408. },
  409. { NULL }
  410. };
  411. AVFilter ff_asrc_abuffer = {
  412. .name = "abuffer",
  413. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  414. .priv_size = sizeof(BufferSourceContext),
  415. .query_formats = query_formats,
  416. .init = init_audio,
  417. .uninit = uninit,
  418. .inputs = NULL,
  419. .outputs = avfilter_asrc_abuffer_outputs,
  420. .priv_class = &abuffer_class,
  421. };