25 #include <sys/types.h>
26 #include <mfx/mfxvideo.h>
42 const char *ratecontrol_desc;
64 q->
param.mfx.EncodedOrder = 0;
65 q->
param.mfx.BufferSizeInKB = 0;
67 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.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
77 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
78 q->
param.mfx.FrameInfo.BitDepthLuma = 8;
79 q->
param.mfx.FrameInfo.BitDepthChroma = 8;
90 q->
param.mfx.RateControlMethod = MFX_RATECONTROL_CQP;
91 ratecontrol_desc =
"constant quantization parameter (CQP)";
93 q->
param.mfx.RateControlMethod = MFX_RATECONTROL_CBR;
94 ratecontrol_desc =
"constant bitrate (CBR)";
96 q->
param.mfx.RateControlMethod = MFX_RATECONTROL_AVBR;
97 ratecontrol_desc =
"average variable bitrate (AVBR)";
99 q->
param.mfx.RateControlMethod = MFX_RATECONTROL_VBR;
100 ratecontrol_desc =
"variable bitrate (VBR)";
105 switch (q->
param.mfx.RateControlMethod) {
106 case MFX_RATECONTROL_CBR:
107 case MFX_RATECONTROL_VBR:
112 case MFX_RATECONTROL_CQP:
116 q->
param.mfx.QPP = av_clip(quant, 0, 51);
120 case MFX_RATECONTROL_AVBR:
127 q->
extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
130 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
145 mfxExtCodingOptionSPSPPS extradata = {
146 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
147 .Header.BufferSz =
sizeof(extradata),
148 .SPSBuffer = sps_buf, .SPSBufSize =
sizeof(sps_buf),
149 .PPSBuffer = pps_buf, .PPSBufSize =
sizeof(pps_buf)
152 mfxExtBuffer *ext_buffers[] = {
153 (mfxExtBuffer*)&extradata,
158 q->
param.ExtParam = ext_buffers;
161 ret = MFXVideoENCODE_GetVideoParam(q->
session, &q->
param);
167 if (!extradata.SPSBufSize || !extradata.PPSBufSize) {
177 memcpy(avctx->
extradata, sps_buf, extradata.SPSBufSize);
178 memcpy(avctx->
extradata + extradata.SPSBufSize, pps_buf, extradata.PPSBufSize);
179 avctx->
extradata_size = extradata.SPSBufSize + extradata.PPSBufSize;
189 q->
param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
284 mfxFrameSurface1 **surface)
330 MFX_PICSTRUCT_FIELD_BFF;
343 qf->surface = &qf->surface_internal;
345 *surface = qf->surface;
352 if (q->
param.mfx.CodecId == MFX_CODEC_AVC) {
353 if (q->
param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
354 q->
param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
355 q->
param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
357 "Interlaced coding is supported"
358 " at Main/High Profile Level 2.1-4.1\n");
365 mfxBitstream bs = { { { 0 } } };
367 mfxFrameSurface1 *surf =
NULL;
368 mfxSyncPoint sync =
NULL;
385 bs.MaxLength = pkt->
size;
388 ret = MFXVideoENCODE_EncodeFrameAsync(q->
session,
NULL, surf, &bs, &sync);
389 if (ret == MFX_WRN_DEVICE_BUSY)
394 return (ret == MFX_ERR_MORE_DATA) ? 0 :
ff_qsv_error(ret);
400 MFXVideoCORE_SyncOperation(q->
session, sync, 60000);
402 if (bs.FrameType & MFX_FRAMETYPE_I || bs.FrameType & MFX_FRAMETYPE_xI)
404 else if (bs.FrameType & MFX_FRAMETYPE_P || bs.FrameType & MFX_FRAMETYPE_xP)
406 else if (bs.FrameType & MFX_FRAMETYPE_B || bs.FrameType & MFX_FRAMETYPE_xB)
413 if (bs.FrameType & MFX_FRAMETYPE_IDR ||
414 bs.FrameType & MFX_FRAMETYPE_xIDR)
427 MFXVideoENCODE_Close(q->
session);
This structure describes decoded (raw) audio or video data.
#define AV_LOG_WARNING
Something somehow does not look correct.
memory handling functions
AVFrame * coded_frame
the picture in the bitstream
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...
#define FF_ARRAY_ELEMS(a)
mfxFrameSurface1 * surface
int av_usleep(unsigned usec)
Sleep for a period of time.
mfxSession internal_session
static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
float i_quant_offset
qscale offset between P and I-frames
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)
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).
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AV_LOG_VERBOSE
Detailed information.
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.
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.
#define CODEC_FLAG_QSCALE
Use fixed qscale.
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
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 flags
A combination of AV_PKT_FLAG values.
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int refs
number of reference frames
int bit_rate
the average bitrate
enum AVPictureType pict_type
Picture type of the frame.
int width
picture width / height.
int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
int ff_alloc_packet(AVPacket *avpkt, int size)
mfxFrameSurface1 surface_internal
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
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_init_internal_session(AVCodecContext *avctx, mfxSession *session)
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
#define CODEC_FLAG_CLOSED_GOP
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
common internal api header.
common internal and external API header
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.
int slices
Number of slices.
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...
static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
mfxExtBuffer * extparam[1]
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)