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 AVBufferRef *hw_device_ctx;
32 char *qsv_device = NULL;
34 static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
36 InputStream *ist = s->opaque;
38 return av_hwframe_get_buffer(ist->hw_frames_ctx, frame, 0);
41 static void qsv_uninit(AVCodecContext *s)
43 InputStream *ist = s->opaque;
44 av_buffer_unref(&ist->hw_frames_ctx);
47 static int qsv_device_init(InputStream *ist)
50 AVDictionary *dict = NULL;
53 err = av_dict_set(&dict, "child_device", qsv_device, 0);
58 err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_QSV,
59 ist->hwaccel_device, dict, 0);
61 av_log(NULL, AV_LOG_ERROR, "Error creating a QSV device\n");
72 int qsv_init(AVCodecContext *s)
74 InputStream *ist = s->opaque;
75 AVHWFramesContext *frames_ctx;
76 AVQSVFramesContext *frames_hwctx;
80 ret = qsv_device_init(ist);
85 av_buffer_unref(&ist->hw_frames_ctx);
86 ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
87 if (!ist->hw_frames_ctx)
88 return AVERROR(ENOMEM);
90 frames_ctx = (AVHWFramesContext*)ist->hw_frames_ctx->data;
91 frames_hwctx = frames_ctx->hwctx;
93 frames_ctx->width = FFALIGN(s->coded_width, 32);
94 frames_ctx->height = FFALIGN(s->coded_height, 32);
95 frames_ctx->format = AV_PIX_FMT_QSV;
96 frames_ctx->sw_format = s->sw_pix_fmt;
97 frames_ctx->initial_pool_size = 64 + s->extra_hw_frames;
98 frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
100 ret = av_hwframe_ctx_init(ist->hw_frames_ctx);
102 av_log(NULL, AV_LOG_ERROR, "Error initializing a QSV frame pool\n");
106 ist->hwaccel_get_buffer = qsv_get_buffer;
107 ist->hwaccel_uninit = qsv_uninit;