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.

440 lines
14KB

  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. if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
  113. return AVERROR(ENOSYS);
  114. if (hwctx) {
  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. if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
  127. type != VDP_CHROMA_TYPE_420)
  128. return AVERROR(ENOSYS);
  129. } else {
  130. AVHWFramesContext *frames_ctx = NULL;
  131. AVVDPAUDeviceContext *dev_ctx;
  132. // We assume the hw_frames_ctx always survives until ff_vdpau_common_uninit
  133. // is called. This holds true as the user is not allowed to touch
  134. // hw_device_ctx, or hw_frames_ctx after get_format (and ff_get_format
  135. // itself also uninits before unreffing hw_frames_ctx).
  136. if (avctx->hw_frames_ctx) {
  137. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  138. } else if (avctx->hw_device_ctx) {
  139. int ret;
  140. avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
  141. if (!avctx->hw_frames_ctx)
  142. return AVERROR(ENOMEM);
  143. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  144. frames_ctx->format = AV_PIX_FMT_VDPAU;
  145. frames_ctx->sw_format = avctx->sw_pix_fmt;
  146. frames_ctx->width = avctx->coded_width;
  147. frames_ctx->height = avctx->coded_height;
  148. ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
  149. if (ret < 0) {
  150. av_buffer_unref(&avctx->hw_frames_ctx);
  151. return ret;
  152. }
  153. }
  154. if (!frames_ctx) {
  155. av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
  156. "required for VDPAU decoding.\n");
  157. return AVERROR(EINVAL);
  158. }
  159. dev_ctx = frames_ctx->device_ctx->hwctx;
  160. vdctx->device = dev_ctx->device;
  161. vdctx->get_proc_address = dev_ctx->get_proc_address;
  162. if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
  163. level = 0;
  164. }
  165. if (level < 0)
  166. return AVERROR(ENOTSUP);
  167. status = vdctx->get_proc_address(vdctx->device,
  168. VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
  169. &func);
  170. if (status != VDP_STATUS_OK)
  171. return vdpau_error(status);
  172. else
  173. surface_query_caps = func;
  174. status = surface_query_caps(vdctx->device, type, &supported,
  175. &max_width, &max_height);
  176. if (status != VDP_STATUS_OK)
  177. return vdpau_error(status);
  178. if (supported != VDP_TRUE ||
  179. max_width < width || max_height < height)
  180. return AVERROR(ENOTSUP);
  181. status = vdctx->get_proc_address(vdctx->device,
  182. VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
  183. &func);
  184. if (status != VDP_STATUS_OK)
  185. return vdpau_error(status);
  186. else
  187. decoder_query_caps = func;
  188. status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
  189. &max_mb, &max_width, &max_height);
  190. #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
  191. if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
  192. profile = VDP_DECODER_PROFILE_H264_MAIN;
  193. status = decoder_query_caps(vdctx->device, profile, &supported,
  194. &max_level, &max_mb,
  195. &max_width, &max_height);
  196. }
  197. #endif
  198. if (status != VDP_STATUS_OK)
  199. return vdpau_error(status);
  200. if (supported != VDP_TRUE || max_level < level ||
  201. max_width < width || max_height < height)
  202. return AVERROR(ENOTSUP);
  203. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
  204. &func);
  205. if (status != VDP_STATUS_OK)
  206. return vdpau_error(status);
  207. else
  208. create = func;
  209. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
  210. &func);
  211. if (status != VDP_STATUS_OK)
  212. return vdpau_error(status);
  213. else
  214. vdctx->render = func;
  215. status = create(vdctx->device, profile, width, height, avctx->refs,
  216. &vdctx->decoder);
  217. if (status == VDP_STATUS_OK) {
  218. vdctx->width = avctx->coded_width;
  219. vdctx->height = avctx->coded_height;
  220. }
  221. return vdpau_error(status);
  222. }
  223. int ff_vdpau_common_uninit(AVCodecContext *avctx)
  224. {
  225. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  226. VdpDecoderDestroy *destroy;
  227. void *func;
  228. VdpStatus status;
  229. if (vdctx->device == VDP_INVALID_HANDLE)
  230. return 0; /* Decoder created and destroyed by user */
  231. if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
  232. return 0;
  233. status = vdctx->get_proc_address(vdctx->device,
  234. VDP_FUNC_ID_DECODER_DESTROY, &func);
  235. if (status != VDP_STATUS_OK)
  236. return vdpau_error(status);
  237. else
  238. destroy = func;
  239. status = destroy(vdctx->decoder);
  240. return vdpau_error(status);
  241. }
  242. static int ff_vdpau_common_reinit(AVCodecContext *avctx)
  243. {
  244. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  245. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  246. if (vdctx->device == VDP_INVALID_HANDLE)
  247. return 0; /* Decoder created by user */
  248. if (avctx->coded_width == vdctx->width &&
  249. avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
  250. return 0;
  251. avctx->hwaccel->uninit(avctx);
  252. return avctx->hwaccel->init(avctx);
  253. }
  254. int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
  255. av_unused const uint8_t *buffer,
  256. av_unused uint32_t size)
  257. {
  258. pic_ctx->bitstream_buffers_allocated = 0;
  259. pic_ctx->bitstream_buffers_used = 0;
  260. pic_ctx->bitstream_buffers = NULL;
  261. return 0;
  262. }
  263. int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  264. struct vdpau_picture_context *pic_ctx)
  265. {
  266. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  267. VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
  268. VdpStatus status;
  269. int val;
  270. val = ff_vdpau_common_reinit(avctx);
  271. if (val < 0)
  272. return val;
  273. status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
  274. pic_ctx->bitstream_buffers_used,
  275. pic_ctx->bitstream_buffers);
  276. av_freep(&pic_ctx->bitstream_buffers);
  277. return vdpau_error(status);
  278. }
  279. #if CONFIG_MPEG1_VDPAU_HWACCEL || \
  280. CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
  281. CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
  282. int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
  283. {
  284. MpegEncContext *s = avctx->priv_data;
  285. Picture *pic = s->current_picture_ptr;
  286. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  287. int val;
  288. val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
  289. if (val < 0)
  290. return val;
  291. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  292. return 0;
  293. }
  294. #endif
  295. int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
  296. const uint8_t *buf, uint32_t size)
  297. {
  298. VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
  299. buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
  300. (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
  301. if (!buffers)
  302. return AVERROR(ENOMEM);
  303. pic_ctx->bitstream_buffers = buffers;
  304. buffers += pic_ctx->bitstream_buffers_used++;
  305. buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  306. buffers->bitstream = buf;
  307. buffers->bitstream_bytes = size;
  308. return 0;
  309. }
  310. #if FF_API_VDPAU_PROFILE
  311. int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
  312. {
  313. #define PROFILE(prof) \
  314. do { \
  315. *profile = VDP_DECODER_PROFILE_##prof; \
  316. return 0; \
  317. } while (0)
  318. switch (avctx->codec_id) {
  319. case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
  320. case AV_CODEC_ID_MPEG2VIDEO:
  321. switch (avctx->profile) {
  322. case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
  323. case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
  324. default: return AVERROR(EINVAL);
  325. }
  326. case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
  327. case AV_CODEC_ID_MPEG4:
  328. switch (avctx->profile) {
  329. case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
  330. case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
  331. default: return AVERROR(EINVAL);
  332. }
  333. case AV_CODEC_ID_H264:
  334. switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
  335. case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
  336. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  337. case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
  338. case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
  339. #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
  340. case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
  341. #endif
  342. default: return AVERROR(EINVAL);
  343. }
  344. case AV_CODEC_ID_WMV3:
  345. case AV_CODEC_ID_VC1:
  346. switch (avctx->profile) {
  347. case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
  348. case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
  349. case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
  350. default: return AVERROR(EINVAL);
  351. }
  352. }
  353. return AVERROR(EINVAL);
  354. #undef PROFILE
  355. }
  356. #endif /* FF_API_VDPAU_PROFILE */
  357. AVVDPAUContext *av_vdpau_alloc_context(void)
  358. {
  359. return av_mallocz(sizeof(AVVDPAUContext));
  360. }
  361. int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
  362. VdpGetProcAddress *get_proc, unsigned flags)
  363. {
  364. VDPAUHWContext *hwctx;
  365. if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
  366. return AVERROR(EINVAL);
  367. if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
  368. return AVERROR(ENOMEM);
  369. hwctx = avctx->hwaccel_context;
  370. memset(hwctx, 0, sizeof(*hwctx));
  371. hwctx->context.decoder = VDP_INVALID_HANDLE;
  372. hwctx->device = device;
  373. hwctx->get_proc_address = get_proc;
  374. hwctx->flags = flags;
  375. hwctx->reset = 1;
  376. return 0;
  377. }
  378. /* @}*/