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.

206 lines
5.7KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/buffer.h"
  19. #include "libavutil/hwcontext.h"
  20. #include "libavutil/log.h"
  21. #include "libavutil/mem.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct HWDownloadContext {
  29. const AVClass *class;
  30. AVBufferRef *hwframes_ref;
  31. AVHWFramesContext *hwframes;
  32. } HWDownloadContext;
  33. static int hwdownload_query_formats(AVFilterContext *avctx)
  34. {
  35. AVFilterFormats *fmts;
  36. int err;
  37. if ((err = ff_formats_pixdesc_filter(&fmts, AV_PIX_FMT_FLAG_HWACCEL, 0)) ||
  38. (err = ff_formats_ref(fmts, &avctx->inputs[0]->outcfg.formats)) ||
  39. (err = ff_formats_pixdesc_filter(&fmts, 0, AV_PIX_FMT_FLAG_HWACCEL)) ||
  40. (err = ff_formats_ref(fmts, &avctx->outputs[0]->incfg.formats)))
  41. return err;
  42. return 0;
  43. }
  44. static int hwdownload_config_input(AVFilterLink *inlink)
  45. {
  46. AVFilterContext *avctx = inlink->dst;
  47. HWDownloadContext *ctx = avctx->priv;
  48. av_buffer_unref(&ctx->hwframes_ref);
  49. if (!inlink->hw_frames_ctx) {
  50. av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
  51. "reference.\n");
  52. return AVERROR(EINVAL);
  53. }
  54. ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
  55. if (!ctx->hwframes_ref)
  56. return AVERROR(ENOMEM);
  57. ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
  58. return 0;
  59. }
  60. static int hwdownload_config_output(AVFilterLink *outlink)
  61. {
  62. AVFilterContext *avctx = outlink->src;
  63. AVFilterLink *inlink = avctx->inputs[0];
  64. HWDownloadContext *ctx = avctx->priv;
  65. enum AVPixelFormat *formats;
  66. int err, i, found;
  67. if (!ctx->hwframes_ref)
  68. return AVERROR(EINVAL);
  69. err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
  70. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  71. &formats, 0);
  72. if (err < 0)
  73. return err;
  74. found = 0;
  75. for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
  76. if (formats[i] == outlink->format) {
  77. found = 1;
  78. break;
  79. }
  80. }
  81. av_freep(&formats);
  82. if (!found) {
  83. av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
  84. "download.\n", av_get_pix_fmt_name(outlink->format));
  85. return AVERROR(EINVAL);
  86. }
  87. outlink->w = inlink->w;
  88. outlink->h = inlink->h;
  89. return 0;
  90. }
  91. static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
  92. {
  93. AVFilterContext *avctx = link->dst;
  94. AVFilterLink *outlink = avctx->outputs[0];
  95. HWDownloadContext *ctx = avctx->priv;
  96. AVFrame *output = NULL;
  97. int err;
  98. if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
  99. av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
  100. err = AVERROR(EINVAL);
  101. goto fail;
  102. }
  103. if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
  104. av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
  105. "hwframe context.\n");
  106. err = AVERROR(EINVAL);
  107. goto fail;
  108. }
  109. output = ff_get_video_buffer(outlink, ctx->hwframes->width,
  110. ctx->hwframes->height);
  111. if (!output) {
  112. err = AVERROR(ENOMEM);
  113. goto fail;
  114. }
  115. err = av_hwframe_transfer_data(output, input, 0);
  116. if (err < 0) {
  117. av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
  118. goto fail;
  119. }
  120. output->width = outlink->w;
  121. output->height = outlink->h;
  122. err = av_frame_copy_props(output, input);
  123. if (err < 0)
  124. goto fail;
  125. av_frame_free(&input);
  126. return ff_filter_frame(avctx->outputs[0], output);
  127. fail:
  128. av_frame_free(&input);
  129. av_frame_free(&output);
  130. return err;
  131. }
  132. static av_cold void hwdownload_uninit(AVFilterContext *avctx)
  133. {
  134. HWDownloadContext *ctx = avctx->priv;
  135. av_buffer_unref(&ctx->hwframes_ref);
  136. }
  137. static const AVClass hwdownload_class = {
  138. .class_name = "hwdownload",
  139. .item_name = av_default_item_name,
  140. .option = NULL,
  141. .version = LIBAVUTIL_VERSION_INT,
  142. };
  143. static const AVFilterPad hwdownload_inputs[] = {
  144. {
  145. .name = "default",
  146. .type = AVMEDIA_TYPE_VIDEO,
  147. .config_props = hwdownload_config_input,
  148. .filter_frame = hwdownload_filter_frame,
  149. },
  150. { NULL }
  151. };
  152. static const AVFilterPad hwdownload_outputs[] = {
  153. {
  154. .name = "default",
  155. .type = AVMEDIA_TYPE_VIDEO,
  156. .config_props = hwdownload_config_output,
  157. },
  158. { NULL }
  159. };
  160. AVFilter ff_vf_hwdownload = {
  161. .name = "hwdownload",
  162. .description = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
  163. .uninit = hwdownload_uninit,
  164. .query_formats = hwdownload_query_formats,
  165. .priv_size = sizeof(HWDownloadContext),
  166. .priv_class = &hwdownload_class,
  167. .inputs = hwdownload_inputs,
  168. .outputs = hwdownload_outputs,
  169. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  170. };