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.

240 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 = av_frame_alloc();
  133. if (!output) {
  134. err = AVERROR(ENOMEM);
  135. goto fail;
  136. }
  137. err = av_hwframe_get_buffer(ctx->hwframes_ref, output, 0);
  138. if (err < 0) {
  139. av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
  140. goto fail;
  141. }
  142. output->width = input->width;
  143. output->height = input->height;
  144. err = av_hwframe_transfer_data(output, input, 0);
  145. if (err < 0) {
  146. av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
  147. goto fail;
  148. }
  149. err = av_frame_copy_props(output, input);
  150. if (err < 0)
  151. goto fail;
  152. av_frame_free(&input);
  153. return ff_filter_frame(outlink, output);
  154. fail:
  155. av_frame_free(&input);
  156. av_frame_free(&output);
  157. return err;
  158. }
  159. static av_cold void hwupload_uninit(AVFilterContext *avctx)
  160. {
  161. HWUploadContext *ctx = avctx->priv;
  162. av_buffer_unref(&ctx->hwframes_ref);
  163. av_buffer_unref(&ctx->hwdevice_ref);
  164. }
  165. static const AVClass hwupload_class = {
  166. .class_name = "hwupload",
  167. .item_name = av_default_item_name,
  168. .option = NULL,
  169. .version = LIBAVUTIL_VERSION_INT,
  170. };
  171. static const AVFilterPad hwupload_inputs[] = {
  172. {
  173. .name = "default",
  174. .type = AVMEDIA_TYPE_VIDEO,
  175. .filter_frame = hwupload_filter_frame,
  176. },
  177. { NULL }
  178. };
  179. static const AVFilterPad hwupload_outputs[] = {
  180. {
  181. .name = "default",
  182. .type = AVMEDIA_TYPE_VIDEO,
  183. .config_props = hwupload_config_output,
  184. },
  185. { NULL }
  186. };
  187. AVFilter ff_vf_hwupload = {
  188. .name = "hwupload",
  189. .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
  190. .uninit = hwupload_uninit,
  191. .query_formats = hwupload_query_formats,
  192. .priv_size = sizeof(HWUploadContext),
  193. .priv_class = &hwupload_class,
  194. .inputs = hwupload_inputs,
  195. .outputs = hwupload_outputs,
  196. };