vsrc_buffer.c 7.7 KB

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