]> git.sesse.net Git - ffmpeg/blob - libavcodec/vdpau.c
vdpau: return MAIN instead of BASELINE for H.264 CBP
[ffmpeg] / 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 "libavutil/avassert.h"
26 #include "avcodec.h"
27 #include "internal.h"
28 #include "h264.h"
29 #include "vc1.h"
30
31 #undef NDEBUG
32 #include <assert.h>
33
34 #include "vdpau.h"
35 #include "vdpau_internal.h"
36
37 /**
38  * @addtogroup VDPAU_Decoding
39  *
40  * @{
41  */
42
43 static int vdpau_error(VdpStatus status)
44 {
45     switch (status) {
46     case VDP_STATUS_OK:
47         return 0;
48     case VDP_STATUS_NO_IMPLEMENTATION:
49         return AVERROR(ENOSYS);
50     case VDP_STATUS_DISPLAY_PREEMPTED:
51         return AVERROR(EIO);
52     case VDP_STATUS_INVALID_HANDLE:
53         return AVERROR(EBADF);
54     case VDP_STATUS_INVALID_POINTER:
55         return AVERROR(EFAULT);
56     case VDP_STATUS_RESOURCES:
57         return AVERROR(ENOBUFS);
58     case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
59         return AVERROR(EXDEV);
60     case VDP_STATUS_ERROR:
61         return AVERROR(EIO);
62     default:
63         return AVERROR(EINVAL);
64     }
65 }
66
67 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
68                          int level)
69 {
70     VDPAUHWContext *hwctx = avctx->hwaccel_context;
71     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
72     VdpVideoSurfaceQueryCapabilities *surface_query_caps;
73     VdpDecoderQueryCapabilities *decoder_query_caps;
74     VdpDecoderCreate *create;
75     void *func;
76     VdpStatus status;
77     VdpBool supported;
78     uint32_t max_level, max_mb, max_width, max_height;
79     /* See vdpau/vdpau.h for alignment constraints. */
80     uint32_t width  = (avctx->coded_width + 1) & ~1;
81     uint32_t height = (avctx->coded_height + 3) & ~3;
82
83     vdctx->width            = UINT32_MAX;
84     vdctx->height           = UINT32_MAX;
85     hwctx->reset            = 0;
86
87     if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
88         vdctx->decoder = hwctx->context.decoder;
89         vdctx->render  = hwctx->context.render;
90         vdctx->device  = VDP_INVALID_HANDLE;
91         return 0; /* Decoder created by user */
92     }
93
94     vdctx->device           = hwctx->device;
95     vdctx->get_proc_address = hwctx->get_proc_address;
96
97     if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
98         level = 0;
99     else if (level < 0)
100         return AVERROR(ENOTSUP);
101
102     status = vdctx->get_proc_address(vdctx->device,
103                                      VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
104                                      &func);
105     if (status != VDP_STATUS_OK)
106         return vdpau_error(status);
107     else
108         surface_query_caps = func;
109
110     status = surface_query_caps(vdctx->device, VDP_CHROMA_TYPE_420, &supported,
111                                 &max_width, &max_height);
112     if (status != VDP_STATUS_OK)
113         return vdpau_error(status);
114     if (supported != VDP_TRUE ||
115         max_width < width || max_height < height)
116         return AVERROR(ENOTSUP);
117
118     status = vdctx->get_proc_address(vdctx->device,
119                                      VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
120                                      &func);
121     if (status != VDP_STATUS_OK)
122         return vdpau_error(status);
123     else
124         decoder_query_caps = func;
125
126     status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
127                                 &max_mb, &max_width, &max_height);
128     if (status != VDP_STATUS_OK)
129         return vdpau_error(status);
130
131     if (supported != VDP_TRUE || max_level < level ||
132         max_width < width || max_height < height)
133         return AVERROR(ENOTSUP);
134
135     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
136                                      &func);
137     if (status != VDP_STATUS_OK)
138         return vdpau_error(status);
139     else
140         create = func;
141
142     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
143                                      &func);
144     if (status != VDP_STATUS_OK)
145         return vdpau_error(status);
146     else
147         vdctx->render = func;
148
149     status = create(vdctx->device, profile, width, height, avctx->refs,
150                     &vdctx->decoder);
151     if (status == VDP_STATUS_OK) {
152         vdctx->width  = avctx->coded_width;
153         vdctx->height = avctx->coded_height;
154     }
155
156     return vdpau_error(status);
157 }
158
159 int ff_vdpau_common_uninit(AVCodecContext *avctx)
160 {
161     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
162     VdpDecoderDestroy *destroy;
163     void *func;
164     VdpStatus status;
165
166     if (vdctx->device == VDP_INVALID_HANDLE)
167         return 0; /* Decoder created and destroyed by user */
168     if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
169         return 0;
170
171     status = vdctx->get_proc_address(vdctx->device,
172                                      VDP_FUNC_ID_DECODER_DESTROY, &func);
173     if (status != VDP_STATUS_OK)
174         return vdpau_error(status);
175     else
176         destroy = func;
177
178     status = destroy(vdctx->decoder);
179     return vdpau_error(status);
180 }
181
182 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
183 {
184     VDPAUHWContext *hwctx = avctx->hwaccel_context;
185     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
186
187     if (vdctx->device == VDP_INVALID_HANDLE)
188         return 0; /* Decoder created by user */
189     if (avctx->coded_width == vdctx->width &&
190         avctx->coded_height == vdctx->height && !hwctx->reset)
191         return 0;
192
193     avctx->hwaccel->uninit(avctx);
194     return avctx->hwaccel->init(avctx);
195 }
196
197 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
198                                 av_unused const uint8_t *buffer,
199                                 av_unused uint32_t size)
200 {
201     pic_ctx->bitstream_buffers_allocated = 0;
202     pic_ctx->bitstream_buffers_used      = 0;
203     pic_ctx->bitstream_buffers           = NULL;
204     return 0;
205 }
206
207 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
208                               struct vdpau_picture_context *pic_ctx)
209 {
210     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
211     VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
212     VdpStatus status;
213     int val;
214
215     val = ff_vdpau_common_reinit(avctx);
216     if (val < 0)
217         return val;
218
219     status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
220                            pic_ctx->bitstream_buffers_used,
221                            pic_ctx->bitstream_buffers);
222
223     av_freep(&pic_ctx->bitstream_buffers);
224     return vdpau_error(status);
225 }
226
227 #if CONFIG_H263_VDPAU_HWACCEL  || CONFIG_MPEG1_VDPAU_HWACCEL || \
228     CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
229     CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
230 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
231 {
232     MpegEncContext *s = avctx->priv_data;
233     Picture *pic = s->current_picture_ptr;
234     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
235     int val;
236
237     val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
238     if (val < 0)
239         return val;
240
241     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
242     return 0;
243 }
244 #endif
245
246 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
247                         const uint8_t *buf, uint32_t size)
248 {
249     VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
250
251     buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
252                               (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
253     if (!buffers)
254         return AVERROR(ENOMEM);
255
256     pic_ctx->bitstream_buffers = buffers;
257     buffers += pic_ctx->bitstream_buffers_used++;
258
259     buffers->struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
260     buffers->bitstream       = buf;
261     buffers->bitstream_bytes = size;
262     return 0;
263 }
264
265 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
266 {
267 #define PROFILE(prof)       \
268 do {                        \
269     *profile = prof;        \
270     return 0;               \
271 } while (0)
272
273     switch (avctx->codec_id) {
274     case AV_CODEC_ID_MPEG1VIDEO:               PROFILE(VDP_DECODER_PROFILE_MPEG1);
275     case AV_CODEC_ID_MPEG2VIDEO:
276         switch (avctx->profile) {
277         case FF_PROFILE_MPEG2_MAIN:            PROFILE(VDP_DECODER_PROFILE_MPEG2_MAIN);
278         case FF_PROFILE_MPEG2_SIMPLE:          PROFILE(VDP_DECODER_PROFILE_MPEG2_SIMPLE);
279         default:                               return AVERROR(EINVAL);
280         }
281     case AV_CODEC_ID_H263:                     PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
282     case AV_CODEC_ID_MPEG4:
283         switch (avctx->profile) {
284         case FF_PROFILE_MPEG4_SIMPLE:          PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_SP);
285         case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
286         default:                               return AVERROR(EINVAL);
287         }
288     case AV_CODEC_ID_H264:
289         switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
290         case FF_PROFILE_H264_BASELINE:         PROFILE(VDP_DECODER_PROFILE_H264_BASELINE);
291         case FF_PROFILE_H264_CONSTRAINED_BASELINE:
292         case FF_PROFILE_H264_MAIN:             PROFILE(VDP_DECODER_PROFILE_H264_MAIN);
293         case FF_PROFILE_H264_HIGH:             PROFILE(VDP_DECODER_PROFILE_H264_HIGH);
294         default:                               return AVERROR(EINVAL);
295         }
296     case AV_CODEC_ID_WMV3:
297     case AV_CODEC_ID_VC1:
298         switch (avctx->profile) {
299         case FF_PROFILE_VC1_SIMPLE:            PROFILE(VDP_DECODER_PROFILE_VC1_SIMPLE);
300         case FF_PROFILE_VC1_MAIN:              PROFILE(VDP_DECODER_PROFILE_VC1_MAIN);
301         case FF_PROFILE_VC1_ADVANCED:          PROFILE(VDP_DECODER_PROFILE_VC1_ADVANCED);
302         default:                               return AVERROR(EINVAL);
303         }
304     }
305     return AVERROR(EINVAL);
306 }
307
308 AVVDPAUContext *av_vdpau_alloc_context(void)
309 {
310     return av_mallocz(sizeof(AVVDPAUContext));
311 }
312
313 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
314                           VdpGetProcAddress *get_proc, unsigned flags)
315 {
316     VDPAUHWContext *hwctx;
317
318     if (flags & ~AV_HWACCEL_FLAG_IGNORE_LEVEL)
319         return AVERROR(EINVAL);
320
321     if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
322         return AVERROR(ENOMEM);
323
324     hwctx = avctx->hwaccel_context;
325
326     memset(hwctx, 0, sizeof(*hwctx));
327     hwctx->context.decoder  = VDP_INVALID_HANDLE;
328     hwctx->device           = device;
329     hwctx->get_proc_address = get_proc;
330     hwctx->flags            = flags;
331     hwctx->reset            = 1;
332     return 0;
333 }
334
335 /* @}*/