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.

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