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.

642 lines
22KB

  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. // Allowing a profile mismatch can be useful because streams may
  249. // over-declare their required capabilities - in particular, many
  250. // H.264 baseline profile streams (notably some of those in FATE)
  251. // only use the feature set of constrained baseline. This flag
  252. // would have to be be set by some external means in order to
  253. // actually be useful. (AV_HWACCEL_FLAG_IGNORE_PROFILE?)
  254. int allow_profile_mismatch = 0;
  255. codec_desc = avcodec_descriptor_get(avctx->codec_id);
  256. if (!codec_desc) {
  257. err = AVERROR(EINVAL);
  258. goto fail;
  259. }
  260. profile_count = vaMaxNumProfiles(ctx->hwctx->display);
  261. profile_list = av_malloc_array(profile_count,
  262. sizeof(VAProfile));
  263. if (!profile_list) {
  264. err = AVERROR(ENOMEM);
  265. goto fail;
  266. }
  267. vas = vaQueryConfigProfiles(ctx->hwctx->display,
  268. profile_list, &profile_count);
  269. if (vas != VA_STATUS_SUCCESS) {
  270. av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
  271. "%d (%s).\n", vas, vaErrorStr(vas));
  272. err = AVERROR(ENOSYS);
  273. goto fail;
  274. }
  275. profile = VAProfileNone;
  276. exact_match = 0;
  277. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
  278. int profile_match = 0;
  279. if (avctx->codec_id != vaapi_profile_map[i].codec_id)
  280. continue;
  281. if (avctx->profile == vaapi_profile_map[i].codec_profile ||
  282. vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
  283. profile_match = 1;
  284. profile = vaapi_profile_map[i].va_profile;
  285. for (j = 0; j < profile_count; j++) {
  286. if (profile == profile_list[j]) {
  287. exact_match = profile_match;
  288. break;
  289. }
  290. }
  291. if (j < profile_count) {
  292. if (exact_match)
  293. break;
  294. alt_profile = vaapi_profile_map[i].codec_profile;
  295. }
  296. }
  297. av_freep(&profile_list);
  298. if (profile == VAProfileNone) {
  299. av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
  300. "profile %d.\n", codec_desc->name, avctx->profile);
  301. err = AVERROR(ENOSYS);
  302. goto fail;
  303. }
  304. if (!exact_match) {
  305. if (allow_profile_mismatch) {
  306. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  307. "supported for hardware decode.\n",
  308. codec_desc->name, avctx->profile);
  309. av_log(avctx, AV_LOG_WARNING, "Using possibly-"
  310. "incompatible profile %d instead.\n",
  311. alt_profile);
  312. } else {
  313. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  314. "supported for hardware decode.\n",
  315. codec_desc->name, avctx->profile);
  316. err = AVERROR(EINVAL);
  317. goto fail;
  318. }
  319. }
  320. ctx->va_profile = profile;
  321. ctx->va_entrypoint = VAEntrypointVLD;
  322. vas = vaCreateConfig(ctx->hwctx->display, ctx->va_profile,
  323. ctx->va_entrypoint, NULL, 0,
  324. &ctx->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(avctx->hw_device_ctx ?
  332. avctx->hw_device_ctx :
  333. ctx->frames->device_ref);
  334. if (!hwconfig) {
  335. err = AVERROR(ENOMEM);
  336. goto fail;
  337. }
  338. hwconfig->config_id = ctx->va_config;
  339. constraints =
  340. av_hwdevice_get_hwframe_constraints(avctx->hw_device_ctx ?
  341. avctx->hw_device_ctx :
  342. ctx->frames->device_ref,
  343. hwconfig);
  344. if (!constraints) {
  345. err = AVERROR(ENOMEM);
  346. goto fail;
  347. }
  348. if (avctx->coded_width < constraints->min_width ||
  349. avctx->coded_height < constraints->min_height ||
  350. avctx->coded_width > constraints->max_width ||
  351. avctx->coded_height > constraints->max_height) {
  352. av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
  353. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  354. avctx->coded_width, avctx->coded_height,
  355. constraints->min_width, constraints->max_width,
  356. constraints->min_height, constraints->max_height);
  357. err = AVERROR(EINVAL);
  358. goto fail;
  359. }
  360. if (!constraints->valid_sw_formats ||
  361. constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
  362. av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
  363. "usable surface formats.\n");
  364. err = AVERROR(EINVAL);
  365. goto fail;
  366. }
  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. ctx->surface_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. ctx->surface_format = constraints->valid_sw_formats[i];
  386. break;
  387. }
  388. // Start with at least four surfaces.
  389. ctx->surface_count = 4;
  390. // Add per-codec number of surfaces used for storing reference frames.
  391. switch (avctx->codec_id) {
  392. case AV_CODEC_ID_H264:
  393. case AV_CODEC_ID_HEVC:
  394. ctx->surface_count += 16;
  395. break;
  396. case AV_CODEC_ID_VP9:
  397. ctx->surface_count += 8;
  398. break;
  399. case AV_CODEC_ID_VP8:
  400. ctx->surface_count += 3;
  401. break;
  402. default:
  403. ctx->surface_count += 2;
  404. }
  405. // Add an additional surface per thread is frame threading is enabled.
  406. if (avctx->active_thread_type & FF_THREAD_FRAME)
  407. ctx->surface_count += avctx->thread_count;
  408. av_hwframe_constraints_free(&constraints);
  409. av_freep(&hwconfig);
  410. return 0;
  411. fail:
  412. av_hwframe_constraints_free(&constraints);
  413. av_freep(&hwconfig);
  414. if (ctx->va_config != VA_INVALID_ID) {
  415. vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  416. ctx->va_config = VA_INVALID_ID;
  417. }
  418. av_freep(&profile_list);
  419. return err;
  420. }
  421. int ff_vaapi_decode_init(AVCodecContext *avctx)
  422. {
  423. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  424. VAStatus vas;
  425. int err;
  426. ctx->va_config = VA_INVALID_ID;
  427. ctx->va_context = VA_INVALID_ID;
  428. #if FF_API_VAAPI_CONTEXT
  429. if (avctx->hwaccel_context) {
  430. av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
  431. "vaapi_context in decode.\n");
  432. ctx->have_old_context = 1;
  433. ctx->old_context = avctx->hwaccel_context;
  434. // Really we only want the VAAPI device context, but this
  435. // allocates a whole generic device context because we don't
  436. // have any other way to determine how big it should be.
  437. ctx->device_ref =
  438. av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
  439. if (!ctx->device_ref) {
  440. err = AVERROR(ENOMEM);
  441. goto fail;
  442. }
  443. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  444. ctx->hwctx = ctx->device->hwctx;
  445. ctx->hwctx->display = ctx->old_context->display;
  446. // The old VAAPI decode setup assumed this quirk was always
  447. // present, so set it here to avoid the behaviour changing.
  448. ctx->hwctx->driver_quirks =
  449. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  450. } else
  451. #endif
  452. if (avctx->hw_frames_ctx) {
  453. // This structure has a shorter lifetime than the enclosing
  454. // AVCodecContext, so we inherit the references from there
  455. // and do not need to make separate ones.
  456. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  457. ctx->hwfc = ctx->frames->hwctx;
  458. ctx->device = ctx->frames->device_ctx;
  459. ctx->hwctx = ctx->device->hwctx;
  460. } else if (avctx->hw_device_ctx) {
  461. ctx->device = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
  462. ctx->hwctx = ctx->device->hwctx;
  463. if (ctx->device->type != AV_HWDEVICE_TYPE_VAAPI) {
  464. av_log(avctx, AV_LOG_ERROR, "Device supplied for VAAPI "
  465. "decoding must be a VAAPI device (not %d).\n",
  466. ctx->device->type);
  467. err = AVERROR(EINVAL);
  468. goto fail;
  469. }
  470. } else {
  471. av_log(avctx, AV_LOG_ERROR, "A hardware device or frames context "
  472. "is required for VAAPI decoding.\n");
  473. err = AVERROR(EINVAL);
  474. goto fail;
  475. }
  476. #if FF_API_VAAPI_CONTEXT
  477. if (ctx->have_old_context) {
  478. ctx->va_config = ctx->old_context->config_id;
  479. ctx->va_context = ctx->old_context->context_id;
  480. av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
  481. "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
  482. } else {
  483. #endif
  484. err = vaapi_decode_make_config(avctx);
  485. if (err)
  486. goto fail;
  487. if (!avctx->hw_frames_ctx) {
  488. avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
  489. if (!avctx->hw_frames_ctx) {
  490. err = AVERROR(ENOMEM);
  491. goto fail;
  492. }
  493. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  494. ctx->frames->format = AV_PIX_FMT_VAAPI;
  495. ctx->frames->width = avctx->coded_width;
  496. ctx->frames->height = avctx->coded_height;
  497. ctx->frames->sw_format = ctx->surface_format;
  498. ctx->frames->initial_pool_size = ctx->surface_count;
  499. err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
  500. if (err < 0) {
  501. av_log(avctx, AV_LOG_ERROR, "Failed to initialise internal "
  502. "frames context: %d.\n", err);
  503. goto fail;
  504. }
  505. ctx->hwfc = ctx->frames->hwctx;
  506. }
  507. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  508. avctx->coded_width, avctx->coded_height,
  509. VA_PROGRESSIVE,
  510. ctx->hwfc->surface_ids,
  511. ctx->hwfc->nb_surfaces,
  512. &ctx->va_context);
  513. if (vas != VA_STATUS_SUCCESS) {
  514. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  515. "context: %d (%s).\n", vas, vaErrorStr(vas));
  516. err = AVERROR(EIO);
  517. goto fail;
  518. }
  519. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  520. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  521. #if FF_API_VAAPI_CONTEXT
  522. }
  523. #endif
  524. return 0;
  525. fail:
  526. ff_vaapi_decode_uninit(avctx);
  527. return err;
  528. }
  529. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  530. {
  531. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  532. VAStatus vas;
  533. #if FF_API_VAAPI_CONTEXT
  534. if (ctx->have_old_context) {
  535. av_buffer_unref(&ctx->device_ref);
  536. } else {
  537. #endif
  538. if (ctx->va_context != VA_INVALID_ID) {
  539. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  540. if (vas != VA_STATUS_SUCCESS) {
  541. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  542. "context %#x: %d (%s).\n",
  543. ctx->va_context, vas, vaErrorStr(vas));
  544. }
  545. }
  546. if (ctx->va_config != VA_INVALID_ID) {
  547. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  548. if (vas != VA_STATUS_SUCCESS) {
  549. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  550. "configuration %#x: %d (%s).\n",
  551. ctx->va_config, vas, vaErrorStr(vas));
  552. }
  553. }
  554. #if FF_API_VAAPI_CONTEXT
  555. }
  556. #endif
  557. return 0;
  558. }