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.

161 lines
5.1KB

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