vsrc_buffer.c 7.5 KB

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