581eadae36d0cb055879aae7d24de3be61b2bbcb
[ffmpeg.git] / libavcodec / vdpau.c
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
24 #include <limits.h>
25 #include "avcodec.h"
26 #include "h264.h"
27 #include "vc1.h"
28
29 #undef NDEBUG
30 #include <assert.h>
31
32 #include "vdpau.h"
33 #include "vdpau_internal.h"
34
35 /**
36  * @addtogroup VDPAU_Decoding
37  *
38  * @{
39  */
40
41 static int vdpau_error(VdpStatus status)
42 {
43     switch (status) {
44     case VDP_STATUS_OK:
45         return 0;
46     case VDP_STATUS_NO_IMPLEMENTATION:
47         return AVERROR(ENOSYS);
48     case VDP_STATUS_DISPLAY_PREEMPTED:
49         return AVERROR(EIO);
50     case VDP_STATUS_INVALID_HANDLE:
51         return AVERROR(EBADF);
52     case VDP_STATUS_INVALID_POINTER:
53         return AVERROR(EFAULT);
54     case VDP_STATUS_RESOURCES:
55         return AVERROR(ENOBUFS);
56     case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
57         return AVERROR(EXDEV);
58     case VDP_STATUS_ERROR:
59         return AVERROR(EIO);
60     default:
61         return AVERROR(EINVAL);
62     }
63 }
64
65 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
66                                 av_unused const uint8_t *buffer,
67                                 av_unused uint32_t size)
68 {
69     pic_ctx->bitstream_buffers_allocated = 0;
70     pic_ctx->bitstream_buffers_used      = 0;
71     pic_ctx->bitstream_buffers           = NULL;
72     return 0;
73 }
74
75 #if CONFIG_H263_VDPAU_HWACCEL  || CONFIG_MPEG1_VDPAU_HWACCEL || \
76     CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
77     CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
78 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
79 {
80     AVVDPAUContext *hwctx = avctx->hwaccel_context;
81     MpegEncContext *s = avctx->priv_data;
82     Picture *pic = s->current_picture_ptr;
83     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
84     VdpVideoSurface surf = ff_vdpau_get_surface_id(pic->f);
85
86     hwctx->render(hwctx->decoder, surf, (void *)&pic_ctx->info,
87                   pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
88
89     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
90     av_freep(&pic_ctx->bitstream_buffers);
91
92     return 0;
93 }
94 #endif
95
96 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
97                         const uint8_t *buf, uint32_t size)
98 {
99     VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
100
101     buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
102                               (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
103     if (!buffers)
104         return AVERROR(ENOMEM);
105
106     pic_ctx->bitstream_buffers = buffers;
107     buffers += pic_ctx->bitstream_buffers_used++;
108
109     buffers->struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
110     buffers->bitstream       = buf;
111     buffers->bitstream_bytes = size;
112     return 0;
113 }
114
115 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
116 {
117 #define PROFILE(prof)       \
118 do {                        \
119     *profile = prof;        \
120     return 0;               \
121 } while (0)
122
123     switch (avctx->codec_id) {
124     case AV_CODEC_ID_MPEG1VIDEO:               PROFILE(VDP_DECODER_PROFILE_MPEG1);
125     case AV_CODEC_ID_MPEG2VIDEO:
126         switch (avctx->profile) {
127         case FF_PROFILE_MPEG2_MAIN:            PROFILE(VDP_DECODER_PROFILE_MPEG2_MAIN);
128         case FF_PROFILE_MPEG2_SIMPLE:          PROFILE(VDP_DECODER_PROFILE_MPEG2_SIMPLE);
129         default:                               return AVERROR(EINVAL);
130         }
131     case AV_CODEC_ID_H263:                     PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
132     case AV_CODEC_ID_MPEG4:
133         switch (avctx->profile) {
134         case FF_PROFILE_MPEG4_SIMPLE:          PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_SP);
135         case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
136         default:                               return AVERROR(EINVAL);
137         }
138     case AV_CODEC_ID_H264:
139         switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
140         case FF_PROFILE_H264_CONSTRAINED_BASELINE:
141         case FF_PROFILE_H264_BASELINE:         PROFILE(VDP_DECODER_PROFILE_H264_BASELINE);
142         case FF_PROFILE_H264_MAIN:             PROFILE(VDP_DECODER_PROFILE_H264_MAIN);
143         case FF_PROFILE_H264_HIGH:             PROFILE(VDP_DECODER_PROFILE_H264_HIGH);
144         default:                               return AVERROR(EINVAL);
145         }
146     case AV_CODEC_ID_WMV3:
147     case AV_CODEC_ID_VC1:
148         switch (avctx->profile) {
149         case FF_PROFILE_VC1_SIMPLE:            PROFILE(VDP_DECODER_PROFILE_VC1_SIMPLE);
150         case FF_PROFILE_VC1_MAIN:              PROFILE(VDP_DECODER_PROFILE_VC1_MAIN);
151         case FF_PROFILE_VC1_ADVANCED:          PROFILE(VDP_DECODER_PROFILE_VC1_ADVANCED);
152         default:                               return AVERROR(EINVAL);
153         }
154     }
155     return AVERROR(EINVAL);
156 }
157
158 AVVDPAUContext *av_vdpau_alloc_context(void)
159 {
160     return av_mallocz(sizeof(AVVDPAUContext));
161 }
162
163 /* @}*/