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.

731 lines
24KB

  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. uint32_t fourcc;
  202. enum AVPixelFormat pix_fmt;
  203. } vaapi_format_map[] = {
  204. #define MAP(va, av) { VA_FOURCC_ ## va, AV_PIX_FMT_ ## av }
  205. // 4:0:0
  206. MAP(Y800, GRAY8),
  207. // 4:2:0
  208. MAP(NV12, NV12),
  209. MAP(YV12, YUV420P),
  210. MAP(IYUV, YUV420P),
  211. #ifdef VA_FOURCC_I420
  212. MAP(I420, YUV420P),
  213. #endif
  214. MAP(IMC3, YUV420P),
  215. // 4:1:1
  216. MAP(411P, YUV411P),
  217. // 4:2:2
  218. MAP(422H, YUV422P),
  219. #ifdef VA_FOURCC_YV16
  220. MAP(YV16, YUV422P),
  221. #endif
  222. // 4:4:0
  223. MAP(422V, YUV440P),
  224. // 4:4:4
  225. MAP(444P, YUV444P),
  226. // 4:2:0 10-bit
  227. #ifdef VA_FOURCC_P010
  228. MAP(P010, P010),
  229. #endif
  230. #ifdef VA_FOURCC_I010
  231. MAP(I010, YUV420P10),
  232. #endif
  233. #undef MAP
  234. };
  235. static int vaapi_decode_find_best_format(AVCodecContext *avctx,
  236. AVHWDeviceContext *device,
  237. VAConfigID config_id,
  238. AVHWFramesContext *frames)
  239. {
  240. AVVAAPIDeviceContext *hwctx = device->hwctx;
  241. VAStatus vas;
  242. VASurfaceAttrib *attr;
  243. enum AVPixelFormat source_format, best_format, format;
  244. uint32_t best_fourcc, fourcc;
  245. int i, j, nb_attr;
  246. source_format = avctx->sw_pix_fmt;
  247. av_assert0(source_format != AV_PIX_FMT_NONE);
  248. vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
  249. NULL, &nb_attr);
  250. if (vas != VA_STATUS_SUCCESS) {
  251. av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
  252. "%d (%s).\n", vas, vaErrorStr(vas));
  253. return AVERROR(ENOSYS);
  254. }
  255. attr = av_malloc_array(nb_attr, sizeof(*attr));
  256. if (!attr)
  257. return AVERROR(ENOMEM);
  258. vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
  259. attr, &nb_attr);
  260. if (vas != VA_STATUS_SUCCESS) {
  261. av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
  262. "%d (%s).\n", vas, vaErrorStr(vas));
  263. av_freep(&attr);
  264. return AVERROR(ENOSYS);
  265. }
  266. best_format = AV_PIX_FMT_NONE;
  267. for (i = 0; i < nb_attr; i++) {
  268. if (attr[i].type != VASurfaceAttribPixelFormat)
  269. continue;
  270. fourcc = attr[i].value.value.i;
  271. for (j = 0; j < FF_ARRAY_ELEMS(vaapi_format_map); j++) {
  272. if (fourcc == vaapi_format_map[j].fourcc)
  273. break;
  274. }
  275. if (j >= FF_ARRAY_ELEMS(vaapi_format_map)) {
  276. av_log(avctx, AV_LOG_DEBUG, "Ignoring unknown format %#x.\n",
  277. fourcc);
  278. continue;
  279. }
  280. format = vaapi_format_map[j].pix_fmt;
  281. av_log(avctx, AV_LOG_DEBUG, "Considering format %#x -> %s.\n",
  282. fourcc, av_get_pix_fmt_name(format));
  283. best_format = av_find_best_pix_fmt_of_2(format, best_format,
  284. source_format, 0, NULL);
  285. if (format == best_format)
  286. best_fourcc = fourcc;
  287. }
  288. av_freep(&attr);
  289. if (best_format == AV_PIX_FMT_NONE) {
  290. av_log(avctx, AV_LOG_ERROR, "No usable formats for decoding!\n");
  291. return AVERROR(EINVAL);
  292. }
  293. av_log(avctx, AV_LOG_DEBUG, "Picked %s (%#x) as best match for %s.\n",
  294. av_get_pix_fmt_name(best_format), best_fourcc,
  295. av_get_pix_fmt_name(source_format));
  296. frames->sw_format = best_format;
  297. if (avctx->internal->hwaccel_priv_data) {
  298. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  299. AVVAAPIFramesContext *avfc = frames->hwctx;
  300. ctx->pixel_format_attribute = (VASurfaceAttrib) {
  301. .type = VASurfaceAttribPixelFormat,
  302. .value.value.i = best_fourcc,
  303. };
  304. avfc->attributes = &ctx->pixel_format_attribute;
  305. avfc->nb_attributes = 1;
  306. }
  307. return 0;
  308. }
  309. static const struct {
  310. enum AVCodecID codec_id;
  311. int codec_profile;
  312. VAProfile va_profile;
  313. } vaapi_profile_map[] = {
  314. #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
  315. MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
  316. MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
  317. MAP(H263, UNKNOWN, H263Baseline),
  318. MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
  319. MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
  320. MPEG4AdvancedSimple),
  321. MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
  322. MAP(H264, H264_CONSTRAINED_BASELINE,
  323. H264ConstrainedBaseline),
  324. MAP(H264, H264_MAIN, H264Main ),
  325. MAP(H264, H264_HIGH, H264High ),
  326. #if VA_CHECK_VERSION(0, 37, 0)
  327. MAP(HEVC, HEVC_MAIN, HEVCMain ),
  328. MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ),
  329. #endif
  330. MAP(MJPEG, MJPEG_HUFFMAN_BASELINE_DCT,
  331. JPEGBaseline),
  332. MAP(WMV3, VC1_SIMPLE, VC1Simple ),
  333. MAP(WMV3, VC1_MAIN, VC1Main ),
  334. MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
  335. MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
  336. MAP(VC1, VC1_SIMPLE, VC1Simple ),
  337. MAP(VC1, VC1_MAIN, VC1Main ),
  338. MAP(VC1, VC1_COMPLEX, VC1Advanced ),
  339. MAP(VC1, VC1_ADVANCED, VC1Advanced ),
  340. MAP(VP8, UNKNOWN, VP8Version0_3 ),
  341. #if VA_CHECK_VERSION(0, 38, 0)
  342. MAP(VP9, VP9_0, VP9Profile0 ),
  343. #endif
  344. #if VA_CHECK_VERSION(0, 39, 0)
  345. MAP(VP9, VP9_2, VP9Profile2 ),
  346. #endif
  347. #undef MAP
  348. };
  349. /*
  350. * Set *va_config and the frames_ref fields from the current codec parameters
  351. * in avctx.
  352. */
  353. static int vaapi_decode_make_config(AVCodecContext *avctx,
  354. AVBufferRef *device_ref,
  355. VAConfigID *va_config,
  356. AVBufferRef *frames_ref)
  357. {
  358. AVVAAPIHWConfig *hwconfig = NULL;
  359. AVHWFramesConstraints *constraints = NULL;
  360. VAStatus vas;
  361. int err, i, j;
  362. const AVCodecDescriptor *codec_desc;
  363. VAProfile *profile_list = NULL, matched_va_profile;
  364. int profile_count, exact_match, matched_ff_profile;
  365. AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data;
  366. AVVAAPIDeviceContext *hwctx = device->hwctx;
  367. codec_desc = avcodec_descriptor_get(avctx->codec_id);
  368. if (!codec_desc) {
  369. err = AVERROR(EINVAL);
  370. goto fail;
  371. }
  372. profile_count = vaMaxNumProfiles(hwctx->display);
  373. profile_list = av_malloc_array(profile_count,
  374. sizeof(VAProfile));
  375. if (!profile_list) {
  376. err = AVERROR(ENOMEM);
  377. goto fail;
  378. }
  379. vas = vaQueryConfigProfiles(hwctx->display,
  380. profile_list, &profile_count);
  381. if (vas != VA_STATUS_SUCCESS) {
  382. av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
  383. "%d (%s).\n", vas, vaErrorStr(vas));
  384. err = AVERROR(ENOSYS);
  385. goto fail;
  386. }
  387. matched_va_profile = VAProfileNone;
  388. exact_match = 0;
  389. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
  390. int profile_match = 0;
  391. if (avctx->codec_id != vaapi_profile_map[i].codec_id)
  392. continue;
  393. if (avctx->profile == vaapi_profile_map[i].codec_profile ||
  394. vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
  395. profile_match = 1;
  396. for (j = 0; j < profile_count; j++) {
  397. if (vaapi_profile_map[i].va_profile == profile_list[j]) {
  398. exact_match = profile_match;
  399. break;
  400. }
  401. }
  402. if (j < profile_count) {
  403. matched_va_profile = vaapi_profile_map[i].va_profile;
  404. matched_ff_profile = vaapi_profile_map[i].codec_profile;
  405. if (exact_match)
  406. break;
  407. }
  408. }
  409. av_freep(&profile_list);
  410. if (matched_va_profile == VAProfileNone) {
  411. av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
  412. "profile %d.\n", codec_desc->name, avctx->profile);
  413. err = AVERROR(ENOSYS);
  414. goto fail;
  415. }
  416. if (!exact_match) {
  417. if (avctx->hwaccel_flags &
  418. AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
  419. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  420. "supported for hardware decode.\n",
  421. codec_desc->name, avctx->profile);
  422. av_log(avctx, AV_LOG_WARNING, "Using possibly-"
  423. "incompatible profile %d instead.\n",
  424. matched_ff_profile);
  425. } else {
  426. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  427. "supported for hardware decode.\n",
  428. codec_desc->name, avctx->profile);
  429. err = AVERROR(EINVAL);
  430. goto fail;
  431. }
  432. }
  433. vas = vaCreateConfig(hwctx->display, matched_va_profile,
  434. VAEntrypointVLD, NULL, 0,
  435. va_config);
  436. if (vas != VA_STATUS_SUCCESS) {
  437. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  438. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  439. err = AVERROR(EIO);
  440. goto fail;
  441. }
  442. hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
  443. if (!hwconfig) {
  444. err = AVERROR(ENOMEM);
  445. goto fail;
  446. }
  447. hwconfig->config_id = *va_config;
  448. constraints =
  449. av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
  450. if (!constraints) {
  451. err = AVERROR(ENOMEM);
  452. goto fail;
  453. }
  454. if (avctx->coded_width < constraints->min_width ||
  455. avctx->coded_height < constraints->min_height ||
  456. avctx->coded_width > constraints->max_width ||
  457. avctx->coded_height > constraints->max_height) {
  458. av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
  459. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  460. avctx->coded_width, avctx->coded_height,
  461. constraints->min_width, constraints->max_width,
  462. constraints->min_height, constraints->max_height);
  463. err = AVERROR(EINVAL);
  464. goto fail;
  465. }
  466. if (!constraints->valid_sw_formats ||
  467. constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
  468. av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
  469. "usable surface formats.\n");
  470. err = AVERROR(EINVAL);
  471. goto fail;
  472. }
  473. if (frames_ref) {
  474. AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
  475. frames->format = AV_PIX_FMT_VAAPI;
  476. frames->width = avctx->coded_width;
  477. frames->height = avctx->coded_height;
  478. err = vaapi_decode_find_best_format(avctx, device,
  479. *va_config, frames);
  480. if (err < 0)
  481. goto fail;
  482. frames->initial_pool_size = 1;
  483. // Add per-codec number of surfaces used for storing reference frames.
  484. switch (avctx->codec_id) {
  485. case AV_CODEC_ID_H264:
  486. case AV_CODEC_ID_HEVC:
  487. frames->initial_pool_size += 16;
  488. break;
  489. case AV_CODEC_ID_VP9:
  490. frames->initial_pool_size += 8;
  491. break;
  492. case AV_CODEC_ID_VP8:
  493. frames->initial_pool_size += 3;
  494. break;
  495. default:
  496. frames->initial_pool_size += 2;
  497. }
  498. }
  499. av_hwframe_constraints_free(&constraints);
  500. av_freep(&hwconfig);
  501. return 0;
  502. fail:
  503. av_hwframe_constraints_free(&constraints);
  504. av_freep(&hwconfig);
  505. if (*va_config != VA_INVALID_ID) {
  506. vaDestroyConfig(hwctx->display, *va_config);
  507. *va_config = VA_INVALID_ID;
  508. }
  509. av_freep(&profile_list);
  510. return err;
  511. }
  512. int ff_vaapi_common_frame_params(AVCodecContext *avctx,
  513. AVBufferRef *hw_frames_ctx)
  514. {
  515. AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
  516. AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
  517. AVVAAPIDeviceContext *hwctx;
  518. VAConfigID va_config = VA_INVALID_ID;
  519. int err;
  520. if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
  521. return AVERROR(EINVAL);
  522. hwctx = device_ctx->hwctx;
  523. err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
  524. hw_frames_ctx);
  525. if (err)
  526. return err;
  527. if (va_config != VA_INVALID_ID)
  528. vaDestroyConfig(hwctx->display, va_config);
  529. return 0;
  530. }
  531. int ff_vaapi_decode_init(AVCodecContext *avctx)
  532. {
  533. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  534. VAStatus vas;
  535. int err;
  536. ctx->va_config = VA_INVALID_ID;
  537. ctx->va_context = VA_INVALID_ID;
  538. #if FF_API_STRUCT_VAAPI_CONTEXT
  539. if (avctx->hwaccel_context) {
  540. av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
  541. "vaapi_context in decode.\n");
  542. ctx->have_old_context = 1;
  543. ctx->old_context = avctx->hwaccel_context;
  544. // Really we only want the VAAPI device context, but this
  545. // allocates a whole generic device context because we don't
  546. // have any other way to determine how big it should be.
  547. ctx->device_ref =
  548. av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
  549. if (!ctx->device_ref) {
  550. err = AVERROR(ENOMEM);
  551. goto fail;
  552. }
  553. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  554. ctx->hwctx = ctx->device->hwctx;
  555. ctx->hwctx->display = ctx->old_context->display;
  556. // The old VAAPI decode setup assumed this quirk was always
  557. // present, so set it here to avoid the behaviour changing.
  558. ctx->hwctx->driver_quirks =
  559. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  560. }
  561. #endif
  562. #if FF_API_STRUCT_VAAPI_CONTEXT
  563. if (ctx->have_old_context) {
  564. ctx->va_config = ctx->old_context->config_id;
  565. ctx->va_context = ctx->old_context->context_id;
  566. av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
  567. "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
  568. } else {
  569. #endif
  570. err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
  571. if (err < 0)
  572. goto fail;
  573. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  574. ctx->hwfc = ctx->frames->hwctx;
  575. ctx->device = ctx->frames->device_ctx;
  576. ctx->hwctx = ctx->device->hwctx;
  577. err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
  578. &ctx->va_config, avctx->hw_frames_ctx);
  579. if (err)
  580. goto fail;
  581. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  582. avctx->coded_width, avctx->coded_height,
  583. VA_PROGRESSIVE,
  584. ctx->hwfc->surface_ids,
  585. ctx->hwfc->nb_surfaces,
  586. &ctx->va_context);
  587. if (vas != VA_STATUS_SUCCESS) {
  588. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  589. "context: %d (%s).\n", vas, vaErrorStr(vas));
  590. err = AVERROR(EIO);
  591. goto fail;
  592. }
  593. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  594. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  595. #if FF_API_STRUCT_VAAPI_CONTEXT
  596. }
  597. #endif
  598. return 0;
  599. fail:
  600. ff_vaapi_decode_uninit(avctx);
  601. return err;
  602. }
  603. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  604. {
  605. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  606. VAStatus vas;
  607. #if FF_API_STRUCT_VAAPI_CONTEXT
  608. if (ctx->have_old_context) {
  609. av_buffer_unref(&ctx->device_ref);
  610. } else {
  611. #endif
  612. if (ctx->va_context != VA_INVALID_ID) {
  613. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  614. if (vas != VA_STATUS_SUCCESS) {
  615. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  616. "context %#x: %d (%s).\n",
  617. ctx->va_context, vas, vaErrorStr(vas));
  618. }
  619. }
  620. if (ctx->va_config != VA_INVALID_ID) {
  621. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  622. if (vas != VA_STATUS_SUCCESS) {
  623. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  624. "configuration %#x: %d (%s).\n",
  625. ctx->va_config, vas, vaErrorStr(vas));
  626. }
  627. }
  628. #if FF_API_STRUCT_VAAPI_CONTEXT
  629. }
  630. #endif
  631. return 0;
  632. }