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.

535 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, i;
  132. av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
  133. pic->output_surface);
  134. for (i = 0; i < pic->nb_param_buffers; i++)
  135. vaUnmapBuffer(ctx->hwctx->display, pic->param_buffers[i]);
  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 (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 (ctx->hwctx->driver_quirks &
  172. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
  173. ff_vaapi_decode_destroy_buffers(avctx, pic);
  174. pic->nb_slices = 0;
  175. pic->slices_allocated = 0;
  176. av_freep(&pic->slice_buffers);
  177. return 0;
  178. fail_with_picture:
  179. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  180. if (vas != VA_STATUS_SUCCESS) {
  181. av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
  182. "after error: %d (%s).\n", vas, vaErrorStr(vas));
  183. }
  184. fail:
  185. ff_vaapi_decode_destroy_buffers(avctx, pic);
  186. fail_at_end:
  187. return err;
  188. }
  189. int ff_vaapi_decode_cancel(AVCodecContext *avctx,
  190. VAAPIDecodePicture *pic)
  191. {
  192. ff_vaapi_decode_destroy_buffers(avctx, pic);
  193. pic->nb_param_buffers = 0;
  194. pic->nb_slices = 0;
  195. pic->slices_allocated = 0;
  196. av_freep(&pic->slice_buffers);
  197. return 0;
  198. }
  199. static const struct {
  200. enum AVCodecID codec_id;
  201. int codec_profile;
  202. VAProfile va_profile;
  203. } vaapi_profile_map[] = {
  204. #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
  205. MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
  206. MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
  207. MAP(H263, UNKNOWN, H263Baseline),
  208. MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
  209. MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
  210. MPEG4AdvancedSimple),
  211. MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
  212. MAP(H264, H264_CONSTRAINED_BASELINE,
  213. H264ConstrainedBaseline),
  214. MAP(H264, H264_BASELINE, H264Baseline),
  215. MAP(H264, H264_MAIN, H264Main ),
  216. MAP(H264, H264_HIGH, H264High ),
  217. #if VA_CHECK_VERSION(0, 37, 0)
  218. MAP(HEVC, HEVC_MAIN, HEVCMain ),
  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(ctx, 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. 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 (avctx->hwaccel_context) {
  374. av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
  375. "vaapi_context in decode.\n");
  376. ctx->have_old_context = 1;
  377. ctx->old_context = avctx->hwaccel_context;
  378. // Really we only want the VAAPI device context, but this
  379. // allocates a whole generic device context because we don't
  380. // have any other way to determine how big it should be.
  381. ctx->device_ref =
  382. av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
  383. if (!ctx->device_ref) {
  384. err = AVERROR(ENOMEM);
  385. goto fail;
  386. }
  387. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  388. ctx->hwctx = ctx->device->hwctx;
  389. ctx->hwctx->display = ctx->old_context->display;
  390. // The old VAAPI decode setup assumed this quirk was always
  391. // present, so set it here to avoid the behaviour changing.
  392. ctx->hwctx->driver_quirks =
  393. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  394. } else if (avctx->hw_frames_ctx) {
  395. // This structure has a shorter lifetime than the enclosing
  396. // AVCodecContext, so we inherit the references from there
  397. // and do not need to make separate ones.
  398. ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  399. ctx->hwfc = ctx->frames->hwctx;
  400. ctx->device = ctx->frames->device_ctx;
  401. ctx->hwctx = ctx->device->hwctx;
  402. } else {
  403. av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
  404. "required for VAAPI decoding.\n");
  405. err = AVERROR(EINVAL);
  406. goto fail;
  407. }
  408. if (ctx->have_old_context) {
  409. ctx->va_config = ctx->old_context->config_id;
  410. ctx->va_context = ctx->old_context->context_id;
  411. av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
  412. "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
  413. } else {
  414. err = vaapi_decode_make_config(avctx);
  415. if (err)
  416. goto fail;
  417. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  418. avctx->coded_width, avctx->coded_height,
  419. VA_PROGRESSIVE,
  420. ctx->hwfc->surface_ids,
  421. ctx->hwfc->nb_surfaces,
  422. &ctx->va_context);
  423. if (vas != VA_STATUS_SUCCESS) {
  424. av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
  425. "context: %d (%s).\n", vas, vaErrorStr(vas));
  426. err = AVERROR(EIO);
  427. goto fail;
  428. }
  429. av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
  430. "%#x/%#x.\n", ctx->va_config, ctx->va_context);
  431. }
  432. return 0;
  433. fail:
  434. ff_vaapi_decode_uninit(avctx);
  435. return err;
  436. }
  437. int ff_vaapi_decode_uninit(AVCodecContext *avctx)
  438. {
  439. VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
  440. VAStatus vas;
  441. if (ctx->have_old_context) {
  442. av_buffer_unref(&ctx->device_ref);
  443. } else {
  444. if (ctx->va_context != VA_INVALID_ID) {
  445. vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  446. if (vas != VA_STATUS_SUCCESS) {
  447. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  448. "context %#x: %d (%s).\n",
  449. ctx->va_context, vas, vaErrorStr(vas));
  450. }
  451. }
  452. if (ctx->va_config != VA_INVALID_ID) {
  453. vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  454. if (vas != VA_STATUS_SUCCESS) {
  455. av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
  456. "configuration %#x: %d (%s).\n",
  457. ctx->va_config, vas, vaErrorStr(vas));
  458. }
  459. }
  460. }
  461. return 0;
  462. }