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.

202 lines
7.0KB

  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. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  93. {
  94. BufferSourceContext *c = ctx->priv;
  95. char pix_fmt_str[128];
  96. int n = 0;
  97. *c->sws_param = 0;
  98. if (!args ||
  99. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
  100. &c->time_base.num, &c->time_base.den,
  101. &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
  102. av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
  103. return AVERROR(EINVAL);
  104. }
  105. if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
  106. char *tail;
  107. c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
  108. if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
  109. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
  110. return AVERROR(EINVAL);
  111. }
  112. }
  113. av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
  114. c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
  115. c->time_base.num, c->time_base.den,
  116. c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
  117. return 0;
  118. }
  119. static int query_formats(AVFilterContext *ctx)
  120. {
  121. BufferSourceContext *c = ctx->priv;
  122. enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
  123. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  124. return 0;
  125. }
  126. static int config_props(AVFilterLink *link)
  127. {
  128. BufferSourceContext *c = link->src->priv;
  129. link->w = c->w;
  130. link->h = c->h;
  131. link->sample_aspect_ratio = c->sample_aspect_ratio;
  132. link->time_base = c->time_base;
  133. return 0;
  134. }
  135. static int request_frame(AVFilterLink *link)
  136. {
  137. BufferSourceContext *c = link->src->priv;
  138. if (!c->picref) {
  139. av_log(link->src, AV_LOG_ERROR,
  140. "request_frame() called with no available frame!\n");
  141. //return -1;
  142. }
  143. avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0));
  144. avfilter_draw_slice(link, 0, link->h, 1);
  145. avfilter_end_frame(link);
  146. avfilter_unref_buffer(c->picref);
  147. c->picref = NULL;
  148. return 0;
  149. }
  150. static int poll_frame(AVFilterLink *link)
  151. {
  152. BufferSourceContext *c = link->src->priv;
  153. return !!(c->picref);
  154. }
  155. AVFilter avfilter_vsrc_buffer = {
  156. .name = "buffer",
  157. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  158. .priv_size = sizeof(BufferSourceContext),
  159. .query_formats = query_formats,
  160. .init = init,
  161. .inputs = (AVFilterPad[]) {{ .name = NULL }},
  162. .outputs = (AVFilterPad[]) {{ .name = "default",
  163. .type = AVMEDIA_TYPE_VIDEO,
  164. .request_frame = request_frame,
  165. .poll_frame = poll_frame,
  166. .config_props = config_props, },
  167. { .name = NULL}},
  168. };