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