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.

551 lines
18KB

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