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.

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