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.

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