Ticket #10062: qsvenc.c

File qsvenc.c, 97.3 KB (added by oviano, 4 years ago)
Line 
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 "config_components.h"
25
26#include <string.h>
27#include <sys/types.h>
28#include <mfxvideo.h>
29
30#include "libavutil/common.h"
31#include "libavutil/hwcontext.h"
32#include "libavutil/hwcontext_qsv.h"
33#include "libavutil/mem.h"
34#include "libavutil/log.h"
35#include "libavutil/time.h"
36#include "libavutil/imgutils.h"
37#include "libavcodec/bytestream.h"
38
39#include "avcodec.h"
40#include "internal.h"
41#include "packet_internal.h"
42#include "qsv.h"
43#include "qsv_internal.h"
44#include "qsvenc.h"
45#include "get_bits.h"
46#include "av1.h"
47
48struct profile_names {
49 mfxU16 profile;
50 const char *name;
51};
52
53static const struct profile_names avc_profiles[] = {
54 { MFX_PROFILE_AVC_BASELINE, "avc baseline" },
55 { MFX_PROFILE_AVC_MAIN, "avc main" },
56 { MFX_PROFILE_AVC_EXTENDED, "avc extended" },
57 { MFX_PROFILE_AVC_HIGH, "avc high" },
58 { MFX_PROFILE_AVC_HIGH_422, "avc high 422" },
59 { MFX_PROFILE_AVC_CONSTRAINED_BASELINE, "avc constrained baseline" },
60 { MFX_PROFILE_AVC_CONSTRAINED_HIGH, "avc constrained high" },
61 { MFX_PROFILE_AVC_PROGRESSIVE_HIGH, "avc progressive high" },
62};
63
64static const struct profile_names mpeg2_profiles[] = {
65 { MFX_PROFILE_MPEG2_SIMPLE, "mpeg2 simple" },
66 { MFX_PROFILE_MPEG2_MAIN, "mpeg2 main" },
67 { MFX_PROFILE_MPEG2_HIGH, "mpeg2 high" },
68};
69
70static const struct profile_names hevc_profiles[] = {
71 { MFX_PROFILE_HEVC_MAIN, "hevc main" },
72 { MFX_PROFILE_HEVC_MAIN10, "hevc main10" },
73 { MFX_PROFILE_HEVC_MAINSP, "hevc mainsp" },
74 { MFX_PROFILE_HEVC_REXT, "hevc rext" },
75#if QSV_VERSION_ATLEAST(1, 32)
76 { MFX_PROFILE_HEVC_SCC, "hevc scc" },
77#endif
78};
79
80static const struct profile_names vp9_profiles[] = {
81 { MFX_PROFILE_VP9_0, "vp9 0" },
82 { MFX_PROFILE_VP9_1, "vp9 1" },
83 { MFX_PROFILE_VP9_2, "vp9 2" },
84 { MFX_PROFILE_VP9_3, "vp9 3" },
85};
86
87static const struct profile_names av1_profiles[] = {
88#if QSV_VERSION_ATLEAST(1, 34)
89 { MFX_PROFILE_AV1_MAIN, "av1 main" },
90 { MFX_PROFILE_AV1_HIGH, "av1 high" },
91 { MFX_PROFILE_AV1_PRO, "av1 professional" },
92#endif
93};
94
95typedef struct QSVPacket {
96 AVPacket pkt;
97 mfxSyncPoint *sync;
98 mfxBitstream *bs;
99} QSVPacket;
100
101static const char *print_profile(enum AVCodecID codec_id, mfxU16 profile)
102{
103 const struct profile_names *profiles;
104 int i, num_profiles;
105
106 switch (codec_id) {
107 case AV_CODEC_ID_H264:
108 profiles = avc_profiles;
109 num_profiles = FF_ARRAY_ELEMS(avc_profiles);
110 break;
111
112 case AV_CODEC_ID_MPEG2VIDEO:
113 profiles = mpeg2_profiles;
114 num_profiles = FF_ARRAY_ELEMS(mpeg2_profiles);
115 break;
116
117 case AV_CODEC_ID_HEVC:
118 profiles = hevc_profiles;
119 num_profiles = FF_ARRAY_ELEMS(hevc_profiles);
120 break;
121
122 case AV_CODEC_ID_VP9:
123 profiles = vp9_profiles;
124 num_profiles = FF_ARRAY_ELEMS(vp9_profiles);
125 break;
126
127 case AV_CODEC_ID_AV1:
128 profiles = av1_profiles;
129 num_profiles = FF_ARRAY_ELEMS(av1_profiles);
130 break;
131
132 default:
133 return "unknown";
134 }
135
136 for (i = 0; i < num_profiles; i++)
137 if (profile == profiles[i].profile)
138 return profiles[i].name;
139
140 return "unknown";
141}
142
143static const struct {
144 mfxU16 rc_mode;
145 const char *name;
146} rc_names[] = {
147 { MFX_RATECONTROL_CBR, "CBR" },
148 { MFX_RATECONTROL_VBR, "VBR" },
149 { MFX_RATECONTROL_CQP, "CQP" },
150#if QSV_HAVE_AVBR
151 { MFX_RATECONTROL_AVBR, "AVBR" },
152#endif
153 { MFX_RATECONTROL_LA, "LA" },
154 { MFX_RATECONTROL_ICQ, "ICQ" },
155 { MFX_RATECONTROL_LA_ICQ, "LA_ICQ" },
156#if QSV_HAVE_VCM
157 { MFX_RATECONTROL_VCM, "VCM" },
158#endif
159#if !QSV_ONEVPL
160 { MFX_RATECONTROL_LA_EXT, "LA_EXT" },
161#endif
162 { MFX_RATECONTROL_LA_HRD, "LA_HRD" },
163 { MFX_RATECONTROL_QVBR, "QVBR" },
164};
165
166#define UPDATE_PARAM(a, b) \
167do { \
168 if ((a) != (b)) { \
169 a = b; \
170 updated = 1; \
171 } \
172} while (0) \
173
174static const char *print_ratecontrol(mfxU16 rc_mode)
175{
176 int i;
177 for (i = 0; i < FF_ARRAY_ELEMS(rc_names); i++)
178 if (rc_mode == rc_names[i].rc_mode)
179 return rc_names[i].name;
180 return "unknown";
181}
182
183static const char *print_threestate(mfxU16 val)
184{
185 if (val == MFX_CODINGOPTION_ON)
186 return "ON";
187 else if (val == MFX_CODINGOPTION_OFF)
188 return "OFF";
189 return "unknown";
190}
191
192static void dump_video_param(AVCodecContext *avctx, QSVEncContext *q,
193 mfxExtBuffer **coding_opts)
194{
195 mfxInfoMFX *info = &q->param.mfx;
196
197 // co is always at index 1
198 mfxExtCodingOption *co = (mfxExtCodingOption*)coding_opts[1];
199 mfxExtCodingOption2 *co2 = NULL;
200 mfxExtCodingOption3 *co3 = NULL;
201 mfxExtHEVCTiles *exthevctiles = NULL;
202 const char *tmp_str = NULL;
203
204 if (q->co2_idx > 0)
205 co2 = (mfxExtCodingOption2*)coding_opts[q->co2_idx];
206
207 if (q->co3_idx > 0)
208 co3 = (mfxExtCodingOption3*)coding_opts[q->co3_idx];
209
210 if (q->exthevctiles_idx > 0)
211 exthevctiles = (mfxExtHEVCTiles *)coding_opts[q->exthevctiles_idx];
212
213 av_log(avctx, AV_LOG_VERBOSE, "profile: %s; level: %"PRIu16"\n",
214 print_profile(avctx->codec_id, info->CodecProfile), info->CodecLevel);
215
216 av_log(avctx, AV_LOG_VERBOSE,
217 "GopPicSize: %"PRIu16"; GopRefDist: %"PRIu16"; GopOptFlag:%s%s; IdrInterval: %"PRIu16"\n",
218 info->GopPicSize, info->GopRefDist,
219 info->GopOptFlag & MFX_GOP_CLOSED ? " closed" : "",
220 info->GopOptFlag & MFX_GOP_STRICT ? " strict" : "",
221 info->IdrInterval);
222
223 av_log(avctx, AV_LOG_VERBOSE, "TargetUsage: %"PRIu16"; RateControlMethod: %s\n",
224 info->TargetUsage, print_ratecontrol(info->RateControlMethod));
225
226 if (info->RateControlMethod == MFX_RATECONTROL_CBR ||
227 info->RateControlMethod == MFX_RATECONTROL_VBR
228#if QSV_HAVE_VCM
229 || info->RateControlMethod == MFX_RATECONTROL_VCM
230#endif
231 ) {
232 av_log(avctx, AV_LOG_VERBOSE,
233 "BufferSizeInKB: %"PRIu16"; InitialDelayInKB: %"PRIu16"; TargetKbps: %"PRIu16"; MaxKbps: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n",
234 info->BufferSizeInKB, info->InitialDelayInKB, info->TargetKbps, info->MaxKbps, info->BRCParamMultiplier);
235 } else if (info->RateControlMethod == MFX_RATECONTROL_CQP) {
236 av_log(avctx, AV_LOG_VERBOSE, "QPI: %"PRIu16"; QPP: %"PRIu16"; QPB: %"PRIu16"\n",
237 info->QPI, info->QPP, info->QPB);
238 }
239#if QSV_HAVE_AVBR
240 else if (info->RateControlMethod == MFX_RATECONTROL_AVBR) {
241 av_log(avctx, AV_LOG_VERBOSE,
242 "TargetKbps: %"PRIu16"; Accuracy: %"PRIu16"; Convergence: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n",
243 info->TargetKbps, info->Accuracy, info->Convergence, info->BRCParamMultiplier);
244 }
245#endif
246 else if (info->RateControlMethod == MFX_RATECONTROL_LA
247 || info->RateControlMethod == MFX_RATECONTROL_LA_HRD
248 ) {
249 av_log(avctx, AV_LOG_VERBOSE,
250 "TargetKbps: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n",
251 info->TargetKbps, info->BRCParamMultiplier);
252 } else if (info->RateControlMethod == MFX_RATECONTROL_ICQ ||
253 info->RateControlMethod == MFX_RATECONTROL_LA_ICQ)
254 av_log(avctx, AV_LOG_VERBOSE, "ICQQuality: %"PRIu16"\n", info->ICQQuality);
255 av_log(avctx, AV_LOG_VERBOSE, "NumSlice: %"PRIu16"; NumRefFrame: %"PRIu16"\n",
256 info->NumSlice, info->NumRefFrame);
257 av_log(avctx, AV_LOG_VERBOSE, "RateDistortionOpt: %s\n",
258 print_threestate(co->RateDistortionOpt));
259
260 av_log(avctx, AV_LOG_VERBOSE, "RecoveryPointSEI: %s\n", print_threestate(co->RecoveryPointSEI));
261 av_log(avctx, AV_LOG_VERBOSE, "VDENC: %s\n", print_threestate(info->LowPower));
262
263 if (avctx->codec_id == AV_CODEC_ID_H264) {
264 av_log(avctx, AV_LOG_VERBOSE, "Entropy coding: %s; MaxDecFrameBuffering: %"PRIu16"\n",
265 co->CAVLC == MFX_CODINGOPTION_ON ? "CAVLC" : "CABAC", co->MaxDecFrameBuffering);
266 av_log(avctx, AV_LOG_VERBOSE,
267 "NalHrdConformance: %s; SingleSeiNalUnit: %s; VuiVclHrdParameters: %s VuiNalHrdParameters: %s\n",
268 print_threestate(co->NalHrdConformance), print_threestate(co->SingleSeiNalUnit),
269 print_threestate(co->VuiVclHrdParameters), print_threestate(co->VuiNalHrdParameters));
270 } else if ((avctx->codec_id == AV_CODEC_ID_HEVC) && QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 28)) {
271 av_log(avctx, AV_LOG_VERBOSE,
272 "NalHrdConformance: %s; VuiNalHrdParameters: %s\n",
273 print_threestate(co->NalHrdConformance), print_threestate(co->VuiNalHrdParameters));
274 }
275
276 av_log(avctx, AV_LOG_VERBOSE, "FrameRateExtD: %"PRIu32"; FrameRateExtN: %"PRIu32" \n",
277 info->FrameInfo.FrameRateExtD, info->FrameInfo.FrameRateExtN);
278
279 if (co2) {
280 if ((info->RateControlMethod == MFX_RATECONTROL_VBR && q->extbrc && q->look_ahead_depth > 0) ||
281 (info->RateControlMethod == MFX_RATECONTROL_LA) ||
282 (info->RateControlMethod == MFX_RATECONTROL_LA_HRD) ||
283 (info->RateControlMethod == MFX_RATECONTROL_LA_ICQ))
284 av_log(avctx, AV_LOG_VERBOSE, "LookAheadDepth: %"PRIu16"\n", co2->LookAheadDepth);
285
286 av_log(avctx, AV_LOG_VERBOSE, "IntRefType: %"PRIu16"; IntRefCycleSize: %"PRIu16"; IntRefQPDelta: %"PRId16"\n",
287 co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
288
289 av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSize: %d; MaxSliceSize: %d\n",
290 co2->MaxFrameSize, co2->MaxSliceSize);
291
292 av_log(avctx, AV_LOG_VERBOSE,
293 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
294 print_threestate(co2->BitrateLimit), print_threestate(co2->MBBRC),
295 print_threestate(co2->ExtBRC));
296
297 if (co2->Trellis & MFX_TRELLIS_OFF) {
298 av_log(avctx, AV_LOG_VERBOSE, "Trellis: off\n");
299 } else if (!co2->Trellis) {
300 av_log(avctx, AV_LOG_VERBOSE, "Trellis: auto\n");
301 } else {
302 char trellis_type[4];
303 int i = 0;
304 if (co2->Trellis & MFX_TRELLIS_I) trellis_type[i++] = 'I';
305 if (co2->Trellis & MFX_TRELLIS_P) trellis_type[i++] = 'P';
306 if (co2->Trellis & MFX_TRELLIS_B) trellis_type[i++] = 'B';
307 trellis_type[i] = 0;
308 av_log(avctx, AV_LOG_VERBOSE, "Trellis: %s\n", trellis_type);
309 }
310
311 switch (co2->LookAheadDS) {
312 case MFX_LOOKAHEAD_DS_OFF: tmp_str = "off"; break;
313 case MFX_LOOKAHEAD_DS_2x: tmp_str = "2x"; break;
314 case MFX_LOOKAHEAD_DS_4x: tmp_str = "4x"; break;
315 default: tmp_str = "unknown"; break;
316 }
317 av_log(avctx, AV_LOG_VERBOSE,
318 "RepeatPPS: %s; NumMbPerSlice: %"PRIu16"; LookAheadDS: %s\n",
319 print_threestate(co2->RepeatPPS), co2->NumMbPerSlice, tmp_str);
320
321 switch (co2->BRefType) {
322 case MFX_B_REF_OFF: tmp_str = "off"; break;
323 case MFX_B_REF_PYRAMID: tmp_str = "pyramid"; break;
324 default: tmp_str = "auto"; break;
325 }
326 av_log(avctx, AV_LOG_VERBOSE,
327 "AdaptiveI: %s; AdaptiveB: %s; BRefType:%s\n",
328 print_threestate(co2->AdaptiveI), print_threestate(co2->AdaptiveB), tmp_str);
329
330 av_log(avctx, AV_LOG_VERBOSE,
331 "MinQPI: %"PRIu8"; MaxQPI: %"PRIu8"; MinQPP: %"PRIu8"; MaxQPP: %"PRIu8"; MinQPB: %"PRIu8"; MaxQPB: %"PRIu8"\n",
332 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
333 av_log(avctx, AV_LOG_VERBOSE, "DisableDeblockingIdc: %"PRIu32" \n", co2->DisableDeblockingIdc);
334
335 switch (co2->SkipFrame) {
336 case MFX_SKIPFRAME_NO_SKIP:
337 av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: no_skip\n");
338 break;
339 case MFX_SKIPFRAME_INSERT_DUMMY:
340 av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: insert_dummy\n");
341 break;
342 case MFX_SKIPFRAME_INSERT_NOTHING:
343 av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: insert_nothing\n");
344 break;
345 case MFX_SKIPFRAME_BRC_ONLY:
346 av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: brc_only\n");
347 break;
348 default: break;
349 }
350 }
351
352 if (co3) {
353 if (info->RateControlMethod == MFX_RATECONTROL_QVBR)
354 av_log(avctx, AV_LOG_VERBOSE, "QVBRQuality: %"PRIu16"\n", co3->QVBRQuality);
355
356 switch (co3->PRefType) {
357 case MFX_P_REF_DEFAULT: av_log(avctx, AV_LOG_VERBOSE, "PRefType: default\n"); break;
358 case MFX_P_REF_SIMPLE: av_log(avctx, AV_LOG_VERBOSE, "PRefType: simple\n"); break;
359 case MFX_P_REF_PYRAMID: av_log(avctx, AV_LOG_VERBOSE, "PRefType: pyramid\n"); break;
360 default: av_log(avctx, AV_LOG_VERBOSE, "PRefType: unknown\n"); break;
361 }
362
363 if (avctx->codec_id == AV_CODEC_ID_HEVC)
364 av_log(avctx, AV_LOG_VERBOSE,"GPB: %s\n", print_threestate(co3->GPB));
365
366 av_log(avctx, AV_LOG_VERBOSE, "TransformSkip: %s \n", print_threestate(co3->TransformSkip));
367 av_log(avctx, AV_LOG_VERBOSE, "IntRefCycleDist: %"PRId16"\n", co3->IntRefCycleDist);
368 av_log(avctx, AV_LOG_VERBOSE, "LowDelayBRC: %s\n", print_threestate(co3->LowDelayBRC));
369 av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSizeI: %d; ", co3->MaxFrameSizeI);
370 av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSizeP: %d\n", co3->MaxFrameSizeP);
371 av_log(avctx, AV_LOG_VERBOSE, "ScenarioInfo: %"PRId16"\n", co3->ScenarioInfo);
372 }
373
374 if (exthevctiles) {
375 av_log(avctx, AV_LOG_VERBOSE, "NumTileColumns: %"PRIu16"; NumTileRows: %"PRIu16"\n",
376 exthevctiles->NumTileColumns, exthevctiles->NumTileRows);
377 }
378}
379
380static void dump_video_vp9_param(AVCodecContext *avctx, QSVEncContext *q,
381 mfxExtBuffer **coding_opts)
382{
383 mfxInfoMFX *info = &q->param.mfx;
384 mfxExtVP9Param *vp9_param = NULL;
385 mfxExtCodingOption2 *co2 = NULL;
386
387 if (q->vp9_idx >= 0)
388 vp9_param = (mfxExtVP9Param *)coding_opts[q->vp9_idx];
389
390 if (q->co2_idx >= 0)
391 co2 = (mfxExtCodingOption2*)coding_opts[q->co2_idx];
392
393 av_log(avctx, AV_LOG_VERBOSE, "profile: %s \n",
394 print_profile(avctx->codec_id, info->CodecProfile));
395
396 av_log(avctx, AV_LOG_VERBOSE,
397 "GopPicSize: %"PRIu16"; GopRefDist: %"PRIu16"; GopOptFlag:%s%s; IdrInterval: %"PRIu16"\n",
398 info->GopPicSize, info->GopRefDist,
399 info->GopOptFlag & MFX_GOP_CLOSED ? " closed" : "",
400 info->GopOptFlag & MFX_GOP_STRICT ? " strict" : "",
401 info->IdrInterval);
402
403 av_log(avctx, AV_LOG_VERBOSE, "TargetUsage: %"PRIu16"; RateControlMethod: %s\n",
404 info->TargetUsage, print_ratecontrol(info->RateControlMethod));
405
406 if (info->RateControlMethod == MFX_RATECONTROL_CBR ||
407 info->RateControlMethod == MFX_RATECONTROL_VBR) {
408 av_log(avctx, AV_LOG_VERBOSE,
409 "BufferSizeInKB: %"PRIu16"; InitialDelayInKB: %"PRIu16"; TargetKbps: %"PRIu16"; MaxKbps: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n",
410 info->BufferSizeInKB, info->InitialDelayInKB, info->TargetKbps, info->MaxKbps, info->BRCParamMultiplier);
411 } else if (info->RateControlMethod == MFX_RATECONTROL_CQP) {
412 av_log(avctx, AV_LOG_VERBOSE, "QPI: %"PRIu16"; QPP: %"PRIu16"; QPB: %"PRIu16"\n",
413 info->QPI, info->QPP, info->QPB);
414 }
415 else if (info->RateControlMethod == MFX_RATECONTROL_ICQ) {
416 av_log(avctx, AV_LOG_VERBOSE, "ICQQuality: %"PRIu16"\n", info->ICQQuality);
417 }
418 else {
419 av_log(avctx, AV_LOG_VERBOSE, "Unsupported ratecontrol method: %d \n", info->RateControlMethod);
420 }
421
422 av_log(avctx, AV_LOG_VERBOSE, "NumRefFrame: %"PRIu16"\n", info->NumRefFrame);
423 av_log(avctx, AV_LOG_VERBOSE, "FrameRateExtD: %"PRIu32"; FrameRateExtN: %"PRIu32" \n",
424 info->FrameInfo.FrameRateExtD, info->FrameInfo.FrameRateExtN);
425
426 if (co2) {
427 av_log(avctx, AV_LOG_VERBOSE,
428 "IntRefType: %"PRIu16"; IntRefCycleSize: %"PRIu16"; IntRefQPDelta: %"PRId16"\n",
429 co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
430
431 av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSize: %d\n", co2->MaxFrameSize);
432
433 av_log(avctx, AV_LOG_VERBOSE,
434 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
435 print_threestate(co2->BitrateLimit), print_threestate(co2->MBBRC),
436 print_threestate(co2->ExtBRC));
437
438 av_log(avctx, AV_LOG_VERBOSE, "VDENC: %s\n", print_threestate(info->LowPower));
439
440 av_log(avctx, AV_LOG_VERBOSE,
441 "MinQPI: %"PRIu8"; MaxQPI: %"PRIu8"; MinQPP: %"PRIu8"; MaxQPP: %"PRIu8"; MinQPB: %"PRIu8"; MaxQPB: %"PRIu8"\n",
442 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
443 }
444
445 if (vp9_param) {
446 av_log(avctx, AV_LOG_VERBOSE, "WriteIVFHeaders: %s \n",
447 print_threestate(vp9_param->WriteIVFHeaders));
448 }
449}
450
451static void dump_video_mjpeg_param(AVCodecContext *avctx, QSVEncContext *q)
452{
453 mfxInfoMFX *info = &q->param.mfx;
454
455 av_log(avctx, AV_LOG_VERBOSE, "Interleaved: %"PRIu16" \n", info->Interleaved);
456 av_log(avctx, AV_LOG_VERBOSE, "Quality: %"PRIu16" \n", info->Quality);
457 av_log(avctx, AV_LOG_VERBOSE, "RestartInterval: %"PRIu16" \n", info->RestartInterval);
458
459 av_log(avctx, AV_LOG_VERBOSE, "FrameRateExtD: %"PRIu32"; FrameRateExtN: %"PRIu32" \n",
460 info->FrameInfo.FrameRateExtD, info->FrameInfo.FrameRateExtN);
461}
462
463#if QSV_HAVE_EXT_AV1_PARAM
464static void dump_video_av1_param(AVCodecContext *avctx, QSVEncContext *q,
465 mfxExtBuffer **coding_opts)
466{
467 mfxInfoMFX *info = &q->param.mfx;
468 mfxExtAV1TileParam *av1_tile_param = (mfxExtAV1TileParam *)coding_opts[0];
469 mfxExtAV1BitstreamParam *av1_bs_param = (mfxExtAV1BitstreamParam *)coding_opts[1];
470 mfxExtCodingOption2 *co2 = (mfxExtCodingOption2*)coding_opts[2];
471 mfxExtCodingOption3 *co3 = (mfxExtCodingOption3*)coding_opts[3];
472
473 av_log(avctx, AV_LOG_VERBOSE, "profile: %s; level: %"PRIu16"\n",
474 print_profile(avctx->codec_id, info->CodecProfile), info->CodecLevel);
475
476 av_log(avctx, AV_LOG_VERBOSE,
477 "GopPicSize: %"PRIu16"; GopRefDist: %"PRIu16"; GopOptFlag:%s%s; IdrInterval: %"PRIu16"\n",
478 info->GopPicSize, info->GopRefDist,
479 info->GopOptFlag & MFX_GOP_CLOSED ? " closed" : "",
480 info->GopOptFlag & MFX_GOP_STRICT ? " strict" : "",
481 info->IdrInterval);
482
483 av_log(avctx, AV_LOG_VERBOSE, "TargetUsage: %"PRIu16"; RateControlMethod: %s\n",
484 info->TargetUsage, print_ratecontrol(info->RateControlMethod));
485
486 if (info->RateControlMethod == MFX_RATECONTROL_CBR ||
487 info->RateControlMethod == MFX_RATECONTROL_VBR)
488 av_log(avctx, AV_LOG_VERBOSE,
489 "BufferSizeInKB: %"PRIu16"; InitialDelayInKB: %"PRIu16"; TargetKbps: %"PRIu16"; MaxKbps: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n",
490 info->BufferSizeInKB, info->InitialDelayInKB, info->TargetKbps, info->MaxKbps, info->BRCParamMultiplier);
491 else if (info->RateControlMethod == MFX_RATECONTROL_CQP)
492 av_log(avctx, AV_LOG_VERBOSE, "QPI: %"PRIu16"; QPP: %"PRIu16"; QPB: %"PRIu16"\n",
493 info->QPI, info->QPP, info->QPB);
494 else if (info->RateControlMethod == MFX_RATECONTROL_ICQ)
495 av_log(avctx, AV_LOG_VERBOSE, "ICQQuality: %"PRIu16"\n", info->ICQQuality);
496 else
497 av_log(avctx, AV_LOG_VERBOSE, "Unsupported ratecontrol method: %d \n", info->RateControlMethod);
498
499 av_log(avctx, AV_LOG_VERBOSE, "NumRefFrame: %"PRIu16"\n", info->NumRefFrame);
500
501 av_log(avctx, AV_LOG_VERBOSE,
502 "IntRefType: %"PRIu16"; IntRefCycleSize: %"PRIu16
503 "; IntRefQPDelta: %"PRId16"; IntRefCycleDist: %"PRId16"\n",
504 co2->IntRefType, co2->IntRefCycleSize,
505 co2->IntRefQPDelta, co3->IntRefCycleDist);
506
507 av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSize: %d;\n", co2->MaxFrameSize);
508
509 av_log(avctx, AV_LOG_VERBOSE,
510 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
511 print_threestate(co2->BitrateLimit), print_threestate(co2->MBBRC),
512 print_threestate(co2->ExtBRC));
513
514 av_log(avctx, AV_LOG_VERBOSE, "VDENC: %s\n", print_threestate(info->LowPower));
515
516 switch (co2->BRefType) {
517 case MFX_B_REF_OFF: av_log(avctx, AV_LOG_VERBOSE, "BRefType: off\n"); break;
518 case MFX_B_REF_PYRAMID: av_log(avctx, AV_LOG_VERBOSE, "BRefType: pyramid\n"); break;
519 default: av_log(avctx, AV_LOG_VERBOSE, "BRefType: auto\n"); break;
520 }
521
522 switch (co3->PRefType) {
523 case MFX_P_REF_DEFAULT: av_log(avctx, AV_LOG_VERBOSE, "PRefType: default\n"); break;
524 case MFX_P_REF_SIMPLE: av_log(avctx, AV_LOG_VERBOSE, "PRefType: simple\n"); break;
525 case MFX_P_REF_PYRAMID: av_log(avctx, AV_LOG_VERBOSE, "PRefType: pyramid\n"); break;
526 default: av_log(avctx, AV_LOG_VERBOSE, "PRefType: unknown\n"); break;
527 }
528
529 av_log(avctx, AV_LOG_VERBOSE,
530 "MinQPI: %"PRIu8"; MaxQPI: %"PRIu8"; MinQPP: %"PRIu8"; MaxQPP: %"PRIu8"; MinQPB: %"PRIu8"; MaxQPB: %"PRIu8"\n",
531 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
532
533 av_log(avctx, AV_LOG_VERBOSE, "FrameRateExtD: %"PRIu32"; FrameRateExtN: %"PRIu32" \n",
534 info->FrameInfo.FrameRateExtD, info->FrameInfo.FrameRateExtN);
535
536 av_log(avctx, AV_LOG_VERBOSE,
537 "NumTileRows: %"PRIu16"; NumTileColumns: %"PRIu16"; NumTileGroups: %"PRIu16"\n",
538 av1_tile_param->NumTileRows, av1_tile_param->NumTileColumns, av1_tile_param->NumTileGroups);
539
540 av_log(avctx, AV_LOG_VERBOSE, "WriteIVFHeaders: %s \n",
541 print_threestate(av1_bs_param->WriteIVFHeaders));
542}
543#endif
544
545static int select_rc_mode(AVCodecContext *avctx, QSVEncContext *q)
546{
547 const char *rc_desc;
548 mfxU16 rc_mode;
549
550 int want_la = q->look_ahead;
551 int want_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
552 int want_vcm = q->vcm;
553
554 if (want_vcm && !QSV_HAVE_VCM) {
555 av_log(avctx, AV_LOG_ERROR,
556 "VCM ratecontrol mode requested, but is not supported by this SDK version\n");
557 return AVERROR(ENOSYS);
558 }
559
560 if (want_la + want_qscale + want_vcm > 1) {
561 av_log(avctx, AV_LOG_ERROR,
562 "More than one of: { constant qscale, lookahead, VCM } requested, "
563 "only one of them can be used at a time.\n");
564 return AVERROR(EINVAL);
565 }
566
567 if (want_qscale) {
568 rc_mode = MFX_RATECONTROL_CQP;
569 rc_desc = "constant quantization parameter (CQP)";
570 }
571#if QSV_HAVE_VCM
572 else if (want_vcm) {
573 rc_mode = MFX_RATECONTROL_VCM;
574 rc_desc = "video conferencing mode (VCM)";
575 }
576#endif
577 else if (want_la) {
578 rc_mode = MFX_RATECONTROL_LA;
579 rc_desc = "VBR with lookahead (LA)";
580
581 if (avctx->global_quality > 0) {
582 rc_mode = MFX_RATECONTROL_LA_ICQ;
583 rc_desc = "intelligent constant quality with lookahead (LA_ICQ)";
584 }
585 }
586 else if (avctx->global_quality > 0 && !avctx->rc_max_rate) {
587 rc_mode = MFX_RATECONTROL_ICQ;
588 rc_desc = "intelligent constant quality (ICQ)";
589 }
590 else if (avctx->rc_max_rate == avctx->bit_rate) {
591 rc_mode = MFX_RATECONTROL_CBR;
592 rc_desc = "constant bitrate (CBR)";
593 }
594#if QSV_HAVE_AVBR
595 else if (!avctx->rc_max_rate &&
596 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC) &&
597 q->avbr_accuracy &&
598 q->avbr_convergence) {
599 rc_mode = MFX_RATECONTROL_AVBR;
600 rc_desc = "average variable bitrate (AVBR)";
601 }
602#endif
603 else if (avctx->global_quality > 0) {
604 rc_mode = MFX_RATECONTROL_QVBR;
605 rc_desc = "constant quality with VBR algorithm (QVBR)";
606 }
607 else {
608 rc_mode = MFX_RATECONTROL_VBR;
609 rc_desc = "variable bitrate (VBR)";
610 }
611
612 q->param.mfx.RateControlMethod = rc_mode;
613 av_log(avctx, AV_LOG_VERBOSE, "Using the %s ratecontrol method\n", rc_desc);
614
615 return 0;
616}
617
618static int check_enc_param(AVCodecContext *avctx, QSVEncContext *q)
619{
620 mfxVideoParam param_out = { .mfx.CodecId = q->param.mfx.CodecId };
621 mfxStatus ret;
622
623#define UNMATCH(x) (param_out.mfx.x != q->param.mfx.x)
624
625 ret = MFXVideoENCODE_Query(q->session, &q->param, &param_out);
626
627 if (ret < 0) {
628 if (UNMATCH(CodecId))
629 av_log(avctx, AV_LOG_ERROR, "Current codec type is unsupported\n");
630 if (UNMATCH(CodecProfile))
631 av_log(avctx, AV_LOG_ERROR, "Current profile is unsupported\n");
632 if (UNMATCH(RateControlMethod))
633 av_log(avctx, AV_LOG_ERROR, "Selected ratecontrol mode is unsupported\n");
634 if (UNMATCH(LowPower))
635 av_log(avctx, AV_LOG_ERROR, "Low power mode is unsupported\n");
636 if (UNMATCH(FrameInfo.FrameRateExtN) || UNMATCH(FrameInfo.FrameRateExtD))
637 av_log(avctx, AV_LOG_ERROR, "Current frame rate is unsupported\n");
638 if (UNMATCH(FrameInfo.PicStruct))
639 av_log(avctx, AV_LOG_ERROR, "Current picture structure is unsupported\n");
640 if (UNMATCH(FrameInfo.Width) || UNMATCH(FrameInfo.Height))
641 av_log(avctx, AV_LOG_ERROR, "Current resolution is unsupported\n");
642 if (UNMATCH(FrameInfo.FourCC))
643 av_log(avctx, AV_LOG_ERROR, "Current pixel format is unsupported\n");
644 return 0;
645 }
646 return 1;
647}
648
649static int init_video_param_jpeg(AVCodecContext *avctx, QSVEncContext *q)
650{
651 enum AVPixelFormat sw_format = avctx->pix_fmt == AV_PIX_FMT_QSV ?
652 avctx->sw_pix_fmt : avctx->pix_fmt;
653 const AVPixFmtDescriptor *desc;
654 int ret;
655
656 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
657 if (ret < 0)
658 return AVERROR_BUG;
659 q->param.mfx.CodecId = ret;
660
661 if (avctx->level > 0)
662 q->param.mfx.CodecLevel = avctx->level;
663 q->param.mfx.CodecProfile = q->profile;
664
665 desc = av_pix_fmt_desc_get(sw_format);
666 if (!desc)
667 return AVERROR_BUG;
668
669 ret = ff_qsv_map_pixfmt(sw_format, &q->param.mfx.FrameInfo.FourCC, &q->param.mfx.FrameInfo.Shift);
670 if (ret < 0)
671 return AVERROR_BUG;
672
673 q->param.mfx.FrameInfo.CropX = 0;
674 q->param.mfx.FrameInfo.CropY = 0;
675 q->param.mfx.FrameInfo.CropW = avctx->width;
676 q->param.mfx.FrameInfo.CropH = avctx->height;
677 q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num;
678 q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den;
679 q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
680 !desc->log2_chroma_w + !desc->log2_chroma_h;
681 q->param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
682 q->param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
683
684 q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, 16);
685 q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, 16);
686
687 if (avctx->hw_frames_ctx) {
688 AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
689 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
690 q->param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
691 q->param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
692 }
693
694 if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
695 q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
696 q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
697 } else {
698 q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
699 q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
700 }
701
702 q->param.mfx.Interleaved = 1;
703 q->param.mfx.Quality = av_clip(avctx->global_quality, 1, 100);
704 q->param.mfx.RestartInterval = 0;
705
706 q->width_align = 16;
707 q->height_align = 16;
708
709 q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align);
710 q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, q->height_align);
711
712 return 0;
713}
714
715static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
716{
717 enum AVPixelFormat sw_format = avctx->pix_fmt == AV_PIX_FMT_QSV ?
718 avctx->sw_pix_fmt : avctx->pix_fmt;
719 const AVPixFmtDescriptor *desc;
720 float quant;
721 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
722 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
723 int ret;
724
725 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
726 if (ret < 0)
727 return AVERROR_BUG;
728 q->param.mfx.CodecId = ret;
729
730 if (avctx->level > 0) {
731 q->param.mfx.CodecLevel = avctx->level;
732 if (avctx->codec_id == AV_CODEC_ID_HEVC && avctx->level >= MFX_LEVEL_HEVC_4)
733 q->param.mfx.CodecLevel |= q->tier;
734 }
735
736 if (avctx->compression_level == FF_COMPRESSION_DEFAULT) {
737 avctx->compression_level = q->preset;
738 } else if (avctx->compression_level >= 0) {
739 if (avctx->compression_level > MFX_TARGETUSAGE_BEST_SPEED) {
740 av_log(avctx, AV_LOG_WARNING, "Invalid compression level: "
741 "valid range is 0-%d, using %d instead\n",
742 MFX_TARGETUSAGE_BEST_SPEED, MFX_TARGETUSAGE_BEST_SPEED);
743 avctx->compression_level = MFX_TARGETUSAGE_BEST_SPEED;
744 }
745 }
746
747 if (q->low_power == 1) {
748 q->param.mfx.LowPower = MFX_CODINGOPTION_ON;
749 } else if (q->low_power == -1)
750 q->param.mfx.LowPower = MFX_CODINGOPTION_UNKNOWN;
751 else
752 q->param.mfx.LowPower = MFX_CODINGOPTION_OFF;
753
754 q->param.mfx.CodecProfile = q->profile;
755 q->param.mfx.TargetUsage = avctx->compression_level;
756 q->param.mfx.GopPicSize = FFMAX(0, avctx->gop_size);
757 q->old_gop_size = avctx->gop_size;
758 q->param.mfx.GopRefDist = FFMAX(-1, avctx->max_b_frames) + 1;
759 q->param.mfx.GopOptFlag = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ?
760 MFX_GOP_CLOSED : MFX_GOP_STRICT;
761 q->param.mfx.IdrInterval = q->idr_interval;
762 q->param.mfx.NumSlice = avctx->slices;
763 q->param.mfx.NumRefFrame = FFMAX(0, avctx->refs);
764 q->param.mfx.EncodedOrder = 0;
765 q->param.mfx.BufferSizeInKB = 0;
766
767 desc = av_pix_fmt_desc_get(sw_format);
768 if (!desc)
769 return AVERROR_BUG;
770
771 ret = ff_qsv_map_pixfmt(sw_format, &q->param.mfx.FrameInfo.FourCC, &q->param.mfx.FrameInfo.Shift);
772 if (ret < 0)
773 return AVERROR_BUG;
774
775 q->param.mfx.FrameInfo.CropX = 0;
776 q->param.mfx.FrameInfo.CropY = 0;
777 q->param.mfx.FrameInfo.CropW = avctx->width;
778 q->param.mfx.FrameInfo.CropH = avctx->height;
779 q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num;
780 q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den;
781 q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
782 !desc->log2_chroma_w + !desc->log2_chroma_h;
783 q->param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
784 q->param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
785
786 // If the minor version is greater than or equal to 19,
787 // then can use the same alignment settings as H.264 for HEVC
788 q->width_align = (avctx->codec_id != AV_CODEC_ID_HEVC ||
789 QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 19)) ? 16 : 32;
790 q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align);
791
792 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
793 // it is important that PicStruct be setup correctly from the
794 // start--otherwise, encoding doesn't work and results in a bunch
795 // of incompatible video parameter errors
796 q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
797 // height alignment always must be 32 for interlaced video
798 q->height_align = 32;
799 } else {
800 q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
801 // for progressive video, the height should be aligned to 16 for
802 // H.264. For HEVC, depending on the version of MFX, it should be
803 // either 32 or 16. The lower number is better if possible.
804 q->height_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16;
805 }
806 q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, q->height_align);
807
808 if (avctx->hw_frames_ctx) {
809 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
810 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
811 q->param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
812 q->param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
813 }
814
815 if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
816 q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
817 q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
818 } else {
819 q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
820 q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
821 }
822 q->old_framerate = avctx->framerate;
823
824 ret = select_rc_mode(avctx, q);
825 if (ret < 0)
826 return ret;
827
828 //libmfx BRC parameters are 16 bits thus maybe overflow, then BRCParamMultiplier is needed
829 buffer_size_in_kilobytes = avctx->rc_buffer_size / 8000;
830 initial_delay_in_kilobytes = avctx->rc_initial_buffer_occupancy / 8000;
831 target_bitrate_kbps = avctx->bit_rate / 1000;
832 max_bitrate_kbps = avctx->rc_max_rate / 1000;
833 brc_param_multiplier = (FFMAX(FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
834 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
835 q->old_rc_buffer_size = avctx->rc_buffer_size;
836 q->old_rc_initial_buffer_occupancy = avctx->rc_initial_buffer_occupancy;
837 q->old_bit_rate = avctx->bit_rate;
838 q->old_rc_max_rate = avctx->rc_max_rate;
839
840 switch (q->param.mfx.RateControlMethod) {
841 case MFX_RATECONTROL_CBR:
842 case MFX_RATECONTROL_VBR:
843 if (q->extbrc) {
844 q->extco2.LookAheadDepth = q->look_ahead_depth;
845 }
846#if QSV_HAVE_VCM
847 case MFX_RATECONTROL_VCM:
848#endif
849 case MFX_RATECONTROL_QVBR:
850 q->param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
851 q->param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
852 q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
853 q->param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
854 q->param.mfx.BRCParamMultiplier = brc_param_multiplier;
855 if (q->param.mfx.RateControlMethod == MFX_RATECONTROL_QVBR)
856 q->extco3.QVBRQuality = av_clip(avctx->global_quality, 0, 51);
857 break;
858 case MFX_RATECONTROL_CQP:
859 quant = avctx->global_quality / FF_QP2LAMBDA;
860 if (avctx->codec_id == AV_CODEC_ID_AV1) {
861 q->param.mfx.QPI = av_clip_uintp2(quant * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 8);
862 q->param.mfx.QPP = av_clip_uintp2(quant, 8);
863 q->param.mfx.QPB = av_clip_uintp2(quant * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 8);
864 } else {
865 q->param.mfx.QPI = av_clip(quant * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
866 q->param.mfx.QPP = av_clip(quant, 0, 51);
867 q->param.mfx.QPB = av_clip(quant * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51);
868 }
869 q->old_global_quality = avctx->global_quality;
870 q->old_i_quant_factor = avctx->i_quant_factor;
871 q->old_i_quant_offset = avctx->i_quant_offset;
872 q->old_b_quant_factor = avctx->b_quant_factor;
873 q->old_b_quant_offset = avctx->b_quant_offset;
874
875 break;
876#if QSV_HAVE_AVBR
877 case MFX_RATECONTROL_AVBR:
878 q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
879 q->param.mfx.Convergence = q->avbr_convergence;
880 q->param.mfx.Accuracy = q->avbr_accuracy;
881 q->param.mfx.BRCParamMultiplier = brc_param_multiplier;
882 break;
883#endif
884 case MFX_RATECONTROL_LA:
885 q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
886 q->extco2.LookAheadDepth = q->look_ahead_depth;
887 q->param.mfx.BRCParamMultiplier = brc_param_multiplier;
888 break;
889 case MFX_RATECONTROL_LA_ICQ:
890 q->extco2.LookAheadDepth = q->look_ahead_depth;
891 case MFX_RATECONTROL_ICQ:
892 q->param.mfx.ICQQuality = av_clip(avctx->global_quality, 1, 51);
893 break;
894 }
895
896 // The HEVC encoder plugin currently fails with some old libmfx version if coding options
897 // are provided. Can't find the extract libmfx version which fixed it, just enable it from
898 // V1.28 in order to keep compatibility security.
899 if (((avctx->codec_id != AV_CODEC_ID_HEVC) || QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 28))
900 && (avctx->codec_id != AV_CODEC_ID_VP9)) {
901 q->extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
902 q->extco.Header.BufferSz = sizeof(q->extco);
903
904 q->extco.PicTimingSEI = q->pic_timing_sei ?
905 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
906 q->old_pic_timing_sei = q->pic_timing_sei;
907
908 if (q->rdo >= 0)
909 q->extco.RateDistortionOpt = q->rdo > 0 ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
910
911 if (avctx->codec_id == AV_CODEC_ID_H264) {
912 q->extco.CAVLC = q->cavlc ? MFX_CODINGOPTION_ON
913 : MFX_CODINGOPTION_UNKNOWN;
914
915 if (avctx->strict_std_compliance != FF_COMPLIANCE_NORMAL)
916 q->extco.NalHrdConformance = avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL ?
917 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
918
919 if (q->single_sei_nal_unit >= 0)
920 q->extco.SingleSeiNalUnit = q->single_sei_nal_unit ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
921 if (q->recovery_point_sei >= 0)
922 q->extco.RecoveryPointSEI = q->recovery_point_sei ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
923 q->extco.MaxDecFrameBuffering = q->max_dec_frame_buffering;
924 q->extco.AUDelimiter = q->aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
925 } else if (avctx->codec_id == AV_CODEC_ID_HEVC) {
926 if (avctx->strict_std_compliance != FF_COMPLIANCE_NORMAL)
927 q->extco.NalHrdConformance = avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL ?
928 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
929
930 if (q->recovery_point_sei >= 0)
931 q->extco.RecoveryPointSEI = q->recovery_point_sei ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
932
933 q->extco.AUDelimiter = q->aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
934 }
935
936 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco;
937
938 if (avctx->codec_id == AV_CODEC_ID_H264) {
939 if (q->bitrate_limit >= 0)
940 q->extco2.BitrateLimit = q->bitrate_limit ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
941
942 if (avctx->trellis >= 0)
943 q->extco2.Trellis = (avctx->trellis == 0) ? MFX_TRELLIS_OFF : (MFX_TRELLIS_I | MFX_TRELLIS_P | MFX_TRELLIS_B);
944 else
945 q->extco2.Trellis = MFX_TRELLIS_UNKNOWN;
946
947 q->extco2.LookAheadDS = q->look_ahead_downsampling;
948 q->extco2.RepeatPPS = q->repeat_pps ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
949 }
950
951 if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC) {
952 if (q->extbrc >= 0)
953 q->extco2.ExtBRC = q->extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
954 if (q->max_frame_size >= 0)
955 q->extco2.MaxFrameSize = q->max_frame_size;
956 q->old_max_frame_size = q->max_frame_size;
957 if (q->int_ref_type >= 0)
958 q->extco2.IntRefType = q->int_ref_type;
959 q->old_int_ref_type = q->int_ref_type;
960 if (q->int_ref_cycle_size >= 0)
961 q->extco2.IntRefCycleSize = q->int_ref_cycle_size;
962 q->old_int_ref_cycle_size = q->int_ref_cycle_size;
963 if (q->int_ref_qp_delta != INT16_MIN)
964 q->extco2.IntRefQPDelta = q->int_ref_qp_delta;
965 q->old_int_ref_qp_delta = q->int_ref_qp_delta;
966 if (q->max_slice_size >= 0)
967 q->extco2.MaxSliceSize = q->max_slice_size;
968 q->extco2.DisableDeblockingIdc = q->dblk_idc;
969
970 if (q->b_strategy >= 0)
971 q->extco2.BRefType = q->b_strategy ? MFX_B_REF_PYRAMID : MFX_B_REF_OFF;
972 if (q->adaptive_i >= 0)
973 q->extco2.AdaptiveI = q->adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
974 if (q->adaptive_b >= 0)
975 q->extco2.AdaptiveB = q->adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
976 if ((avctx->qmin >= 0 && avctx->qmax >= 0 && avctx->qmin > avctx->qmax) ||
977 (q->max_qp_i >= 0 && q->min_qp_i >= 0 && q->min_qp_i > q->max_qp_i) ||
978 (q->max_qp_p >= 0 && q->min_qp_p >= 0 && q->min_qp_p > q->max_qp_p) ||
979 (q->max_qp_b >= 0 && q->min_qp_b >= 0 && q->min_qp_b > q->max_qp_b)) {
980 av_log(avctx, AV_LOG_ERROR,
981 "qmin and or qmax are set but invalid,"
982 " please make sure min <= max\n");
983 return AVERROR(EINVAL);
984 }
985 if (avctx->qmin >= 0) {
986 q->extco2.MinQPI = avctx->qmin > 51 ? 51 : avctx->qmin;
987 q->extco2.MinQPP = q->extco2.MinQPB = q->extco2.MinQPI;
988 }
989 q->old_qmin = avctx->qmin;
990 if (avctx->qmax >= 0) {
991 q->extco2.MaxQPI = avctx->qmax > 51 ? 51 : avctx->qmax;
992 q->extco2.MaxQPP = q->extco2.MaxQPB = q->extco2.MaxQPI;
993 }
994 q->old_qmax = avctx->qmax;
995 if (q->min_qp_i >= 0)
996 q->extco2.MinQPI = q->min_qp_i > 51 ? 51 : q->min_qp_i;
997 q->old_min_qp_i = q->min_qp_i;
998 if (q->max_qp_i >= 0)
999 q->extco2.MaxQPI = q->max_qp_i > 51 ? 51 : q->max_qp_i;
1000 q->old_max_qp_i = q->max_qp_i;
1001 if (q->min_qp_p >= 0)
1002 q->extco2.MinQPP = q->min_qp_p > 51 ? 51 : q->min_qp_p;
1003 q->old_min_qp_p = q->min_qp_p;
1004 if (q->max_qp_p >= 0)
1005 q->extco2.MaxQPP = q->max_qp_p > 51 ? 51 : q->max_qp_p;
1006 q->old_max_qp_p = q->max_qp_p;
1007 if (q->min_qp_b >= 0)
1008 q->extco2.MinQPB = q->min_qp_b > 51 ? 51 : q->min_qp_b;
1009 q->old_min_qp_b = q->min_qp_b;
1010 if (q->max_qp_b >= 0)
1011 q->extco2.MaxQPB = q->max_qp_b > 51 ? 51 : q->max_qp_b;
1012 q->old_max_qp_b = q->max_qp_b;
1013 if (q->mbbrc >= 0)
1014 q->extco2.MBBRC = q->mbbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1015 if (q->skip_frame >= 0)
1016 q->extco2.SkipFrame = q->skip_frame;
1017
1018 q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1019 q->extco2.Header.BufferSz = sizeof(q->extco2);
1020
1021 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco2;
1022 } else if (avctx->codec_id == AV_CODEC_ID_AV1) {
1023 if (q->extbrc >= 0)
1024 q->extco2.ExtBRC = q->extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1025 if (q->b_strategy >= 0)
1026 q->extco2.BRefType = q->b_strategy ? MFX_B_REF_PYRAMID : MFX_B_REF_OFF;
1027 if (q->adaptive_i >= 0)
1028 q->extco2.AdaptiveI = q->adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1029 if (q->adaptive_b >= 0)
1030 q->extco2.AdaptiveB = q->adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1031
1032 q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1033 q->extco2.Header.BufferSz = sizeof(q->extco2);
1034
1035 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco2;
1036 }
1037
1038 if (avctx->codec_id == AV_CODEC_ID_H264) {
1039#if QSV_HAVE_MF
1040 if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 25)) {
1041 q->extmfp.Header.BufferId = MFX_EXTBUFF_MULTI_FRAME_PARAM;
1042 q->extmfp.Header.BufferSz = sizeof(q->extmfp);
1043
1044 q->extmfp.MFMode = q->mfmode;
1045 av_log(avctx,AV_LOG_VERBOSE,"MFMode:%d\n", q->extmfp.MFMode);
1046 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extmfp;
1047 }
1048#endif
1049 }
1050 q->extco3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
1051 q->extco3.Header.BufferSz = sizeof(q->extco3);
1052
1053 if (avctx->codec_id == AV_CODEC_ID_HEVC ||
1054 avctx->codec_id == AV_CODEC_ID_H264) {
1055 switch (q->p_strategy) {
1056 case 0:
1057 q->extco3.PRefType = MFX_P_REF_DEFAULT;
1058 break;
1059 case 1:
1060 q->extco3.PRefType = MFX_P_REF_SIMPLE;
1061 break;
1062 case 2:
1063 q->extco3.PRefType = MFX_P_REF_PYRAMID;
1064 break;
1065 default:
1066 q->extco3.PRefType = MFX_P_REF_DEFAULT;
1067 av_log(avctx, AV_LOG_WARNING,
1068 "invalid p_strategy, set to default\n");
1069 break;
1070 }
1071 if (q->extco3.PRefType == MFX_P_REF_PYRAMID &&
1072 avctx->max_b_frames != 0) {
1073 av_log(avctx, AV_LOG_WARNING,
1074 "Please set max_b_frames(-bf) to 0 to enable P-pyramid\n");
1075 }
1076 if (q->int_ref_cycle_dist >= 0)
1077 q->extco3.IntRefCycleDist = q->int_ref_cycle_dist;
1078 q->old_int_ref_cycle_dist = q->int_ref_cycle_dist;
1079 if (q->low_delay_brc >= 0)
1080 q->extco3.LowDelayBRC = q->low_delay_brc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1081 q->old_low_delay_brc = q->low_delay_brc;
1082 if (q->max_frame_size_i >= 0)
1083 q->extco3.MaxFrameSizeI = q->max_frame_size_i;
1084 if (q->max_frame_size_p >= 0)
1085 q->extco3.MaxFrameSizeP = q->max_frame_size_p;
1086
1087 q->extco3.ScenarioInfo = q->scenario;
1088 }
1089
1090 if (avctx->codec_id == AV_CODEC_ID_HEVC) {
1091 if (q->transform_skip >= 0)
1092 q->extco3.TransformSkip = q->transform_skip ? MFX_CODINGOPTION_ON :
1093 MFX_CODINGOPTION_OFF;
1094 else
1095 q->extco3.TransformSkip = MFX_CODINGOPTION_UNKNOWN;
1096 q->extco3.GPB = q->gpb ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1097 }
1098 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco3;
1099 }
1100
1101 if (avctx->codec_id == AV_CODEC_ID_VP9) {
1102 q->extvp9param.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
1103 q->extvp9param.Header.BufferSz = sizeof(q->extvp9param);
1104 q->extvp9param.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1105#if QSV_HAVE_EXT_VP9_TILES
1106 q->extvp9param.NumTileColumns = q->tile_cols;
1107 q->extvp9param.NumTileRows = q->tile_rows;
1108#endif
1109 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extvp9param;
1110 }
1111
1112#if QSV_HAVE_EXT_AV1_PARAM
1113 if (avctx->codec_id == AV_CODEC_ID_AV1) {
1114 if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 2, 5)) {
1115 q->extav1tileparam.Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM;
1116 q->extav1tileparam.Header.BufferSz = sizeof(q->extav1tileparam);
1117 q->extav1tileparam.NumTileColumns = q->tile_cols;
1118 q->extav1tileparam.NumTileRows = q->tile_rows;
1119 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extav1tileparam;
1120
1121 q->extav1bsparam.Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM;
1122 q->extav1bsparam.Header.BufferSz = sizeof(q->extav1bsparam);
1123 q->extav1bsparam.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1124 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extav1bsparam;
1125 } else {
1126 av_log(avctx, AV_LOG_ERROR,
1127 "This version of runtime doesn't support AV1 encoding\n");
1128 return AVERROR_UNKNOWN;
1129 }
1130 }
1131#endif
1132
1133 if (avctx->codec_id == AV_CODEC_ID_HEVC) {
1134 q->exthevctiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES;
1135 q->exthevctiles.Header.BufferSz = sizeof(q->exthevctiles);
1136 q->exthevctiles.NumTileColumns = q->tile_cols;
1137 q->exthevctiles.NumTileRows = q->tile_rows;
1138 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->exthevctiles;
1139 }
1140
1141 q->extvsi.VideoFullRange = (avctx->color_range == AVCOL_RANGE_JPEG);
1142 q->extvsi.ColourDescriptionPresent = 0;
1143
1144 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
1145 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
1146 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
1147 q->extvsi.ColourDescriptionPresent = 1;
1148 q->extvsi.ColourPrimaries = avctx->color_primaries;
1149 q->extvsi.TransferCharacteristics = avctx->color_trc;
1150 q->extvsi.MatrixCoefficients = avctx->colorspace;
1151 }
1152
1153 if ((avctx->codec_id != AV_CODEC_ID_VP9) && (q->extvsi.VideoFullRange || q->extvsi.ColourDescriptionPresent)) {
1154 q->extvsi.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO;
1155 q->extvsi.Header.BufferSz = sizeof(q->extvsi);
1156 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extvsi;
1157 }
1158
1159 if (!check_enc_param(avctx,q)) {
1160 av_log(avctx, AV_LOG_ERROR,
1161 "some encoding parameters are not supported by the QSV "
1162 "runtime. Please double check the input parameters.\n");
1163 return AVERROR(ENOSYS);
1164 }
1165
1166 return 0;
1167}
1168
1169static int qsv_retrieve_enc_jpeg_params(AVCodecContext *avctx, QSVEncContext *q)
1170{
1171 int ret = 0;
1172
1173 ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
1174 if (ret < 0)
1175 return ff_qsv_print_error(avctx, ret,
1176 "Error calling GetVideoParam");
1177
1178 q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000;
1179
1180 // for qsv mjpeg the return value maybe 0 so alloc the buffer
1181 if (q->packet_size == 0)
1182 q->packet_size = q->param.mfx.FrameInfo.Height * q->param.mfx.FrameInfo.Width * 4;
1183
1184 dump_video_mjpeg_param(avctx, q);
1185
1186 return 0;
1187}
1188
1189static int qsv_retrieve_enc_vp9_params(AVCodecContext *avctx, QSVEncContext *q)
1190{
1191 int ret = 0;
1192 mfxExtVP9Param vp9_extend_buf = {
1193 .Header.BufferId = MFX_EXTBUFF_VP9_PARAM,
1194 .Header.BufferSz = sizeof(vp9_extend_buf),
1195 };
1196
1197 mfxExtCodingOption2 co2 = {
1198 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1199 .Header.BufferSz = sizeof(co2),
1200 };
1201
1202 mfxExtCodingOption3 co3 = {
1203 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1204 .Header.BufferSz = sizeof(co3),
1205 };
1206
1207 mfxExtBuffer *ext_buffers[3];
1208 int ext_buf_num = 0;
1209
1210 q->co2_idx = q->co3_idx = q->vp9_idx = -1;
1211
1212 // It is possible the runtime doesn't support the given ext buffer
1213 if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 6)) {
1214 q->co2_idx = ext_buf_num;
1215 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1216 }
1217
1218 if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 11)) {
1219 q->co3_idx = ext_buf_num;
1220 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1221 }
1222
1223 if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 26)) {
1224 q->vp9_idx = ext_buf_num;
1225 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&vp9_extend_buf;
1226 }
1227
1228 q->param.ExtParam = ext_buffers;
1229 q->param.NumExtParam = ext_buf_num;
1230
1231 ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
1232 if (ret < 0)
1233 return ff_qsv_print_error(avctx, ret,
1234 "Error calling GetVideoParam");
1235
1236 q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000;
1237
1238 dump_video_vp9_param(avctx, q, ext_buffers);
1239
1240 return 0;
1241}
1242
1243static int qsv_retrieve_enc_av1_params(AVCodecContext *avctx, QSVEncContext *q)
1244{
1245#if QSV_HAVE_EXT_AV1_PARAM
1246 int ret = 0;
1247 mfxExtAV1TileParam av1_extend_tile_buf = {
1248 .Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM,
1249 .Header.BufferSz = sizeof(av1_extend_tile_buf),
1250 };
1251 mfxExtAV1BitstreamParam av1_bs_param = {
1252 .Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM,
1253 .Header.BufferSz = sizeof(av1_bs_param),
1254 };
1255
1256 mfxExtCodingOption2 co2 = {
1257 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1258 .Header.BufferSz = sizeof(co2),
1259 };
1260
1261 mfxExtCodingOption3 co3 = {
1262 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1263 .Header.BufferSz = sizeof(co3),
1264 };
1265
1266 mfxExtBuffer *ext_buffers[] = {
1267 (mfxExtBuffer*)&av1_extend_tile_buf,
1268 (mfxExtBuffer*)&av1_bs_param,
1269 (mfxExtBuffer*)&co2,
1270 (mfxExtBuffer*)&co3,
1271 };
1272
1273 if (!QSV_RUNTIME_VERSION_ATLEAST(q->ver, 2, 5)) {
1274 av_log(avctx, AV_LOG_ERROR,
1275 "This version of runtime doesn't support AV1 encoding\n");
1276 return AVERROR_UNKNOWN;
1277 }
1278
1279 q->param.ExtParam = ext_buffers;
1280 q->param.NumExtParam = FF_ARRAY_ELEMS(ext_buffers);
1281
1282 ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
1283 if (ret < 0)
1284 return ff_qsv_print_error(avctx, ret,
1285 "Error calling GetVideoParam");
1286
1287 q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000;
1288 dump_video_av1_param(avctx, q, ext_buffers);
1289#endif
1290 return 0;
1291}
1292
1293static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q)
1294{
1295 AVCPBProperties *cpb_props;
1296
1297 uint8_t sps_buf[512];
1298 uint8_t pps_buf[128];
1299
1300 mfxExtCodingOptionSPSPPS extradata = {
1301 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
1302 .Header.BufferSz = sizeof(extradata),
1303 .SPSBuffer = sps_buf, .SPSBufSize = sizeof(sps_buf),
1304 .PPSBuffer = pps_buf, .PPSBufSize = sizeof(pps_buf)
1305 };
1306
1307 mfxExtCodingOption co = {
1308 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION,
1309 .Header.BufferSz = sizeof(co),
1310 };
1311 mfxExtCodingOption2 co2 = {
1312 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1313 .Header.BufferSz = sizeof(co2),
1314 };
1315 mfxExtCodingOption3 co3 = {
1316 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1317 .Header.BufferSz = sizeof(co3),
1318 };
1319
1320 uint8_t vps_buf[128];
1321 mfxExtCodingOptionVPS extradata_vps = {
1322 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_VPS,
1323 .Header.BufferSz = sizeof(extradata_vps),
1324 .VPSBuffer = vps_buf,
1325 .VPSBufSize = sizeof(vps_buf),
1326 };
1327
1328 mfxExtHEVCTiles hevc_tile_buf = {
1329 .Header.BufferId = MFX_EXTBUFF_HEVC_TILES,
1330 .Header.BufferSz = sizeof(hevc_tile_buf),
1331 };
1332
1333 mfxExtBuffer *ext_buffers[6];
1334
1335 int need_pps = avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO;
1336 int ret, ext_buf_num = 0, extradata_offset = 0;
1337
1338 q->co2_idx = q->co3_idx = q->exthevctiles_idx = -1;
1339 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata;
1340 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co;
1341
1342 // It is possible the runtime doesn't support the given ext buffer
1343 if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 6)) {
1344 q->co2_idx = ext_buf_num;
1345 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1346 }
1347
1348 if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 11)) {
1349 q->co3_idx = ext_buf_num;
1350 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1351 }
1352
1353 q->hevc_vps = ((avctx->codec_id == AV_CODEC_ID_HEVC) && QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 17));
1354 if (q->hevc_vps)
1355 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata_vps;
1356 if (avctx->codec_id == AV_CODEC_ID_HEVC && QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 13)) {
1357 q->exthevctiles_idx = ext_buf_num;
1358 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hevc_tile_buf;
1359 }
1360
1361 q->param.ExtParam = ext_buffers;
1362 q->param.NumExtParam = ext_buf_num;
1363
1364 ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
1365 if (ret < 0)
1366 return ff_qsv_print_error(avctx, ret,
1367 "Error calling GetVideoParam");
1368
1369 q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000;
1370
1371 if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)
1372 || (q->hevc_vps && !extradata_vps.VPSBufSize)
1373 ) {
1374 av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n");
1375 return AVERROR_UNKNOWN;
1376 }
1377
1378 avctx->extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
1379 avctx->extradata_size += q->hevc_vps * extradata_vps.VPSBufSize;
1380
1381 avctx->extradata = av_malloc(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
1382 if (!avctx->extradata)
1383 return AVERROR(ENOMEM);
1384
1385 if (q->hevc_vps) {
1386 memcpy(avctx->extradata, vps_buf, extradata_vps.VPSBufSize);
1387 extradata_offset += extradata_vps.VPSBufSize;
1388 }
1389
1390 memcpy(avctx->extradata + extradata_offset, sps_buf, extradata.SPSBufSize);
1391 extradata_offset += extradata.SPSBufSize;
1392 if (need_pps) {
1393 memcpy(avctx->extradata + extradata_offset, pps_buf, extradata.PPSBufSize);
1394 extradata_offset += extradata.PPSBufSize;
1395 }
1396 memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1397
1398 cpb_props = ff_add_cpb_side_data(avctx);
1399 if (!cpb_props)
1400 return AVERROR(ENOMEM);
1401 cpb_props->max_bitrate = avctx->rc_max_rate;
1402 cpb_props->min_bitrate = avctx->rc_min_rate;
1403 cpb_props->avg_bitrate = avctx->bit_rate;
1404 cpb_props->buffer_size = avctx->rc_buffer_size;
1405
1406 dump_video_param(avctx, q, ext_buffers);
1407
1408 return 0;
1409}
1410
1411#if QSV_HAVE_OPAQUE
1412static int qsv_init_opaque_alloc(AVCodecContext *avctx, QSVEncContext *q)
1413{
1414 AVQSVContext *qsv = avctx->hwaccel_context;
1415 mfxFrameSurface1 *surfaces;
1416 int nb_surfaces, i;
1417
1418 nb_surfaces = qsv->nb_opaque_surfaces + q->req.NumFrameSuggested;
1419
1420 q->opaque_alloc_buf = av_buffer_allocz(sizeof(*surfaces) * nb_surfaces);
1421 if (!q->opaque_alloc_buf)
1422 return AVERROR(ENOMEM);
1423
1424 q->opaque_surfaces = av_malloc_array(nb_surfaces, sizeof(*q->opaque_surfaces));
1425 if (!q->opaque_surfaces)
1426 return AVERROR(ENOMEM);
1427
1428 surfaces = (mfxFrameSurface1*)q->opaque_alloc_buf->data;
1429 for (i = 0; i < nb_surfaces; i++) {
1430 surfaces[i].Info = q->req.Info;
1431 q->opaque_surfaces[i] = surfaces + i;
1432 }
1433
1434 q->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
1435 q->opaque_alloc.Header.BufferSz = sizeof(q->opaque_alloc);
1436 q->opaque_alloc.In.Surfaces = q->opaque_surfaces;
1437 q->opaque_alloc.In.NumSurface = nb_surfaces;
1438 q->opaque_alloc.In.Type = q->req.Type;
1439
1440 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->opaque_alloc;
1441
1442 qsv->nb_opaque_surfaces = nb_surfaces;
1443 qsv->opaque_surfaces = q->opaque_alloc_buf;
1444 qsv->opaque_alloc_type = q->req.Type;
1445
1446 return 0;
1447}
1448#endif
1449
1450static int qsvenc_init_session(AVCodecContext *avctx, QSVEncContext *q)
1451{
1452 int ret;
1453
1454 if (avctx->hwaccel_context) {
1455 AVQSVContext *qsv = avctx->hwaccel_context;
1456 q->session = qsv->session;
1457 } else if (avctx->hw_frames_ctx) {
1458 q->frames_ctx.hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
1459 if (!q->frames_ctx.hw_frames_ctx)
1460 return AVERROR(ENOMEM);
1461
1462 ret = ff_qsv_init_session_frames(avctx, &q->internal_qs.session,
1463 &q->frames_ctx, q->load_plugins,
1464#if QSV_HAVE_OPAQUE
1465 q->param.IOPattern == MFX_IOPATTERN_IN_OPAQUE_MEMORY,
1466#else
1467 0,
1468#endif
1469 MFX_GPUCOPY_OFF);
1470 if (ret < 0) {
1471 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
1472 return ret;
1473 }
1474
1475 q->session = q->internal_qs.session;
1476 } else if (avctx->hw_device_ctx) {
1477 ret = ff_qsv_init_session_device(avctx, &q->internal_qs.session,
1478 avctx->hw_device_ctx, q->load_plugins,
1479 MFX_GPUCOPY_OFF);
1480 if (ret < 0)
1481 return ret;
1482
1483 q->session = q->internal_qs.session;
1484 } else {
1485 ret = ff_qsv_init_internal_session(avctx, &q->internal_qs,
1486 q->load_plugins, MFX_GPUCOPY_OFF);
1487 if (ret < 0)
1488 return ret;
1489
1490 q->session = q->internal_qs.session;
1491 }
1492
1493 return 0;
1494}
1495
1496int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
1497{
1498 int iopattern = 0;
1499 int opaque_alloc = 0;
1500 int ret;
1501
1502 q->param.AsyncDepth = q->async_depth;
1503
1504 q->async_fifo = av_fifo_alloc2(q->async_depth, sizeof(QSVPacket), 0);
1505 if (!q->async_fifo)
1506 return AVERROR(ENOMEM);
1507
1508 if (avctx->codec_id == AV_CODEC_ID_AV1) {
1509 q->input_pts = av_fifo_alloc2(8, sizeof(int64_t), AV_FIFO_FLAG_AUTO_GROW);
1510 if (!q->input_pts)
1511 return AVERROR(ENOMEM);
1512 q->output_pkts = av_fifo_alloc2(8, sizeof(AVPacket), AV_FIFO_FLAG_AUTO_GROW);
1513 if (!q->output_pkts)
1514 return AVERROR(ENOMEM);
1515 av_packet_unref(&q->current_pkt);
1516 }
1517
1518 if (avctx->hwaccel_context) {
1519 AVQSVContext *qsv = avctx->hwaccel_context;
1520
1521 iopattern = qsv->iopattern;
1522 opaque_alloc = qsv->opaque_alloc;
1523 }
1524
1525 if (avctx->hw_frames_ctx) {
1526 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1527 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
1528
1529 if (!iopattern) {
1530#if QSV_HAVE_OPAQUE
1531 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
1532 iopattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY;
1533 else if (frames_hwctx->frame_type &
1534 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1535 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1536#else
1537 if (frames_hwctx->frame_type &
1538 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1539 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1540#endif
1541 }
1542 }
1543
1544 if (!iopattern)
1545 iopattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
1546 q->param.IOPattern = iopattern;
1547 ff_qsv_print_iopattern(avctx, iopattern, "Encoder");
1548
1549 ret = qsvenc_init_session(avctx, q);
1550 if (ret < 0)
1551 return ret;
1552
1553 ret = MFXQueryVersion(q->session,&q->ver);
1554 if (ret < 0) {
1555 return ff_qsv_print_error(avctx, ret,
1556 "Error querying mfx version");
1557 }
1558
1559 // in the mfxInfoMFX struct, JPEG is different from other codecs
1560 switch (avctx->codec_id) {
1561 case AV_CODEC_ID_MJPEG:
1562 ret = init_video_param_jpeg(avctx, q);
1563 break;
1564 default:
1565 ret = init_video_param(avctx, q);
1566 break;
1567 }
1568 if (ret < 0)
1569 return ret;
1570
1571 if (avctx->hwaccel_context) {
1572 AVQSVContext *qsv = avctx->hwaccel_context;
1573 int i, j;
1574
1575 q->extparam = av_calloc(qsv->nb_ext_buffers + q->nb_extparam_internal,
1576 sizeof(*q->extparam));
1577 if (!q->extparam)
1578 return AVERROR(ENOMEM);
1579
1580 q->param.ExtParam = q->extparam;
1581 for (i = 0; i < qsv->nb_ext_buffers; i++)
1582 q->param.ExtParam[i] = qsv->ext_buffers[i];
1583 q->param.NumExtParam = qsv->nb_ext_buffers;
1584
1585 for (i = 0; i < q->nb_extparam_internal; i++) {
1586 for (j = 0; j < qsv->nb_ext_buffers; j++) {
1587 if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId)
1588 break;
1589 }
1590 if (j < qsv->nb_ext_buffers)
1591 continue;
1592
1593 q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i];
1594 }
1595 } else {
1596 q->param.ExtParam = q->extparam_internal;
1597 q->param.NumExtParam = q->nb_extparam_internal;
1598 }
1599
1600 ret = MFXVideoENCODE_Query(q->session, &q->param, &q->param);
1601 if (ret == MFX_WRN_PARTIAL_ACCELERATION) {
1602 av_log(avctx, AV_LOG_WARNING, "Encoder will work with partial HW acceleration\n");
1603 } else if (ret < 0) {
1604 return ff_qsv_print_error(avctx, ret,
1605 "Error querying encoder params");
1606 }
1607
1608 ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req);
1609 if (ret < 0)
1610 return ff_qsv_print_error(avctx, ret,
1611 "Error querying (IOSurf) the encoding parameters");
1612
1613 if (opaque_alloc) {
1614#if QSV_HAVE_OPAQUE
1615 ret = qsv_init_opaque_alloc(avctx, q);
1616 if (ret < 0)
1617 return ret;
1618#else
1619 av_log(avctx, AV_LOG_ERROR, "User is requesting to allocate OPAQUE surface, "
1620 "however libmfx %d.%d doesn't support OPAQUE memory.\n",
1621 q->ver.Major, q->ver.Minor);
1622 return AVERROR_UNKNOWN;
1623#endif
1624 }
1625
1626 ret = MFXVideoENCODE_Init(q->session, &q->param);
1627 if (ret < 0)
1628 return ff_qsv_print_error(avctx, ret,
1629 "Error initializing the encoder");
1630 else if (ret > 0)
1631 ff_qsv_print_warning(avctx, ret,
1632 "Warning in encoder initialization");
1633
1634 switch (avctx->codec_id) {
1635 case AV_CODEC_ID_MJPEG:
1636 ret = qsv_retrieve_enc_jpeg_params(avctx, q);
1637 break;
1638 case AV_CODEC_ID_VP9:
1639 ret = qsv_retrieve_enc_vp9_params(avctx, q);
1640 break;
1641 case AV_CODEC_ID_AV1:
1642 ret = qsv_retrieve_enc_av1_params(avctx, q);
1643 break;
1644 default:
1645 ret = qsv_retrieve_enc_params(avctx, q);
1646 break;
1647 }
1648 if (ret < 0) {
1649 av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n");
1650 return ret;
1651 }
1652
1653 q->avctx = avctx;
1654
1655 return 0;
1656}
1657
1658static void free_encoder_ctrl(mfxEncodeCtrl* enc_ctrl)
1659{
1660 if (enc_ctrl) {
1661 for (int i = 0; i < enc_ctrl->NumPayload && i < QSV_MAX_ENC_PAYLOAD; i++)
1662 av_freep(&enc_ctrl->Payload[i]);
1663
1664 for (int i = 0; i < enc_ctrl->NumExtParam && i < QSV_MAX_ENC_EXTPARAM; i++)
1665 av_freep(&enc_ctrl->ExtParam[i]);
1666
1667 enc_ctrl->NumPayload = 0;
1668 enc_ctrl->NumExtParam = 0;
1669 }
1670}
1671
1672static void clear_unused_frames(QSVEncContext *q)
1673{
1674 QSVFrame *cur = q->work_frames;
1675 while (cur) {
1676 if (cur->used && !cur->surface.Data.Locked) {
1677 free_encoder_ctrl(&cur->enc_ctrl);
1678 //do not reuse enc_ctrl from previous frame
1679 memset(&cur->enc_ctrl, 0, sizeof(cur->enc_ctrl));
1680 cur->enc_ctrl.Payload = cur->payloads;
1681 cur->enc_ctrl.ExtParam = cur->extparam;
1682 if (cur->frame->format == AV_PIX_FMT_QSV) {
1683 av_frame_unref(cur->frame);
1684 }
1685 cur->used = 0;
1686 }
1687 cur = cur->next;
1688 }
1689}
1690
1691static int get_free_frame(QSVEncContext *q, QSVFrame **f)
1692{
1693 QSVFrame *frame, **last;
1694
1695 clear_unused_frames(q);
1696
1697 frame = q->work_frames;
1698 last = &q->work_frames;
1699 while (frame) {
1700 if (!frame->used) {
1701 *f = frame;
1702 frame->used = 1;
1703 return 0;
1704 }
1705
1706 last = &frame->next;
1707 frame = frame->next;
1708 }
1709
1710 frame = av_mallocz(sizeof(*frame));
1711 if (!frame)
1712 return AVERROR(ENOMEM);
1713 frame->frame = av_frame_alloc();
1714 if (!frame->frame) {
1715 av_freep(&frame);
1716 return AVERROR(ENOMEM);
1717 }
1718 frame->enc_ctrl.Payload = frame->payloads;
1719 frame->enc_ctrl.ExtParam = frame->extparam;
1720 *last = frame;
1721
1722 *f = frame;
1723 frame->used = 1;
1724
1725 return 0;
1726}
1727
1728static int qsvenc_fill_padding_area(AVFrame *frame, int new_w, int new_h)
1729{
1730 const AVPixFmtDescriptor *desc;
1731 int max_step[4], filled[4] = { 0 };
1732
1733 desc = av_pix_fmt_desc_get(frame->format);
1734 av_assert0(desc);
1735 av_image_fill_max_pixsteps(max_step, NULL, desc);
1736
1737 for (int i = 0; i < desc->nb_components; i++) {
1738 const AVComponentDescriptor *comp = &desc->comp[i];
1739 int sheight, dheight, plane = comp->plane;
1740 ptrdiff_t swidth = av_image_get_linesize(frame->format,
1741 frame->width,
1742 plane);
1743 ptrdiff_t dwidth = av_image_get_linesize(frame->format,
1744 new_w,
1745 plane);
1746
1747 if (swidth < 0 || dwidth < 0) {
1748 av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
1749 return AVERROR(EINVAL);
1750 }
1751
1752 if (filled[plane])
1753 continue;
1754
1755 sheight = frame->height;
1756 dheight = new_h;
1757
1758 if (plane) {
1759 sheight = AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h);
1760 dheight = AV_CEIL_RSHIFT(new_h, desc->log2_chroma_h);
1761 }
1762
1763 // Fill right padding
1764 if (new_w > frame->width) {
1765 for (int j = 0; j < sheight; j++) {
1766 void *line_ptr = frame->data[plane] + j * frame->linesize[plane] + swidth;
1767
1768 av_memcpy_backptr(line_ptr,
1769 max_step[plane],
1770 new_w - frame->width);
1771 }
1772 }
1773
1774 // Fill bottom padding
1775 for (int j = sheight; j < dheight; j++)
1776 memcpy(frame->data[plane] + j * frame->linesize[plane],
1777 frame->data[plane] + (sheight - 1) * frame->linesize[plane],
1778 dwidth);
1779
1780 filled[plane] = 1;
1781 }
1782
1783 return 0;
1784}
1785
1786static int submit_frame(QSVEncContext *q, const AVFrame *frame,
1787 QSVFrame **new_frame)
1788{
1789 QSVFrame *qf;
1790 int ret;
1791
1792 ret = get_free_frame(q, &qf);
1793 if (ret < 0)
1794 return ret;
1795
1796 if (frame->format == AV_PIX_FMT_QSV) {
1797 ret = av_frame_ref(qf->frame, frame);
1798 if (ret < 0)
1799 return ret;
1800
1801 qf->surface = *(mfxFrameSurface1*)qf->frame->data[3];
1802
1803 if (q->frames_ctx.mids) {
1804 ret = ff_qsv_find_surface_idx(&q->frames_ctx, qf);
1805 if (ret < 0)
1806 return ret;
1807
1808 qf->surface.Data.MemId = &q->frames_ctx.mids[ret];
1809 }
1810 } else {
1811 /* make a copy if the input is not padded as libmfx requires */
1812 /* and to make allocation continious for data[0]/data[1] */
1813 if ((frame->height & 31 || frame->linesize[0] & (q->width_align - 1)) ||
1814 (frame->data[1] - frame->data[0] != frame->linesize[0] * FFALIGN(qf->frame->height, q->height_align))) {
1815 int tmp_w, tmp_h;
1816 qf->frame->height = tmp_h = FFALIGN(frame->height, q->height_align);
1817 qf->frame->width = tmp_w = FFALIGN(frame->width, q->width_align);
1818
1819 qf->frame->format = frame->format;
1820
1821 if (!qf->frame->data[0]) {
1822 ret = av_frame_get_buffer(qf->frame, q->width_align);
1823 if (ret < 0)
1824 return ret;
1825 }
1826
1827 qf->frame->height = frame->height;
1828 qf->frame->width = frame->width;
1829
1830 ret = av_frame_copy(qf->frame, frame);
1831 if (ret < 0) {
1832 av_frame_unref(qf->frame);
1833 return ret;
1834 }
1835
1836 ret = qsvenc_fill_padding_area(qf->frame, tmp_w, tmp_h);
1837 if (ret < 0) {
1838 av_frame_unref(qf->frame);
1839 return ret;
1840 }
1841 } else {
1842 av_frame_unref(qf->frame);
1843 ret = av_frame_ref(qf->frame, frame);
1844 if (ret < 0)
1845 return ret;
1846 }
1847
1848 qf->surface.Info = q->param.mfx.FrameInfo;
1849
1850 qf->surface.Info.PicStruct =
1851 !frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
1852 frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
1853 MFX_PICSTRUCT_FIELD_BFF;
1854 if (frame->repeat_pict == 1)
1855 qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
1856 else if (frame->repeat_pict == 2)
1857 qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
1858 else if (frame->repeat_pict == 4)
1859 qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
1860
1861 ret = ff_qsv_map_frame_to_surface(qf->frame, &qf->surface);
1862 if (ret < 0) {
1863 av_log(q->avctx, AV_LOG_ERROR, "map frame to surface failed.\n");
1864 return ret;
1865 }
1866 }
1867
1868 if (q->avctx->codec_id == AV_CODEC_ID_AV1) {
1869 av_fifo_write(q->input_pts, &frame->pts, 1);
1870 }
1871 else
1872 qf->surface.Data.TimeStamp = av_rescale_q(frame->pts, q->avctx->time_base, (AVRational) { 1, 90000 });
1873
1874 *new_frame = qf;
1875
1876 return 0;
1877}
1878
1879static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
1880{
1881 if (q->param.mfx.CodecId == MFX_CODEC_AVC) {
1882 if (q->param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
1883 q->param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
1884 q->param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
1885 av_log(avctx, AV_LOG_WARNING,
1886 "Interlaced coding is supported"
1887 " at Main/High Profile Level 2.2-4.0\n");
1888 }
1889}
1890
1891static int set_roi_encode_ctrl(AVCodecContext *avctx, const AVFrame *frame,
1892 mfxEncodeCtrl *enc_ctrl)
1893{
1894 AVFrameSideData *sd = NULL;
1895 int mb_size;
1896
1897 if (avctx->codec_id == AV_CODEC_ID_H264)
1898 mb_size = 16;
1899 else if (avctx->codec_id == AV_CODEC_ID_H265)
1900 mb_size = 32;
1901 else
1902 return 0;
1903
1904 if (frame)
1905 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
1906
1907 if (sd) {
1908 mfxExtEncoderROI *enc_roi = NULL;
1909 AVRegionOfInterest *roi;
1910 uint32_t roi_size;
1911 int nb_roi, i;
1912
1913 roi = (AVRegionOfInterest *)sd->data;
1914 roi_size = roi->self_size;
1915 if (!roi_size || sd->size % roi_size) {
1916 av_log(avctx, AV_LOG_ERROR, "Invalid ROI Data.\n");
1917 return AVERROR(EINVAL);
1918 }
1919 nb_roi = sd->size / roi_size;
1920 if (nb_roi > QSV_MAX_ROI_NUM) {
1921 av_log(avctx, AV_LOG_WARNING, "More ROIs set than "
1922 "supported by driver (%d > %d).\n",
1923 nb_roi, QSV_MAX_ROI_NUM);
1924 nb_roi = QSV_MAX_ROI_NUM;
1925 }
1926
1927 enc_roi = av_mallocz(sizeof(*enc_roi));
1928 if (!enc_roi)
1929 return AVERROR(ENOMEM);
1930 enc_roi->Header.BufferId = MFX_EXTBUFF_ENCODER_ROI;
1931 enc_roi->Header.BufferSz = sizeof(*enc_roi);
1932 enc_roi->NumROI = nb_roi;
1933 enc_roi->ROIMode = MFX_ROI_MODE_QP_DELTA;
1934 for (i = 0; i < nb_roi; i++) {
1935 roi = (AVRegionOfInterest *)(sd->data + roi_size * i);
1936 enc_roi->ROI[i].Top = FFALIGN(roi->top, mb_size);
1937 enc_roi->ROI[i].Bottom = FFALIGN(roi->bottom, mb_size);
1938 enc_roi->ROI[i].Left = FFALIGN(roi->left, mb_size);
1939 enc_roi->ROI[i].Right = FFALIGN(roi->right, mb_size);
1940 enc_roi->ROI[i].DeltaQP =
1941 roi->qoffset.num * 51 / roi->qoffset.den;
1942 av_log(avctx, AV_LOG_DEBUG, "ROI: (%d,%d)-(%d,%d) -> %+d.\n",
1943 roi->top, roi->left, roi->bottom, roi->right,
1944 enc_roi->ROI[i].DeltaQP);
1945 }
1946 enc_ctrl->ExtParam[enc_ctrl->NumExtParam] = (mfxExtBuffer *)enc_roi;
1947 enc_ctrl->NumExtParam++;
1948 }
1949 return 0;
1950}
1951
1952static void set_skip_frame_encode_ctrl(AVCodecContext *avctx, const AVFrame *frame,
1953 mfxEncodeCtrl *enc_ctrl)
1954{
1955 AVDictionaryEntry* skip_frame_dict = NULL;
1956 if (!frame->metadata)
1957 return;
1958 skip_frame_dict = av_dict_get(frame->metadata, "qsv_skip_frame", NULL, 0);
1959 if (!skip_frame_dict)
1960 return;
1961 enc_ctrl->SkipFrame = strtol(skip_frame_dict->value, NULL, 10);
1962 return;
1963}
1964
1965static int update_qp(AVCodecContext *avctx, QSVEncContext *q)
1966{
1967 int updated = 0, new_qp = 0;
1968
1969 if (avctx->codec_id != AV_CODEC_ID_H264 && avctx->codec_id != AV_CODEC_ID_HEVC)
1970 return 0;
1971
1972 if (q->param.mfx.RateControlMethod == MFX_RATECONTROL_CQP) {
1973 UPDATE_PARAM(q->old_global_quality, avctx->global_quality);
1974 UPDATE_PARAM(q->old_i_quant_factor, avctx->i_quant_factor);
1975 UPDATE_PARAM(q->old_i_quant_offset, avctx->i_quant_offset);
1976 UPDATE_PARAM(q->old_b_quant_factor, avctx->b_quant_factor);
1977 UPDATE_PARAM(q->old_b_quant_offset, avctx->b_quant_offset);
1978 if (!updated)
1979 return 0;
1980
1981 new_qp = avctx->global_quality / FF_QP2LAMBDA;
1982 q->param.mfx.QPI = av_clip(new_qp * fabs(avctx->i_quant_factor) +
1983 avctx->i_quant_offset, 0, 51);
1984 q->param.mfx.QPP = av_clip(new_qp, 0, 51);
1985 q->param.mfx.QPB = av_clip(new_qp * fabs(avctx->b_quant_factor) +
1986 avctx->b_quant_offset, 0, 51);
1987 av_log(avctx, AV_LOG_DEBUG,
1988 "Reset qp = %d/%d/%d for idr/p/b frames\n",
1989 q->param.mfx.QPI, q->param.mfx.QPP, q->param.mfx.QPB);
1990 }
1991 return updated;
1992}
1993
1994static int update_max_frame_size(AVCodecContext *avctx, QSVEncContext *q)
1995{
1996 int updated = 0;
1997
1998 if (avctx->codec_id != AV_CODEC_ID_H264 && avctx->codec_id != AV_CODEC_ID_HEVC)
1999 return 0;
2000
2001 UPDATE_PARAM(q->old_max_frame_size, q->max_frame_size);
2002 if (!updated)
2003 return 0;
2004
2005 q->extco2.MaxFrameSize = FFMAX(0, q->max_frame_size);
2006 av_log(avctx, AV_LOG_DEBUG,
2007 "Reset MaxFrameSize: %d;\n", q->extco2.MaxFrameSize);
2008
2009 return updated;
2010}
2011
2012static int update_gop_size(AVCodecContext *avctx, QSVEncContext *q)
2013{
2014 int updated = 0;
2015 UPDATE_PARAM(q->old_gop_size, avctx->gop_size);
2016 if (!updated)
2017 return 0;
2018
2019 q->param.mfx.GopPicSize = FFMAX(0, avctx->gop_size);
2020 av_log(avctx, AV_LOG_DEBUG, "reset GopPicSize to %d\n",
2021 q->param.mfx.GopPicSize);
2022
2023 return updated;
2024}
2025
2026static int update_rir(AVCodecContext *avctx, QSVEncContext *q)
2027{
2028 int updated = 0;
2029
2030 if (avctx->codec_id != AV_CODEC_ID_H264 && avctx->codec_id != AV_CODEC_ID_HEVC)
2031 return 0;
2032
2033 UPDATE_PARAM(q->old_int_ref_type, q->int_ref_type);
2034 UPDATE_PARAM(q->old_int_ref_cycle_size, q->int_ref_cycle_size);
2035 UPDATE_PARAM(q->old_int_ref_qp_delta, q->int_ref_qp_delta);
2036 UPDATE_PARAM(q->old_int_ref_cycle_dist, q->int_ref_cycle_dist);
2037 if (!updated)
2038 return 0;
2039
2040 q->extco2.IntRefType = FFMAX(0, q->int_ref_type);
2041 q->extco2.IntRefCycleSize = FFMAX(0, q->int_ref_cycle_size);
2042 q->extco2.IntRefQPDelta =
2043 q->int_ref_qp_delta != INT16_MIN ? q->int_ref_qp_delta : 0;
2044 q->extco3.IntRefCycleDist = FFMAX(0, q->int_ref_cycle_dist);
2045 av_log(avctx, AV_LOG_DEBUG,
2046 "Reset IntRefType: %d; IntRefCycleSize: %d; "
2047 "IntRefQPDelta: %d; IntRefCycleDist: %d\n",
2048 q->extco2.IntRefType, q->extco2.IntRefCycleSize,
2049 q->extco2.IntRefQPDelta, q->extco3.IntRefCycleDist);
2050
2051 return updated;
2052}
2053
2054static int update_min_max_qp(AVCodecContext *avctx, QSVEncContext *q)
2055{
2056 int updated = 0;
2057
2058 if (avctx->codec_id != AV_CODEC_ID_H264)
2059 return 0;
2060
2061 UPDATE_PARAM(q->old_qmin, avctx->qmin);
2062 UPDATE_PARAM(q->old_qmax, avctx->qmax);
2063 UPDATE_PARAM(q->old_min_qp_i, q->min_qp_i);
2064 UPDATE_PARAM(q->old_max_qp_i, q->max_qp_i);
2065 UPDATE_PARAM(q->old_min_qp_p, q->min_qp_p);
2066 UPDATE_PARAM(q->old_max_qp_p, q->max_qp_p);
2067 UPDATE_PARAM(q->old_min_qp_b, q->min_qp_b);
2068 UPDATE_PARAM(q->old_max_qp_b, q->max_qp_b);
2069 if (!updated)
2070 return 0;
2071
2072 if ((avctx->qmin >= 0 && avctx->qmax >= 0 && avctx->qmin > avctx->qmax) ||
2073 (q->max_qp_i >= 0 && q->min_qp_i >= 0 && q->min_qp_i > q->max_qp_i) ||
2074 (q->max_qp_p >= 0 && q->min_qp_p >= 0 && q->min_qp_p > q->max_qp_p) ||
2075 (q->max_qp_b >= 0 && q->min_qp_b >= 0 && q->min_qp_b > q->max_qp_b)) {
2076 av_log(avctx, AV_LOG_ERROR,
2077 "qmin and or qmax are set but invalid,"
2078 " please make sure min <= max\n");
2079 return AVERROR(EINVAL);
2080 }
2081
2082 q->extco2.MinQPI = 0;
2083 q->extco2.MaxQPI = 0;
2084 q->extco2.MinQPP = 0;
2085 q->extco2.MaxQPP = 0;
2086 q->extco2.MinQPB = 0;
2087 q->extco2.MaxQPB = 0;
2088 if (avctx->qmin >= 0) {
2089 q->extco2.MinQPI = avctx->qmin > 51 ? 51 : avctx->qmin;
2090 q->extco2.MinQPB = q->extco2.MinQPP = q->extco2.MinQPI;
2091 }
2092 if (avctx->qmax >= 0) {
2093 q->extco2.MaxQPI = avctx->qmax > 51 ? 51 : avctx->qmax;
2094 q->extco2.MaxQPB = q->extco2.MaxQPP = q->extco2.MaxQPI;
2095 }
2096 if (q->min_qp_i >= 0)
2097 q->extco2.MinQPI = q->min_qp_i > 51 ? 51 : q->min_qp_i;
2098 if (q->max_qp_i >= 0)
2099 q->extco2.MaxQPI = q->max_qp_i > 51 ? 51 : q->max_qp_i;
2100 if (q->min_qp_p >= 0)
2101 q->extco2.MinQPP = q->min_qp_p > 51 ? 51 : q->min_qp_p;
2102 if (q->max_qp_p >= 0)
2103 q->extco2.MaxQPP = q->max_qp_p > 51 ? 51 : q->max_qp_p;
2104 if (q->min_qp_b >= 0)
2105 q->extco2.MinQPB = q->min_qp_b > 51 ? 51 : q->min_qp_b;
2106 if (q->max_qp_b >= 0)
2107 q->extco2.MaxQPB = q->max_qp_b > 51 ? 51 : q->max_qp_b;
2108
2109 av_log(avctx, AV_LOG_VERBOSE, "Reset MinQPI: %d; MaxQPI: %d; "
2110 "MinQPP: %d; MaxQPP: %d; "
2111 "MinQPB: %d; MaxQPB: %d\n",
2112 q->extco2.MinQPI, q->extco2.MaxQPI,
2113 q->extco2.MinQPP, q->extco2.MaxQPP,
2114 q->extco2.MinQPB, q->extco2.MaxQPB);
2115
2116 return updated;
2117}
2118
2119static int update_low_delay_brc(AVCodecContext *avctx, QSVEncContext *q)
2120{
2121 int updated = 0;
2122
2123 if (avctx->codec_id != AV_CODEC_ID_H264 && avctx->codec_id != AV_CODEC_ID_HEVC)
2124 return 0;
2125
2126 UPDATE_PARAM(q->old_low_delay_brc, q->low_delay_brc);
2127 if (!updated)
2128 return 0;
2129
2130 q->extco3.LowDelayBRC = MFX_CODINGOPTION_UNKNOWN;
2131 if (q->low_delay_brc >= 0)
2132 q->extco3.LowDelayBRC = q->low_delay_brc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
2133 av_log(avctx, AV_LOG_DEBUG, "Reset LowDelayBRC: %s\n",
2134 print_threestate(q->extco3.LowDelayBRC));
2135
2136 return updated;
2137}
2138
2139static int update_frame_rate(AVCodecContext *avctx, QSVEncContext *q)
2140{
2141 int updated = 0;
2142
2143 UPDATE_PARAM(q->old_framerate.num, avctx->framerate.num);
2144 UPDATE_PARAM(q->old_framerate.den, avctx->framerate.den);
2145 if (!updated)
2146 return 0;
2147
2148 if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
2149 q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
2150 q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
2151 } else {
2152 q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
2153 q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
2154 }
2155 av_log(avctx, AV_LOG_DEBUG, "Reset framerate: %d/%d (%.2f fps).\n",
2156 q->param.mfx.FrameInfo.FrameRateExtN,
2157 q->param.mfx.FrameInfo.FrameRateExtD,
2158 (double)q->param.mfx.FrameInfo.FrameRateExtN / q->param.mfx.FrameInfo.FrameRateExtD);
2159
2160 return updated;
2161}
2162
2163static int update_bitrate(AVCodecContext *avctx, QSVEncContext *q)
2164{
2165 int updated = 0;
2166 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
2167 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
2168
2169 UPDATE_PARAM(q->old_rc_buffer_size, avctx->rc_buffer_size);
2170 UPDATE_PARAM(q->old_rc_initial_buffer_occupancy, avctx->rc_initial_buffer_occupancy);
2171 UPDATE_PARAM(q->old_bit_rate, avctx->bit_rate);
2172 UPDATE_PARAM(q->old_rc_max_rate, avctx->rc_max_rate);
2173 if (!updated)
2174 return 0;
2175
2176 buffer_size_in_kilobytes = avctx->rc_buffer_size / 8000;
2177 initial_delay_in_kilobytes = avctx->rc_initial_buffer_occupancy / 8000;
2178 target_bitrate_kbps = avctx->bit_rate / 1000;
2179 max_bitrate_kbps = avctx->rc_max_rate / 1000;
2180 brc_param_multiplier = (FFMAX(FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
2181 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
2182
2183 q->param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
2184 q->param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
2185 q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
2186 q->param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
2187 q->param.mfx.BRCParamMultiplier = brc_param_multiplier;
2188 av_log(avctx, AV_LOG_VERBOSE,
2189 "Reset BufferSizeInKB: %d; InitialDelayInKB: %d; "
2190 "TargetKbps: %d; MaxKbps: %d; BRCParamMultiplier: %d\n",
2191 q->param.mfx.BufferSizeInKB, q->param.mfx.InitialDelayInKB,
2192 q->param.mfx.TargetKbps, q->param.mfx.MaxKbps, q->param.mfx.BRCParamMultiplier);
2193 return updated;
2194}
2195
2196static int update_pic_timing_sei(AVCodecContext *avctx, QSVEncContext *q)
2197{
2198 int updated = 0;
2199
2200 if (avctx->codec_id != AV_CODEC_ID_H264 && avctx->codec_id != AV_CODEC_ID_HEVC)
2201 return 0;
2202
2203 UPDATE_PARAM(q->old_pic_timing_sei, q->pic_timing_sei);
2204 if (!updated)
2205 return 0;
2206
2207 q->extco.PicTimingSEI = q->pic_timing_sei ?
2208 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
2209 av_log(avctx, AV_LOG_DEBUG, "Reset PicTimingSEI: %s\n",
2210 print_threestate(q->extco.PicTimingSEI));
2211
2212 return updated;
2213}
2214
2215static int update_parameters(AVCodecContext *avctx, QSVEncContext *q,
2216 const AVFrame *frame)
2217{
2218 int needReset = 0, ret = 0;
2219
2220 if (!frame || avctx->codec_id == AV_CODEC_ID_MJPEG)
2221 return 0;
2222
2223 needReset = update_qp(avctx, q);
2224 needReset |= update_max_frame_size(avctx, q);
2225 needReset |= update_gop_size(avctx, q);
2226 needReset |= update_rir(avctx, q);
2227 needReset |= update_low_delay_brc(avctx, q);
2228 needReset |= update_frame_rate(avctx, q);
2229 needReset |= update_bitrate(avctx, q);
2230 needReset |= update_pic_timing_sei(avctx, q);
2231 ret = update_min_max_qp(avctx, q);
2232 if (ret < 0)
2233 return ret;
2234 needReset |= ret;
2235 if (!needReset)
2236 return 0;
2237
2238 if (avctx->hwaccel_context) {
2239 AVQSVContext *qsv = avctx->hwaccel_context;
2240 int i, j;
2241 q->param.ExtParam = q->extparam;
2242 for (i = 0; i < qsv->nb_ext_buffers; i++)
2243 q->param.ExtParam[i] = qsv->ext_buffers[i];
2244 q->param.NumExtParam = qsv->nb_ext_buffers;
2245
2246 for (i = 0; i < q->nb_extparam_internal; i++) {
2247 for (j = 0; j < qsv->nb_ext_buffers; j++) {
2248 if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId)
2249 break;
2250 }
2251 if (j < qsv->nb_ext_buffers)
2252 continue;
2253 q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i];
2254 }
2255 } else {
2256 q->param.ExtParam = q->extparam_internal;
2257 q->param.NumExtParam = q->nb_extparam_internal;
2258 }
2259 av_log(avctx, AV_LOG_DEBUG, "Parameter change, call msdk reset.\n");
2260 ret = MFXVideoENCODE_Reset(q->session, &q->param);
2261 if (ret < 0)
2262 return ff_qsv_print_error(avctx, ret, "Error during resetting");
2263
2264 return 0;
2265}
2266
2267static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
2268 const AVFrame *frame)
2269{
2270 QSVPacket pkt = { { 0 } };
2271 mfxExtAVCEncodedFrameInfo *enc_info = NULL;
2272 mfxExtBuffer **enc_buf = NULL;
2273
2274 mfxFrameSurface1 *surf = NULL;
2275 QSVFrame *qsv_frame = NULL;
2276 mfxEncodeCtrl* enc_ctrl = NULL;
2277 int ret;
2278
2279 if (frame) {
2280 ret = submit_frame(q, frame, &qsv_frame);
2281 if (ret < 0) {
2282 av_log(avctx, AV_LOG_ERROR, "Error submitting the frame for encoding.\n");
2283 return ret;
2284 }
2285 }
2286 if (qsv_frame) {
2287 surf = &qsv_frame->surface;
2288 enc_ctrl = &qsv_frame->enc_ctrl;
2289
2290 if (frame->pict_type == AV_PICTURE_TYPE_I) {
2291 enc_ctrl->FrameType = MFX_FRAMETYPE_I | MFX_FRAMETYPE_REF;
2292 if (q->forced_idr)
2293 enc_ctrl->FrameType |= MFX_FRAMETYPE_IDR;
2294 }
2295 }
2296
2297 ret = av_new_packet(&pkt.pkt, q->packet_size);
2298 if (ret < 0) {
2299 av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
2300 return ret;
2301 }
2302
2303 pkt.bs = av_mallocz(sizeof(*pkt.bs));
2304 if (!pkt.bs)
2305 goto nomem;
2306 pkt.bs->Data = pkt.pkt.data;
2307 pkt.bs->MaxLength = pkt.pkt.size;
2308
2309 if (avctx->codec_id == AV_CODEC_ID_H264) {
2310 enc_info = av_mallocz(sizeof(*enc_info));
2311 if (!enc_info)
2312 goto nomem;
2313
2314 enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO;
2315 enc_info->Header.BufferSz = sizeof (*enc_info);
2316 pkt.bs->NumExtParam = 1;
2317 enc_buf = av_mallocz(sizeof(mfxExtBuffer *));
2318 if (!enc_buf)
2319 goto nomem;
2320 enc_buf[0] = (mfxExtBuffer *)enc_info;
2321
2322 pkt.bs->ExtParam = enc_buf;
2323 }
2324
2325 if (q->set_encode_ctrl_cb && enc_ctrl) {
2326 q->set_encode_ctrl_cb(avctx, frame, enc_ctrl);
2327 }
2328
2329 if ((avctx->codec_id == AV_CODEC_ID_H264 ||
2330 avctx->codec_id == AV_CODEC_ID_H265) &&
2331 enc_ctrl && QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 8)) {
2332 ret = set_roi_encode_ctrl(avctx, frame, enc_ctrl);
2333 if (ret < 0)
2334 goto free;
2335 }
2336 if ((avctx->codec_id == AV_CODEC_ID_H264 ||
2337 avctx->codec_id == AV_CODEC_ID_H265) &&
2338 q->skip_frame != MFX_SKIPFRAME_NO_SKIP &&
2339 enc_ctrl && QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 13))
2340 set_skip_frame_encode_ctrl(avctx, frame, enc_ctrl);
2341
2342 pkt.sync = av_mallocz(sizeof(*pkt.sync));
2343 if (!pkt.sync)
2344 goto nomem;
2345
2346 do {
2347 ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, pkt.bs, pkt.sync);
2348 if (ret == MFX_WRN_DEVICE_BUSY)
2349 av_usleep(500);
2350 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_WRN_IN_EXECUTION);
2351
2352 if (ret > 0)
2353 ff_qsv_print_warning(avctx, ret, "Warning during encoding");
2354
2355 if (ret < 0) {
2356 ret = (ret == MFX_ERR_MORE_DATA) ?
2357 0 : ff_qsv_print_error(avctx, ret, "Error during encoding");
2358 goto free;
2359 }
2360
2361 if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM && frame && frame->interlaced_frame)
2362 print_interlace_msg(avctx, q);
2363
2364 ret = 0;
2365
2366 if (*pkt.sync) {
2367 av_fifo_write(q->async_fifo, &pkt, 1);
2368 } else {
2369free:
2370 av_freep(&pkt.sync);
2371 av_packet_unref(&pkt.pkt);
2372 av_freep(&pkt.bs);
2373 if (avctx->codec_id == AV_CODEC_ID_H264) {
2374 av_freep(&enc_info);
2375 av_freep(&enc_buf);
2376 }
2377 }
2378
2379 return ret;
2380nomem:
2381 ret = AVERROR(ENOMEM);
2382 goto free;
2383}
2384
2385static unsigned get_uleb128(GetBitContext* const c, int* error) {
2386 uint64_t val = 0;
2387 unsigned i = 0, more;
2388 *error = 0;
2389
2390 do {
2391 const int v = get_bits(c, 8);
2392 more = v & 0x80;
2393 val |= ((uint64_t)(v & 0x7F)) << i;
2394 i += 7;
2395 } while (more && i < 56);
2396
2397 if (val > UINT_MAX || more) {
2398 *error = 1;
2399 return 0;
2400 }
2401
2402 return (unsigned)val;
2403}
2404
2405int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
2406 AVPacket *pkt, const AVFrame *frame, int *got_packet)
2407{
2408 int ret;
2409
2410 ret = update_parameters(avctx, q, frame);
2411 if (ret < 0)
2412 return ret;
2413
2414 ret = encode_frame(avctx, q, frame);
2415 if (ret < 0)
2416 return ret;
2417
2418 if ((av_fifo_can_read(q->async_fifo) >= q->async_depth) ||
2419 (!frame && av_fifo_can_read(q->async_fifo))) {
2420 QSVPacket qpkt;
2421 mfxExtAVCEncodedFrameInfo *enc_info;
2422 mfxExtBuffer **enc_buf;
2423 enum AVPictureType pict_type;
2424
2425 av_fifo_read(q->async_fifo, &qpkt, 1);
2426
2427 do {
2428 ret = MFXVideoCORE_SyncOperation(q->session, *qpkt.sync, 1000);
2429 } while (ret == MFX_WRN_IN_EXECUTION);
2430
2431 qpkt.pkt.size = qpkt.bs->DataLength;
2432
2433 if (avctx->codec_id == AV_CODEC_ID_AV1) {
2434 GetBitContext gbc;
2435 init_get_bits(&gbc, qpkt.pkt.data, qpkt.pkt.size * 8);
2436 while (get_bits_left(&gbc) > 0) {
2437 int start_index = gbc.index;
2438 get_bits(&gbc, 1);
2439 const enum AV1_OBU_Type type = (enum AV1_OBU_Type)get_bits(&gbc, 4);
2440 const int has_extension = get_bits(&gbc, 1);
2441 const int has_length_field = get_bits(&gbc, 1);
2442 get_bits(&gbc, 1);
2443 int temporal_id = 0, spatial_id = 0;
2444 if (has_extension) {
2445 temporal_id = get_bits(&gbc, 3);
2446 spatial_id = get_bits(&gbc, 2);
2447 get_bits(&gbc, 3);
2448 }
2449 int error = 0;
2450 const unsigned len = has_length_field ?
2451 get_uleb128(&gbc, &error) : qpkt.pkt.size - 1 - has_extension;
2452 skip_bits(&gbc, len * 8);
2453
2454 if (type == AV1_OBU_TEMPORAL_DELIMITER) {
2455 if (q->current_pkt.size > 0) {
2456 AVPacket tmp = { 0 };
2457 av_packet_move_ref(&tmp, &q->current_pkt);
2458 av_fifo_write(q->output_pkts, &tmp, 1);
2459 }
2460 av_fifo_read(q->input_pts, &q->current_pkt.pts, 1);
2461 q->current_pkt.dts = q->current_pkt.pts;
2462 if (qpkt.bs->FrameType & MFX_FRAMETYPE_IDR || qpkt.bs->FrameType & MFX_FRAMETYPE_xIDR)
2463 q->current_pkt.flags |= AV_PKT_FLAG_KEY;
2464 }
2465
2466 int start_offset = start_index >> 3;
2467 int append_bytes = (gbc.index - start_index) >> 3;
2468 av_grow_packet(&q->current_pkt, append_bytes);
2469 memcpy((uint8_t*)q->current_pkt.data + q->current_pkt.size - append_bytes, gbc.buffer + start_offset, append_bytes);
2470 }
2471 } else {
2472 qpkt.pkt.dts = av_rescale_q(qpkt.bs->DecodeTimeStamp, (AVRational) { 1, 90000 }, avctx->time_base);
2473 qpkt.pkt.pts = av_rescale_q(qpkt.bs->TimeStamp, (AVRational) { 1, 90000 }, avctx->time_base);
2474
2475 if (qpkt.bs->FrameType & MFX_FRAMETYPE_IDR || qpkt.bs->FrameType & MFX_FRAMETYPE_xIDR) {
2476 qpkt.pkt.flags |= AV_PKT_FLAG_KEY;
2477 pict_type = AV_PICTURE_TYPE_I;
2478 }
2479 else if (qpkt.bs->FrameType & MFX_FRAMETYPE_I || qpkt.bs->FrameType & MFX_FRAMETYPE_xI)
2480 pict_type = AV_PICTURE_TYPE_I;
2481 else if (qpkt.bs->FrameType & MFX_FRAMETYPE_P || qpkt.bs->FrameType & MFX_FRAMETYPE_xP)
2482 pict_type = AV_PICTURE_TYPE_P;
2483 else if (qpkt.bs->FrameType & MFX_FRAMETYPE_B || qpkt.bs->FrameType & MFX_FRAMETYPE_xB)
2484 pict_type = AV_PICTURE_TYPE_B;
2485 else if (qpkt.bs->FrameType == MFX_FRAMETYPE_UNKNOWN) {
2486 pict_type = AV_PICTURE_TYPE_NONE;
2487 av_log(avctx, AV_LOG_WARNING, "Unknown FrameType, set pict_type to AV_PICTURE_TYPE_NONE.\n");
2488 }
2489 else {
2490 av_log(avctx, AV_LOG_ERROR, "Invalid FrameType:%d.\n", qpkt.bs->FrameType);
2491 return AVERROR_INVALIDDATA;
2492 }
2493
2494 if (avctx->codec_id == AV_CODEC_ID_H264) {
2495 enc_buf = qpkt.bs->ExtParam;
2496 enc_info = (mfxExtAVCEncodedFrameInfo*)(*enc_buf);
2497 ff_side_data_set_encoder_stats(&qpkt.pkt,
2498 enc_info->QP * FF_QP2LAMBDA, NULL, 0, pict_type);
2499 av_freep(&enc_info);
2500 av_freep(&enc_buf);
2501 }
2502
2503 av_packet_move_ref(pkt, &qpkt.pkt);
2504
2505 *got_packet = 1;
2506 }
2507
2508 av_freep(&qpkt.bs);
2509 av_freep(&qpkt.sync);
2510 }
2511
2512 if (q->avctx->codec_id == AV_CODEC_ID_AV1) {
2513 if (av_fifo_can_read(q->output_pkts) > 0) {
2514 AVPacket tmp = { 0 };
2515 av_fifo_read(q->output_pkts, &tmp, 1);
2516 av_packet_move_ref(pkt, &tmp);
2517 *got_packet = 1;
2518 }
2519 else if (!frame && !av_fifo_can_read(q->async_fifo) && q->current_pkt.size > 0) {
2520 av_packet_move_ref(pkt, &q->current_pkt);
2521 *got_packet = 1;
2522 }
2523 }
2524
2525 return 0;
2526}
2527
2528int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
2529{
2530 QSVFrame *cur;
2531
2532 if (q->session)
2533 MFXVideoENCODE_Close(q->session);
2534
2535 q->session = NULL;
2536 ff_qsv_close_internal_session(&q->internal_qs);
2537
2538 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
2539 av_buffer_unref(&q->frames_ctx.mids_buf);
2540
2541 cur = q->work_frames;
2542 while (cur) {
2543 q->work_frames = cur->next;
2544 av_frame_free(&cur->frame);
2545 free_encoder_ctrl(&cur->enc_ctrl);
2546 av_freep(&cur);
2547 cur = q->work_frames;
2548 }
2549
2550 if (q->async_fifo) {
2551 QSVPacket pkt;
2552 while (av_fifo_read(q->async_fifo, &pkt, 1) >= 0) {
2553 if (avctx->codec_id == AV_CODEC_ID_H264) {
2554 mfxExtBuffer **enc_buf = pkt.bs->ExtParam;
2555 mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
2556 av_freep(&enc_info);
2557 av_freep(&enc_buf);
2558 }
2559 av_freep(&pkt.sync);
2560 av_freep(&pkt.bs);
2561 av_packet_unref(&pkt.pkt);
2562 }
2563 av_fifo_freep2(&q->async_fifo);
2564 }
2565
2566 if (avctx->codec_id == AV_CODEC_ID_AV1) {
2567 av_fifo_freep2(&q->input_pts);
2568 if (q->output_pkts) {
2569 AVPacket pkt;
2570 while (av_fifo_read(q->output_pkts, &pkt, 1) >= 0) {
2571 av_packet_unref(&pkt);
2572 }
2573 av_fifo_freep2(&q->output_pkts);
2574 }
2575 av_packet_unref(&q->current_pkt);
2576 }
2577
2578#if QSV_HAVE_OPAQUE
2579 av_freep(&q->opaque_surfaces);
2580 av_buffer_unref(&q->opaque_alloc_buf);
2581#endif
2582
2583 av_freep(&q->extparam);
2584
2585 return 0;
2586}
2587
2588const AVCodecHWConfigInternal *const ff_qsv_enc_hw_configs[] = {
2589 HW_CONFIG_ENCODER_FRAMES(QSV, QSV),
2590 HW_CONFIG_ENCODER_DEVICE(NV12, QSV),
2591 HW_CONFIG_ENCODER_DEVICE(P010, QSV),
2592 NULL,
2593};