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.

241 lines
7.9KB

  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. VdpDecoderCreate *create;
  67. void *func;
  68. VdpStatus status;
  69. /* See vdpau/vdpau.h for alignment constraints. */
  70. uint32_t width = (avctx->coded_width + 1) & ~1;
  71. uint32_t height = (avctx->coded_height + 3) & ~3;
  72. if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
  73. vdctx->decoder = hwctx->context.decoder;
  74. vdctx->render = hwctx->context.render;
  75. vdctx->device = VDP_INVALID_HANDLE;
  76. return 0; /* Decoder created by user */
  77. }
  78. vdctx->device = hwctx->device;
  79. vdctx->get_proc_address = hwctx->get_proc_address;
  80. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
  81. &func);
  82. if (status != VDP_STATUS_OK)
  83. return vdpau_error(status);
  84. else
  85. create = func;
  86. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
  87. &func);
  88. if (status != VDP_STATUS_OK)
  89. return vdpau_error(status);
  90. else
  91. vdctx->render = func;
  92. status = create(vdctx->device, profile, width, height, avctx->refs,
  93. &vdctx->decoder);
  94. return vdpau_error(status);
  95. }
  96. int ff_vdpau_common_uninit(AVCodecContext *avctx)
  97. {
  98. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  99. VdpDecoderDestroy *destroy;
  100. void *func;
  101. VdpStatus status;
  102. if (vdctx->device == VDP_INVALID_HANDLE)
  103. return 0; /* Decoder created and destroyed by user */
  104. status = vdctx->get_proc_address(vdctx->device,
  105. VDP_FUNC_ID_DECODER_DESTROY, &func);
  106. if (status != VDP_STATUS_OK)
  107. return vdpau_error(status);
  108. else
  109. destroy = func;
  110. status = destroy(vdctx->decoder);
  111. return vdpau_error(status);
  112. }
  113. int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
  114. av_unused const uint8_t *buffer,
  115. av_unused uint32_t size)
  116. {
  117. pic_ctx->bitstream_buffers_allocated = 0;
  118. pic_ctx->bitstream_buffers_used = 0;
  119. pic_ctx->bitstream_buffers = NULL;
  120. return 0;
  121. }
  122. int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  123. struct vdpau_picture_context *pic_ctx)
  124. {
  125. AVVDPAUContext *hwctx = avctx->hwaccel_context;
  126. VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
  127. VdpStatus status;
  128. status = hwctx->render(hwctx->decoder, surf, (void *)&pic_ctx->info,
  129. pic_ctx->bitstream_buffers_used,
  130. pic_ctx->bitstream_buffers);
  131. av_freep(&pic_ctx->bitstream_buffers);
  132. return vdpau_error(status);
  133. }
  134. #if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG1_VDPAU_HWACCEL || \
  135. CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
  136. CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
  137. int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
  138. {
  139. MpegEncContext *s = avctx->priv_data;
  140. Picture *pic = s->current_picture_ptr;
  141. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  142. int val;
  143. val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
  144. if (val < 0)
  145. return val;
  146. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  147. return 0;
  148. }
  149. #endif
  150. int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
  151. const uint8_t *buf, uint32_t size)
  152. {
  153. VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
  154. buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
  155. (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
  156. if (!buffers)
  157. return AVERROR(ENOMEM);
  158. pic_ctx->bitstream_buffers = buffers;
  159. buffers += pic_ctx->bitstream_buffers_used++;
  160. buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  161. buffers->bitstream = buf;
  162. buffers->bitstream_bytes = size;
  163. return 0;
  164. }
  165. int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
  166. {
  167. #define PROFILE(prof) \
  168. do { \
  169. *profile = prof; \
  170. return 0; \
  171. } while (0)
  172. switch (avctx->codec_id) {
  173. case AV_CODEC_ID_MPEG1VIDEO: PROFILE(VDP_DECODER_PROFILE_MPEG1);
  174. case AV_CODEC_ID_MPEG2VIDEO:
  175. switch (avctx->profile) {
  176. case FF_PROFILE_MPEG2_MAIN: PROFILE(VDP_DECODER_PROFILE_MPEG2_MAIN);
  177. case FF_PROFILE_MPEG2_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG2_SIMPLE);
  178. default: return AVERROR(EINVAL);
  179. }
  180. case AV_CODEC_ID_H263: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
  181. case AV_CODEC_ID_MPEG4:
  182. switch (avctx->profile) {
  183. case FF_PROFILE_MPEG4_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_SP);
  184. case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
  185. default: return AVERROR(EINVAL);
  186. }
  187. case AV_CODEC_ID_H264:
  188. switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
  189. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  190. case FF_PROFILE_H264_BASELINE: PROFILE(VDP_DECODER_PROFILE_H264_BASELINE);
  191. case FF_PROFILE_H264_MAIN: PROFILE(VDP_DECODER_PROFILE_H264_MAIN);
  192. case FF_PROFILE_H264_HIGH: PROFILE(VDP_DECODER_PROFILE_H264_HIGH);
  193. default: return AVERROR(EINVAL);
  194. }
  195. case AV_CODEC_ID_WMV3:
  196. case AV_CODEC_ID_VC1:
  197. switch (avctx->profile) {
  198. case FF_PROFILE_VC1_SIMPLE: PROFILE(VDP_DECODER_PROFILE_VC1_SIMPLE);
  199. case FF_PROFILE_VC1_MAIN: PROFILE(VDP_DECODER_PROFILE_VC1_MAIN);
  200. case FF_PROFILE_VC1_ADVANCED: PROFILE(VDP_DECODER_PROFILE_VC1_ADVANCED);
  201. default: return AVERROR(EINVAL);
  202. }
  203. }
  204. return AVERROR(EINVAL);
  205. }
  206. AVVDPAUContext *av_vdpau_alloc_context(void)
  207. {
  208. return av_mallocz(sizeof(AVVDPAUContext));
  209. }
  210. /* @}*/