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.

1118 lines
37KB

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