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.

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