25 #include <sys/types.h>
26 #include <mfx/mfxvideo.h>
42 const char *ratecontrol_desc;
50 q->
param.mfx.CodecId = ret;
66 q->
param.mfx.EncodedOrder = 0;
67 q->
param.mfx.BufferSizeInKB = 0;
69 q->
param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
70 q->
param.mfx.FrameInfo.CropX = 0;
71 q->
param.mfx.FrameInfo.CropY = 0;
76 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
77 q->
param.mfx.FrameInfo.BitDepthLuma = 8;
78 q->
param.mfx.FrameInfo.BitDepthChroma = 8;
87 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
90 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
104 q->
param.mfx.RateControlMethod = MFX_RATECONTROL_CQP;
105 ratecontrol_desc =
"constant quantization parameter (CQP)";
107 q->
param.mfx.RateControlMethod = MFX_RATECONTROL_CBR;
108 ratecontrol_desc =
"constant bitrate (CBR)";
110 #if QSV_VERSION_ATLEAST(1,7)
112 q->
param.mfx.RateControlMethod = MFX_RATECONTROL_LA;
113 ratecontrol_desc =
"lookahead (LA)";
117 q->
param.mfx.RateControlMethod = MFX_RATECONTROL_AVBR;
118 ratecontrol_desc =
"average variable bitrate (AVBR)";
121 q->
param.mfx.RateControlMethod = MFX_RATECONTROL_VBR;
122 ratecontrol_desc =
"variable bitrate (VBR)";
127 switch (q->
param.mfx.RateControlMethod) {
128 case MFX_RATECONTROL_CBR:
129 case MFX_RATECONTROL_VBR:
134 case MFX_RATECONTROL_CQP:
138 q->
param.mfx.QPP = av_clip(quant, 0, 51);
142 case MFX_RATECONTROL_AVBR:
143 #if QSV_VERSION_ATLEAST(1,7)
144 case MFX_RATECONTROL_LA:
155 q->
extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
158 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
161 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
166 q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
167 q->extco2.Header.BufferSz =
sizeof(q->extco2);
169 #if QSV_VERSION_ATLEAST(1,7)
174 #if QSV_VERSION_ATLEAST(1,8)
178 q->
extparam[1] = (mfxExtBuffer *)&q->extco2;
193 mfxExtCodingOptionSPSPPS extradata = {
194 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
195 .Header.BufferSz =
sizeof(extradata),
196 .SPSBuffer = sps_buf, .SPSBufSize =
sizeof(sps_buf),
197 .PPSBuffer = pps_buf, .PPSBufSize =
sizeof(pps_buf)
200 mfxExtBuffer *ext_buffers[] = {
201 (mfxExtBuffer*)&extradata,
207 q->
param.ExtParam = ext_buffers;
210 ret = MFXVideoENCODE_GetVideoParam(q->
session, &q->
param);
216 if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)) {
226 memcpy(avctx->
extradata, sps_buf, extradata.SPSBufSize);
228 memcpy(avctx->
extradata + extradata.SPSBufSize, pps_buf, extradata.PPSBufSize);
229 avctx->
extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
239 q->
param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
243 (
sizeof(
AVPacket) +
sizeof(mfxSyncPoint) +
sizeof(mfxBitstream*)));
274 if (MFX_WRN_PARTIAL_ACCELERATION==ret) {
276 }
else if (ret < 0) {
338 mfxFrameSurface1 **surface)
385 MFX_PICSTRUCT_FIELD_BFF;
398 qf->surface = &qf->surface_internal;
400 *surface = qf->surface;
407 if (q->
param.mfx.CodecId == MFX_CODEC_AVC) {
408 if (q->
param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
409 q->
param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
410 q->
param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
412 "Interlaced coding is supported"
413 " at Main/High Profile Level 2.1-4.1\n");
423 mfxFrameSurface1 *surf =
NULL;
424 mfxSyncPoint sync =
NULL;
446 bs->Data = new_pkt.
data;
447 bs->MaxLength = new_pkt.
size;
450 ret = MFXVideoENCODE_EncodeFrameAsync(q->
session,
NULL, surf, bs, &sync);
451 if (ret == MFX_WRN_DEVICE_BUSY) {
461 if (ret == MFX_ERR_MORE_DATA)
467 if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM) {
472 "EncodeFrameAsync returned 'incompatible param' code\n");
489 MFXVideoCORE_SyncOperation(q->
session, sync, 60000);
493 new_pkt.size = bs->DataLength;
495 if (bs->FrameType & MFX_FRAMETYPE_IDR ||
496 bs->FrameType & MFX_FRAMETYPE_xIDR)
499 #if FF_API_CODED_FRAME
501 if (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI)
503 else if (bs->FrameType & MFX_FRAMETYPE_P || bs->FrameType & MFX_FRAMETYPE_xP)
505 else if (bs->FrameType & MFX_FRAMETYPE_B || bs->FrameType & MFX_FRAMETYPE_xB)
513 if (
pkt->
size < new_pkt.size) {
520 memcpy(
pkt->
data, new_pkt.data, new_pkt.size);
540 MFXVideoENCODE_Close(q->
session);
This structure describes decoded (raw) audio or video data.
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
#define AV_LOG_WARNING
Something somehow does not look correct.
memory handling functions
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
int repeat_pict
When decoding, this signals how much the picture must be delayed.
static int get_free_frame(QSVEncContext *q, QSVFrame **f)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
mfxFrameSurface1 * surface
int av_usleep(unsigned usec)
Sleep for a period of time.
static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
float i_quant_offset
qscale offset between P and I-frames
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.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, AVPacket *pkt, const AVFrame *frame, int *got_packet)
AVFifoBuffer * async_fifo
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
void * hwaccel_context
Hardware accelerator context.
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
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...
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AV_LOG_VERBOSE
Detailed information.
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
int interlaced_frame
The content of the picture is interlaced.
int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static int submit_frame(QSVEncContext *q, const AVFrame *frame, mfxFrameSurface1 **surface)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define QSV_VERSION_ATLEAST(MAJOR, MINOR)
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
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 flags
AV_CODEC_FLAG_*.
int rc_max_rate
maximum bitrate
float i_quant_factor
qscale factor between P and I-frames If > 0 then the last p frame quantizer will be used (q= lastp_q*...
#define FF_CODER_TYPE_VLC
int ff_qsv_close_internal_session(QSVSession *qs)
Libavcodec external API header.
static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q)
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
int refs
number of reference frames
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
int bit_rate
the average bitrate
enum AVPictureType pict_type
Picture type of the frame.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
int width
picture width / height.
int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
mfxFrameSurface1 surface_internal
preferred ID for MPEG-1/2 video decoding
int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs, const char *load_plugins)
Initialize a MSDK session.
#define FF_ARRAY_ELEMS(a)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
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 linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
rational number numerator/denominator
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
float b_quant_offset
qscale offset between IP and B-frames
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int global_quality
Global quality for codecs which cannot change it per frame.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
common internal and external API header
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
int ff_qsv_error(int mfx_err)
Convert a libmfx error code into a ffmpeg error code.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int slices
Number of slices.
#define FF_ENABLE_DEPRECATION_WARNINGS
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
int top_field_first
If the content is interlaced, is top field displayed first.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int look_ahead_downsampling
static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
mfxExtBuffer * extparam[1]
#define AV_CODEC_FLAG_CLOSED_GOP
Allow non spec compliant speedup tricks.
This structure stores compressed data.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static void clear_unused_frames(QSVEncContext *q)