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.

620 lines
21KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 (HAVE_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 (HAVE_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. #undef MAP
  236. };
  237. /*
  238. * Set *va_config and the frames_ref fields from the current codec parameters
  239. * in avctx.
  240. */
  241. static int vaapi_decode_make_config(AVCodecContext *avctx,
  242. AVBufferRef *device_ref,
  243. VAConfigID *va_config,
  244. AVBufferRef *frames_ref)
  245. {
  246. AVVAAPIHWConfig *hwconfig = NULL;
  247. AVHWFramesConstraints *constraints = NULL;
  248. VAStatus vas;
  249. int err, i, j;
  250. const AVCodecDescriptor *codec_desc;
  251. VAProfile *profile_list = NULL, matched_va_profile;
  252. int profile_count, exact_match, matched_ff_profile;
  253. const AVPixFmtDescriptor *sw_desc, *desc;
  254. AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data;
  255. AVVAAPIDeviceContext *hwctx = device->hwctx;
  256. codec_desc = avcodec_descriptor_get(avctx->codec_id);
  257. if (!codec_desc) {
  258. err = AVERROR(EINVAL);
  259. goto fail;
  260. }
  261. profile_count = vaMaxNumProfiles(hwctx->display);
  262. profile_list = av_malloc_array(profile_count,
  263. sizeof(VAProfile));
  264. if (!profile_list) {
  265. err = AVERROR(ENOMEM);
  266. goto fail;
  267. }
  268. vas = vaQueryConfigProfiles(hwctx->display,
  269. profile_list, &profile_count);
  270. if (vas != VA_STATUS_SUCCESS) {
  271. av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
  272. "%d (%s).\n", vas, vaErrorStr(vas));
  273. err = AVERROR(ENOSYS);
  274. goto fail;
  275. }
  276. matched_va_profile = VAProfileNone;
  277. exact_match = 0;
  278. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
  279. int profile_match = 0;
  280. if (avctx->codec_id != vaapi_profile_map[i].codec_id)
  281. continue;
  282. if (avctx->profile == vaapi_profile_map[i].codec_profile ||
  283. vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
  284. profile_match = 1;
  285. for (j = 0; j < profile_count; j++) {
  286. if (vaapi_profile_map[i].va_profile == profile_list[j]) {
  287. exact_match = profile_match;
  288. break;
  289. }
  290. }
  291. if (j < profile_count) {
  292. matched_va_profile = vaapi_profile_map[i].va_profile;
  293. matched_ff_profile = vaapi_profile_map[i].codec_profile;
  294. if (exact_match)
  295. break;
  296. }
  297. }
  298. av_freep(&profile_list);
  299. if (matched_va_profile == VAProfileNone) {
  300. av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
  301. "profile %d.\n", codec_desc->name, avctx->profile);
  302. err = AVERROR(ENOSYS);
  303. goto fail;
  304. }
  305. if (!exact_match) {
  306. if (avctx->hwaccel_flags &
  307. AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
  308. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  309. "supported for hardware decode.\n",
  310. codec_desc->name, avctx->profile);
  311. av_log(avctx, AV_LOG_WARNING, "Using possibly-"
  312. "incompatible profile %d instead.\n",
  313. matched_ff_profile);
  314. } else {
  315. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  316. "supported for hardware decode.\n",
  317. codec_desc->name, avctx->profile);
  318. err = AVERROR(EINVAL);
  319. goto fail;
  320. }
  321. }
  322. vas = vaCreateConfig(hwctx->display, matched_va_profile,
  323. VAEntrypointVLD, NULL, 0,
  324. va_config);
  325. if (vas != VA_STATUS_SUCCESS) {
  326. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  327. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  328. err = AVERROR(EIO);
  329. goto fail;
  330. }
  331. hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
  332. if (!hwconfig) {
  333. err = AVERROR(ENOMEM);
  334. goto fail;
  335. }
  336. hwconfig->config_id = *va_config;
  337. constraints =
  338. av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
  339. if (!constraints) {
  340. err = AVERROR(ENOMEM);
  341. goto fail;
  342. }
  343. if (avctx->coded_width < constraints->min_width ||
  344. avctx->coded_height < constraints->min_height ||
  345. avctx->coded_width > constraints->max_width ||
  346. avctx->coded_height > constraints->max_height) {
  347. av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
  348. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  349. avctx->coded_width, avctx->coded_height,
  350. constraints->min_width, constraints->max_width,
  351. constraints->min_height, constraints->max_height);
  352. err = AVERROR(EINVAL);
  353. goto fail;
  354. }
  355. if (!constraints->valid_sw_formats ||
  356. constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
  357. av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
  358. "usable surface formats.\n");
  359. err = AVERROR(EINVAL);
  360. goto fail;
  361. }
  362. if (frames_ref) {
  363. AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
  364. frames->format = AV_PIX_FMT_VAAPI;
  365. frames->width = avctx->coded_width;
  366. frames->height = avctx->coded_height;
  367. // Find the first format in the list which matches the expected
  368. // bit depth and subsampling. If none are found (this can happen
  369. // when 10-bit streams are decoded to 8-bit surfaces, for example)
  370. // then just take the first format on the list.
  371. frames->sw_format = constraints->valid_sw_formats[0];
  372. sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  373. for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
  374. desc = av_pix_fmt_desc_get(constraints->valid_sw_formats[i]);
  375. if (desc->nb_components != sw_desc->nb_components ||
  376. desc->log2_chroma_w != sw_desc->log2_chroma_w ||
  377. desc->log2_chroma_h != sw_desc->log2_chroma_h)
  378. continue;
  379. for (j = 0; j < desc->nb_components; j++) {
  380. if (desc->comp[j].depth != sw_desc->comp[j].depth)
  381. break;
  382. }
  383. if (j < desc->nb_components)
  384. continue;
  385. frames->sw_format = constraints->valid_sw_formats[i];
  386. break;
  387. }
  388. frames->initial_pool_size = 1;
  389. // Add per-codec number of surfaces used for storing reference frames.
  390. switch (avctx->codec_id) {
  391. case AV_CODEC_ID_H264:
  392. case AV_CODEC_ID_HEVC:
  393. frames->initial_pool_size += 16;
  394. break;
  395. case AV_CODEC_ID_VP9:
  396. frames->initial_pool_size += 8;
  397. break;
  398. case AV_CODEC_ID_VP8:
  399. frames->initial_pool_size += 3;
  400. break;
  401. default:
  402. frames->initial_pool_size += 2;
  403. }
  404. }
  405. av_hwframe_constraints_free(&constraints);
  406. av_freep(&hwconfig);
  407. return 0;
  408. fail:
  409. av_hwframe_constraints_free(&constraints);
  410. av_freep(&hwconfig);
  411. if (*va_config != VA_INVALID_ID) {
  412. vaDestroyConfig(hwctx->display, *va_config);
  413. *va_config = VA_INVALID_ID;
  414. }
  415. av_freep(&profile_list);
  416. return err;
  417. }
  418. int ff_vaapi_common_frame_params(AVCodecContext *avctx,
  419. AVBufferRef *hw_frames_ctx)
  420. {
  421. AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
  422. AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
  423. AVVAAPIDeviceContext *hwctx;
  424. VAConfigID va_config = VA_INVALID_ID;
  425. int err;
  426. if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
  427. return AVERROR(EINVAL);
  428. hwctx = device_ctx->hwctx;
  429. err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
  430. hw_frames_ctx);
  431. if (err)
  432. return err;
  433. if (va_config != VA_INVALID_ID)
  434. vaDestroyConfig(hwctx->display, va_config);
  435. return 0;
  436. }
  437. int ff_vaapi_decode_init(AVCodecContext *avctx)
  438. {
  439. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  440. VAStatus vas;
  441. int err;
  442. ctx->va_config = VA_INVALID_ID;
  443. ctx->va_context = VA_INVALID_ID;
  444. #if FF_API_VAAPI_CONTEXT
  445. if (avctx->hwaccel_context) {
  446. av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
  447. "vaapi_context in decode.\n");
  448. ctx->have_old_context = 1;
  449. ctx->old_context = avctx->hwaccel_context;
  450. // Really we only want the VAAPI device context, but this
  451. // allocates a whole generic device context because we don't
  452. // have any other way to determine how big it should be.
  453. ctx->device_ref =
  454. av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
  455. if (!ctx->device_ref) {
  456. err = AVERROR(ENOMEM);
  457. goto fail;
  458. }
  459. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  460. ctx->hwctx = ctx->device->hwctx;
  461. ctx->hwctx->display = ctx->old_context->display;
  462. // The old VAAPI decode setup assumed this quirk was always
  463. // present, so set it here to avoid the behaviour changing.
  464. ctx->hwctx->driver_quirks =
  465. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  466. }
  467. #endif
  468. #if FF_API_VAAPI_CONTEXT
  469. if (ctx->have_old_context) {
  470. ctx->va_config = ctx->old_context->config_id;
  471. ctx->va_context = ctx->old_context->context_id;
  472. av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
  473. "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
  474. } else {
  475. #endif
  476. err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
  477. if (err < 0)
  478. goto fail;
  479. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  480. ctx->hwfc = ctx->frames->hwctx;
  481. ctx->device = ctx->frames->device_ctx;
  482. ctx->hwctx = ctx->device->hwctx;
  483. err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
  484. &ctx->va_config, avctx->hw_frames_ctx);
  485. if (err)
  486. goto fail;
  487. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  488. avctx->coded_width, avctx->coded_height,
  489. VA_PROGRESSIVE,
  490. ctx->hwfc->surface_ids,
  491. ctx->hwfc->nb_surfaces,
  492. &ctx->va_context);
  493. if (vas != VA_STATUS_SUCCESS) {
  494. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  495. "context: %d (%s).\n", vas, vaErrorStr(vas));
  496. err = AVERROR(EIO);
  497. goto fail;
  498. }
  499. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  500. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  501. #if FF_API_VAAPI_CONTEXT
  502. }
  503. #endif
  504. return 0;
  505. fail:
  506. ff_vaapi_decode_uninit(avctx);
  507. return err;
  508. }
  509. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  510. {
  511. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  512. VAStatus vas;
  513. #if FF_API_VAAPI_CONTEXT
  514. if (ctx->have_old_context) {
  515. av_buffer_unref(&ctx->device_ref);
  516. } else {
  517. #endif
  518. if (ctx->va_context != VA_INVALID_ID) {
  519. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  520. if (vas != VA_STATUS_SUCCESS) {
  521. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  522. "context %#x: %d (%s).\n",
  523. ctx->va_context, vas, vaErrorStr(vas));
  524. }
  525. }
  526. if (ctx->va_config != VA_INVALID_ID) {
  527. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  528. if (vas != VA_STATUS_SUCCESS) {
  529. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  530. "configuration %#x: %d (%s).\n",
  531. ctx->va_config, vas, vaErrorStr(vas));
  532. }
  533. }
  534. #if FF_API_VAAPI_CONTEXT
  535. }
  536. #endif
  537. return 0;
  538. }