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.

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