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.

478 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 push_frame(AVFilterGraph *graph)
  133. {
  134. int ret;
  135. while (1) {
  136. ret = ff_filter_graph_run_once(graph);
  137. if (ret == AVERROR(EAGAIN))
  138. break;
  139. if (ret < 0)
  140. return ret;
  141. }
  142. return 0;
  143. }
  144. int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  145. {
  146. BufferSourceContext *s = ctx->priv;
  147. AVFrame *copy;
  148. int refcounted, ret;
  149. if (frame && frame->channel_layout &&
  150. av_get_channel_layout_nb_channels(frame->channel_layout) != frame->channels) {
  151. av_log(ctx, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  152. return AVERROR(EINVAL);
  153. }
  154. s->nb_failed_requests = 0;
  155. if (!frame)
  156. return av_buffersrc_close(ctx, AV_NOPTS_VALUE, flags);
  157. if (s->eof)
  158. return AVERROR(EINVAL);
  159. refcounted = !!frame->buf[0];
  160. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  161. switch (ctx->outputs[0]->type) {
  162. case AVMEDIA_TYPE_VIDEO:
  163. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  164. frame->format, frame->pts);
  165. break;
  166. case AVMEDIA_TYPE_AUDIO:
  167. /* For layouts unknown on input but known on link after negotiation. */
  168. if (!frame->channel_layout)
  169. frame->channel_layout = s->channel_layout;
  170. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  171. frame->channels, frame->format, frame->pts);
  172. break;
  173. default:
  174. return AVERROR(EINVAL);
  175. }
  176. }
  177. if (!(copy = av_frame_alloc()))
  178. return AVERROR(ENOMEM);
  179. if (refcounted && !(flags & AV_BUFFERSRC_FLAG_KEEP_REF)) {
  180. av_frame_move_ref(copy, frame);
  181. } else {
  182. ret = av_frame_ref(copy, frame);
  183. if (ret < 0) {
  184. av_frame_free(&copy);
  185. return ret;
  186. }
  187. }
  188. ret = ff_filter_frame(ctx->outputs[0], copy);
  189. if (ret < 0)
  190. return ret;
  191. if ((flags & AV_BUFFERSRC_FLAG_PUSH)) {
  192. ret = push_frame(ctx->graph);
  193. if (ret < 0)
  194. return ret;
  195. }
  196. return 0;
  197. }
  198. int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
  199. {
  200. BufferSourceContext *s = ctx->priv;
  201. s->eof = 1;
  202. ff_avfilter_link_set_in_status(ctx->outputs[0], AVERROR_EOF, pts);
  203. return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
  204. }
  205. static av_cold int init_video(AVFilterContext *ctx)
  206. {
  207. BufferSourceContext *c = ctx->priv;
  208. if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h ||
  209. av_q2d(c->time_base) <= 0) {
  210. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  211. return AVERROR(EINVAL);
  212. }
  213. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d\n",
  214. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  215. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  216. c->pixel_aspect.num, c->pixel_aspect.den);
  217. #if FF_API_SWS_PARAM_OPTION
  218. if (c->sws_param)
  219. av_log(ctx, AV_LOG_WARNING, "sws_param option is deprecated and ignored\n");
  220. #endif
  221. return 0;
  222. }
  223. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  224. {
  225. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  226. }
  227. #define OFFSET(x) offsetof(BufferSourceContext, x)
  228. #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  229. #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  230. static const AVOption buffer_options[] = {
  231. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  232. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
  233. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  234. { "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 },
  235. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  236. { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  237. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  238. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  239. #if FF_API_SWS_PARAM_OPTION
  240. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
  241. #endif
  242. { NULL },
  243. };
  244. AVFILTER_DEFINE_CLASS(buffer);
  245. static const AVOption abuffer_options[] = {
  246. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  247. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  248. { "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 },
  249. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  250. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  251. { NULL },
  252. };
  253. AVFILTER_DEFINE_CLASS(abuffer);
  254. static av_cold int init_audio(AVFilterContext *ctx)
  255. {
  256. BufferSourceContext *s = ctx->priv;
  257. int ret = 0;
  258. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  259. av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
  260. return AVERROR(EINVAL);
  261. }
  262. if (s->channel_layout_str || s->channel_layout) {
  263. int n;
  264. if (!s->channel_layout) {
  265. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  266. if (!s->channel_layout) {
  267. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  268. s->channel_layout_str);
  269. return AVERROR(EINVAL);
  270. }
  271. }
  272. n = av_get_channel_layout_nb_channels(s->channel_layout);
  273. if (s->channels) {
  274. if (n != s->channels) {
  275. av_log(ctx, AV_LOG_ERROR,
  276. "Mismatching channel count %d and layout '%s' "
  277. "(%d channels)\n",
  278. s->channels, s->channel_layout_str, n);
  279. return AVERROR(EINVAL);
  280. }
  281. }
  282. s->channels = n;
  283. } else if (!s->channels) {
  284. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  285. "channel layout specified\n");
  286. return AVERROR(EINVAL);
  287. }
  288. if (!s->time_base.num)
  289. s->time_base = (AVRational){1, s->sample_rate};
  290. av_log(ctx, AV_LOG_VERBOSE,
  291. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  292. s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
  293. s->sample_rate, s->channel_layout_str);
  294. return ret;
  295. }
  296. static av_cold void uninit(AVFilterContext *ctx)
  297. {
  298. BufferSourceContext *s = ctx->priv;
  299. av_buffer_unref(&s->hw_frames_ctx);
  300. }
  301. static int query_formats(AVFilterContext *ctx)
  302. {
  303. BufferSourceContext *c = ctx->priv;
  304. AVFilterChannelLayouts *channel_layouts = NULL;
  305. AVFilterFormats *formats = NULL;
  306. AVFilterFormats *samplerates = NULL;
  307. int ret;
  308. switch (ctx->outputs[0]->type) {
  309. case AVMEDIA_TYPE_VIDEO:
  310. if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
  311. (ret = ff_set_common_formats (ctx , formats )) < 0)
  312. return ret;
  313. break;
  314. case AVMEDIA_TYPE_AUDIO:
  315. if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
  316. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  317. (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
  318. (ret = ff_set_common_samplerates (ctx , samplerates )) < 0)
  319. return ret;
  320. if ((ret = ff_add_channel_layout(&channel_layouts,
  321. c->channel_layout ? c->channel_layout :
  322. FF_COUNT2LAYOUT(c->channels))) < 0)
  323. return ret;
  324. if ((ret = ff_set_common_channel_layouts(ctx, channel_layouts)) < 0)
  325. return ret;
  326. break;
  327. default:
  328. return AVERROR(EINVAL);
  329. }
  330. return 0;
  331. }
  332. static int config_props(AVFilterLink *link)
  333. {
  334. BufferSourceContext *c = link->src->priv;
  335. switch (link->type) {
  336. case AVMEDIA_TYPE_VIDEO:
  337. link->w = c->w;
  338. link->h = c->h;
  339. link->sample_aspect_ratio = c->pixel_aspect;
  340. if (c->hw_frames_ctx) {
  341. link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
  342. if (!link->hw_frames_ctx)
  343. return AVERROR(ENOMEM);
  344. }
  345. break;
  346. case AVMEDIA_TYPE_AUDIO:
  347. if (!c->channel_layout)
  348. c->channel_layout = link->channel_layout;
  349. break;
  350. default:
  351. return AVERROR(EINVAL);
  352. }
  353. link->time_base = c->time_base;
  354. link->frame_rate = c->frame_rate;
  355. return 0;
  356. }
  357. static int request_frame(AVFilterLink *link)
  358. {
  359. BufferSourceContext *c = link->src->priv;
  360. if (c->eof)
  361. return AVERROR_EOF;
  362. c->nb_failed_requests++;
  363. return AVERROR(EAGAIN);
  364. }
  365. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  366. {
  367. .name = "default",
  368. .type = AVMEDIA_TYPE_VIDEO,
  369. .request_frame = request_frame,
  370. .config_props = config_props,
  371. },
  372. { NULL }
  373. };
  374. AVFilter ff_vsrc_buffer = {
  375. .name = "buffer",
  376. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  377. .priv_size = sizeof(BufferSourceContext),
  378. .query_formats = query_formats,
  379. .init = init_video,
  380. .uninit = uninit,
  381. .inputs = NULL,
  382. .outputs = avfilter_vsrc_buffer_outputs,
  383. .priv_class = &buffer_class,
  384. };
  385. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  386. {
  387. .name = "default",
  388. .type = AVMEDIA_TYPE_AUDIO,
  389. .request_frame = request_frame,
  390. .config_props = config_props,
  391. },
  392. { NULL }
  393. };
  394. AVFilter ff_asrc_abuffer = {
  395. .name = "abuffer",
  396. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  397. .priv_size = sizeof(BufferSourceContext),
  398. .query_formats = query_formats,
  399. .init = init_audio,
  400. .uninit = uninit,
  401. .inputs = NULL,
  402. .outputs = avfilter_asrc_abuffer_outputs,
  403. .priv_class = &abuffer_class,
  404. };