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.

548 lines
18KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/avassert.h"
  19. #include "libavutil/common.h"
  20. #include "avcodec.h"
  21. #include "internal.h"
  22. #include "vaapi_decode.h"
  23. int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx,
  24. VAAPIDecodePicture *pic,
  25. int type,
  26. const void *data,
  27. size_t size)
  28. {
  29. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  30. VAStatus vas;
  31. VABufferID buffer;
  32. av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
  33. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  34. type, size, 1, (void*)data, &buffer);
  35. if (vas != VA_STATUS_SUCCESS) {
  36. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
  37. "buffer (type %d): %d (%s).\n",
  38. type, vas, vaErrorStr(vas));
  39. return AVERROR(EIO);
  40. }
  41. pic->param_buffers[pic->nb_param_buffers++] = buffer;
  42. av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) "
  43. "is %#x.\n", type, size, buffer);
  44. return 0;
  45. }
  46. int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx,
  47. VAAPIDecodePicture *pic,
  48. const void *params_data,
  49. size_t params_size,
  50. const void *slice_data,
  51. size_t slice_size)
  52. {
  53. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  54. VAStatus vas;
  55. int index;
  56. av_assert0(pic->nb_slices <= pic->slices_allocated);
  57. if (pic->nb_slices == pic->slices_allocated) {
  58. if (pic->slices_allocated > 0)
  59. pic->slices_allocated *= 2;
  60. else
  61. pic->slices_allocated = 64;
  62. pic->slice_buffers =
  63. av_realloc_array(pic->slice_buffers,
  64. pic->slices_allocated,
  65. 2 * sizeof(*pic->slice_buffers));
  66. if (!pic->slice_buffers)
  67. return AVERROR(ENOMEM);
  68. }
  69. av_assert0(pic->nb_slices + 1 <= pic->slices_allocated);
  70. index = 2 * pic->nb_slices;
  71. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  72. VASliceParameterBufferType,
  73. params_size, 1, (void*)params_data,
  74. &pic->slice_buffers[index]);
  75. if (vas != VA_STATUS_SUCCESS) {
  76. av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
  77. "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
  78. return AVERROR(EIO);
  79. }
  80. av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) "
  81. "is %#x.\n", pic->nb_slices, params_size,
  82. pic->slice_buffers[index]);
  83. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  84. VASliceDataBufferType,
  85. slice_size, 1, (void*)slice_data,
  86. &pic->slice_buffers[index + 1]);
  87. if (vas != VA_STATUS_SUCCESS) {
  88. av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
  89. "data buffer (size %zu): %d (%s).\n",
  90. slice_size, vas, vaErrorStr(vas));
  91. vaDestroyBuffer(ctx->hwctx->display,
  92. pic->slice_buffers[index]);
  93. return AVERROR(EIO);
  94. }
  95. av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) "
  96. "is %#x.\n", pic->nb_slices, slice_size,
  97. pic->slice_buffers[index + 1]);
  98. ++pic->nb_slices;
  99. return 0;
  100. }
  101. static void ff_vaapi_decode_destroy_buffers(AVCodecContext *avctx,
  102. VAAPIDecodePicture *pic)
  103. {
  104. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  105. VAStatus vas;
  106. int i;
  107. for (i = 0; i < pic->nb_param_buffers; i++) {
  108. vas = vaDestroyBuffer(ctx->hwctx->display,
  109. pic->param_buffers[i]);
  110. if (vas != VA_STATUS_SUCCESS) {
  111. av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
  112. "parameter buffer %#x: %d (%s).\n",
  113. pic->param_buffers[i], vas, vaErrorStr(vas));
  114. }
  115. }
  116. for (i = 0; i < 2 * pic->nb_slices; i++) {
  117. vas = vaDestroyBuffer(ctx->hwctx->display,
  118. pic->slice_buffers[i]);
  119. if (vas != VA_STATUS_SUCCESS) {
  120. av_log(avctx, AV_LOG_ERROR, "Failed to destroy slice "
  121. "slice buffer %#x: %d (%s).\n",
  122. pic->slice_buffers[i], vas, vaErrorStr(vas));
  123. }
  124. }
  125. }
  126. int ff_vaapi_decode_issue(AVCodecContext *avctx,
  127. VAAPIDecodePicture *pic)
  128. {
  129. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  130. VAStatus vas;
  131. int err;
  132. av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
  133. pic->output_surface);
  134. vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
  135. pic->output_surface);
  136. if (vas != VA_STATUS_SUCCESS) {
  137. av_log(avctx, AV_LOG_ERROR, "Failed to begin picture decode "
  138. "issue: %d (%s).\n", vas, vaErrorStr(vas));
  139. err = AVERROR(EIO);
  140. goto fail_with_picture;
  141. }
  142. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  143. pic->param_buffers, pic->nb_param_buffers);
  144. if (vas != VA_STATUS_SUCCESS) {
  145. av_log(avctx, AV_LOG_ERROR, "Failed to upload decode "
  146. "parameters: %d (%s).\n", vas, vaErrorStr(vas));
  147. err = AVERROR(EIO);
  148. goto fail_with_picture;
  149. }
  150. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  151. pic->slice_buffers, 2 * pic->nb_slices);
  152. if (vas != VA_STATUS_SUCCESS) {
  153. av_log(avctx, AV_LOG_ERROR, "Failed to upload slices: "
  154. "%d (%s).\n", vas, vaErrorStr(vas));
  155. err = AVERROR(EIO);
  156. goto fail_with_picture;
  157. }
  158. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  159. if (vas != VA_STATUS_SUCCESS) {
  160. av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
  161. "issue: %d (%s).\n", vas, vaErrorStr(vas));
  162. err = AVERROR(EIO);
  163. if (ctx->hwctx->driver_quirks &
  164. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
  165. goto fail;
  166. else
  167. goto fail_at_end;
  168. }
  169. if (ctx->hwctx->driver_quirks &
  170. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
  171. ff_vaapi_decode_destroy_buffers(avctx, pic);
  172. pic->nb_param_buffers = 0;
  173. pic->nb_slices = 0;
  174. pic->slices_allocated = 0;
  175. av_freep(&pic->slice_buffers);
  176. return 0;
  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. return err;
  187. }
  188. int ff_vaapi_decode_cancel(AVCodecContext *avctx,
  189. VAAPIDecodePicture *pic)
  190. {
  191. ff_vaapi_decode_destroy_buffers(avctx, pic);
  192. pic->nb_param_buffers = 0;
  193. pic->nb_slices = 0;
  194. pic->slices_allocated = 0;
  195. av_freep(&pic->slice_buffers);
  196. return 0;
  197. }
  198. static const struct {
  199. enum AVCodecID codec_id;
  200. int codec_profile;
  201. VAProfile va_profile;
  202. } vaapi_profile_map[] = {
  203. #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
  204. MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
  205. MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
  206. MAP(H263, UNKNOWN, H263Baseline),
  207. MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
  208. MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
  209. MPEG4AdvancedSimple),
  210. MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
  211. MAP(H264, H264_CONSTRAINED_BASELINE,
  212. H264ConstrainedBaseline),
  213. MAP(H264, H264_BASELINE, H264Baseline),
  214. MAP(H264, H264_MAIN, H264Main ),
  215. MAP(H264, H264_HIGH, H264High ),
  216. #if VA_CHECK_VERSION(0, 37, 0)
  217. MAP(HEVC, HEVC_MAIN, HEVCMain ),
  218. #endif
  219. MAP(WMV3, VC1_SIMPLE, VC1Simple ),
  220. MAP(WMV3, VC1_MAIN, VC1Main ),
  221. MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
  222. MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
  223. MAP(VC1, VC1_SIMPLE, VC1Simple ),
  224. MAP(VC1, VC1_MAIN, VC1Main ),
  225. MAP(VC1, VC1_COMPLEX, VC1Advanced ),
  226. MAP(VC1, VC1_ADVANCED, VC1Advanced ),
  227. #if VA_CHECK_VERSION(0, 35, 0)
  228. MAP(VP8, UNKNOWN, VP8Version0_3 ),
  229. #endif
  230. #if VA_CHECK_VERSION(0, 38, 0)
  231. MAP(VP9, VP9_0, VP9Profile0 ),
  232. #endif
  233. #undef MAP
  234. };
  235. static int vaapi_decode_make_config(AVCodecContext *avctx)
  236. {
  237. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  238. AVVAAPIHWConfig *hwconfig = NULL;
  239. AVHWFramesConstraints *constraints = NULL;
  240. VAStatus vas;
  241. int err, i, j;
  242. const AVCodecDescriptor *codec_desc;
  243. VAProfile profile, *profile_list = NULL;
  244. int profile_count, exact_match, alt_profile;
  245. // Allowing a profile mismatch can be useful because streams may
  246. // over-declare their required capabilities - in particular, many
  247. // H.264 baseline profile streams (notably some of those in FATE)
  248. // only use the feature set of constrained baseline. This flag
  249. // would have to be be set by some external means in order to
  250. // actually be useful. (AV_HWACCEL_FLAG_IGNORE_PROFILE?)
  251. int allow_profile_mismatch = 0;
  252. codec_desc = avcodec_descriptor_get(avctx->codec_id);
  253. if (!codec_desc) {
  254. err = AVERROR(EINVAL);
  255. goto fail;
  256. }
  257. profile_count = vaMaxNumProfiles(ctx->hwctx->display);
  258. profile_list = av_malloc_array(profile_count,
  259. sizeof(VAProfile));
  260. if (!profile_list) {
  261. err = AVERROR(ENOMEM);
  262. goto fail;
  263. }
  264. vas = vaQueryConfigProfiles(ctx->hwctx->display,
  265. profile_list, &profile_count);
  266. if (vas != VA_STATUS_SUCCESS) {
  267. av_log(ctx, AV_LOG_ERROR, "Failed to query profiles: "
  268. "%d (%s).\n", vas, vaErrorStr(vas));
  269. err = AVERROR(ENOSYS);
  270. goto fail;
  271. }
  272. profile = VAProfileNone;
  273. exact_match = 0;
  274. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
  275. int profile_match = 0;
  276. if (avctx->codec_id != vaapi_profile_map[i].codec_id)
  277. continue;
  278. if (avctx->profile == vaapi_profile_map[i].codec_profile ||
  279. vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
  280. profile_match = 1;
  281. profile = vaapi_profile_map[i].va_profile;
  282. for (j = 0; j < profile_count; j++) {
  283. if (profile == profile_list[j]) {
  284. exact_match = profile_match;
  285. break;
  286. }
  287. }
  288. if (j < profile_count) {
  289. if (exact_match)
  290. break;
  291. alt_profile = vaapi_profile_map[i].codec_profile;
  292. }
  293. }
  294. av_freep(&profile_list);
  295. if (profile == VAProfileNone) {
  296. av_log(ctx, AV_LOG_ERROR, "No support for codec %s "
  297. "profile %d.\n", codec_desc->name, avctx->profile);
  298. err = AVERROR(ENOSYS);
  299. goto fail;
  300. }
  301. if (!exact_match) {
  302. if (allow_profile_mismatch) {
  303. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  304. "supported for hardware decode.\n",
  305. codec_desc->name, avctx->profile);
  306. av_log(avctx, AV_LOG_WARNING, "Using possibly-"
  307. "incompatible profile %d instead.\n",
  308. alt_profile);
  309. } else {
  310. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  311. "supported for hardware decode.\n",
  312. codec_desc->name, avctx->profile);
  313. err = AVERROR(EINVAL);
  314. goto fail;
  315. }
  316. }
  317. ctx->va_profile = profile;
  318. ctx->va_entrypoint = VAEntrypointVLD;
  319. vas = vaCreateConfig(ctx->hwctx->display, ctx->va_profile,
  320. ctx->va_entrypoint, NULL, 0,
  321. &ctx->va_config);
  322. if (vas != VA_STATUS_SUCCESS) {
  323. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  324. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  325. err = AVERROR(EIO);
  326. goto fail;
  327. }
  328. hwconfig = av_hwdevice_hwconfig_alloc(ctx->frames->device_ref);
  329. if (!hwconfig) {
  330. err = AVERROR(ENOMEM);
  331. goto fail;
  332. }
  333. hwconfig->config_id = ctx->va_config;
  334. constraints =
  335. av_hwdevice_get_hwframe_constraints(ctx->frames->device_ref,
  336. hwconfig);
  337. if (!constraints) {
  338. // Ignore.
  339. } else {
  340. if (avctx->coded_width < constraints->min_width ||
  341. avctx->coded_height < constraints->min_height ||
  342. avctx->coded_width > constraints->max_width ||
  343. avctx->coded_height > constraints->max_height) {
  344. av_log(ctx, AV_LOG_ERROR, "Hardware does not support image "
  345. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  346. avctx->coded_width, avctx->coded_height,
  347. constraints->min_width, constraints->max_width,
  348. constraints->min_height, constraints->max_height);
  349. err = AVERROR(EINVAL);
  350. goto fail;
  351. }
  352. }
  353. av_hwframe_constraints_free(&constraints);
  354. av_freep(&hwconfig);
  355. return 0;
  356. fail:
  357. av_hwframe_constraints_free(&constraints);
  358. av_freep(&hwconfig);
  359. if (ctx->va_config != VA_INVALID_ID) {
  360. vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  361. ctx->va_config = VA_INVALID_ID;
  362. }
  363. av_freep(&profile_list);
  364. return err;
  365. }
  366. int ff_vaapi_decode_init(AVCodecContext *avctx)
  367. {
  368. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  369. VAStatus vas;
  370. int err;
  371. ctx->va_config = VA_INVALID_ID;
  372. ctx->va_context = VA_INVALID_ID;
  373. #if FF_API_VAAPI_CONTEXT
  374. if (avctx->hwaccel_context) {
  375. av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
  376. "vaapi_context in decode.\n");
  377. ctx->have_old_context = 1;
  378. ctx->old_context = avctx->hwaccel_context;
  379. // Really we only want the VAAPI device context, but this
  380. // allocates a whole generic device context because we don't
  381. // have any other way to determine how big it should be.
  382. ctx->device_ref =
  383. av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
  384. if (!ctx->device_ref) {
  385. err = AVERROR(ENOMEM);
  386. goto fail;
  387. }
  388. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  389. ctx->hwctx = ctx->device->hwctx;
  390. ctx->hwctx->display = ctx->old_context->display;
  391. // The old VAAPI decode setup assumed this quirk was always
  392. // present, so set it here to avoid the behaviour changing.
  393. ctx->hwctx->driver_quirks =
  394. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  395. } else
  396. #endif
  397. if (avctx->hw_frames_ctx) {
  398. // This structure has a shorter lifetime than the enclosing
  399. // AVCodecContext, so we inherit the references from there
  400. // and do not need to make separate ones.
  401. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  402. ctx->hwfc = ctx->frames->hwctx;
  403. ctx->device = ctx->frames->device_ctx;
  404. ctx->hwctx = ctx->device->hwctx;
  405. } else {
  406. av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
  407. "required for VAAPI decoding.\n");
  408. err = AVERROR(EINVAL);
  409. goto fail;
  410. }
  411. #if FF_API_VAAPI_CONTEXT
  412. if (ctx->have_old_context) {
  413. ctx->va_config = ctx->old_context->config_id;
  414. ctx->va_context = ctx->old_context->context_id;
  415. av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
  416. "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
  417. } else {
  418. #endif
  419. err = vaapi_decode_make_config(avctx);
  420. if (err)
  421. goto fail;
  422. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  423. avctx->coded_width, avctx->coded_height,
  424. VA_PROGRESSIVE,
  425. ctx->hwfc->surface_ids,
  426. ctx->hwfc->nb_surfaces,
  427. &ctx->va_context);
  428. if (vas != VA_STATUS_SUCCESS) {
  429. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  430. "context: %d (%s).\n", vas, vaErrorStr(vas));
  431. err = AVERROR(EIO);
  432. goto fail;
  433. }
  434. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  435. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  436. #if FF_API_VAAPI_CONTEXT
  437. }
  438. #endif
  439. return 0;
  440. fail:
  441. ff_vaapi_decode_uninit(avctx);
  442. return err;
  443. }
  444. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  445. {
  446. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  447. VAStatus vas;
  448. #if FF_API_VAAPI_CONTEXT
  449. if (ctx->have_old_context) {
  450. av_buffer_unref(&ctx->device_ref);
  451. } else {
  452. #endif
  453. if (ctx->va_context != VA_INVALID_ID) {
  454. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  455. if (vas != VA_STATUS_SUCCESS) {
  456. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  457. "context %#x: %d (%s).\n",
  458. ctx->va_context, vas, vaErrorStr(vas));
  459. }
  460. }
  461. if (ctx->va_config != VA_INVALID_ID) {
  462. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  463. if (vas != VA_STATUS_SUCCESS) {
  464. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  465. "configuration %#x: %d (%s).\n",
  466. ctx->va_config, vas, vaErrorStr(vas));
  467. }
  468. }
  469. #if FF_API_VAAPI_CONTEXT
  470. }
  471. #endif
  472. return 0;
  473. }