FFmpeg
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/hwcontext.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/log.h"
33 #include "libavutil/time.h"
34 #include "libavutil/imgutils.h"
35 #include "libavcodec/bytestream.h"
36 
37 #include "avcodec.h"
38 #include "internal.h"
39 #include "packet_internal.h"
40 #include "qsv.h"
41 #include "qsv_internal.h"
42 #include "qsvenc.h"
43 
44 static const struct {
45  mfxU16 profile;
46  const char *name;
47 } profile_names[] = {
48  { MFX_PROFILE_AVC_BASELINE, "baseline" },
49  { MFX_PROFILE_AVC_MAIN, "main" },
50  { MFX_PROFILE_AVC_EXTENDED, "extended" },
51  { MFX_PROFILE_AVC_HIGH, "high" },
52 #if QSV_VERSION_ATLEAST(1, 15)
53  { MFX_PROFILE_AVC_HIGH_422, "high 422" },
54 #endif
55 #if QSV_VERSION_ATLEAST(1, 4)
56  { MFX_PROFILE_AVC_CONSTRAINED_BASELINE, "constrained baseline" },
57  { MFX_PROFILE_AVC_CONSTRAINED_HIGH, "constrained high" },
58  { MFX_PROFILE_AVC_PROGRESSIVE_HIGH, "progressive high" },
59 #endif
60  { MFX_PROFILE_MPEG2_SIMPLE, "simple" },
61  { MFX_PROFILE_MPEG2_MAIN, "main" },
62  { MFX_PROFILE_MPEG2_HIGH, "high" },
63  { MFX_PROFILE_VC1_SIMPLE, "simple" },
64  { MFX_PROFILE_VC1_MAIN, "main" },
65  { MFX_PROFILE_VC1_ADVANCED, "advanced" },
66 #if QSV_VERSION_ATLEAST(1, 8)
67  { MFX_PROFILE_HEVC_MAIN, "main" },
68  { MFX_PROFILE_HEVC_MAIN10, "main10" },
69  { MFX_PROFILE_HEVC_MAINSP, "mainsp" },
70  { MFX_PROFILE_HEVC_REXT, "rext" },
71 #endif
72 };
73 
74 static const char *print_profile(mfxU16 profile)
75 {
76  int i;
77  for (i = 0; i < FF_ARRAY_ELEMS(profile_names); i++)
79  return profile_names[i].name;
80  return "unknown";
81 }
82 
83 static const struct {
84  mfxU16 rc_mode;
85  const char *name;
86 } rc_names[] = {
87  { MFX_RATECONTROL_CBR, "CBR" },
88  { MFX_RATECONTROL_VBR, "VBR" },
89  { MFX_RATECONTROL_CQP, "CQP" },
90 #if QSV_HAVE_AVBR
91  { MFX_RATECONTROL_AVBR, "AVBR" },
92 #endif
93 #if QSV_HAVE_LA
94  { MFX_RATECONTROL_LA, "LA" },
95 #endif
96 #if QSV_HAVE_ICQ
97  { MFX_RATECONTROL_ICQ, "ICQ" },
98  { MFX_RATECONTROL_LA_ICQ, "LA_ICQ" },
99 #endif
100 #if QSV_HAVE_VCM
101  { MFX_RATECONTROL_VCM, "VCM" },
102 #endif
103 #if QSV_VERSION_ATLEAST(1, 10)
104  { MFX_RATECONTROL_LA_EXT, "LA_EXT" },
105 #endif
106 #if QSV_HAVE_LA_HRD
107  { MFX_RATECONTROL_LA_HRD, "LA_HRD" },
108 #endif
109 #if QSV_HAVE_QVBR
110  { MFX_RATECONTROL_QVBR, "QVBR" },
111 #endif
112 };
113 
114 static const char *print_ratecontrol(mfxU16 rc_mode)
115 {
116  int i;
117  for (i = 0; i < FF_ARRAY_ELEMS(rc_names); i++)
118  if (rc_mode == rc_names[i].rc_mode)
119  return rc_names[i].name;
120  return "unknown";
121 }
122 
123 static const char *print_threestate(mfxU16 val)
124 {
125  if (val == MFX_CODINGOPTION_ON)
126  return "ON";
127  else if (val == MFX_CODINGOPTION_OFF)
128  return "OFF";
129  return "unknown";
130 }
131 
133  mfxExtBuffer **coding_opts)
134 {
135  mfxInfoMFX *info = &q->param.mfx;
136 
137  mfxExtCodingOption *co = (mfxExtCodingOption*)coding_opts[0];
138 #if QSV_HAVE_CO2
139  mfxExtCodingOption2 *co2 = (mfxExtCodingOption2*)coding_opts[1];
140 #endif
141 #if QSV_HAVE_CO3
142  mfxExtCodingOption3 *co3 = (mfxExtCodingOption3*)coding_opts[2];
143 #endif
144 #if QSV_HAVE_EXT_HEVC_TILES
145  mfxExtHEVCTiles *exthevctiles = (mfxExtHEVCTiles *)coding_opts[3 + QSV_HAVE_CO_VPS];
146 #endif
147 
148  av_log(avctx, AV_LOG_VERBOSE, "profile: %s; level: %"PRIu16"\n",
149  print_profile(info->CodecProfile), info->CodecLevel);
150 
151  av_log(avctx, AV_LOG_VERBOSE, "GopPicSize: %"PRIu16"; GopRefDist: %"PRIu16"; GopOptFlag: ",
152  info->GopPicSize, info->GopRefDist);
153  if (info->GopOptFlag & MFX_GOP_CLOSED)
154  av_log(avctx, AV_LOG_VERBOSE, "closed ");
155  if (info->GopOptFlag & MFX_GOP_STRICT)
156  av_log(avctx, AV_LOG_VERBOSE, "strict ");
157  av_log(avctx, AV_LOG_VERBOSE, "; IdrInterval: %"PRIu16"\n", info->IdrInterval);
158 
159  av_log(avctx, AV_LOG_VERBOSE, "TargetUsage: %"PRIu16"; RateControlMethod: %s\n",
160  info->TargetUsage, print_ratecontrol(info->RateControlMethod));
161 
162  if (info->RateControlMethod == MFX_RATECONTROL_CBR ||
163  info->RateControlMethod == MFX_RATECONTROL_VBR
164 #if QSV_HAVE_VCM
165  || info->RateControlMethod == MFX_RATECONTROL_VCM
166 #endif
167  ) {
168  av_log(avctx, AV_LOG_VERBOSE,
169  "BufferSizeInKB: %"PRIu16"; InitialDelayInKB: %"PRIu16"; TargetKbps: %"PRIu16"; MaxKbps: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n",
170  info->BufferSizeInKB, info->InitialDelayInKB, info->TargetKbps, info->MaxKbps, info->BRCParamMultiplier);
171  } else if (info->RateControlMethod == MFX_RATECONTROL_CQP) {
172  av_log(avctx, AV_LOG_VERBOSE, "QPI: %"PRIu16"; QPP: %"PRIu16"; QPB: %"PRIu16"\n",
173  info->QPI, info->QPP, info->QPB);
174  }
175 #if QSV_HAVE_AVBR
176  else if (info->RateControlMethod == MFX_RATECONTROL_AVBR) {
177  av_log(avctx, AV_LOG_VERBOSE,
178  "TargetKbps: %"PRIu16"; Accuracy: %"PRIu16"; Convergence: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n",
179  info->TargetKbps, info->Accuracy, info->Convergence, info->BRCParamMultiplier);
180  }
181 #endif
182 #if QSV_HAVE_LA
183  else if (info->RateControlMethod == MFX_RATECONTROL_LA
184 #if QSV_HAVE_LA_HRD
185  || info->RateControlMethod == MFX_RATECONTROL_LA_HRD
186 #endif
187  ) {
188  av_log(avctx, AV_LOG_VERBOSE,
189  "TargetKbps: %"PRIu16"; LookAheadDepth: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n",
190  info->TargetKbps, co2->LookAheadDepth, info->BRCParamMultiplier);
191  }
192 #endif
193 #if QSV_HAVE_ICQ
194  else if (info->RateControlMethod == MFX_RATECONTROL_ICQ) {
195  av_log(avctx, AV_LOG_VERBOSE, "ICQQuality: %"PRIu16"\n", info->ICQQuality);
196  } else if (info->RateControlMethod == MFX_RATECONTROL_LA_ICQ) {
197  av_log(avctx, AV_LOG_VERBOSE, "ICQQuality: %"PRIu16"; LookAheadDepth: %"PRIu16"\n",
198  info->ICQQuality, co2->LookAheadDepth);
199  }
200 #endif
201 #if QSV_HAVE_QVBR
202  else if (info->RateControlMethod == MFX_RATECONTROL_QVBR) {
203  av_log(avctx, AV_LOG_VERBOSE, "QVBRQuality: %"PRIu16"\n",
204  co3->QVBRQuality);
205  }
206 #endif
207  av_log(avctx, AV_LOG_VERBOSE, "NumSlice: %"PRIu16"; NumRefFrame: %"PRIu16"\n",
208  info->NumSlice, info->NumRefFrame);
209  av_log(avctx, AV_LOG_VERBOSE, "RateDistortionOpt: %s\n",
210  print_threestate(co->RateDistortionOpt));
211 
212 #if QSV_HAVE_EXT_HEVC_TILES
213  if (avctx->codec_id == AV_CODEC_ID_HEVC)
214  av_log(avctx, AV_LOG_VERBOSE, "NumTileColumns: %"PRIu16"; NumTileRows: %"PRIu16"\n",
215  exthevctiles->NumTileColumns, exthevctiles->NumTileRows);
216 #endif
217 
218 #if QSV_HAVE_CO2
219  av_log(avctx, AV_LOG_VERBOSE,
220  "RecoveryPointSEI: %s IntRefType: %"PRIu16"; IntRefCycleSize: %"PRIu16"; IntRefQPDelta: %"PRId16"\n",
221  print_threestate(co->RecoveryPointSEI), co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
222 
223  av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSize: %d; ", co2->MaxFrameSize);
224 #if QSV_HAVE_MAX_SLICE_SIZE
225  av_log(avctx, AV_LOG_VERBOSE, "MaxSliceSize: %d; ", co2->MaxSliceSize);
226 #endif
227  av_log(avctx, AV_LOG_VERBOSE, "\n");
228 
229  av_log(avctx, AV_LOG_VERBOSE,
230  "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
231  print_threestate(co2->BitrateLimit), print_threestate(co2->MBBRC),
232  print_threestate(co2->ExtBRC));
233 
234 #if QSV_HAVE_TRELLIS
235  av_log(avctx, AV_LOG_VERBOSE, "Trellis: ");
236  if (co2->Trellis & MFX_TRELLIS_OFF) {
237  av_log(avctx, AV_LOG_VERBOSE, "off");
238  } else if (!co2->Trellis) {
239  av_log(avctx, AV_LOG_VERBOSE, "auto");
240  } else {
241  if (co2->Trellis & MFX_TRELLIS_I) av_log(avctx, AV_LOG_VERBOSE, "I");
242  if (co2->Trellis & MFX_TRELLIS_P) av_log(avctx, AV_LOG_VERBOSE, "P");
243  if (co2->Trellis & MFX_TRELLIS_B) av_log(avctx, AV_LOG_VERBOSE, "B");
244  }
245  av_log(avctx, AV_LOG_VERBOSE, "\n");
246 #endif
247 
248 #if QSV_HAVE_VDENC
249  av_log(avctx, AV_LOG_VERBOSE, "VDENC: %s\n", print_threestate(info->LowPower));
250 #endif
251 
252 #if QSV_VERSION_ATLEAST(1, 8)
253  av_log(avctx, AV_LOG_VERBOSE,
254  "RepeatPPS: %s; NumMbPerSlice: %"PRIu16"; LookAheadDS: ",
255  print_threestate(co2->RepeatPPS), co2->NumMbPerSlice);
256  switch (co2->LookAheadDS) {
257  case MFX_LOOKAHEAD_DS_OFF: av_log(avctx, AV_LOG_VERBOSE, "off"); break;
258  case MFX_LOOKAHEAD_DS_2x: av_log(avctx, AV_LOG_VERBOSE, "2x"); break;
259  case MFX_LOOKAHEAD_DS_4x: av_log(avctx, AV_LOG_VERBOSE, "4x"); break;
260  default: av_log(avctx, AV_LOG_VERBOSE, "unknown"); break;
261  }
262  av_log(avctx, AV_LOG_VERBOSE, "\n");
263 
264  av_log(avctx, AV_LOG_VERBOSE, "AdaptiveI: %s; AdaptiveB: %s; BRefType: ",
265  print_threestate(co2->AdaptiveI), print_threestate(co2->AdaptiveB));
266  switch (co2->BRefType) {
267  case MFX_B_REF_OFF: av_log(avctx, AV_LOG_VERBOSE, "off"); break;
268  case MFX_B_REF_PYRAMID: av_log(avctx, AV_LOG_VERBOSE, "pyramid"); break;
269  default: av_log(avctx, AV_LOG_VERBOSE, "auto"); break;
270  }
271  av_log(avctx, AV_LOG_VERBOSE, "\n");
272 #endif
273 
274 #if QSV_VERSION_ATLEAST(1, 9)
275  av_log(avctx, AV_LOG_VERBOSE,
276  "MinQPI: %"PRIu8"; MaxQPI: %"PRIu8"; MinQPP: %"PRIu8"; MaxQPP: %"PRIu8"; MinQPB: %"PRIu8"; MaxQPB: %"PRIu8"\n",
277  co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
278 #endif
279 #endif
280 
281 #if QSV_HAVE_GPB
282  if (avctx->codec_id == AV_CODEC_ID_HEVC)
283  av_log(avctx, AV_LOG_VERBOSE,"GPB: %s\n", print_threestate(co3->GPB));
284 #endif
285 
286  if (avctx->codec_id == AV_CODEC_ID_H264) {
287  av_log(avctx, AV_LOG_VERBOSE, "Entropy coding: %s; MaxDecFrameBuffering: %"PRIu16"\n",
288  co->CAVLC == MFX_CODINGOPTION_ON ? "CAVLC" : "CABAC", co->MaxDecFrameBuffering);
289  av_log(avctx, AV_LOG_VERBOSE,
290  "NalHrdConformance: %s; SingleSeiNalUnit: %s; VuiVclHrdParameters: %s VuiNalHrdParameters: %s\n",
291  print_threestate(co->NalHrdConformance), print_threestate(co->SingleSeiNalUnit),
292  print_threestate(co->VuiVclHrdParameters), print_threestate(co->VuiNalHrdParameters));
293  } else if ((avctx->codec_id == AV_CODEC_ID_HEVC) && QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 28)) {
294  av_log(avctx, AV_LOG_VERBOSE,
295  "NalHrdConformance: %s; VuiNalHrdParameters: %s\n",
296  print_threestate(co->NalHrdConformance), print_threestate(co->VuiNalHrdParameters));
297  }
298 
299  av_log(avctx, AV_LOG_VERBOSE, "FrameRateExtD: %"PRIu32"; FrameRateExtN: %"PRIu32" \n",
300  info->FrameInfo.FrameRateExtD, info->FrameInfo.FrameRateExtN);
301 
302 }
303 
305 {
306  const char *rc_desc;
307  mfxU16 rc_mode;
308 
309  int want_la = q->look_ahead;
310  int want_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
311  int want_vcm = q->vcm;
312 
313  if (want_la && !QSV_HAVE_LA) {
314  av_log(avctx, AV_LOG_ERROR,
315  "Lookahead ratecontrol mode requested, but is not supported by this SDK version\n");
316  return AVERROR(ENOSYS);
317  }
318  if (want_vcm && !QSV_HAVE_VCM) {
319  av_log(avctx, AV_LOG_ERROR,
320  "VCM ratecontrol mode requested, but is not supported by this SDK version\n");
321  return AVERROR(ENOSYS);
322  }
323 
324  if (want_la + want_qscale + want_vcm > 1) {
325  av_log(avctx, AV_LOG_ERROR,
326  "More than one of: { constant qscale, lookahead, VCM } requested, "
327  "only one of them can be used at a time.\n");
328  return AVERROR(EINVAL);
329  }
330 
331  if (!want_qscale && avctx->global_quality > 0 && !QSV_HAVE_ICQ){
332  av_log(avctx, AV_LOG_ERROR,
333  "ICQ ratecontrol mode requested, but is not supported by this SDK version\n");
334  return AVERROR(ENOSYS);
335  }
336 
337  if (want_qscale) {
338  rc_mode = MFX_RATECONTROL_CQP;
339  rc_desc = "constant quantization parameter (CQP)";
340  }
341 #if QSV_HAVE_VCM
342  else if (want_vcm) {
343  rc_mode = MFX_RATECONTROL_VCM;
344  rc_desc = "video conferencing mode (VCM)";
345  }
346 #endif
347 #if QSV_HAVE_LA
348  else if (want_la) {
349  rc_mode = MFX_RATECONTROL_LA;
350  rc_desc = "VBR with lookahead (LA)";
351 
352 #if QSV_HAVE_ICQ
353  if (avctx->global_quality > 0) {
354  rc_mode = MFX_RATECONTROL_LA_ICQ;
355  rc_desc = "intelligent constant quality with lookahead (LA_ICQ)";
356  }
357 #endif
358  }
359 #endif
360 #if QSV_HAVE_ICQ
361  else if (avctx->global_quality > 0 && !avctx->rc_max_rate) {
362  rc_mode = MFX_RATECONTROL_ICQ;
363  rc_desc = "intelligent constant quality (ICQ)";
364  }
365 #endif
366  else if (avctx->rc_max_rate == avctx->bit_rate) {
367  rc_mode = MFX_RATECONTROL_CBR;
368  rc_desc = "constant bitrate (CBR)";
369  }
370 #if QSV_HAVE_AVBR
371  else if (!avctx->rc_max_rate) {
372  rc_mode = MFX_RATECONTROL_AVBR;
373  rc_desc = "average variable bitrate (AVBR)";
374  }
375 #endif
376 #if QSV_HAVE_QVBR
377  else if (avctx->global_quality > 0) {
378  rc_mode = MFX_RATECONTROL_QVBR;
379  rc_desc = "constant quality with VBR algorithm (QVBR)";
380  }
381 #endif
382  else {
383  rc_mode = MFX_RATECONTROL_VBR;
384  rc_desc = "variable bitrate (VBR)";
385  }
386 
387  q->param.mfx.RateControlMethod = rc_mode;
388  av_log(avctx, AV_LOG_VERBOSE, "Using the %s ratecontrol method\n", rc_desc);
389 
390  return 0;
391 }
392 
394 {
395  mfxVideoParam param_out = { .mfx.CodecId = q->param.mfx.CodecId };
396  mfxStatus ret;
397 
398 #define UNMATCH(x) (param_out.mfx.x != q->param.mfx.x)
399 
400  ret = MFXVideoENCODE_Query(q->session, &q->param, &param_out);
401 
402  if (ret < 0) {
403  if (UNMATCH(CodecId))
404  av_log(avctx, AV_LOG_ERROR, "Current codec type is unsupported\n");
405  if (UNMATCH(CodecProfile))
406  av_log(avctx, AV_LOG_ERROR, "Current profile is unsupported\n");
407  if (UNMATCH(RateControlMethod))
408  av_log(avctx, AV_LOG_ERROR, "Selected ratecontrol mode is unsupported\n");
409  if (UNMATCH(LowPower))
410  av_log(avctx, AV_LOG_ERROR, "Low power mode is unsupported\n");
411  if (UNMATCH(FrameInfo.FrameRateExtN) || UNMATCH(FrameInfo.FrameRateExtD))
412  av_log(avctx, AV_LOG_ERROR, "Current frame rate is unsupported\n");
413  if (UNMATCH(FrameInfo.PicStruct))
414  av_log(avctx, AV_LOG_ERROR, "Current picture structure is unsupported\n");
415  if (UNMATCH(FrameInfo.Width) || UNMATCH(FrameInfo.Height))
416  av_log(avctx, AV_LOG_ERROR, "Current resolution is unsupported\n");
417  if (UNMATCH(FrameInfo.FourCC))
418  av_log(avctx, AV_LOG_ERROR, "Current pixel format is unsupported\n");
419  return 0;
420  }
421  return 1;
422 }
423 
425 {
426  enum AVPixelFormat sw_format = avctx->pix_fmt == AV_PIX_FMT_QSV ?
427  avctx->sw_pix_fmt : avctx->pix_fmt;
428  const AVPixFmtDescriptor *desc;
429  int ret;
430 
432  if (ret < 0)
433  return AVERROR_BUG;
434  q->param.mfx.CodecId = ret;
435 
436  if (avctx->level > 0)
437  q->param.mfx.CodecLevel = avctx->level;
438  q->param.mfx.CodecProfile = q->profile;
439 
440  desc = av_pix_fmt_desc_get(sw_format);
441  if (!desc)
442  return AVERROR_BUG;
443 
444  ff_qsv_map_pixfmt(sw_format, &q->param.mfx.FrameInfo.FourCC);
445 
446  q->param.mfx.FrameInfo.CropX = 0;
447  q->param.mfx.FrameInfo.CropY = 0;
448  q->param.mfx.FrameInfo.CropW = avctx->width;
449  q->param.mfx.FrameInfo.CropH = avctx->height;
450  q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num;
451  q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den;
452  q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
453  q->param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
454  q->param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
455  q->param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
456 
457  q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, 16);
458  q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, 16);
459 
460  if (avctx->hw_frames_ctx) {
461  AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
462  AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
463  q->param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
464  q->param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
465  }
466 
467  if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
468  q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
469  q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
470  } else {
471  q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
472  q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
473  }
474 
475  q->param.mfx.Interleaved = 1;
476  q->param.mfx.Quality = av_clip(avctx->global_quality, 1, 100);
477  q->param.mfx.RestartInterval = 0;
478 
479  q->width_align = 16;
480  q->height_align = 16;
481 
482  q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align);
483  q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, q->height_align);
484 
485  return 0;
486 }
487 
489 {
490  enum AVPixelFormat sw_format = avctx->pix_fmt == AV_PIX_FMT_QSV ?
491  avctx->sw_pix_fmt : avctx->pix_fmt;
492  const AVPixFmtDescriptor *desc;
493  float quant;
494  int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
495  int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
496  int ret;
497 
499  if (ret < 0)
500  return AVERROR_BUG;
501  q->param.mfx.CodecId = ret;
502 
503  if (avctx->level > 0)
504  q->param.mfx.CodecLevel = avctx->level;
505 
507  avctx->compression_level = q->preset;
508  } else if (avctx->compression_level >= 0) {
509  if (avctx->compression_level > MFX_TARGETUSAGE_BEST_SPEED) {
510  av_log(avctx, AV_LOG_WARNING, "Invalid compression level: "
511  "valid range is 0-%d, using %d instead\n",
512  MFX_TARGETUSAGE_BEST_SPEED, MFX_TARGETUSAGE_BEST_SPEED);
513  avctx->compression_level = MFX_TARGETUSAGE_BEST_SPEED;
514  }
515  }
516 
517  if (q->low_power == 1) {
518 #if QSV_HAVE_VDENC
519  q->param.mfx.LowPower = MFX_CODINGOPTION_ON;
520 #else
521  av_log(avctx, AV_LOG_WARNING, "The low_power option is "
522  "not supported with this MSDK version.\n");
523  q->low_power = 0;
524  q->param.mfx.LowPower = MFX_CODINGOPTION_OFF;
525 #endif
526  } else if (q->low_power == -1)
527  q->param.mfx.LowPower = MFX_CODINGOPTION_UNKNOWN;
528  else
529  q->param.mfx.LowPower = MFX_CODINGOPTION_OFF;
530 
531  q->param.mfx.CodecProfile = q->profile;
532  q->param.mfx.TargetUsage = avctx->compression_level;
533  q->param.mfx.GopPicSize = FFMAX(0, avctx->gop_size);
534  q->param.mfx.GopRefDist = FFMAX(-1, avctx->max_b_frames) + 1;
535  q->param.mfx.GopOptFlag = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ?
536  MFX_GOP_CLOSED : MFX_GOP_STRICT;
537  q->param.mfx.IdrInterval = q->idr_interval;
538  q->param.mfx.NumSlice = avctx->slices;
539  q->param.mfx.NumRefFrame = FFMAX(0, avctx->refs);
540  q->param.mfx.EncodedOrder = 0;
541  q->param.mfx.BufferSizeInKB = 0;
542 
543  desc = av_pix_fmt_desc_get(sw_format);
544  if (!desc)
545  return AVERROR_BUG;
546 
547  ff_qsv_map_pixfmt(sw_format, &q->param.mfx.FrameInfo.FourCC);
548 
549  q->param.mfx.FrameInfo.CropX = 0;
550  q->param.mfx.FrameInfo.CropY = 0;
551  q->param.mfx.FrameInfo.CropW = avctx->width;
552  q->param.mfx.FrameInfo.CropH = avctx->height;
553  q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num;
554  q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den;
555  q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
556  !desc->log2_chroma_w + !desc->log2_chroma_h;
557  q->param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
558  q->param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
559  q->param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
560 
561  // If the minor version is greater than or equal to 19,
562  // then can use the same alignment settings as H.264 for HEVC
563  q->width_align = (avctx->codec_id != AV_CODEC_ID_HEVC ||
564  QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 19)) ? 16 : 32;
565  q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align);
566 
567  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
568  // it is important that PicStruct be setup correctly from the
569  // start--otherwise, encoding doesn't work and results in a bunch
570  // of incompatible video parameter errors
571  q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
572  // height alignment always must be 32 for interlaced video
573  q->height_align = 32;
574  } else {
575  q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
576  // for progressive video, the height should be aligned to 16 for
577  // H.264. For HEVC, depending on the version of MFX, it should be
578  // either 32 or 16. The lower number is better if possible.
579  q->height_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16;
580  }
581  q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, q->height_align);
582 
583  if (avctx->hw_frames_ctx) {
584  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
585  AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
586  q->param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
587  q->param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
588  }
589 
590  if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
591  q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
592  q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
593  } else {
594  q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
595  q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
596  }
597 
598  ret = select_rc_mode(avctx, q);
599  if (ret < 0)
600  return ret;
601 
602  //libmfx BRC parameters are 16 bits thus maybe overflow, then BRCParamMultiplier is needed
603  buffer_size_in_kilobytes = avctx->rc_buffer_size / 8000;
604  initial_delay_in_kilobytes = avctx->rc_initial_buffer_occupancy / 8000;
605  target_bitrate_kbps = avctx->bit_rate / 1000;
606  max_bitrate_kbps = avctx->rc_max_rate / 1000;
607  brc_param_multiplier = (FFMAX(FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
608  initial_delay_in_kilobytes) + 0x10000) / 0x10000;
609 
610  switch (q->param.mfx.RateControlMethod) {
611  case MFX_RATECONTROL_CBR:
612  case MFX_RATECONTROL_VBR:
613 #if QSV_HAVE_VCM
614  case MFX_RATECONTROL_VCM:
615 #endif
616 #if QSV_HAVE_QVBR
617  case MFX_RATECONTROL_QVBR:
618 #endif
619  q->param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
620  q->param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
621  q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
622  q->param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
623  q->param.mfx.BRCParamMultiplier = brc_param_multiplier;
624 #if QSV_HAVE_QVBR
625  if (q->param.mfx.RateControlMethod == MFX_RATECONTROL_QVBR)
626  q->extco3.QVBRQuality = av_clip(avctx->global_quality, 0, 51);
627 #endif
628  break;
629  case MFX_RATECONTROL_CQP:
630  quant = avctx->global_quality / FF_QP2LAMBDA;
631 
632  q->param.mfx.QPI = av_clip(quant * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
633  q->param.mfx.QPP = av_clip(quant, 0, 51);
634  q->param.mfx.QPB = av_clip(quant * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51);
635 
636  break;
637 #if QSV_HAVE_AVBR
638  case MFX_RATECONTROL_AVBR:
639  q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
640  q->param.mfx.Convergence = q->avbr_convergence;
641  q->param.mfx.Accuracy = q->avbr_accuracy;
642  q->param.mfx.BRCParamMultiplier = brc_param_multiplier;
643  break;
644 #endif
645 #if QSV_HAVE_LA
646  case MFX_RATECONTROL_LA:
647  q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
648  q->extco2.LookAheadDepth = q->look_ahead_depth;
649  q->param.mfx.BRCParamMultiplier = brc_param_multiplier;
650  break;
651 #if QSV_HAVE_ICQ
652  case MFX_RATECONTROL_LA_ICQ:
653  q->extco2.LookAheadDepth = q->look_ahead_depth;
654  case MFX_RATECONTROL_ICQ:
655  q->param.mfx.ICQQuality = av_clip(avctx->global_quality, 1, 51);
656  break;
657 #endif
658 #endif
659  }
660 
661  // The HEVC encoder plugin currently fails with some old libmfx version if coding options
662  // are provided. Can't find the extract libmfx version which fixed it, just enable it from
663  // V1.28 in order to keep compatibility security.
664  if (((avctx->codec_id != AV_CODEC_ID_HEVC) || QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 28))
665  && (avctx->codec_id != AV_CODEC_ID_VP9)) {
666  q->extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
667  q->extco.Header.BufferSz = sizeof(q->extco);
668 
669  q->extco.PicTimingSEI = q->pic_timing_sei ?
670  MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
671 
672  if (q->rdo >= 0)
673  q->extco.RateDistortionOpt = q->rdo > 0 ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
674 
675  if (avctx->codec_id == AV_CODEC_ID_H264) {
676  q->extco.CAVLC = q->cavlc ? MFX_CODINGOPTION_ON
677  : MFX_CODINGOPTION_UNKNOWN;
678 
680  q->extco.NalHrdConformance = avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL ?
681  MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
682 
683  if (q->single_sei_nal_unit >= 0)
684  q->extco.SingleSeiNalUnit = q->single_sei_nal_unit ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
685  if (q->recovery_point_sei >= 0)
686  q->extco.RecoveryPointSEI = q->recovery_point_sei ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
687  q->extco.MaxDecFrameBuffering = q->max_dec_frame_buffering;
688  q->extco.AUDelimiter = q->aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
689  } else if (avctx->codec_id == AV_CODEC_ID_HEVC) {
691  q->extco.NalHrdConformance = avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL ?
692  MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
693 
694  if (q->recovery_point_sei >= 0)
695  q->extco.RecoveryPointSEI = q->recovery_point_sei ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
696 
697  q->extco.AUDelimiter = q->aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
698  }
699 
700  q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco;
701 
702 #if QSV_HAVE_CO2
703  if (avctx->codec_id == AV_CODEC_ID_H264) {
704  if (q->int_ref_type >= 0)
705  q->extco2.IntRefType = q->int_ref_type;
706  if (q->int_ref_cycle_size >= 0)
707  q->extco2.IntRefCycleSize = q->int_ref_cycle_size;
708  if (q->int_ref_qp_delta != INT16_MIN)
709  q->extco2.IntRefQPDelta = q->int_ref_qp_delta;
710 
711  if (q->bitrate_limit >= 0)
712  q->extco2.BitrateLimit = q->bitrate_limit ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
713  if (q->mbbrc >= 0)
714  q->extco2.MBBRC = q->mbbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
715 
716  if (q->max_frame_size >= 0)
717  q->extco2.MaxFrameSize = q->max_frame_size;
718 #if QSV_HAVE_MAX_SLICE_SIZE
719  if (q->max_slice_size >= 0)
720  q->extco2.MaxSliceSize = q->max_slice_size;
721 #endif
722 
723 #if QSV_HAVE_TRELLIS
724  if (avctx->trellis >= 0)
725  q->extco2.Trellis = (avctx->trellis == 0) ? MFX_TRELLIS_OFF : (MFX_TRELLIS_I | MFX_TRELLIS_P | MFX_TRELLIS_B);
726  else
727  q->extco2.Trellis = MFX_TRELLIS_UNKNOWN;
728 #endif
729 
730 #if QSV_VERSION_ATLEAST(1, 8)
731  q->extco2.LookAheadDS = q->look_ahead_downsampling;
732  q->extco2.RepeatPPS = q->repeat_pps ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
733 
734  if (q->b_strategy >= 0)
735  q->extco2.BRefType = q->b_strategy ? MFX_B_REF_PYRAMID : MFX_B_REF_OFF;
736  if (q->adaptive_i >= 0)
737  q->extco2.AdaptiveI = q->adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
738  if (q->adaptive_b >= 0)
739  q->extco2.AdaptiveB = q->adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
740 #endif
741  }
742 
743  if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC) {
744  if (q->extbrc >= 0)
745  q->extco2.ExtBRC = q->extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
746 
747 #if QSV_VERSION_ATLEAST(1, 9)
748  if (avctx->qmin >= 0 && avctx->qmax >= 0 && avctx->qmin > avctx->qmax) {
749  av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are set but invalid, please make sure min <= max\n");
750  return AVERROR(EINVAL);
751  }
752  if (avctx->qmin >= 0) {
753  q->extco2.MinQPI = avctx->qmin > 51 ? 51 : avctx->qmin;
754  q->extco2.MinQPP = q->extco2.MinQPB = q->extco2.MinQPI;
755  }
756  if (avctx->qmax >= 0) {
757  q->extco2.MaxQPI = avctx->qmax > 51 ? 51 : avctx->qmax;
758  q->extco2.MaxQPP = q->extco2.MaxQPB = q->extco2.MaxQPI;
759  }
760 #endif
761  q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
762  q->extco2.Header.BufferSz = sizeof(q->extco2);
763 
764  q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco2;
765  }
766 #endif
767 
768  if (avctx->codec_id == AV_CODEC_ID_H264) {
769 #if QSV_HAVE_MF
770  if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 25)) {
771  q->extmfp.Header.BufferId = MFX_EXTBUFF_MULTI_FRAME_PARAM;
772  q->extmfp.Header.BufferSz = sizeof(q->extmfp);
773 
774  q->extmfp.MFMode = q->mfmode;
775  av_log(avctx,AV_LOG_VERBOSE,"MFMode:%d\n", q->extmfp.MFMode);
776  q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extmfp;
777  }
778 #endif
779  }
780 #if QSV_HAVE_CO3
781  q->extco3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
782  q->extco3.Header.BufferSz = sizeof(q->extco3);
783 #if QSV_HAVE_GPB
784  if (avctx->codec_id == AV_CODEC_ID_HEVC)
785  q->extco3.GPB = q->gpb ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
786 #endif
787  q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco3;
788 #endif
789  }
790 
791 #if QSV_HAVE_EXT_VP9_PARAM
792  if (avctx->codec_id == AV_CODEC_ID_VP9) {
793  q->extvp9param.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
794  q->extvp9param.Header.BufferSz = sizeof(q->extvp9param);
795  q->extvp9param.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
796  q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extvp9param;
797  }
798 #endif
799 
800 #if QSV_HAVE_EXT_HEVC_TILES
801  if (avctx->codec_id == AV_CODEC_ID_HEVC) {
802  q->exthevctiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES;
803  q->exthevctiles.Header.BufferSz = sizeof(q->exthevctiles);
804  q->exthevctiles.NumTileColumns = q->tile_cols;
805  q->exthevctiles.NumTileRows = q->tile_rows;
806  q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->exthevctiles;
807  }
808 #endif
809 
810  q->extvsi.VideoFullRange = (avctx->color_range == AVCOL_RANGE_JPEG);
811  q->extvsi.ColourDescriptionPresent = 0;
812 
813  if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
814  avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
815  avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
816  q->extvsi.ColourDescriptionPresent = 1;
817  q->extvsi.ColourPrimaries = avctx->color_primaries;
818  q->extvsi.TransferCharacteristics = avctx->color_trc;
819  q->extvsi.MatrixCoefficients = avctx->colorspace;
820  }
821 
822  if (q->extvsi.VideoFullRange || q->extvsi.ColourDescriptionPresent) {
823  q->extvsi.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO;
824  q->extvsi.Header.BufferSz = sizeof(q->extvsi);
825  q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extvsi;
826  }
827 
828  if (!check_enc_param(avctx,q)) {
829  av_log(avctx, AV_LOG_ERROR,
830  "some encoding parameters are not supported by the QSV "
831  "runtime. Please double check the input parameters.\n");
832  return AVERROR(ENOSYS);
833  }
834 
835  return 0;
836 }
837 
839 {
840  int ret = 0;
841 
842  ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
843  if (ret < 0)
844  return ff_qsv_print_error(avctx, ret,
845  "Error calling GetVideoParam");
846 
847  q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000;
848 
849  // for qsv mjpeg the return value maybe 0 so alloc the buffer
850  if (q->packet_size == 0)
851  q->packet_size = q->param.mfx.FrameInfo.Height * q->param.mfx.FrameInfo.Width * 4;
852 
853  return 0;
854 }
855 
857 {
858  int ret = 0;
859 #if QSV_HAVE_EXT_VP9_PARAM
860  mfxExtVP9Param vp9_extend_buf = {
861  .Header.BufferId = MFX_EXTBUFF_VP9_PARAM,
862  .Header.BufferSz = sizeof(vp9_extend_buf),
863  };
864 #endif
865 
866 #if QSV_HAVE_CO2
867  mfxExtCodingOption2 co2 = {
868  .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
869  .Header.BufferSz = sizeof(co2),
870  };
871 #endif
872 
873 #if QSV_HAVE_CO3
874  mfxExtCodingOption3 co3 = {
875  .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
876  .Header.BufferSz = sizeof(co3),
877  };
878 #endif
879 
880  mfxExtBuffer *ext_buffers[] = {
881 #if QSV_HAVE_EXT_VP9_PARAM
882  (mfxExtBuffer*)&vp9_extend_buf,
883 #endif
884 #if QSV_HAVE_CO2
885  (mfxExtBuffer*)&co2,
886 #endif
887 #if QSV_HAVE_CO3
888  (mfxExtBuffer*)&co3,
889 #endif
890  };
891 
892  q->param.ExtParam = ext_buffers;
893  q->param.NumExtParam = FF_ARRAY_ELEMS(ext_buffers);
894 
895  ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
896  if (ret < 0)
897  return ff_qsv_print_error(avctx, ret,
898  "Error calling GetVideoParam");
899 
900  q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000;
901 
902  return 0;
903 }
904 
906 {
907  AVCPBProperties *cpb_props;
908 
909  uint8_t sps_buf[128];
910  uint8_t pps_buf[128];
911 
912  mfxExtCodingOptionSPSPPS extradata = {
913  .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
914  .Header.BufferSz = sizeof(extradata),
915  .SPSBuffer = sps_buf, .SPSBufSize = sizeof(sps_buf),
916  .PPSBuffer = pps_buf, .PPSBufSize = sizeof(pps_buf)
917  };
918 
919  mfxExtCodingOption co = {
920  .Header.BufferId = MFX_EXTBUFF_CODING_OPTION,
921  .Header.BufferSz = sizeof(co),
922  };
923 #if QSV_HAVE_CO2
924  mfxExtCodingOption2 co2 = {
925  .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
926  .Header.BufferSz = sizeof(co2),
927  };
928 #endif
929 #if QSV_HAVE_CO3
930  mfxExtCodingOption3 co3 = {
931  .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
932  .Header.BufferSz = sizeof(co3),
933  };
934 #endif
935 
936 #if QSV_HAVE_CO_VPS
937  uint8_t vps_buf[128];
938  mfxExtCodingOptionVPS extradata_vps = {
939  .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_VPS,
940  .Header.BufferSz = sizeof(extradata_vps),
941  .VPSBuffer = vps_buf,
942  .VPSBufSize = sizeof(vps_buf),
943  };
944 #endif
945 
946 #if QSV_HAVE_EXT_HEVC_TILES
947  mfxExtHEVCTiles hevc_tile_buf = {
948  .Header.BufferId = MFX_EXTBUFF_HEVC_TILES,
949  .Header.BufferSz = sizeof(hevc_tile_buf),
950  };
951 #endif
952 
953  mfxExtBuffer *ext_buffers[2 + QSV_HAVE_CO2 + QSV_HAVE_CO3 + QSV_HAVE_CO_VPS + QSV_HAVE_EXT_HEVC_TILES];
954 
955  int need_pps = avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO;
956  int ret, ext_buf_num = 0, extradata_offset = 0;
957 
958  ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata;
959  ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co;
960 #if QSV_HAVE_CO2
961  ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
962 #endif
963 #if QSV_HAVE_CO3
964  ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
965 #endif
966 #if QSV_HAVE_CO_VPS
967  q->hevc_vps = ((avctx->codec_id == AV_CODEC_ID_HEVC) && QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 17));
968  if (q->hevc_vps)
969  ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata_vps;
970 #endif
971 #if QSV_HAVE_EXT_HEVC_TILES
972  if (avctx->codec_id == AV_CODEC_ID_HEVC)
973  ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hevc_tile_buf;
974 #endif
975 
976  q->param.ExtParam = ext_buffers;
977  q->param.NumExtParam = ext_buf_num;
978 
979  ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
980  if (ret < 0)
981  return ff_qsv_print_error(avctx, ret,
982  "Error calling GetVideoParam");
983 
984  q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000;
985 
986  if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)
987 #if QSV_HAVE_CO_VPS
988  || (q->hevc_vps && !extradata_vps.VPSBufSize)
989 #endif
990  ) {
991  av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n");
992  return AVERROR_UNKNOWN;
993  }
994 
995  avctx->extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
996 #if QSV_HAVE_CO_VPS
997  avctx->extradata_size += q->hevc_vps * extradata_vps.VPSBufSize;
998 #endif
999 
1001  if (!avctx->extradata)
1002  return AVERROR(ENOMEM);
1003 
1004 #if QSV_HAVE_CO_VPS
1005  if (q->hevc_vps) {
1006  memcpy(avctx->extradata, vps_buf, extradata_vps.VPSBufSize);
1007  extradata_offset += extradata_vps.VPSBufSize;
1008  }
1009 #endif
1010 
1011  memcpy(avctx->extradata + extradata_offset, sps_buf, extradata.SPSBufSize);
1012  extradata_offset += extradata.SPSBufSize;
1013  if (need_pps) {
1014  memcpy(avctx->extradata + extradata_offset, pps_buf, extradata.PPSBufSize);
1015  extradata_offset += extradata.PPSBufSize;
1016  }
1017  memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1018 
1019  cpb_props = ff_add_cpb_side_data(avctx);
1020  if (!cpb_props)
1021  return AVERROR(ENOMEM);
1022  cpb_props->max_bitrate = avctx->rc_max_rate;
1023  cpb_props->min_bitrate = avctx->rc_min_rate;
1024  cpb_props->avg_bitrate = avctx->bit_rate;
1025  cpb_props->buffer_size = avctx->rc_buffer_size;
1026 
1027  dump_video_param(avctx, q, ext_buffers + 1);
1028 
1029  return 0;
1030 }
1031 
1033 {
1034  AVQSVContext *qsv = avctx->hwaccel_context;
1035  mfxFrameSurface1 *surfaces;
1036  int nb_surfaces, i;
1037 
1038  nb_surfaces = qsv->nb_opaque_surfaces + q->req.NumFrameSuggested;
1039 
1040  q->opaque_alloc_buf = av_buffer_allocz(sizeof(*surfaces) * nb_surfaces);
1041  if (!q->opaque_alloc_buf)
1042  return AVERROR(ENOMEM);
1043 
1044  q->opaque_surfaces = av_malloc_array(nb_surfaces, sizeof(*q->opaque_surfaces));
1045  if (!q->opaque_surfaces)
1046  return AVERROR(ENOMEM);
1047 
1048  surfaces = (mfxFrameSurface1*)q->opaque_alloc_buf->data;
1049  for (i = 0; i < nb_surfaces; i++) {
1050  surfaces[i].Info = q->req.Info;
1051  q->opaque_surfaces[i] = surfaces + i;
1052  }
1053 
1054  q->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
1055  q->opaque_alloc.Header.BufferSz = sizeof(q->opaque_alloc);
1056  q->opaque_alloc.In.Surfaces = q->opaque_surfaces;
1057  q->opaque_alloc.In.NumSurface = nb_surfaces;
1058  q->opaque_alloc.In.Type = q->req.Type;
1059 
1060  q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->opaque_alloc;
1061 
1062  qsv->nb_opaque_surfaces = nb_surfaces;
1064  qsv->opaque_alloc_type = q->req.Type;
1065 
1066  return 0;
1067 }
1068 
1070 {
1071  int ret;
1072 
1073  if (avctx->hwaccel_context) {
1074  AVQSVContext *qsv = avctx->hwaccel_context;
1075  q->session = qsv->session;
1076  } else if (avctx->hw_frames_ctx) {
1078  if (!q->frames_ctx.hw_frames_ctx)
1079  return AVERROR(ENOMEM);
1080 
1082  &q->frames_ctx, q->load_plugins,
1083  q->param.IOPattern == MFX_IOPATTERN_IN_OPAQUE_MEMORY,
1084  MFX_GPUCOPY_OFF);
1085  if (ret < 0) {
1087  return ret;
1088  }
1089 
1090  q->session = q->internal_qs.session;
1091  } else if (avctx->hw_device_ctx) {
1093  avctx->hw_device_ctx, q->load_plugins,
1094  MFX_GPUCOPY_OFF);
1095  if (ret < 0)
1096  return ret;
1097 
1098  q->session = q->internal_qs.session;
1099  } else {
1101  q->load_plugins, MFX_GPUCOPY_OFF);
1102  if (ret < 0)
1103  return ret;
1104 
1105  q->session = q->internal_qs.session;
1106  }
1107 
1108  return 0;
1109 }
1110 
1111 static inline unsigned int qsv_fifo_item_size(void)
1112 {
1113  return sizeof(AVPacket) + sizeof(mfxSyncPoint*) + sizeof(mfxBitstream*);
1114 }
1115 
1116 static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
1117 {
1118  return av_fifo_size(fifo)/qsv_fifo_item_size();
1119 }
1120 
1122 {
1123  int iopattern = 0;
1124  int opaque_alloc = 0;
1125  int ret;
1126 
1127  q->param.AsyncDepth = q->async_depth;
1128 
1130  if (!q->async_fifo)
1131  return AVERROR(ENOMEM);
1132 
1133  if (avctx->hwaccel_context) {
1134  AVQSVContext *qsv = avctx->hwaccel_context;
1135 
1136  iopattern = qsv->iopattern;
1137  opaque_alloc = qsv->opaque_alloc;
1138  }
1139 
1140  if (avctx->hw_frames_ctx) {
1141  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1142  AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
1143 
1144  if (!iopattern) {
1145  if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
1146  iopattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY;
1147  else if (frames_hwctx->frame_type &
1148  (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1149  iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1150  }
1151  }
1152 
1153  if (!iopattern)
1154  iopattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
1155  q->param.IOPattern = iopattern;
1156  ff_qsv_print_iopattern(avctx, iopattern, "Encoder");
1157 
1158  ret = qsvenc_init_session(avctx, q);
1159  if (ret < 0)
1160  return ret;
1161 
1162  ret = MFXQueryVersion(q->session,&q->ver);
1163  if (ret < 0) {
1164  return ff_qsv_print_error(avctx, ret,
1165  "Error querying mfx version");
1166  }
1167 
1168  // in the mfxInfoMFX struct, JPEG is different from other codecs
1169  switch (avctx->codec_id) {
1170  case AV_CODEC_ID_MJPEG:
1171  ret = init_video_param_jpeg(avctx, q);
1172  break;
1173  default:
1174  ret = init_video_param(avctx, q);
1175  break;
1176  }
1177  if (ret < 0)
1178  return ret;
1179 
1180  if (avctx->hwaccel_context) {
1181  AVQSVContext *qsv = avctx->hwaccel_context;
1182  int i, j;
1183 
1185  sizeof(*q->extparam));
1186  if (!q->extparam)
1187  return AVERROR(ENOMEM);
1188 
1189  q->param.ExtParam = q->extparam;
1190  for (i = 0; i < qsv->nb_ext_buffers; i++)
1191  q->param.ExtParam[i] = qsv->ext_buffers[i];
1192  q->param.NumExtParam = qsv->nb_ext_buffers;
1193 
1194  for (i = 0; i < q->nb_extparam_internal; i++) {
1195  for (j = 0; j < qsv->nb_ext_buffers; j++) {
1196  if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId)
1197  break;
1198  }
1199  if (j < qsv->nb_ext_buffers)
1200  continue;
1201 
1202  q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i];
1203  }
1204  } else {
1205  q->param.ExtParam = q->extparam_internal;
1206  q->param.NumExtParam = q->nb_extparam_internal;
1207  }
1208 
1209  ret = MFXVideoENCODE_Query(q->session, &q->param, &q->param);
1210  if (ret == MFX_WRN_PARTIAL_ACCELERATION) {
1211  av_log(avctx, AV_LOG_WARNING, "Encoder will work with partial HW acceleration\n");
1212  } else if (ret < 0) {
1213  return ff_qsv_print_error(avctx, ret,
1214  "Error querying encoder params");
1215  }
1216 
1217  ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req);
1218  if (ret < 0)
1219  return ff_qsv_print_error(avctx, ret,
1220  "Error querying (IOSurf) the encoding parameters");
1221 
1222  if (opaque_alloc) {
1223  ret = qsv_init_opaque_alloc(avctx, q);
1224  if (ret < 0)
1225  return ret;
1226  }
1227 
1228  ret = MFXVideoENCODE_Init(q->session, &q->param);
1229  if (ret < 0)
1230  return ff_qsv_print_error(avctx, ret,
1231  "Error initializing the encoder");
1232  else if (ret > 0)
1233  ff_qsv_print_warning(avctx, ret,
1234  "Warning in encoder initialization");
1235 
1236  switch (avctx->codec_id) {
1237  case AV_CODEC_ID_MJPEG:
1238  ret = qsv_retrieve_enc_jpeg_params(avctx, q);
1239  break;
1240  case AV_CODEC_ID_VP9:
1241  ret = qsv_retrieve_enc_vp9_params(avctx, q);
1242  break;
1243  default:
1244  ret = qsv_retrieve_enc_params(avctx, q);
1245  break;
1246  }
1247  if (ret < 0) {
1248  av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n");
1249  return ret;
1250  }
1251 
1252  q->avctx = avctx;
1253 
1254  return 0;
1255 }
1256 
1257 static void free_encoder_ctrl_payloads(mfxEncodeCtrl* enc_ctrl)
1258 {
1259  if (enc_ctrl) {
1260  int i;
1261  for (i = 0; i < enc_ctrl->NumPayload && i < QSV_MAX_ENC_PAYLOAD; i++) {
1262  av_free(enc_ctrl->Payload[i]);
1263  }
1264  enc_ctrl->NumPayload = 0;
1265  }
1266 }
1267 
1269 {
1270  QSVFrame *cur = q->work_frames;
1271  while (cur) {
1272  if (cur->used && !cur->surface.Data.Locked) {
1274  //do not reuse enc_ctrl from previous frame
1275  memset(&cur->enc_ctrl, 0, sizeof(cur->enc_ctrl));
1276  if (cur->frame->format == AV_PIX_FMT_QSV) {
1277  av_frame_unref(cur->frame);
1278  }
1279  cur->used = 0;
1280  }
1281  cur = cur->next;
1282  }
1283 }
1284 
1286 {
1287  QSVFrame *frame, **last;
1288 
1290 
1291  frame = q->work_frames;
1292  last = &q->work_frames;
1293  while (frame) {
1294  if (!frame->used) {
1295  *f = frame;
1296  frame->used = 1;
1297  return 0;
1298  }
1299 
1300  last = &frame->next;
1301  frame = frame->next;
1302  }
1303 
1304  frame = av_mallocz(sizeof(*frame));
1305  if (!frame)
1306  return AVERROR(ENOMEM);
1307  frame->frame = av_frame_alloc();
1308  if (!frame->frame) {
1309  av_freep(&frame);
1310  return AVERROR(ENOMEM);
1311  }
1312  frame->enc_ctrl.Payload = av_mallocz(sizeof(mfxPayload*) * QSV_MAX_ENC_PAYLOAD);
1313  if (!frame->enc_ctrl.Payload) {
1314  av_freep(&frame);
1315  return AVERROR(ENOMEM);
1316  }
1317  *last = frame;
1318 
1319  *f = frame;
1320  frame->used = 1;
1321 
1322  return 0;
1323 }
1324 
1325 static int submit_frame(QSVEncContext *q, const AVFrame *frame,
1326  QSVFrame **new_frame)
1327 {
1328  QSVFrame *qf;
1329  int ret;
1330 
1331  ret = get_free_frame(q, &qf);
1332  if (ret < 0)
1333  return ret;
1334 
1335  if (frame->format == AV_PIX_FMT_QSV) {
1336  ret = av_frame_ref(qf->frame, frame);
1337  if (ret < 0)
1338  return ret;
1339 
1340  qf->surface = *(mfxFrameSurface1*)qf->frame->data[3];
1341 
1342  if (q->frames_ctx.mids) {
1344  if (ret < 0)
1345  return ret;
1346 
1347  qf->surface.Data.MemId = &q->frames_ctx.mids[ret];
1348  }
1349  } else {
1350  /* make a copy if the input is not padded as libmfx requires */
1351  /* and to make allocation continious for data[0]/data[1] */
1352  if ((frame->height & 31 || frame->linesize[0] & (q->width_align - 1)) ||
1353  (frame->data[1] - frame->data[0] != frame->linesize[0] * FFALIGN(qf->frame->height, q->height_align))) {
1354  qf->frame->height = FFALIGN(frame->height, q->height_align);
1355  qf->frame->width = FFALIGN(frame->width, q->width_align);
1356 
1357  qf->frame->format = frame->format;
1358 
1359  if (!qf->frame->data[0]) {
1361  if (ret < 0)
1362  return ret;
1363  }
1364 
1365  qf->frame->height = frame->height;
1366  qf->frame->width = frame->width;
1367 
1368  ret = av_frame_copy(qf->frame, frame);
1369  if (ret < 0) {
1370  av_frame_unref(qf->frame);
1371  return ret;
1372  }
1373  } else {
1374  ret = av_frame_ref(qf->frame, frame);
1375  if (ret < 0)
1376  return ret;
1377  }
1378 
1379  qf->surface.Info = q->param.mfx.FrameInfo;
1380 
1381  qf->surface.Info.PicStruct =
1382  !frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
1383  frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
1384  MFX_PICSTRUCT_FIELD_BFF;
1385  if (frame->repeat_pict == 1)
1386  qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
1387  else if (frame->repeat_pict == 2)
1388  qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
1389  else if (frame->repeat_pict == 4)
1390  qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
1391 
1392  qf->surface.Data.PitchLow = qf->frame->linesize[0];
1393  qf->surface.Data.Y = qf->frame->data[0];
1394  qf->surface.Data.UV = qf->frame->data[1];
1395  }
1396 
1397  qf->surface.Data.TimeStamp = av_rescale_q(frame->pts, q->avctx->time_base, (AVRational){1, 90000});
1398 
1399  *new_frame = qf;
1400 
1401  return 0;
1402 }
1403 
1405 {
1406  if (q->param.mfx.CodecId == MFX_CODEC_AVC) {
1407  if (q->param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
1408  q->param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
1409  q->param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
1410  av_log(avctx, AV_LOG_WARNING,
1411  "Interlaced coding is supported"
1412  " at Main/High Profile Level 2.2-4.0\n");
1413  }
1414 }
1415 
1417  const AVFrame *frame)
1418 {
1419  AVPacket new_pkt = { 0 };
1420  mfxBitstream *bs;
1421 #if QSV_VERSION_ATLEAST(1, 26)
1422  mfxExtAVCEncodedFrameInfo *enc_info;
1423  mfxExtBuffer **enc_buf;
1424 #endif
1425 
1426  mfxFrameSurface1 *surf = NULL;
1427  mfxSyncPoint *sync = NULL;
1428  QSVFrame *qsv_frame = NULL;
1429  mfxEncodeCtrl* enc_ctrl = NULL;
1430  int ret;
1431 
1432  if (frame) {
1433  ret = submit_frame(q, frame, &qsv_frame);
1434  if (ret < 0) {
1435  av_log(avctx, AV_LOG_ERROR, "Error submitting the frame for encoding.\n");
1436  return ret;
1437  }
1438  }
1439  if (qsv_frame) {
1440  surf = &qsv_frame->surface;
1441  enc_ctrl = &qsv_frame->enc_ctrl;
1442 
1443  if (frame->pict_type == AV_PICTURE_TYPE_I) {
1444  enc_ctrl->FrameType = MFX_FRAMETYPE_I | MFX_FRAMETYPE_REF;
1445  if (q->forced_idr)
1446  enc_ctrl->FrameType |= MFX_FRAMETYPE_IDR;
1447  }
1448  }
1449 
1450  ret = av_new_packet(&new_pkt, q->packet_size);
1451  if (ret < 0) {
1452  av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
1453  return ret;
1454  }
1455 
1456  bs = av_mallocz(sizeof(*bs));
1457  if (!bs) {
1458  av_packet_unref(&new_pkt);
1459  return AVERROR(ENOMEM);
1460  }
1461  bs->Data = new_pkt.data;
1462  bs->MaxLength = new_pkt.size;
1463 
1464 #if QSV_VERSION_ATLEAST(1, 26)
1465  if (avctx->codec_id == AV_CODEC_ID_H264) {
1466  enc_info = av_mallocz(sizeof(*enc_info));
1467  if (!enc_info)
1468  return AVERROR(ENOMEM);
1469 
1470  enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO;
1471  enc_info->Header.BufferSz = sizeof (*enc_info);
1472  bs->NumExtParam = 1;
1473  enc_buf = av_mallocz(sizeof(mfxExtBuffer *));
1474  if (!enc_buf)
1475  return AVERROR(ENOMEM);
1476  enc_buf[0] = (mfxExtBuffer *)enc_info;
1477 
1478  bs->ExtParam = enc_buf;
1479  }
1480 #endif
1481 
1482  if (q->set_encode_ctrl_cb) {
1483  q->set_encode_ctrl_cb(avctx, frame, &qsv_frame->enc_ctrl);
1484  }
1485 
1486  sync = av_mallocz(sizeof(*sync));
1487  if (!sync) {
1488  av_freep(&bs);
1489  #if QSV_VERSION_ATLEAST(1, 26)
1490  if (avctx->codec_id == AV_CODEC_ID_H264) {
1491  av_freep(&enc_info);
1492  av_freep(&enc_buf);
1493  }
1494  #endif
1495  av_packet_unref(&new_pkt);
1496  return AVERROR(ENOMEM);
1497  }
1498 
1499  do {
1500  ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, bs, sync);
1501  if (ret == MFX_WRN_DEVICE_BUSY)
1502  av_usleep(500);
1503  } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_WRN_IN_EXECUTION);
1504 
1505  if (ret > 0)
1506  ff_qsv_print_warning(avctx, ret, "Warning during encoding");
1507 
1508  if (ret < 0) {
1509  av_packet_unref(&new_pkt);
1510  av_freep(&bs);
1511 #if QSV_VERSION_ATLEAST(1, 26)
1512  if (avctx->codec_id == AV_CODEC_ID_H264) {
1513  av_freep(&enc_info);
1514  av_freep(&enc_buf);
1515  }
1516 #endif
1517  av_freep(&sync);
1518  return (ret == MFX_ERR_MORE_DATA) ?
1519  0 : ff_qsv_print_error(avctx, ret, "Error during encoding");
1520  }
1521 
1522  if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM && frame->interlaced_frame)
1523  print_interlace_msg(avctx, q);
1524 
1525  if (*sync) {
1526  av_fifo_generic_write(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
1527  av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
1528  av_fifo_generic_write(q->async_fifo, &bs, sizeof(bs), NULL);
1529  } else {
1530  av_freep(&sync);
1531  av_packet_unref(&new_pkt);
1532  av_freep(&bs);
1533 #if QSV_VERSION_ATLEAST(1, 26)
1534  if (avctx->codec_id == AV_CODEC_ID_H264) {
1535  av_freep(&enc_info);
1536  av_freep(&enc_buf);
1537  }
1538 #endif
1539  }
1540 
1541  return 0;
1542 }
1543 
1545  AVPacket *pkt, const AVFrame *frame, int *got_packet)
1546 {
1547  int ret;
1548 
1549  ret = encode_frame(avctx, q, frame);
1550  if (ret < 0)
1551  return ret;
1552 
1553  if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) ||
1554  (!frame && av_fifo_size(q->async_fifo))) {
1555  AVPacket new_pkt;
1556  mfxBitstream *bs;
1557  mfxSyncPoint *sync;
1558 #if QSV_VERSION_ATLEAST(1, 26)
1559  mfxExtAVCEncodedFrameInfo *enc_info;
1560  mfxExtBuffer **enc_buf;
1561 #endif
1562  enum AVPictureType pict_type;
1563 
1564  av_fifo_generic_read(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
1565  av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
1566  av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
1567 
1568  do {
1569  ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
1570  } while (ret == MFX_WRN_IN_EXECUTION);
1571 
1572  new_pkt.dts = av_rescale_q(bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
1573  new_pkt.pts = av_rescale_q(bs->TimeStamp, (AVRational){1, 90000}, avctx->time_base);
1574  new_pkt.size = bs->DataLength;
1575 
1576  if (bs->FrameType & MFX_FRAMETYPE_IDR || bs->FrameType & MFX_FRAMETYPE_xIDR) {
1577  new_pkt.flags |= AV_PKT_FLAG_KEY;
1578  pict_type = AV_PICTURE_TYPE_I;
1579  } else if (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI)
1580  pict_type = AV_PICTURE_TYPE_I;
1581  else if (bs->FrameType & MFX_FRAMETYPE_P || bs->FrameType & MFX_FRAMETYPE_xP)
1582  pict_type = AV_PICTURE_TYPE_P;
1583  else if (bs->FrameType & MFX_FRAMETYPE_B || bs->FrameType & MFX_FRAMETYPE_xB)
1584  pict_type = AV_PICTURE_TYPE_B;
1585  else if (bs->FrameType == MFX_FRAMETYPE_UNKNOWN) {
1586  pict_type = AV_PICTURE_TYPE_NONE;
1587  av_log(avctx, AV_LOG_WARNING, "Unknown FrameType, set pict_type to AV_PICTURE_TYPE_NONE.\n");
1588  } else {
1589  av_log(avctx, AV_LOG_ERROR, "Invalid FrameType:%d.\n", bs->FrameType);
1590  return AVERROR_INVALIDDATA;
1591  }
1592 
1593 #if QSV_VERSION_ATLEAST(1, 26)
1594  if (avctx->codec_id == AV_CODEC_ID_H264) {
1595  enc_buf = bs->ExtParam;
1596  enc_info = (mfxExtAVCEncodedFrameInfo *)(*bs->ExtParam);
1598  enc_info->QP * FF_QP2LAMBDA, NULL, 0, pict_type);
1599  av_freep(&enc_info);
1600  av_freep(&enc_buf);
1601  }
1602 #endif
1603  av_freep(&bs);
1604  av_freep(&sync);
1605 
1606  if (pkt->data) {
1607  if (pkt->size < new_pkt.size) {
1608  av_log(avctx, AV_LOG_ERROR, "Submitted buffer not large enough: %d < %d\n",
1609  pkt->size, new_pkt.size);
1610  av_packet_unref(&new_pkt);
1611  return AVERROR(EINVAL);
1612  }
1613 
1614  memcpy(pkt->data, new_pkt.data, new_pkt.size);
1615  pkt->size = new_pkt.size;
1616 
1617  ret = av_packet_copy_props(pkt, &new_pkt);
1618  av_packet_unref(&new_pkt);
1619  if (ret < 0)
1620  return ret;
1621  } else
1622  *pkt = new_pkt;
1623 
1624  *got_packet = 1;
1625  }
1626 
1627  return 0;
1628 }
1629 
1631 {
1632  QSVFrame *cur;
1633 
1634  if (q->session)
1635  MFXVideoENCODE_Close(q->session);
1636 
1637  q->session = NULL;
1639 
1642 
1643  cur = q->work_frames;
1644  while (cur) {
1645  q->work_frames = cur->next;
1646  av_frame_free(&cur->frame);
1647  av_free(cur->enc_ctrl.Payload);
1648  av_freep(&cur);
1649  cur = q->work_frames;
1650  }
1651 
1652  while (q->async_fifo && av_fifo_size(q->async_fifo)) {
1653  AVPacket pkt;
1654  mfxSyncPoint *sync;
1655  mfxBitstream *bs;
1656 
1657  av_fifo_generic_read(q->async_fifo, &pkt, sizeof(pkt), NULL);
1658  av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
1659  av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
1660 
1661  av_freep(&sync);
1662  av_freep(&bs);
1663  av_packet_unref(&pkt);
1664  }
1666  q->async_fifo = NULL;
1667 
1670 
1671  av_freep(&q->extparam);
1672 
1673  return 0;
1674 }
1675 
1677  HW_CONFIG_ENCODER_FRAMES(QSV, QSV),
1678  HW_CONFIG_ENCODER_DEVICE(NV12, QSV),
1679  HW_CONFIG_ENCODER_DEVICE(P010, QSV),
1680  NULL,
1681 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:417
AVCodecContext::hwaccel_context
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:1366
QSVEncContext::look_ahead_depth
int look_ahead_depth
Definition: qsvenc.h:166
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
QSVEncContext::async_fifo
AVFifoBuffer * async_fifo
Definition: qsvenc.h:149
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
av_clip
#define av_clip
Definition: common.h:96
QSVEncContext::repeat_pps
int repeat_pps
Definition: qsvenc.h:194
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
name
const char * name
Definition: qsvenc.c:46
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:956
qsv_retrieve_enc_vp9_params
static int qsv_retrieve_enc_vp9_params(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:856
av_fifo_generic_write
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
QSVFramesContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
Definition: qsv_internal.h:94
av_frame_get_buffer
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:246
AVCodecContext::rc_min_rate
int64_t rc_min_rate
minimum bitrate
Definition: avcodec.h:1197
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2540
ff_side_data_set_encoder_stats
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:601
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
av_fifo_free
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:55
AVPictureType
AVPictureType
Definition: avutil.h:272
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:210
QSVEncContext::avbr_accuracy
int avbr_accuracy
Definition: qsvenc.h:162
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
profile
mfxU16 profile
Definition: qsvenc.c:45
QSVEncContext::extco
mfxExtCodingOption extco
Definition: qsvenc.h:120
ff_qsv_close_internal_session
int ff_qsv_close_internal_session(QSVSession *qs)
Definition: qsv.c:820
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:949
AVFrame::width
int width
Definition: frame.h:361
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:576
internal.h
QSVEncContext::adaptive_b
int adaptive_b
Definition: qsvenc.h:185
AVPacket::data
uint8_t * data
Definition: packet.h:373
MFX_LOOKAHEAD_DS_2x
#define MFX_LOOKAHEAD_DS_2x
Definition: qsvenc.h:73
QSVEncContext::max_frame_size
int max_frame_size
Definition: qsvenc.h:170
QSVEncContext::tile_cols
int tile_cols
Definition: qsvenc.h:173
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:476
QSVEncContext::packet_size
int packet_size
Definition: qsvenc.h:113
ff_qsv_find_surface_idx
int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
Definition: qsv.c:231
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:667
QSV_HAVE_ICQ
#define QSV_HAVE_ICQ
Definition: qsvenc.h:64
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:214
QSVEncContext::adaptive_i
int adaptive_i
Definition: qsvenc.h:184
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
QSVEncContext::int_ref_qp_delta
int int_ref_qp_delta
Definition: qsvenc.h:191
print_threestate
static const char * print_threestate(mfxU16 val)
Definition: qsvenc.c:123
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
QSVEncContext::frames_ctx
QSVFramesContext frames_ctx
Definition: qsvenc.h:151
av_fifo_generic_read
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:213
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
ff_add_cpb_side_data
AVCPBProperties * ff_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition: utils.c:1003
AVCodecContext::qmax
int qmax
maximum quantizer
Definition: avcodec.h:1161
AVQSVContext::opaque_alloc_type
int opaque_alloc_type
Encoding only, and only if opaque_alloc is set to non-zero.
Definition: qsv.h:97
FF_COMPRESSION_DEFAULT
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:452
QSVEncContext::load_plugins
char * load_plugins
Definition: qsvenc.h:203
QSVFrame::frame
AVFrame * frame
Definition: qsv_internal.h:73
AVQSVContext::iopattern
int iopattern
The IO pattern to use.
Definition: qsv.h:46
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:425
QSVFrame::used
int used
Definition: qsv_internal.h:80
select_rc_mode
static int select_rc_mode(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:304
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
QSVFrame::enc_ctrl
mfxEncodeCtrl enc_ctrl
Definition: qsv_internal.h:75
ff_qsv_init_session_device
int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession, AVBufferRef *device_ref, const char *load_plugins, int gpu_copy)
Definition: qsv.c:691
QSVEncContext::extvsi
mfxExtVideoSignalInfo extvsi
Definition: qsvenc.h:142
AVFifoBuffer
Definition: fifo.h:31
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1706
profile_names
static const struct @112 profile_names[]
QSVEncContext::recovery_point_sei
int recovery_point_sei
Definition: qsvenc.h:192
QSVEncContext::hevc_vps
int hevc_vps
Definition: qsvenc.h:155
AVCodecContext::i_quant_factor
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
Definition: avcodec.h:684
QSVEncContext::param
mfxVideoParam param
Definition: qsvenc.h:117
QSVEncContext::height_align
int height_align
Definition: qsvenc.h:115
QSVEncContext::profile
int profile
Definition: qsvenc.h:160
AVCodecContext::refs
int refs
number of reference frames
Definition: avcodec.h:928
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:459
val
static double val(void *priv, double ch)
Definition: aeval.c:76
qsv_retrieve_enc_params
static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:905
AVRational::num
int num
Numerator.
Definition: rational.h:59
quant
static int quant(float coef, const float Q, const float rounding)
Quantize one coefficient.
Definition: aacenc_utils.h:59
QSV_HAVE_EXT_HEVC_TILES
#define QSV_HAVE_EXT_HEVC_TILES
Definition: qsvenc.h:42
qsv_internal.h
AV_CODEC_FLAG_INTERLACED_DCT
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:256
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
QSVEncContext::extbrc
int extbrc
Definition: qsvenc.h:183
ff_qsv_enc_hw_configs
const AVCodecHWConfigInternal *const ff_qsv_enc_hw_configs[]
Definition: qsvenc.c:1676
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:942
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
ff_qsv_print_warning
int ff_qsv_print_warning(void *log_ctx, mfxStatus err, const char *warning_string)
Definition: qsv.c:177
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
AVCodecContext::rc_initial_buffer_occupancy
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:1218
QSVEncContext
Definition: qsvenc.h:105
QSV_HAVE_VCM
#define QSV_HAVE_VCM
Definition: qsvenc.h:65
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:481
qsvenc.h
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:445
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:218
QSV_RUNTIME_VERSION_ATLEAST
#define QSV_RUNTIME_VERSION_ATLEAST(MFX_VERSION, MAJOR, MINOR)
Definition: qsv_internal.h:59
info
MIPS optimizations info
Definition: mips.txt:2
rc_names
static const struct @113 rc_names[]
QSVEncContext::nb_extparam_internal
int nb_extparam_internal
Definition: qsvenc.h:145
QSVEncContext::pic_timing_sei
int pic_timing_sei
Definition: qsvenc.h:164
print_profile
static const char * print_profile(mfxU16 profile)
Definition: qsvenc.c:74
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:141
AVQSVContext::nb_opaque_surfaces
int nb_opaque_surfaces
Encoding only, and only if opaque_alloc is set to non-zero.
Definition: qsv.h:76
init_video_param_jpeg
static int init_video_param_jpeg(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:424
AVCodecContext::rc_max_rate
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1190
av_usleep
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
QSV_HAVE_CO_VPS
#define QSV_HAVE_CO_VPS
Definition: qsvenc.h:40
QSVEncContext::forced_idr
int forced_idr
Definition: qsvenc.h:205
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:451
AVCPBProperties
This structure describes the bitrate properties of an encoded bitstream.
Definition: defs.h:104
f
#define f(width, name)
Definition: cbs_vp9.c:255
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:77
AVQSVContext::nb_ext_buffers
int nb_ext_buffers
Definition: qsv.h:52
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:389
print_interlace_msg
static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:1404
if
if(ret)
Definition: filter_design.txt:179
free_encoder_ctrl_payloads
static void free_encoder_ctrl_payloads(mfxEncodeCtrl *enc_ctrl)
Definition: qsvenc.c:1257
ff_qsv_init_session_frames
int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession, QSVFramesContext *qsv_frames_ctx, const char *load_plugins, int opaque, int gpu_copy)
Definition: qsv.c:773
check_enc_param
static int check_enc_param(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:393
AVCodecContext::rc_buffer_size
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1175
QSVFrame
Definition: qsv_internal.h:72
QSVEncContext::int_ref_cycle_size
int int_ref_cycle_size
Definition: qsvenc.h:190
QSVEncContext::opaque_surfaces
mfxFrameSurface1 ** opaque_surfaces
Definition: qsvenc.h:139
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:963
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:125
QSVEncContext::req
mfxFrameAllocRequest req
Definition: qsvenc.h:118
qsv.h
QSVEncContext::extparam_internal
mfxExtBuffer * extparam_internal[3+QSV_HAVE_CO2+QSV_HAVE_CO3+(QSV_HAVE_MF *2)]
Definition: qsvenc.h:144
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
QSVEncContext::look_ahead_downsampling
int look_ahead_downsampling
Definition: qsvenc.h:167
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:429
init_video_param
static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:488
QSVEncContext::max_dec_frame_buffering
int max_dec_frame_buffering
Definition: qsvenc.h:179
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
get_free_frame
static int get_free_frame(QSVEncContext *q, QSVFrame **f)
Definition: qsvenc.c:1285
ff_qsv_print_iopattern
int ff_qsv_print_iopattern(void *log_ctx, int mfx_iopattern, const char *extra_string)
Definition: qsv.c:93
QSVFrame::surface
mfxFrameSurface1 surface
Definition: qsv_internal.h:74
time.h
QSVFramesContext::mids_buf
AVBufferRef * mids_buf
Definition: qsv_internal.h:101
AVCodecContext::trellis
int trellis
trellis RD quantization
Definition: avcodec.h:1225
MFX_LOOKAHEAD_DS_4x
#define MFX_LOOKAHEAD_DS_4x
Definition: qsvenc.h:74
AV_PIX_FMT_QSV
@ AV_PIX_FMT_QSV
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:212
AVCodecContext::level
int level
level
Definition: avcodec.h:1647
QSVEncContext::mbbrc
int mbbrc
Definition: qsvenc.h:182
FrameInfo
Definition: af_amix.c:57
QSVEncContext::preset
int preset
Definition: qsvenc.h:161
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
ff_qsv_map_pixfmt
int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
Definition: qsv.c:202
AVQSVContext::opaque_surfaces
AVBufferRef * opaque_surfaces
Encoding only, and only if opaque_alloc is set to non-zero.
Definition: qsv.h:90
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:502
HW_CONFIG_ENCODER_DEVICE
#define HW_CONFIG_ENCODER_DEVICE(format, device_type_)
Definition: hwconfig.h:96
AVPacket::size
int size
Definition: packet.h:374
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:574
av_frame_ref
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:327
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:678
QSVEncContext::avbr_convergence
int avbr_convergence
Definition: qsvenc.h:163
AVQSVContext::session
mfxSession session
If non-NULL, the session to use for encoding or decoding.
Definition: qsv.h:41
QSVEncContext::opaque_alloc_buf
AVBufferRef * opaque_alloc_buf
Definition: qsvenc.h:140
qsv_init_opaque_alloc
static int qsv_init_opaque_alloc(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:1032
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:376
AVCodecHWConfigInternal
Definition: hwconfig.h:29
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:273
AVCPBProperties::min_bitrate
int64_t min_bitrate
Minimum bitrate of the stream, in bits per second.
Definition: defs.h:114
AVQSVContext::ext_buffers
mfxExtBuffer ** ext_buffers
Extra buffers to pass to encoder or decoder initialization.
Definition: qsv.h:51
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:372
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
QSVEncContext::max_slice_size
int max_slice_size
Definition: qsvenc.h:171
AVCPBProperties::avg_bitrate
int64_t avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: defs.h:119
FF_COMPLIANCE_NORMAL
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:1279
QSVEncContext::work_frames
QSVFrame * work_frames
Definition: qsvenc.h:108
AVCodecContext::b_quant_factor
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:660
QSVFramesContext::mids
QSVMid * mids
Definition: qsv_internal.h:102
QSVEncContext::bitrate_limit
int bitrate_limit
Definition: qsvenc.h:181
QSVEncContext::rdo
int rdo
Definition: qsvenc.h:169
QSVEncContext::opaque_alloc
mfxExtOpaqueSurfaceAlloc opaque_alloc
Definition: qsvenc.h:138
HW_CONFIG_ENCODER_FRAMES
#define HW_CONFIG_ENCODER_FRAMES(format, device_type_)
Definition: hwconfig.h:99
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:57
QSVEncContext::single_sei_nal_unit
int single_sei_nal_unit
Definition: qsvenc.h:178
hwcontext_qsv.h
av_packet_copy_props
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:380
i
int i
Definition: input.c:406
ff_qsv_enc_close
int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:1630
QSVEncContext::set_encode_ctrl_cb
SetEncodeCtrlCB * set_encode_ctrl_cb
Definition: qsvenc.h:204
log.h
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:480
QSVEncContext::internal_qs
QSVSession internal_qs
Definition: qsvenc.h:111
qsv_fifo_size
static unsigned int qsv_fifo_size(const AVFifoBuffer *fifo)
Definition: qsvenc.c:1116
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
common.h
AVCPBProperties::max_bitrate
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: defs.h:109
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:224
QSVEncContext::extparam
mfxExtBuffer ** extparam
Definition: qsvenc.h:147
QSVEncContext::async_depth
int async_depth
Definition: qsvenc.h:158
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:437
submit_frame
static int submit_frame(QSVEncContext *q, const AVFrame *frame, QSVFrame **new_frame)
Definition: qsvenc.c:1325
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:261
QSVEncContext::cavlc
int cavlc
Definition: qsvenc.h:187
AVCodecContext::hw_device_ctx
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:1903
MFX_LOOKAHEAD_DS_OFF
#define MFX_LOOKAHEAD_DS_OFF
Definition: qsvenc.h:72
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:505
clear_unused_frames
static void clear_unused_frames(QSVEncContext *q)
Definition: qsvenc.c:1268
dump_video_param
static void dump_video_param(AVCodecContext *avctx, QSVEncContext *q, mfxExtBuffer **coding_opts)
Definition: qsvenc.c:132
AVCodecContext::height
int height
Definition: avcodec.h:552
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:589
QSVEncContext::aud
int aud
Definition: qsvenc.h:176
AVCodecContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1854
avcodec.h
QSVEncContext::int_ref_type
int int_ref_type
Definition: qsvenc.h:189
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
av_buffer_allocz
AVBufferRef * av_buffer_allocz(size_t size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:83
AV_CODEC_FLAG_CLOSED_GOP
#define AV_CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:278
ret
ret
Definition: filter_design.txt:187
print_ratecontrol
static const char * print_ratecontrol(mfxU16 rc_mode)
Definition: qsvenc.c:114
QSVEncContext::look_ahead
int look_ahead
Definition: qsvenc.h:165
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCPBProperties::buffer_size
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: defs.h:125
UNMATCH
#define UNMATCH(x)
AVHWFramesContext::hwctx
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:162
ff_qsv_codec_id_to_mfx
int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
Definition: qsv.c:45
AVCodecContext::strict_std_compliance
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1276
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
QSV_HAVE_CO2
#define QSV_HAVE_CO2
Definition: qsvenc.h:38
AVCodecContext
main external API structure.
Definition: avcodec.h:379
AVFrame::height
int height
Definition: frame.h:361
QSVEncContext::b_strategy
int b_strategy
Definition: qsvenc.h:186
encode_frame
static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, const AVFrame *frame)
Definition: qsvenc.c:1416
QSV_HAVE_LA
#define QSV_HAVE_LA
Definition: qsvenc.h:49
QSVEncContext::gpb
int gpb
Definition: qsvenc.h:196
QSVEncContext::vcm
int vcm
Definition: qsvenc.h:168
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1154
AVRational::den
int den
Denominator.
Definition: rational.h:60
QSVEncContext::idr_interval
int idr_interval
Definition: qsvenc.h:159
AVQSVContext
This struct is used for communicating QSV parameters between libavcodec and the caller.
Definition: qsv.h:36
QSVSession::session
mfxSession session
Definition: qsv_internal.h:86
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:691
QSVEncContext::ver
mfxVersion ver
Definition: qsvenc.h:153
av_buffer_ref
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
QSVEncContext::session
mfxSession session
Definition: qsvenc.h:110
AVQSVFramesContext
This struct is allocated as AVHWFramesContext.hwctx.
Definition: hwcontext_qsv.h:42
desc
const char * desc
Definition: libsvtav1.c:79
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
QSV_MAX_ENC_PAYLOAD
#define QSV_MAX_ENC_PAYLOAD
Definition: qsv_internal.h:53
mem.h
AVCodecContext::max_b_frames
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:651
ff_qsv_enc_init
int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:1121
packet_internal.h
av_fifo_size
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
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
qsv_fifo_item_size
static unsigned int qsv_fifo_item_size(void)
Definition: qsvenc.c:1111
AVQSVContext::opaque_alloc
int opaque_alloc
Encoding only.
Definition: qsv.h:65
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecContext::slices
int slices
Number of slices.
Definition: avcodec.h:979
QSV_HAVE_CO3
#define QSV_HAVE_CO3
Definition: qsvenc.h:39
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
FFMAX3
#define FFMAX3(a, b, c)
Definition: macros.h:48
QSVEncContext::width_align
int width_align
Definition: qsvenc.h:114
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:552
bytestream.h
av_fifo_alloc
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
imgutils.h
hwcontext.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
qsv_retrieve_enc_jpeg_params
static int qsv_retrieve_enc_jpeg_params(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:838
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1713
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
rc_mode
mfxU16 rc_mode
Definition: qsvenc.c:84
QSVEncContext::avctx
AVCodecContext * avctx
Definition: qsvenc.h:106
QSVFrame::next
struct QSVFrame * next
Definition: qsv_internal.h:82
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:52
ff_qsv_print_error
int ff_qsv_print_error(void *log_ctx, mfxStatus err, const char *error_string)
Definition: qsv.c:168
ff_qsv_init_internal_session
int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs, const char *load_plugins, int gpu_copy)
Definition: qsv.c:375
QSVEncContext::tile_rows
int tile_rows
Definition: qsvenc.h:174
ff_qsv_encode
int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: qsvenc.c:1544
AVCodecContext::sample_aspect_ratio
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:749
QSV_HAVE_LA_HRD
#define QSV_HAVE_LA_HRD
Definition: qsvenc.h:51
AVCodecContext::compression_level
int compression_level
Definition: avcodec.h:451
qsvenc_init_session
static int qsvenc_init_session(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:1069
QSVEncContext::low_power
int low_power
Definition: qsvenc.h:195