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.

393 lines
12KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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 "internal.h"
  26. #include "h264dec.h"
  27. #include "vc1.h"
  28. #include "vdpau.h"
  29. #include "vdpau_internal.h"
  30. /**
  31. * @addtogroup VDPAU_Decoding
  32. *
  33. * @{
  34. */
  35. static int vdpau_error(VdpStatus status)
  36. {
  37. switch (status) {
  38. case VDP_STATUS_OK:
  39. return 0;
  40. case VDP_STATUS_NO_IMPLEMENTATION:
  41. return AVERROR(ENOSYS);
  42. case VDP_STATUS_DISPLAY_PREEMPTED:
  43. return AVERROR(EIO);
  44. case VDP_STATUS_INVALID_HANDLE:
  45. return AVERROR(EBADF);
  46. case VDP_STATUS_INVALID_POINTER:
  47. return AVERROR(EFAULT);
  48. case VDP_STATUS_RESOURCES:
  49. return AVERROR(ENOBUFS);
  50. case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
  51. return AVERROR(EXDEV);
  52. case VDP_STATUS_ERROR:
  53. return AVERROR(EIO);
  54. default:
  55. return AVERROR(EINVAL);
  56. }
  57. }
  58. int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
  59. VdpChromaType *type,
  60. uint32_t *width, uint32_t *height)
  61. {
  62. VdpChromaType t;
  63. uint32_t w = avctx->coded_width;
  64. uint32_t h = avctx->coded_height;
  65. /* See <vdpau/vdpau.h> for per-type alignment constraints. */
  66. switch (avctx->sw_pix_fmt) {
  67. case AV_PIX_FMT_YUV420P:
  68. case AV_PIX_FMT_YUVJ420P:
  69. t = VDP_CHROMA_TYPE_420;
  70. w = (w + 1) & ~1;
  71. h = (h + 3) & ~3;
  72. break;
  73. case AV_PIX_FMT_YUV422P:
  74. case AV_PIX_FMT_YUVJ422P:
  75. t = VDP_CHROMA_TYPE_422;
  76. w = (w + 1) & ~1;
  77. h = (h + 1) & ~1;
  78. break;
  79. case AV_PIX_FMT_YUV444P:
  80. case AV_PIX_FMT_YUVJ444P:
  81. t = VDP_CHROMA_TYPE_444;
  82. h = (h + 1) & ~1;
  83. break;
  84. default:
  85. return AVERROR(ENOSYS);
  86. }
  87. if (type)
  88. *type = t;
  89. if (width)
  90. *width = w;
  91. if (height)
  92. *height = h;
  93. return 0;
  94. }
  95. int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
  96. int level)
  97. {
  98. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  99. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  100. VdpVideoSurfaceQueryCapabilities *surface_query_caps;
  101. VdpDecoderQueryCapabilities *decoder_query_caps;
  102. VdpDecoderCreate *create;
  103. void *func;
  104. VdpStatus status;
  105. VdpBool supported;
  106. uint32_t max_level, max_mb, max_width, max_height;
  107. VdpChromaType type;
  108. uint32_t width;
  109. uint32_t height;
  110. vdctx->width = UINT32_MAX;
  111. vdctx->height = UINT32_MAX;
  112. hwctx->reset = 0;
  113. if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
  114. vdctx->decoder = hwctx->context.decoder;
  115. vdctx->render = hwctx->context.render;
  116. vdctx->device = VDP_INVALID_HANDLE;
  117. return 0; /* Decoder created by user */
  118. }
  119. vdctx->device = hwctx->device;
  120. vdctx->get_proc_address = hwctx->get_proc_address;
  121. if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
  122. level = 0;
  123. else if (level < 0)
  124. return AVERROR(ENOTSUP);
  125. if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
  126. return AVERROR(ENOSYS);
  127. if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
  128. type != VDP_CHROMA_TYPE_420)
  129. return AVERROR(ENOSYS);
  130. status = vdctx->get_proc_address(vdctx->device,
  131. VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
  132. &func);
  133. if (status != VDP_STATUS_OK)
  134. return vdpau_error(status);
  135. else
  136. surface_query_caps = func;
  137. status = surface_query_caps(vdctx->device, type, &supported,
  138. &max_width, &max_height);
  139. if (status != VDP_STATUS_OK)
  140. return vdpau_error(status);
  141. if (supported != VDP_TRUE ||
  142. max_width < width || max_height < height)
  143. return AVERROR(ENOTSUP);
  144. status = vdctx->get_proc_address(vdctx->device,
  145. VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
  146. &func);
  147. if (status != VDP_STATUS_OK)
  148. return vdpau_error(status);
  149. else
  150. decoder_query_caps = func;
  151. status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
  152. &max_mb, &max_width, &max_height);
  153. #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
  154. if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
  155. profile = VDP_DECODER_PROFILE_H264_MAIN;
  156. status = decoder_query_caps(vdctx->device, profile, &supported,
  157. &max_level, &max_mb,
  158. &max_width, &max_height);
  159. }
  160. #endif
  161. if (status != VDP_STATUS_OK)
  162. return vdpau_error(status);
  163. if (supported != VDP_TRUE || max_level < level ||
  164. max_width < width || max_height < height)
  165. return AVERROR(ENOTSUP);
  166. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
  167. &func);
  168. if (status != VDP_STATUS_OK)
  169. return vdpau_error(status);
  170. else
  171. create = func;
  172. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
  173. &func);
  174. if (status != VDP_STATUS_OK)
  175. return vdpau_error(status);
  176. else
  177. vdctx->render = func;
  178. status = create(vdctx->device, profile, width, height, avctx->refs,
  179. &vdctx->decoder);
  180. if (status == VDP_STATUS_OK) {
  181. vdctx->width = avctx->coded_width;
  182. vdctx->height = avctx->coded_height;
  183. }
  184. return vdpau_error(status);
  185. }
  186. int ff_vdpau_common_uninit(AVCodecContext *avctx)
  187. {
  188. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  189. VdpDecoderDestroy *destroy;
  190. void *func;
  191. VdpStatus status;
  192. if (vdctx->device == VDP_INVALID_HANDLE)
  193. return 0; /* Decoder created and destroyed by user */
  194. if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
  195. return 0;
  196. status = vdctx->get_proc_address(vdctx->device,
  197. VDP_FUNC_ID_DECODER_DESTROY, &func);
  198. if (status != VDP_STATUS_OK)
  199. return vdpau_error(status);
  200. else
  201. destroy = func;
  202. status = destroy(vdctx->decoder);
  203. return vdpau_error(status);
  204. }
  205. static int ff_vdpau_common_reinit(AVCodecContext *avctx)
  206. {
  207. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  208. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  209. if (vdctx->device == VDP_INVALID_HANDLE)
  210. return 0; /* Decoder created by user */
  211. if (avctx->coded_width == vdctx->width &&
  212. avctx->coded_height == vdctx->height && !hwctx->reset)
  213. return 0;
  214. avctx->hwaccel->uninit(avctx);
  215. return avctx->hwaccel->init(avctx);
  216. }
  217. int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
  218. av_unused const uint8_t *buffer,
  219. av_unused uint32_t size)
  220. {
  221. pic_ctx->bitstream_buffers_allocated = 0;
  222. pic_ctx->bitstream_buffers_used = 0;
  223. pic_ctx->bitstream_buffers = NULL;
  224. return 0;
  225. }
  226. int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  227. struct vdpau_picture_context *pic_ctx)
  228. {
  229. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  230. VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
  231. VdpStatus status;
  232. int val;
  233. val = ff_vdpau_common_reinit(avctx);
  234. if (val < 0)
  235. return val;
  236. status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
  237. pic_ctx->bitstream_buffers_used,
  238. pic_ctx->bitstream_buffers);
  239. av_freep(&pic_ctx->bitstream_buffers);
  240. return vdpau_error(status);
  241. }
  242. #if CONFIG_MPEG1_VDPAU_HWACCEL || \
  243. CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
  244. CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
  245. int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
  246. {
  247. MpegEncContext *s = avctx->priv_data;
  248. Picture *pic = s->current_picture_ptr;
  249. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  250. int val;
  251. val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
  252. if (val < 0)
  253. return val;
  254. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  255. return 0;
  256. }
  257. #endif
  258. int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
  259. const uint8_t *buf, uint32_t size)
  260. {
  261. VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
  262. buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
  263. (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
  264. if (!buffers)
  265. return AVERROR(ENOMEM);
  266. pic_ctx->bitstream_buffers = buffers;
  267. buffers += pic_ctx->bitstream_buffers_used++;
  268. buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  269. buffers->bitstream = buf;
  270. buffers->bitstream_bytes = size;
  271. return 0;
  272. }
  273. #if FF_API_VDPAU_PROFILE
  274. int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
  275. {
  276. #define PROFILE(prof) \
  277. do { \
  278. *profile = VDP_DECODER_PROFILE_##prof; \
  279. return 0; \
  280. } while (0)
  281. switch (avctx->codec_id) {
  282. case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
  283. case AV_CODEC_ID_MPEG2VIDEO:
  284. switch (avctx->profile) {
  285. case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
  286. case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
  287. default: return AVERROR(EINVAL);
  288. }
  289. case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
  290. case AV_CODEC_ID_MPEG4:
  291. switch (avctx->profile) {
  292. case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
  293. case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
  294. default: return AVERROR(EINVAL);
  295. }
  296. case AV_CODEC_ID_H264:
  297. switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
  298. case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
  299. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  300. case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
  301. case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
  302. #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
  303. case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
  304. #endif
  305. default: return AVERROR(EINVAL);
  306. }
  307. case AV_CODEC_ID_WMV3:
  308. case AV_CODEC_ID_VC1:
  309. switch (avctx->profile) {
  310. case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
  311. case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
  312. case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
  313. default: return AVERROR(EINVAL);
  314. }
  315. }
  316. return AVERROR(EINVAL);
  317. #undef PROFILE
  318. }
  319. #endif /* FF_API_VDPAU_PROFILE */
  320. AVVDPAUContext *av_vdpau_alloc_context(void)
  321. {
  322. return av_mallocz(sizeof(AVVDPAUContext));
  323. }
  324. int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
  325. VdpGetProcAddress *get_proc, unsigned flags)
  326. {
  327. VDPAUHWContext *hwctx;
  328. if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
  329. return AVERROR(EINVAL);
  330. if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
  331. return AVERROR(ENOMEM);
  332. hwctx = avctx->hwaccel_context;
  333. memset(hwctx, 0, sizeof(*hwctx));
  334. hwctx->context.decoder = VDP_INVALID_HANDLE;
  335. hwctx->device = device;
  336. hwctx->get_proc_address = get_proc;
  337. hwctx->flags = flags;
  338. hwctx->reset = 1;
  339. return 0;
  340. }
  341. /* @}*/