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.

150 lines
4.6KB

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