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 <mfx/mfxvideo.h>
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"
31 static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
33 InputStream *ist = s->opaque;
35 return av_hwframe_get_buffer(ist->hw_frames_ctx, frame, 0);
38 static void qsv_uninit(AVCodecContext *s)
40 InputStream *ist = s->opaque;
41 av_buffer_unref(&ist->hw_frames_ctx);
44 static int qsv_device_init(InputStream *ist)
48 err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_QSV,
49 ist->hwaccel_device, NULL, 0);
51 av_log(NULL, AV_LOG_ERROR, "Error creating a QSV device\n");
58 int qsv_init(AVCodecContext *s)
60 InputStream *ist = s->opaque;
61 AVHWFramesContext *frames_ctx;
62 AVQSVFramesContext *frames_hwctx;
66 ret = qsv_device_init(ist);
71 av_buffer_unref(&ist->hw_frames_ctx);
72 ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
73 if (!ist->hw_frames_ctx)
74 return AVERROR(ENOMEM);
76 frames_ctx = (AVHWFramesContext*)ist->hw_frames_ctx->data;
77 frames_hwctx = frames_ctx->hwctx;
79 frames_ctx->width = FFALIGN(s->coded_width, 32);
80 frames_ctx->height = FFALIGN(s->coded_height, 32);
81 frames_ctx->format = AV_PIX_FMT_QSV;
82 frames_ctx->sw_format = s->sw_pix_fmt;
83 frames_ctx->initial_pool_size = 64;
84 frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
86 ret = av_hwframe_ctx_init(ist->hw_frames_ctx);
88 av_log(NULL, AV_LOG_ERROR, "Error initializing a QSV frame pool\n");
92 ist->hwaccel_get_buffer = qsv_get_buffer;
93 ist->hwaccel_uninit = qsv_uninit;
98 int qsv_transcode_init(OutputStream *ost)
101 const enum AVPixelFormat *pix_fmt;
104 AVBufferRef *encode_frames_ref = NULL;
105 AVHWFramesContext *encode_frames;
106 AVQSVFramesContext *qsv_frames;
108 /* check if the encoder supports QSV */
109 if (!ost->enc->pix_fmts)
111 for (pix_fmt = ost->enc->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
112 if (*pix_fmt == AV_PIX_FMT_QSV)
114 if (*pix_fmt == AV_PIX_FMT_NONE)
117 if (strcmp(ost->avfilter, "null") || ost->source_index < 0)
120 /* check if the decoder supports QSV and the output only goes to this stream */
121 ist = input_streams[ost->source_index];
122 if (ist->hwaccel_id != HWACCEL_QSV || !ist->dec || !ist->dec->pix_fmts)
124 for (pix_fmt = ist->dec->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
125 if (*pix_fmt == AV_PIX_FMT_QSV)
127 if (*pix_fmt == AV_PIX_FMT_NONE)
130 for (i = 0; i < nb_output_streams; i++)
131 if (output_streams[i] != ost &&
132 output_streams[i]->source_index == ost->source_index)
135 av_log(NULL, AV_LOG_VERBOSE, "Setting up QSV transcoding\n");
137 if (!hw_device_ctx) {
138 err = qsv_device_init(ist);
143 // This creates a dummy hw_frames_ctx for the encoder to be
144 // suitably initialised. It only contains one real frame, so
145 // hopefully doesn't waste too much memory.
147 encode_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx);
148 if (!encode_frames_ref) {
149 err = AVERROR(ENOMEM);
152 encode_frames = (AVHWFramesContext*)encode_frames_ref->data;
153 qsv_frames = encode_frames->hwctx;
155 encode_frames->width = FFALIGN(ist->resample_width, 32);
156 encode_frames->height = FFALIGN(ist->resample_height, 32);
157 encode_frames->format = AV_PIX_FMT_QSV;
158 encode_frames->sw_format = AV_PIX_FMT_NV12;
159 encode_frames->initial_pool_size = 1;
161 qsv_frames->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
163 err = av_hwframe_ctx_init(encode_frames_ref);
167 ist->dec_ctx->pix_fmt = AV_PIX_FMT_QSV;
168 ist->resample_pix_fmt = AV_PIX_FMT_QSV;
170 ost->enc_ctx->pix_fmt = AV_PIX_FMT_QSV;
171 ost->enc_ctx->hw_frames_ctx = encode_frames_ref;
176 av_buffer_unref(&encode_frames_ref);