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.

550 lines
18KB

  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/channel_layout.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/fifo.h"
  28. #include "libavutil/frame.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/samplefmt.h"
  32. #include "audio.h"
  33. #include "avfilter.h"
  34. #include "buffersrc.h"
  35. #include "formats.h"
  36. #include "internal.h"
  37. #include "video.h"
  38. #include "avcodec.h"
  39. typedef struct {
  40. const AVClass *class;
  41. AVFifoBuffer *fifo;
  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. unsigned warning_limit;
  46. /* video only */
  47. int w, h;
  48. enum AVPixelFormat pix_fmt;
  49. char *pix_fmt_str;
  50. AVRational pixel_aspect;
  51. char *sws_param;
  52. /* audio only */
  53. int sample_rate;
  54. enum AVSampleFormat sample_fmt;
  55. char *sample_fmt_str;
  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)\
  62. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  63. av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
  64. }
  65. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, ch_count, format)\
  66. if (c->sample_fmt != format || c->sample_rate != srate ||\
  67. c->channel_layout != ch_layout || c->channels != ch_count) {\
  68. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  69. return AVERROR(EINVAL);\
  70. }
  71. int av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  72. {
  73. return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
  74. AV_BUFFERSRC_FLAG_KEEP_REF);
  75. }
  76. int av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
  77. {
  78. return av_buffersrc_add_frame_flags(ctx, frame, 0);
  79. }
  80. static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  81. AVFrame *frame, int flags);
  82. int av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  83. {
  84. AVFrame *copy = NULL;
  85. int ret = 0;
  86. if (frame && frame->channel_layout &&
  87. av_get_channel_layout_nb_channels(frame->channel_layout) != av_frame_get_channels(frame)) {
  88. av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  89. return AVERROR(EINVAL);
  90. }
  91. if (!(flags & AV_BUFFERSRC_FLAG_KEEP_REF) || !frame)
  92. return av_buffersrc_add_frame_internal(ctx, frame, flags);
  93. if (!(copy = av_frame_alloc()))
  94. return AVERROR(ENOMEM);
  95. ret = av_frame_ref(copy, frame);
  96. if (ret >= 0)
  97. ret = av_buffersrc_add_frame_internal(ctx, copy, flags);
  98. av_frame_free(&copy);
  99. return ret;
  100. }
  101. static int attribute_align_arg av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  102. AVFrame *frame, int flags)
  103. {
  104. BufferSourceContext *s = ctx->priv;
  105. AVFrame *copy;
  106. int ret;
  107. s->nb_failed_requests = 0;
  108. if (!frame) {
  109. s->eof = 1;
  110. return 0;
  111. } else if (s->eof)
  112. return AVERROR(EINVAL);
  113. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  114. switch (ctx->outputs[0]->type) {
  115. case AVMEDIA_TYPE_VIDEO:
  116. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  117. frame->format);
  118. break;
  119. case AVMEDIA_TYPE_AUDIO:
  120. /* For layouts unknown on input but known on link after negotiation. */
  121. if (!frame->channel_layout)
  122. frame->channel_layout = s->channel_layout;
  123. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
  124. av_frame_get_channels(frame), frame->format);
  125. break;
  126. default:
  127. return AVERROR(EINVAL);
  128. }
  129. }
  130. if (!av_fifo_space(s->fifo) &&
  131. (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
  132. sizeof(copy))) < 0)
  133. return ret;
  134. if (!(copy = av_frame_alloc()))
  135. return AVERROR(ENOMEM);
  136. av_frame_move_ref(copy, frame);
  137. if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
  138. av_frame_move_ref(frame, copy);
  139. av_frame_free(&copy);
  140. return ret;
  141. }
  142. if ((flags & AV_BUFFERSRC_FLAG_PUSH))
  143. if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
  144. return ret;
  145. return 0;
  146. }
  147. #if FF_API_AVFILTERBUFFER
  148. static void compat_free_buffer(void *opaque, uint8_t *data)
  149. {
  150. AVFilterBufferRef *buf = opaque;
  151. AV_NOWARN_DEPRECATED(
  152. avfilter_unref_buffer(buf);
  153. )
  154. }
  155. static void compat_unref_buffer(void *opaque, uint8_t *data)
  156. {
  157. AVBufferRef *buf = opaque;
  158. AV_NOWARN_DEPRECATED(
  159. av_buffer_unref(&buf);
  160. )
  161. }
  162. int av_buffersrc_add_ref(AVFilterContext *ctx, AVFilterBufferRef *buf,
  163. int flags)
  164. {
  165. BufferSourceContext *s = ctx->priv;
  166. AVFrame *frame = NULL;
  167. AVBufferRef *dummy_buf = NULL;
  168. int ret = 0, planes, i;
  169. if (!buf) {
  170. s->eof = 1;
  171. return 0;
  172. } else if (s->eof)
  173. return AVERROR(EINVAL);
  174. frame = av_frame_alloc();
  175. if (!frame)
  176. return AVERROR(ENOMEM);
  177. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf,
  178. (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);
  179. if (!dummy_buf) {
  180. ret = AVERROR(ENOMEM);
  181. goto fail;
  182. }
  183. AV_NOWARN_DEPRECATED(
  184. if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
  185. goto fail;
  186. )
  187. #define WRAP_PLANE(ref_out, data, data_size) \
  188. do { \
  189. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  190. if (!dummy_ref) { \
  191. ret = AVERROR(ENOMEM); \
  192. goto fail; \
  193. } \
  194. ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
  195. dummy_ref, (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY); \
  196. if (!ref_out) { \
  197. av_frame_unref(frame); \
  198. ret = AVERROR(ENOMEM); \
  199. goto fail; \
  200. } \
  201. } while (0)
  202. if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  203. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  204. planes = av_pix_fmt_count_planes(frame->format);
  205. if (!desc || planes <= 0) {
  206. ret = AVERROR(EINVAL);
  207. goto fail;
  208. }
  209. for (i = 0; i < planes; i++) {
  210. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  211. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  212. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  213. }
  214. } else {
  215. int planar = av_sample_fmt_is_planar(frame->format);
  216. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  217. planes = planar ? channels : 1;
  218. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  219. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  220. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  221. frame->nb_extended_buf);
  222. if (!frame->extended_buf) {
  223. ret = AVERROR(ENOMEM);
  224. goto fail;
  225. }
  226. }
  227. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  228. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  229. for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
  230. WRAP_PLANE(frame->extended_buf[i],
  231. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  232. frame->linesize[0]);
  233. }
  234. ret = av_buffersrc_add_frame_flags(ctx, frame, flags);
  235. fail:
  236. av_buffer_unref(&dummy_buf);
  237. av_frame_free(&frame);
  238. return ret;
  239. }
  240. int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
  241. {
  242. return av_buffersrc_add_ref(ctx, buf, 0);
  243. }
  244. #endif
  245. static av_cold int init_video(AVFilterContext *ctx)
  246. {
  247. BufferSourceContext *c = ctx->priv;
  248. if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
  249. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  250. return AVERROR(EINVAL);
  251. }
  252. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  253. return AVERROR(ENOMEM);
  254. 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",
  255. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  256. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  257. c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
  258. c->warning_limit = 100;
  259. return 0;
  260. }
  261. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  262. {
  263. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  264. }
  265. #define OFFSET(x) offsetof(BufferSourceContext, x)
  266. #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  267. #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  268. static const AVOption buffer_options[] = {
  269. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  270. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
  271. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  272. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = V },
  273. #if FF_API_OLD_FILTER_OPTS
  274. /* those 4 are for compatibility with the old option passing system where each filter
  275. * did its own parsing */
  276. { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  277. { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  278. { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  279. { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  280. #endif
  281. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
  282. { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
  283. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  284. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  285. { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
  286. { NULL },
  287. };
  288. AVFILTER_DEFINE_CLASS(buffer);
  289. static const AVOption abuffer_options[] = {
  290. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  291. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  292. { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
  293. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  294. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  295. { NULL },
  296. };
  297. AVFILTER_DEFINE_CLASS(abuffer);
  298. static av_cold int init_audio(AVFilterContext *ctx)
  299. {
  300. BufferSourceContext *s = ctx->priv;
  301. int ret = 0;
  302. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  303. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  304. av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s\n",
  305. s->sample_fmt_str);
  306. return AVERROR(EINVAL);
  307. }
  308. if (s->channel_layout_str) {
  309. int n;
  310. /* TODO reindent */
  311. s->channel_layout = av_get_channel_layout(s->channel_layout_str);
  312. if (!s->channel_layout) {
  313. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  314. s->channel_layout_str);
  315. return AVERROR(EINVAL);
  316. }
  317. n = av_get_channel_layout_nb_channels(s->channel_layout);
  318. if (s->channels) {
  319. if (n != s->channels) {
  320. av_log(ctx, AV_LOG_ERROR,
  321. "Mismatching channel count %d and layout '%s' "
  322. "(%d channels)\n",
  323. s->channels, s->channel_layout_str, n);
  324. return AVERROR(EINVAL);
  325. }
  326. }
  327. s->channels = n;
  328. } else if (!s->channels) {
  329. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  330. "channel layout specified\n");
  331. return AVERROR(EINVAL);
  332. }
  333. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  334. return AVERROR(ENOMEM);
  335. if (!s->time_base.num)
  336. s->time_base = (AVRational){1, s->sample_rate};
  337. av_log(ctx, AV_LOG_VERBOSE,
  338. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  339. s->time_base.num, s->time_base.den, s->sample_fmt_str,
  340. s->sample_rate, s->channel_layout_str);
  341. s->warning_limit = 100;
  342. return ret;
  343. }
  344. static av_cold void uninit(AVFilterContext *ctx)
  345. {
  346. BufferSourceContext *s = ctx->priv;
  347. while (s->fifo && av_fifo_size(s->fifo)) {
  348. AVFrame *frame;
  349. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  350. av_frame_free(&frame);
  351. }
  352. av_fifo_free(s->fifo);
  353. s->fifo = NULL;
  354. }
  355. static int query_formats(AVFilterContext *ctx)
  356. {
  357. BufferSourceContext *c = ctx->priv;
  358. AVFilterChannelLayouts *channel_layouts = NULL;
  359. AVFilterFormats *formats = NULL;
  360. AVFilterFormats *samplerates = NULL;
  361. switch (ctx->outputs[0]->type) {
  362. case AVMEDIA_TYPE_VIDEO:
  363. ff_add_format(&formats, c->pix_fmt);
  364. ff_set_common_formats(ctx, formats);
  365. break;
  366. case AVMEDIA_TYPE_AUDIO:
  367. ff_add_format(&formats, c->sample_fmt);
  368. ff_set_common_formats(ctx, formats);
  369. ff_add_format(&samplerates, c->sample_rate);
  370. ff_set_common_samplerates(ctx, samplerates);
  371. ff_add_channel_layout(&channel_layouts,
  372. c->channel_layout ? c->channel_layout :
  373. FF_COUNT2LAYOUT(c->channels));
  374. ff_set_common_channel_layouts(ctx, channel_layouts);
  375. break;
  376. default:
  377. return AVERROR(EINVAL);
  378. }
  379. return 0;
  380. }
  381. static int config_props(AVFilterLink *link)
  382. {
  383. BufferSourceContext *c = link->src->priv;
  384. switch (link->type) {
  385. case AVMEDIA_TYPE_VIDEO:
  386. link->w = c->w;
  387. link->h = c->h;
  388. link->sample_aspect_ratio = c->pixel_aspect;
  389. break;
  390. case AVMEDIA_TYPE_AUDIO:
  391. if (!c->channel_layout)
  392. c->channel_layout = link->channel_layout;
  393. break;
  394. default:
  395. return AVERROR(EINVAL);
  396. }
  397. link->time_base = c->time_base;
  398. link->frame_rate = c->frame_rate;
  399. return 0;
  400. }
  401. static int request_frame(AVFilterLink *link)
  402. {
  403. BufferSourceContext *c = link->src->priv;
  404. AVFrame *frame;
  405. if (!av_fifo_size(c->fifo)) {
  406. if (c->eof)
  407. return AVERROR_EOF;
  408. c->nb_failed_requests++;
  409. return AVERROR(EAGAIN);
  410. }
  411. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  412. return ff_filter_frame(link, frame);
  413. }
  414. static int poll_frame(AVFilterLink *link)
  415. {
  416. BufferSourceContext *c = link->src->priv;
  417. int size = av_fifo_size(c->fifo);
  418. if (!size && c->eof)
  419. return AVERROR_EOF;
  420. return size/sizeof(AVFrame*);
  421. }
  422. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  423. {
  424. .name = "default",
  425. .type = AVMEDIA_TYPE_VIDEO,
  426. .request_frame = request_frame,
  427. .poll_frame = poll_frame,
  428. .config_props = config_props,
  429. },
  430. { NULL }
  431. };
  432. AVFilter avfilter_vsrc_buffer = {
  433. .name = "buffer",
  434. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  435. .priv_size = sizeof(BufferSourceContext),
  436. .query_formats = query_formats,
  437. .init = init_video,
  438. .uninit = uninit,
  439. .inputs = NULL,
  440. .outputs = avfilter_vsrc_buffer_outputs,
  441. .priv_class = &buffer_class,
  442. };
  443. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  444. {
  445. .name = "default",
  446. .type = AVMEDIA_TYPE_AUDIO,
  447. .request_frame = request_frame,
  448. .poll_frame = poll_frame,
  449. .config_props = config_props,
  450. },
  451. { NULL }
  452. };
  453. AVFilter avfilter_asrc_abuffer = {
  454. .name = "abuffer",
  455. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  456. .priv_size = sizeof(BufferSourceContext),
  457. .query_formats = query_formats,
  458. .init = init_audio,
  459. .uninit = uninit,
  460. .inputs = NULL,
  461. .outputs = avfilter_asrc_abuffer_outputs,
  462. .priv_class = &abuffer_class,
  463. };