ffmpeg_cuvid.c 7.0 KB

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