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.

624 lines
21KB

  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. int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx,
  26. VAAPIDecodePicture *pic,
  27. int type,
  28. const void *data,
  29. size_t size)
  30. {
  31. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  32. VAStatus vas;
  33. VABufferID buffer;
  34. av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
  35. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  36. type, size, 1, (void*)data, &buffer);
  37. if (vas != VA_STATUS_SUCCESS) {
  38. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
  39. "buffer (type %d): %d (%s).\n",
  40. type, vas, vaErrorStr(vas));
  41. return AVERROR(EIO);
  42. }
  43. pic->param_buffers[pic->nb_param_buffers++] = buffer;
  44. av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) "
  45. "is %#x.\n", type, size, buffer);
  46. return 0;
  47. }
  48. int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx,
  49. VAAPIDecodePicture *pic,
  50. const void *params_data,
  51. size_t params_size,
  52. const void *slice_data,
  53. size_t slice_size)
  54. {
  55. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  56. VAStatus vas;
  57. int index;
  58. av_assert0(pic->nb_slices <= pic->slices_allocated);
  59. if (pic->nb_slices == pic->slices_allocated) {
  60. if (pic->slices_allocated > 0)
  61. pic->slices_allocated *= 2;
  62. else
  63. pic->slices_allocated = 64;
  64. pic->slice_buffers =
  65. av_realloc_array(pic->slice_buffers,
  66. pic->slices_allocated,
  67. 2 * sizeof(*pic->slice_buffers));
  68. if (!pic->slice_buffers)
  69. return AVERROR(ENOMEM);
  70. }
  71. av_assert0(pic->nb_slices + 1 <= pic->slices_allocated);
  72. index = 2 * pic->nb_slices;
  73. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  74. VASliceParameterBufferType,
  75. params_size, 1, (void*)params_data,
  76. &pic->slice_buffers[index]);
  77. if (vas != VA_STATUS_SUCCESS) {
  78. av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
  79. "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
  80. return AVERROR(EIO);
  81. }
  82. av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) "
  83. "is %#x.\n", pic->nb_slices, params_size,
  84. pic->slice_buffers[index]);
  85. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  86. VASliceDataBufferType,
  87. slice_size, 1, (void*)slice_data,
  88. &pic->slice_buffers[index + 1]);
  89. if (vas != VA_STATUS_SUCCESS) {
  90. av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
  91. "data buffer (size %zu): %d (%s).\n",
  92. slice_size, vas, vaErrorStr(vas));
  93. vaDestroyBuffer(ctx->hwctx->display,
  94. pic->slice_buffers[index]);
  95. return AVERROR(EIO);
  96. }
  97. av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) "
  98. "is %#x.\n", pic->nb_slices, slice_size,
  99. pic->slice_buffers[index + 1]);
  100. ++pic->nb_slices;
  101. return 0;
  102. }
  103. static void ff_vaapi_decode_destroy_buffers(AVCodecContext *avctx,
  104. VAAPIDecodePicture *pic)
  105. {
  106. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  107. VAStatus vas;
  108. int i;
  109. for (i = 0; i < pic->nb_param_buffers; i++) {
  110. vas = vaDestroyBuffer(ctx->hwctx->display,
  111. pic->param_buffers[i]);
  112. if (vas != VA_STATUS_SUCCESS) {
  113. av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
  114. "parameter buffer %#x: %d (%s).\n",
  115. pic->param_buffers[i], vas, vaErrorStr(vas));
  116. }
  117. }
  118. for (i = 0; i < 2 * pic->nb_slices; i++) {
  119. vas = vaDestroyBuffer(ctx->hwctx->display,
  120. pic->slice_buffers[i]);
  121. if (vas != VA_STATUS_SUCCESS) {
  122. av_log(avctx, AV_LOG_ERROR, "Failed to destroy slice "
  123. "slice buffer %#x: %d (%s).\n",
  124. pic->slice_buffers[i], vas, vaErrorStr(vas));
  125. }
  126. }
  127. }
  128. int ff_vaapi_decode_issue(AVCodecContext *avctx,
  129. VAAPIDecodePicture *pic)
  130. {
  131. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  132. VAStatus vas;
  133. int err;
  134. av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
  135. pic->output_surface);
  136. vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
  137. pic->output_surface);
  138. if (vas != VA_STATUS_SUCCESS) {
  139. av_log(avctx, AV_LOG_ERROR, "Failed to begin picture decode "
  140. "issue: %d (%s).\n", vas, vaErrorStr(vas));
  141. err = AVERROR(EIO);
  142. goto fail_with_picture;
  143. }
  144. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  145. pic->param_buffers, pic->nb_param_buffers);
  146. if (vas != VA_STATUS_SUCCESS) {
  147. av_log(avctx, AV_LOG_ERROR, "Failed to upload decode "
  148. "parameters: %d (%s).\n", vas, vaErrorStr(vas));
  149. err = AVERROR(EIO);
  150. goto fail_with_picture;
  151. }
  152. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  153. pic->slice_buffers, 2 * pic->nb_slices);
  154. if (vas != VA_STATUS_SUCCESS) {
  155. av_log(avctx, AV_LOG_ERROR, "Failed to upload slices: "
  156. "%d (%s).\n", vas, vaErrorStr(vas));
  157. err = AVERROR(EIO);
  158. goto fail_with_picture;
  159. }
  160. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  161. if (vas != VA_STATUS_SUCCESS) {
  162. av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
  163. "issue: %d (%s).\n", vas, vaErrorStr(vas));
  164. err = AVERROR(EIO);
  165. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  166. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
  167. goto fail;
  168. else
  169. goto fail_at_end;
  170. }
  171. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  172. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
  173. ff_vaapi_decode_destroy_buffers(avctx, pic);
  174. pic->nb_param_buffers = 0;
  175. pic->nb_slices = 0;
  176. pic->slices_allocated = 0;
  177. av_freep(&pic->slice_buffers);
  178. return 0;
  179. fail_with_picture:
  180. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  181. if (vas != VA_STATUS_SUCCESS) {
  182. av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
  183. "after error: %d (%s).\n", vas, vaErrorStr(vas));
  184. }
  185. fail:
  186. ff_vaapi_decode_destroy_buffers(avctx, pic);
  187. fail_at_end:
  188. return err;
  189. }
  190. int ff_vaapi_decode_cancel(AVCodecContext *avctx,
  191. VAAPIDecodePicture *pic)
  192. {
  193. ff_vaapi_decode_destroy_buffers(avctx, pic);
  194. pic->nb_param_buffers = 0;
  195. pic->nb_slices = 0;
  196. pic->slices_allocated = 0;
  197. av_freep(&pic->slice_buffers);
  198. return 0;
  199. }
  200. static const struct {
  201. enum AVCodecID codec_id;
  202. int codec_profile;
  203. VAProfile va_profile;
  204. } vaapi_profile_map[] = {
  205. #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
  206. MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
  207. MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
  208. MAP(H263, UNKNOWN, H263Baseline),
  209. MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
  210. MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
  211. MPEG4AdvancedSimple),
  212. MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
  213. MAP(H264, H264_CONSTRAINED_BASELINE,
  214. H264ConstrainedBaseline),
  215. MAP(H264, H264_MAIN, H264Main ),
  216. MAP(H264, H264_HIGH, H264High ),
  217. #if VA_CHECK_VERSION(0, 37, 0)
  218. MAP(HEVC, HEVC_MAIN, HEVCMain ),
  219. MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ),
  220. #endif
  221. MAP(WMV3, VC1_SIMPLE, VC1Simple ),
  222. MAP(WMV3, VC1_MAIN, VC1Main ),
  223. MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
  224. MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
  225. MAP(VC1, VC1_SIMPLE, VC1Simple ),
  226. MAP(VC1, VC1_MAIN, VC1Main ),
  227. MAP(VC1, VC1_COMPLEX, VC1Advanced ),
  228. MAP(VC1, VC1_ADVANCED, VC1Advanced ),
  229. #if VA_CHECK_VERSION(0, 35, 0)
  230. MAP(VP8, UNKNOWN, VP8Version0_3 ),
  231. #endif
  232. #if VA_CHECK_VERSION(0, 38, 0)
  233. MAP(VP9, VP9_0, VP9Profile0 ),
  234. #endif
  235. #if VA_CHECK_VERSION(0, 39, 0)
  236. MAP(VP9, VP9_2, VP9Profile2 ),
  237. #endif
  238. #undef MAP
  239. };
  240. /*
  241. * Set *va_config and the frames_ref fields from the current codec parameters
  242. * in avctx.
  243. */
  244. static int vaapi_decode_make_config(AVCodecContext *avctx,
  245. AVBufferRef *device_ref,
  246. VAConfigID *va_config,
  247. AVBufferRef *frames_ref)
  248. {
  249. AVVAAPIHWConfig *hwconfig = NULL;
  250. AVHWFramesConstraints *constraints = NULL;
  251. VAStatus vas;
  252. int err, i, j;
  253. const AVCodecDescriptor *codec_desc;
  254. VAProfile profile, va_profile, *profile_list = NULL;
  255. int profile_count, exact_match, alt_profile;
  256. const AVPixFmtDescriptor *sw_desc, *desc;
  257. AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data;
  258. AVVAAPIDeviceContext *hwctx = device->hwctx;
  259. codec_desc = avcodec_descriptor_get(avctx->codec_id);
  260. if (!codec_desc) {
  261. err = AVERROR(EINVAL);
  262. goto fail;
  263. }
  264. profile_count = vaMaxNumProfiles(hwctx->display);
  265. profile_list = av_malloc_array(profile_count,
  266. sizeof(VAProfile));
  267. if (!profile_list) {
  268. err = AVERROR(ENOMEM);
  269. goto fail;
  270. }
  271. vas = vaQueryConfigProfiles(hwctx->display,
  272. profile_list, &profile_count);
  273. if (vas != VA_STATUS_SUCCESS) {
  274. av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
  275. "%d (%s).\n", vas, vaErrorStr(vas));
  276. err = AVERROR(ENOSYS);
  277. goto fail;
  278. }
  279. profile = VAProfileNone;
  280. exact_match = 0;
  281. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
  282. int profile_match = 0;
  283. if (avctx->codec_id != vaapi_profile_map[i].codec_id)
  284. continue;
  285. if (avctx->profile == vaapi_profile_map[i].codec_profile)
  286. profile_match = 1;
  287. profile = vaapi_profile_map[i].va_profile;
  288. for (j = 0; j < profile_count; j++) {
  289. if (profile == profile_list[j]) {
  290. exact_match = profile_match;
  291. break;
  292. }
  293. }
  294. if (j < profile_count) {
  295. if (exact_match)
  296. break;
  297. alt_profile = vaapi_profile_map[i].codec_profile;
  298. va_profile = vaapi_profile_map[i].va_profile;
  299. }
  300. }
  301. av_freep(&profile_list);
  302. if (profile == VAProfileNone) {
  303. av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
  304. "profile %d.\n", codec_desc->name, avctx->profile);
  305. err = AVERROR(ENOSYS);
  306. goto fail;
  307. }
  308. if (!exact_match) {
  309. if (avctx->hwaccel_flags &
  310. AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
  311. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  312. "supported for hardware decode.\n",
  313. codec_desc->name, avctx->profile);
  314. av_log(avctx, AV_LOG_WARNING, "Using possibly-"
  315. "incompatible profile %d instead.\n",
  316. alt_profile);
  317. profile = va_profile;
  318. } else {
  319. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  320. "supported for hardware decode.\n",
  321. codec_desc->name, avctx->profile);
  322. err = AVERROR(EINVAL);
  323. goto fail;
  324. }
  325. }
  326. vas = vaCreateConfig(hwctx->display, profile,
  327. VAEntrypointVLD, NULL, 0,
  328. va_config);
  329. if (vas != VA_STATUS_SUCCESS) {
  330. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  331. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  332. err = AVERROR(EIO);
  333. goto fail;
  334. }
  335. hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
  336. if (!hwconfig) {
  337. err = AVERROR(ENOMEM);
  338. goto fail;
  339. }
  340. hwconfig->config_id = *va_config;
  341. constraints =
  342. av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
  343. if (!constraints) {
  344. err = AVERROR(ENOMEM);
  345. goto fail;
  346. }
  347. if (avctx->coded_width < constraints->min_width ||
  348. avctx->coded_height < constraints->min_height ||
  349. avctx->coded_width > constraints->max_width ||
  350. avctx->coded_height > constraints->max_height) {
  351. av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
  352. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  353. avctx->coded_width, avctx->coded_height,
  354. constraints->min_width, constraints->max_width,
  355. constraints->min_height, constraints->max_height);
  356. err = AVERROR(EINVAL);
  357. goto fail;
  358. }
  359. if (!constraints->valid_sw_formats ||
  360. constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
  361. av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
  362. "usable surface formats.\n");
  363. err = AVERROR(EINVAL);
  364. goto fail;
  365. }
  366. if (frames_ref) {
  367. AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
  368. frames->format = AV_PIX_FMT_VAAPI;
  369. frames->width = avctx->coded_width;
  370. frames->height = avctx->coded_height;
  371. // Find the first format in the list which matches the expected
  372. // bit depth and subsampling. If none are found (this can happen
  373. // when 10-bit streams are decoded to 8-bit surfaces, for example)
  374. // then just take the first format on the list.
  375. frames->sw_format = constraints->valid_sw_formats[0];
  376. sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  377. for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
  378. desc = av_pix_fmt_desc_get(constraints->valid_sw_formats[i]);
  379. if (desc->nb_components != sw_desc->nb_components ||
  380. desc->log2_chroma_w != sw_desc->log2_chroma_w ||
  381. desc->log2_chroma_h != sw_desc->log2_chroma_h)
  382. continue;
  383. for (j = 0; j < desc->nb_components; j++) {
  384. if (desc->comp[j].depth != sw_desc->comp[j].depth)
  385. break;
  386. }
  387. if (j < desc->nb_components)
  388. continue;
  389. frames->sw_format = constraints->valid_sw_formats[i];
  390. break;
  391. }
  392. frames->initial_pool_size = 1;
  393. // Add per-codec number of surfaces used for storing reference frames.
  394. switch (avctx->codec_id) {
  395. case AV_CODEC_ID_H264:
  396. case AV_CODEC_ID_HEVC:
  397. frames->initial_pool_size += 16;
  398. break;
  399. case AV_CODEC_ID_VP9:
  400. frames->initial_pool_size += 8;
  401. break;
  402. case AV_CODEC_ID_VP8:
  403. frames->initial_pool_size += 3;
  404. break;
  405. default:
  406. frames->initial_pool_size += 2;
  407. }
  408. }
  409. av_hwframe_constraints_free(&constraints);
  410. av_freep(&hwconfig);
  411. return 0;
  412. fail:
  413. av_hwframe_constraints_free(&constraints);
  414. av_freep(&hwconfig);
  415. if (*va_config != VA_INVALID_ID) {
  416. vaDestroyConfig(hwctx->display, *va_config);
  417. *va_config = VA_INVALID_ID;
  418. }
  419. av_freep(&profile_list);
  420. return err;
  421. }
  422. int ff_vaapi_common_frame_params(AVCodecContext *avctx,
  423. AVBufferRef *hw_frames_ctx)
  424. {
  425. AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
  426. AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
  427. AVVAAPIDeviceContext *hwctx;
  428. VAConfigID va_config = VA_INVALID_ID;
  429. int err;
  430. if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
  431. return AVERROR(EINVAL);
  432. hwctx = device_ctx->hwctx;
  433. err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
  434. hw_frames_ctx);
  435. if (err)
  436. return err;
  437. if (va_config != VA_INVALID_ID)
  438. vaDestroyConfig(hwctx->display, va_config);
  439. return 0;
  440. }
  441. int ff_vaapi_decode_init(AVCodecContext *avctx)
  442. {
  443. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  444. VAStatus vas;
  445. int err;
  446. ctx->va_config = VA_INVALID_ID;
  447. ctx->va_context = VA_INVALID_ID;
  448. #if FF_API_STRUCT_VAAPI_CONTEXT
  449. if (avctx->hwaccel_context) {
  450. av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
  451. "vaapi_context in decode.\n");
  452. ctx->have_old_context = 1;
  453. ctx->old_context = avctx->hwaccel_context;
  454. // Really we only want the VAAPI device context, but this
  455. // allocates a whole generic device context because we don't
  456. // have any other way to determine how big it should be.
  457. ctx->device_ref =
  458. av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
  459. if (!ctx->device_ref) {
  460. err = AVERROR(ENOMEM);
  461. goto fail;
  462. }
  463. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  464. ctx->hwctx = ctx->device->hwctx;
  465. ctx->hwctx->display = ctx->old_context->display;
  466. // The old VAAPI decode setup assumed this quirk was always
  467. // present, so set it here to avoid the behaviour changing.
  468. ctx->hwctx->driver_quirks =
  469. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  470. }
  471. #endif
  472. #if FF_API_STRUCT_VAAPI_CONTEXT
  473. if (ctx->have_old_context) {
  474. ctx->va_config = ctx->old_context->config_id;
  475. ctx->va_context = ctx->old_context->context_id;
  476. av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
  477. "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
  478. } else {
  479. #endif
  480. err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
  481. if (err < 0)
  482. goto fail;
  483. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  484. ctx->hwfc = ctx->frames->hwctx;
  485. ctx->device = ctx->frames->device_ctx;
  486. ctx->hwctx = ctx->device->hwctx;
  487. err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
  488. &ctx->va_config, avctx->hw_frames_ctx);
  489. if (err)
  490. goto fail;
  491. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  492. avctx->coded_width, avctx->coded_height,
  493. VA_PROGRESSIVE,
  494. ctx->hwfc->surface_ids,
  495. ctx->hwfc->nb_surfaces,
  496. &ctx->va_context);
  497. if (vas != VA_STATUS_SUCCESS) {
  498. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  499. "context: %d (%s).\n", vas, vaErrorStr(vas));
  500. err = AVERROR(EIO);
  501. goto fail;
  502. }
  503. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  504. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  505. #if FF_API_STRUCT_VAAPI_CONTEXT
  506. }
  507. #endif
  508. return 0;
  509. fail:
  510. ff_vaapi_decode_uninit(avctx);
  511. return err;
  512. }
  513. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  514. {
  515. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  516. VAStatus vas;
  517. #if FF_API_STRUCT_VAAPI_CONTEXT
  518. if (ctx->have_old_context) {
  519. av_buffer_unref(&ctx->device_ref);
  520. } else {
  521. #endif
  522. if (ctx->va_context != VA_INVALID_ID) {
  523. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  524. if (vas != VA_STATUS_SUCCESS) {
  525. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  526. "context %#x: %d (%s).\n",
  527. ctx->va_context, vas, vaErrorStr(vas));
  528. }
  529. }
  530. if (ctx->va_config != VA_INVALID_ID) {
  531. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  532. if (vas != VA_STATUS_SUCCESS) {
  533. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  534. "configuration %#x: %d (%s).\n",
  535. ctx->va_config, vas, vaErrorStr(vas));
  536. }
  537. }
  538. #if FF_API_STRUCT_VAAPI_CONTEXT
  539. }
  540. #endif
  541. return 0;
  542. }