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.

336 lines
11KB

  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 ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
  62. int level)
  63. {
  64. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  65. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  66. VdpVideoSurfaceQueryCapabilities *surface_query_caps;
  67. VdpDecoderQueryCapabilities *decoder_query_caps;
  68. VdpDecoderCreate *create;
  69. void *func;
  70. VdpStatus status;
  71. VdpBool supported;
  72. uint32_t max_level, max_mb, max_width, max_height;
  73. /* See vdpau/vdpau.h for alignment constraints. */
  74. uint32_t width = (avctx->coded_width + 1) & ~1;
  75. uint32_t height = (avctx->coded_height + 3) & ~3;
  76. vdctx->width = UINT32_MAX;
  77. vdctx->height = UINT32_MAX;
  78. hwctx->reset = 0;
  79. if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
  80. vdctx->decoder = hwctx->context.decoder;
  81. vdctx->render = hwctx->context.render;
  82. vdctx->device = VDP_INVALID_HANDLE;
  83. return 0; /* Decoder created by user */
  84. }
  85. vdctx->device = hwctx->device;
  86. vdctx->get_proc_address = hwctx->get_proc_address;
  87. if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
  88. level = 0;
  89. else if (level < 0)
  90. return AVERROR(ENOTSUP);
  91. status = vdctx->get_proc_address(vdctx->device,
  92. VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
  93. &func);
  94. if (status != VDP_STATUS_OK)
  95. return vdpau_error(status);
  96. else
  97. surface_query_caps = func;
  98. status = surface_query_caps(vdctx->device, VDP_CHROMA_TYPE_420, &supported,
  99. &max_width, &max_height);
  100. if (status != VDP_STATUS_OK)
  101. return vdpau_error(status);
  102. if (supported != VDP_TRUE ||
  103. max_width < width || max_height < height)
  104. return AVERROR(ENOTSUP);
  105. status = vdctx->get_proc_address(vdctx->device,
  106. VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
  107. &func);
  108. if (status != VDP_STATUS_OK)
  109. return vdpau_error(status);
  110. else
  111. decoder_query_caps = func;
  112. status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
  113. &max_mb, &max_width, &max_height);
  114. if (status != VDP_STATUS_OK)
  115. return vdpau_error(status);
  116. if (supported != VDP_TRUE || max_level < level ||
  117. max_width < width || max_height < height)
  118. return AVERROR(ENOTSUP);
  119. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
  120. &func);
  121. if (status != VDP_STATUS_OK)
  122. return vdpau_error(status);
  123. else
  124. create = func;
  125. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
  126. &func);
  127. if (status != VDP_STATUS_OK)
  128. return vdpau_error(status);
  129. else
  130. vdctx->render = func;
  131. status = create(vdctx->device, profile, width, height, avctx->refs,
  132. &vdctx->decoder);
  133. if (status == VDP_STATUS_OK) {
  134. vdctx->width = avctx->coded_width;
  135. vdctx->height = avctx->coded_height;
  136. }
  137. return vdpau_error(status);
  138. }
  139. int ff_vdpau_common_uninit(AVCodecContext *avctx)
  140. {
  141. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  142. VdpDecoderDestroy *destroy;
  143. void *func;
  144. VdpStatus status;
  145. if (vdctx->device == VDP_INVALID_HANDLE)
  146. return 0; /* Decoder created and destroyed by user */
  147. if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
  148. return 0;
  149. status = vdctx->get_proc_address(vdctx->device,
  150. VDP_FUNC_ID_DECODER_DESTROY, &func);
  151. if (status != VDP_STATUS_OK)
  152. return vdpau_error(status);
  153. else
  154. destroy = func;
  155. status = destroy(vdctx->decoder);
  156. return vdpau_error(status);
  157. }
  158. static int ff_vdpau_common_reinit(AVCodecContext *avctx)
  159. {
  160. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  161. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  162. if (vdctx->device == VDP_INVALID_HANDLE)
  163. return 0; /* Decoder created by user */
  164. if (avctx->coded_width == vdctx->width &&
  165. avctx->coded_height == vdctx->height && !hwctx->reset)
  166. return 0;
  167. avctx->hwaccel->uninit(avctx);
  168. return avctx->hwaccel->init(avctx);
  169. }
  170. int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
  171. av_unused const uint8_t *buffer,
  172. av_unused uint32_t size)
  173. {
  174. pic_ctx->bitstream_buffers_allocated = 0;
  175. pic_ctx->bitstream_buffers_used = 0;
  176. pic_ctx->bitstream_buffers = NULL;
  177. return 0;
  178. }
  179. int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  180. struct vdpau_picture_context *pic_ctx)
  181. {
  182. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  183. VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
  184. VdpStatus status;
  185. int val;
  186. val = ff_vdpau_common_reinit(avctx);
  187. if (val < 0)
  188. return val;
  189. status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
  190. pic_ctx->bitstream_buffers_used,
  191. pic_ctx->bitstream_buffers);
  192. av_freep(&pic_ctx->bitstream_buffers);
  193. return vdpau_error(status);
  194. }
  195. #if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG1_VDPAU_HWACCEL || \
  196. CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
  197. CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
  198. int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
  199. {
  200. MpegEncContext *s = avctx->priv_data;
  201. Picture *pic = s->current_picture_ptr;
  202. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  203. int val;
  204. val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
  205. if (val < 0)
  206. return val;
  207. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  208. return 0;
  209. }
  210. #endif
  211. int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
  212. const uint8_t *buf, uint32_t size)
  213. {
  214. VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
  215. buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
  216. (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
  217. if (!buffers)
  218. return AVERROR(ENOMEM);
  219. pic_ctx->bitstream_buffers = buffers;
  220. buffers += pic_ctx->bitstream_buffers_used++;
  221. buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  222. buffers->bitstream = buf;
  223. buffers->bitstream_bytes = size;
  224. return 0;
  225. }
  226. int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
  227. {
  228. #define PROFILE(prof) \
  229. do { \
  230. *profile = prof; \
  231. return 0; \
  232. } while (0)
  233. switch (avctx->codec_id) {
  234. case AV_CODEC_ID_MPEG1VIDEO: PROFILE(VDP_DECODER_PROFILE_MPEG1);
  235. case AV_CODEC_ID_MPEG2VIDEO:
  236. switch (avctx->profile) {
  237. case FF_PROFILE_MPEG2_MAIN: PROFILE(VDP_DECODER_PROFILE_MPEG2_MAIN);
  238. case FF_PROFILE_MPEG2_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG2_SIMPLE);
  239. default: return AVERROR(EINVAL);
  240. }
  241. case AV_CODEC_ID_H263: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
  242. case AV_CODEC_ID_MPEG4:
  243. switch (avctx->profile) {
  244. case FF_PROFILE_MPEG4_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_SP);
  245. case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
  246. default: return AVERROR(EINVAL);
  247. }
  248. case AV_CODEC_ID_H264:
  249. switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
  250. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  251. case FF_PROFILE_H264_BASELINE: PROFILE(VDP_DECODER_PROFILE_H264_BASELINE);
  252. case FF_PROFILE_H264_MAIN: PROFILE(VDP_DECODER_PROFILE_H264_MAIN);
  253. case FF_PROFILE_H264_HIGH: PROFILE(VDP_DECODER_PROFILE_H264_HIGH);
  254. default: return AVERROR(EINVAL);
  255. }
  256. case AV_CODEC_ID_WMV3:
  257. case AV_CODEC_ID_VC1:
  258. switch (avctx->profile) {
  259. case FF_PROFILE_VC1_SIMPLE: PROFILE(VDP_DECODER_PROFILE_VC1_SIMPLE);
  260. case FF_PROFILE_VC1_MAIN: PROFILE(VDP_DECODER_PROFILE_VC1_MAIN);
  261. case FF_PROFILE_VC1_ADVANCED: PROFILE(VDP_DECODER_PROFILE_VC1_ADVANCED);
  262. default: return AVERROR(EINVAL);
  263. }
  264. }
  265. return AVERROR(EINVAL);
  266. }
  267. AVVDPAUContext *av_vdpau_alloc_context(void)
  268. {
  269. return av_mallocz(sizeof(AVVDPAUContext));
  270. }
  271. int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
  272. VdpGetProcAddress *get_proc, unsigned flags)
  273. {
  274. VDPAUHWContext *hwctx;
  275. if (flags & ~AV_HWACCEL_FLAG_IGNORE_LEVEL)
  276. return AVERROR(EINVAL);
  277. if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
  278. return AVERROR(ENOMEM);
  279. hwctx = avctx->hwaccel_context;
  280. memset(hwctx, 0, sizeof(*hwctx));
  281. hwctx->context.decoder = VDP_INVALID_HANDLE;
  282. hwctx->device = device;
  283. hwctx->get_proc_address = get_proc;
  284. hwctx->flags = flags;
  285. hwctx->reset = 1;
  286. return 0;
  287. }
  288. /* @}*/