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.

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