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.

392 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. 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 && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
  155. /* Run-time backward compatibility for libvdpau 0.8 and earlier */
  156. profile = VDP_DECODER_PROFILE_H264_MAIN;
  157. status = decoder_query_caps(vdctx->device, profile, &supported,
  158. &max_level, &max_mb,
  159. &max_width, &max_height);
  160. }
  161. #endif
  162. if (status != VDP_STATUS_OK)
  163. return vdpau_error(status);
  164. if (supported != VDP_TRUE || max_level < level ||
  165. max_width < width || max_height < height)
  166. return AVERROR(ENOTSUP);
  167. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
  168. &func);
  169. if (status != VDP_STATUS_OK)
  170. return vdpau_error(status);
  171. else
  172. create = func;
  173. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
  174. &func);
  175. if (status != VDP_STATUS_OK)
  176. return vdpau_error(status);
  177. else
  178. vdctx->render = func;
  179. status = create(vdctx->device, profile, width, height, avctx->refs,
  180. &vdctx->decoder);
  181. if (status == VDP_STATUS_OK) {
  182. vdctx->width = avctx->coded_width;
  183. vdctx->height = avctx->coded_height;
  184. }
  185. return vdpau_error(status);
  186. }
  187. int ff_vdpau_common_uninit(AVCodecContext *avctx)
  188. {
  189. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  190. VdpDecoderDestroy *destroy;
  191. void *func;
  192. VdpStatus status;
  193. if (vdctx->device == VDP_INVALID_HANDLE)
  194. return 0; /* Decoder created and destroyed by user */
  195. if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
  196. return 0;
  197. status = vdctx->get_proc_address(vdctx->device,
  198. VDP_FUNC_ID_DECODER_DESTROY, &func);
  199. if (status != VDP_STATUS_OK)
  200. return vdpau_error(status);
  201. else
  202. destroy = func;
  203. status = destroy(vdctx->decoder);
  204. return vdpau_error(status);
  205. }
  206. static int ff_vdpau_common_reinit(AVCodecContext *avctx)
  207. {
  208. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  209. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  210. if (vdctx->device == VDP_INVALID_HANDLE)
  211. return 0; /* Decoder created by user */
  212. if (avctx->coded_width == vdctx->width &&
  213. avctx->coded_height == vdctx->height && !hwctx->reset)
  214. return 0;
  215. avctx->hwaccel->uninit(avctx);
  216. return avctx->hwaccel->init(avctx);
  217. }
  218. int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
  219. av_unused const uint8_t *buffer,
  220. av_unused uint32_t size)
  221. {
  222. pic_ctx->bitstream_buffers_allocated = 0;
  223. pic_ctx->bitstream_buffers_used = 0;
  224. pic_ctx->bitstream_buffers = NULL;
  225. return 0;
  226. }
  227. int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  228. struct vdpau_picture_context *pic_ctx)
  229. {
  230. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  231. VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
  232. VdpStatus status;
  233. int val;
  234. val = ff_vdpau_common_reinit(avctx);
  235. if (val < 0)
  236. return val;
  237. status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
  238. pic_ctx->bitstream_buffers_used,
  239. pic_ctx->bitstream_buffers);
  240. av_freep(&pic_ctx->bitstream_buffers);
  241. return vdpau_error(status);
  242. }
  243. #if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG1_VDPAU_HWACCEL || \
  244. CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
  245. CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
  246. int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
  247. {
  248. MpegEncContext *s = avctx->priv_data;
  249. Picture *pic = s->current_picture_ptr;
  250. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  251. int val;
  252. val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
  253. if (val < 0)
  254. return val;
  255. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  256. return 0;
  257. }
  258. #endif
  259. int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
  260. const uint8_t *buf, uint32_t size)
  261. {
  262. VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
  263. buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
  264. (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
  265. if (!buffers)
  266. return AVERROR(ENOMEM);
  267. pic_ctx->bitstream_buffers = buffers;
  268. buffers += pic_ctx->bitstream_buffers_used++;
  269. buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  270. buffers->bitstream = buf;
  271. buffers->bitstream_bytes = size;
  272. return 0;
  273. }
  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. AVVDPAUContext *av_vdpau_alloc_context(void)
  320. {
  321. return av_mallocz(sizeof(AVVDPAUContext));
  322. }
  323. int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
  324. VdpGetProcAddress *get_proc, unsigned flags)
  325. {
  326. VDPAUHWContext *hwctx;
  327. if (flags & ~AV_HWACCEL_FLAG_IGNORE_LEVEL)
  328. return AVERROR(EINVAL);
  329. if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
  330. return AVERROR(ENOMEM);
  331. hwctx = avctx->hwaccel_context;
  332. memset(hwctx, 0, sizeof(*hwctx));
  333. hwctx->context.decoder = VDP_INVALID_HANDLE;
  334. hwctx->device = device;
  335. hwctx->get_proc_address = get_proc;
  336. hwctx->flags = flags;
  337. hwctx->reset = 1;
  338. return 0;
  339. }
  340. /* @}*/