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.

495 lines
16KB

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