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.

237 lines
7.0KB

  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/hwcontext.h"
  19. #include "libavutil/hwcontext_cuda.h"
  20. #include "ffmpeg.h"
  21. #include <cuda.h>
  22. typedef struct CUVIDContext {
  23. AVBufferRef *hw_frames_ctx;
  24. } CUVIDContext;
  25. static void cuvid_uninit(AVCodecContext *avctx)
  26. {
  27. InputStream *ist = avctx->opaque;
  28. CUVIDContext *ctx = ist->hwaccel_ctx;
  29. if (ctx) {
  30. av_buffer_unref(&ctx->hw_frames_ctx);
  31. av_freep(&ctx);
  32. }
  33. av_buffer_unref(&ist->hw_frames_ctx);
  34. ist->hwaccel_ctx = 0;
  35. ist->hwaccel_uninit = 0;
  36. }
  37. int cuvid_init(AVCodecContext *avctx)
  38. {
  39. InputStream *ist = avctx->opaque;
  40. CUVIDContext *ctx = ist->hwaccel_ctx;
  41. av_log(NULL, AV_LOG_TRACE, "Initializing cuvid hwaccel\n");
  42. if (!ctx) {
  43. av_log(NULL, AV_LOG_ERROR, "CUVID transcoding is not initialized. "
  44. "-hwaccel cuvid should only be used for one-to-one CUVID transcoding "
  45. "with no (software) filters.\n");
  46. return AVERROR(EINVAL);
  47. }
  48. return 0;
  49. }
  50. static void cuvid_ctx_free(AVHWDeviceContext *ctx)
  51. {
  52. AVCUDADeviceContext *hwctx = ctx->hwctx;
  53. cuCtxDestroy(hwctx->cuda_ctx);
  54. }
  55. int cuvid_transcode_init(OutputStream *ost)
  56. {
  57. InputStream *ist;
  58. const enum AVPixelFormat *pix_fmt;
  59. AVCUDADeviceContext *device_hwctx;
  60. AVHWDeviceContext *device_ctx;
  61. AVHWFramesContext *hwframe_ctx;
  62. CUVIDContext *ctx = NULL;
  63. CUdevice device;
  64. CUcontext cuda_ctx = NULL;
  65. CUcontext dummy;
  66. CUresult err;
  67. int ret = 0;
  68. av_log(NULL, AV_LOG_TRACE, "Initializing cuvid transcoding\n");
  69. if (ost->source_index < 0)
  70. return 0;
  71. ist = input_streams[ost->source_index];
  72. /* check if the encoder supports CUVID */
  73. if (!ost->enc->pix_fmts)
  74. goto cancel;
  75. for (pix_fmt = ost->enc->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
  76. if (*pix_fmt == AV_PIX_FMT_CUDA)
  77. break;
  78. if (*pix_fmt == AV_PIX_FMT_NONE)
  79. goto cancel;
  80. /* check if the decoder supports CUVID */
  81. if (ist->hwaccel_id != HWACCEL_CUVID || !ist->dec || !ist->dec->pix_fmts)
  82. goto cancel;
  83. for (pix_fmt = ist->dec->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
  84. if (*pix_fmt == AV_PIX_FMT_CUDA)
  85. break;
  86. if (*pix_fmt == AV_PIX_FMT_NONE)
  87. goto cancel;
  88. av_log(NULL, AV_LOG_VERBOSE, "Setting up CUVID transcoding\n");
  89. if (ist->hwaccel_ctx) {
  90. ctx = ist->hwaccel_ctx;
  91. } else {
  92. ctx = av_mallocz(sizeof(*ctx));
  93. if (!ctx) {
  94. ret = AVERROR(ENOMEM);
  95. goto error;
  96. }
  97. }
  98. if (!hw_device_ctx) {
  99. hw_device_ctx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA);
  100. if (!hw_device_ctx) {
  101. av_log(NULL, AV_LOG_ERROR, "av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA) failed\n");
  102. ret = AVERROR(ENOMEM);
  103. goto error;
  104. }
  105. err = cuInit(0);
  106. if (err != CUDA_SUCCESS) {
  107. av_log(NULL, AV_LOG_ERROR, "Could not initialize the CUDA driver API\n");
  108. ret = AVERROR_UNKNOWN;
  109. goto error;
  110. }
  111. err = cuDeviceGet(&device, 0); ///TODO: Make device index configurable
  112. if (err != CUDA_SUCCESS) {
  113. av_log(NULL, AV_LOG_ERROR, "Could not get the device number %d\n", 0);
  114. ret = AVERROR_UNKNOWN;
  115. goto error;
  116. }
  117. err = cuCtxCreate(&cuda_ctx, CU_CTX_SCHED_BLOCKING_SYNC, device);
  118. if (err != CUDA_SUCCESS) {
  119. av_log(NULL, AV_LOG_ERROR, "Error creating a CUDA context\n");
  120. ret = AVERROR_UNKNOWN;
  121. goto error;
  122. }
  123. device_ctx = (AVHWDeviceContext*)hw_device_ctx->data;
  124. device_ctx->free = cuvid_ctx_free;
  125. device_hwctx = device_ctx->hwctx;
  126. device_hwctx->cuda_ctx = cuda_ctx;
  127. err = cuCtxPopCurrent(&dummy);
  128. if (err != CUDA_SUCCESS) {
  129. av_log(NULL, AV_LOG_ERROR, "cuCtxPopCurrent failed\n");
  130. ret = AVERROR_UNKNOWN;
  131. goto error;
  132. }
  133. ret = av_hwdevice_ctx_init(hw_device_ctx);
  134. if (ret < 0) {
  135. av_log(NULL, AV_LOG_ERROR, "av_hwdevice_ctx_init failed\n");
  136. goto error;
  137. }
  138. } else {
  139. device_ctx = (AVHWDeviceContext*)hw_device_ctx->data;
  140. device_hwctx = device_ctx->hwctx;
  141. cuda_ctx = device_hwctx->cuda_ctx;
  142. }
  143. if (device_ctx->type != AV_HWDEVICE_TYPE_CUDA) {
  144. av_log(NULL, AV_LOG_ERROR, "Hardware device context is already initialized for a diffrent hwaccel.\n");
  145. ret = AVERROR(EINVAL);
  146. goto error;
  147. }
  148. if (!ctx->hw_frames_ctx) {
  149. ctx->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
  150. if (!ctx->hw_frames_ctx) {
  151. av_log(NULL, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n");
  152. ret = AVERROR(ENOMEM);
  153. goto error;
  154. }
  155. }
  156. /* This is a bit hacky, av_hwframe_ctx_init is called by the cuvid decoder
  157. * once it has probed the neccesary format information. But as filters/nvenc
  158. * need to know the format/sw_format, set them here so they are happy.
  159. * This is fine as long as CUVID doesn't add another supported pix_fmt.
  160. */
  161. hwframe_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  162. hwframe_ctx->format = AV_PIX_FMT_CUDA;
  163. hwframe_ctx->sw_format = AV_PIX_FMT_NV12;
  164. ost->hwaccel_ctx = ctx;
  165. ost->enc_ctx->hw_frames_ctx = av_buffer_ref(ctx->hw_frames_ctx);
  166. ost->enc_ctx->pix_fmt = AV_PIX_FMT_CUDA;
  167. if (!ost->enc_ctx->hw_frames_ctx) {
  168. av_log(NULL, AV_LOG_ERROR, "av_buffer_ref failed\n");
  169. ret = AVERROR(ENOMEM);
  170. goto error;
  171. }
  172. if (!ist->hwaccel_ctx) {
  173. ist->hwaccel_ctx = ctx;
  174. ist->hw_frames_ctx = av_buffer_ref(ctx->hw_frames_ctx);
  175. ist->dec_ctx->hw_frames_ctx = av_buffer_ref(ctx->hw_frames_ctx);
  176. ist->dec_ctx->pix_fmt = AV_PIX_FMT_CUDA;
  177. ist->resample_pix_fmt = AV_PIX_FMT_CUDA;
  178. ist->hwaccel_uninit = cuvid_uninit;
  179. if (!ist->hw_frames_ctx || !ist->dec_ctx->hw_frames_ctx) {
  180. av_log(NULL, AV_LOG_ERROR, "av_buffer_ref failed\n");
  181. ret = AVERROR(ENOMEM);
  182. goto error;
  183. }
  184. }
  185. return 0;
  186. error:
  187. av_freep(&ctx);
  188. return ret;
  189. cancel:
  190. if (ist->hwaccel_id == HWACCEL_CUVID) {
  191. av_log(NULL, AV_LOG_ERROR, "CUVID hwaccel requested, but impossible to achive.\n");
  192. return AVERROR(EINVAL);
  193. }
  194. return 0;
  195. }