vf_hwupload.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. ff_formats_unref(&input_formats);
  68. goto fail;
  69. }
  70. }
  71. }
  72. ff_formats_ref(input_formats, &avctx->inputs[0]->out_formats);
  73. ff_formats_ref(ff_make_format_list(output_pix_fmts),
  74. &avctx->outputs[0]->in_formats);
  75. av_hwframe_constraints_free(&constraints);
  76. return 0;
  77. fail:
  78. av_buffer_unref(&ctx->hwdevice_ref);
  79. av_hwframe_constraints_free(&constraints);
  80. return err;
  81. }
  82. static int hwupload_config_output(AVFilterLink *outlink)
  83. {
  84. AVFilterContext *avctx = outlink->src;
  85. AVFilterLink *inlink = avctx->inputs[0];
  86. HWUploadContext *ctx = avctx->priv;
  87. int err;
  88. av_buffer_unref(&ctx->hwframes_ref);
  89. if (inlink->format == outlink->format) {
  90. // The input is already a hardware format, so we just want to
  91. // pass through the input frames in their own hardware context.
  92. if (!inlink->hw_frames_ctx) {
  93. av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
  94. return AVERROR(EINVAL);
  95. }
  96. outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
  97. if (!outlink->hw_frames_ctx)
  98. return AVERROR(ENOMEM);
  99. return 0;
  100. }
  101. ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
  102. if (!ctx->hwframes_ref)
  103. return AVERROR(ENOMEM);
  104. ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
  105. av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
  106. av_get_pix_fmt_name(inlink->format));
  107. ctx->hwframes->format = outlink->format;
  108. ctx->hwframes->sw_format = inlink->format;
  109. ctx->hwframes->width = inlink->w;
  110. ctx->hwframes->height = inlink->h;
  111. err = av_hwframe_ctx_init(ctx->hwframes_ref);
  112. if (err < 0)
  113. goto fail;
  114. outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  115. if (!outlink->hw_frames_ctx) {
  116. err = AVERROR(ENOMEM);
  117. goto fail;
  118. }
  119. return 0;
  120. fail:
  121. av_buffer_unref(&ctx->hwframes_ref);
  122. return err;
  123. }
  124. static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
  125. {
  126. AVFilterContext *avctx = link->dst;
  127. AVFilterLink *outlink = avctx->outputs[0];
  128. HWUploadContext *ctx = avctx->priv;
  129. AVFrame *output = NULL;
  130. int err;
  131. if (input->format == outlink->format)
  132. return ff_filter_frame(outlink, input);
  133. output = av_frame_alloc();
  134. if (!output) {
  135. err = AVERROR(ENOMEM);
  136. goto fail;
  137. }
  138. err = av_hwframe_get_buffer(ctx->hwframes_ref, output, 0);
  139. if (err < 0) {
  140. av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
  141. goto fail;
  142. }
  143. output->width = input->width;
  144. output->height = input->height;
  145. err = av_hwframe_transfer_data(output, input, 0);
  146. if (err < 0) {
  147. av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
  148. goto fail;
  149. }
  150. err = av_frame_copy_props(output, input);
  151. if (err < 0)
  152. goto fail;
  153. av_frame_free(&input);
  154. return ff_filter_frame(outlink, output);
  155. fail:
  156. av_frame_free(&input);
  157. av_frame_free(&output);
  158. return err;
  159. }
  160. static av_cold void hwupload_uninit(AVFilterContext *avctx)
  161. {
  162. HWUploadContext *ctx = avctx->priv;
  163. av_buffer_unref(&ctx->hwframes_ref);
  164. av_buffer_unref(&ctx->hwdevice_ref);
  165. }
  166. static const AVClass hwupload_class = {
  167. .class_name = "hwupload",
  168. .item_name = av_default_item_name,
  169. .option = NULL,
  170. .version = LIBAVUTIL_VERSION_INT,
  171. };
  172. static const AVFilterPad hwupload_inputs[] = {
  173. {
  174. .name = "default",
  175. .type = AVMEDIA_TYPE_VIDEO,
  176. .filter_frame = hwupload_filter_frame,
  177. },
  178. { NULL }
  179. };
  180. static const AVFilterPad hwupload_outputs[] = {
  181. {
  182. .name = "default",
  183. .type = AVMEDIA_TYPE_VIDEO,
  184. .config_props = hwupload_config_output,
  185. },
  186. { NULL }
  187. };
  188. AVFilter ff_vf_hwupload = {
  189. .name = "hwupload",
  190. .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
  191. .uninit = hwupload_uninit,
  192. .query_formats = hwupload_query_formats,
  193. .priv_size = sizeof(HWUploadContext),
  194. .priv_class = &hwupload_class,
  195. .inputs = hwupload_inputs,
  196. .outputs = hwupload_outputs,
  197. };