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.

146 lines
4.3KB

  1. /*
  2. * Memory buffer source filter
  3. * Copyright (c) 2008 Vitor Sessak
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avfilter.h"
  22. #include "vsrc_buffer.h"
  23. typedef struct {
  24. int64_t pts;
  25. AVFrame frame;
  26. int has_frame;
  27. int h, w;
  28. enum PixelFormat pix_fmt;
  29. AVRational pixel_aspect;
  30. } BufferSourceContext;
  31. int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
  32. int64_t pts, AVRational pixel_aspect)
  33. {
  34. BufferSourceContext *c = buffer_filter->priv;
  35. if (c->has_frame) {
  36. av_log(buffer_filter, AV_LOG_ERROR,
  37. "Buffering several frames is not supported. "
  38. "Please consume all available frames before adding a new one.\n"
  39. );
  40. //return -1;
  41. }
  42. memcpy(c->frame.data , frame->data , sizeof(frame->data));
  43. memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
  44. c->frame.interlaced_frame= frame->interlaced_frame;
  45. c->frame.top_field_first = frame->top_field_first;
  46. c->pts = pts;
  47. c->pixel_aspect = pixel_aspect;
  48. c->has_frame = 1;
  49. return 0;
  50. }
  51. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  52. {
  53. BufferSourceContext *c = ctx->priv;
  54. if(args && sscanf(args, "%d:%d:%d", &c->w, &c->h, &c->pix_fmt) == 3)
  55. return 0;
  56. av_log(ctx, AV_LOG_ERROR, "init() expected 3 arguments:'%s'\n", args);
  57. return -1;
  58. }
  59. static int query_formats(AVFilterContext *ctx)
  60. {
  61. BufferSourceContext *c = ctx->priv;
  62. enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
  63. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  64. return 0;
  65. }
  66. static int config_props(AVFilterLink *link)
  67. {
  68. BufferSourceContext *c = link->src->priv;
  69. link->w = c->w;
  70. link->h = c->h;
  71. return 0;
  72. }
  73. static int request_frame(AVFilterLink *link)
  74. {
  75. BufferSourceContext *c = link->src->priv;
  76. AVFilterPicRef *picref;
  77. if (!c->has_frame) {
  78. av_log(link->src, AV_LOG_ERROR,
  79. "request_frame() called with no available frame!\n");
  80. //return -1;
  81. }
  82. /* This picture will be needed unmodified later for decoding the next
  83. * frame */
  84. picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  85. AV_PERM_REUSE2,
  86. link->w, link->h);
  87. av_picture_copy((AVPicture *)&picref->data, (AVPicture *)&c->frame,
  88. picref->pic->format, link->w, link->h);
  89. picref->pts = c->pts;
  90. picref->pixel_aspect = c->pixel_aspect;
  91. picref->interlaced = c->frame.interlaced_frame;
  92. picref->top_field_first = c->frame.top_field_first;
  93. avfilter_start_frame(link, avfilter_ref_pic(picref, ~0));
  94. avfilter_draw_slice(link, 0, link->h, 1);
  95. avfilter_end_frame(link);
  96. avfilter_unref_pic(picref);
  97. c->has_frame = 0;
  98. return 0;
  99. }
  100. static int poll_frame(AVFilterLink *link)
  101. {
  102. BufferSourceContext *c = link->src->priv;
  103. return !!(c->has_frame);
  104. }
  105. AVFilter avfilter_vsrc_buffer =
  106. {
  107. .name = "buffer",
  108. .priv_size = sizeof(BufferSourceContext),
  109. .query_formats = query_formats,
  110. .init = init,
  111. .inputs = (AVFilterPad[]) {{ .name = NULL }},
  112. .outputs = (AVFilterPad[]) {{ .name = "default",
  113. .type = AVMEDIA_TYPE_VIDEO,
  114. .request_frame = request_frame,
  115. .poll_frame = poll_frame,
  116. .config_props = config_props, },
  117. { .name = NULL}},
  118. };