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.

469 lines
15KB

  1. /*
  2. * Video Decode and Presentation API for UNIX (VDPAU) is used for
  3. * HW decode acceleration for MPEG-1/2, MPEG-4 ASP, H.264 and VC-1.
  4. *
  5. * Copyright (c) 2008 NVIDIA
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <limits.h>
  24. #include "avcodec.h"
  25. #include "decode.h"
  26. #include "internal.h"
  27. #include "h264dec.h"
  28. #include "vc1.h"
  29. #include "vdpau.h"
  30. #include "vdpau_internal.h"
  31. // XXX: at the time of adding this ifdefery, av_assert* wasn't use outside.
  32. // When dropping it, make sure other av_assert* were not added since then.
  33. /**
  34. * @addtogroup VDPAU_Decoding
  35. *
  36. * @{
  37. */
  38. static int vdpau_error(VdpStatus status)
  39. {
  40. switch (status) {
  41. case VDP_STATUS_OK:
  42. return 0;
  43. case VDP_STATUS_NO_IMPLEMENTATION:
  44. return AVERROR(ENOSYS);
  45. case VDP_STATUS_DISPLAY_PREEMPTED:
  46. return AVERROR(EIO);
  47. case VDP_STATUS_INVALID_HANDLE:
  48. return AVERROR(EBADF);
  49. case VDP_STATUS_INVALID_POINTER:
  50. return AVERROR(EFAULT);
  51. case VDP_STATUS_RESOURCES:
  52. return AVERROR(ENOBUFS);
  53. case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
  54. return AVERROR(EXDEV);
  55. case VDP_STATUS_ERROR:
  56. return AVERROR(EIO);
  57. default:
  58. return AVERROR(EINVAL);
  59. }
  60. }
  61. AVVDPAUContext *av_alloc_vdpaucontext(void)
  62. {
  63. return av_vdpau_alloc_context();
  64. }
  65. MAKE_ACCESSORS(AVVDPAUContext, vdpau_hwaccel, AVVDPAU_Render2, render2)
  66. int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
  67. VdpChromaType *type,
  68. uint32_t *width, uint32_t *height)
  69. {
  70. VdpChromaType t;
  71. uint32_t w = avctx->coded_width;
  72. uint32_t h = avctx->coded_height;
  73. /* See <vdpau/vdpau.h> for per-type alignment constraints. */
  74. switch (avctx->sw_pix_fmt) {
  75. case AV_PIX_FMT_YUV420P:
  76. case AV_PIX_FMT_YUVJ420P:
  77. t = VDP_CHROMA_TYPE_420;
  78. w = (w + 1) & ~1;
  79. h = (h + 3) & ~3;
  80. break;
  81. case AV_PIX_FMT_YUV422P:
  82. case AV_PIX_FMT_YUVJ422P:
  83. t = VDP_CHROMA_TYPE_422;
  84. w = (w + 1) & ~1;
  85. h = (h + 1) & ~1;
  86. break;
  87. case AV_PIX_FMT_YUV444P:
  88. case AV_PIX_FMT_YUVJ444P:
  89. t = VDP_CHROMA_TYPE_444;
  90. h = (h + 1) & ~1;
  91. break;
  92. default:
  93. return AVERROR(ENOSYS);
  94. }
  95. if (type)
  96. *type = t;
  97. if (width)
  98. *width = w;
  99. if (height)
  100. *height = h;
  101. return 0;
  102. }
  103. int ff_vdpau_common_frame_params(AVCodecContext *avctx,
  104. AVBufferRef *hw_frames_ctx)
  105. {
  106. AVHWFramesContext *hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
  107. VdpChromaType type;
  108. uint32_t width;
  109. uint32_t height;
  110. if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
  111. return AVERROR(EINVAL);
  112. hw_frames->format = AV_PIX_FMT_VDPAU;
  113. hw_frames->sw_format = avctx->sw_pix_fmt;
  114. hw_frames->width = width;
  115. hw_frames->height = height;
  116. return 0;
  117. }
  118. int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
  119. int level)
  120. {
  121. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  122. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  123. VdpVideoSurfaceQueryCapabilities *surface_query_caps;
  124. VdpDecoderQueryCapabilities *decoder_query_caps;
  125. VdpDecoderCreate *create;
  126. VdpGetInformationString *info;
  127. const char *info_string;
  128. void *func;
  129. VdpStatus status;
  130. VdpBool supported;
  131. uint32_t max_level, max_mb, max_width, max_height;
  132. VdpChromaType type;
  133. uint32_t width;
  134. uint32_t height;
  135. int ret;
  136. vdctx->width = UINT32_MAX;
  137. vdctx->height = UINT32_MAX;
  138. if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
  139. return AVERROR(ENOSYS);
  140. if (hwctx) {
  141. hwctx->reset = 0;
  142. if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
  143. vdctx->decoder = hwctx->context.decoder;
  144. vdctx->render = hwctx->context.render;
  145. vdctx->device = VDP_INVALID_HANDLE;
  146. return 0; /* Decoder created by user */
  147. }
  148. vdctx->device = hwctx->device;
  149. vdctx->get_proc_address = hwctx->get_proc_address;
  150. if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
  151. level = 0;
  152. if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
  153. type != VDP_CHROMA_TYPE_420)
  154. return AVERROR(ENOSYS);
  155. } else {
  156. AVHWFramesContext *frames_ctx;
  157. AVVDPAUDeviceContext *dev_ctx;
  158. ret = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VDPAU);
  159. if (ret < 0)
  160. return ret;
  161. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  162. dev_ctx = frames_ctx->device_ctx->hwctx;
  163. vdctx->device = dev_ctx->device;
  164. vdctx->get_proc_address = dev_ctx->get_proc_address;
  165. if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
  166. level = 0;
  167. }
  168. if (level < 0)
  169. return AVERROR(ENOTSUP);
  170. status = vdctx->get_proc_address(vdctx->device,
  171. VDP_FUNC_ID_GET_INFORMATION_STRING,
  172. &func);
  173. if (status != VDP_STATUS_OK)
  174. return vdpau_error(status);
  175. else
  176. info = func;
  177. status = info(&info_string);
  178. if (status != VDP_STATUS_OK)
  179. return vdpau_error(status);
  180. if (avctx->codec_id == AV_CODEC_ID_HEVC && strncmp(info_string, "NVIDIA ", 7) == 0 &&
  181. !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
  182. av_log(avctx, AV_LOG_VERBOSE, "HEVC with NVIDIA VDPAU drivers is buggy, skipping.\n");
  183. return AVERROR(ENOTSUP);
  184. }
  185. status = vdctx->get_proc_address(vdctx->device,
  186. VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
  187. &func);
  188. if (status != VDP_STATUS_OK)
  189. return vdpau_error(status);
  190. else
  191. surface_query_caps = func;
  192. status = surface_query_caps(vdctx->device, type, &supported,
  193. &max_width, &max_height);
  194. if (status != VDP_STATUS_OK)
  195. return vdpau_error(status);
  196. if (supported != VDP_TRUE ||
  197. max_width < width || max_height < height)
  198. return AVERROR(ENOTSUP);
  199. status = vdctx->get_proc_address(vdctx->device,
  200. VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
  201. &func);
  202. if (status != VDP_STATUS_OK)
  203. return vdpau_error(status);
  204. else
  205. decoder_query_caps = func;
  206. status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
  207. &max_mb, &max_width, &max_height);
  208. #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
  209. if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
  210. profile = VDP_DECODER_PROFILE_H264_MAIN;
  211. status = decoder_query_caps(vdctx->device, profile, &supported,
  212. &max_level, &max_mb,
  213. &max_width, &max_height);
  214. }
  215. #endif
  216. if (status != VDP_STATUS_OK)
  217. return vdpau_error(status);
  218. if (supported != VDP_TRUE || max_level < level ||
  219. max_width < width || max_height < height)
  220. return AVERROR(ENOTSUP);
  221. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
  222. &func);
  223. if (status != VDP_STATUS_OK)
  224. return vdpau_error(status);
  225. else
  226. create = func;
  227. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
  228. &func);
  229. if (status != VDP_STATUS_OK)
  230. return vdpau_error(status);
  231. else
  232. vdctx->render = func;
  233. status = create(vdctx->device, profile, width, height, avctx->refs,
  234. &vdctx->decoder);
  235. if (status == VDP_STATUS_OK) {
  236. vdctx->width = avctx->coded_width;
  237. vdctx->height = avctx->coded_height;
  238. }
  239. return vdpau_error(status);
  240. }
  241. int ff_vdpau_common_uninit(AVCodecContext *avctx)
  242. {
  243. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  244. VdpDecoderDestroy *destroy;
  245. void *func;
  246. VdpStatus status;
  247. if (vdctx->device == VDP_INVALID_HANDLE)
  248. return 0; /* Decoder created and destroyed by user */
  249. if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
  250. return 0;
  251. status = vdctx->get_proc_address(vdctx->device,
  252. VDP_FUNC_ID_DECODER_DESTROY, &func);
  253. if (status != VDP_STATUS_OK)
  254. return vdpau_error(status);
  255. else
  256. destroy = func;
  257. status = destroy(vdctx->decoder);
  258. return vdpau_error(status);
  259. }
  260. static int ff_vdpau_common_reinit(AVCodecContext *avctx)
  261. {
  262. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  263. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  264. if (vdctx->device == VDP_INVALID_HANDLE)
  265. return 0; /* Decoder created by user */
  266. if (avctx->coded_width == vdctx->width &&
  267. avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
  268. return 0;
  269. avctx->hwaccel->uninit(avctx);
  270. return avctx->hwaccel->init(avctx);
  271. }
  272. int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
  273. av_unused const uint8_t *buffer,
  274. av_unused uint32_t size)
  275. {
  276. pic_ctx->bitstream_buffers_allocated = 0;
  277. pic_ctx->bitstream_buffers_used = 0;
  278. pic_ctx->bitstream_buffers = NULL;
  279. return 0;
  280. }
  281. int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  282. struct vdpau_picture_context *pic_ctx)
  283. {
  284. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  285. AVVDPAUContext *hwctx = avctx->hwaccel_context;
  286. VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
  287. VdpStatus status;
  288. int val;
  289. val = ff_vdpau_common_reinit(avctx);
  290. if (val < 0)
  291. return val;
  292. if (hwctx && !hwctx->render && hwctx->render2) {
  293. status = hwctx->render2(avctx, frame, (void *)&pic_ctx->info,
  294. pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
  295. } else
  296. status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
  297. pic_ctx->bitstream_buffers_used,
  298. pic_ctx->bitstream_buffers);
  299. av_freep(&pic_ctx->bitstream_buffers);
  300. return vdpau_error(status);
  301. }
  302. #if CONFIG_MPEG1_VDPAU_HWACCEL || \
  303. CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
  304. CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
  305. int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
  306. {
  307. MpegEncContext *s = avctx->priv_data;
  308. Picture *pic = s->current_picture_ptr;
  309. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  310. int val;
  311. val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
  312. if (val < 0)
  313. return val;
  314. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  315. return 0;
  316. }
  317. #endif
  318. int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
  319. const uint8_t *buf, uint32_t size)
  320. {
  321. VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
  322. buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
  323. (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
  324. if (!buffers)
  325. return AVERROR(ENOMEM);
  326. pic_ctx->bitstream_buffers = buffers;
  327. buffers += pic_ctx->bitstream_buffers_used++;
  328. buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  329. buffers->bitstream = buf;
  330. buffers->bitstream_bytes = size;
  331. return 0;
  332. }
  333. #if FF_API_VDPAU_PROFILE
  334. int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
  335. {
  336. #define PROFILE(prof) \
  337. do { \
  338. *profile = VDP_DECODER_PROFILE_##prof; \
  339. return 0; \
  340. } while (0)
  341. switch (avctx->codec_id) {
  342. case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
  343. case AV_CODEC_ID_MPEG2VIDEO:
  344. switch (avctx->profile) {
  345. case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
  346. case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
  347. default: return AVERROR(EINVAL);
  348. }
  349. case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
  350. case AV_CODEC_ID_MPEG4:
  351. switch (avctx->profile) {
  352. case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
  353. case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
  354. default: return AVERROR(EINVAL);
  355. }
  356. case AV_CODEC_ID_H264:
  357. switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
  358. case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
  359. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  360. case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
  361. case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
  362. #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
  363. case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
  364. #endif
  365. default: return AVERROR(EINVAL);
  366. }
  367. case AV_CODEC_ID_WMV3:
  368. case AV_CODEC_ID_VC1:
  369. switch (avctx->profile) {
  370. case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
  371. case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
  372. case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
  373. default: return AVERROR(EINVAL);
  374. }
  375. }
  376. return AVERROR(EINVAL);
  377. #undef PROFILE
  378. }
  379. #endif /* FF_API_VDPAU_PROFILE */
  380. AVVDPAUContext *av_vdpau_alloc_context(void)
  381. {
  382. return av_mallocz(sizeof(VDPAUHWContext));
  383. }
  384. int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
  385. VdpGetProcAddress *get_proc, unsigned flags)
  386. {
  387. VDPAUHWContext *hwctx;
  388. if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
  389. return AVERROR(EINVAL);
  390. if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
  391. return AVERROR(ENOMEM);
  392. hwctx = avctx->hwaccel_context;
  393. memset(hwctx, 0, sizeof(*hwctx));
  394. hwctx->context.decoder = VDP_INVALID_HANDLE;
  395. hwctx->device = device;
  396. hwctx->get_proc_address = get_proc;
  397. hwctx->flags = flags;
  398. hwctx->reset = 1;
  399. return 0;
  400. }
  401. /* @}*/