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.

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