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.

169 lines
5.1KB

  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/imgutils.h"
  28. typedef struct {
  29. AVFilterBufferRef *buf;
  30. int h, w;
  31. enum PixelFormat pix_fmt;
  32. AVRational time_base; ///< time_base to set in the output link
  33. AVRational pixel_aspect;
  34. } BufferSourceContext;
  35. int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
  36. int64_t pts, AVRational pixel_aspect)
  37. {
  38. BufferSourceContext *c = buffer_filter->priv;
  39. if (c->buf) {
  40. av_log(buffer_filter, AV_LOG_ERROR,
  41. "Buffering several frames is not supported. "
  42. "Please consume all available frames before adding a new one.\n"
  43. );
  44. //return -1;
  45. }
  46. c->buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
  47. c->w, c->h);
  48. av_image_copy(c->buf->data, c->buf->linesize, frame->data, frame->linesize,
  49. c->pix_fmt, c->w, c->h);
  50. avfilter_copy_frame_props(c->buf, frame);
  51. c->buf->pts = pts;
  52. c->buf->video->pixel_aspect = pixel_aspect;
  53. return 0;
  54. }
  55. int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
  56. {
  57. BufferSourceContext *c = s->priv;
  58. if (c->buf) {
  59. av_log(s, AV_LOG_ERROR,
  60. "Buffering several frames is not supported. "
  61. "Please consume all available frames before adding a new one.\n"
  62. );
  63. return AVERROR(EINVAL);
  64. }
  65. c->buf = buf;
  66. return 0;
  67. }
  68. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  69. {
  70. BufferSourceContext *c = ctx->priv;
  71. char pix_fmt_str[128];
  72. int n = 0;
  73. if (!args ||
  74. (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
  75. &c->time_base.num, &c->time_base.den,
  76. &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
  77. av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
  78. return AVERROR(EINVAL);
  79. }
  80. if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
  81. char *tail;
  82. c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
  83. if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
  84. av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
  85. return AVERROR(EINVAL);
  86. }
  87. }
  88. 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);
  89. return 0;
  90. }
  91. static int query_formats(AVFilterContext *ctx)
  92. {
  93. BufferSourceContext *c = ctx->priv;
  94. enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
  95. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  96. return 0;
  97. }
  98. static int config_props(AVFilterLink *link)
  99. {
  100. BufferSourceContext *c = link->src->priv;
  101. link->w = c->w;
  102. link->h = c->h;
  103. link->sample_aspect_ratio = c->pixel_aspect;
  104. link->time_base = c->time_base;
  105. return 0;
  106. }
  107. static int request_frame(AVFilterLink *link)
  108. {
  109. BufferSourceContext *c = link->src->priv;
  110. if (!c->buf) {
  111. av_log(link->src, AV_LOG_ERROR,
  112. "request_frame() called with no available frame!\n");
  113. //return -1;
  114. }
  115. avfilter_start_frame(link, avfilter_ref_buffer(c->buf, ~0));
  116. avfilter_draw_slice(link, 0, link->h, 1);
  117. avfilter_end_frame(link);
  118. avfilter_unref_buffer(c->buf);
  119. c->buf = NULL;
  120. return 0;
  121. }
  122. static int poll_frame(AVFilterLink *link)
  123. {
  124. BufferSourceContext *c = link->src->priv;
  125. return !!(c->buf);
  126. }
  127. AVFilter avfilter_vsrc_buffer = {
  128. .name = "buffer",
  129. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
  130. .priv_size = sizeof(BufferSourceContext),
  131. .query_formats = query_formats,
  132. .init = init,
  133. .inputs = (AVFilterPad[]) {{ .name = NULL }},
  134. .outputs = (AVFilterPad[]) {{ .name = "default",
  135. .type = AVMEDIA_TYPE_VIDEO,
  136. .request_frame = request_frame,
  137. .poll_frame = poll_frame,
  138. .config_props = config_props, },
  139. { .name = NULL}},
  140. };