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.

215 lines
5.9KB

  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/avassert.h"
  19. #include "libavutil/buffer.h"
  20. #include "libavutil/hwcontext.h"
  21. #include "libavutil/log.h"
  22. #include "libavutil/mem.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. typedef struct HWDownloadContext {
  30. const AVClass *class;
  31. AVBufferRef *hwframes_ref;
  32. AVHWFramesContext *hwframes;
  33. } HWDownloadContext;
  34. static int hwdownload_query_formats(AVFilterContext *avctx)
  35. {
  36. AVFilterFormats *infmts = NULL;
  37. AVFilterFormats *outfmts = NULL;
  38. const AVPixFmtDescriptor *desc;
  39. int err;
  40. for (desc = av_pix_fmt_desc_next(NULL); desc;
  41. desc = av_pix_fmt_desc_next(desc)) {
  42. if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  43. err = ff_add_format(&infmts, av_pix_fmt_desc_get_id(desc));
  44. else
  45. err = ff_add_format(&outfmts, av_pix_fmt_desc_get_id(desc));
  46. if (err) {
  47. ff_formats_unref(&infmts);
  48. ff_formats_unref(&outfmts);
  49. return err;
  50. }
  51. }
  52. if ((err = ff_formats_ref(infmts, &avctx->inputs[0]->out_formats)) < 0 ||
  53. (err = ff_formats_ref(outfmts, &avctx->outputs[0]->in_formats)) < 0)
  54. return err;
  55. return 0;
  56. }
  57. static int hwdownload_config_input(AVFilterLink *inlink)
  58. {
  59. AVFilterContext *avctx = inlink->dst;
  60. HWDownloadContext *ctx = avctx->priv;
  61. av_buffer_unref(&ctx->hwframes_ref);
  62. if (!inlink->hw_frames_ctx) {
  63. av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
  64. "reference.\n");
  65. return AVERROR(EINVAL);
  66. }
  67. ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
  68. if (!ctx->hwframes_ref)
  69. return AVERROR(ENOMEM);
  70. ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
  71. return 0;
  72. }
  73. static int hwdownload_config_output(AVFilterLink *outlink)
  74. {
  75. AVFilterContext *avctx = outlink->src;
  76. AVFilterLink *inlink = avctx->inputs[0];
  77. HWDownloadContext *ctx = avctx->priv;
  78. enum AVPixelFormat *formats;
  79. int err, i, found;
  80. if (!ctx->hwframes_ref)
  81. return AVERROR(EINVAL);
  82. err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
  83. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  84. &formats, 0);
  85. if (err < 0)
  86. return err;
  87. found = 0;
  88. for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
  89. if (formats[i] == outlink->format) {
  90. found = 1;
  91. break;
  92. }
  93. }
  94. av_freep(&formats);
  95. if (!found) {
  96. av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
  97. "download.\n", av_get_pix_fmt_name(outlink->format));
  98. return AVERROR(EINVAL);
  99. }
  100. outlink->w = inlink->w;
  101. outlink->h = inlink->h;
  102. return 0;
  103. }
  104. static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
  105. {
  106. AVFilterContext *avctx = link->dst;
  107. AVFilterLink *outlink = avctx->outputs[0];
  108. HWDownloadContext *ctx = avctx->priv;
  109. AVFrame *output = NULL;
  110. int err;
  111. if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
  112. av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
  113. err = AVERROR(EINVAL);
  114. goto fail;
  115. }
  116. if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
  117. av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
  118. "hwframe context.\n");
  119. err = AVERROR(EINVAL);
  120. goto fail;
  121. }
  122. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  123. if (!output) {
  124. err = AVERROR(ENOMEM);
  125. goto fail;
  126. }
  127. err = av_hwframe_transfer_data(output, input, 0);
  128. if (err < 0) {
  129. av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
  130. goto fail;
  131. }
  132. err = av_frame_copy_props(output, input);
  133. if (err < 0)
  134. goto fail;
  135. av_frame_free(&input);
  136. return ff_filter_frame(avctx->outputs[0], output);
  137. fail:
  138. av_frame_free(&input);
  139. av_frame_free(&output);
  140. return err;
  141. }
  142. static av_cold void hwdownload_uninit(AVFilterContext *avctx)
  143. {
  144. HWDownloadContext *ctx = avctx->priv;
  145. av_buffer_unref(&ctx->hwframes_ref);
  146. }
  147. static const AVClass hwdownload_class = {
  148. .class_name = "hwdownload",
  149. .item_name = av_default_item_name,
  150. .option = NULL,
  151. .version = LIBAVUTIL_VERSION_INT,
  152. };
  153. static const AVFilterPad hwdownload_inputs[] = {
  154. {
  155. .name = "default",
  156. .type = AVMEDIA_TYPE_VIDEO,
  157. .config_props = hwdownload_config_input,
  158. .filter_frame = hwdownload_filter_frame,
  159. },
  160. { NULL }
  161. };
  162. static const AVFilterPad hwdownload_outputs[] = {
  163. {
  164. .name = "default",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. .config_props = hwdownload_config_output,
  167. },
  168. { NULL }
  169. };
  170. AVFilter ff_vf_hwdownload = {
  171. .name = "hwdownload",
  172. .description = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
  173. .uninit = hwdownload_uninit,
  174. .query_formats = hwdownload_query_formats,
  175. .priv_size = sizeof(HWDownloadContext),
  176. .priv_class = &hwdownload_class,
  177. .inputs = hwdownload_inputs,
  178. .outputs = hwdownload_outputs,
  179. };