2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/hwcontext.h"
23 typedef struct CUVIDContext {
24 AVBufferRef *hw_frames_ctx;
27 static void cuvid_uninit(AVCodecContext *avctx)
29 InputStream *ist = avctx->opaque;
30 CUVIDContext *ctx = ist->hwaccel_ctx;
33 av_buffer_unref(&ctx->hw_frames_ctx);
37 av_buffer_unref(&ist->hw_frames_ctx);
40 ist->hwaccel_uninit = 0;
43 int cuvid_init(AVCodecContext *avctx)
45 InputStream *ist = avctx->opaque;
46 CUVIDContext *ctx = ist->hwaccel_ctx;
48 av_log(NULL, AV_LOG_TRACE, "Initializing cuvid hwaccel\n");
51 av_log(NULL, AV_LOG_ERROR, "CUVID transcoding is not initialized. "
52 "-hwaccel cuvid should only be used for one-to-one CUVID transcoding "
53 "with no (software) filters.\n");
54 return AVERROR(EINVAL);
60 int cuvid_transcode_init(OutputStream *ost)
63 const enum AVPixelFormat *pix_fmt;
64 AVHWFramesContext *hwframe_ctx;
65 AVBufferRef *device_ref = NULL;
66 CUVIDContext *ctx = NULL;
69 av_log(NULL, AV_LOG_TRACE, "Initializing cuvid transcoding\n");
71 if (ost->source_index < 0)
74 ist = input_streams[ost->source_index];
76 /* check if the encoder supports CUVID */
77 if (!ost->enc->pix_fmts)
79 for (pix_fmt = ost->enc->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
80 if (*pix_fmt == AV_PIX_FMT_CUDA)
82 if (*pix_fmt == AV_PIX_FMT_NONE)
85 /* check if the decoder supports CUVID */
86 if (ist->hwaccel_id != HWACCEL_CUVID || !ist->dec || !ist->dec->pix_fmts)
88 for (pix_fmt = ist->dec->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
89 if (*pix_fmt == AV_PIX_FMT_CUDA)
91 if (*pix_fmt == AV_PIX_FMT_NONE)
94 av_log(NULL, AV_LOG_VERBOSE, "Setting up CUVID transcoding\n");
96 if (ist->hwaccel_ctx) {
97 ctx = ist->hwaccel_ctx;
99 ctx = av_mallocz(sizeof(*ctx));
101 ret = AVERROR(ENOMEM);
106 if (!ctx->hw_frames_ctx) {
107 ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_CUDA,
108 ist->hwaccel_device, NULL, 0);
112 ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
113 if (!ctx->hw_frames_ctx) {
114 av_log(NULL, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n");
115 ret = AVERROR(ENOMEM);
118 av_buffer_unref(&device_ref);
120 ist->hw_frames_ctx = av_buffer_ref(ctx->hw_frames_ctx);
121 if (!ist->hw_frames_ctx) {
122 av_log(NULL, AV_LOG_ERROR, "av_buffer_ref failed\n");
123 ret = AVERROR(ENOMEM);
127 ist->hwaccel_ctx = ctx;
128 ist->resample_pix_fmt = AV_PIX_FMT_CUDA;
129 ist->hwaccel_uninit = cuvid_uninit;
131 /* This is a bit hacky, av_hwframe_ctx_init is called by the cuvid decoder
132 * once it has probed the necessary format information. But as filters/nvenc
133 * need to know the format/sw_format, set them here so they are happy.
134 * This is fine as long as CUVID doesn't add another supported pix_fmt.
136 hwframe_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
137 hwframe_ctx->format = AV_PIX_FMT_CUDA;
138 hwframe_ctx->sw_format = AV_PIX_FMT_NV12;
145 av_buffer_unref(&device_ref);
149 if (ist->hwaccel_id == HWACCEL_CUVID) {
150 av_log(NULL, AV_LOG_ERROR, "CUVID hwaccel requested, but impossible to achieve.\n");
151 return AVERROR(EINVAL);