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.

236 lines
6.6KB

  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/hwcontext_internal.h"
  21. #include "libavutil/log.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/opt.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct HWUploadContext {
  29. const AVClass *class;
  30. AVBufferRef *hwdevice_ref;
  31. AVHWDeviceContext *hwdevice;
  32. AVBufferRef *hwframes_ref;
  33. AVHWFramesContext *hwframes;
  34. } HWUploadContext;
  35. static int hwupload_query_formats(AVFilterContext *avctx)
  36. {
  37. HWUploadContext *ctx = avctx->priv;
  38. AVHWFramesConstraints *constraints = NULL;
  39. const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts;
  40. AVFilterFormats *input_formats = NULL;
  41. int err, i;
  42. if (!avctx->hw_device_ctx) {
  43. av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
  44. "to upload frames to.\n");
  45. return AVERROR(EINVAL);
  46. }
  47. ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
  48. if (!ctx->hwdevice_ref)
  49. return AVERROR(ENOMEM);
  50. ctx->hwdevice = (AVHWDeviceContext*)ctx->hwdevice_ref->data;
  51. constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL);
  52. if (!constraints) {
  53. err = AVERROR(EINVAL);
  54. goto fail;
  55. }
  56. input_pix_fmts = constraints->valid_sw_formats;
  57. output_pix_fmts = constraints->valid_hw_formats;
  58. input_formats = ff_make_format_list(output_pix_fmts);
  59. if (!input_formats) {
  60. err = AVERROR(ENOMEM);
  61. goto fail;
  62. }
  63. if (input_pix_fmts) {
  64. for (i = 0; input_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
  65. err = ff_add_format(&input_formats, input_pix_fmts[i]);
  66. if (err < 0)
  67. goto fail;
  68. }
  69. }
  70. if ((err = ff_formats_ref(input_formats, &avctx->inputs[0]->out_formats)) < 0 ||
  71. (err = ff_formats_ref(ff_make_format_list(output_pix_fmts),
  72. &avctx->outputs[0]->in_formats)) < 0)
  73. goto fail;
  74. av_hwframe_constraints_free(&constraints);
  75. return 0;
  76. fail:
  77. av_buffer_unref(&ctx->hwdevice_ref);
  78. av_hwframe_constraints_free(&constraints);
  79. return err;
  80. }
  81. static int hwupload_config_output(AVFilterLink *outlink)
  82. {
  83. AVFilterContext *avctx = outlink->src;
  84. AVFilterLink *inlink = avctx->inputs[0];
  85. HWUploadContext *ctx = avctx->priv;
  86. int err;
  87. av_buffer_unref(&ctx->hwframes_ref);
  88. if (inlink->format == outlink->format) {
  89. // The input is already a hardware format, so we just want to
  90. // pass through the input frames in their own hardware context.
  91. if (!inlink->hw_frames_ctx) {
  92. av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
  93. return AVERROR(EINVAL);
  94. }
  95. outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
  96. if (!outlink->hw_frames_ctx)
  97. return AVERROR(ENOMEM);
  98. return 0;
  99. }
  100. ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
  101. if (!ctx->hwframes_ref)
  102. return AVERROR(ENOMEM);
  103. ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
  104. av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
  105. av_get_pix_fmt_name(inlink->format));
  106. ctx->hwframes->format = outlink->format;
  107. ctx->hwframes->sw_format = inlink->format;
  108. ctx->hwframes->width = inlink->w;
  109. ctx->hwframes->height = inlink->h;
  110. err = av_hwframe_ctx_init(ctx->hwframes_ref);
  111. if (err < 0)
  112. goto fail;
  113. outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  114. if (!outlink->hw_frames_ctx) {
  115. err = AVERROR(ENOMEM);
  116. goto fail;
  117. }
  118. return 0;
  119. fail:
  120. av_buffer_unref(&ctx->hwframes_ref);
  121. return err;
  122. }
  123. static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
  124. {
  125. AVFilterContext *avctx = link->dst;
  126. AVFilterLink *outlink = avctx->outputs[0];
  127. HWUploadContext *ctx = avctx->priv;
  128. AVFrame *output = NULL;
  129. int err;
  130. if (input->format == outlink->format)
  131. return ff_filter_frame(outlink, input);
  132. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  133. if (!output) {
  134. av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
  135. err = AVERROR(ENOMEM);
  136. goto fail;
  137. }
  138. output->width = input->width;
  139. output->height = input->height;
  140. err = av_hwframe_transfer_data(output, input, 0);
  141. if (err < 0) {
  142. av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
  143. goto fail;
  144. }
  145. err = av_frame_copy_props(output, input);
  146. if (err < 0)
  147. goto fail;
  148. av_frame_free(&input);
  149. return ff_filter_frame(outlink, output);
  150. fail:
  151. av_frame_free(&input);
  152. av_frame_free(&output);
  153. return err;
  154. }
  155. static av_cold void hwupload_uninit(AVFilterContext *avctx)
  156. {
  157. HWUploadContext *ctx = avctx->priv;
  158. av_buffer_unref(&ctx->hwframes_ref);
  159. av_buffer_unref(&ctx->hwdevice_ref);
  160. }
  161. static const AVClass hwupload_class = {
  162. .class_name = "hwupload",
  163. .item_name = av_default_item_name,
  164. .option = NULL,
  165. .version = LIBAVUTIL_VERSION_INT,
  166. };
  167. static const AVFilterPad hwupload_inputs[] = {
  168. {
  169. .name = "default",
  170. .type = AVMEDIA_TYPE_VIDEO,
  171. .filter_frame = hwupload_filter_frame,
  172. },
  173. { NULL }
  174. };
  175. static const AVFilterPad hwupload_outputs[] = {
  176. {
  177. .name = "default",
  178. .type = AVMEDIA_TYPE_VIDEO,
  179. .config_props = hwupload_config_output,
  180. },
  181. { NULL }
  182. };
  183. AVFilter ff_vf_hwupload = {
  184. .name = "hwupload",
  185. .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
  186. .uninit = hwupload_uninit,
  187. .query_formats = hwupload_query_formats,
  188. .priv_size = sizeof(HWUploadContext),
  189. .priv_class = &hwupload_class,
  190. .inputs = hwupload_inputs,
  191. .outputs = hwupload_outputs,
  192. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  193. };