vf_hwupload_cuda.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_cuda.h"
  21. #include "libavutil/log.h"
  22. #include "libavutil/opt.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. typedef struct CudaUploadContext {
  28. const AVClass *class;
  29. int device_idx;
  30. AVBufferRef *hwdevice;
  31. AVBufferRef *hwframe;
  32. } CudaUploadContext;
  33. static void cudaupload_ctx_free(AVHWDeviceContext *ctx)
  34. {
  35. AVCUDADeviceContext *hwctx = ctx->hwctx;
  36. cuCtxDestroy(hwctx->cuda_ctx);
  37. }
  38. static av_cold int cudaupload_init(AVFilterContext *ctx)
  39. {
  40. CudaUploadContext *s = ctx->priv;
  41. AVHWDeviceContext *device_ctx;
  42. AVCUDADeviceContext *device_hwctx;
  43. CUdevice device;
  44. CUcontext cuda_ctx = NULL, dummy;
  45. CUresult err;
  46. int ret;
  47. err = cuInit(0);
  48. if (err != CUDA_SUCCESS) {
  49. av_log(ctx, AV_LOG_ERROR, "Could not initialize the CUDA driver API\n");
  50. return AVERROR_UNKNOWN;
  51. }
  52. err = cuDeviceGet(&device, s->device_idx);
  53. if (err != CUDA_SUCCESS) {
  54. av_log(ctx, AV_LOG_ERROR, "Could not get the device number %d\n", s->device_idx);
  55. return AVERROR_UNKNOWN;
  56. }
  57. err = cuCtxCreate(&cuda_ctx, 0, device);
  58. if (err != CUDA_SUCCESS) {
  59. av_log(ctx, AV_LOG_ERROR, "Error creating a CUDA context\n");
  60. return AVERROR_UNKNOWN;
  61. }
  62. cuCtxPopCurrent(&dummy);
  63. s->hwdevice = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA);
  64. if (!s->hwdevice) {
  65. cuCtxDestroy(cuda_ctx);
  66. return AVERROR(ENOMEM);
  67. }
  68. device_ctx = (AVHWDeviceContext*)s->hwdevice->data;
  69. device_ctx->free = cudaupload_ctx_free;
  70. device_hwctx = device_ctx->hwctx;
  71. device_hwctx->cuda_ctx = cuda_ctx;
  72. ret = av_hwdevice_ctx_init(s->hwdevice);
  73. if (ret < 0)
  74. return ret;
  75. return 0;
  76. }
  77. static av_cold void cudaupload_uninit(AVFilterContext *ctx)
  78. {
  79. CudaUploadContext *s = ctx->priv;
  80. av_buffer_unref(&s->hwframe);
  81. av_buffer_unref(&s->hwdevice);
  82. }
  83. static int cudaupload_query_formats(AVFilterContext *ctx)
  84. {
  85. static const enum AVPixelFormat input_pix_fmts[] = {
  86. AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P,
  87. AV_PIX_FMT_NONE,
  88. };
  89. static const enum AVPixelFormat output_pix_fmts[] = {
  90. AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
  91. };
  92. AVFilterFormats *in_fmts = ff_make_format_list(input_pix_fmts);
  93. AVFilterFormats *out_fmts = ff_make_format_list(output_pix_fmts);
  94. ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
  95. ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
  96. return 0;
  97. }
  98. static int cudaupload_config_output(AVFilterLink *outlink)
  99. {
  100. AVFilterContext *ctx = outlink->src;
  101. AVFilterLink *inlink = ctx->inputs[0];
  102. CudaUploadContext *s = ctx->priv;
  103. AVHWFramesContext *hwframe_ctx;
  104. int ret;
  105. av_buffer_unref(&s->hwframe);
  106. s->hwframe = av_hwframe_ctx_alloc(s->hwdevice);
  107. if (!s->hwframe)
  108. return AVERROR(ENOMEM);
  109. hwframe_ctx = (AVHWFramesContext*)s->hwframe->data;
  110. hwframe_ctx->format = AV_PIX_FMT_CUDA;
  111. hwframe_ctx->sw_format = inlink->format;
  112. hwframe_ctx->width = FFALIGN(inlink->w, 16);
  113. hwframe_ctx->height = FFALIGN(inlink->h, 16);
  114. ret = av_hwframe_ctx_init(s->hwframe);
  115. if (ret < 0)
  116. return ret;
  117. outlink->hw_frames_ctx = av_buffer_ref(s->hwframe);
  118. if (!outlink->hw_frames_ctx)
  119. return AVERROR(ENOMEM);
  120. return 0;
  121. }
  122. static int cudaupload_filter_frame(AVFilterLink *link, AVFrame *in)
  123. {
  124. AVFilterContext *ctx = link->dst;
  125. CudaUploadContext *s = ctx->priv;
  126. AVFrame *out = NULL;
  127. int ret;
  128. out = av_frame_alloc();
  129. if (!out) {
  130. ret = AVERROR(ENOMEM);
  131. goto fail;
  132. }
  133. ret = av_hwframe_get_buffer(s->hwframe, out, 0);
  134. if (ret < 0)
  135. goto fail;
  136. out->width = in->width;
  137. out->height = in->height;
  138. ret = av_hwframe_transfer_data(out, in, 0);
  139. if (ret < 0) {
  140. av_log(ctx, AV_LOG_ERROR, "Error transferring data to the GPU\n");
  141. goto fail;
  142. }
  143. ret = av_frame_copy_props(out, in);
  144. if (ret < 0)
  145. goto fail;
  146. av_frame_free(&in);
  147. return ff_filter_frame(ctx->outputs[0], out);
  148. fail:
  149. av_frame_free(&in);
  150. av_frame_free(&out);
  151. return ret;
  152. }
  153. #define OFFSET(x) offsetof(CudaUploadContext, x)
  154. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  155. static const AVOption cudaupload_options[] = {
  156. { "device", "Number of the device to use", OFFSET(device_idx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  157. { NULL },
  158. };
  159. AVFILTER_DEFINE_CLASS(cudaupload);
  160. static const AVFilterPad cudaupload_inputs[] = {
  161. {
  162. .name = "default",
  163. .type = AVMEDIA_TYPE_VIDEO,
  164. .filter_frame = cudaupload_filter_frame,
  165. },
  166. { NULL }
  167. };
  168. static const AVFilterPad cudaupload_outputs[] = {
  169. {
  170. .name = "default",
  171. .type = AVMEDIA_TYPE_VIDEO,
  172. .config_props = cudaupload_config_output,
  173. },
  174. { NULL }
  175. };
  176. AVFilter ff_vf_hwupload_cuda = {
  177. .name = "hwupload_cuda",
  178. .description = NULL_IF_CONFIG_SMALL("Upload a system memory frame to a CUDA device."),
  179. .init = cudaupload_init,
  180. .uninit = cudaupload_uninit,
  181. .query_formats = cudaupload_query_formats,
  182. .priv_size = sizeof(CudaUploadContext),
  183. .priv_class = &cudaupload_class,
  184. .inputs = cudaupload_inputs,
  185. .outputs = cudaupload_outputs,
  186. };