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