OSDN Git Service

lavfi/buffersrc: fix directly setting channel layout
[android-x86/external-ffmpeg.git] / ffmpeg_cuvid.c
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
19 #include "libavutil/hwcontext.h"
20
21 #include "ffmpeg.h"
22
23 typedef struct CUVIDContext {
24     AVBufferRef *hw_frames_ctx;
25 } CUVIDContext;
26
27 static void cuvid_uninit(AVCodecContext *avctx)
28 {
29     InputStream  *ist = avctx->opaque;
30     CUVIDContext *ctx = ist->hwaccel_ctx;
31
32     if (ctx) {
33         av_buffer_unref(&ctx->hw_frames_ctx);
34         av_freep(&ctx);
35     }
36
37     av_buffer_unref(&ist->hw_frames_ctx);
38
39     ist->hwaccel_ctx = 0;
40     ist->hwaccel_uninit = 0;
41 }
42
43 int cuvid_init(AVCodecContext *avctx)
44 {
45     InputStream  *ist = avctx->opaque;
46     CUVIDContext *ctx = ist->hwaccel_ctx;
47
48     av_log(NULL, AV_LOG_TRACE, "Initializing cuvid hwaccel\n");
49
50     if (!ctx) {
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);
55     }
56
57     return 0;
58 }
59
60 int cuvid_transcode_init(OutputStream *ost)
61 {
62     InputStream *ist;
63     const enum AVPixelFormat *pix_fmt;
64     AVHWFramesContext *hwframe_ctx;
65     AVBufferRef *device_ref = NULL;
66     CUVIDContext *ctx = NULL;
67     int ret = 0;
68
69     av_log(NULL, AV_LOG_TRACE, "Initializing cuvid transcoding\n");
70
71     if (ost->source_index < 0)
72         return 0;
73
74     ist = input_streams[ost->source_index];
75
76     /* check if the encoder supports CUVID */
77     if (!ost->enc->pix_fmts)
78         goto cancel;
79     for (pix_fmt = ost->enc->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
80         if (*pix_fmt == AV_PIX_FMT_CUDA)
81             break;
82     if (*pix_fmt == AV_PIX_FMT_NONE)
83         goto cancel;
84
85     /* check if the decoder supports CUVID */
86     if (ist->hwaccel_id != HWACCEL_CUVID || !ist->dec || !ist->dec->pix_fmts)
87         goto cancel;
88     for (pix_fmt = ist->dec->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
89         if (*pix_fmt == AV_PIX_FMT_CUDA)
90             break;
91     if (*pix_fmt == AV_PIX_FMT_NONE)
92         goto cancel;
93
94     av_log(NULL, AV_LOG_VERBOSE, "Setting up CUVID transcoding\n");
95
96     if (ist->hwaccel_ctx) {
97         ctx = ist->hwaccel_ctx;
98     } else {
99         ctx = av_mallocz(sizeof(*ctx));
100         if (!ctx) {
101             ret = AVERROR(ENOMEM);
102             goto error;
103         }
104     }
105
106     if (!ctx->hw_frames_ctx) {
107         ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_CUDA,
108                                      ist->hwaccel_device, NULL, 0);
109         if (ret < 0)
110             goto error;
111
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);
116             goto error;
117         }
118         av_buffer_unref(&device_ref);
119
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);
124             goto error;
125         }
126
127         ist->hwaccel_ctx = ctx;
128         ist->resample_pix_fmt = AV_PIX_FMT_CUDA;
129         ist->hwaccel_uninit = cuvid_uninit;
130
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.
135          */
136         hwframe_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
137         hwframe_ctx->format = AV_PIX_FMT_CUDA;
138         hwframe_ctx->sw_format = ist->st->codecpar->format == AV_PIX_FMT_YUV420P10 ? AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
139     }
140
141     return 0;
142
143 error:
144     av_freep(&ctx);
145     av_buffer_unref(&device_ref);
146     return ret;
147
148 cancel:
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);
152     }
153
154     return 0;
155 }