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.

395 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 "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 || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
  158. profile = VDP_DECODER_PROFILE_H264_MAIN;
  159. status = decoder_query_caps(vdctx->device, profile, &supported,
  160. &max_level, &max_mb,
  161. &max_width, &max_height);
  162. }
  163. #endif
  164. if (status != VDP_STATUS_OK)
  165. return vdpau_error(status);
  166. if (supported != VDP_TRUE || max_level < level ||
  167. max_width < width || max_height < height)
  168. return AVERROR(ENOTSUP);
  169. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
  170. &func);
  171. if (status != VDP_STATUS_OK)
  172. return vdpau_error(status);
  173. else
  174. create = func;
  175. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
  176. &func);
  177. if (status != VDP_STATUS_OK)
  178. return vdpau_error(status);
  179. else
  180. vdctx->render = func;
  181. status = create(vdctx->device, profile, width, height, avctx->refs,
  182. &vdctx->decoder);
  183. if (status == VDP_STATUS_OK) {
  184. vdctx->width = avctx->coded_width;
  185. vdctx->height = avctx->coded_height;
  186. }
  187. return vdpau_error(status);
  188. }
  189. int ff_vdpau_common_uninit(AVCodecContext *avctx)
  190. {
  191. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  192. VdpDecoderDestroy *destroy;
  193. void *func;
  194. VdpStatus status;
  195. if (vdctx->device == VDP_INVALID_HANDLE)
  196. return 0; /* Decoder created and destroyed by user */
  197. if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
  198. return 0;
  199. status = vdctx->get_proc_address(vdctx->device,
  200. VDP_FUNC_ID_DECODER_DESTROY, &func);
  201. if (status != VDP_STATUS_OK)
  202. return vdpau_error(status);
  203. else
  204. destroy = func;
  205. status = destroy(vdctx->decoder);
  206. return vdpau_error(status);
  207. }
  208. static int ff_vdpau_common_reinit(AVCodecContext *avctx)
  209. {
  210. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  211. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  212. if (vdctx->device == VDP_INVALID_HANDLE)
  213. return 0; /* Decoder created by user */
  214. if (avctx->coded_width == vdctx->width &&
  215. avctx->coded_height == vdctx->height && !hwctx->reset)
  216. return 0;
  217. avctx->hwaccel->uninit(avctx);
  218. return avctx->hwaccel->init(avctx);
  219. }
  220. int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
  221. av_unused const uint8_t *buffer,
  222. av_unused uint32_t size)
  223. {
  224. pic_ctx->bitstream_buffers_allocated = 0;
  225. pic_ctx->bitstream_buffers_used = 0;
  226. pic_ctx->bitstream_buffers = NULL;
  227. return 0;
  228. }
  229. int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  230. struct vdpau_picture_context *pic_ctx)
  231. {
  232. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  233. VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
  234. VdpStatus status;
  235. int val;
  236. val = ff_vdpau_common_reinit(avctx);
  237. if (val < 0)
  238. return val;
  239. status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
  240. pic_ctx->bitstream_buffers_used,
  241. pic_ctx->bitstream_buffers);
  242. av_freep(&pic_ctx->bitstream_buffers);
  243. return vdpau_error(status);
  244. }
  245. #if CONFIG_MPEG1_VDPAU_HWACCEL || \
  246. CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
  247. CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
  248. int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
  249. {
  250. MpegEncContext *s = avctx->priv_data;
  251. Picture *pic = s->current_picture_ptr;
  252. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  253. int val;
  254. val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
  255. if (val < 0)
  256. return val;
  257. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  258. return 0;
  259. }
  260. #endif
  261. int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
  262. const uint8_t *buf, uint32_t size)
  263. {
  264. VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
  265. buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
  266. (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
  267. if (!buffers)
  268. return AVERROR(ENOMEM);
  269. pic_ctx->bitstream_buffers = buffers;
  270. buffers += pic_ctx->bitstream_buffers_used++;
  271. buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  272. buffers->bitstream = buf;
  273. buffers->bitstream_bytes = size;
  274. return 0;
  275. }
  276. int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
  277. {
  278. #define PROFILE(prof) \
  279. do { \
  280. *profile = VDP_DECODER_PROFILE_##prof; \
  281. return 0; \
  282. } while (0)
  283. switch (avctx->codec_id) {
  284. case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
  285. case AV_CODEC_ID_MPEG2VIDEO:
  286. switch (avctx->profile) {
  287. case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
  288. case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
  289. default: return AVERROR(EINVAL);
  290. }
  291. case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
  292. case AV_CODEC_ID_MPEG4:
  293. switch (avctx->profile) {
  294. case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
  295. case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
  296. default: return AVERROR(EINVAL);
  297. }
  298. case AV_CODEC_ID_H264:
  299. switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
  300. case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
  301. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  302. case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
  303. case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
  304. #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
  305. case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
  306. #endif
  307. default: return AVERROR(EINVAL);
  308. }
  309. case AV_CODEC_ID_WMV3:
  310. case AV_CODEC_ID_VC1:
  311. switch (avctx->profile) {
  312. case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
  313. case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
  314. case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
  315. default: return AVERROR(EINVAL);
  316. }
  317. }
  318. return AVERROR(EINVAL);
  319. #undef PROFILE
  320. }
  321. AVVDPAUContext *av_vdpau_alloc_context(void)
  322. {
  323. return av_mallocz(sizeof(AVVDPAUContext));
  324. }
  325. int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
  326. VdpGetProcAddress *get_proc, unsigned flags)
  327. {
  328. VDPAUHWContext *hwctx;
  329. if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
  330. return AVERROR(EINVAL);
  331. if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
  332. return AVERROR(ENOMEM);
  333. hwctx = avctx->hwaccel_context;
  334. memset(hwctx, 0, sizeof(*hwctx));
  335. hwctx->context.decoder = VDP_INVALID_HANDLE;
  336. hwctx->device = device;
  337. hwctx->get_proc_address = get_proc;
  338. hwctx->flags = flags;
  339. hwctx->reset = 1;
  340. return 0;
  341. }
  342. /* @}*/