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.

239 lines
6.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/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. if (avctx->extra_hw_frames >= 0)
  111. ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
  112. err = av_hwframe_ctx_init(ctx->hwframes_ref);
  113. if (err < 0)
  114. goto fail;
  115. outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  116. if (!outlink->hw_frames_ctx) {
  117. err = AVERROR(ENOMEM);
  118. goto fail;
  119. }
  120. return 0;
  121. fail:
  122. av_buffer_unref(&ctx->hwframes_ref);
  123. return err;
  124. }
  125. static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
  126. {
  127. AVFilterContext *avctx = link->dst;
  128. AVFilterLink *outlink = avctx->outputs[0];
  129. HWUploadContext *ctx = avctx->priv;
  130. AVFrame *output = NULL;
  131. int err;
  132. if (input->format == outlink->format)
  133. return ff_filter_frame(outlink, input);
  134. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  135. if (!output) {
  136. av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
  137. err = AVERROR(ENOMEM);
  138. goto fail;
  139. }
  140. output->width = input->width;
  141. output->height = input->height;
  142. err = av_hwframe_transfer_data(output, input, 0);
  143. if (err < 0) {
  144. av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
  145. goto fail;
  146. }
  147. err = av_frame_copy_props(output, input);
  148. if (err < 0)
  149. goto fail;
  150. av_frame_free(&input);
  151. return ff_filter_frame(outlink, output);
  152. fail:
  153. av_frame_free(&input);
  154. av_frame_free(&output);
  155. return err;
  156. }
  157. static av_cold void hwupload_uninit(AVFilterContext *avctx)
  158. {
  159. HWUploadContext *ctx = avctx->priv;
  160. av_buffer_unref(&ctx->hwframes_ref);
  161. av_buffer_unref(&ctx->hwdevice_ref);
  162. }
  163. static const AVClass hwupload_class = {
  164. .class_name = "hwupload",
  165. .item_name = av_default_item_name,
  166. .option = NULL,
  167. .version = LIBAVUTIL_VERSION_INT,
  168. };
  169. static const AVFilterPad hwupload_inputs[] = {
  170. {
  171. .name = "default",
  172. .type = AVMEDIA_TYPE_VIDEO,
  173. .filter_frame = hwupload_filter_frame,
  174. },
  175. { NULL }
  176. };
  177. static const AVFilterPad hwupload_outputs[] = {
  178. {
  179. .name = "default",
  180. .type = AVMEDIA_TYPE_VIDEO,
  181. .config_props = hwupload_config_output,
  182. },
  183. { NULL }
  184. };
  185. AVFilter ff_vf_hwupload = {
  186. .name = "hwupload",
  187. .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
  188. .uninit = hwupload_uninit,
  189. .query_formats = hwupload_query_formats,
  190. .priv_size = sizeof(HWUploadContext),
  191. .priv_class = &hwupload_class,
  192. .inputs = hwupload_inputs,
  193. .outputs = hwupload_outputs,
  194. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  195. };