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.

494 lines
15KB

  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_frame_unref(frame); \
  170. ret = AVERROR(ENOMEM); \
  171. goto fail; \
  172. } \
  173. } while (0)
  174. if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  175. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  176. planes = av_pix_fmt_count_planes(frame->format);
  177. if (!desc || planes <= 0) {
  178. ret = AVERROR(EINVAL);
  179. goto fail;
  180. }
  181. for (i = 0; i < planes; i++) {
  182. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  183. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  184. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  185. }
  186. } else {
  187. int planar = av_sample_fmt_is_planar(frame->format);
  188. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  189. planes = planar ? channels : 1;
  190. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  191. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  192. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  193. frame->nb_extended_buf);
  194. if (!frame->extended_buf) {
  195. ret = AVERROR(ENOMEM);
  196. goto fail;
  197. }
  198. }
  199. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  200. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  201. for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
  202. WRAP_PLANE(frame->extended_buf[i],
  203. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  204. frame->linesize[0]);
  205. }
  206. ret = av_buffersrc_add_frame(ctx, frame);
  207. fail:
  208. av_buffer_unref(&dummy_buf);
  209. av_frame_free(&frame);
  210. return ret;
  211. }
  212. FF_ENABLE_DEPRECATION_WARNINGS
  213. #endif
  214. static av_cold int init_video(AVFilterContext *ctx)
  215. {
  216. BufferSourceContext *c = ctx->priv;
  217. if (!c->pix_fmt_str || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
  218. av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
  219. return AVERROR(EINVAL);
  220. }
  221. if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) {
  222. char *tail;
  223. c->pix_fmt = strtol(c->pix_fmt_str, &tail, 10);
  224. if (*tail || c->pix_fmt < 0 || !av_pix_fmt_desc_get(c->pix_fmt)) {
  225. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", c->pix_fmt_str);
  226. return AVERROR(EINVAL);
  227. }
  228. }
  229. if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  230. return AVERROR(ENOMEM);
  231. 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));
  232. return 0;
  233. }
  234. #define OFFSET(x) offsetof(BufferSourceContext, x)
  235. #define A AV_OPT_FLAG_AUDIO_PARAM
  236. #define V AV_OPT_FLAG_VIDEO_PARAM
  237. static const AVOption video_options[] = {
  238. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  239. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  240. { "pix_fmt", NULL, OFFSET(pix_fmt_str), AV_OPT_TYPE_STRING, .flags = V },
  241. #if FF_API_OLD_FILTER_OPTS
  242. /* those 4 are for compatibility with the old option passing system where each filter
  243. * did its own parsing */
  244. { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  245. { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  246. { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  247. { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  248. #endif
  249. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
  250. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  251. { NULL },
  252. };
  253. static const AVClass buffer_class = {
  254. .class_name = "buffer source",
  255. .item_name = av_default_item_name,
  256. .option = video_options,
  257. .version = LIBAVUTIL_VERSION_INT,
  258. };
  259. static const AVOption audio_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_str), AV_OPT_TYPE_STRING, .flags = A },
  263. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  264. { NULL },
  265. };
  266. static const AVClass abuffer_class = {
  267. .class_name = "abuffer source",
  268. .item_name = av_default_item_name,
  269. .option = audio_options,
  270. .version = LIBAVUTIL_VERSION_INT,
  271. };
  272. static av_cold int init_audio(AVFilterContext *ctx)
  273. {
  274. BufferSourceContext *s = ctx->priv;
  275. int ret = 0;
  276. s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
  277. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  278. av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
  279. s->sample_fmt_str);
  280. return AVERROR(EINVAL);
  281. }
  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. if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
  289. return AVERROR(ENOMEM);
  290. if (!s->time_base.num)
  291. s->time_base = (AVRational){1, s->sample_rate};
  292. av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
  293. "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
  294. s->sample_rate, s->channel_layout_str);
  295. return ret;
  296. }
  297. static av_cold void uninit(AVFilterContext *ctx)
  298. {
  299. BufferSourceContext *s = ctx->priv;
  300. while (s->fifo && av_fifo_size(s->fifo)) {
  301. AVFrame *frame;
  302. av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
  303. av_frame_free(&frame);
  304. }
  305. av_fifo_free(s->fifo);
  306. s->fifo = NULL;
  307. }
  308. static int query_formats(AVFilterContext *ctx)
  309. {
  310. BufferSourceContext *c = ctx->priv;
  311. AVFilterChannelLayouts *channel_layouts = NULL;
  312. AVFilterFormats *formats = NULL;
  313. AVFilterFormats *samplerates = NULL;
  314. switch (ctx->outputs[0]->type) {
  315. case AVMEDIA_TYPE_VIDEO:
  316. ff_add_format(&formats, c->pix_fmt);
  317. ff_set_common_formats(ctx, formats);
  318. break;
  319. case AVMEDIA_TYPE_AUDIO:
  320. ff_add_format(&formats, c->sample_fmt);
  321. ff_set_common_formats(ctx, formats);
  322. ff_add_format(&samplerates, c->sample_rate);
  323. ff_set_common_samplerates(ctx, samplerates);
  324. ff_add_channel_layout(&channel_layouts, c->channel_layout);
  325. ff_set_common_channel_layouts(ctx, channel_layouts);
  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. break;
  341. case AVMEDIA_TYPE_AUDIO:
  342. link->channel_layout = c->channel_layout;
  343. link->sample_rate = c->sample_rate;
  344. break;
  345. default:
  346. return AVERROR(EINVAL);
  347. }
  348. link->time_base = c->time_base;
  349. return 0;
  350. }
  351. static int request_frame(AVFilterLink *link)
  352. {
  353. BufferSourceContext *c = link->src->priv;
  354. AVFrame *frame;
  355. int ret = 0;
  356. if (!av_fifo_size(c->fifo)) {
  357. if (c->eof)
  358. return AVERROR_EOF;
  359. return AVERROR(EAGAIN);
  360. }
  361. av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  362. ff_filter_frame(link, frame);
  363. return ret;
  364. }
  365. static int poll_frame(AVFilterLink *link)
  366. {
  367. BufferSourceContext *c = link->src->priv;
  368. int size = av_fifo_size(c->fifo);
  369. if (!size && c->eof)
  370. return AVERROR_EOF;
  371. return size/sizeof(AVFrame*);
  372. }
  373. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  374. {
  375. .name = "default",
  376. .type = AVMEDIA_TYPE_VIDEO,
  377. .request_frame = request_frame,
  378. .poll_frame = poll_frame,
  379. .config_props = config_props,
  380. },
  381. { NULL }
  382. };
  383. AVFilter ff_vsrc_buffer = {
  384. .name = "buffer",
  385. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  386. .priv_size = sizeof(BufferSourceContext),
  387. .priv_class = &buffer_class,
  388. .query_formats = query_formats,
  389. .init = init_video,
  390. .uninit = uninit,
  391. .inputs = NULL,
  392. .outputs = avfilter_vsrc_buffer_outputs,
  393. };
  394. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  395. {
  396. .name = "default",
  397. .type = AVMEDIA_TYPE_AUDIO,
  398. .request_frame = request_frame,
  399. .poll_frame = poll_frame,
  400. .config_props = config_props,
  401. },
  402. { NULL }
  403. };
  404. AVFilter ff_asrc_abuffer = {
  405. .name = "abuffer",
  406. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  407. .priv_size = sizeof(BufferSourceContext),
  408. .priv_class = &abuffer_class,
  409. .query_formats = query_formats,
  410. .init = init_audio,
  411. .uninit = uninit,
  412. .inputs = NULL,
  413. .outputs = avfilter_asrc_abuffer_outputs,
  414. };