vaapi_decode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config_components.h"
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/common.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "avcodec.h"
  23. #include "decode.h"
  24. #include "internal.h"
  25. #include "vaapi_decode.h"
  26. #include "vaapi_hevc.h"
  27. int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx,
  28. VAAPIDecodePicture *pic,
  29. int type,
  30. const void *data,
  31. size_t size)
  32. {
  33. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  34. VAStatus vas;
  35. VABufferID buffer;
  36. av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
  37. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  38. type, size, 1, (void*)data, &buffer);
  39. if (vas != VA_STATUS_SUCCESS) {
  40. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
  41. "buffer (type %d): %d (%s).\n",
  42. type, vas, vaErrorStr(vas));
  43. return AVERROR(EIO);
  44. }
  45. pic->param_buffers[pic->nb_param_buffers++] = buffer;
  46. av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) "
  47. "is %#x.\n", type, size, buffer);
  48. return 0;
  49. }
  50. int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx,
  51. VAAPIDecodePicture *pic,
  52. const void *params_data,
  53. size_t params_size,
  54. const void *slice_data,
  55. size_t slice_size)
  56. {
  57. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  58. VAStatus vas;
  59. int index;
  60. av_assert0(pic->nb_slices <= pic->slices_allocated);
  61. if (pic->nb_slices == pic->slices_allocated) {
  62. if (pic->slices_allocated > 0)
  63. pic->slices_allocated *= 2;
  64. else
  65. pic->slices_allocated = 64;
  66. pic->slice_buffers =
  67. av_realloc_array(pic->slice_buffers,
  68. pic->slices_allocated,
  69. 2 * sizeof(*pic->slice_buffers));
  70. if (!pic->slice_buffers)
  71. return AVERROR(ENOMEM);
  72. }
  73. av_assert0(pic->nb_slices + 1 <= pic->slices_allocated);
  74. index = 2 * pic->nb_slices;
  75. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  76. VASliceParameterBufferType,
  77. params_size, 1, (void*)params_data,
  78. &pic->slice_buffers[index]);
  79. if (vas != VA_STATUS_SUCCESS) {
  80. av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
  81. "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
  82. return AVERROR(EIO);
  83. }
  84. av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) "
  85. "is %#x.\n", pic->nb_slices, params_size,
  86. pic->slice_buffers[index]);
  87. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  88. VASliceDataBufferType,
  89. slice_size, 1, (void*)slice_data,
  90. &pic->slice_buffers[index + 1]);
  91. if (vas != VA_STATUS_SUCCESS) {
  92. av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
  93. "data buffer (size %zu): %d (%s).\n",
  94. slice_size, vas, vaErrorStr(vas));
  95. vaDestroyBuffer(ctx->hwctx->display,
  96. pic->slice_buffers[index]);
  97. return AVERROR(EIO);
  98. }
  99. av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) "
  100. "is %#x.\n", pic->nb_slices, slice_size,
  101. pic->slice_buffers[index + 1]);
  102. ++pic->nb_slices;
  103. return 0;
  104. }
  105. static void ff_vaapi_decode_destroy_buffers(AVCodecContext *avctx,
  106. VAAPIDecodePicture *pic)
  107. {
  108. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  109. VAStatus vas;
  110. int i;
  111. for (i = 0; i < pic->nb_param_buffers; i++) {
  112. vas = vaDestroyBuffer(ctx->hwctx->display,
  113. pic->param_buffers[i]);
  114. if (vas != VA_STATUS_SUCCESS) {
  115. av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
  116. "parameter buffer %#x: %d (%s).\n",
  117. pic->param_buffers[i], vas, vaErrorStr(vas));
  118. }
  119. }
  120. for (i = 0; i < 2 * pic->nb_slices; i++) {
  121. vas = vaDestroyBuffer(ctx->hwctx->display,
  122. pic->slice_buffers[i]);
  123. if (vas != VA_STATUS_SUCCESS) {
  124. av_log(avctx, AV_LOG_ERROR, "Failed to destroy slice "
  125. "slice buffer %#x: %d (%s).\n",
  126. pic->slice_buffers[i], vas, vaErrorStr(vas));
  127. }
  128. }
  129. }
  130. int ff_vaapi_decode_issue(AVCodecContext *avctx,
  131. VAAPIDecodePicture *pic)
  132. {
  133. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  134. VAStatus vas;
  135. int err;
  136. av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
  137. pic->output_surface);
  138. vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
  139. pic->output_surface);
  140. if (vas != VA_STATUS_SUCCESS) {
  141. av_log(avctx, AV_LOG_ERROR, "Failed to begin picture decode "
  142. "issue: %d (%s).\n", vas, vaErrorStr(vas));
  143. err = AVERROR(EIO);
  144. goto fail_with_picture;
  145. }
  146. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  147. pic->param_buffers, pic->nb_param_buffers);
  148. if (vas != VA_STATUS_SUCCESS) {
  149. av_log(avctx, AV_LOG_ERROR, "Failed to upload decode "
  150. "parameters: %d (%s).\n", vas, vaErrorStr(vas));
  151. err = AVERROR(EIO);
  152. goto fail_with_picture;
  153. }
  154. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  155. pic->slice_buffers, 2 * pic->nb_slices);
  156. if (vas != VA_STATUS_SUCCESS) {
  157. av_log(avctx, AV_LOG_ERROR, "Failed to upload slices: "
  158. "%d (%s).\n", vas, vaErrorStr(vas));
  159. err = AVERROR(EIO);
  160. goto fail_with_picture;
  161. }
  162. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  163. if (vas != VA_STATUS_SUCCESS) {
  164. av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
  165. "issue: %d (%s).\n", vas, vaErrorStr(vas));
  166. err = AVERROR(EIO);
  167. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  168. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
  169. goto fail;
  170. else
  171. goto fail_at_end;
  172. }
  173. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  174. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
  175. ff_vaapi_decode_destroy_buffers(avctx, pic);
  176. err = 0;
  177. goto exit;
  178. fail_with_picture:
  179. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  180. if (vas != VA_STATUS_SUCCESS) {
  181. av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
  182. "after error: %d (%s).\n", vas, vaErrorStr(vas));
  183. }
  184. fail:
  185. ff_vaapi_decode_destroy_buffers(avctx, pic);
  186. fail_at_end:
  187. exit:
  188. pic->nb_param_buffers = 0;
  189. pic->nb_slices = 0;
  190. pic->slices_allocated = 0;
  191. av_freep(&pic->slice_buffers);
  192. return err;
  193. }
  194. int ff_vaapi_decode_cancel(AVCodecContext *avctx,
  195. VAAPIDecodePicture *pic)
  196. {
  197. ff_vaapi_decode_destroy_buffers(avctx, pic);
  198. pic->nb_param_buffers = 0;
  199. pic->nb_slices = 0;
  200. pic->slices_allocated = 0;
  201. av_freep(&pic->slice_buffers);
  202. return 0;
  203. }
  204. static const struct {
  205. uint32_t fourcc;
  206. enum AVPixelFormat pix_fmt;
  207. } vaapi_format_map[] = {
  208. #define MAP(va, av) { VA_FOURCC_ ## va, AV_PIX_FMT_ ## av }
  209. // 4:0:0
  210. MAP(Y800, GRAY8),
  211. // 4:2:0
  212. MAP(NV12, NV12),
  213. MAP(YV12, YUV420P),
  214. MAP(IYUV, YUV420P),
  215. #ifdef VA_FOURCC_I420
  216. MAP(I420, YUV420P),
  217. #endif
  218. MAP(IMC3, YUV420P),
  219. // 4:1:1
  220. MAP(411P, YUV411P),
  221. // 4:2:2
  222. MAP(422H, YUV422P),
  223. #ifdef VA_FOURCC_YV16
  224. MAP(YV16, YUV422P),
  225. #endif
  226. MAP(YUY2, YUYV422),
  227. #ifdef VA_FOURCC_Y210
  228. MAP(Y210, Y210),
  229. #endif
  230. // 4:4:0
  231. MAP(422V, YUV440P),
  232. // 4:4:4
  233. MAP(444P, YUV444P),
  234. MAP(AYUV, VUYA),
  235. // 4:2:0 10-bit
  236. #ifdef VA_FOURCC_P010
  237. MAP(P010, P010),
  238. #endif
  239. #ifdef VA_FOURCC_I010
  240. MAP(I010, YUV420P10),
  241. #endif
  242. #undef MAP
  243. };
  244. static int vaapi_decode_find_best_format(AVCodecContext *avctx,
  245. AVHWDeviceContext *device,
  246. VAConfigID config_id,
  247. AVHWFramesContext *frames)
  248. {
  249. AVVAAPIDeviceContext *hwctx = device->hwctx;
  250. VAStatus vas;
  251. VASurfaceAttrib *attr;
  252. enum AVPixelFormat source_format, best_format, format;
  253. uint32_t best_fourcc, fourcc;
  254. int i, j, nb_attr;
  255. source_format = avctx->sw_pix_fmt;
  256. av_assert0(source_format != AV_PIX_FMT_NONE);
  257. vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
  258. NULL, &nb_attr);
  259. if (vas != VA_STATUS_SUCCESS) {
  260. av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
  261. "%d (%s).\n", vas, vaErrorStr(vas));
  262. return AVERROR(ENOSYS);
  263. }
  264. attr = av_malloc_array(nb_attr, sizeof(*attr));
  265. if (!attr)
  266. return AVERROR(ENOMEM);
  267. vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
  268. attr, &nb_attr);
  269. if (vas != VA_STATUS_SUCCESS) {
  270. av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
  271. "%d (%s).\n", vas, vaErrorStr(vas));
  272. av_freep(&attr);
  273. return AVERROR(ENOSYS);
  274. }
  275. best_format = AV_PIX_FMT_NONE;
  276. for (i = 0; i < nb_attr; i++) {
  277. if (attr[i].type != VASurfaceAttribPixelFormat)
  278. continue;
  279. fourcc = attr[i].value.value.i;
  280. for (j = 0; j < FF_ARRAY_ELEMS(vaapi_format_map); j++) {
  281. if (fourcc == vaapi_format_map[j].fourcc)
  282. break;
  283. }
  284. if (j >= FF_ARRAY_ELEMS(vaapi_format_map)) {
  285. av_log(avctx, AV_LOG_DEBUG, "Ignoring unknown format %#x.\n",
  286. fourcc);
  287. continue;
  288. }
  289. format = vaapi_format_map[j].pix_fmt;
  290. av_log(avctx, AV_LOG_DEBUG, "Considering format %#x -> %s.\n",
  291. fourcc, av_get_pix_fmt_name(format));
  292. best_format = av_find_best_pix_fmt_of_2(format, best_format,
  293. source_format, 0, NULL);
  294. if (format == best_format)
  295. best_fourcc = fourcc;
  296. }
  297. av_freep(&attr);
  298. if (best_format == AV_PIX_FMT_NONE) {
  299. av_log(avctx, AV_LOG_ERROR, "No usable formats for decoding!\n");
  300. return AVERROR(EINVAL);
  301. }
  302. av_log(avctx, AV_LOG_DEBUG, "Picked %s (%#x) as best match for %s.\n",
  303. av_get_pix_fmt_name(best_format), best_fourcc,
  304. av_get_pix_fmt_name(source_format));
  305. frames->sw_format = best_format;
  306. if (avctx->internal->hwaccel_priv_data) {
  307. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  308. AVVAAPIFramesContext *avfc = frames->hwctx;
  309. ctx->pixel_format_attribute = (VASurfaceAttrib) {
  310. .type = VASurfaceAttribPixelFormat,
  311. .value.value.i = best_fourcc,
  312. };
  313. avfc->attributes = &ctx->pixel_format_attribute;
  314. avfc->nb_attributes = 1;
  315. }
  316. return 0;
  317. }
  318. static const struct {
  319. enum AVCodecID codec_id;
  320. int codec_profile;
  321. VAProfile va_profile;
  322. VAProfile (*profile_parser)(AVCodecContext *avctx);
  323. } vaapi_profile_map[] = {
  324. #define MAP(c, p, v, ...) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v, __VA_ARGS__ }
  325. MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
  326. MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
  327. MAP(H263, UNKNOWN, H263Baseline),
  328. MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
  329. MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
  330. MPEG4AdvancedSimple),
  331. MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
  332. MAP(H264, H264_CONSTRAINED_BASELINE,
  333. H264ConstrainedBaseline),
  334. MAP(H264, H264_MAIN, H264Main ),
  335. MAP(H264, H264_HIGH, H264High ),
  336. #if VA_CHECK_VERSION(0, 37, 0)
  337. MAP(HEVC, HEVC_MAIN, HEVCMain ),
  338. MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ),
  339. MAP(HEVC, HEVC_MAIN_STILL_PICTURE,
  340. HEVCMain ),
  341. #endif
  342. #if VA_CHECK_VERSION(1, 2, 0) && CONFIG_HEVC_VAAPI_HWACCEL
  343. MAP(HEVC, HEVC_REXT, None,
  344. ff_vaapi_parse_hevc_rext_profile ),
  345. #endif
  346. MAP(MJPEG, MJPEG_HUFFMAN_BASELINE_DCT,
  347. JPEGBaseline),
  348. MAP(WMV3, VC1_SIMPLE, VC1Simple ),
  349. MAP(WMV3, VC1_MAIN, VC1Main ),
  350. MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
  351. MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
  352. MAP(VC1, VC1_SIMPLE, VC1Simple ),
  353. MAP(VC1, VC1_MAIN, VC1Main ),
  354. MAP(VC1, VC1_COMPLEX, VC1Advanced ),
  355. MAP(VC1, VC1_ADVANCED, VC1Advanced ),
  356. MAP(VP8, UNKNOWN, VP8Version0_3 ),
  357. #if VA_CHECK_VERSION(0, 38, 0)
  358. MAP(VP9, VP9_0, VP9Profile0 ),
  359. #endif
  360. #if VA_CHECK_VERSION(0, 39, 0)
  361. MAP(VP9, VP9_1, VP9Profile1 ),
  362. MAP(VP9, VP9_2, VP9Profile2 ),
  363. #endif
  364. #if VA_CHECK_VERSION(1, 8, 0)
  365. MAP(AV1, AV1_MAIN, AV1Profile0),
  366. MAP(AV1, AV1_HIGH, AV1Profile1),
  367. #endif
  368. #undef MAP
  369. };
  370. /*
  371. * Set *va_config and the frames_ref fields from the current codec parameters
  372. * in avctx.
  373. */
  374. static int vaapi_decode_make_config(AVCodecContext *avctx,
  375. AVBufferRef *device_ref,
  376. VAConfigID *va_config,
  377. AVBufferRef *frames_ref)
  378. {
  379. AVVAAPIHWConfig *hwconfig = NULL;
  380. AVHWFramesConstraints *constraints = NULL;
  381. VAStatus vas;
  382. int err, i, j;
  383. const AVCodecDescriptor *codec_desc;
  384. VAProfile *profile_list = NULL, matched_va_profile, va_profile;
  385. int profile_count, exact_match, matched_ff_profile, codec_profile;
  386. AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data;
  387. AVVAAPIDeviceContext *hwctx = device->hwctx;
  388. codec_desc = avcodec_descriptor_get(avctx->codec_id);
  389. if (!codec_desc) {
  390. err = AVERROR(EINVAL);
  391. goto fail;
  392. }
  393. profile_count = vaMaxNumProfiles(hwctx->display);
  394. profile_list = av_malloc_array(profile_count,
  395. sizeof(VAProfile));
  396. if (!profile_list) {
  397. err = AVERROR(ENOMEM);
  398. goto fail;
  399. }
  400. vas = vaQueryConfigProfiles(hwctx->display,
  401. profile_list, &profile_count);
  402. if (vas != VA_STATUS_SUCCESS) {
  403. av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
  404. "%d (%s).\n", vas, vaErrorStr(vas));
  405. err = AVERROR(ENOSYS);
  406. goto fail;
  407. }
  408. matched_va_profile = VAProfileNone;
  409. exact_match = 0;
  410. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
  411. int profile_match = 0;
  412. if (avctx->codec_id != vaapi_profile_map[i].codec_id)
  413. continue;
  414. if (avctx->profile == vaapi_profile_map[i].codec_profile ||
  415. vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
  416. profile_match = 1;
  417. va_profile = vaapi_profile_map[i].profile_parser ?
  418. vaapi_profile_map[i].profile_parser(avctx) :
  419. vaapi_profile_map[i].va_profile;
  420. codec_profile = vaapi_profile_map[i].codec_profile;
  421. for (j = 0; j < profile_count; j++) {
  422. if (va_profile == profile_list[j]) {
  423. exact_match = profile_match;
  424. break;
  425. }
  426. }
  427. if (j < profile_count) {
  428. matched_va_profile = va_profile;
  429. matched_ff_profile = codec_profile;
  430. if (exact_match)
  431. break;
  432. }
  433. }
  434. av_freep(&profile_list);
  435. if (matched_va_profile == VAProfileNone) {
  436. av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
  437. "profile %d.\n", codec_desc->name, avctx->profile);
  438. err = AVERROR(ENOSYS);
  439. goto fail;
  440. }
  441. if (!exact_match) {
  442. if (avctx->hwaccel_flags &
  443. AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
  444. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  445. "supported for hardware decode.\n",
  446. codec_desc->name, avctx->profile);
  447. av_log(avctx, AV_LOG_WARNING, "Using possibly-"
  448. "incompatible profile %d instead.\n",
  449. matched_ff_profile);
  450. } else {
  451. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  452. "supported for hardware decode.\n",
  453. codec_desc->name, avctx->profile);
  454. err = AVERROR(EINVAL);
  455. goto fail;
  456. }
  457. }
  458. vas = vaCreateConfig(hwctx->display, matched_va_profile,
  459. VAEntrypointVLD, NULL, 0,
  460. va_config);
  461. if (vas != VA_STATUS_SUCCESS) {
  462. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  463. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  464. err = AVERROR(EIO);
  465. goto fail;
  466. }
  467. hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
  468. if (!hwconfig) {
  469. err = AVERROR(ENOMEM);
  470. goto fail;
  471. }
  472. hwconfig->config_id = *va_config;
  473. constraints =
  474. av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
  475. if (!constraints) {
  476. err = AVERROR(ENOMEM);
  477. goto fail;
  478. }
  479. if (avctx->coded_width < constraints->min_width ||
  480. avctx->coded_height < constraints->min_height ||
  481. avctx->coded_width > constraints->max_width ||
  482. avctx->coded_height > constraints->max_height) {
  483. av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
  484. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  485. avctx->coded_width, avctx->coded_height,
  486. constraints->min_width, constraints->max_width,
  487. constraints->min_height, constraints->max_height);
  488. err = AVERROR(EINVAL);
  489. goto fail;
  490. }
  491. if (!constraints->valid_sw_formats ||
  492. constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
  493. av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
  494. "usable surface formats.\n");
  495. err = AVERROR(EINVAL);
  496. goto fail;
  497. }
  498. if (frames_ref) {
  499. AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
  500. frames->format = AV_PIX_FMT_VAAPI;
  501. frames->width = avctx->coded_width;
  502. frames->height = avctx->coded_height;
  503. err = vaapi_decode_find_best_format(avctx, device,
  504. *va_config, frames);
  505. if (err < 0)
  506. goto fail;
  507. frames->initial_pool_size = 1;
  508. // Add per-codec number of surfaces used for storing reference frames.
  509. switch (avctx->codec_id) {
  510. case AV_CODEC_ID_H264:
  511. case AV_CODEC_ID_HEVC:
  512. case AV_CODEC_ID_AV1:
  513. frames->initial_pool_size += 16;
  514. break;
  515. case AV_CODEC_ID_VP9:
  516. frames->initial_pool_size += 8;
  517. break;
  518. case AV_CODEC_ID_VP8:
  519. frames->initial_pool_size += 3;
  520. break;
  521. default:
  522. frames->initial_pool_size += 2;
  523. }
  524. }
  525. av_hwframe_constraints_free(&constraints);
  526. av_freep(&hwconfig);
  527. return 0;
  528. fail:
  529. av_hwframe_constraints_free(&constraints);
  530. av_freep(&hwconfig);
  531. if (*va_config != VA_INVALID_ID) {
  532. vaDestroyConfig(hwctx->display, *va_config);
  533. *va_config = VA_INVALID_ID;
  534. }
  535. av_freep(&profile_list);
  536. return err;
  537. }
  538. int ff_vaapi_common_frame_params(AVCodecContext *avctx,
  539. AVBufferRef *hw_frames_ctx)
  540. {
  541. AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
  542. AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
  543. AVVAAPIDeviceContext *hwctx;
  544. VAConfigID va_config = VA_INVALID_ID;
  545. int err;
  546. if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
  547. return AVERROR(EINVAL);
  548. hwctx = device_ctx->hwctx;
  549. err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
  550. hw_frames_ctx);
  551. if (err)
  552. return err;
  553. if (va_config != VA_INVALID_ID)
  554. vaDestroyConfig(hwctx->display, va_config);
  555. return 0;
  556. }
  557. int ff_vaapi_decode_init(AVCodecContext *avctx)
  558. {
  559. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  560. VAStatus vas;
  561. int err;
  562. ctx->va_config = VA_INVALID_ID;
  563. ctx->va_context = VA_INVALID_ID;
  564. err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
  565. if (err < 0)
  566. goto fail;
  567. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  568. ctx->hwfc = ctx->frames->hwctx;
  569. ctx->device = ctx->frames->device_ctx;
  570. ctx->hwctx = ctx->device->hwctx;
  571. err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
  572. &ctx->va_config, NULL);
  573. if (err)
  574. goto fail;
  575. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  576. avctx->coded_width, avctx->coded_height,
  577. VA_PROGRESSIVE,
  578. ctx->hwfc->surface_ids,
  579. ctx->hwfc->nb_surfaces,
  580. &ctx->va_context);
  581. if (vas != VA_STATUS_SUCCESS) {
  582. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  583. "context: %d (%s).\n", vas, vaErrorStr(vas));
  584. err = AVERROR(EIO);
  585. goto fail;
  586. }
  587. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  588. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  589. return 0;
  590. fail:
  591. ff_vaapi_decode_uninit(avctx);
  592. return err;
  593. }
  594. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  595. {
  596. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  597. VAStatus vas;
  598. if (ctx->va_context != VA_INVALID_ID) {
  599. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  600. if (vas != VA_STATUS_SUCCESS) {
  601. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  602. "context %#x: %d (%s).\n",
  603. ctx->va_context, vas, vaErrorStr(vas));
  604. }
  605. }
  606. if (ctx->va_config != VA_INVALID_ID) {
  607. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  608. if (vas != VA_STATUS_SUCCESS) {
  609. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  610. "configuration %#x: %d (%s).\n",
  611. ctx->va_config, vas, vaErrorStr(vas));
  612. }
  613. }
  614. return 0;
  615. }