You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
7.4KB

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