vdpau: factor out common end-of-frame handling
[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 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
76                               struct vdpau_picture_context *pic_ctx)
77 {
78     AVVDPAUContext *hwctx = avctx->hwaccel_context;
79     VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
80     VdpStatus status;
81
82     status = hwctx->render(hwctx->decoder, surf, (void *)&pic_ctx->info,
83                            pic_ctx->bitstream_buffers_used,
84                            pic_ctx->bitstream_buffers);
85
86     av_freep(&pic_ctx->bitstream_buffers);
87     return vdpau_error(status);
88 }
89
90 #if CONFIG_H263_VDPAU_HWACCEL  || CONFIG_MPEG1_VDPAU_HWACCEL || \
91     CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
92     CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
93 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
94 {
95     MpegEncContext *s = avctx->priv_data;
96     Picture *pic = s->current_picture_ptr;
97     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
98     int val;
99
100     val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
101     if (val < 0)
102         return val;
103
104     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
105     return 0;
106 }
107 #endif
108
109 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
110                         const uint8_t *buf, uint32_t size)
111 {
112     VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
113
114     buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
115                               (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
116     if (!buffers)
117         return AVERROR(ENOMEM);
118
119     pic_ctx->bitstream_buffers = buffers;
120     buffers += pic_ctx->bitstream_buffers_used++;
121
122     buffers->struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
123     buffers->bitstream       = buf;
124     buffers->bitstream_bytes = size;
125     return 0;
126 }
127
128 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
129 {
130 #define PROFILE(prof)       \
131 do {                        \
132     *profile = prof;        \
133     return 0;               \
134 } while (0)
135
136     switch (avctx->codec_id) {
137     case AV_CODEC_ID_MPEG1VIDEO:               PROFILE(VDP_DECODER_PROFILE_MPEG1);
138     case AV_CODEC_ID_MPEG2VIDEO:
139         switch (avctx->profile) {
140         case FF_PROFILE_MPEG2_MAIN:            PROFILE(VDP_DECODER_PROFILE_MPEG2_MAIN);
141         case FF_PROFILE_MPEG2_SIMPLE:          PROFILE(VDP_DECODER_PROFILE_MPEG2_SIMPLE);
142         default:                               return AVERROR(EINVAL);
143         }
144     case AV_CODEC_ID_H263:                     PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
145     case AV_CODEC_ID_MPEG4:
146         switch (avctx->profile) {
147         case FF_PROFILE_MPEG4_SIMPLE:          PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_SP);
148         case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
149         default:                               return AVERROR(EINVAL);
150         }
151     case AV_CODEC_ID_H264:
152         switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
153         case FF_PROFILE_H264_CONSTRAINED_BASELINE:
154         case FF_PROFILE_H264_BASELINE:         PROFILE(VDP_DECODER_PROFILE_H264_BASELINE);
155         case FF_PROFILE_H264_MAIN:             PROFILE(VDP_DECODER_PROFILE_H264_MAIN);
156         case FF_PROFILE_H264_HIGH:             PROFILE(VDP_DECODER_PROFILE_H264_HIGH);
157         default:                               return AVERROR(EINVAL);
158         }
159     case AV_CODEC_ID_WMV3:
160     case AV_CODEC_ID_VC1:
161         switch (avctx->profile) {
162         case FF_PROFILE_VC1_SIMPLE:            PROFILE(VDP_DECODER_PROFILE_VC1_SIMPLE);
163         case FF_PROFILE_VC1_MAIN:              PROFILE(VDP_DECODER_PROFILE_VC1_MAIN);
164         case FF_PROFILE_VC1_ADVANCED:          PROFILE(VDP_DECODER_PROFILE_VC1_ADVANCED);
165         default:                               return AVERROR(EINVAL);
166         }
167     }
168     return AVERROR(EINVAL);
169 }
170
171 AVVDPAUContext *av_vdpau_alloc_context(void)
172 {
173     return av_mallocz(sizeof(AVVDPAUContext));
174 }
175
176 /* @}*/