vsrc_buffer.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "avfilter.h"
  25. #include "internal.h"
  26. #include "avcodec.h"
  27. #include "buffersrc.h"
  28. #include "vsrc_buffer.h"
  29. #include "libavutil/imgutils.h"
  30. typedef struct {
  31. AVFilterBufferRef *picref;
  32. int h, w;
  33. enum PixelFormat pix_fmt;
  34. AVRational time_base; ///< time_base to set in the output link
  35. AVRational sample_aspect_ratio;
  36. char sws_param[256];
  37. } BufferSourceContext;
  38. #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
  39. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  40. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  41. return AVERROR(EINVAL);\
  42. }
  43. int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
  44. AVFilterBufferRef *picref, int flags)
  45. {
  46. BufferSourceContext *c = buffer_filter->priv;
  47. AVFilterLink *outlink = buffer_filter->outputs[0];
  48. int ret;
  49. if (c->picref) {
  50. if (flags & AV_VSRC_BUF_FLAG_OVERWRITE) {
  51. avfilter_unref_buffer(c->picref);
  52. c->picref = NULL;
  53. } else {
  54. av_log(buffer_filter, AV_LOG_ERROR,
  55. "Buffering several frames is not supported. "
  56. "Please consume all available frames before adding a new one.\n");
  57. return AVERROR(EINVAL);
  58. }
  59. }
  60. if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
  61. AVFilterContext *scale = buffer_filter->outputs[0]->dst;
  62. AVFilterLink *link;
  63. char scale_param[1024];
  64. av_log(buffer_filter, AV_LOG_INFO,
  65. "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
  66. c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
  67. picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
  68. if (!scale || strcmp(scale->filter->name, "scale")) {
  69. AVFilter *f = avfilter_get_by_name("scale");
  70. av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
  71. if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
  72. return ret;
  73. snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
  74. if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
  75. avfilter_free(scale);
  76. return ret;
  77. }
  78. if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
  79. avfilter_free(scale);
  80. return ret;
  81. }
  82. scale->outputs[0]->time_base = scale->inputs[0]->time_base;
  83. scale->outputs[0]->format= c->pix_fmt;
  84. } else if (!strcmp(scale->filter->name, "scale")) {
  85. snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
  86. scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
  87. scale->filter->init(scale, scale_param, NULL);
  88. }
  89. c->pix_fmt = scale->inputs[0]->format = picref->format;
  90. c->w = scale->inputs[0]->w = picref->video->w;
  91. c->h = scale->inputs[0]->h = picref->video->h;
  92. link = scale->outputs[0];
  93. if ((ret = link->srcpad->config_props(link)) < 0)
  94. return ret;
  95. }
  96. c->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
  97. picref->video->w, picref->video->h);
  98. av_image_copy(c->picref->data, c->picref->linesize,
  99. (void*)picref->data, picref->linesize,
  100. picref->format, picref->video->w, picref->video->h);
  101. avfilter_copy_buffer_ref_props(c->picref, picref);
  102. return 0;
  103. }
  104. int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
  105. {
  106. BufferSourceContext *c = s->priv;
  107. if (c->picref) {
  108. av_log(s, AV_LOG_ERROR,
  109. "Buffering several frames is not supported. "
  110. "Please consume all available frames before adding a new one.\n"
  111. );
  112. return AVERROR(EINVAL);
  113. }
  114. // CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
  115. c->picref = buf;
  116. return 0;
  117. }
  118. #if CONFIG_AVCODEC
  119. #include "avcodec.h"
  120. int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
  121. const AVFrame *frame, int flags)
  122. {
  123. int ret;
  124. AVFilterBufferRef *picref =
  125. avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
  126. if (!picref)
  127. return AVERROR(ENOMEM);
  128. ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
  129. picref->buf->data[0] = NULL;
  130. avfilter_unref_buffer(picref);
  131. return ret;
  132. }
  133. #endif
  134. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  135. {
  136. BufferSourceContext *c = ctx->priv;
  137. char pix_fmt_str[128];
  138. int ret, n = 0;
  139. *c->sws_param = 0;
  140. if (!args ||
  141. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
  142. &c->time_base.num, &c->time_base.den,
  143. &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
  144. av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
  145. return AVERROR(EINVAL);
  146. }
  147. if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
  148. return ret;
  149. av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
  150. c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
  151. c->time_base.num, c->time_base.den,
  152. c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
  153. return 0;
  154. }
  155. static av_cold void uninit(AVFilterContext *ctx)
  156. {
  157. BufferSourceContext *s = ctx->priv;
  158. if (s->picref)
  159. avfilter_unref_buffer(s->picref);
  160. s->picref = NULL;
  161. }
  162. static int query_formats(AVFilterContext *ctx)
  163. {
  164. BufferSourceContext *c = ctx->priv;
  165. enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
  166. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  167. return 0;
  168. }
  169. static int config_props(AVFilterLink *link)
  170. {
  171. BufferSourceContext *c = link->src->priv;
  172. link->w = c->w;
  173. link->h = c->h;
  174. link->sample_aspect_ratio = c->sample_aspect_ratio;
  175. link->time_base = c->time_base;
  176. return 0;
  177. }
  178. static int request_frame(AVFilterLink *link)
  179. {
  180. BufferSourceContext *c = link->src->priv;
  181. if (!c->picref) {
  182. av_log(link->src, AV_LOG_WARNING,
  183. "request_frame() called with no available frame!\n");
  184. return AVERROR(EINVAL);
  185. }
  186. avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0));
  187. avfilter_draw_slice(link, 0, link->h, 1);
  188. avfilter_end_frame(link);
  189. avfilter_unref_buffer(c->picref);
  190. c->picref = NULL;
  191. return 0;
  192. }
  193. static int poll_frame(AVFilterLink *link)
  194. {
  195. BufferSourceContext *c = link->src->priv;
  196. return !!c->picref;
  197. }
  198. AVFilter avfilter_vsrc_buffer = {
  199. .name = "buffer",
  200. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  201. .priv_size = sizeof(BufferSourceContext),
  202. .query_formats = query_formats,
  203. .init = init,
  204. .uninit = uninit,
  205. .inputs = (const AVFilterPad[]) {{ .name = NULL }},
  206. .outputs = (const AVFilterPad[]) {{ .name = "default",
  207. .type = AVMEDIA_TYPE_VIDEO,
  208. .request_frame = request_frame,
  209. .poll_frame = poll_frame,
  210. .config_props = config_props, },
  211. { .name = NULL}},
  212. };