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.

1451 lines
50KB

  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/hwcontext.h"
  28. #include "libavutil/hwcontext_qsv.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/log.h"
  31. #include "libavutil/time.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavcodec/bytestream.h"
  34. #include "avcodec.h"
  35. #include "internal.h"
  36. #include "qsv.h"
  37. #include "qsv_internal.h"
  38. #include "qsvenc.h"
  39. static const struct {
  40. mfxU16 profile;
  41. const char *name;
  42. } profile_names[] = {
  43. { MFX_PROFILE_AVC_BASELINE, "baseline" },
  44. { MFX_PROFILE_AVC_MAIN, "main" },
  45. { MFX_PROFILE_AVC_EXTENDED, "extended" },
  46. { MFX_PROFILE_AVC_HIGH, "high" },
  47. #if QSV_VERSION_ATLEAST(1, 15)
  48. { MFX_PROFILE_AVC_HIGH_422, "high 422" },
  49. #endif
  50. #if QSV_VERSION_ATLEAST(1, 4)
  51. { MFX_PROFILE_AVC_CONSTRAINED_BASELINE, "constrained baseline" },
  52. { MFX_PROFILE_AVC_CONSTRAINED_HIGH, "constrained high" },
  53. { MFX_PROFILE_AVC_PROGRESSIVE_HIGH, "progressive high" },
  54. #endif
  55. { MFX_PROFILE_MPEG2_SIMPLE, "simple" },
  56. { MFX_PROFILE_MPEG2_MAIN, "main" },
  57. { MFX_PROFILE_MPEG2_HIGH, "high" },
  58. { MFX_PROFILE_VC1_SIMPLE, "simple" },
  59. { MFX_PROFILE_VC1_MAIN, "main" },
  60. { MFX_PROFILE_VC1_ADVANCED, "advanced" },
  61. #if QSV_VERSION_ATLEAST(1, 8)
  62. { MFX_PROFILE_HEVC_MAIN, "main" },
  63. { MFX_PROFILE_HEVC_MAIN10, "main10" },
  64. { MFX_PROFILE_HEVC_MAINSP, "mainsp" },
  65. #endif
  66. };
  67. static const char *print_profile(mfxU16 profile)
  68. {
  69. int i;
  70. for (i = 0; i < FF_ARRAY_ELEMS(profile_names); i++)
  71. if (profile == profile_names[i].profile)
  72. return profile_names[i].name;
  73. return "unknown";
  74. }
  75. static const struct {
  76. mfxU16 rc_mode;
  77. const char *name;
  78. } rc_names[] = {
  79. { MFX_RATECONTROL_CBR, "CBR" },
  80. { MFX_RATECONTROL_VBR, "VBR" },
  81. { MFX_RATECONTROL_CQP, "CQP" },
  82. #if QSV_HAVE_AVBR
  83. { MFX_RATECONTROL_AVBR, "AVBR" },
  84. #endif
  85. #if QSV_HAVE_LA
  86. { MFX_RATECONTROL_LA, "LA" },
  87. #endif
  88. #if QSV_HAVE_ICQ
  89. { MFX_RATECONTROL_ICQ, "ICQ" },
  90. { MFX_RATECONTROL_LA_ICQ, "LA_ICQ" },
  91. #endif
  92. #if QSV_HAVE_VCM
  93. { MFX_RATECONTROL_VCM, "VCM" },
  94. #endif
  95. #if QSV_VERSION_ATLEAST(1, 10)
  96. { MFX_RATECONTROL_LA_EXT, "LA_EXT" },
  97. #endif
  98. #if QSV_HAVE_LA_HRD
  99. { MFX_RATECONTROL_LA_HRD, "LA_HRD" },
  100. #endif
  101. #if QSV_HAVE_QVBR
  102. { MFX_RATECONTROL_QVBR, "QVBR" },
  103. #endif
  104. };
  105. static const char *print_ratecontrol(mfxU16 rc_mode)
  106. {
  107. int i;
  108. for (i = 0; i < FF_ARRAY_ELEMS(rc_names); i++)
  109. if (rc_mode == rc_names[i].rc_mode)
  110. return rc_names[i].name;
  111. return "unknown";
  112. }
  113. static const char *print_threestate(mfxU16 val)
  114. {
  115. if (val == MFX_CODINGOPTION_ON)
  116. return "ON";
  117. else if (val == MFX_CODINGOPTION_OFF)
  118. return "OFF";
  119. return "unknown";
  120. }
  121. static void dump_video_param(AVCodecContext *avctx, QSVEncContext *q,
  122. mfxExtBuffer **coding_opts)
  123. {
  124. mfxInfoMFX *info = &q->param.mfx;
  125. mfxExtCodingOption *co = (mfxExtCodingOption*)coding_opts[0];
  126. #if QSV_HAVE_CO2
  127. mfxExtCodingOption2 *co2 = (mfxExtCodingOption2*)coding_opts[1];
  128. #endif
  129. av_log(avctx, AV_LOG_VERBOSE, "profile: %s; level: %"PRIu16"\n",
  130. print_profile(info->CodecProfile), info->CodecLevel);
  131. av_log(avctx, AV_LOG_VERBOSE, "GopPicSize: %"PRIu16"; GopRefDist: %"PRIu16"; GopOptFlag: ",
  132. info->GopPicSize, info->GopRefDist);
  133. if (info->GopOptFlag & MFX_GOP_CLOSED)
  134. av_log(avctx, AV_LOG_VERBOSE, "closed ");
  135. if (info->GopOptFlag & MFX_GOP_STRICT)
  136. av_log(avctx, AV_LOG_VERBOSE, "strict ");
  137. av_log(avctx, AV_LOG_VERBOSE, "; IdrInterval: %"PRIu16"\n", info->IdrInterval);
  138. av_log(avctx, AV_LOG_VERBOSE, "TargetUsage: %"PRIu16"; RateControlMethod: %s\n",
  139. info->TargetUsage, print_ratecontrol(info->RateControlMethod));
  140. if (info->RateControlMethod == MFX_RATECONTROL_CBR ||
  141. info->RateControlMethod == MFX_RATECONTROL_VBR
  142. #if QSV_HAVE_VCM
  143. || info->RateControlMethod == MFX_RATECONTROL_VCM
  144. #endif
  145. ) {
  146. av_log(avctx, AV_LOG_VERBOSE,
  147. "BufferSizeInKB: %"PRIu16"; InitialDelayInKB: %"PRIu16"; TargetKbps: %"PRIu16"; MaxKbps: %"PRIu16"\n",
  148. info->BufferSizeInKB, info->InitialDelayInKB, info->TargetKbps, info->MaxKbps);
  149. } else if (info->RateControlMethod == MFX_RATECONTROL_CQP) {
  150. av_log(avctx, AV_LOG_VERBOSE, "QPI: %"PRIu16"; QPP: %"PRIu16"; QPB: %"PRIu16"\n",
  151. info->QPI, info->QPP, info->QPB);
  152. }
  153. #if QSV_HAVE_AVBR
  154. else if (info->RateControlMethod == MFX_RATECONTROL_AVBR) {
  155. av_log(avctx, AV_LOG_VERBOSE,
  156. "TargetKbps: %"PRIu16"; Accuracy: %"PRIu16"; Convergence: %"PRIu16"\n",
  157. info->TargetKbps, info->Accuracy, info->Convergence);
  158. }
  159. #endif
  160. #if QSV_HAVE_LA
  161. else if (info->RateControlMethod == MFX_RATECONTROL_LA
  162. #if QSV_HAVE_LA_HRD
  163. || info->RateControlMethod == MFX_RATECONTROL_LA_HRD
  164. #endif
  165. ) {
  166. av_log(avctx, AV_LOG_VERBOSE,
  167. "TargetKbps: %"PRIu16"; LookAheadDepth: %"PRIu16"\n",
  168. info->TargetKbps, co2->LookAheadDepth);
  169. }
  170. #endif
  171. #if QSV_HAVE_ICQ
  172. else if (info->RateControlMethod == MFX_RATECONTROL_ICQ) {
  173. av_log(avctx, AV_LOG_VERBOSE, "ICQQuality: %"PRIu16"\n", info->ICQQuality);
  174. } else if (info->RateControlMethod == MFX_RATECONTROL_LA_ICQ) {
  175. av_log(avctx, AV_LOG_VERBOSE, "ICQQuality: %"PRIu16"; LookAheadDepth: %"PRIu16"\n",
  176. info->ICQQuality, co2->LookAheadDepth);
  177. }
  178. #endif
  179. av_log(avctx, AV_LOG_VERBOSE, "NumSlice: %"PRIu16"; NumRefFrame: %"PRIu16"\n",
  180. info->NumSlice, info->NumRefFrame);
  181. av_log(avctx, AV_LOG_VERBOSE, "RateDistortionOpt: %s\n",
  182. print_threestate(co->RateDistortionOpt));
  183. #if QSV_HAVE_CO2
  184. av_log(avctx, AV_LOG_VERBOSE,
  185. "RecoveryPointSEI: %s IntRefType: %"PRIu16"; IntRefCycleSize: %"PRIu16"; IntRefQPDelta: %"PRId16"\n",
  186. print_threestate(co->RecoveryPointSEI), co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
  187. av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSize: %"PRIu16"; ", co2->MaxFrameSize);
  188. #if QSV_HAVE_MAX_SLICE_SIZE
  189. av_log(avctx, AV_LOG_VERBOSE, "MaxSliceSize: %"PRIu16"; ", co2->MaxSliceSize);
  190. #endif
  191. av_log(avctx, AV_LOG_VERBOSE, "\n");
  192. av_log(avctx, AV_LOG_VERBOSE,
  193. "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
  194. print_threestate(co2->BitrateLimit), print_threestate(co2->MBBRC),
  195. print_threestate(co2->ExtBRC));
  196. #if QSV_HAVE_TRELLIS
  197. av_log(avctx, AV_LOG_VERBOSE, "Trellis: ");
  198. if (co2->Trellis & MFX_TRELLIS_OFF) {
  199. av_log(avctx, AV_LOG_VERBOSE, "off");
  200. } else if (!co2->Trellis) {
  201. av_log(avctx, AV_LOG_VERBOSE, "auto");
  202. } else {
  203. if (co2->Trellis & MFX_TRELLIS_I) av_log(avctx, AV_LOG_VERBOSE, "I");
  204. if (co2->Trellis & MFX_TRELLIS_P) av_log(avctx, AV_LOG_VERBOSE, "P");
  205. if (co2->Trellis & MFX_TRELLIS_B) av_log(avctx, AV_LOG_VERBOSE, "B");
  206. }
  207. av_log(avctx, AV_LOG_VERBOSE, "\n");
  208. #endif
  209. #if QSV_HAVE_VDENC
  210. av_log(avctx, AV_LOG_VERBOSE, "VDENC: %s\n", print_threestate(info->LowPower));
  211. #endif
  212. #if QSV_VERSION_ATLEAST(1, 8)
  213. av_log(avctx, AV_LOG_VERBOSE,
  214. "RepeatPPS: %s; NumMbPerSlice: %"PRIu16"; LookAheadDS: ",
  215. print_threestate(co2->RepeatPPS), co2->NumMbPerSlice);
  216. switch (co2->LookAheadDS) {
  217. case MFX_LOOKAHEAD_DS_OFF: av_log(avctx, AV_LOG_VERBOSE, "off"); break;
  218. case MFX_LOOKAHEAD_DS_2x: av_log(avctx, AV_LOG_VERBOSE, "2x"); break;
  219. case MFX_LOOKAHEAD_DS_4x: av_log(avctx, AV_LOG_VERBOSE, "4x"); break;
  220. default: av_log(avctx, AV_LOG_VERBOSE, "unknown"); break;
  221. }
  222. av_log(avctx, AV_LOG_VERBOSE, "\n");
  223. av_log(avctx, AV_LOG_VERBOSE, "AdaptiveI: %s; AdaptiveB: %s; BRefType: ",
  224. print_threestate(co2->AdaptiveI), print_threestate(co2->AdaptiveB));
  225. switch (co2->BRefType) {
  226. case MFX_B_REF_OFF: av_log(avctx, AV_LOG_VERBOSE, "off"); break;
  227. case MFX_B_REF_PYRAMID: av_log(avctx, AV_LOG_VERBOSE, "pyramid"); break;
  228. default: av_log(avctx, AV_LOG_VERBOSE, "auto"); break;
  229. }
  230. av_log(avctx, AV_LOG_VERBOSE, "\n");
  231. #endif
  232. #if QSV_VERSION_ATLEAST(1, 9)
  233. av_log(avctx, AV_LOG_VERBOSE,
  234. "MinQPI: %"PRIu8"; MaxQPI: %"PRIu8"; MinQPP: %"PRIu8"; MaxQPP: %"PRIu8"; MinQPB: %"PRIu8"; MaxQPB: %"PRIu8"\n",
  235. co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
  236. #endif
  237. #endif
  238. if (avctx->codec_id == AV_CODEC_ID_H264) {
  239. av_log(avctx, AV_LOG_VERBOSE, "Entropy coding: %s; MaxDecFrameBuffering: %"PRIu16"\n",
  240. co->CAVLC == MFX_CODINGOPTION_ON ? "CAVLC" : "CABAC", co->MaxDecFrameBuffering);
  241. av_log(avctx, AV_LOG_VERBOSE,
  242. "NalHrdConformance: %s; SingleSeiNalUnit: %s; VuiVclHrdParameters: %s VuiNalHrdParameters: %s\n",
  243. print_threestate(co->NalHrdConformance), print_threestate(co->SingleSeiNalUnit),
  244. print_threestate(co->VuiVclHrdParameters), print_threestate(co->VuiNalHrdParameters));
  245. }
  246. av_log(avctx, AV_LOG_VERBOSE, "FrameRateExtD: %"PRIu32"; FrameRateExtN: %"PRIu32" \n",
  247. info->FrameInfo.FrameRateExtD, info->FrameInfo.FrameRateExtN);
  248. }
  249. static int select_rc_mode(AVCodecContext *avctx, QSVEncContext *q)
  250. {
  251. const char *rc_desc;
  252. mfxU16 rc_mode;
  253. int want_la = q->look_ahead;
  254. int want_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
  255. int want_vcm = q->vcm;
  256. if (want_la && !QSV_HAVE_LA) {
  257. av_log(avctx, AV_LOG_ERROR,
  258. "Lookahead ratecontrol mode requested, but is not supported by this SDK version\n");
  259. return AVERROR(ENOSYS);
  260. }
  261. if (want_vcm && !QSV_HAVE_VCM) {
  262. av_log(avctx, AV_LOG_ERROR,
  263. "VCM ratecontrol mode requested, but is not supported by this SDK version\n");
  264. return AVERROR(ENOSYS);
  265. }
  266. if (want_la + want_qscale + want_vcm > 1) {
  267. av_log(avctx, AV_LOG_ERROR,
  268. "More than one of: { constant qscale, lookahead, VCM } requested, "
  269. "only one of them can be used at a time.\n");
  270. return AVERROR(EINVAL);
  271. }
  272. if (!want_qscale && avctx->global_quality > 0 && !QSV_HAVE_ICQ){
  273. av_log(avctx, AV_LOG_ERROR,
  274. "ICQ ratecontrol mode requested, but is not supported by this SDK version\n");
  275. return AVERROR(ENOSYS);
  276. }
  277. if (want_qscale) {
  278. rc_mode = MFX_RATECONTROL_CQP;
  279. rc_desc = "constant quantization parameter (CQP)";
  280. }
  281. #if QSV_HAVE_VCM
  282. else if (want_vcm) {
  283. rc_mode = MFX_RATECONTROL_VCM;
  284. rc_desc = "video conferencing mode (VCM)";
  285. }
  286. #endif
  287. #if QSV_HAVE_LA
  288. else if (want_la) {
  289. rc_mode = MFX_RATECONTROL_LA;
  290. rc_desc = "VBR with lookahead (LA)";
  291. #if QSV_HAVE_ICQ
  292. if (avctx->global_quality > 0) {
  293. rc_mode = MFX_RATECONTROL_LA_ICQ;
  294. rc_desc = "intelligent constant quality with lookahead (LA_ICQ)";
  295. }
  296. #endif
  297. }
  298. #endif
  299. #if QSV_HAVE_ICQ
  300. else if (avctx->global_quality > 0) {
  301. rc_mode = MFX_RATECONTROL_ICQ;
  302. rc_desc = "intelligent constant quality (ICQ)";
  303. }
  304. #endif
  305. else if (avctx->rc_max_rate == avctx->bit_rate) {
  306. rc_mode = MFX_RATECONTROL_CBR;
  307. rc_desc = "constant bitrate (CBR)";
  308. }
  309. #if QSV_HAVE_AVBR
  310. else if (!avctx->rc_max_rate) {
  311. rc_mode = MFX_RATECONTROL_AVBR;
  312. rc_desc = "average variable bitrate (AVBR)";
  313. }
  314. #endif
  315. else {
  316. rc_mode = MFX_RATECONTROL_VBR;
  317. rc_desc = "variable bitrate (VBR)";
  318. }
  319. q->param.mfx.RateControlMethod = rc_mode;
  320. av_log(avctx, AV_LOG_VERBOSE, "Using the %s ratecontrol method\n", rc_desc);
  321. return 0;
  322. }
  323. static int check_enc_param(AVCodecContext *avctx, QSVEncContext *q)
  324. {
  325. mfxVideoParam param_out = { .mfx.CodecId = q->param.mfx.CodecId };
  326. mfxStatus ret;
  327. #define UNMATCH(x) (param_out.mfx.x != q->param.mfx.x)
  328. ret = MFXVideoENCODE_Query(q->session, &q->param, &param_out);
  329. if (ret < 0) {
  330. if (UNMATCH(CodecId))
  331. av_log(avctx, AV_LOG_ERROR, "Current codec type is unsupported\n");
  332. if (UNMATCH(CodecProfile))
  333. av_log(avctx, AV_LOG_ERROR, "Current profile is unsupported\n");
  334. if (UNMATCH(RateControlMethod))
  335. av_log(avctx, AV_LOG_ERROR, "Selected ratecontrol mode is unsupported\n");
  336. if (UNMATCH(LowPower))
  337. av_log(avctx, AV_LOG_ERROR, "Low power mode is unsupported\n");
  338. if (UNMATCH(FrameInfo.FrameRateExtN) || UNMATCH(FrameInfo.FrameRateExtD))
  339. av_log(avctx, AV_LOG_ERROR, "Current frame rate is unsupported\n");
  340. if (UNMATCH(FrameInfo.PicStruct))
  341. av_log(avctx, AV_LOG_ERROR, "Current picture structure is unsupported\n");
  342. if (UNMATCH(FrameInfo.Width) || UNMATCH(FrameInfo.Height))
  343. av_log(avctx, AV_LOG_ERROR, "Current resolution is unsupported\n");
  344. if (UNMATCH(FrameInfo.FourCC))
  345. av_log(avctx, AV_LOG_ERROR, "Current pixel format is unsupported\n");
  346. return 0;
  347. }
  348. return 1;
  349. }
  350. static int init_video_param_jpeg(AVCodecContext *avctx, QSVEncContext *q)
  351. {
  352. enum AVPixelFormat sw_format = avctx->pix_fmt == AV_PIX_FMT_QSV ?
  353. avctx->sw_pix_fmt : avctx->pix_fmt;
  354. const AVPixFmtDescriptor *desc;
  355. int ret;
  356. ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
  357. if (ret < 0)
  358. return AVERROR_BUG;
  359. q->param.mfx.CodecId = ret;
  360. if (avctx->level > 0)
  361. q->param.mfx.CodecLevel = avctx->level;
  362. q->param.mfx.CodecProfile = q->profile;
  363. desc = av_pix_fmt_desc_get(sw_format);
  364. if (!desc)
  365. return AVERROR_BUG;
  366. ff_qsv_map_pixfmt(sw_format, &q->param.mfx.FrameInfo.FourCC);
  367. q->param.mfx.FrameInfo.CropX = 0;
  368. q->param.mfx.FrameInfo.CropY = 0;
  369. q->param.mfx.FrameInfo.CropW = avctx->width;
  370. q->param.mfx.FrameInfo.CropH = avctx->height;
  371. q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num;
  372. q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den;
  373. q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  374. q->param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
  375. q->param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
  376. q->param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
  377. q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, 16);
  378. q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, 16);
  379. if (avctx->hw_frames_ctx) {
  380. AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
  381. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  382. q->param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
  383. q->param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
  384. }
  385. if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
  386. q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
  387. q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
  388. } else {
  389. q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
  390. q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
  391. }
  392. q->param.mfx.Interleaved = 1;
  393. q->param.mfx.Quality = av_clip(avctx->global_quality, 1, 100);
  394. q->param.mfx.RestartInterval = 0;
  395. return 0;
  396. }
  397. static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
  398. {
  399. enum AVPixelFormat sw_format = avctx->pix_fmt == AV_PIX_FMT_QSV ?
  400. avctx->sw_pix_fmt : avctx->pix_fmt;
  401. const AVPixFmtDescriptor *desc;
  402. float quant;
  403. int ret;
  404. mfxVersion ver;
  405. ret = MFXQueryVersion(q->session,&ver);
  406. if (ret != MFX_ERR_NONE) {
  407. av_log(avctx, AV_LOG_ERROR, "Error getting the session handle\n");
  408. return AVERROR_UNKNOWN;
  409. }
  410. ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
  411. if (ret < 0)
  412. return AVERROR_BUG;
  413. q->param.mfx.CodecId = ret;
  414. if (avctx->level > 0)
  415. q->param.mfx.CodecLevel = avctx->level;
  416. if (avctx->compression_level == FF_COMPRESSION_DEFAULT) {
  417. avctx->compression_level = q->preset;
  418. } else if (avctx->compression_level >= 0) {
  419. if (avctx->compression_level > MFX_TARGETUSAGE_BEST_SPEED) {
  420. av_log(avctx, AV_LOG_WARNING, "Invalid compression level: "
  421. "valid range is 0-%d, using %d instead\n",
  422. MFX_TARGETUSAGE_BEST_SPEED, MFX_TARGETUSAGE_BEST_SPEED);
  423. avctx->compression_level = MFX_TARGETUSAGE_BEST_SPEED;
  424. }
  425. }
  426. #if QSV_HAVE_VDENC
  427. q->param.mfx.LowPower = q->low_power ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  428. #endif
  429. q->param.mfx.CodecProfile = q->profile;
  430. q->param.mfx.TargetUsage = avctx->compression_level;
  431. q->param.mfx.GopPicSize = FFMAX(0, avctx->gop_size);
  432. q->param.mfx.GopRefDist = FFMAX(-1, avctx->max_b_frames) + 1;
  433. q->param.mfx.GopOptFlag = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ?
  434. MFX_GOP_CLOSED : 0;
  435. q->param.mfx.IdrInterval = q->idr_interval;
  436. q->param.mfx.NumSlice = avctx->slices;
  437. q->param.mfx.NumRefFrame = FFMAX(0, avctx->refs);
  438. q->param.mfx.EncodedOrder = 0;
  439. q->param.mfx.BufferSizeInKB = 0;
  440. desc = av_pix_fmt_desc_get(sw_format);
  441. if (!desc)
  442. return AVERROR_BUG;
  443. ff_qsv_map_pixfmt(sw_format, &q->param.mfx.FrameInfo.FourCC);
  444. q->param.mfx.FrameInfo.CropX = 0;
  445. q->param.mfx.FrameInfo.CropY = 0;
  446. q->param.mfx.FrameInfo.CropW = avctx->width;
  447. q->param.mfx.FrameInfo.CropH = avctx->height;
  448. q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num;
  449. q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den;
  450. q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  451. q->param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
  452. q->param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
  453. q->param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
  454. // If the minor version is greater than or equal to 19,
  455. // then can use the same alignment settings as H.264 for HEVC
  456. q->width_align = (avctx->codec_id != AV_CODEC_ID_HEVC ||
  457. QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 19)) ? 16 : 32;
  458. q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align);
  459. if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  460. // it is important that PicStruct be setup correctly from the
  461. // start--otherwise, encoding doesn't work and results in a bunch
  462. // of incompatible video parameter errors
  463. q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
  464. // height alignment always must be 32 for interlaced video
  465. q->height_align = 32;
  466. } else {
  467. q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  468. // for progressive video, the height should be aligned to 16 for
  469. // H.264. For HEVC, depending on the version of MFX, it should be
  470. // either 32 or 16. The lower number is better if possible.
  471. q->height_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16;
  472. }
  473. q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, q->height_align);
  474. if (avctx->hw_frames_ctx) {
  475. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  476. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  477. q->param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
  478. q->param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
  479. }
  480. if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
  481. q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
  482. q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
  483. } else {
  484. q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
  485. q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
  486. }
  487. ret = select_rc_mode(avctx, q);
  488. if (ret < 0)
  489. return ret;
  490. switch (q->param.mfx.RateControlMethod) {
  491. case MFX_RATECONTROL_CBR:
  492. case MFX_RATECONTROL_VBR:
  493. #if QSV_HAVE_VCM
  494. case MFX_RATECONTROL_VCM:
  495. #endif
  496. q->param.mfx.BufferSizeInKB = avctx->rc_buffer_size / 8000;
  497. q->param.mfx.InitialDelayInKB = avctx->rc_initial_buffer_occupancy / 1000;
  498. q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
  499. q->param.mfx.MaxKbps = avctx->rc_max_rate / 1000;
  500. break;
  501. case MFX_RATECONTROL_CQP:
  502. quant = avctx->global_quality / FF_QP2LAMBDA;
  503. q->param.mfx.QPI = av_clip(quant * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
  504. q->param.mfx.QPP = av_clip(quant, 0, 51);
  505. q->param.mfx.QPB = av_clip(quant * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51);
  506. break;
  507. #if QSV_HAVE_AVBR
  508. case MFX_RATECONTROL_AVBR:
  509. q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
  510. q->param.mfx.Convergence = q->avbr_convergence;
  511. q->param.mfx.Accuracy = q->avbr_accuracy;
  512. break;
  513. #endif
  514. #if QSV_HAVE_LA
  515. case MFX_RATECONTROL_LA:
  516. q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
  517. q->extco2.LookAheadDepth = q->look_ahead_depth;
  518. break;
  519. #if QSV_HAVE_ICQ
  520. case MFX_RATECONTROL_LA_ICQ:
  521. q->extco2.LookAheadDepth = q->look_ahead_depth;
  522. case MFX_RATECONTROL_ICQ:
  523. q->param.mfx.ICQQuality = avctx->global_quality;
  524. break;
  525. #endif
  526. #endif
  527. }
  528. // the HEVC encoder plugin currently fails if coding options
  529. // are provided
  530. if (avctx->codec_id != AV_CODEC_ID_HEVC) {
  531. q->extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
  532. q->extco.Header.BufferSz = sizeof(q->extco);
  533. q->extco.PicTimingSEI = q->pic_timing_sei ?
  534. MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
  535. if (q->rdo >= 0)
  536. q->extco.RateDistortionOpt = q->rdo > 0 ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  537. if (avctx->codec_id == AV_CODEC_ID_H264) {
  538. #if FF_API_CODER_TYPE
  539. FF_DISABLE_DEPRECATION_WARNINGS
  540. if (avctx->coder_type >= 0)
  541. q->cavlc = avctx->coder_type == FF_CODER_TYPE_VLC;
  542. FF_ENABLE_DEPRECATION_WARNINGS
  543. #endif
  544. q->extco.CAVLC = q->cavlc ? MFX_CODINGOPTION_ON
  545. : MFX_CODINGOPTION_UNKNOWN;
  546. if (avctx->strict_std_compliance != FF_COMPLIANCE_NORMAL)
  547. q->extco.NalHrdConformance = avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL ?
  548. MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  549. if (q->single_sei_nal_unit >= 0)
  550. q->extco.SingleSeiNalUnit = q->single_sei_nal_unit ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  551. if (q->recovery_point_sei >= 0)
  552. q->extco.RecoveryPointSEI = q->recovery_point_sei ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  553. q->extco.MaxDecFrameBuffering = q->max_dec_frame_buffering;
  554. q->extco.AUDelimiter = q->aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  555. }
  556. q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco;
  557. if (avctx->codec_id == AV_CODEC_ID_H264) {
  558. #if QSV_HAVE_CO2
  559. q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
  560. q->extco2.Header.BufferSz = sizeof(q->extco2);
  561. if (q->int_ref_type >= 0)
  562. q->extco2.IntRefType = q->int_ref_type;
  563. if (q->int_ref_cycle_size >= 0)
  564. q->extco2.IntRefCycleSize = q->int_ref_cycle_size;
  565. if (q->int_ref_qp_delta != INT16_MIN)
  566. q->extco2.IntRefQPDelta = q->int_ref_qp_delta;
  567. if (q->bitrate_limit >= 0)
  568. q->extco2.BitrateLimit = q->bitrate_limit ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  569. if (q->mbbrc >= 0)
  570. q->extco2.MBBRC = q->mbbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  571. if (q->extbrc >= 0)
  572. q->extco2.ExtBRC = q->extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  573. if (q->max_frame_size >= 0)
  574. q->extco2.MaxFrameSize = q->max_frame_size;
  575. #if QSV_HAVE_MAX_SLICE_SIZE
  576. if (q->max_slice_size >= 0)
  577. q->extco2.MaxSliceSize = q->max_slice_size;
  578. #endif
  579. #if QSV_HAVE_TRELLIS
  580. q->extco2.Trellis = q->trellis;
  581. #endif
  582. #if QSV_VERSION_ATLEAST(1, 8)
  583. q->extco2.LookAheadDS = q->look_ahead_downsampling;
  584. q->extco2.RepeatPPS = q->repeat_pps ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  585. #if FF_API_PRIVATE_OPT
  586. FF_DISABLE_DEPRECATION_WARNINGS
  587. if (avctx->b_frame_strategy >= 0)
  588. q->b_strategy = avctx->b_frame_strategy;
  589. FF_ENABLE_DEPRECATION_WARNINGS
  590. #endif
  591. if (q->b_strategy >= 0)
  592. q->extco2.BRefType = q->b_strategy ? MFX_B_REF_PYRAMID : MFX_B_REF_OFF;
  593. if (q->adaptive_i >= 0)
  594. q->extco2.AdaptiveI = q->adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  595. if (q->adaptive_b >= 0)
  596. q->extco2.AdaptiveB = q->adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
  597. #endif
  598. #if QSV_VERSION_ATLEAST(1, 9)
  599. if (avctx->qmin >= 0 && avctx->qmax >= 0 && avctx->qmin > avctx->qmax) {
  600. av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are set but invalid, please make sure min <= max\n");
  601. return AVERROR(EINVAL);
  602. }
  603. if (avctx->qmin >= 0) {
  604. q->extco2.MinQPI = avctx->qmin > 51 ? 51 : avctx->qmin;
  605. q->extco2.MinQPP = q->extco2.MinQPB = q->extco2.MinQPI;
  606. }
  607. if (avctx->qmax >= 0) {
  608. q->extco2.MaxQPI = avctx->qmax > 51 ? 51 : avctx->qmax;
  609. q->extco2.MaxQPP = q->extco2.MaxQPB = q->extco2.MaxQPI;
  610. }
  611. #endif
  612. q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco2;
  613. #endif
  614. #if QSV_HAVE_MF
  615. if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
  616. q->extmfp.Header.BufferId = MFX_EXTBUFF_MULTI_FRAME_PARAM;
  617. q->extmfp.Header.BufferSz = sizeof(q->extmfp);
  618. q->extmfp.MFMode = q->mfmode;
  619. av_log(avctx,AV_LOG_VERBOSE,"MFMode:%d\n", q->extmfp.MFMode);
  620. q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extmfp;
  621. }
  622. #endif
  623. }
  624. }
  625. if (!check_enc_param(avctx,q)) {
  626. av_log(avctx, AV_LOG_ERROR,
  627. "some encoding parameters are not supported by the QSV "
  628. "runtime. Please double check the input parameters.\n");
  629. return AVERROR(ENOSYS);
  630. }
  631. return 0;
  632. }
  633. static int qsv_retrieve_enc_jpeg_params(AVCodecContext *avctx, QSVEncContext *q)
  634. {
  635. int ret = 0;
  636. ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
  637. if (ret < 0)
  638. return ff_qsv_print_error(avctx, ret,
  639. "Error calling GetVideoParam");
  640. q->packet_size = q->param.mfx.BufferSizeInKB * 1000;
  641. // for qsv mjpeg the return value maybe 0 so alloc the buffer
  642. if (q->packet_size == 0)
  643. q->packet_size = q->param.mfx.FrameInfo.Height * q->param.mfx.FrameInfo.Width * 4;
  644. return 0;
  645. }
  646. static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q)
  647. {
  648. AVCPBProperties *cpb_props;
  649. uint8_t sps_buf[128];
  650. uint8_t pps_buf[128];
  651. mfxExtCodingOptionSPSPPS extradata = {
  652. .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
  653. .Header.BufferSz = sizeof(extradata),
  654. .SPSBuffer = sps_buf, .SPSBufSize = sizeof(sps_buf),
  655. .PPSBuffer = pps_buf, .PPSBufSize = sizeof(pps_buf)
  656. };
  657. mfxExtCodingOption co = {
  658. .Header.BufferId = MFX_EXTBUFF_CODING_OPTION,
  659. .Header.BufferSz = sizeof(co),
  660. };
  661. #if QSV_HAVE_CO2
  662. mfxExtCodingOption2 co2 = {
  663. .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
  664. .Header.BufferSz = sizeof(co2),
  665. };
  666. #endif
  667. mfxExtBuffer *ext_buffers[] = {
  668. (mfxExtBuffer*)&extradata,
  669. (mfxExtBuffer*)&co,
  670. #if QSV_HAVE_CO2
  671. (mfxExtBuffer*)&co2,
  672. #endif
  673. };
  674. int need_pps = avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO;
  675. int ret;
  676. q->param.ExtParam = ext_buffers;
  677. q->param.NumExtParam = FF_ARRAY_ELEMS(ext_buffers);
  678. ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
  679. if (ret < 0)
  680. return ff_qsv_print_error(avctx, ret,
  681. "Error calling GetVideoParam");
  682. q->packet_size = q->param.mfx.BufferSizeInKB * 1000;
  683. if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)) {
  684. av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n");
  685. return AVERROR_UNKNOWN;
  686. }
  687. avctx->extradata = av_malloc(extradata.SPSBufSize + need_pps * extradata.PPSBufSize +
  688. AV_INPUT_BUFFER_PADDING_SIZE);
  689. if (!avctx->extradata)
  690. return AVERROR(ENOMEM);
  691. memcpy(avctx->extradata, sps_buf, extradata.SPSBufSize);
  692. if (need_pps)
  693. memcpy(avctx->extradata + extradata.SPSBufSize, pps_buf, extradata.PPSBufSize);
  694. avctx->extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
  695. memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  696. cpb_props = ff_add_cpb_side_data(avctx);
  697. if (!cpb_props)
  698. return AVERROR(ENOMEM);
  699. cpb_props->max_bitrate = avctx->rc_max_rate;
  700. cpb_props->min_bitrate = avctx->rc_min_rate;
  701. cpb_props->avg_bitrate = avctx->bit_rate;
  702. cpb_props->buffer_size = avctx->rc_buffer_size;
  703. dump_video_param(avctx, q, ext_buffers + 1);
  704. return 0;
  705. }
  706. static int qsv_init_opaque_alloc(AVCodecContext *avctx, QSVEncContext *q)
  707. {
  708. AVQSVContext *qsv = avctx->hwaccel_context;
  709. mfxFrameSurface1 *surfaces;
  710. int nb_surfaces, i;
  711. nb_surfaces = qsv->nb_opaque_surfaces + q->req.NumFrameSuggested;
  712. q->opaque_alloc_buf = av_buffer_allocz(sizeof(*surfaces) * nb_surfaces);
  713. if (!q->opaque_alloc_buf)
  714. return AVERROR(ENOMEM);
  715. q->opaque_surfaces = av_malloc_array(nb_surfaces, sizeof(*q->opaque_surfaces));
  716. if (!q->opaque_surfaces)
  717. return AVERROR(ENOMEM);
  718. surfaces = (mfxFrameSurface1*)q->opaque_alloc_buf->data;
  719. for (i = 0; i < nb_surfaces; i++) {
  720. surfaces[i].Info = q->req.Info;
  721. q->opaque_surfaces[i] = surfaces + i;
  722. }
  723. q->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
  724. q->opaque_alloc.Header.BufferSz = sizeof(q->opaque_alloc);
  725. q->opaque_alloc.In.Surfaces = q->opaque_surfaces;
  726. q->opaque_alloc.In.NumSurface = nb_surfaces;
  727. q->opaque_alloc.In.Type = q->req.Type;
  728. q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->opaque_alloc;
  729. qsv->nb_opaque_surfaces = nb_surfaces;
  730. qsv->opaque_surfaces = q->opaque_alloc_buf;
  731. qsv->opaque_alloc_type = q->req.Type;
  732. return 0;
  733. }
  734. static int qsvenc_init_session(AVCodecContext *avctx, QSVEncContext *q)
  735. {
  736. int ret;
  737. if (avctx->hwaccel_context) {
  738. AVQSVContext *qsv = avctx->hwaccel_context;
  739. q->session = qsv->session;
  740. } else if (avctx->hw_frames_ctx) {
  741. q->frames_ctx.hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
  742. if (!q->frames_ctx.hw_frames_ctx)
  743. return AVERROR(ENOMEM);
  744. ret = ff_qsv_init_session_frames(avctx, &q->internal_session,
  745. &q->frames_ctx, q->load_plugins,
  746. q->param.IOPattern == MFX_IOPATTERN_IN_OPAQUE_MEMORY);
  747. if (ret < 0) {
  748. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  749. return ret;
  750. }
  751. q->session = q->internal_session;
  752. } else if (avctx->hw_device_ctx) {
  753. ret = ff_qsv_init_session_device(avctx, &q->internal_session,
  754. avctx->hw_device_ctx, q->load_plugins);
  755. if (ret < 0)
  756. return ret;
  757. q->session = q->internal_session;
  758. } else {
  759. ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
  760. q->load_plugins);
  761. if (ret < 0)
  762. return ret;
  763. q->session = q->internal_session;
  764. }
  765. return 0;
  766. }
  767. static inline unsigned int qsv_fifo_item_size(void)
  768. {
  769. return sizeof(AVPacket) + sizeof(mfxSyncPoint*) + sizeof(mfxBitstream*);
  770. }
  771. static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
  772. {
  773. return av_fifo_size(fifo)/qsv_fifo_item_size();
  774. }
  775. int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
  776. {
  777. int iopattern = 0;
  778. int opaque_alloc = 0;
  779. int ret;
  780. q->param.AsyncDepth = q->async_depth;
  781. q->async_fifo = av_fifo_alloc(q->async_depth * qsv_fifo_item_size());
  782. if (!q->async_fifo)
  783. return AVERROR(ENOMEM);
  784. if (avctx->hwaccel_context) {
  785. AVQSVContext *qsv = avctx->hwaccel_context;
  786. iopattern = qsv->iopattern;
  787. opaque_alloc = qsv->opaque_alloc;
  788. }
  789. if (avctx->hw_frames_ctx) {
  790. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  791. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  792. if (!iopattern) {
  793. if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
  794. iopattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY;
  795. else if (frames_hwctx->frame_type &
  796. (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
  797. iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
  798. }
  799. }
  800. if (!iopattern)
  801. iopattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
  802. q->param.IOPattern = iopattern;
  803. ret = qsvenc_init_session(avctx, q);
  804. if (ret < 0)
  805. return ret;
  806. // in the mfxInfoMFX struct, JPEG is different from other codecs
  807. switch (avctx->codec_id) {
  808. case AV_CODEC_ID_MJPEG:
  809. ret = init_video_param_jpeg(avctx, q);
  810. break;
  811. default:
  812. ret = init_video_param(avctx, q);
  813. break;
  814. }
  815. if (ret < 0)
  816. return ret;
  817. ret = MFXVideoENCODE_Query(q->session, &q->param, &q->param);
  818. if (ret == MFX_WRN_PARTIAL_ACCELERATION) {
  819. av_log(avctx, AV_LOG_WARNING, "Encoder will work with partial HW acceleration\n");
  820. } else if (ret < 0) {
  821. return ff_qsv_print_error(avctx, ret,
  822. "Error querying encoder params");
  823. }
  824. ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req);
  825. if (ret < 0)
  826. return ff_qsv_print_error(avctx, ret,
  827. "Error querying (IOSurf) the encoding parameters");
  828. if (opaque_alloc) {
  829. ret = qsv_init_opaque_alloc(avctx, q);
  830. if (ret < 0)
  831. return ret;
  832. }
  833. if (avctx->hwaccel_context) {
  834. AVQSVContext *qsv = avctx->hwaccel_context;
  835. int i, j;
  836. q->extparam = av_mallocz_array(qsv->nb_ext_buffers + q->nb_extparam_internal,
  837. sizeof(*q->extparam));
  838. if (!q->extparam)
  839. return AVERROR(ENOMEM);
  840. q->param.ExtParam = q->extparam;
  841. for (i = 0; i < qsv->nb_ext_buffers; i++)
  842. q->param.ExtParam[i] = qsv->ext_buffers[i];
  843. q->param.NumExtParam = qsv->nb_ext_buffers;
  844. for (i = 0; i < q->nb_extparam_internal; i++) {
  845. for (j = 0; j < qsv->nb_ext_buffers; j++) {
  846. if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId)
  847. break;
  848. }
  849. if (j < qsv->nb_ext_buffers)
  850. continue;
  851. q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i];
  852. }
  853. } else {
  854. q->param.ExtParam = q->extparam_internal;
  855. q->param.NumExtParam = q->nb_extparam_internal;
  856. }
  857. ret = MFXVideoENCODE_Init(q->session, &q->param);
  858. if (ret < 0)
  859. return ff_qsv_print_error(avctx, ret,
  860. "Error initializing the encoder");
  861. else if (ret > 0)
  862. ff_qsv_print_warning(avctx, ret,
  863. "Warning in encoder initialization");
  864. switch (avctx->codec_id) {
  865. case AV_CODEC_ID_MJPEG:
  866. ret = qsv_retrieve_enc_jpeg_params(avctx, q);
  867. break;
  868. default:
  869. ret = qsv_retrieve_enc_params(avctx, q);
  870. break;
  871. }
  872. if (ret < 0) {
  873. av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n");
  874. return ret;
  875. }
  876. q->avctx = avctx;
  877. return 0;
  878. }
  879. static void free_encoder_ctrl_payloads(mfxEncodeCtrl* enc_ctrl)
  880. {
  881. if (enc_ctrl) {
  882. int i;
  883. for (i = 0; i < enc_ctrl->NumPayload && i < QSV_MAX_ENC_PAYLOAD; i++) {
  884. av_free(enc_ctrl->Payload[i]);
  885. }
  886. enc_ctrl->NumPayload = 0;
  887. }
  888. }
  889. static void clear_unused_frames(QSVEncContext *q)
  890. {
  891. QSVFrame *cur = q->work_frames;
  892. while (cur) {
  893. if (cur->used && !cur->surface.Data.Locked) {
  894. free_encoder_ctrl_payloads(&cur->enc_ctrl);
  895. if (cur->frame->format == AV_PIX_FMT_QSV) {
  896. av_frame_unref(cur->frame);
  897. }
  898. cur->used = 0;
  899. }
  900. cur = cur->next;
  901. }
  902. }
  903. static int get_free_frame(QSVEncContext *q, QSVFrame **f)
  904. {
  905. QSVFrame *frame, **last;
  906. clear_unused_frames(q);
  907. frame = q->work_frames;
  908. last = &q->work_frames;
  909. while (frame) {
  910. if (!frame->used) {
  911. *f = frame;
  912. frame->used = 1;
  913. return 0;
  914. }
  915. last = &frame->next;
  916. frame = frame->next;
  917. }
  918. frame = av_mallocz(sizeof(*frame));
  919. if (!frame)
  920. return AVERROR(ENOMEM);
  921. frame->frame = av_frame_alloc();
  922. if (!frame->frame) {
  923. av_freep(&frame);
  924. return AVERROR(ENOMEM);
  925. }
  926. frame->enc_ctrl.Payload = av_mallocz(sizeof(mfxPayload*) * QSV_MAX_ENC_PAYLOAD);
  927. if (!frame->enc_ctrl.Payload) {
  928. av_freep(&frame);
  929. return AVERROR(ENOMEM);
  930. }
  931. *last = frame;
  932. *f = frame;
  933. frame->used = 1;
  934. return 0;
  935. }
  936. static int submit_frame(QSVEncContext *q, const AVFrame *frame,
  937. QSVFrame **new_frame)
  938. {
  939. QSVFrame *qf;
  940. int ret;
  941. ret = get_free_frame(q, &qf);
  942. if (ret < 0)
  943. return ret;
  944. if (frame->format == AV_PIX_FMT_QSV) {
  945. ret = av_frame_ref(qf->frame, frame);
  946. if (ret < 0)
  947. return ret;
  948. qf->surface = *(mfxFrameSurface1*)qf->frame->data[3];
  949. if (q->frames_ctx.mids) {
  950. ret = ff_qsv_find_surface_idx(&q->frames_ctx, qf);
  951. if (ret < 0)
  952. return ret;
  953. qf->surface.Data.MemId = &q->frames_ctx.mids[ret];
  954. }
  955. } else {
  956. /* make a copy if the input is not padded as libmfx requires */
  957. /* and to make allocation continious for data[0]/data[1] */
  958. if ((frame->height & 31 || frame->linesize[0] & (q->width_align - 1)) ||
  959. (frame->data[1] - frame->data[0] != frame->linesize[0] * FFALIGN(qf->frame->height, q->height_align))) {
  960. qf->frame->height = FFALIGN(frame->height, q->height_align);
  961. qf->frame->width = FFALIGN(frame->width, q->width_align);
  962. qf->frame->format = frame->format;
  963. if (!qf->frame->data[0]) {
  964. ret = av_frame_get_buffer(qf->frame, q->width_align);
  965. if (ret < 0)
  966. return ret;
  967. }
  968. qf->frame->height = frame->height;
  969. qf->frame->width = frame->width;
  970. ret = av_frame_copy(qf->frame, frame);
  971. if (ret < 0) {
  972. av_frame_unref(qf->frame);
  973. return ret;
  974. }
  975. } else {
  976. ret = av_frame_ref(qf->frame, frame);
  977. if (ret < 0)
  978. return ret;
  979. }
  980. qf->surface.Info = q->param.mfx.FrameInfo;
  981. qf->surface.Info.PicStruct =
  982. !frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
  983. frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
  984. MFX_PICSTRUCT_FIELD_BFF;
  985. if (frame->repeat_pict == 1)
  986. qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
  987. else if (frame->repeat_pict == 2)
  988. qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
  989. else if (frame->repeat_pict == 4)
  990. qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
  991. qf->surface.Data.PitchLow = qf->frame->linesize[0];
  992. qf->surface.Data.Y = qf->frame->data[0];
  993. qf->surface.Data.UV = qf->frame->data[1];
  994. }
  995. qf->surface.Data.TimeStamp = av_rescale_q(frame->pts, q->avctx->time_base, (AVRational){1, 90000});
  996. *new_frame = qf;
  997. return 0;
  998. }
  999. static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
  1000. {
  1001. if (q->param.mfx.CodecId == MFX_CODEC_AVC) {
  1002. if (q->param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
  1003. q->param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
  1004. q->param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
  1005. av_log(avctx, AV_LOG_WARNING,
  1006. "Interlaced coding is supported"
  1007. " at Main/High Profile Level 2.2-4.0\n");
  1008. }
  1009. }
  1010. static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
  1011. const AVFrame *frame)
  1012. {
  1013. AVPacket new_pkt = { 0 };
  1014. mfxBitstream *bs;
  1015. #if QSV_VERSION_ATLEAST(1, 26)
  1016. mfxExtAVCEncodedFrameInfo *enc_info;
  1017. mfxExtBuffer **enc_buf;
  1018. #endif
  1019. mfxFrameSurface1 *surf = NULL;
  1020. mfxSyncPoint *sync = NULL;
  1021. QSVFrame *qsv_frame = NULL;
  1022. mfxEncodeCtrl* enc_ctrl = NULL;
  1023. int ret;
  1024. if (frame) {
  1025. ret = submit_frame(q, frame, &qsv_frame);
  1026. if (ret < 0) {
  1027. av_log(avctx, AV_LOG_ERROR, "Error submitting the frame for encoding.\n");
  1028. return ret;
  1029. }
  1030. }
  1031. if (qsv_frame) {
  1032. surf = &qsv_frame->surface;
  1033. enc_ctrl = &qsv_frame->enc_ctrl;
  1034. memset(enc_ctrl, 0, sizeof(mfxEncodeCtrl));
  1035. if (frame->pict_type == AV_PICTURE_TYPE_I) {
  1036. enc_ctrl->FrameType = MFX_FRAMETYPE_I | MFX_FRAMETYPE_REF;
  1037. if (q->forced_idr)
  1038. enc_ctrl->FrameType |= MFX_FRAMETYPE_IDR;
  1039. }
  1040. }
  1041. ret = av_new_packet(&new_pkt, q->packet_size);
  1042. if (ret < 0) {
  1043. av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
  1044. return ret;
  1045. }
  1046. bs = av_mallocz(sizeof(*bs));
  1047. if (!bs) {
  1048. av_packet_unref(&new_pkt);
  1049. return AVERROR(ENOMEM);
  1050. }
  1051. bs->Data = new_pkt.data;
  1052. bs->MaxLength = new_pkt.size;
  1053. #if QSV_VERSION_ATLEAST(1, 26)
  1054. if (avctx->codec_id == AV_CODEC_ID_H264) {
  1055. enc_info = av_mallocz(sizeof(*enc_info));
  1056. if (!enc_info)
  1057. return AVERROR(ENOMEM);
  1058. enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO;
  1059. enc_info->Header.BufferSz = sizeof (*enc_info);
  1060. bs->NumExtParam = 1;
  1061. enc_buf = av_mallocz(sizeof(mfxExtBuffer *));
  1062. if (!enc_buf)
  1063. return AVERROR(ENOMEM);
  1064. enc_buf[0] = (mfxExtBuffer *)enc_info;
  1065. bs->ExtParam = enc_buf;
  1066. }
  1067. #endif
  1068. if (q->set_encode_ctrl_cb) {
  1069. q->set_encode_ctrl_cb(avctx, frame, &qsv_frame->enc_ctrl);
  1070. }
  1071. sync = av_mallocz(sizeof(*sync));
  1072. if (!sync) {
  1073. av_freep(&bs);
  1074. #if QSV_VERSION_ATLEAST(1, 26)
  1075. if (avctx->codec_id == AV_CODEC_ID_H264) {
  1076. av_freep(&enc_info);
  1077. av_freep(&enc_buf);
  1078. }
  1079. #endif
  1080. av_packet_unref(&new_pkt);
  1081. return AVERROR(ENOMEM);
  1082. }
  1083. do {
  1084. ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, bs, sync);
  1085. if (ret == MFX_WRN_DEVICE_BUSY)
  1086. av_usleep(500);
  1087. } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_WRN_IN_EXECUTION);
  1088. if (ret > 0)
  1089. ff_qsv_print_warning(avctx, ret, "Warning during encoding");
  1090. if (ret < 0) {
  1091. av_packet_unref(&new_pkt);
  1092. av_freep(&bs);
  1093. #if QSV_VERSION_ATLEAST(1, 26)
  1094. if (avctx->codec_id == AV_CODEC_ID_H264) {
  1095. av_freep(&enc_info);
  1096. av_freep(&enc_buf);
  1097. }
  1098. #endif
  1099. av_freep(&sync);
  1100. return (ret == MFX_ERR_MORE_DATA) ?
  1101. 0 : ff_qsv_print_error(avctx, ret, "Error during encoding");
  1102. }
  1103. if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM && frame->interlaced_frame)
  1104. print_interlace_msg(avctx, q);
  1105. if (*sync) {
  1106. av_fifo_generic_write(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
  1107. av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
  1108. av_fifo_generic_write(q->async_fifo, &bs, sizeof(bs), NULL);
  1109. } else {
  1110. av_freep(&sync);
  1111. av_packet_unref(&new_pkt);
  1112. av_freep(&bs);
  1113. #if QSV_VERSION_ATLEAST(1, 26)
  1114. if (avctx->codec_id == AV_CODEC_ID_H264) {
  1115. av_freep(&enc_info);
  1116. av_freep(&enc_buf);
  1117. }
  1118. #endif
  1119. }
  1120. return 0;
  1121. }
  1122. int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
  1123. AVPacket *pkt, const AVFrame *frame, int *got_packet)
  1124. {
  1125. int ret;
  1126. ret = encode_frame(avctx, q, frame);
  1127. if (ret < 0)
  1128. return ret;
  1129. if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) ||
  1130. (!frame && av_fifo_size(q->async_fifo))) {
  1131. AVPacket new_pkt;
  1132. mfxBitstream *bs;
  1133. mfxSyncPoint *sync;
  1134. #if QSV_VERSION_ATLEAST(1, 26)
  1135. mfxExtAVCEncodedFrameInfo *enc_info;
  1136. mfxExtBuffer **enc_buf;
  1137. #endif
  1138. enum AVPictureType pict_type;
  1139. av_fifo_generic_read(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
  1140. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  1141. av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
  1142. do {
  1143. ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
  1144. } while (ret == MFX_WRN_IN_EXECUTION);
  1145. new_pkt.dts = av_rescale_q(bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
  1146. new_pkt.pts = av_rescale_q(bs->TimeStamp, (AVRational){1, 90000}, avctx->time_base);
  1147. new_pkt.size = bs->DataLength;
  1148. if (bs->FrameType & MFX_FRAMETYPE_IDR ||
  1149. bs->FrameType & MFX_FRAMETYPE_xIDR)
  1150. new_pkt.flags |= AV_PKT_FLAG_KEY;
  1151. if (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI)
  1152. pict_type = AV_PICTURE_TYPE_I;
  1153. else if (bs->FrameType & MFX_FRAMETYPE_P || bs->FrameType & MFX_FRAMETYPE_xP)
  1154. pict_type = AV_PICTURE_TYPE_P;
  1155. else if (bs->FrameType & MFX_FRAMETYPE_B || bs->FrameType & MFX_FRAMETYPE_xB)
  1156. pict_type = AV_PICTURE_TYPE_B;
  1157. else if (bs->FrameType == MFX_FRAMETYPE_UNKNOWN) {
  1158. pict_type = AV_PICTURE_TYPE_NONE;
  1159. av_log(avctx, AV_LOG_WARNING, "Unknown FrameType, set pict_type to AV_PICTURE_TYPE_NONE.\n");
  1160. } else {
  1161. av_log(avctx, AV_LOG_ERROR, "Invalid FrameType:%d.\n", bs->FrameType);
  1162. return AVERROR_INVALIDDATA;
  1163. }
  1164. #if FF_API_CODED_FRAME
  1165. FF_DISABLE_DEPRECATION_WARNINGS
  1166. avctx->coded_frame->pict_type = pict_type;
  1167. FF_ENABLE_DEPRECATION_WARNINGS
  1168. #endif
  1169. #if QSV_VERSION_ATLEAST(1, 26)
  1170. if (avctx->codec_id == AV_CODEC_ID_H264) {
  1171. enc_buf = bs->ExtParam;
  1172. enc_info = (mfxExtAVCEncodedFrameInfo *)(*bs->ExtParam);
  1173. ff_side_data_set_encoder_stats(&new_pkt,
  1174. enc_info->QP * FF_QP2LAMBDA, NULL, 0, pict_type);
  1175. av_freep(&enc_info);
  1176. av_freep(&enc_buf);
  1177. }
  1178. #endif
  1179. av_freep(&bs);
  1180. av_freep(&sync);
  1181. if (pkt->data) {
  1182. if (pkt->size < new_pkt.size) {
  1183. av_log(avctx, AV_LOG_ERROR, "Submitted buffer not large enough: %d < %d\n",
  1184. pkt->size, new_pkt.size);
  1185. av_packet_unref(&new_pkt);
  1186. return AVERROR(EINVAL);
  1187. }
  1188. memcpy(pkt->data, new_pkt.data, new_pkt.size);
  1189. pkt->size = new_pkt.size;
  1190. ret = av_packet_copy_props(pkt, &new_pkt);
  1191. av_packet_unref(&new_pkt);
  1192. if (ret < 0)
  1193. return ret;
  1194. } else
  1195. *pkt = new_pkt;
  1196. *got_packet = 1;
  1197. }
  1198. return 0;
  1199. }
  1200. int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
  1201. {
  1202. QSVFrame *cur;
  1203. if (q->session)
  1204. MFXVideoENCODE_Close(q->session);
  1205. if (q->internal_session)
  1206. MFXClose(q->internal_session);
  1207. q->session = NULL;
  1208. q->internal_session = NULL;
  1209. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  1210. av_buffer_unref(&q->frames_ctx.mids_buf);
  1211. cur = q->work_frames;
  1212. while (cur) {
  1213. q->work_frames = cur->next;
  1214. av_frame_free(&cur->frame);
  1215. av_free(cur->enc_ctrl.Payload);
  1216. av_freep(&cur);
  1217. cur = q->work_frames;
  1218. }
  1219. while (q->async_fifo && av_fifo_size(q->async_fifo)) {
  1220. AVPacket pkt;
  1221. mfxSyncPoint *sync;
  1222. mfxBitstream *bs;
  1223. av_fifo_generic_read(q->async_fifo, &pkt, sizeof(pkt), NULL);
  1224. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  1225. av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
  1226. av_freep(&sync);
  1227. av_freep(&bs);
  1228. av_packet_unref(&pkt);
  1229. }
  1230. av_fifo_free(q->async_fifo);
  1231. q->async_fifo = NULL;
  1232. av_freep(&q->opaque_surfaces);
  1233. av_buffer_unref(&q->opaque_alloc_buf);
  1234. av_freep(&q->extparam);
  1235. return 0;
  1236. }