]> git.sesse.net Git - ffmpeg/blob - ffmpeg_qsv.c
avformat/hlsenc: deprecate hls_wrap option
[ffmpeg] / ffmpeg_qsv.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 <mfx/mfxvideo.h>
20 #include <stdlib.h>
21
22 #include "libavutil/dict.h"
23 #include "libavutil/hwcontext.h"
24 #include "libavutil/hwcontext_qsv.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 #include "libavcodec/qsv.h"
28
29 #include "ffmpeg.h"
30
31 char *qsv_device = NULL;
32
33 static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
34 {
35     InputStream *ist = s->opaque;
36
37     return av_hwframe_get_buffer(ist->hw_frames_ctx, frame, 0);
38 }
39
40 static void qsv_uninit(AVCodecContext *s)
41 {
42     InputStream *ist = s->opaque;
43     av_buffer_unref(&ist->hw_frames_ctx);
44 }
45
46 static int qsv_device_init(InputStream *ist)
47 {
48     int err;
49     AVDictionary *dict = NULL;
50
51     if (qsv_device) {
52         err = av_dict_set(&dict, "child_device", qsv_device, 0);
53         if (err < 0)
54             return err;
55     }
56
57     err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_QSV,
58                                  ist->hwaccel_device, dict, 0);
59     if (err < 0) {
60         av_log(NULL, AV_LOG_ERROR, "Error creating a QSV device\n");
61         goto err_out;
62     }
63
64 err_out:
65     if (dict)
66         av_dict_free(&dict);
67
68     return err;
69 }
70
71 int qsv_init(AVCodecContext *s)
72 {
73     InputStream *ist = s->opaque;
74     AVHWFramesContext *frames_ctx;
75     AVQSVFramesContext *frames_hwctx;
76     int ret;
77
78     if (!hw_device_ctx) {
79         ret = qsv_device_init(ist);
80         if (ret < 0)
81             return ret;
82     }
83
84     av_buffer_unref(&ist->hw_frames_ctx);
85     ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
86     if (!ist->hw_frames_ctx)
87         return AVERROR(ENOMEM);
88
89     frames_ctx   = (AVHWFramesContext*)ist->hw_frames_ctx->data;
90     frames_hwctx = frames_ctx->hwctx;
91
92     frames_ctx->width             = FFALIGN(s->coded_width,  32);
93     frames_ctx->height            = FFALIGN(s->coded_height, 32);
94     frames_ctx->format            = AV_PIX_FMT_QSV;
95     frames_ctx->sw_format         = s->sw_pix_fmt;
96     frames_ctx->initial_pool_size = 64;
97     frames_hwctx->frame_type      = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
98
99     ret = av_hwframe_ctx_init(ist->hw_frames_ctx);
100     if (ret < 0) {
101         av_log(NULL, AV_LOG_ERROR, "Error initializing a QSV frame pool\n");
102         return ret;
103     }
104
105     ist->hwaccel_get_buffer = qsv_get_buffer;
106     ist->hwaccel_uninit     = qsv_uninit;
107
108     return 0;
109 }
110
111 int qsv_transcode_init(OutputStream *ost)
112 {
113     InputStream *ist;
114     const enum AVPixelFormat *pix_fmt;
115
116     int err, i;
117     AVBufferRef *encode_frames_ref = NULL;
118     AVHWFramesContext *encode_frames;
119     AVQSVFramesContext *qsv_frames;
120
121     /* check if the encoder supports QSV */
122     if (!ost->enc->pix_fmts)
123         return 0;
124     for (pix_fmt = ost->enc->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
125         if (*pix_fmt == AV_PIX_FMT_QSV)
126             break;
127     if (*pix_fmt == AV_PIX_FMT_NONE)
128         return 0;
129
130     if (strcmp(ost->avfilter, "null") || ost->source_index < 0)
131         return 0;
132
133     /* check if the decoder supports QSV and the output only goes to this stream */
134     ist = input_streams[ost->source_index];
135     if (ist->hwaccel_id != HWACCEL_QSV || !ist->dec || !ist->dec->pix_fmts)
136         return 0;
137     for (pix_fmt = ist->dec->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
138         if (*pix_fmt == AV_PIX_FMT_QSV)
139             break;
140     if (*pix_fmt == AV_PIX_FMT_NONE)
141         return 0;
142
143     for (i = 0; i < nb_output_streams; i++)
144         if (output_streams[i] != ost &&
145             output_streams[i]->source_index == ost->source_index)
146             return 0;
147
148     av_log(NULL, AV_LOG_VERBOSE, "Setting up QSV transcoding\n");
149
150     if (!hw_device_ctx) {
151         err = qsv_device_init(ist);
152         if (err < 0)
153             goto fail;
154     }
155
156     // This creates a dummy hw_frames_ctx for the encoder to be
157     // suitably initialised.  It only contains one real frame, so
158     // hopefully doesn't waste too much memory.
159
160     encode_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx);
161     if (!encode_frames_ref) {
162         err = AVERROR(ENOMEM);
163         goto fail;
164     }
165     encode_frames = (AVHWFramesContext*)encode_frames_ref->data;
166     qsv_frames = encode_frames->hwctx;
167
168     encode_frames->width     = FFALIGN(ist->resample_width,  32);
169     encode_frames->height    = FFALIGN(ist->resample_height, 32);
170     encode_frames->format    = AV_PIX_FMT_QSV;
171     encode_frames->sw_format = AV_PIX_FMT_NV12;
172     encode_frames->initial_pool_size = 1;
173
174     qsv_frames->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
175
176     err = av_hwframe_ctx_init(encode_frames_ref);
177     if (err < 0)
178         goto fail;
179
180     ist->dec_ctx->pix_fmt       = AV_PIX_FMT_QSV;
181     ist->resample_pix_fmt       = AV_PIX_FMT_QSV;
182
183     ost->enc_ctx->pix_fmt       = AV_PIX_FMT_QSV;
184     ost->enc_ctx->hw_frames_ctx = encode_frames_ref;
185
186     return 0;
187
188 fail:
189     av_buffer_unref(&encode_frames_ref);
190     return err;
191 }