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.

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