buffersrc.c 14 KB

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