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.

757 lines
25KB

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