2 * Intel MediaSDK QSV codec-independent code
4 * copyright (c) 2013 Luca Barbato
5 * copyright (c) 2015 Anton Khirnov <anton@khirnov.net>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/types.h>
28 #include <mfx/mfxvideo.h>
30 #include "libavutil/common.h"
31 #include "libavutil/fifo.h"
32 #include "libavutil/frame.h"
33 #include "libavutil/hwcontext.h"
34 #include "libavutil/hwcontext_qsv.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/log.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/pixfmt.h"
39 #include "libavutil/time.h"
40 #include "libavutil/imgutils.h"
47 #include "qsv_internal.h"
49 typedef struct QSVContext {
50 // the session used for decoding
53 // the session we allocated internally, in case the caller did not provide
55 QSVSession internal_qs;
57 QSVFramesContext frames_ctx;
60 * a linked list of frames currently being used by QSV
62 QSVFrame *work_frames;
64 AVFifoBuffer *async_fifo;
69 enum AVPixelFormat orig_pix_fmt;
71 mfxFrameInfo frame_info;
76 // options set by the caller
83 mfxExtBuffer **ext_buffers;
87 static const AVCodecHWConfigInternal *const qsv_hw_configs[] = {
88 &(const AVCodecHWConfigInternal) {
90 .pix_fmt = AV_PIX_FMT_QSV,
91 .methods = AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX |
92 AV_CODEC_HW_CONFIG_METHOD_AD_HOC,
93 .device_type = AV_HWDEVICE_TYPE_QSV,
100 static int qsv_get_continuous_buffer(AVCodecContext *avctx, AVFrame *frame,
105 ff_decode_frame_props(avctx, frame);
107 frame->width = avctx->width;
108 frame->height = avctx->height;
110 switch (avctx->pix_fmt) {
111 case AV_PIX_FMT_NV12:
112 frame->linesize[0] = FFALIGN(avctx->width, 128);
114 case AV_PIX_FMT_P010:
115 frame->linesize[0] = 2 * FFALIGN(avctx->width, 128);
118 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
119 return AVERROR(EINVAL);
122 frame->linesize[1] = frame->linesize[0];
123 frame->buf[0] = av_buffer_pool_get(pool);
125 return AVERROR(ENOMEM);
127 frame->data[0] = frame->buf[0]->data;
128 frame->data[1] = frame->data[0] +
129 frame->linesize[0] * FFALIGN(avctx->height, 64);
131 ret = ff_attach_decode_data(frame);
138 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
139 AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
143 if (q->gpu_copy == MFX_GPUCOPY_ON &&
144 !(q->iopattern & MFX_IOPATTERN_OUT_SYSTEM_MEMORY)) {
145 av_log(avctx, AV_LOG_WARNING, "GPU-accelerated memory copy "
146 "only works in system memory mode.\n");
147 q->gpu_copy = MFX_GPUCOPY_OFF;
150 q->session = session;
151 } else if (hw_frames_ref) {
152 if (q->internal_qs.session) {
153 MFXClose(q->internal_qs.session);
154 q->internal_qs.session = NULL;
156 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
158 q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref);
159 if (!q->frames_ctx.hw_frames_ctx)
160 return AVERROR(ENOMEM);
162 ret = ff_qsv_init_session_frames(avctx, &q->internal_qs.session,
163 &q->frames_ctx, q->load_plugins,
164 q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY,
167 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
171 q->session = q->internal_qs.session;
172 } else if (hw_device_ref) {
173 if (q->internal_qs.session) {
174 MFXClose(q->internal_qs.session);
175 q->internal_qs.session = NULL;
178 ret = ff_qsv_init_session_device(avctx, &q->internal_qs.session,
179 hw_device_ref, q->load_plugins, q->gpu_copy);
183 q->session = q->internal_qs.session;
185 if (!q->internal_qs.session) {
186 ret = ff_qsv_init_internal_session(avctx, &q->internal_qs,
187 q->load_plugins, q->gpu_copy);
192 q->session = q->internal_qs.session;
195 /* make sure the decoder is uninitialized */
196 MFXVideoDECODE_Close(q->session);
201 static inline unsigned int qsv_fifo_item_size(void)
203 return sizeof(mfxSyncPoint*) + sizeof(QSVFrame*);
206 static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
208 return av_fifo_size(fifo) / qsv_fifo_item_size();
211 static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
213 mfxSession session = NULL;
216 enum AVPixelFormat pix_fmts[3] = {
217 AV_PIX_FMT_QSV, /* opaque format in case of video memory output */
218 pix_fmt, /* system memory format obtained from bitstream parser */
221 ret = ff_get_format(avctx, pix_fmts);
223 q->orig_pix_fmt = avctx->pix_fmt = AV_PIX_FMT_NONE;
227 if (!q->async_fifo) {
228 q->async_fifo = av_fifo_alloc(q->async_depth * qsv_fifo_item_size());
230 return AVERROR(ENOMEM);
233 if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
234 AVQSVContext *user_ctx = avctx->hwaccel_context;
235 session = user_ctx->session;
236 iopattern = user_ctx->iopattern;
237 q->ext_buffers = user_ctx->ext_buffers;
238 q->nb_ext_buffers = user_ctx->nb_ext_buffers;
241 if (avctx->hw_frames_ctx) {
242 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
243 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
246 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
247 iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
248 else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
249 iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
254 iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
255 q->iopattern = iopattern;
257 ff_qsv_print_iopattern(avctx, q->iopattern, "Decoder");
259 ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx);
261 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
265 param->IOPattern = q->iopattern;
266 param->AsyncDepth = q->async_depth;
267 param->ExtParam = q->ext_buffers;
268 param->NumExtParam = q->nb_ext_buffers;
273 static int qsv_decode_init_context(AVCodecContext *avctx, QSVContext *q, mfxVideoParam *param)
277 avctx->width = param->mfx.FrameInfo.CropW;
278 avctx->height = param->mfx.FrameInfo.CropH;
279 avctx->coded_width = param->mfx.FrameInfo.Width;
280 avctx->coded_height = param->mfx.FrameInfo.Height;
281 avctx->level = param->mfx.CodecLevel;
282 avctx->profile = param->mfx.CodecProfile;
283 avctx->field_order = ff_qsv_map_picstruct(param->mfx.FrameInfo.PicStruct);
284 avctx->pix_fmt = ff_qsv_map_fourcc(param->mfx.FrameInfo.FourCC);
286 ret = MFXVideoDECODE_Init(q->session, param);
288 return ff_qsv_print_error(avctx, ret,
289 "Error initializing the MFX video decoder");
291 q->frame_info = param->mfx.FrameInfo;
293 if (!avctx->hw_frames_ctx)
294 q->pool = av_buffer_pool_init(av_image_get_buffer_size(avctx->pix_fmt,
295 FFALIGN(avctx->width, 128), FFALIGN(avctx->height, 64), 1), av_buffer_allocz);
299 static int qsv_decode_header(AVCodecContext *avctx, QSVContext *q,
300 const AVPacket *avpkt, enum AVPixelFormat pix_fmt,
301 mfxVideoParam *param)
305 mfxBitstream bs = { 0 };
308 bs.Data = avpkt->data;
309 bs.DataLength = avpkt->size;
310 bs.MaxLength = bs.DataLength;
311 bs.TimeStamp = avpkt->pts;
312 if (avctx->field_order == AV_FIELD_PROGRESSIVE)
313 bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
315 return AVERROR_INVALIDDATA;
319 ret = qsv_decode_preinit(avctx, q, pix_fmt, param);
324 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
328 param->mfx.CodecId = ret;
329 ret = MFXVideoDECODE_DecodeHeader(q->session, &bs, param);
330 if (MFX_ERR_MORE_DATA == ret) {
331 return AVERROR(EAGAIN);
334 return ff_qsv_print_error(avctx, ret,
335 "Error decoding stream header");
340 static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
345 ret = qsv_get_continuous_buffer(avctx, frame->frame, q->pool);
347 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
352 if (frame->frame->format == AV_PIX_FMT_QSV) {
353 frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
355 frame->surface.Info = q->frame_info;
357 frame->surface.Data.PitchLow = frame->frame->linesize[0];
358 frame->surface.Data.Y = frame->frame->data[0];
359 frame->surface.Data.UV = frame->frame->data[1];
362 if (q->frames_ctx.mids) {
363 ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
367 frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
369 frame->surface.Data.ExtParam = &frame->ext_param;
370 frame->surface.Data.NumExtParam = 1;
371 frame->ext_param = (mfxExtBuffer*)&frame->dec_info;
372 frame->dec_info.Header.BufferId = MFX_EXTBUFF_DECODED_FRAME_INFO;
373 frame->dec_info.Header.BufferSz = sizeof(frame->dec_info);
380 static void qsv_clear_unused_frames(QSVContext *q)
382 QSVFrame *cur = q->work_frames;
384 if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
386 av_frame_unref(cur->frame);
392 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
394 QSVFrame *frame, **last;
397 qsv_clear_unused_frames(q);
399 frame = q->work_frames;
400 last = &q->work_frames;
403 ret = alloc_frame(avctx, q, frame);
406 *surf = &frame->surface;
414 frame = av_mallocz(sizeof(*frame));
416 return AVERROR(ENOMEM);
417 frame->frame = av_frame_alloc();
420 return AVERROR(ENOMEM);
424 ret = alloc_frame(avctx, q, frame);
428 *surf = &frame->surface;
433 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
435 QSVFrame *cur = q->work_frames;
437 if (surf == &cur->surface)
444 static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
445 AVFrame *frame, int *got_frame,
446 const AVPacket *avpkt)
449 mfxFrameSurface1 *insurf;
450 mfxFrameSurface1 *outsurf;
452 mfxBitstream bs = { { { 0 } } };
456 bs.Data = avpkt->data;
457 bs.DataLength = avpkt->size;
458 bs.MaxLength = bs.DataLength;
459 bs.TimeStamp = avpkt->pts;
460 if (avctx->field_order == AV_FIELD_PROGRESSIVE)
461 bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
464 sync = av_mallocz(sizeof(*sync));
467 return AVERROR(ENOMEM);
471 ret = get_surface(avctx, q, &insurf);
477 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
478 insurf, &outsurf, sync);
479 if (ret == MFX_WRN_DEVICE_BUSY)
482 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
484 if (ret != MFX_ERR_NONE &&
485 ret != MFX_ERR_MORE_DATA &&
486 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
487 ret != MFX_ERR_MORE_SURFACE) {
489 return ff_qsv_print_error(avctx, ret,
490 "Error during QSV decoding.");
493 /* make sure we do not enter an infinite loop if the SDK
494 * did not consume any data and did not return anything */
495 if (!*sync && !bs.DataOffset) {
496 bs.DataOffset = avpkt->size;
497 ++q->zero_consume_run;
498 if (q->zero_consume_run > 1)
499 ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
500 } else if (!*sync && bs.DataOffset) {
503 q->zero_consume_run = 0;
507 QSVFrame *out_frame = find_frame(q, outsurf);
510 av_log(avctx, AV_LOG_ERROR,
511 "The returned surface does not correspond to any frame\n");
516 out_frame->queued = 1;
517 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
518 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
523 if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) ||
524 (!avpkt->size && av_fifo_size(q->async_fifo))) {
527 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
528 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
529 out_frame->queued = 0;
531 if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
533 ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
534 } while (ret == MFX_WRN_IN_EXECUTION);
539 src_frame = out_frame->frame;
541 ret = av_frame_ref(frame, src_frame);
545 outsurf = &out_frame->surface;
547 frame->pts = outsurf->Data.TimeStamp;
550 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
551 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
552 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
553 frame->top_field_first =
554 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
555 frame->interlaced_frame =
556 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
557 frame->pict_type = ff_qsv_map_pictype(out_frame->dec_info.FrameType);
558 //Key frame is IDR frame is only suitable for H264. For HEVC, IRAPs are key frames.
559 if (avctx->codec_id == AV_CODEC_ID_H264)
560 frame->key_frame = !!(out_frame->dec_info.FrameType & MFX_FRAMETYPE_IDR);
562 /* update the surface properties */
563 if (avctx->pix_fmt == AV_PIX_FMT_QSV)
564 ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
569 return bs.DataOffset;
572 static void qsv_decode_close_qsvcontext(QSVContext *q)
574 QSVFrame *cur = q->work_frames;
577 MFXVideoDECODE_Close(q->session);
579 while (q->async_fifo && av_fifo_size(q->async_fifo)) {
583 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
584 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
590 q->work_frames = cur->next;
591 av_frame_free(&cur->frame);
593 cur = q->work_frames;
596 av_fifo_free(q->async_fifo);
597 q->async_fifo = NULL;
599 ff_qsv_close_internal_session(&q->internal_qs);
601 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
602 av_buffer_unref(&q->frames_ctx.mids_buf);
603 av_buffer_pool_uninit(&q->pool);
606 static int qsv_process_data(AVCodecContext *avctx, QSVContext *q,
607 AVFrame *frame, int *got_frame, const AVPacket *pkt)
610 mfxVideoParam param = { 0 };
611 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NV12;
614 return qsv_decode(avctx, q, frame, got_frame, pkt);
616 /* TODO: flush delayed frames on reinit */
618 // sw_pix_fmt, coded_width/height should be set for ff_get_format(),
619 // assume sw_pix_fmt is NV12 and coded_width/height to be 1280x720,
620 // the assumption may be not corret but will be updated after header decoded if not true.
621 if (q->orig_pix_fmt != AV_PIX_FMT_NONE)
622 pix_fmt = q->orig_pix_fmt;
623 if (!avctx->coded_width)
624 avctx->coded_width = 1280;
625 if (!avctx->coded_height)
626 avctx->coded_height = 720;
628 ret = qsv_decode_header(avctx, q, pkt, pix_fmt, ¶m);
630 if (ret >= 0 && (q->orig_pix_fmt != ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC) ||
631 avctx->coded_width != param.mfx.FrameInfo.Width ||
632 avctx->coded_height != param.mfx.FrameInfo.Height)) {
633 AVPacket zero_pkt = {0};
635 if (q->buffered_count) {
637 /* decode zero-size pkt to flush the buffered pkt before reinit */
639 return qsv_decode(avctx, q, frame, got_frame, &zero_pkt);
643 q->orig_pix_fmt = avctx->pix_fmt = pix_fmt = ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC);
645 avctx->coded_width = param.mfx.FrameInfo.Width;
646 avctx->coded_height = param.mfx.FrameInfo.Height;
648 ret = qsv_decode_preinit(avctx, q, pix_fmt, ¶m);
654 if (!q->initialized) {
655 ret = qsv_decode_init_context(avctx, q, ¶m);
661 return qsv_decode(avctx, q, frame, got_frame, pkt);
664 q->orig_pix_fmt = avctx->pix_fmt = AV_PIX_FMT_NONE;
674 typedef struct QSVDecContext {
680 AVFifoBuffer *packet_fifo;
685 static void qsv_clear_buffers(QSVDecContext *s)
688 while (av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
689 av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
690 av_packet_unref(&pkt);
693 av_packet_unref(&s->buffer_pkt);
696 static av_cold int qsv_decode_close(AVCodecContext *avctx)
698 QSVDecContext *s = avctx->priv_data;
700 av_freep(&s->qsv.load_plugins);
702 qsv_decode_close_qsvcontext(&s->qsv);
704 qsv_clear_buffers(s);
706 av_fifo_free(s->packet_fifo);
711 static av_cold int qsv_decode_init(AVCodecContext *avctx)
713 QSVDecContext *s = avctx->priv_data;
715 const char *uid = NULL;
717 if (avctx->codec_id == AV_CODEC_ID_VP8) {
718 uid = "f622394d8d87452f878c51f2fc9b4131";
719 } else if (avctx->codec_id == AV_CODEC_ID_VP9) {
720 uid = "a922394d8d87452f878c51f2fc9b4131";
722 else if (avctx->codec_id == AV_CODEC_ID_HEVC && s->load_plugin != LOAD_PLUGIN_NONE) {
723 static const char * const uid_hevcdec_sw = "15dd936825ad475ea34e35f3f54217a6";
724 static const char * const uid_hevcdec_hw = "33a61c0b4c27454ca8d85dde757c6f8e";
726 if (s->qsv.load_plugins[0]) {
727 av_log(avctx, AV_LOG_WARNING,
728 "load_plugins is not empty, but load_plugin is not set to 'none'."
729 "The load_plugin value will be ignored.\n");
731 if (s->load_plugin == LOAD_PLUGIN_HEVC_SW)
732 uid = uid_hevcdec_sw;
734 uid = uid_hevcdec_hw;
738 av_freep(&s->qsv.load_plugins);
739 s->qsv.load_plugins = av_strdup(uid);
740 if (!s->qsv.load_plugins)
741 return AVERROR(ENOMEM);
744 s->qsv.orig_pix_fmt = AV_PIX_FMT_NV12;
745 s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
746 if (!s->packet_fifo) {
747 ret = AVERROR(ENOMEM);
753 qsv_decode_close(avctx);
757 static int qsv_decode_frame(AVCodecContext *avctx, void *data,
758 int *got_frame, AVPacket *avpkt)
760 QSVDecContext *s = avctx->priv_data;
761 AVFrame *frame = data;
764 /* buffer the input packet */
768 if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
769 ret = av_fifo_realloc2(s->packet_fifo,
770 av_fifo_size(s->packet_fifo) + sizeof(input_ref));
775 ret = av_packet_ref(&input_ref, avpkt);
778 av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
781 /* process buffered data */
782 while (!*got_frame) {
783 /* prepare the input data */
784 if (s->buffer_pkt.size <= 0) {
786 if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
787 return avpkt->size ? avpkt->size : qsv_process_data(avctx, &s->qsv, frame, got_frame, avpkt);
788 /* in progress of reinit, no read from fifo and keep the buffer_pkt */
789 if (!s->qsv.reinit_flag) {
790 av_packet_unref(&s->buffer_pkt);
791 av_fifo_generic_read(s->packet_fifo, &s->buffer_pkt, sizeof(s->buffer_pkt), NULL);
795 ret = qsv_process_data(avctx, &s->qsv, frame, got_frame, &s->buffer_pkt);
797 /* Drop buffer_pkt when failed to decode the packet. Otherwise,
798 the decoder will keep decoding the failure packet. */
799 av_packet_unref(&s->buffer_pkt);
802 if (s->qsv.reinit_flag)
805 s->buffer_pkt.size -= ret;
806 s->buffer_pkt.data += ret;
812 static void qsv_decode_flush(AVCodecContext *avctx)
814 QSVDecContext *s = avctx->priv_data;
816 qsv_clear_buffers(s);
818 s->qsv.orig_pix_fmt = AV_PIX_FMT_NONE;
819 s->qsv.initialized = 0;
822 #define OFFSET(x) offsetof(QSVDecContext, x)
823 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
825 #define DEFINE_QSV_DECODER_WITH_OPTION(x, X, bsf_name, opt) \
826 static const AVClass x##_qsv_class = { \
827 .class_name = #x "_qsv", \
828 .item_name = av_default_item_name, \
830 .version = LIBAVUTIL_VERSION_INT, \
832 const AVCodec ff_##x##_qsv_decoder = { \
834 .long_name = NULL_IF_CONFIG_SMALL(#X " video (Intel Quick Sync Video acceleration)"), \
835 .priv_data_size = sizeof(QSVDecContext), \
836 .type = AVMEDIA_TYPE_VIDEO, \
837 .id = AV_CODEC_ID_##X, \
838 .init = qsv_decode_init, \
839 .decode = qsv_decode_frame, \
840 .flush = qsv_decode_flush, \
841 .close = qsv_decode_close, \
843 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HYBRID, \
844 .priv_class = &x##_qsv_class, \
845 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12, \
849 .hw_configs = qsv_hw_configs, \
850 .wrapper_name = "qsv", \
853 #define DEFINE_QSV_DECODER(x, X, bsf_name) DEFINE_QSV_DECODER_WITH_OPTION(x, X, bsf_name, options)
855 #if CONFIG_HEVC_QSV_DECODER
856 static const AVOption hevc_options[] = {
857 { "async_depth", "Internal parallelization depth, the higher the value the higher the latency.", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 1, INT_MAX, VD },
859 { "load_plugin", "A user plugin to load in an internal session", OFFSET(load_plugin), AV_OPT_TYPE_INT, { .i64 = LOAD_PLUGIN_HEVC_HW }, LOAD_PLUGIN_NONE, LOAD_PLUGIN_HEVC_HW, VD, "load_plugin" },
860 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VD, "load_plugin" },
861 { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VD, "load_plugin" },
862 { "hevc_hw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_HW }, 0, 0, VD, "load_plugin" },
864 { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session",
865 OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
867 { "gpu_copy", "A GPU-accelerated copy between video and system memory", OFFSET(qsv.gpu_copy), AV_OPT_TYPE_INT, { .i64 = MFX_GPUCOPY_DEFAULT }, MFX_GPUCOPY_DEFAULT, MFX_GPUCOPY_OFF, VD, "gpu_copy"},
868 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_DEFAULT }, 0, 0, VD, "gpu_copy"},
869 { "on", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_ON }, 0, 0, VD, "gpu_copy"},
870 { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_OFF }, 0, 0, VD, "gpu_copy"},
873 DEFINE_QSV_DECODER_WITH_OPTION(hevc, HEVC, "hevc_mp4toannexb", hevc_options)
876 static const AVOption options[] = {
877 { "async_depth", "Internal parallelization depth, the higher the value the higher the latency.", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 1, INT_MAX, VD },
879 { "gpu_copy", "A GPU-accelerated copy between video and system memory", OFFSET(qsv.gpu_copy), AV_OPT_TYPE_INT, { .i64 = MFX_GPUCOPY_DEFAULT }, MFX_GPUCOPY_DEFAULT, MFX_GPUCOPY_OFF, VD, "gpu_copy"},
880 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_DEFAULT }, 0, 0, VD, "gpu_copy"},
881 { "on", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_ON }, 0, 0, VD, "gpu_copy"},
882 { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_OFF }, 0, 0, VD, "gpu_copy"},
886 #if CONFIG_H264_QSV_DECODER
887 DEFINE_QSV_DECODER(h264, H264, "h264_mp4toannexb")
890 #if CONFIG_MPEG2_QSV_DECODER
891 DEFINE_QSV_DECODER(mpeg2, MPEG2VIDEO, NULL)
894 #if CONFIG_VC1_QSV_DECODER
895 DEFINE_QSV_DECODER(vc1, VC1, NULL)
898 #if CONFIG_MJPEG_QSV_DECODER
899 DEFINE_QSV_DECODER(mjpeg, MJPEG, NULL)
902 #if CONFIG_VP8_QSV_DECODER
903 DEFINE_QSV_DECODER(vp8, VP8, NULL)
906 #if CONFIG_VP9_QSV_DECODER
907 DEFINE_QSV_DECODER(vp9, VP9, NULL)
910 #if CONFIG_AV1_QSV_DECODER
911 DEFINE_QSV_DECODER(av1, AV1, NULL)