vsrc_buffer.c 5.3 KB

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