buffersrc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/frame.h"
  28. #include "libavutil/hwcontext.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/internal.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/timestamp.h"
  34. #include "audio.h"
  35. #include "avfilter.h"
  36. #include "buffersrc.h"
  37. #include "filters.h"
  38. #include "formats.h"
  39. #include "internal.h"
  40. #include "video.h"
  41. typedef struct BufferSourceContext {
  42. const AVClass *class;
  43. AVRational time_base; ///< time_base to set in the output link
  44. AVRational frame_rate; ///< frame_rate to set in the output link
  45. unsigned nb_failed_requests;
  46. /* video only */
  47. int w, h, prev_w, prev_h;
  48. enum AVPixelFormat pix_fmt, prev_pix_fmt;
  49. enum AVColorSpace color_space, prev_color_space;
  50. enum AVColorRange color_range, prev_color_range;
  51. AVRational pixel_aspect;
  52. AVBufferRef *hw_frames_ctx;
  53. /* audio only */
  54. int sample_rate;
  55. enum AVSampleFormat sample_fmt;
  56. int channels;
  57. char *channel_layout_str;
  58. AVChannelLayout ch_layout;
  59. int eof;
  60. int64_t last_pts;
  61. int link_delta, prev_delta;
  62. } BufferSourceContext;
  63. #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format, csp, range, pts)\
  64. c->link_delta = c->w != width || c->h != height || c->pix_fmt != format ||\
  65. c->color_space != csp || c->color_range != range;\
  66. c->prev_delta = c->prev_w != width || c->prev_h != height || c->prev_pix_fmt != format ||\
  67. c->prev_color_space != csp || c->prev_color_range != range;\
  68. if (c->link_delta) {\
  69. int loglevel = c->prev_delta ? AV_LOG_WARNING : AV_LOG_DEBUG;\
  70. av_log(s, loglevel, "Changing video frame properties on the fly is not supported by all filters.\n");\
  71. av_log(s, loglevel, "filter context - w: %d h: %d fmt: %d csp: %s range: %s, incoming frame - w: %d h: %d fmt: %d csp: %s range: %s pts_time: %s\n",\
  72. c->w, c->h, c->pix_fmt, av_color_space_name(c->color_space), av_color_range_name(c->color_range),\
  73. width, height, format, av_color_space_name(csp), av_color_range_name(range),\
  74. av_ts2timestr(pts, &s->outputs[0]->time_base));\
  75. }\
  76. if (c->prev_delta) {\
  77. if (!c->link_delta)\
  78. av_log(s, AV_LOG_VERBOSE, "video frame properties congruent with link at pts_time: %s\n", av_ts2timestr(pts, &s->outputs[0]->time_base));\
  79. c->prev_w = width;\
  80. c->prev_h = height;\
  81. c->prev_pix_fmt = format;\
  82. c->prev_color_space = csp;\
  83. c->prev_color_range = range;\
  84. }
  85. #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, layout, format, pts)\
  86. if (c->sample_fmt != format || c->sample_rate != srate ||\
  87. av_channel_layout_compare(&c->ch_layout, &layout) || c->channels != layout.nb_channels) {\
  88. av_log(s, AV_LOG_INFO, "filter context - fmt: %s r: %d layout: %"PRIX64" ch: %d, incoming frame - fmt: %s r: %d layout: %"PRIX64" ch: %d pts_time: %s\n",\
  89. av_get_sample_fmt_name(c->sample_fmt), c->sample_rate, c->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? c->ch_layout.u.mask : 0, c->channels,\
  90. av_get_sample_fmt_name(format), srate, layout.order == AV_CHANNEL_ORDER_NATIVE ? layout.u.mask : 0, layout.nb_channels, av_ts2timestr(pts, &s->outputs[0]->time_base));\
  91. av_log(s, AV_LOG_ERROR, "Changing audio frame properties on the fly is not supported.\n");\
  92. return AVERROR(EINVAL);\
  93. }
  94. AVBufferSrcParameters *av_buffersrc_parameters_alloc(void)
  95. {
  96. AVBufferSrcParameters *par = av_mallocz(sizeof(*par));
  97. if (!par)
  98. return NULL;
  99. par->format = -1;
  100. par->color_range = AVCOL_RANGE_UNSPECIFIED;
  101. par->color_space = AVCOL_SPC_UNSPECIFIED;
  102. return par;
  103. }
  104. int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param)
  105. {
  106. BufferSourceContext *s = ctx->priv;
  107. if (param->time_base.num > 0 && param->time_base.den > 0)
  108. s->time_base = param->time_base;
  109. switch (ctx->filter->outputs[0].type) {
  110. case AVMEDIA_TYPE_VIDEO:
  111. if (param->format != AV_PIX_FMT_NONE) {
  112. s->pix_fmt = s->prev_pix_fmt = param->format;
  113. }
  114. if (param->width > 0)
  115. s->w = s->prev_w = param->width;
  116. if (param->height > 0)
  117. s->h = s->prev_h = param->height;
  118. if (param->sample_aspect_ratio.num > 0 && param->sample_aspect_ratio.den > 0)
  119. s->pixel_aspect = param->sample_aspect_ratio;
  120. if (param->frame_rate.num > 0 && param->frame_rate.den > 0)
  121. s->frame_rate = param->frame_rate;
  122. if (param->hw_frames_ctx) {
  123. av_buffer_unref(&s->hw_frames_ctx);
  124. s->hw_frames_ctx = av_buffer_ref(param->hw_frames_ctx);
  125. if (!s->hw_frames_ctx)
  126. return AVERROR(ENOMEM);
  127. }
  128. if (param->color_space != AVCOL_SPC_UNSPECIFIED)
  129. s->color_space = s->prev_color_space = param->color_space;
  130. if (param->color_range != AVCOL_RANGE_UNSPECIFIED)
  131. s->color_range = s->prev_color_range = param->color_range;
  132. break;
  133. case AVMEDIA_TYPE_AUDIO:
  134. if (param->format != AV_SAMPLE_FMT_NONE) {
  135. s->sample_fmt = param->format;
  136. }
  137. if (param->sample_rate > 0)
  138. s->sample_rate = param->sample_rate;
  139. if (param->ch_layout.nb_channels) {
  140. int ret = av_channel_layout_copy(&s->ch_layout, &param->ch_layout);
  141. if (ret < 0)
  142. return ret;
  143. }
  144. break;
  145. default:
  146. return AVERROR_BUG;
  147. }
  148. return 0;
  149. }
  150. int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
  151. {
  152. return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
  153. AV_BUFFERSRC_FLAG_KEEP_REF);
  154. }
  155. int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
  156. {
  157. return av_buffersrc_add_frame_flags(ctx, frame, 0);
  158. }
  159. static int push_frame(AVFilterGraph *graph)
  160. {
  161. int ret;
  162. while (1) {
  163. ret = ff_filter_graph_run_once(graph);
  164. if (ret == AVERROR(EAGAIN))
  165. break;
  166. if (ret < 0)
  167. return ret;
  168. }
  169. return 0;
  170. }
  171. int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  172. {
  173. BufferSourceContext *s = ctx->priv;
  174. AVFrame *copy;
  175. int refcounted, ret;
  176. s->nb_failed_requests = 0;
  177. if (!frame)
  178. return av_buffersrc_close(ctx, s->last_pts, flags);
  179. if (s->eof)
  180. return AVERROR_EOF;
  181. s->last_pts = frame->pts + frame->duration;
  182. refcounted = !!frame->buf[0];
  183. if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
  184. switch (ctx->outputs[0]->type) {
  185. case AVMEDIA_TYPE_VIDEO:
  186. CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
  187. frame->format, frame->colorspace,
  188. frame->color_range, frame->pts);
  189. break;
  190. case AVMEDIA_TYPE_AUDIO:
  191. /* For layouts unknown on input but known on link after negotiation. */
  192. if (frame->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
  193. ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
  194. if (ret < 0)
  195. return ret;
  196. }
  197. CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->ch_layout,
  198. frame->format, frame->pts);
  199. break;
  200. default:
  201. return AVERROR(EINVAL);
  202. }
  203. }
  204. if (refcounted && !(flags & AV_BUFFERSRC_FLAG_KEEP_REF)) {
  205. if (!(copy = av_frame_alloc()))
  206. return AVERROR(ENOMEM);
  207. av_frame_move_ref(copy, frame);
  208. } else {
  209. copy = av_frame_clone(frame);
  210. if (!copy)
  211. return AVERROR(ENOMEM);
  212. }
  213. #if FF_API_INTERLACED_FRAME
  214. FF_DISABLE_DEPRECATION_WARNINGS
  215. if (copy->interlaced_frame)
  216. copy->flags |= AV_FRAME_FLAG_INTERLACED;
  217. if (copy->top_field_first)
  218. copy->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
  219. FF_ENABLE_DEPRECATION_WARNINGS
  220. #endif
  221. #if FF_API_FRAME_KEY
  222. FF_DISABLE_DEPRECATION_WARNINGS
  223. if (copy->key_frame)
  224. copy->flags |= AV_FRAME_FLAG_KEY;
  225. FF_ENABLE_DEPRECATION_WARNINGS
  226. #endif
  227. if (copy->colorspace == AVCOL_SPC_UNSPECIFIED)
  228. copy->colorspace = ctx->outputs[0]->colorspace;
  229. if (copy->color_range == AVCOL_RANGE_UNSPECIFIED)
  230. copy->color_range = ctx->outputs[0]->color_range;
  231. ret = ff_filter_frame(ctx->outputs[0], copy);
  232. if (ret < 0)
  233. return ret;
  234. if ((flags & AV_BUFFERSRC_FLAG_PUSH)) {
  235. ret = push_frame(ctx->graph);
  236. if (ret < 0)
  237. return ret;
  238. }
  239. return 0;
  240. }
  241. int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
  242. {
  243. BufferSourceContext *s = ctx->priv;
  244. s->eof = 1;
  245. ff_avfilter_link_set_in_status(ctx->outputs[0], AVERROR_EOF, pts);
  246. return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
  247. }
  248. static av_cold int init_video(AVFilterContext *ctx)
  249. {
  250. BufferSourceContext *c = ctx->priv;
  251. if (c->pix_fmt == AV_PIX_FMT_NONE) {
  252. av_log(ctx, AV_LOG_ERROR, "Unspecified pixel format\n");
  253. return AVERROR(EINVAL);
  254. }
  255. if (c->w <= 0 || c->h <= 0) {
  256. av_log(ctx, AV_LOG_ERROR, "Invalid size %dx%d\n", c->w, c->h);
  257. return AVERROR(EINVAL);
  258. }
  259. if (av_q2d(c->time_base) <= 0) {
  260. av_log(ctx, AV_LOG_ERROR, "Invalid time base %d/%d\n", c->time_base.num, c->time_base.den);
  261. return AVERROR(EINVAL);
  262. }
  263. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d csp:%s range:%s\n",
  264. c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
  265. c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
  266. c->pixel_aspect.num, c->pixel_aspect.den,
  267. av_color_space_name(c->color_space), av_color_range_name(c->color_range));
  268. return 0;
  269. }
  270. unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
  271. {
  272. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  273. }
  274. #define OFFSET(x) offsetof(BufferSourceContext, x)
  275. #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  276. #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  277. static const AVOption buffer_options[] = {
  278. { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  279. { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
  280. { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
  281. { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_NONE }, .min = AV_PIX_FMT_NONE, .max = INT_MAX, .flags = V },
  282. { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  283. { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  284. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  285. { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
  286. { "colorspace", "select colorspace", OFFSET(color_space), AV_OPT_TYPE_INT, {.i64=AVCOL_SPC_UNSPECIFIED}, 0, AVCOL_SPC_NB-1, V, .unit = "colorspace"},
  287. { "gbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_RGB}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  288. { "bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT709}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  289. { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_UNSPECIFIED}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  290. { "fcc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_FCC}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  291. { "bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT470BG}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  292. { "smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE170M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  293. { "smpte240m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE240M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  294. { "ycgco", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  295. { "bt2020nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_NCL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  296. { "bt2020c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  297. { "smpte2085", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE2085}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  298. { "chroma-derived-nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_NCL},INT_MIN, INT_MAX, V, .unit = "colorspace"},
  299. { "chroma-derived-c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  300. { "ictcp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_ICTCP}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
  301. { "range", "select color range", OFFSET(color_range), AV_OPT_TYPE_INT, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, AVCOL_RANGE_NB-1, V, .unit = "range"},
  302. { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
  303. { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
  304. { "limited", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
  305. { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
  306. { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
  307. { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
  308. { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
  309. { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
  310. { NULL },
  311. };
  312. AVFILTER_DEFINE_CLASS(buffer);
  313. static const AVOption abuffer_options[] = {
  314. { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
  315. { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  316. { "sample_fmt", NULL, OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_NONE }, .min = AV_SAMPLE_FMT_NONE, .max = INT_MAX, .flags = A },
  317. { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
  318. { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
  319. { NULL },
  320. };
  321. AVFILTER_DEFINE_CLASS(abuffer);
  322. static av_cold int init_audio(AVFilterContext *ctx)
  323. {
  324. BufferSourceContext *s = ctx->priv;
  325. char buf[128];
  326. int ret = 0;
  327. if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
  328. av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
  329. return AVERROR(EINVAL);
  330. }
  331. if (s->channel_layout_str || s->ch_layout.nb_channels) {
  332. int n;
  333. if (!s->ch_layout.nb_channels) {
  334. ret = av_channel_layout_from_string(&s->ch_layout, s->channel_layout_str);
  335. if (ret < 0) {
  336. av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
  337. s->channel_layout_str);
  338. return AVERROR(EINVAL);
  339. }
  340. }
  341. n = s->ch_layout.nb_channels;
  342. av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
  343. if (s->channels) {
  344. if (n != s->channels) {
  345. av_log(ctx, AV_LOG_ERROR,
  346. "Mismatching channel count %d and layout '%s' "
  347. "(%d channels)\n",
  348. s->channels, buf, n);
  349. return AVERROR(EINVAL);
  350. }
  351. }
  352. s->channels = n;
  353. } else if (!s->channels) {
  354. av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
  355. "channel layout specified\n");
  356. return AVERROR(EINVAL);
  357. } else {
  358. s->ch_layout = FF_COUNT2LAYOUT(s->channels);
  359. av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
  360. }
  361. if (!s->time_base.num)
  362. s->time_base = (AVRational){1, s->sample_rate};
  363. av_log(ctx, AV_LOG_VERBOSE,
  364. "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
  365. s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
  366. s->sample_rate, buf);
  367. return ret;
  368. }
  369. static av_cold void uninit(AVFilterContext *ctx)
  370. {
  371. BufferSourceContext *s = ctx->priv;
  372. av_buffer_unref(&s->hw_frames_ctx);
  373. av_channel_layout_uninit(&s->ch_layout);
  374. }
  375. static int query_formats(AVFilterContext *ctx)
  376. {
  377. BufferSourceContext *c = ctx->priv;
  378. AVFilterChannelLayouts *channel_layouts = NULL;
  379. AVFilterFormats *formats = NULL;
  380. AVFilterFormats *samplerates = NULL;
  381. AVFilterFormats *color_spaces = NULL;
  382. AVFilterFormats *color_ranges = NULL;
  383. int ret;
  384. switch (ctx->outputs[0]->type) {
  385. case AVMEDIA_TYPE_VIDEO: {
  386. enum AVPixelFormat swfmt = c->pix_fmt;
  387. if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
  388. if (!c->hw_frames_ctx) {
  389. av_log(ctx, AV_LOG_ERROR, "Setting BufferSourceContext.pix_fmt "
  390. "to a HW format requires hw_frames_ctx to be non-NULL!\n");
  391. return AVERROR(EINVAL);
  392. }
  393. swfmt = ((AVHWFramesContext *) c->hw_frames_ctx->data)->sw_format;
  394. }
  395. if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
  396. (ret = ff_set_common_formats (ctx , formats )) < 0)
  397. return ret;
  398. /* force specific colorspace/range downstream only for ordinary YUV */
  399. if (ff_fmt_is_regular_yuv(swfmt)) {
  400. if ((ret = ff_add_format(&color_spaces, c->color_space)) < 0 ||
  401. (ret = ff_set_common_color_spaces(ctx, color_spaces)) < 0)
  402. return ret;
  403. if ((ret = ff_add_format(&color_ranges, c->color_range)) < 0)
  404. return ret;
  405. if (c->color_range == AVCOL_RANGE_UNSPECIFIED) {
  406. /* allow implicitly promoting unspecified to mpeg */
  407. if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_MPEG)) < 0)
  408. return ret;
  409. }
  410. if ((ret = ff_set_common_color_ranges(ctx, color_ranges)) < 0)
  411. return ret;
  412. }
  413. break;
  414. }
  415. case AVMEDIA_TYPE_AUDIO:
  416. if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
  417. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  418. (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
  419. (ret = ff_set_common_samplerates (ctx , samplerates )) < 0)
  420. return ret;
  421. if ((ret = ff_add_channel_layout(&channel_layouts, &c->ch_layout)) < 0)
  422. return ret;
  423. if ((ret = ff_set_common_channel_layouts(ctx, channel_layouts)) < 0)
  424. return ret;
  425. break;
  426. default:
  427. return AVERROR(EINVAL);
  428. }
  429. return 0;
  430. }
  431. static int config_props(AVFilterLink *link)
  432. {
  433. BufferSourceContext *c = link->src->priv;
  434. switch (link->type) {
  435. case AVMEDIA_TYPE_VIDEO:
  436. link->w = c->w;
  437. link->h = c->h;
  438. link->sample_aspect_ratio = c->pixel_aspect;
  439. if (c->hw_frames_ctx) {
  440. link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
  441. if (!link->hw_frames_ctx)
  442. return AVERROR(ENOMEM);
  443. }
  444. break;
  445. case AVMEDIA_TYPE_AUDIO:
  446. if (!c->ch_layout.nb_channels) {
  447. int ret = av_channel_layout_copy(&c->ch_layout, &link->ch_layout);
  448. if (ret < 0)
  449. return ret;
  450. }
  451. break;
  452. default:
  453. return AVERROR(EINVAL);
  454. }
  455. link->time_base = c->time_base;
  456. link->frame_rate = c->frame_rate;
  457. return 0;
  458. }
  459. static int activate(AVFilterContext *ctx)
  460. {
  461. AVFilterLink *outlink = ctx->outputs[0];
  462. BufferSourceContext *c = ctx->priv;
  463. if (!c->eof && ff_outlink_get_status(outlink)) {
  464. c->eof = 1;
  465. return 0;
  466. }
  467. if (c->eof) {
  468. ff_outlink_set_status(outlink, AVERROR_EOF, c->last_pts);
  469. return 0;
  470. }
  471. c->nb_failed_requests++;
  472. return FFERROR_NOT_READY;
  473. }
  474. static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
  475. {
  476. .name = "default",
  477. .type = AVMEDIA_TYPE_VIDEO,
  478. .config_props = config_props,
  479. },
  480. };
  481. const AVFilter ff_vsrc_buffer = {
  482. .name = "buffer",
  483. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  484. .priv_size = sizeof(BufferSourceContext),
  485. .activate = activate,
  486. .init = init_video,
  487. .uninit = uninit,
  488. .inputs = NULL,
  489. FILTER_OUTPUTS(avfilter_vsrc_buffer_outputs),
  490. FILTER_QUERY_FUNC(query_formats),
  491. .priv_class = &buffer_class,
  492. };
  493. static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
  494. {
  495. .name = "default",
  496. .type = AVMEDIA_TYPE_AUDIO,
  497. .config_props = config_props,
  498. },
  499. };
  500. const AVFilter ff_asrc_abuffer = {
  501. .name = "abuffer",
  502. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
  503. .priv_size = sizeof(BufferSourceContext),
  504. .activate = activate,
  505. .init = init_audio,
  506. .uninit = uninit,
  507. .inputs = NULL,
  508. FILTER_OUTPUTS(avfilter_asrc_abuffer_outputs),
  509. FILTER_QUERY_FUNC(query_formats),
  510. .priv_class = &abuffer_class,
  511. };