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.

549 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. MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ),
  219. #endif
  220. MAP(WMV3, VC1_SIMPLE, VC1Simple ),
  221. MAP(WMV3, VC1_MAIN, VC1Main ),
  222. MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
  223. MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
  224. MAP(VC1, VC1_SIMPLE, VC1Simple ),
  225. MAP(VC1, VC1_MAIN, VC1Main ),
  226. MAP(VC1, VC1_COMPLEX, VC1Advanced ),
  227. MAP(VC1, VC1_ADVANCED, VC1Advanced ),
  228. #if VA_CHECK_VERSION(0, 35, 0)
  229. MAP(VP8, UNKNOWN, VP8Version0_3 ),
  230. #endif
  231. #if VA_CHECK_VERSION(0, 38, 0)
  232. MAP(VP9, VP9_0, VP9Profile0 ),
  233. #endif
  234. #undef MAP
  235. };
  236. static int vaapi_decode_make_config(AVCodecContext *avctx)
  237. {
  238. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  239. AVVAAPIHWConfig *hwconfig = NULL;
  240. AVHWFramesConstraints *constraints = NULL;
  241. VAStatus vas;
  242. int err, i, j;
  243. const AVCodecDescriptor *codec_desc;
  244. VAProfile profile, *profile_list = NULL;
  245. int profile_count, exact_match, alt_profile;
  246. // Allowing a profile mismatch can be useful because streams may
  247. // over-declare their required capabilities - in particular, many
  248. // H.264 baseline profile streams (notably some of those in FATE)
  249. // only use the feature set of constrained baseline. This flag
  250. // would have to be be set by some external means in order to
  251. // actually be useful. (AV_HWACCEL_FLAG_IGNORE_PROFILE?)
  252. int allow_profile_mismatch = 0;
  253. codec_desc = avcodec_descriptor_get(avctx->codec_id);
  254. if (!codec_desc) {
  255. err = AVERROR(EINVAL);
  256. goto fail;
  257. }
  258. profile_count = vaMaxNumProfiles(ctx->hwctx->display);
  259. profile_list = av_malloc_array(profile_count,
  260. sizeof(VAProfile));
  261. if (!profile_list) {
  262. err = AVERROR(ENOMEM);
  263. goto fail;
  264. }
  265. vas = vaQueryConfigProfiles(ctx->hwctx->display,
  266. profile_list, &profile_count);
  267. if (vas != VA_STATUS_SUCCESS) {
  268. av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
  269. "%d (%s).\n", vas, vaErrorStr(vas));
  270. err = AVERROR(ENOSYS);
  271. goto fail;
  272. }
  273. profile = VAProfileNone;
  274. exact_match = 0;
  275. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
  276. int profile_match = 0;
  277. if (avctx->codec_id != vaapi_profile_map[i].codec_id)
  278. continue;
  279. if (avctx->profile == vaapi_profile_map[i].codec_profile ||
  280. vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
  281. profile_match = 1;
  282. profile = vaapi_profile_map[i].va_profile;
  283. for (j = 0; j < profile_count; j++) {
  284. if (profile == profile_list[j]) {
  285. exact_match = profile_match;
  286. break;
  287. }
  288. }
  289. if (j < profile_count) {
  290. if (exact_match)
  291. break;
  292. alt_profile = vaapi_profile_map[i].codec_profile;
  293. }
  294. }
  295. av_freep(&profile_list);
  296. if (profile == VAProfileNone) {
  297. av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
  298. "profile %d.\n", codec_desc->name, avctx->profile);
  299. err = AVERROR(ENOSYS);
  300. goto fail;
  301. }
  302. if (!exact_match) {
  303. if (allow_profile_mismatch) {
  304. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  305. "supported for hardware decode.\n",
  306. codec_desc->name, avctx->profile);
  307. av_log(avctx, AV_LOG_WARNING, "Using possibly-"
  308. "incompatible profile %d instead.\n",
  309. alt_profile);
  310. } else {
  311. av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
  312. "supported for hardware decode.\n",
  313. codec_desc->name, avctx->profile);
  314. err = AVERROR(EINVAL);
  315. goto fail;
  316. }
  317. }
  318. ctx->va_profile = profile;
  319. ctx->va_entrypoint = VAEntrypointVLD;
  320. vas = vaCreateConfig(ctx->hwctx->display, ctx->va_profile,
  321. ctx->va_entrypoint, NULL, 0,
  322. &ctx->va_config);
  323. if (vas != VA_STATUS_SUCCESS) {
  324. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  325. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  326. err = AVERROR(EIO);
  327. goto fail;
  328. }
  329. hwconfig = av_hwdevice_hwconfig_alloc(ctx->frames->device_ref);
  330. if (!hwconfig) {
  331. err = AVERROR(ENOMEM);
  332. goto fail;
  333. }
  334. hwconfig->config_id = ctx->va_config;
  335. constraints =
  336. av_hwdevice_get_hwframe_constraints(ctx->frames->device_ref,
  337. hwconfig);
  338. if (!constraints) {
  339. // Ignore.
  340. } else {
  341. if (avctx->coded_width < constraints->min_width ||
  342. avctx->coded_height < constraints->min_height ||
  343. avctx->coded_width > constraints->max_width ||
  344. avctx->coded_height > constraints->max_height) {
  345. av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
  346. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  347. avctx->coded_width, avctx->coded_height,
  348. constraints->min_width, constraints->max_width,
  349. constraints->min_height, constraints->max_height);
  350. err = AVERROR(EINVAL);
  351. goto fail;
  352. }
  353. }
  354. av_hwframe_constraints_free(&constraints);
  355. av_freep(&hwconfig);
  356. return 0;
  357. fail:
  358. av_hwframe_constraints_free(&constraints);
  359. av_freep(&hwconfig);
  360. if (ctx->va_config != VA_INVALID_ID) {
  361. vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  362. ctx->va_config = VA_INVALID_ID;
  363. }
  364. av_freep(&profile_list);
  365. return err;
  366. }
  367. int ff_vaapi_decode_init(AVCodecContext *avctx)
  368. {
  369. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  370. VAStatus vas;
  371. int err;
  372. ctx->va_config = VA_INVALID_ID;
  373. ctx->va_context = VA_INVALID_ID;
  374. #if FF_API_VAAPI_CONTEXT
  375. if (avctx->hwaccel_context) {
  376. av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
  377. "vaapi_context in decode.\n");
  378. ctx->have_old_context = 1;
  379. ctx->old_context = avctx->hwaccel_context;
  380. // Really we only want the VAAPI device context, but this
  381. // allocates a whole generic device context because we don't
  382. // have any other way to determine how big it should be.
  383. ctx->device_ref =
  384. av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
  385. if (!ctx->device_ref) {
  386. err = AVERROR(ENOMEM);
  387. goto fail;
  388. }
  389. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  390. ctx->hwctx = ctx->device->hwctx;
  391. ctx->hwctx->display = ctx->old_context->display;
  392. // The old VAAPI decode setup assumed this quirk was always
  393. // present, so set it here to avoid the behaviour changing.
  394. ctx->hwctx->driver_quirks =
  395. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  396. } else
  397. #endif
  398. if (avctx->hw_frames_ctx) {
  399. // This structure has a shorter lifetime than the enclosing
  400. // AVCodecContext, so we inherit the references from there
  401. // and do not need to make separate ones.
  402. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  403. ctx->hwfc = ctx->frames->hwctx;
  404. ctx->device = ctx->frames->device_ctx;
  405. ctx->hwctx = ctx->device->hwctx;
  406. } else {
  407. av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
  408. "required for VAAPI decoding.\n");
  409. err = AVERROR(EINVAL);
  410. goto fail;
  411. }
  412. #if FF_API_VAAPI_CONTEXT
  413. if (ctx->have_old_context) {
  414. ctx->va_config = ctx->old_context->config_id;
  415. ctx->va_context = ctx->old_context->context_id;
  416. av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
  417. "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
  418. } else {
  419. #endif
  420. err = vaapi_decode_make_config(avctx);
  421. if (err)
  422. goto fail;
  423. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  424. avctx->coded_width, avctx->coded_height,
  425. VA_PROGRESSIVE,
  426. ctx->hwfc->surface_ids,
  427. ctx->hwfc->nb_surfaces,
  428. &ctx->va_context);
  429. if (vas != VA_STATUS_SUCCESS) {
  430. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  431. "context: %d (%s).\n", vas, vaErrorStr(vas));
  432. err = AVERROR(EIO);
  433. goto fail;
  434. }
  435. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  436. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  437. #if FF_API_VAAPI_CONTEXT
  438. }
  439. #endif
  440. return 0;
  441. fail:
  442. ff_vaapi_decode_uninit(avctx);
  443. return err;
  444. }
  445. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  446. {
  447. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  448. VAStatus vas;
  449. #if FF_API_VAAPI_CONTEXT
  450. if (ctx->have_old_context) {
  451. av_buffer_unref(&ctx->device_ref);
  452. } else {
  453. #endif
  454. if (ctx->va_context != VA_INVALID_ID) {
  455. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  456. if (vas != VA_STATUS_SUCCESS) {
  457. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  458. "context %#x: %d (%s).\n",
  459. ctx->va_context, vas, vaErrorStr(vas));
  460. }
  461. }
  462. if (ctx->va_config != VA_INVALID_ID) {
  463. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  464. if (vas != VA_STATUS_SUCCESS) {
  465. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  466. "configuration %#x: %d (%s).\n",
  467. ctx->va_config, vas, vaErrorStr(vas));
  468. }
  469. }
  470. #if FF_API_VAAPI_CONTEXT
  471. }
  472. #endif
  473. return 0;
  474. }