FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qsvenc.c
Go to the documentation of this file.
1 /*
2  * Intel MediaSDK QSV encoder utility functions
3  *
4  * copyright (c) 2013 Yukinori Yamazoe
5  * copyright (c) 2015 Anton Khirnov
6  *
7  * This file is part of FFmpeg.
8  *
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.
13  *
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.
18  *
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
22  */
23 
24 #include <string.h>
25 #include <sys/types.h>
26 #include <mfx/mfxvideo.h>
27 
28 #include "libavutil/common.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/log.h"
31 #include "libavutil/time.h"
32 #include "libavutil/imgutils.h"
33 
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "qsv.h"
37 #include "qsv_internal.h"
38 #include "qsvenc.h"
39 
41 {
42  const char *ratecontrol_desc;
43 
44  float quant;
45  int ret;
46 
47  ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
48  if (ret < 0)
49  return AVERROR_BUG;
50  q->param.mfx.CodecId = ret;
51 
52  q->width_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16;
53 
54  if (avctx->level > 0)
55  q->param.mfx.CodecLevel = avctx->level;
56 
57  q->param.mfx.CodecProfile = q->profile;
58  q->param.mfx.TargetUsage = q->preset;
59  q->param.mfx.GopPicSize = FFMAX(0, avctx->gop_size);
60  q->param.mfx.GopRefDist = FFMAX(-1, avctx->max_b_frames) + 1;
61  q->param.mfx.GopOptFlag = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ?
62  MFX_GOP_CLOSED : 0;
63  q->param.mfx.IdrInterval = q->idr_interval;
64  q->param.mfx.NumSlice = avctx->slices;
65  q->param.mfx.NumRefFrame = FFMAX(0, avctx->refs);
66  q->param.mfx.EncodedOrder = 0;
67  q->param.mfx.BufferSizeInKB = 0;
68 
69  q->param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
70  q->param.mfx.FrameInfo.CropX = 0;
71  q->param.mfx.FrameInfo.CropY = 0;
72  q->param.mfx.FrameInfo.CropW = avctx->width;
73  q->param.mfx.FrameInfo.CropH = avctx->height;
74  q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num;
75  q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den;
76  q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
77  q->param.mfx.FrameInfo.BitDepthLuma = 8;
78  q->param.mfx.FrameInfo.BitDepthChroma = 8;
79  q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align);
80 
81  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
82  /* A true field layout (TFF or BFF) is not important here,
83  it will specified later during frame encoding. But it is important
84  to specify is frame progressive or not because allowed heigh alignment
85  does depend by this.
86  */
87  q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
88  q->height_align = 32;
89  } else {
90  q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
91  q->height_align = 16;
92  }
93  q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, q->height_align);
94 
95  if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
96  q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
97  q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
98  } else {
99  q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
100  q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
101  }
102 
103  if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
104  q->param.mfx.RateControlMethod = MFX_RATECONTROL_CQP;
105  ratecontrol_desc = "constant quantization parameter (CQP)";
106  } else if (avctx->rc_max_rate == avctx->bit_rate) {
107  q->param.mfx.RateControlMethod = MFX_RATECONTROL_CBR;
108  ratecontrol_desc = "constant bitrate (CBR)";
109  } else if (!avctx->rc_max_rate) {
110 #if QSV_VERSION_ATLEAST(1,7)
111  if (q->look_ahead) {
112  q->param.mfx.RateControlMethod = MFX_RATECONTROL_LA;
113  ratecontrol_desc = "lookahead (LA)";
114  } else
115 #endif
116  {
117  q->param.mfx.RateControlMethod = MFX_RATECONTROL_AVBR;
118  ratecontrol_desc = "average variable bitrate (AVBR)";
119  }
120  } else {
121  q->param.mfx.RateControlMethod = MFX_RATECONTROL_VBR;
122  ratecontrol_desc = "variable bitrate (VBR)";
123  }
124 
125  av_log(avctx, AV_LOG_VERBOSE, "Using the %s ratecontrol method\n", ratecontrol_desc);
126 
127  switch (q->param.mfx.RateControlMethod) {
128  case MFX_RATECONTROL_CBR:
129  case MFX_RATECONTROL_VBR:
130  q->param.mfx.InitialDelayInKB = avctx->rc_initial_buffer_occupancy / 1000;
131  q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
132  q->param.mfx.MaxKbps = avctx->rc_max_rate / 1000;
133  break;
134  case MFX_RATECONTROL_CQP:
135  quant = avctx->global_quality / FF_QP2LAMBDA;
136 
137  q->param.mfx.QPI = av_clip(quant * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
138  q->param.mfx.QPP = av_clip(quant, 0, 51);
139  q->param.mfx.QPB = av_clip(quant * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51);
140 
141  break;
142  case MFX_RATECONTROL_AVBR:
143 #if QSV_VERSION_ATLEAST(1,7)
144  case MFX_RATECONTROL_LA:
145 #endif
146  q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
147  q->param.mfx.Convergence = q->avbr_convergence;
148  q->param.mfx.Accuracy = q->avbr_accuracy;
149  break;
150  }
151 
152  // the HEVC encoder plugin currently fails if coding options
153  // are provided
154  if (avctx->codec_id != AV_CODEC_ID_HEVC) {
155  q->extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
156  q->extco.Header.BufferSz = sizeof(q->extco);
157  q->extco.CAVLC = avctx->coder_type == FF_CODER_TYPE_VLC ?
158  MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
159 
160  q->extco.PicTimingSEI = q->pic_timing_sei ?
161  MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
162 
163  q->extparam[0] = (mfxExtBuffer *)&q->extco;
164 
165 #if QSV_VERSION_ATLEAST(1,6)
166  q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
167  q->extco2.Header.BufferSz = sizeof(q->extco2);
168 
169 #if QSV_VERSION_ATLEAST(1,7)
170  // valid value range is from 10 to 100 inclusive
171  // to instruct the encoder to use the default value this should be set to zero
172  q->extco2.LookAheadDepth = q->look_ahead_depth != 0 ? FFMAX(10, q->look_ahead_depth) : 0;
173 #endif
174 #if QSV_VERSION_ATLEAST(1,8)
175  q->extco2.LookAheadDS = q->look_ahead_downsampling;
176 #endif
177 
178  q->extparam[1] = (mfxExtBuffer *)&q->extco2;
179 
180 #endif
181  q->param.ExtParam = q->extparam;
182  q->param.NumExtParam = FF_ARRAY_ELEMS(q->extparam);
183  }
184 
185  return 0;
186 }
187 
189 {
190  uint8_t sps_buf[128];
191  uint8_t pps_buf[128];
192 
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)
198  };
199 
200  mfxExtBuffer *ext_buffers[] = {
201  (mfxExtBuffer*)&extradata,
202  };
203 
204  int need_pps = avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO;
205  int ret;
206 
207  q->param.ExtParam = ext_buffers;
208  q->param.NumExtParam = FF_ARRAY_ELEMS(ext_buffers);
209 
210  ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
211  if (ret < 0)
212  return ff_qsv_error(ret);
213 
214  q->packet_size = q->param.mfx.BufferSizeInKB * 1000;
215 
216  if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)) {
217  av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n");
218  return AVERROR_UNKNOWN;
219  }
220 
221  avctx->extradata = av_malloc(extradata.SPSBufSize + need_pps * extradata.PPSBufSize +
223  if (!avctx->extradata)
224  return AVERROR(ENOMEM);
225 
226  memcpy(avctx->extradata, sps_buf, extradata.SPSBufSize);
227  if (need_pps)
228  memcpy(avctx->extradata + extradata.SPSBufSize, pps_buf, extradata.PPSBufSize);
229  avctx->extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
230  memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
231 
232  return 0;
233 }
234 
236 {
237  int ret;
238 
239  q->param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
240  q->param.AsyncDepth = q->async_depth;
241 
242  q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
243  (sizeof(AVPacket) + sizeof(mfxSyncPoint) + sizeof(mfxBitstream*)));
244  if (!q->async_fifo)
245  return AVERROR(ENOMEM);
246 
247  if (avctx->hwaccel_context) {
248  AVQSVContext *qsv = avctx->hwaccel_context;
249 
250  q->session = qsv->session;
251  q->param.IOPattern = qsv->iopattern;
252  }
253 
254  if (!q->session) {
255  ret = ff_qsv_init_internal_session(avctx, &q->internal_qs,
256  q->load_plugins);
257  if (ret < 0)
258  return ret;
259 
260  q->session = q->internal_qs.session;
261  }
262 
263  ret = init_video_param(avctx, q);
264  if (ret < 0)
265  return ret;
266 
267  ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req);
268  if (ret < 0) {
269  av_log(avctx, AV_LOG_ERROR, "Error querying the encoding parameters\n");
270  return ff_qsv_error(ret);
271  }
272 
273  ret = MFXVideoENCODE_Init(q->session, &q->param);
274  if (MFX_WRN_PARTIAL_ACCELERATION==ret) {
275  av_log(avctx, AV_LOG_WARNING, "Encoder will work with partial HW acceleration\n");
276  } else if (ret < 0) {
277  av_log(avctx, AV_LOG_ERROR, "Error initializing the encoder\n");
278  return ff_qsv_error(ret);
279  }
280 
281  ret = qsv_retrieve_enc_params(avctx, q);
282  if (ret < 0) {
283  av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n");
284  return ret;
285  }
286 
287  q->avctx = avctx;
288 
289  return 0;
290 }
291 
293 {
294  QSVFrame *cur = q->work_frames;
295  while (cur) {
296  if (cur->surface && !cur->surface->Data.Locked) {
297  cur->surface = NULL;
298  av_frame_unref(cur->frame);
299  }
300  cur = cur->next;
301  }
302 }
303 
305 {
306  QSVFrame *frame, **last;
307 
309 
310  frame = q->work_frames;
311  last = &q->work_frames;
312  while (frame) {
313  if (!frame->surface) {
314  *f = frame;
315  return 0;
316  }
317 
318  last = &frame->next;
319  frame = frame->next;
320  }
321 
322  frame = av_mallocz(sizeof(*frame));
323  if (!frame)
324  return AVERROR(ENOMEM);
325  frame->frame = av_frame_alloc();
326  if (!frame->frame) {
327  av_freep(&frame);
328  return AVERROR(ENOMEM);
329  }
330  *last = frame;
331 
332  *f = frame;
333 
334  return 0;
335 }
336 
337 static int submit_frame(QSVEncContext *q, const AVFrame *frame,
338  mfxFrameSurface1 **surface)
339 {
340  QSVFrame *qf;
341  int ret;
342 
343  ret = get_free_frame(q, &qf);
344  if (ret < 0)
345  return ret;
346 
347  if (frame->format == AV_PIX_FMT_QSV) {
348  ret = av_frame_ref(qf->frame, frame);
349  if (ret < 0)
350  return ret;
351 
352  qf->surface = (mfxFrameSurface1*)qf->frame->data[3];
353  *surface = qf->surface;
354  return 0;
355  }
356 
357  /* make a copy if the input is not padded as libmfx requires */
358  if ( frame->height & (q->height_align - 1) ||
359  frame->linesize[0] & (q->width_align - 1)) {
360  qf->frame->height = FFALIGN(frame->height, q->height_align);
361  qf->frame->width = FFALIGN(frame->width, q->width_align);
362 
364  if (ret < 0)
365  return ret;
366 
367  qf->frame->height = frame->height;
368  qf->frame->width = frame->width;
369  ret = av_frame_copy(qf->frame, frame);
370  if (ret < 0) {
371  av_frame_unref(qf->frame);
372  return ret;
373  }
374  } else {
375  ret = av_frame_ref(qf->frame, frame);
376  if (ret < 0)
377  return ret;
378  }
379 
380  qf->surface_internal.Info = q->param.mfx.FrameInfo;
381 
382  qf->surface_internal.Info.PicStruct =
383  !frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
384  frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
385  MFX_PICSTRUCT_FIELD_BFF;
386  if (frame->repeat_pict == 1)
387  qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
388  else if (frame->repeat_pict == 2)
389  qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
390  else if (frame->repeat_pict == 4)
391  qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
392 
393  qf->surface_internal.Data.PitchLow = qf->frame->linesize[0];
394  qf->surface_internal.Data.Y = qf->frame->data[0];
395  qf->surface_internal.Data.UV = qf->frame->data[1];
396  qf->surface_internal.Data.TimeStamp = av_rescale_q(frame->pts, q->avctx->time_base, (AVRational){1, 90000});
397 
398  qf->surface = &qf->surface_internal;
399 
400  *surface = qf->surface;
401 
402  return 0;
403 }
404 
406 {
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)
411  av_log(avctx, AV_LOG_WARNING,
412  "Interlaced coding is supported"
413  " at Main/High Profile Level 2.1-4.1\n");
414  }
415 }
416 
418  AVPacket *pkt, const AVFrame *frame, int *got_packet)
419 {
420  AVPacket new_pkt = { 0 };
421  mfxBitstream *bs;
422 
423  mfxFrameSurface1 *surf = NULL;
424  mfxSyncPoint sync = NULL;
425  int ret;
426 
427  if (frame) {
428  ret = submit_frame(q, frame, &surf);
429  if (ret < 0) {
430  av_log(avctx, AV_LOG_ERROR, "Error submitting the frame for encoding.\n");
431  return ret;
432  }
433  }
434 
435  ret = av_new_packet(&new_pkt, q->packet_size);
436  if (ret < 0) {
437  av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
438  return ret;
439  }
440 
441  bs = av_mallocz(sizeof(*bs));
442  if (!bs) {
443  av_packet_unref(&new_pkt);
444  return AVERROR(ENOMEM);
445  }
446  bs->Data = new_pkt.data;
447  bs->MaxLength = new_pkt.size;
448 
449  do {
450  ret = MFXVideoENCODE_EncodeFrameAsync(q->session, NULL, surf, bs, &sync);
451  if (ret == MFX_WRN_DEVICE_BUSY) {
452  av_usleep(500);
453  continue;
454  }
455  break;
456  } while ( 1 );
457 
458  if (ret < 0) {
459  av_packet_unref(&new_pkt);
460  av_freep(&bs);
461  if (ret == MFX_ERR_MORE_DATA)
462  return 0;
463  av_log(avctx, AV_LOG_ERROR, "EncodeFrameAsync returned %d\n", ret);
464  return ff_qsv_error(ret);
465  }
466 
467  if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM) {
468  if (frame->interlaced_frame)
469  print_interlace_msg(avctx, q);
470  else
471  av_log(avctx, AV_LOG_WARNING,
472  "EncodeFrameAsync returned 'incompatible param' code\n");
473  }
474  if (sync) {
475  av_fifo_generic_write(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
476  av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
477  av_fifo_generic_write(q->async_fifo, &bs, sizeof(bs), NULL);
478  } else {
479  av_packet_unref(&new_pkt);
480  av_freep(&bs);
481  }
482 
483  if (!av_fifo_space(q->async_fifo) ||
484  (!frame && av_fifo_size(q->async_fifo))) {
485  av_fifo_generic_read(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
486  av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
487  av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
488 
489  MFXVideoCORE_SyncOperation(q->session, sync, 60000);
490 
491  new_pkt.dts = av_rescale_q(bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
492  new_pkt.pts = av_rescale_q(bs->TimeStamp, (AVRational){1, 90000}, avctx->time_base);
493  new_pkt.size = bs->DataLength;
494 
495  if (bs->FrameType & MFX_FRAMETYPE_IDR ||
496  bs->FrameType & MFX_FRAMETYPE_xIDR)
497  new_pkt.flags |= AV_PKT_FLAG_KEY;
498 
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)
508 #endif
509 
510  av_freep(&bs);
511 
512  if (pkt->data) {
513  if (pkt->size < new_pkt.size) {
514  av_log(avctx, AV_LOG_ERROR, "Submitted buffer not large enough: %d < %d\n",
515  pkt->size, new_pkt.size);
516  av_packet_unref(&new_pkt);
517  return AVERROR(EINVAL);
518  }
519 
520  memcpy(pkt->data, new_pkt.data, new_pkt.size);
521  pkt->size = new_pkt.size;
522 
523  ret = av_packet_copy_props(pkt, &new_pkt);
524  av_packet_unref(&new_pkt);
525  if (ret < 0)
526  return ret;
527  } else
528  *pkt = new_pkt;
529 
530  *got_packet = 1;
531  }
532 
533  return 0;
534 }
535 
537 {
538  QSVFrame *cur;
539 
540  MFXVideoENCODE_Close(q->session);
541  q->session = NULL;
542 
544 
545  cur = q->work_frames;
546  while (cur) {
547  q->work_frames = cur->next;
548  av_frame_free(&cur->frame);
549  av_freep(&cur);
550  cur = q->work_frames;
551  }
552 
553  while (q->async_fifo && av_fifo_size(q->async_fifo)) {
554  AVPacket pkt;
555  mfxSyncPoint sync;
556  mfxBitstream *bs;
557 
558  av_fifo_generic_read(q->async_fifo, &pkt, sizeof(pkt), NULL);
559  av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
560  av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
561 
562  av_freep(&bs);
563  av_packet_unref(&pkt);
564  }
566  q->async_fifo = NULL;
567 
568  return 0;
569 }
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3302
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:776
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
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...
Definition: avcodec.h:1780
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:2643
mfxFrameAllocRequest req
Definition: qsvenc.h:50
int avbr_accuracy
Definition: qsvenc.h:67
QSVFrame * work_frames
Definition: qsvenc.h:40
int num
numerator
Definition: rational.h:44
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: frame.h:362
int look_ahead_depth
Definition: qsvenc.h:71
static int get_free_frame(QSVEncContext *q, QSVFrame **f)
Definition: qsvenc.c:304
int size
Definition: avcodec.h:1424
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1902
int packet_size
Definition: qsvenc.h:45
mfxFrameSurface1 * surface
Definition: qsv_internal.h:54
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:76
static AVPacket pkt
static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:405
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1835
#define FFALIGN(x, a)
Definition: common.h:86
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.
Definition: fifo.c:122
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1631
int look_ahead
Definition: qsvenc.h:70
int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: qsvenc.c:417
mfxVideoParam param
Definition: qsvenc.h:49
uint8_t
AVFifoBuffer * async_fifo
Definition: qsvenc.h:60
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2934
mfxExtCodingOption extco
Definition: qsvenc.h:52
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...
Definition: avcodec.h:1789
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:365
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
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...
Definition: fifo.c:82
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1617
static AVFrame * frame
int coder_type
coder type
Definition: avcodec.h:2657
uint8_t * data
Definition: avcodec.h:1423
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:55
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:367
int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:536
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
static int submit_frame(QSVEncContext *q, const AVFrame *frame, mfxFrameSurface1 **surface)
Definition: qsvenc.c:337
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
#define QSV_VERSION_ATLEAST(MAJOR, MINOR)
Definition: qsv_internal.h:48
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
int width
width and height of the video frame
Definition: frame.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
struct QSVFrame * next
Definition: qsv_internal.h:60
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
char * load_plugins
Definition: qsvenc.h:74
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.
Definition: fifo.c:177
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1597
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2604
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*...
Definition: avcodec.h:1828
#define FF_CODER_TYPE_VLC
Definition: avcodec.h:2645
int iopattern
Definition: qsv.h:28
#define FFMAX(a, b)
Definition: common.h:79
int ff_qsv_close_internal_session(QSVSession *qs)
Definition: qsv.c:252
Libavcodec external API header.
static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:188
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:683
AVFrame * frame
Definition: qsv_internal.h:53
int refs
number of reference frames
Definition: avcodec.h:2178
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:522
int bit_rate
the average bitrate
Definition: avcodec.h:1567
AVCodecContext * avctx
Definition: qsvenc.h:38
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:735
int idr_interval
Definition: qsvenc.h:64
int width
picture width / height.
Definition: avcodec.h:1681
int preset
Definition: qsvenc.h:66
int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
Definition: qsv.c:33
int level
level
Definition: avcodec.h:3204
mfxFrameSurface1 surface_internal
Definition: qsv_internal.h:56
int async_depth
Definition: qsvenc.h:63
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:107
int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs, const char *load_plugins)
Initialize a MSDK session.
Definition: qsv.c:171
#define FF_ARRAY_ELEMS(a)
int width_align
Definition: qsvenc.h:46
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
enum AVCodecID codec_id
Definition: avcodec.h:1519
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 ...
Definition: fifo.c:77
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1502
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:550
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1040
int profile
Definition: qsvenc.h:65
int extradata_size
Definition: avcodec.h:1618
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
int height_align
Definition: qsvenc.h:47
QSVSession internal_qs
Definition: qsvenc.h:43
int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:235
rational number numerator/denominator
Definition: rational.h:43
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:261
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1804
const uint8_t * quant
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:464
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1583
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1707
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
common internal api header.
common internal and external API header
Bi-dir predicted.
Definition: avutil.h:268
int avbr_convergence
Definition: qsvenc.h:68
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:3024
int ff_qsv_error(int mfx_err)
Convert a libmfx error code into a ffmpeg error code.
Definition: qsv.c:54
int den
denominator
Definition: rational.h:45
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:636
int slices
Number of slices.
Definition: avcodec.h:2253
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:372
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:219
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1422
int look_ahead_downsampling
Definition: qsvenc.h:72
static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:40
int height
Definition: frame.h:220
#define av_freep(p)
mfxExtBuffer * extparam[1]
Definition: qsvenc.h:57
#define AV_CODEC_FLAG_CLOSED_GOP
Allow non spec compliant speedup tricks.
Definition: avcodec.h:801
This structure stores compressed data.
Definition: avcodec.h:1400
mfxSession session
Definition: qsvenc.h:42
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1216
mfxSession session
Definition: qsv.h:27
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
mfxSession session
Definition: qsv_internal.h:64
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
int pic_timing_sei
Definition: qsvenc.h:69
Predicted.
Definition: avutil.h:267
static void clear_unused_frames(QSVEncContext *q)
Definition: qsvenc.c:292