You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1040 lines
35KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <mfx/mfxvideo.h>
  26. #include "libavutil/common.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/log.h"
  29. #include "libavutil/time.h"
  30. #include "libavutil/imgutils.h"
  31. #include "avcodec.h"
  32. #include "internal.h"
  33. #include "qsv.h"
  34. #include "qsv_internal.h"
  35. #include "qsvenc.h"
  36. static const struct {
  37. mfxU16 profile;
  38. const char *name;
  39. } profile_names[] = {
  40. { MFX_PROFILE_AVC_BASELINE, "baseline" },
  41. { MFX_PROFILE_AVC_MAIN, "main" },
  42. { MFX_PROFILE_AVC_EXTENDED, "extended" },
  43. { MFX_PROFILE_AVC_HIGH, "high" },
  44. #if QSV_VERSION_ATLEAST(1, 15)
  45. { MFX_PROFILE_AVC_HIGH_422, "high 422" },
  46. #endif
  47. #if QSV_VERSION_ATLEAST(1, 4)
  48. { MFX_PROFILE_AVC_CONSTRAINED_BASELINE, "constrained baseline" },
  49. { MFX_PROFILE_AVC_CONSTRAINED_HIGH, "constrained high" },
  50. { MFX_PROFILE_AVC_PROGRESSIVE_HIGH, "progressive high" },
  51. #endif
  52. { MFX_PROFILE_MPEG2_SIMPLE, "simple" },
  53. { MFX_PROFILE_MPEG2_MAIN, "main" },
  54. { MFX_PROFILE_MPEG2_HIGH, "high" },
  55. { MFX_PROFILE_VC1_SIMPLE, "simple" },
  56. { MFX_PROFILE_VC1_MAIN, "main" },
  57. { MFX_PROFILE_VC1_ADVANCED, "advanced" },
  58. #if QSV_VERSION_ATLEAST(1, 8)
  59. { MFX_PROFILE_HEVC_MAIN, "main" },
  60. { MFX_PROFILE_HEVC_MAIN10, "main10" },
  61. { MFX_PROFILE_HEVC_MAINSP, "mainsp" },
  62. #endif
  63. };
  64. static const char *print_profile(mfxU16 profile)
  65. {
  66. int i;
  67. for (i = 0; i < FF_ARRAY_ELEMS(profile_names); i++)
  68. if (profile == profile_names[i].profile)
  69. return profile_names[i].name;
  70. return "unknown";
  71. }
  72. static const struct {
  73. mfxU16 rc_mode;
  74. const char *name;
  75. } rc_names[] = {
  76. { MFX_RATECONTROL_CBR, "CBR" },
  77. { MFX_RATECONTROL_VBR, "VBR" },
  78. { MFX_RATECONTROL_CQP, "CQP" },
  79. { MFX_RATECONTROL_AVBR, "AVBR" },
  80. #if QSV_HAVE_LA
  81. { MFX_RATECONTROL_LA, "LA" },
  82. #endif
  83. #if QSV_HAVE_ICQ
  84. { MFX_RATECONTROL_ICQ, "ICQ" },
  85. { MFX_RATECONTROL_LA_ICQ, "LA_ICQ" },
  86. #endif
  87. #if QSV_HAVE_VCM
  88. { MFX_RATECONTROL_VCM, "VCM" },
  89. #endif
  90. #if QSV_VERSION_ATLEAST(1, 10)
  91. { MFX_RATECONTROL_LA_EXT, "LA_EXT" },
  92. #endif
  93. #if QSV_HAVE_LA_HRD
  94. { MFX_RATECONTROL_LA_HRD, "LA_HRD" },
  95. #endif
  96. #if QSV_HAVE_QVBR
  97. { MFX_RATECONTROL_QVBR, "QVBR" },
  98. #endif
  99. };
  100. static const char *print_ratecontrol(mfxU16 rc_mode)
  101. {
  102. int i;
  103. for (i = 0; i < FF_ARRAY_ELEMS(rc_names); i++)
  104. if (rc_mode == rc_names[i].rc_mode)
  105. return rc_names[i].name;
  106. return "unknown";
  107. }
  108. static const char *print_threestate(mfxU16 val)
  109. {
  110. if (val == MFX_CODINGOPTION_ON)
  111. return "ON";
  112. else if (val == MFX_CODINGOPTION_OFF)
  113. return "OFF";
  114. return "unknown";
  115. }
  116. static void dump_video_param(AVCodecContext *avctx, QSVEncContext *q,
  117. mfxExtBuffer **coding_opts)
  118. {
  119. mfxInfoMFX *info = &q->param.mfx;
  120. mfxExtCodingOption *co = (mfxExtCodingOption*)coding_opts[0];
  121. #if QSV_HAVE_CO2
  122. mfxExtCodingOption2 *co2 = (mfxExtCodingOption2*)coding_opts[1];
  123. #endif
  124. #if QSV_HAVE_CO3
  125. mfxExtCodingOption3 *co3 = (mfxExtCodingOption3*)coding_opts[2];
  126. #endif
  127. av_log(avctx, AV_LOG_VERBOSE, "profile: %s; level: %"PRIu16"\n",
  128. print_profile(info->CodecProfile), info->CodecLevel);
  129. av_log(avctx, AV_LOG_VERBOSE, "GopPicSize: %"PRIu16"; GopRefDist: %"PRIu16"; GopOptFlag: ",
  130. info->GopPicSize, info->GopRefDist);
  131. if (info->GopOptFlag & MFX_GOP_CLOSED)
  132. av_log(avctx, AV_LOG_VERBOSE, "closed ");
  133. if (info->GopOptFlag & MFX_GOP_STRICT)
  134. av_log(avctx, AV_LOG_VERBOSE, "strict ");
  135. av_log(avctx, AV_LOG_VERBOSE, "; IdrInterval: %"PRIu16"\n", info->IdrInterval);
  136. av_log(avctx, AV_LOG_VERBOSE, "TargetUsage: %"PRIu16"; RateControlMethod: %s\n",
  137. info->TargetUsage, print_ratecontrol(info->RateControlMethod));
  138. if (info->RateControlMethod == MFX_RATECONTROL_CBR ||
  139. info->RateControlMethod == MFX_RATECONTROL_VBR
  140. #if QSV_HAVE_VCM
  141. || info->RateControlMethod == MFX_RATECONTROL_VCM
  142. #endif
  143. ) {
  144. av_log(avctx, AV_LOG_VERBOSE,
  145. "InitialDelayInKB: %"PRIu16"; TargetKbps: %"PRIu16"; MaxKbps: %"PRIu16"\n",
  146. info->InitialDelayInKB, info->TargetKbps, info->MaxKbps);
  147. } else if (info->RateControlMethod == MFX_RATECONTROL_CQP) {
  148. av_log(avctx, AV_LOG_VERBOSE, "QPI: %"PRIu16"; QPP: %"PRIu16"; QPB: %"PRIu16"\n",
  149. info->QPI, info->QPP, info->QPB);
  150. } else if (info->RateControlMethod == MFX_RATECONTROL_AVBR) {
  151. av_log(avctx, AV_LOG_VERBOSE,
  152. "TargetKbps: %"PRIu16"; Accuracy: %"PRIu16"; Convergence: %"PRIu16"\n",
  153. info->TargetKbps, info->Accuracy, info->Convergence);
  154. }
  155. #if QSV_HAVE_LA
  156. else if (info->RateControlMethod == MFX_RATECONTROL_LA
  157. #if QSV_HAVE_LA_HRD
  158. || info->RateControlMethod == MFX_RATECONTROL_LA_HRD
  159. #endif
  160. ) {
  161. av_log(avctx, AV_LOG_VERBOSE,
  162. "TargetKbps: %"PRIu16"; LookAheadDepth: %"PRIu16"\n",
  163. info->TargetKbps, co2->LookAheadDepth);
  164. }
  165. #endif
  166. #if QSV_HAVE_ICQ
  167. else if (info->RateControlMethod == MFX_RATECONTROL_ICQ) {
  168. av_log(avctx, AV_LOG_VERBOSE, "ICQQuality: %"PRIu16"\n", info->ICQQuality);
  169. } else if (info->RateControlMethod == MFX_RATECONTROL_LA_ICQ) {
  170. av_log(avctx, AV_LOG_VERBOSE, "ICQQuality: %"PRIu16"; LookAheadDepth: %"PRIu16"\n",
  171. info->ICQQuality, co2->LookAheadDepth);
  172. }
  173. #endif
  174. #if QSV_HAVE_QVBR
  175. else if (info->RateControlMethod == MFX_RATECONTROL_QVBR) {
  176. av_log(avctx, AV_LOG_VERBOSE, "QVBRQuality: %"PRIu16"\n",
  177. co3->QVBRQuality);
  178. }
  179. #endif
  180. av_log(avctx, AV_LOG_VERBOSE, "NumSlice: %"PRIu16"; NumRefFrame: %"PRIu16"\n",
  181. info->NumSlice, info->NumRefFrame);
  182. av_log(avctx, AV_LOG_VERBOSE, "RateDistortionOpt: %s\n",
  183. print_threestate(co->RateDistortionOpt));
  184. #if QSV_HAVE_CO2
  185. av_log(avctx, AV_LOG_VERBOSE,
  186. "RecoveryPointSEI: %s IntRefType: %"PRIu16"; IntRefCycleSize: %"PRIu16"; IntRefQPDelta: %"PRId16"\n",
  187. print_threestate(co->RecoveryPointSEI), co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
  188. av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSize: %"PRIu16"; ", co2->MaxFrameSize);
  189. #if QSV_HAVE_MAX_SLICE_SIZE
  190. av_log(avctx, AV_LOG_VERBOSE, "MaxSliceSize: %"PRIu16"; ", co2->MaxSliceSize);
  191. #endif
  192. av_log(avctx, AV_LOG_VERBOSE, "\n");
  193. av_log(avctx, AV_LOG_VERBOSE,
  194. "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
  195. print_threestate(co2->BitrateLimit), print_threestate(co2->MBBRC),
  196. print_threestate(co2->ExtBRC));
  197. #if QSV_HAVE_TRELLIS
  198. av_log(avctx, AV_LOG_VERBOSE, "Trellis: ");
  199. if (co2->Trellis & MFX_TRELLIS_OFF) {
  200. av_log(avctx, AV_LOG_VERBOSE, "off");
  201. } else if (!co2->Trellis) {
  202. av_log(avctx, AV_LOG_VERBOSE, "auto");
  203. } else {
  204. if (co2->Trellis & MFX_TRELLIS_I) av_log(avctx, AV_LOG_VERBOSE, "I");
  205. if (co2->Trellis & MFX_TRELLIS_P) av_log(avctx, AV_LOG_VERBOSE, "P");
  206. if (co2->Trellis & MFX_TRELLIS_B) av_log(avctx, AV_LOG_VERBOSE, "B");
  207. }
  208. av_log(avctx, AV_LOG_VERBOSE, "\n");
  209. #endif
  210. #if QSV_VERSION_ATLEAST(1, 8)
  211. av_log(avctx, AV_LOG_VERBOSE,
  212. "RepeatPPS: %s; NumMbPerSlice: %"PRIu16"; LookAheadDS: ",
  213. print_threestate(co2->RepeatPPS), co2->NumMbPerSlice);
  214. switch (co2->LookAheadDS) {
  215. case MFX_LOOKAHEAD_DS_OFF: av_log(avctx, AV_LOG_VERBOSE, "off"); break;
  216. case MFX_LOOKAHEAD_DS_2x: av_log(avctx, AV_LOG_VERBOSE, "2x"); break;
  217. case MFX_LOOKAHEAD_DS_4x: av_log(avctx, AV_LOG_VERBOSE, "4x"); break;
  218. default: av_log(avctx, AV_LOG_VERBOSE, "unknown"); break;
  219. }
  220. av_log(avctx, AV_LOG_VERBOSE, "\n");
  221. av_log(avctx, AV_LOG_VERBOSE, "AdaptiveI: %s; AdaptiveB: %s; BRefType: ",
  222. print_threestate(co2->AdaptiveI), print_threestate(co2->AdaptiveB));
  223. switch (co2->BRefType) {
  224. case MFX_B_REF_OFF: av_log(avctx, AV_LOG_VERBOSE, "off"); break;
  225. case MFX_B_REF_PYRAMID: av_log(avctx, AV_LOG_VERBOSE, "pyramid"); break;
  226. default: av_log(avctx, AV_LOG_VERBOSE, "auto"); break;
  227. }
  228. av_log(avctx, AV_LOG_VERBOSE, "\n");
  229. #endif
  230. #if QSV_VERSION_ATLEAST(1, 9)
  231. av_log(avctx, AV_LOG_VERBOSE,
  232. "MinQPI: %"PRIu8"; MaxQPI: %"PRIu8"; MinQPP: %"PRIu8"; MaxQPP: %"PRIu8"; MinQPB: %"PRIu8"; MaxQPB: %"PRIu8"\n",
  233. co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
  234. #endif
  235. #endif
  236. if (avctx->codec_id == AV_CODEC_ID_H264) {
  237. av_log(avctx, AV_LOG_VERBOSE, "Entropy coding: %s; MaxDecFrameBuffering: %"PRIu16"\n",
  238. co->CAVLC == MFX_CODINGOPTION_ON ? "CAVLC" : "CABAC", co->MaxDecFrameBuffering);
  239. av_log(avctx, AV_LOG_VERBOSE,
  240. "NalHrdConformance: %s; SingleSeiNalUnit: %s; VuiVclHrdParameters: %s VuiNalHrdParameters: %s\n",
  241. print_threestate(co->NalHrdConformance), print_threestate(co->SingleSeiNalUnit),
  242. print_threestate(co->VuiVclHrdParameters), print_threestate(co->VuiNalHrdParameters));
  243. }
  244. }
  245. static int select_rc_mode(AVCodecContext *avctx, QSVEncContext *q)
  246. {
  247. const char *rc_desc;
  248. mfxU16 rc_mode;
  249. int want_la = q->la_depth >= 0;
  250. int want_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
  251. int want_vcm = q->vcm;
  252. if (want_la && !QSV_HAVE_LA) {
  253. av_log(avctx, AV_LOG_ERROR,
  254. "Lookahead ratecontrol mode requested, but is not supported by this SDK version\n");
  255. return AVERROR(ENOSYS);
  256. }
  257. if (want_vcm && !QSV_HAVE_VCM) {
  258. av_log(avctx, AV_LOG_ERROR,
  259. "VCM ratecontrol mode requested, but is not supported by this SDK version\n");
  260. return AVERROR(ENOSYS);
  261. }
  262. if (want_la + want_qscale + want_vcm > 1) {
  263. av_log(avctx, AV_LOG_ERROR,
  264. "More than one of: { constant qscale, lookahead, VCM } requested, "
  265. "only one of them can be used at a time.\n");
  266. return AVERROR(EINVAL);
  267. }
  268. if (want_qscale) {
  269. rc_mode = MFX_RATECONTROL_CQP;
  270. rc_desc = "constant quantization parameter (CQP)";
  271. }
  272. #if QSV_HAVE_VCM
  273. else if (want_vcm) {
  274. rc_mode = MFX_RATECONTROL_VCM;
  275. rc_desc = "video conferencing mode (VCM)";
  276. }
  277. #endif
  278. #if QSV_HAVE_LA
  279. else if (want_la) {
  280. rc_mode = MFX_RATECONTROL_LA;
  281. rc_desc = "VBR with lookahead (LA)";
  282. #if QSV_HAVE_ICQ
  283. if (avctx->global_quality > 0) {
  284. rc_mode = MFX_RATECONTROL_LA_ICQ;
  285. rc_desc = "intelligent constant quality with lookahead (LA_ICQ)";
  286. }
  287. #endif
  288. }
  289. #endif
  290. #if QSV_HAVE_ICQ
  291. else if (avctx->global_quality > 0) {
  292. rc_mode = MFX_RATECONTROL_ICQ;
  293. rc_desc = "intelligent constant quality (ICQ)";
  294. }
  295. #endif
  296. else if (avctx->rc_max_rate == avctx->bit_rate) {
  297. rc_mode = MFX_RATECONTROL_CBR;
  298. rc_desc = "constant bitrate (CBR)";
  299. } else if (!avctx->rc_max_rate) {
  300. rc_mode = MFX_RATECONTROL_AVBR;
  301. rc_desc = "average variable bitrate (AVBR)";
  302. } else {
  303. rc_mode = MFX_RATECONTROL_VBR;
  304. rc_desc = "variable bitrate (VBR)";
  305. }
  306. q->param.mfx.RateControlMethod = rc_mode;
  307. av_log(avctx, AV_LOG_VERBOSE, "Using the %s ratecontrol method\n", rc_desc);
  308. return 0;
  309. }
  310. static int rc_supported(QSVEncContext *q)
  311. {
  312. mfxVideoParam param_out = { .mfx.CodecId = q->param.mfx.CodecId };
  313. mfxStatus ret;
  314. ret = MFXVideoENCODE_Query(q->session, &q->param, &param_out);
  315. if (ret < 0 ||
  316. param_out.mfx.RateControlMethod != q->param.mfx.RateControlMethod)
  317. return 0;
  318. return 1;
  319. }
  320. static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
  321. {
  322. float quant;
  323. int ret;
  324. ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
  325. if (ret < 0)
  326. return AVERROR_BUG;
  327. q->param.mfx.CodecId = ret;
  328. q->width_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16;
  329. if (avctx->level > 0)
  330. q->param.mfx.CodecLevel = avctx->level;
  331. q->param.mfx.CodecProfile = q->profile;
  332. q->param.mfx.TargetUsage = q->preset;
  333. q->param.mfx.GopPicSize = FFMAX(0, avctx->gop_size);
  334. q->param.mfx.GopRefDist = FFMAX(-1, avctx->max_b_frames) + 1;
  335. q->param.mfx.GopOptFlag = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ?
  336. MFX_GOP_CLOSED : 0;
  337. q->param.mfx.IdrInterval = q->idr_interval;
  338. q->param.mfx.NumSlice = avctx->slices;
  339. q->param.mfx.NumRefFrame = FFMAX(0, avctx->refs);
  340. q->param.mfx.EncodedOrder = 0;
  341. q->param.mfx.BufferSizeInKB = 0;
  342. q->param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
  343. q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align);
  344. q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, 32);
  345. q->param.mfx.FrameInfo.CropX = 0;
  346. q->param.mfx.FrameInfo.CropY = 0;
  347. q->param.mfx.FrameInfo.CropW = avctx->width;
  348. q->param.mfx.FrameInfo.CropH = avctx->height;
  349. q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num;
  350. q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den;
  351. q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  352. q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  353. q->param.mfx.FrameInfo.BitDepthLuma = 8;
  354. q->param.mfx.FrameInfo.BitDepthChroma = 8;
  355. if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
  356. q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
  357. q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
  358. } else {
  359. q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
  360. q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
  361. }
  362. ret = select_rc_mode(avctx, q);
  363. if (ret < 0)
  364. return ret;
  365. switch (q->param.mfx.RateControlMethod) {
  366. case MFX_RATECONTROL_CBR:
  367. case MFX_RATECONTROL_VBR:
  368. #if QSV_HAVE_VCM
  369. case MFX_RATECONTROL_VCM:
  370. #endif
  371. q->param.mfx.InitialDelayInKB = avctx->rc_initial_buffer_occupancy / 1000;
  372. q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
  373. q->param.mfx.MaxKbps = avctx->rc_max_rate / 1000;
  374. break;
  375. case MFX_RATECONTROL_CQP:
  376. quant = avctx->global_quality / FF_QP2LAMBDA;
  377. q->param.mfx.QPI = av_clip(quant * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
  378. q->param.mfx.QPP = av_clip(quant, 0, 51);
  379. q->param.mfx.QPB = av_clip(quant * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51);
  380. break;
  381. case MFX_RATECONTROL_AVBR:
  382. q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
  383. q->param.mfx.Convergence = q->avbr_convergence;
  384. q->param.mfx.Accuracy = q->avbr_accuracy;
  385. break;
  386. #if QSV_HAVE_LA
  387. case MFX_RATECONTROL_LA:
  388. q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
  389. q->extco2.LookAheadDepth = q->la_depth;
  390. break;
  391. #if QSV_HAVE_ICQ
  392. case MFX_RATECONTROL_LA_ICQ:
  393. q->extco2.LookAheadDepth = q->la_depth;
  394. case MFX_RATECONTROL_ICQ:
  395. q->param.mfx.ICQQuality = avctx->global_quality;
  396. break;
  397. #endif
  398. #endif
  399. }
  400. // the HEVC encoder plugin currently fails if coding options
  401. // are provided
  402. if (avctx->codec_id != AV_CODEC_ID_HEVC) {
  403. q->extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
  404. q->extco.Header.BufferSz = sizeof(q->extco);
  405. #if FF_API_CODER_TYPE
  406. FF_DISABLE_DEPRECATION_WARNINGS
  407. if (avctx->coder_type != 0)
  408. q->cavlc = avctx->coder_type == FF_CODER_TYPE_VLC;
  409. FF_ENABLE_DEPRECATION_WARNINGS
  410. #endif
  411. q->extco.CAVLC = q->cavlc ? MFX_CODINGOPTION_ON
  412. : MFX_CODINGOPTION_UNKNOWN;
  413. if (q->rdo >= 0)
  414. q->extco.RateDistortionOpt = q->rdo > 0 ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  415. if (avctx->codec_id == AV_CODEC_ID_H264) {
  416. if (avctx->strict_std_compliance != FF_COMPLIANCE_NORMAL)
  417. q->extco.NalHrdConformance = avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL ?
  418. MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  419. if (q->single_sei_nal_unit >= 0)
  420. q->extco.SingleSeiNalUnit = q->single_sei_nal_unit ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  421. if (q->recovery_point_sei >= 0)
  422. q->extco.RecoveryPointSEI = q->recovery_point_sei ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  423. q->extco.MaxDecFrameBuffering = q->max_dec_frame_buffering;
  424. }
  425. q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco;
  426. #if QSV_HAVE_CO2
  427. if (avctx->codec_id == AV_CODEC_ID_H264) {
  428. q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
  429. q->extco2.Header.BufferSz = sizeof(q->extco2);
  430. if (q->int_ref_type >= 0)
  431. q->extco2.IntRefType = q->int_ref_type;
  432. if (q->int_ref_cycle_size >= 0)
  433. q->extco2.IntRefCycleSize = q->int_ref_cycle_size;
  434. if (q->int_ref_qp_delta != INT16_MIN)
  435. q->extco2.IntRefQPDelta = q->int_ref_qp_delta;
  436. if (q->bitrate_limit >= 0)
  437. q->extco2.BitrateLimit = q->bitrate_limit ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  438. if (q->mbbrc >= 0)
  439. q->extco2.MBBRC = q->mbbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  440. if (q->extbrc >= 0)
  441. q->extco2.ExtBRC = q->extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  442. if (q->max_frame_size >= 0)
  443. q->extco2.MaxFrameSize = q->max_frame_size;
  444. #if QSV_HAVE_MAX_SLICE_SIZE
  445. if (q->max_slice_size >= 0)
  446. q->extco2.MaxSliceSize = q->max_slice_size;
  447. #endif
  448. #if QSV_HAVE_TRELLIS
  449. q->extco2.Trellis = q->trellis;
  450. #endif
  451. #if QSV_HAVE_BREF_TYPE
  452. #if FF_API_PRIVATE_OPT
  453. FF_DISABLE_DEPRECATION_WARNINGS
  454. if (avctx->b_frame_strategy >= 0)
  455. q->b_strategy = avctx->b_frame_strategy;
  456. FF_ENABLE_DEPRECATION_WARNINGS
  457. #endif
  458. if (q->b_strategy >= 0)
  459. q->extco2.BRefType = q->b_strategy ? MFX_B_REF_PYRAMID : MFX_B_REF_OFF;
  460. if (q->adaptive_i >= 0)
  461. q->extco2.AdaptiveI = q->adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  462. if (q->adaptive_b >= 0)
  463. q->extco2.AdaptiveB = q->adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  464. #endif
  465. q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco2;
  466. }
  467. #endif
  468. }
  469. if (!rc_supported(q)) {
  470. av_log(avctx, AV_LOG_ERROR,
  471. "Selected ratecontrol mode is not supported by the QSV "
  472. "runtime. Choose a different mode.\n");
  473. return AVERROR(ENOSYS);
  474. }
  475. return 0;
  476. }
  477. static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q)
  478. {
  479. AVCPBProperties *cpb_props;
  480. uint8_t sps_buf[128];
  481. uint8_t pps_buf[128];
  482. mfxExtCodingOptionSPSPPS extradata = {
  483. .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
  484. .Header.BufferSz = sizeof(extradata),
  485. .SPSBuffer = sps_buf, .SPSBufSize = sizeof(sps_buf),
  486. .PPSBuffer = pps_buf, .PPSBufSize = sizeof(pps_buf)
  487. };
  488. mfxExtCodingOption co = {
  489. .Header.BufferId = MFX_EXTBUFF_CODING_OPTION,
  490. .Header.BufferSz = sizeof(co),
  491. };
  492. #if QSV_HAVE_CO2
  493. mfxExtCodingOption2 co2 = {
  494. .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
  495. .Header.BufferSz = sizeof(co2),
  496. };
  497. #endif
  498. #if QSV_HAVE_CO3
  499. mfxExtCodingOption3 co3 = {
  500. .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
  501. .Header.BufferSz = sizeof(co3),
  502. };
  503. #endif
  504. mfxExtBuffer *ext_buffers[] = {
  505. (mfxExtBuffer*)&extradata,
  506. (mfxExtBuffer*)&co,
  507. #if QSV_HAVE_CO2
  508. (mfxExtBuffer*)&co2,
  509. #endif
  510. #if QSV_HAVE_CO3
  511. (mfxExtBuffer*)&co3,
  512. #endif
  513. };
  514. int need_pps = avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO;
  515. int ret;
  516. q->param.ExtParam = ext_buffers;
  517. q->param.NumExtParam = FF_ARRAY_ELEMS(ext_buffers);
  518. ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
  519. if (ret < 0)
  520. return ff_qsv_error(ret);
  521. q->packet_size = q->param.mfx.BufferSizeInKB * 1000;
  522. if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)) {
  523. av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n");
  524. return AVERROR_UNKNOWN;
  525. }
  526. avctx->extradata = av_malloc(extradata.SPSBufSize + need_pps * extradata.PPSBufSize +
  527. AV_INPUT_BUFFER_PADDING_SIZE);
  528. if (!avctx->extradata)
  529. return AVERROR(ENOMEM);
  530. memcpy(avctx->extradata, sps_buf, extradata.SPSBufSize);
  531. if (need_pps)
  532. memcpy(avctx->extradata + extradata.SPSBufSize, pps_buf, extradata.PPSBufSize);
  533. avctx->extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
  534. memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  535. cpb_props = ff_add_cpb_side_data(avctx);
  536. if (!cpb_props)
  537. return AVERROR(ENOMEM);
  538. cpb_props->max_bitrate = avctx->rc_max_rate;
  539. cpb_props->min_bitrate = avctx->rc_min_rate;
  540. cpb_props->avg_bitrate = avctx->bit_rate;
  541. cpb_props->buffer_size = avctx->rc_buffer_size;
  542. dump_video_param(avctx, q, ext_buffers + 1);
  543. return 0;
  544. }
  545. static int qsv_init_opaque_alloc(AVCodecContext *avctx, QSVEncContext *q)
  546. {
  547. AVQSVContext *qsv = avctx->hwaccel_context;
  548. mfxFrameSurface1 *surfaces;
  549. int nb_surfaces, i;
  550. nb_surfaces = qsv->nb_opaque_surfaces + q->req.NumFrameSuggested + q->async_depth;
  551. q->opaque_alloc_buf = av_buffer_allocz(sizeof(*surfaces) * nb_surfaces);
  552. if (!q->opaque_alloc_buf)
  553. return AVERROR(ENOMEM);
  554. q->opaque_surfaces = av_malloc_array(nb_surfaces, sizeof(*q->opaque_surfaces));
  555. if (!q->opaque_surfaces)
  556. return AVERROR(ENOMEM);
  557. surfaces = (mfxFrameSurface1*)q->opaque_alloc_buf->data;
  558. for (i = 0; i < nb_surfaces; i++) {
  559. surfaces[i].Info = q->req.Info;
  560. q->opaque_surfaces[i] = surfaces + i;
  561. }
  562. q->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
  563. q->opaque_alloc.Header.BufferSz = sizeof(q->opaque_alloc);
  564. q->opaque_alloc.In.Surfaces = q->opaque_surfaces;
  565. q->opaque_alloc.In.NumSurface = nb_surfaces;
  566. q->opaque_alloc.In.Type = q->req.Type;
  567. q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->opaque_alloc;
  568. qsv->nb_opaque_surfaces = nb_surfaces;
  569. qsv->opaque_surfaces = q->opaque_alloc_buf;
  570. qsv->opaque_alloc_type = q->req.Type;
  571. return 0;
  572. }
  573. int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
  574. {
  575. int opaque_alloc = 0;
  576. int ret;
  577. q->param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
  578. q->param.AsyncDepth = q->async_depth;
  579. q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
  580. (sizeof(AVPacket) + sizeof(mfxSyncPoint) + sizeof(mfxBitstream*)));
  581. if (!q->async_fifo)
  582. return AVERROR(ENOMEM);
  583. if (avctx->hwaccel_context) {
  584. AVQSVContext *qsv = avctx->hwaccel_context;
  585. q->session = qsv->session;
  586. q->param.IOPattern = qsv->iopattern;
  587. opaque_alloc = qsv->opaque_alloc;
  588. }
  589. if (!q->session) {
  590. ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
  591. q->load_plugins);
  592. if (ret < 0)
  593. return ret;
  594. q->session = q->internal_session;
  595. }
  596. ret = init_video_param(avctx, q);
  597. if (ret < 0)
  598. return ret;
  599. ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req);
  600. if (ret < 0) {
  601. av_log(avctx, AV_LOG_ERROR, "Error querying the encoding parameters\n");
  602. return ff_qsv_error(ret);
  603. }
  604. if (opaque_alloc) {
  605. ret = qsv_init_opaque_alloc(avctx, q);
  606. if (ret < 0)
  607. return ret;
  608. }
  609. if (avctx->hwaccel_context) {
  610. AVQSVContext *qsv = avctx->hwaccel_context;
  611. int i, j;
  612. q->extparam = av_mallocz_array(qsv->nb_ext_buffers + q->nb_extparam_internal,
  613. sizeof(*q->extparam));
  614. if (!q->extparam)
  615. return AVERROR(ENOMEM);
  616. q->param.ExtParam = q->extparam;
  617. for (i = 0; i < qsv->nb_ext_buffers; i++)
  618. q->param.ExtParam[i] = qsv->ext_buffers[i];
  619. q->param.NumExtParam = qsv->nb_ext_buffers;
  620. for (i = 0; i < q->nb_extparam_internal; i++) {
  621. for (j = 0; j < qsv->nb_ext_buffers; j++) {
  622. if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId)
  623. break;
  624. }
  625. if (j < qsv->nb_ext_buffers)
  626. continue;
  627. q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i];
  628. }
  629. } else {
  630. q->param.ExtParam = q->extparam_internal;
  631. q->param.NumExtParam = q->nb_extparam_internal;
  632. }
  633. ret = MFXVideoENCODE_Init(q->session, &q->param);
  634. if (ret < 0) {
  635. av_log(avctx, AV_LOG_ERROR, "Error initializing the encoder\n");
  636. return ff_qsv_error(ret);
  637. }
  638. ret = qsv_retrieve_enc_params(avctx, q);
  639. if (ret < 0) {
  640. av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n");
  641. return ret;
  642. }
  643. q->avctx = avctx;
  644. return 0;
  645. }
  646. static void clear_unused_frames(QSVEncContext *q)
  647. {
  648. QSVFrame *cur = q->work_frames;
  649. while (cur) {
  650. if (cur->surface && !cur->surface->Data.Locked) {
  651. cur->surface = NULL;
  652. av_frame_unref(cur->frame);
  653. }
  654. cur = cur->next;
  655. }
  656. }
  657. static int get_free_frame(QSVEncContext *q, QSVFrame **f)
  658. {
  659. QSVFrame *frame, **last;
  660. clear_unused_frames(q);
  661. frame = q->work_frames;
  662. last = &q->work_frames;
  663. while (frame) {
  664. if (!frame->surface) {
  665. *f = frame;
  666. return 0;
  667. }
  668. last = &frame->next;
  669. frame = frame->next;
  670. }
  671. frame = av_mallocz(sizeof(*frame));
  672. if (!frame)
  673. return AVERROR(ENOMEM);
  674. frame->frame = av_frame_alloc();
  675. if (!frame->frame) {
  676. av_freep(&frame);
  677. return AVERROR(ENOMEM);
  678. }
  679. *last = frame;
  680. *f = frame;
  681. return 0;
  682. }
  683. static int submit_frame(QSVEncContext *q, const AVFrame *frame,
  684. mfxFrameSurface1 **surface)
  685. {
  686. QSVFrame *qf;
  687. int ret;
  688. ret = get_free_frame(q, &qf);
  689. if (ret < 0)
  690. return ret;
  691. if (frame->format == AV_PIX_FMT_QSV) {
  692. ret = av_frame_ref(qf->frame, frame);
  693. if (ret < 0)
  694. return ret;
  695. qf->surface = (mfxFrameSurface1*)qf->frame->data[3];
  696. } else {
  697. /* make a copy if the input is not padded as libmfx requires */
  698. if (frame->height & 31 || frame->linesize[0] & (q->width_align - 1)) {
  699. qf->frame->height = FFALIGN(frame->height, 32);
  700. qf->frame->width = FFALIGN(frame->width, q->width_align);
  701. ret = ff_get_buffer(q->avctx, qf->frame, AV_GET_BUFFER_FLAG_REF);
  702. if (ret < 0)
  703. return ret;
  704. qf->frame->height = frame->height;
  705. qf->frame->width = frame->width;
  706. ret = av_frame_copy(qf->frame, frame);
  707. if (ret < 0) {
  708. av_frame_unref(qf->frame);
  709. return ret;
  710. }
  711. } else {
  712. ret = av_frame_ref(qf->frame, frame);
  713. if (ret < 0)
  714. return ret;
  715. }
  716. qf->surface_internal.Info = q->param.mfx.FrameInfo;
  717. qf->surface_internal.Info.PicStruct =
  718. !frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
  719. frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
  720. MFX_PICSTRUCT_FIELD_BFF;
  721. if (frame->repeat_pict == 1)
  722. qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
  723. else if (frame->repeat_pict == 2)
  724. qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
  725. else if (frame->repeat_pict == 4)
  726. qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
  727. qf->surface_internal.Data.PitchLow = qf->frame->linesize[0];
  728. qf->surface_internal.Data.Y = qf->frame->data[0];
  729. qf->surface_internal.Data.UV = qf->frame->data[1];
  730. qf->surface = &qf->surface_internal;
  731. }
  732. qf->surface->Data.TimeStamp = av_rescale_q(frame->pts, q->avctx->time_base, (AVRational){1, 90000});
  733. *surface = qf->surface;
  734. return 0;
  735. }
  736. static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
  737. {
  738. if (q->param.mfx.CodecId == MFX_CODEC_AVC) {
  739. if (q->param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
  740. q->param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
  741. q->param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
  742. av_log(avctx, AV_LOG_WARNING,
  743. "Interlaced coding is supported"
  744. " at Main/High Profile Level 2.1-4.1\n");
  745. }
  746. }
  747. static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
  748. const AVFrame *frame)
  749. {
  750. AVPacket new_pkt = { 0 };
  751. mfxBitstream *bs;
  752. mfxFrameSurface1 *surf = NULL;
  753. mfxSyncPoint sync = NULL;
  754. int ret;
  755. if (frame) {
  756. ret = submit_frame(q, frame, &surf);
  757. if (ret < 0) {
  758. av_log(avctx, AV_LOG_ERROR, "Error submitting the frame for encoding.\n");
  759. return ret;
  760. }
  761. }
  762. ret = av_new_packet(&new_pkt, q->packet_size);
  763. if (ret < 0) {
  764. av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
  765. return ret;
  766. }
  767. bs = av_mallocz(sizeof(*bs));
  768. if (!bs) {
  769. av_packet_unref(&new_pkt);
  770. return AVERROR(ENOMEM);
  771. }
  772. bs->Data = new_pkt.data;
  773. bs->MaxLength = new_pkt.size;
  774. do {
  775. ret = MFXVideoENCODE_EncodeFrameAsync(q->session, NULL, surf, bs, &sync);
  776. if (ret == MFX_WRN_DEVICE_BUSY)
  777. av_usleep(1);
  778. } while (ret > 0);
  779. if (ret < 0) {
  780. av_packet_unref(&new_pkt);
  781. av_freep(&bs);
  782. return (ret == MFX_ERR_MORE_DATA) ? 0 : ff_qsv_error(ret);
  783. }
  784. if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM && frame->interlaced_frame)
  785. print_interlace_msg(avctx, q);
  786. if (sync) {
  787. av_fifo_generic_write(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
  788. av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
  789. av_fifo_generic_write(q->async_fifo, &bs, sizeof(bs), NULL);
  790. } else {
  791. av_packet_unref(&new_pkt);
  792. av_freep(&bs);
  793. }
  794. return 0;
  795. }
  796. int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
  797. AVPacket *pkt, const AVFrame *frame, int *got_packet)
  798. {
  799. int ret;
  800. ret = encode_frame(avctx, q, frame);
  801. if (ret < 0)
  802. return ret;
  803. if (!av_fifo_space(q->async_fifo) ||
  804. (!frame && av_fifo_size(q->async_fifo))) {
  805. AVPacket new_pkt;
  806. mfxBitstream *bs;
  807. mfxSyncPoint sync;
  808. av_fifo_generic_read(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
  809. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  810. av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
  811. do {
  812. ret = MFXVideoCORE_SyncOperation(q->session, sync, 1000);
  813. } while (ret == MFX_WRN_IN_EXECUTION);
  814. new_pkt.dts = av_rescale_q(bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
  815. new_pkt.pts = av_rescale_q(bs->TimeStamp, (AVRational){1, 90000}, avctx->time_base);
  816. new_pkt.size = bs->DataLength;
  817. if (bs->FrameType & MFX_FRAMETYPE_IDR ||
  818. bs->FrameType & MFX_FRAMETYPE_xIDR)
  819. new_pkt.flags |= AV_PKT_FLAG_KEY;
  820. #if FF_API_CODED_FRAME
  821. FF_DISABLE_DEPRECATION_WARNINGS
  822. if (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI)
  823. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  824. else if (bs->FrameType & MFX_FRAMETYPE_P || bs->FrameType & MFX_FRAMETYPE_xP)
  825. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
  826. else if (bs->FrameType & MFX_FRAMETYPE_B || bs->FrameType & MFX_FRAMETYPE_xB)
  827. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
  828. FF_ENABLE_DEPRECATION_WARNINGS
  829. #endif
  830. av_freep(&bs);
  831. if (pkt->data) {
  832. if (pkt->size < new_pkt.size) {
  833. av_log(avctx, AV_LOG_ERROR, "Submitted buffer not large enough: %d < %d\n",
  834. pkt->size, new_pkt.size);
  835. av_packet_unref(&new_pkt);
  836. return AVERROR(EINVAL);
  837. }
  838. memcpy(pkt->data, new_pkt.data, new_pkt.size);
  839. pkt->size = new_pkt.size;
  840. ret = av_packet_copy_props(pkt, &new_pkt);
  841. av_packet_unref(&new_pkt);
  842. if (ret < 0)
  843. return ret;
  844. } else
  845. *pkt = new_pkt;
  846. *got_packet = 1;
  847. }
  848. return 0;
  849. }
  850. int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
  851. {
  852. QSVFrame *cur;
  853. if (q->session)
  854. MFXVideoENCODE_Close(q->session);
  855. if (q->internal_session)
  856. MFXClose(q->internal_session);
  857. q->session = NULL;
  858. q->internal_session = NULL;
  859. cur = q->work_frames;
  860. while (cur) {
  861. q->work_frames = cur->next;
  862. av_frame_free(&cur->frame);
  863. av_freep(&cur);
  864. cur = q->work_frames;
  865. }
  866. while (q->async_fifo && av_fifo_size(q->async_fifo)) {
  867. AVPacket pkt;
  868. mfxSyncPoint sync;
  869. mfxBitstream *bs;
  870. av_fifo_generic_read(q->async_fifo, &pkt, sizeof(pkt), NULL);
  871. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  872. av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
  873. av_freep(&bs);
  874. av_packet_unref(&pkt);
  875. }
  876. av_fifo_free(q->async_fifo);
  877. q->async_fifo = NULL;
  878. av_freep(&q->opaque_surfaces);
  879. av_buffer_unref(&q->opaque_alloc_buf);
  880. av_freep(&q->extparam);
  881. return 0;
  882. }