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.

638 lines
22KB

  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 "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 (CONFIG_VAAPI_1 || 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 (CONFIG_VAAPI_1 || 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_MAIN, H264Main ),
  215. MAP(H264, H264_HIGH, H264High ),
  216. #if VA_CHECK_VERSION(0, 37, 0)
  217. MAP(HEVC, HEVC_MAIN, HEVCMain ),
  218. MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ),
  219. #endif
  220. MAP(WMV3, VC1_SIMPLE, VC1Simple ),
  221. MAP(WMV3, VC1_MAIN, VC1Main ),
  222. MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
  223. MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
  224. MAP(VC1, VC1_SIMPLE, VC1Simple ),
  225. MAP(VC1, VC1_MAIN, VC1Main ),
  226. MAP(VC1, VC1_COMPLEX, VC1Advanced ),
  227. MAP(VC1, VC1_ADVANCED, VC1Advanced ),
  228. #if VA_CHECK_VERSION(0, 35, 0)
  229. MAP(VP8, UNKNOWN, VP8Version0_3 ),
  230. #endif
  231. #if VA_CHECK_VERSION(0, 38, 0)
  232. MAP(VP9, VP9_0, VP9Profile0 ),
  233. #endif
  234. #if VA_CHECK_VERSION(0, 39, 0)
  235. MAP(VP9, VP9_2, VP9Profile2 ),
  236. #endif
  237. #undef MAP
  238. };
  239. static int vaapi_decode_make_config(AVCodecContext *avctx)
  240. {
  241. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  242. AVVAAPIHWConfig *hwconfig = NULL;
  243. AVHWFramesConstraints *constraints = NULL;
  244. VAStatus vas;
  245. int err, i, j;
  246. const AVCodecDescriptor *codec_desc;
  247. VAProfile profile, va_profile, *profile_list = NULL;
  248. int profile_count, exact_match, alt_profile;
  249. const AVPixFmtDescriptor *sw_desc, *desc;
  250. codec_desc = avcodec_descriptor_get(avctx->codec_id);
  251. if (!codec_desc) {
  252. err = AVERROR(EINVAL);
  253. goto fail;
  254. }
  255. profile_count = vaMaxNumProfiles(ctx->hwctx->display);
  256. profile_list = av_malloc_array(profile_count,
  257. sizeof(VAProfile));
  258. if (!profile_list) {
  259. err = AVERROR(ENOMEM);
  260. goto fail;
  261. }
  262. vas = vaQueryConfigProfiles(ctx->hwctx->display,
  263. profile_list, &profile_count);
  264. if (vas != VA_STATUS_SUCCESS) {
  265. av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
  266. "%d (%s).\n", vas, vaErrorStr(vas));
  267. err = AVERROR(ENOSYS);
  268. goto fail;
  269. }
  270. profile = VAProfileNone;
  271. exact_match = 0;
  272. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
  273. int profile_match = 0;
  274. if (avctx->codec_id != vaapi_profile_map[i].codec_id)
  275. continue;
  276. if (avctx->profile == vaapi_profile_map[i].codec_profile)
  277. profile_match = 1;
  278. profile = vaapi_profile_map[i].va_profile;
  279. for (j = 0; j < profile_count; j++) {
  280. if (profile == profile_list[j]) {
  281. exact_match = profile_match;
  282. break;
  283. }
  284. }
  285. if (j < profile_count) {
  286. if (exact_match)
  287. break;
  288. alt_profile = vaapi_profile_map[i].codec_profile;
  289. va_profile = vaapi_profile_map[i].va_profile;
  290. }
  291. }
  292. av_freep(&profile_list);
  293. if (profile == VAProfileNone) {
  294. av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
  295. "profile %d.\n", codec_desc->name, avctx->profile);
  296. err = AVERROR(ENOSYS);
  297. goto fail;
  298. }
  299. if (!exact_match) {
  300. if (avctx->hwaccel_flags &
  301. AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
  302. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  303. "supported for hardware decode.\n",
  304. codec_desc->name, avctx->profile);
  305. av_log(avctx, AV_LOG_WARNING, "Using possibly-"
  306. "incompatible profile %d instead.\n",
  307. alt_profile);
  308. profile = va_profile;
  309. } else {
  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. err = AVERROR(EINVAL);
  314. goto fail;
  315. }
  316. }
  317. ctx->va_profile = profile;
  318. ctx->va_entrypoint = VAEntrypointVLD;
  319. vas = vaCreateConfig(ctx->hwctx->display, ctx->va_profile,
  320. ctx->va_entrypoint, NULL, 0,
  321. &ctx->va_config);
  322. if (vas != VA_STATUS_SUCCESS) {
  323. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  324. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  325. err = AVERROR(EIO);
  326. goto fail;
  327. }
  328. hwconfig = av_hwdevice_hwconfig_alloc(avctx->hw_device_ctx ?
  329. avctx->hw_device_ctx :
  330. ctx->frames->device_ref);
  331. if (!hwconfig) {
  332. err = AVERROR(ENOMEM);
  333. goto fail;
  334. }
  335. hwconfig->config_id = ctx->va_config;
  336. constraints =
  337. av_hwdevice_get_hwframe_constraints(avctx->hw_device_ctx ?
  338. avctx->hw_device_ctx :
  339. ctx->frames->device_ref,
  340. 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. // Find the first format in the list which matches the expected
  365. // bit depth and subsampling. If none are found (this can happen
  366. // when 10-bit streams are decoded to 8-bit surfaces, for example)
  367. // then just take the first format on the list.
  368. ctx->surface_format = constraints->valid_sw_formats[0];
  369. sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  370. for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
  371. desc = av_pix_fmt_desc_get(constraints->valid_sw_formats[i]);
  372. if (desc->nb_components != sw_desc->nb_components ||
  373. desc->log2_chroma_w != sw_desc->log2_chroma_w ||
  374. desc->log2_chroma_h != sw_desc->log2_chroma_h)
  375. continue;
  376. for (j = 0; j < desc->nb_components; j++) {
  377. if (desc->comp[j].depth != sw_desc->comp[j].depth)
  378. break;
  379. }
  380. if (j < desc->nb_components)
  381. continue;
  382. ctx->surface_format = constraints->valid_sw_formats[i];
  383. break;
  384. }
  385. // Start with at least four surfaces.
  386. ctx->surface_count = 4;
  387. // Add per-codec number of surfaces used for storing reference frames.
  388. switch (avctx->codec_id) {
  389. case AV_CODEC_ID_H264:
  390. case AV_CODEC_ID_HEVC:
  391. ctx->surface_count += 16;
  392. break;
  393. case AV_CODEC_ID_VP9:
  394. ctx->surface_count += 8;
  395. break;
  396. case AV_CODEC_ID_VP8:
  397. ctx->surface_count += 3;
  398. break;
  399. default:
  400. ctx->surface_count += 2;
  401. }
  402. // Add an additional surface per thread is frame threading is enabled.
  403. if (avctx->active_thread_type & FF_THREAD_FRAME)
  404. ctx->surface_count += avctx->thread_count;
  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 (ctx->va_config != VA_INVALID_ID) {
  412. vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  413. ctx->va_config = VA_INVALID_ID;
  414. }
  415. av_freep(&profile_list);
  416. return err;
  417. }
  418. int ff_vaapi_decode_init(AVCodecContext *avctx)
  419. {
  420. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  421. VAStatus vas;
  422. int err;
  423. ctx->va_config = VA_INVALID_ID;
  424. ctx->va_context = VA_INVALID_ID;
  425. #if FF_API_STRUCT_VAAPI_CONTEXT
  426. if (avctx->hwaccel_context) {
  427. av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
  428. "vaapi_context in decode.\n");
  429. ctx->have_old_context = 1;
  430. ctx->old_context = avctx->hwaccel_context;
  431. // Really we only want the VAAPI device context, but this
  432. // allocates a whole generic device context because we don't
  433. // have any other way to determine how big it should be.
  434. ctx->device_ref =
  435. av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
  436. if (!ctx->device_ref) {
  437. err = AVERROR(ENOMEM);
  438. goto fail;
  439. }
  440. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  441. ctx->hwctx = ctx->device->hwctx;
  442. ctx->hwctx->display = ctx->old_context->display;
  443. // The old VAAPI decode setup assumed this quirk was always
  444. // present, so set it here to avoid the behaviour changing.
  445. ctx->hwctx->driver_quirks =
  446. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  447. } else
  448. #endif
  449. if (avctx->hw_frames_ctx) {
  450. // This structure has a shorter lifetime than the enclosing
  451. // AVCodecContext, so we inherit the references from there
  452. // and do not need to make separate ones.
  453. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  454. ctx->hwfc = ctx->frames->hwctx;
  455. ctx->device = ctx->frames->device_ctx;
  456. ctx->hwctx = ctx->device->hwctx;
  457. } else if (avctx->hw_device_ctx) {
  458. ctx->device = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
  459. ctx->hwctx = ctx->device->hwctx;
  460. if (ctx->device->type != AV_HWDEVICE_TYPE_VAAPI) {
  461. av_log(avctx, AV_LOG_ERROR, "Device supplied for VAAPI "
  462. "decoding must be a VAAPI device (not %d).\n",
  463. ctx->device->type);
  464. err = AVERROR(EINVAL);
  465. goto fail;
  466. }
  467. } else {
  468. av_log(avctx, AV_LOG_ERROR, "A hardware device or frames context "
  469. "is required for VAAPI decoding.\n");
  470. err = AVERROR(EINVAL);
  471. goto fail;
  472. }
  473. #if FF_API_STRUCT_VAAPI_CONTEXT
  474. if (ctx->have_old_context) {
  475. ctx->va_config = ctx->old_context->config_id;
  476. ctx->va_context = ctx->old_context->context_id;
  477. av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
  478. "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
  479. } else {
  480. #endif
  481. err = vaapi_decode_make_config(avctx);
  482. if (err)
  483. goto fail;
  484. if (!avctx->hw_frames_ctx) {
  485. avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
  486. if (!avctx->hw_frames_ctx) {
  487. err = AVERROR(ENOMEM);
  488. goto fail;
  489. }
  490. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  491. ctx->frames->format = AV_PIX_FMT_VAAPI;
  492. ctx->frames->width = avctx->coded_width;
  493. ctx->frames->height = avctx->coded_height;
  494. ctx->frames->sw_format = ctx->surface_format;
  495. ctx->frames->initial_pool_size = ctx->surface_count;
  496. err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
  497. if (err < 0) {
  498. av_log(avctx, AV_LOG_ERROR, "Failed to initialise internal "
  499. "frames context: %d.\n", err);
  500. goto fail;
  501. }
  502. ctx->hwfc = ctx->frames->hwctx;
  503. }
  504. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  505. avctx->coded_width, avctx->coded_height,
  506. VA_PROGRESSIVE,
  507. ctx->hwfc->surface_ids,
  508. ctx->hwfc->nb_surfaces,
  509. &ctx->va_context);
  510. if (vas != VA_STATUS_SUCCESS) {
  511. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  512. "context: %d (%s).\n", vas, vaErrorStr(vas));
  513. err = AVERROR(EIO);
  514. goto fail;
  515. }
  516. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  517. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  518. #if FF_API_STRUCT_VAAPI_CONTEXT
  519. }
  520. #endif
  521. return 0;
  522. fail:
  523. ff_vaapi_decode_uninit(avctx);
  524. return err;
  525. }
  526. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  527. {
  528. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  529. VAStatus vas;
  530. #if FF_API_STRUCT_VAAPI_CONTEXT
  531. if (ctx->have_old_context) {
  532. av_buffer_unref(&ctx->device_ref);
  533. } else {
  534. #endif
  535. if (ctx->va_context != VA_INVALID_ID) {
  536. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  537. if (vas != VA_STATUS_SUCCESS) {
  538. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  539. "context %#x: %d (%s).\n",
  540. ctx->va_context, vas, vaErrorStr(vas));
  541. }
  542. }
  543. if (ctx->va_config != VA_INVALID_ID) {
  544. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  545. if (vas != VA_STATUS_SUCCESS) {
  546. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  547. "configuration %#x: %d (%s).\n",
  548. ctx->va_config, vas, vaErrorStr(vas));
  549. }
  550. }
  551. #if FF_API_STRUCT_VAAPI_CONTEXT
  552. }
  553. #endif
  554. return 0;
  555. }