26 #include <sys/types.h>
28 #include <mfx/mfxvideo.h>
143 if (q->
gpu_copy == MFX_GPUCOPY_ON &&
144 !(q->
iopattern & MFX_IOPATTERN_OUT_SYSTEM_MEMORY)) {
146 "only works in system memory mode.\n");
151 }
else if (hw_frames_ref) {
164 q->
iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY,
172 }
else if (hw_device_ref) {
196 MFXVideoDECODE_Close(q->
session);
203 return sizeof(mfxSyncPoint*) +
sizeof(
QSVFrame*);
213 mfxSession session =
NULL;
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;
277 avctx->
width = param->mfx.FrameInfo.CropW;
278 avctx->
height = param->mfx.FrameInfo.CropH;
281 avctx->
level = param->mfx.CodecLevel;
282 avctx->
profile = param->mfx.CodecProfile;
286 ret = MFXVideoDECODE_Init(q->
session, param);
289 "Error initializing the MFX video decoder");
304 mfxVideoParam *param)
308 mfxBitstream bs = { 0 };
311 bs.Data = avpkt->
data;
312 bs.DataLength = avpkt->
size;
313 bs.MaxLength = bs.DataLength;
314 bs.TimeStamp = avpkt->
pts;
316 bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
331 param->mfx.CodecId = ret;
332 ret = MFXVideoDECODE_DecodeHeader(q->
session, &bs, param);
333 if (MFX_ERR_MORE_DATA == ret) {
338 "Error decoding stream header");
372 frame->surface.Data.ExtParam = &
frame->ext_param;
373 frame->surface.Data.NumExtParam = 1;
374 frame->ext_param = (mfxExtBuffer*)&
frame->dec_info;
375 frame->dec_info.Header.BufferId = MFX_EXTBUFF_DECODED_FRAME_INFO;
376 frame->dec_info.Header.BufferSz =
sizeof(
frame->dec_info);
409 *surf = &
frame->surface;
431 *surf = &
frame->surface;
452 mfxFrameSurface1 *insurf;
453 mfxFrameSurface1 *outsurf;
455 mfxBitstream bs = { { { 0 } } };
459 bs.Data = avpkt->
data;
460 bs.DataLength = avpkt->
size;
461 bs.MaxLength = bs.DataLength;
462 bs.TimeStamp = avpkt->
pts;
464 bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
480 ret = MFXVideoDECODE_DecodeFrameAsync(q->
session, avpkt->
size ? &bs :
NULL,
481 insurf, &outsurf, sync);
482 if (ret == MFX_WRN_DEVICE_BUSY)
485 }
while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
487 if (ret != MFX_ERR_NONE &&
488 ret != MFX_ERR_MORE_DATA &&
489 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
490 ret != MFX_ERR_MORE_SURFACE) {
493 "Error during QSV decoding.");
498 if (!*sync && !bs.DataOffset) {
499 bs.DataOffset = avpkt->
size;
503 }
else if (!*sync && bs.DataOffset) {
514 "The returned surface does not correspond to any frame\n");
536 ret = MFXVideoCORE_SyncOperation(q->
session, *sync, 1000);
537 }
while (ret == MFX_WRN_IN_EXECUTION);
542 src_frame = out_frame->
frame;
555 frame->
pts = outsurf->Data.TimeStamp;
558 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
559 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
560 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
562 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
564 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
572 ((mfxFrameSurface1*)
frame->
data[3])->Info = outsurf->Info;
577 return bs.DataOffset;
585 MFXVideoDECODE_Close(q->
session);
618 mfxVideoParam param = { 0 };
726 uid =
"f622394d8d87452f878c51f2fc9b4131";
728 uid =
"a922394d8d87452f878c51f2fc9b4131";
731 static const char *
const uid_hevcdec_sw =
"15dd936825ad475ea34e35f3f54217a6";
732 static const char *
const uid_hevcdec_hw =
"33a61c0b4c27454ca8d85dde757c6f8e";
734 if (
s->qsv.load_plugins[0]) {
736 "load_plugins is not empty, but load_plugin is not set to 'none'."
737 "The load_plugin value will be ignored.\n");
740 uid = uid_hevcdec_sw;
742 uid = uid_hevcdec_hw;
748 if (!
s->qsv.load_plugins)
754 if (!
s->packet_fifo) {
790 while (!*got_frame) {
792 if (
s->buffer_pkt.size <= 0) {
797 if (!
s->qsv.reinit_flag) {
810 if (
s->qsv.reinit_flag)
813 s->buffer_pkt.size -= ret;
814 s->buffer_pkt.data += ret;
827 s->qsv.initialized = 0;
830 #define OFFSET(x) offsetof(QSVDecContext, x)
831 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
833 #define DEFINE_QSV_DECODER_WITH_OPTION(x, X, bsf_name, opt) \
834 static const AVClass x##_qsv_class = { \
835 .class_name = #x "_qsv", \
836 .item_name = av_default_item_name, \
838 .version = LIBAVUTIL_VERSION_INT, \
840 AVCodec ff_##x##_qsv_decoder = { \
842 .long_name = NULL_IF_CONFIG_SMALL(#X " video (Intel Quick Sync Video acceleration)"), \
843 .priv_data_size = sizeof(QSVDecContext), \
844 .type = AVMEDIA_TYPE_VIDEO, \
845 .id = AV_CODEC_ID_##X, \
846 .init = qsv_decode_init, \
847 .decode = qsv_decode_frame, \
848 .flush = qsv_decode_flush, \
849 .close = qsv_decode_close, \
851 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HYBRID, \
852 .priv_class = &x##_qsv_class, \
853 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12, \
857 .hw_configs = qsv_hw_configs, \
858 .wrapper_name = "qsv", \
861 #define DEFINE_QSV_DECODER(x, X, bsf_name) DEFINE_QSV_DECODER_WITH_OPTION(x, X, bsf_name, options)
863 #if CONFIG_HEVC_QSV_DECODER
872 {
"load_plugins",
"A :-separate list of hexadecimal plugin UIDs to load in an internal session",
875 {
"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"},
887 {
"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"},
894 #if CONFIG_H264_QSV_DECODER
898 #if CONFIG_MPEG2_QSV_DECODER
902 #if CONFIG_VC1_QSV_DECODER
906 #if CONFIG_MJPEG_QSV_DECODER
910 #if CONFIG_VP8_QSV_DECODER
914 #if CONFIG_VP9_QSV_DECODER
918 #if CONFIG_AV1_QSV_DECODER
Libavcodec external API header.
common internal and external API header
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
int ff_attach_decode_data(AVFrame *frame)
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
static enum AVPixelFormat pix_fmt
a very simple circular buffer FIFO implementation
reference-counted frame API
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
@ AV_CODEC_HW_CONFIG_METHOD_AD_HOC
The codec supports this format by some ad-hoc method.
@ AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
The codec supports this format via the hw_frames_ctx interface.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_allocz(buffer_size_t size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
AVBufferPool * av_buffer_pool_init(buffer_size_t size, AVBufferRef *(*alloc)(buffer_size_t size))
Allocate and initialize a buffer pool.
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
char * av_strdup(const char *s)
Duplicate a string.
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
An API-specific header for AV_HWDEVICE_TYPE_QSV.
static int qsv_process_data(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, const AVPacket *pkt)
static int qsv_decode(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, const AVPacket *avpkt)
static void qsv_clear_unused_frames(QSVContext *q)
static const AVOption options[]
#define DEFINE_QSV_DECODER_WITH_OPTION(x, X, bsf_name, opt)
static int qsv_decode_header(AVCodecContext *avctx, QSVContext *q, const AVPacket *avpkt, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
static int qsv_decode_init_context(AVCodecContext *avctx, QSVContext *q, mfxVideoParam *param)
static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
static int qsv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static void qsv_decode_flush(AVCodecContext *avctx)
static QSVFrame * find_frame(QSVContext *q, mfxFrameSurface1 *surf)
#define DEFINE_QSV_DECODER(x, X, bsf_name)
static void qsv_decode_close_qsvcontext(QSVContext *q)
static const AVCodecHWConfigInternal *const qsv_hw_configs[]
static void qsv_clear_buffers(QSVDecContext *s)
static av_cold int qsv_decode_init(AVCodecContext *avctx)
static av_cold int qsv_decode_close(AVCodecContext *avctx)
static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
static unsigned int qsv_fifo_item_size(void)
static int qsv_get_continuous_buffer(AVCodecContext *avctx, AVFrame *frame, AVBufferPool *pool)
static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session, AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
static unsigned int qsv_fifo_size(const AVFifoBuffer *fifo)
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
int av_fifo_space(const AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
common internal API header
#define FF_DISABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
static enum AVPixelFormat pix_fmts[]
Memory handling functions.
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
@ AV_PIX_FMT_QSV
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
enum AVFieldOrder ff_qsv_map_picstruct(int mfx_pic_struct)
int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs, const char *load_plugins, int gpu_copy)
int ff_qsv_print_error(void *log_ctx, mfxStatus err, const char *error_string)
int ff_qsv_close_internal_session(QSVSession *qs)
int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession, QSVFramesContext *qsv_frames_ctx, const char *load_plugins, int opaque, int gpu_copy)
int ff_qsv_print_iopattern(void *log_ctx, int mfx_iopattern, const char *extra_string)
int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession, AVBufferRef *device_ref, const char *load_plugins, int gpu_copy)
enum AVPictureType ff_qsv_map_pictype(int mfx_pic_type)
enum AVPixelFormat ff_qsv_map_fourcc(uint32_t fourcc)
int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
int ff_qsv_print_warning(void *log_ctx, mfxStatus err, const char *warning_string)
#define ASYNC_DEPTH_DEFAULT
A reference to a data buffer.
uint8_t * data
The data buffer.
Describe the class of an AVClass context structure.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
enum AVFieldOrder field_order
Field order.
void * hwaccel_context
Hardware accelerator context.
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
int coded_width
Bitstream width / height, may be different from width/height e.g.
AVCodecHWConfig public
This is the structure which will be returned to the user by avcodec_get_hw_config().
enum AVPixelFormat pix_fmt
For decoders, a hardware pixel format which that decoder may be able to decode to if suitable hardwar...
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
attribute_deprecated int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
int key_frame
1 -> keyframe, 0-> not
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
int top_field_first
If the content is interlaced, is top field displayed first.
int interlaced_frame
The content of the picture is interlaced.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
int repeat_pict
When decoding, this signals how much the picture must be delayed.
enum AVPictureType pict_type
Picture type of the frame.
This struct describes a set or pool of "hardware" frames (i.e.
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
This struct is used for communicating QSV parameters between libavcodec and the caller.
mfxExtBuffer ** ext_buffers
Extra buffers to pass to encoder or decoder initialization.
int iopattern
The IO pattern to use.
mfxSession session
If non-NULL, the session to use for encoding or decoding.
This struct is allocated as AVHWFramesContext.hwctx.
mfxExtBuffer ** ext_buffers
enum AVPixelFormat orig_pix_fmt
QSVFramesContext frames_ctx
AVFifoBuffer * async_fifo
QSVFrame * work_frames
a linked list of frames currently being used by QSV
AVFifoBuffer * packet_fifo
mfxExtDecodedFrameInfo dec_info
AVBufferRef * hw_frames_ctx
int av_usleep(unsigned usec)
Sleep for a period of time.