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.

751 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. MAP(HEVC, HEVC_MAIN_STILL_PICTURE,
  338. HEVCMain ),
  339. #endif
  340. #if VA_CHECK_VERSION(1, 2, 0)
  341. MAP(HEVC, HEVC_REXT, None,
  342. ff_vaapi_parse_hevc_rext_profile ),
  343. #endif
  344. MAP(MJPEG, MJPEG_HUFFMAN_BASELINE_DCT,
  345. JPEGBaseline),
  346. MAP(WMV3, VC1_SIMPLE, VC1Simple ),
  347. MAP(WMV3, VC1_MAIN, VC1Main ),
  348. MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
  349. MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
  350. MAP(VC1, VC1_SIMPLE, VC1Simple ),
  351. MAP(VC1, VC1_MAIN, VC1Main ),
  352. MAP(VC1, VC1_COMPLEX, VC1Advanced ),
  353. MAP(VC1, VC1_ADVANCED, VC1Advanced ),
  354. MAP(VP8, UNKNOWN, VP8Version0_3 ),
  355. #if VA_CHECK_VERSION(0, 38, 0)
  356. MAP(VP9, VP9_0, VP9Profile0 ),
  357. #endif
  358. #if VA_CHECK_VERSION(0, 39, 0)
  359. MAP(VP9, VP9_2, VP9Profile2 ),
  360. #endif
  361. #undef MAP
  362. };
  363. /*
  364. * Set *va_config and the frames_ref fields from the current codec parameters
  365. * in avctx.
  366. */
  367. static int vaapi_decode_make_config(AVCodecContext *avctx,
  368. AVBufferRef *device_ref,
  369. VAConfigID *va_config,
  370. AVBufferRef *frames_ref)
  371. {
  372. AVVAAPIHWConfig *hwconfig = NULL;
  373. AVHWFramesConstraints *constraints = NULL;
  374. VAStatus vas;
  375. int err, i, j;
  376. const AVCodecDescriptor *codec_desc;
  377. VAProfile *profile_list = NULL, matched_va_profile, va_profile;
  378. int profile_count, exact_match, matched_ff_profile, codec_profile;
  379. AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data;
  380. AVVAAPIDeviceContext *hwctx = device->hwctx;
  381. codec_desc = avcodec_descriptor_get(avctx->codec_id);
  382. if (!codec_desc) {
  383. err = AVERROR(EINVAL);
  384. goto fail;
  385. }
  386. profile_count = vaMaxNumProfiles(hwctx->display);
  387. profile_list = av_malloc_array(profile_count,
  388. sizeof(VAProfile));
  389. if (!profile_list) {
  390. err = AVERROR(ENOMEM);
  391. goto fail;
  392. }
  393. vas = vaQueryConfigProfiles(hwctx->display,
  394. profile_list, &profile_count);
  395. if (vas != VA_STATUS_SUCCESS) {
  396. av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
  397. "%d (%s).\n", vas, vaErrorStr(vas));
  398. err = AVERROR(ENOSYS);
  399. goto fail;
  400. }
  401. matched_va_profile = VAProfileNone;
  402. exact_match = 0;
  403. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
  404. int profile_match = 0;
  405. if (avctx->codec_id != vaapi_profile_map[i].codec_id)
  406. continue;
  407. if (avctx->profile == vaapi_profile_map[i].codec_profile ||
  408. vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
  409. profile_match = 1;
  410. va_profile = vaapi_profile_map[i].profile_parser ?
  411. vaapi_profile_map[i].profile_parser(avctx) :
  412. vaapi_profile_map[i].va_profile;
  413. codec_profile = vaapi_profile_map[i].codec_profile;
  414. for (j = 0; j < profile_count; j++) {
  415. if (va_profile == profile_list[j]) {
  416. exact_match = profile_match;
  417. break;
  418. }
  419. }
  420. if (j < profile_count) {
  421. matched_va_profile = va_profile;
  422. matched_ff_profile = codec_profile;
  423. if (exact_match)
  424. break;
  425. }
  426. }
  427. av_freep(&profile_list);
  428. if (matched_va_profile == VAProfileNone) {
  429. av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
  430. "profile %d.\n", codec_desc->name, avctx->profile);
  431. err = AVERROR(ENOSYS);
  432. goto fail;
  433. }
  434. if (!exact_match) {
  435. if (avctx->hwaccel_flags &
  436. AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
  437. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  438. "supported for hardware decode.\n",
  439. codec_desc->name, avctx->profile);
  440. av_log(avctx, AV_LOG_WARNING, "Using possibly-"
  441. "incompatible profile %d instead.\n",
  442. matched_ff_profile);
  443. } else {
  444. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  445. "supported for hardware decode.\n",
  446. codec_desc->name, avctx->profile);
  447. err = AVERROR(EINVAL);
  448. goto fail;
  449. }
  450. }
  451. vas = vaCreateConfig(hwctx->display, matched_va_profile,
  452. VAEntrypointVLD, NULL, 0,
  453. va_config);
  454. if (vas != VA_STATUS_SUCCESS) {
  455. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  456. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  457. err = AVERROR(EIO);
  458. goto fail;
  459. }
  460. hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
  461. if (!hwconfig) {
  462. err = AVERROR(ENOMEM);
  463. goto fail;
  464. }
  465. hwconfig->config_id = *va_config;
  466. constraints =
  467. av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
  468. if (!constraints) {
  469. err = AVERROR(ENOMEM);
  470. goto fail;
  471. }
  472. if (avctx->coded_width < constraints->min_width ||
  473. avctx->coded_height < constraints->min_height ||
  474. avctx->coded_width > constraints->max_width ||
  475. avctx->coded_height > constraints->max_height) {
  476. av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
  477. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  478. avctx->coded_width, avctx->coded_height,
  479. constraints->min_width, constraints->max_width,
  480. constraints->min_height, constraints->max_height);
  481. err = AVERROR(EINVAL);
  482. goto fail;
  483. }
  484. if (!constraints->valid_sw_formats ||
  485. constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
  486. av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
  487. "usable surface formats.\n");
  488. err = AVERROR(EINVAL);
  489. goto fail;
  490. }
  491. if (frames_ref) {
  492. AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
  493. frames->format = AV_PIX_FMT_VAAPI;
  494. frames->width = avctx->coded_width;
  495. frames->height = avctx->coded_height;
  496. err = vaapi_decode_find_best_format(avctx, device,
  497. *va_config, frames);
  498. if (err < 0)
  499. goto fail;
  500. frames->initial_pool_size = 1;
  501. // Add per-codec number of surfaces used for storing reference frames.
  502. switch (avctx->codec_id) {
  503. case AV_CODEC_ID_H264:
  504. case AV_CODEC_ID_HEVC:
  505. frames->initial_pool_size += 16;
  506. break;
  507. case AV_CODEC_ID_VP9:
  508. frames->initial_pool_size += 8;
  509. break;
  510. case AV_CODEC_ID_VP8:
  511. frames->initial_pool_size += 3;
  512. break;
  513. default:
  514. frames->initial_pool_size += 2;
  515. }
  516. }
  517. av_hwframe_constraints_free(&constraints);
  518. av_freep(&hwconfig);
  519. return 0;
  520. fail:
  521. av_hwframe_constraints_free(&constraints);
  522. av_freep(&hwconfig);
  523. if (*va_config != VA_INVALID_ID) {
  524. vaDestroyConfig(hwctx->display, *va_config);
  525. *va_config = VA_INVALID_ID;
  526. }
  527. av_freep(&profile_list);
  528. return err;
  529. }
  530. int ff_vaapi_common_frame_params(AVCodecContext *avctx,
  531. AVBufferRef *hw_frames_ctx)
  532. {
  533. AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
  534. AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
  535. AVVAAPIDeviceContext *hwctx;
  536. VAConfigID va_config = VA_INVALID_ID;
  537. int err;
  538. if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
  539. return AVERROR(EINVAL);
  540. hwctx = device_ctx->hwctx;
  541. err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
  542. hw_frames_ctx);
  543. if (err)
  544. return err;
  545. if (va_config != VA_INVALID_ID)
  546. vaDestroyConfig(hwctx->display, va_config);
  547. return 0;
  548. }
  549. int ff_vaapi_decode_init(AVCodecContext *avctx)
  550. {
  551. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  552. VAStatus vas;
  553. int err;
  554. ctx->va_config = VA_INVALID_ID;
  555. ctx->va_context = VA_INVALID_ID;
  556. #if FF_API_STRUCT_VAAPI_CONTEXT
  557. if (avctx->hwaccel_context) {
  558. av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
  559. "vaapi_context in decode.\n");
  560. ctx->have_old_context = 1;
  561. ctx->old_context = avctx->hwaccel_context;
  562. // Really we only want the VAAPI device context, but this
  563. // allocates a whole generic device context because we don't
  564. // have any other way to determine how big it should be.
  565. ctx->device_ref =
  566. av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
  567. if (!ctx->device_ref) {
  568. err = AVERROR(ENOMEM);
  569. goto fail;
  570. }
  571. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  572. ctx->hwctx = ctx->device->hwctx;
  573. ctx->hwctx->display = ctx->old_context->display;
  574. // The old VAAPI decode setup assumed this quirk was always
  575. // present, so set it here to avoid the behaviour changing.
  576. ctx->hwctx->driver_quirks =
  577. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  578. }
  579. #endif
  580. #if FF_API_STRUCT_VAAPI_CONTEXT
  581. if (ctx->have_old_context) {
  582. ctx->va_config = ctx->old_context->config_id;
  583. ctx->va_context = ctx->old_context->context_id;
  584. av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
  585. "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
  586. } else {
  587. #endif
  588. err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
  589. if (err < 0)
  590. goto fail;
  591. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  592. ctx->hwfc = ctx->frames->hwctx;
  593. ctx->device = ctx->frames->device_ctx;
  594. ctx->hwctx = ctx->device->hwctx;
  595. err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
  596. &ctx->va_config, avctx->hw_frames_ctx);
  597. if (err)
  598. goto fail;
  599. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  600. avctx->coded_width, avctx->coded_height,
  601. VA_PROGRESSIVE,
  602. ctx->hwfc->surface_ids,
  603. ctx->hwfc->nb_surfaces,
  604. &ctx->va_context);
  605. if (vas != VA_STATUS_SUCCESS) {
  606. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  607. "context: %d (%s).\n", vas, vaErrorStr(vas));
  608. err = AVERROR(EIO);
  609. goto fail;
  610. }
  611. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  612. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  613. #if FF_API_STRUCT_VAAPI_CONTEXT
  614. }
  615. #endif
  616. return 0;
  617. fail:
  618. ff_vaapi_decode_uninit(avctx);
  619. return err;
  620. }
  621. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  622. {
  623. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  624. VAStatus vas;
  625. #if FF_API_STRUCT_VAAPI_CONTEXT
  626. if (ctx->have_old_context) {
  627. av_buffer_unref(&ctx->device_ref);
  628. } else {
  629. #endif
  630. if (ctx->va_context != VA_INVALID_ID) {
  631. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  632. if (vas != VA_STATUS_SUCCESS) {
  633. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  634. "context %#x: %d (%s).\n",
  635. ctx->va_context, vas, vaErrorStr(vas));
  636. }
  637. }
  638. if (ctx->va_config != VA_INVALID_ID) {
  639. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  640. if (vas != VA_STATUS_SUCCESS) {
  641. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  642. "configuration %#x: %d (%s).\n",
  643. ctx->va_config, vas, vaErrorStr(vas));
  644. }
  645. }
  646. #if FF_API_STRUCT_VAAPI_CONTEXT
  647. }
  648. #endif
  649. return 0;
  650. }