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.

396 lines
13KB

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