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.

218 lines
6.6KB

  1. /*
  2. * Copyright (c) 2008 Vitor Sessak
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "buffersrc.h"
  26. #include "vsrc_buffer.h"
  27. #include "libavutil/fifo.h"
  28. #include "libavutil/imgutils.h"
  29. typedef struct {
  30. AVFifoBuffer *fifo;
  31. int h, w;
  32. enum PixelFormat pix_fmt;
  33. AVRational time_base; ///< time_base to set in the output link
  34. AVRational pixel_aspect;
  35. int eof;
  36. } BufferSourceContext;
  37. #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
  38. if (c->w != width || c->h != height || c->pix_fmt != format) {\
  39. av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
  40. return AVERROR(EINVAL);\
  41. }
  42. int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
  43. int64_t pts, AVRational pixel_aspect)
  44. {
  45. BufferSourceContext *c = buffer_filter->priv;
  46. AVFilterBufferRef *buf;
  47. int ret;
  48. if (!buf) {
  49. c->eof = 1;
  50. return 0;
  51. } else if (c->eof)
  52. return AVERROR(EINVAL);
  53. if (!av_fifo_space(c->fifo) &&
  54. (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
  55. sizeof(buf))) < 0)
  56. return ret;
  57. CHECK_PARAM_CHANGE(buffer_filter, c, frame->width, frame->height, frame->format);
  58. buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
  59. c->w, c->h);
  60. av_image_copy(buf->data, buf->linesize, frame->data, frame->linesize,
  61. c->pix_fmt, c->w, c->h);
  62. avfilter_copy_frame_props(buf, frame);
  63. buf->pts = pts;
  64. buf->video->pixel_aspect = pixel_aspect;
  65. if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
  66. avfilter_unref_buffer(buf);
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
  72. {
  73. BufferSourceContext *c = s->priv;
  74. int ret;
  75. if (!buf) {
  76. c->eof = 1;
  77. return 0;
  78. } else if (c->eof)
  79. return AVERROR(EINVAL);
  80. if (!av_fifo_space(c->fifo) &&
  81. (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
  82. sizeof(buf))) < 0)
  83. return ret;
  84. CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
  85. if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0)
  86. return ret;
  87. return 0;
  88. }
  89. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  90. {
  91. BufferSourceContext *c = ctx->priv;
  92. char pix_fmt_str[128];
  93. int n = 0;
  94. if (!args ||
  95. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
  96. &c->time_base.num, &c->time_base.den,
  97. &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
  98. av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
  99. return AVERROR(EINVAL);
  100. }
  101. if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
  102. char *tail;
  103. c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
  104. if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
  105. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
  106. return AVERROR(EINVAL);
  107. }
  108. }
  109. if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
  110. return AVERROR(ENOMEM);
  111. av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s\n", c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name);
  112. return 0;
  113. }
  114. static av_cold void uninit(AVFilterContext *ctx)
  115. {
  116. BufferSourceContext *s = ctx->priv;
  117. while (av_fifo_size(s->fifo)) {
  118. AVFilterBufferRef *buf;
  119. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  120. avfilter_unref_buffer(buf);
  121. }
  122. av_fifo_free(s->fifo);
  123. s->fifo = NULL;
  124. }
  125. static int query_formats(AVFilterContext *ctx)
  126. {
  127. BufferSourceContext *c = ctx->priv;
  128. enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
  129. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  130. return 0;
  131. }
  132. static int config_props(AVFilterLink *link)
  133. {
  134. BufferSourceContext *c = link->src->priv;
  135. link->w = c->w;
  136. link->h = c->h;
  137. link->sample_aspect_ratio = c->pixel_aspect;
  138. link->time_base = c->time_base;
  139. return 0;
  140. }
  141. static int request_frame(AVFilterLink *link)
  142. {
  143. BufferSourceContext *c = link->src->priv;
  144. AVFilterBufferRef *buf;
  145. if (!av_fifo_size(c->fifo)) {
  146. if (c->eof)
  147. return AVERROR_EOF;
  148. av_log(link->src, AV_LOG_ERROR,
  149. "request_frame() called with no available frame!\n");
  150. return AVERROR(EINVAL);
  151. }
  152. av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
  153. avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0));
  154. avfilter_draw_slice(link, 0, link->h, 1);
  155. avfilter_end_frame(link);
  156. avfilter_unref_buffer(buf);
  157. return 0;
  158. }
  159. static int poll_frame(AVFilterLink *link)
  160. {
  161. BufferSourceContext *c = link->src->priv;
  162. int size = av_fifo_size(c->fifo);
  163. if (!size && c->eof)
  164. return AVERROR_EOF;
  165. return size/sizeof(AVFilterBufferRef*);
  166. }
  167. AVFilter avfilter_vsrc_buffer = {
  168. .name = "buffer",
  169. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  170. .priv_size = sizeof(BufferSourceContext),
  171. .query_formats = query_formats,
  172. .init = init,
  173. .uninit = uninit,
  174. .inputs = (AVFilterPad[]) {{ .name = NULL }},
  175. .outputs = (AVFilterPad[]) {{ .name = "default",
  176. .type = AVMEDIA_TYPE_VIDEO,
  177. .request_frame = request_frame,
  178. .poll_frame = poll_frame,
  179. .config_props = config_props, },
  180. { .name = NULL}},
  181. };