]> git.sesse.net Git - ffmpeg/blob - libavcodec/vdpau.c
lavfi: Drop unused and empty header file
[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
122     if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
123         return AVERROR(ENOSYS);
124
125     if (hwctx) {
126         hwctx->reset            = 0;
127
128         if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
129             vdctx->decoder = hwctx->context.decoder;
130             vdctx->render  = hwctx->context.render;
131             vdctx->device  = VDP_INVALID_HANDLE;
132             return 0; /* Decoder created by user */
133         }
134
135         vdctx->device           = hwctx->device;
136         vdctx->get_proc_address = hwctx->get_proc_address;
137
138         if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
139             level = 0;
140
141         if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
142             type != VDP_CHROMA_TYPE_420)
143             return AVERROR(ENOSYS);
144     } else {
145         AVHWFramesContext *frames_ctx = NULL;
146         AVVDPAUDeviceContext *dev_ctx;
147
148         // We assume the hw_frames_ctx always survives until ff_vdpau_common_uninit
149         // is called. This holds true as the user is not allowed to touch
150         // hw_device_ctx, or hw_frames_ctx after get_format (and ff_get_format
151         // itself also uninits before unreffing hw_frames_ctx).
152         if (avctx->hw_frames_ctx) {
153             frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
154         } else if (avctx->hw_device_ctx) {
155             int ret;
156
157             avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
158             if (!avctx->hw_frames_ctx)
159                 return AVERROR(ENOMEM);
160
161             frames_ctx            = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
162             frames_ctx->format    = AV_PIX_FMT_VDPAU;
163             frames_ctx->sw_format = avctx->sw_pix_fmt;
164             frames_ctx->width     = avctx->coded_width;
165             frames_ctx->height    = avctx->coded_height;
166
167             ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
168             if (ret < 0) {
169                 av_buffer_unref(&avctx->hw_frames_ctx);
170                 return ret;
171             }
172         }
173
174         if (!frames_ctx) {
175             av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
176                    "required for VDPAU decoding.\n");
177             return AVERROR(EINVAL);
178         }
179
180         dev_ctx = frames_ctx->device_ctx->hwctx;
181
182         vdctx->device           = dev_ctx->device;
183         vdctx->get_proc_address = dev_ctx->get_proc_address;
184
185         if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
186             level = 0;
187     }
188
189     if (level < 0)
190         return AVERROR(ENOTSUP);
191
192     status = vdctx->get_proc_address(vdctx->device,
193                                      VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
194                                      &func);
195     if (status != VDP_STATUS_OK)
196         return vdpau_error(status);
197     else
198         surface_query_caps = func;
199
200     status = surface_query_caps(vdctx->device, type, &supported,
201                                 &max_width, &max_height);
202     if (status != VDP_STATUS_OK)
203         return vdpau_error(status);
204     if (supported != VDP_TRUE ||
205         max_width < width || max_height < height)
206         return AVERROR(ENOTSUP);
207
208     status = vdctx->get_proc_address(vdctx->device,
209                                      VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
210                                      &func);
211     if (status != VDP_STATUS_OK)
212         return vdpau_error(status);
213     else
214         decoder_query_caps = func;
215
216     status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
217                                 &max_mb, &max_width, &max_height);
218 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
219     if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
220         profile = VDP_DECODER_PROFILE_H264_MAIN;
221         status = decoder_query_caps(vdctx->device, profile, &supported,
222                                     &max_level, &max_mb,
223                                     &max_width, &max_height);
224     }
225 #endif
226     if (status != VDP_STATUS_OK)
227         return vdpau_error(status);
228
229     if (supported != VDP_TRUE || max_level < level ||
230         max_width < width || max_height < height)
231         return AVERROR(ENOTSUP);
232
233     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
234                                      &func);
235     if (status != VDP_STATUS_OK)
236         return vdpau_error(status);
237     else
238         create = func;
239
240     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
241                                      &func);
242     if (status != VDP_STATUS_OK)
243         return vdpau_error(status);
244     else
245         vdctx->render = func;
246
247     status = create(vdctx->device, profile, width, height, avctx->refs,
248                     &vdctx->decoder);
249     if (status == VDP_STATUS_OK) {
250         vdctx->width  = avctx->coded_width;
251         vdctx->height = avctx->coded_height;
252     }
253
254     return vdpau_error(status);
255 }
256
257 int ff_vdpau_common_uninit(AVCodecContext *avctx)
258 {
259     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
260     VdpDecoderDestroy *destroy;
261     void *func;
262     VdpStatus status;
263
264     if (vdctx->device == VDP_INVALID_HANDLE)
265         return 0; /* Decoder created and destroyed by user */
266     if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
267         return 0;
268
269     status = vdctx->get_proc_address(vdctx->device,
270                                      VDP_FUNC_ID_DECODER_DESTROY, &func);
271     if (status != VDP_STATUS_OK)
272         return vdpau_error(status);
273     else
274         destroy = func;
275
276     status = destroy(vdctx->decoder);
277     return vdpau_error(status);
278 }
279
280 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
281 {
282     VDPAUHWContext *hwctx = avctx->hwaccel_context;
283     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
284
285     if (vdctx->device == VDP_INVALID_HANDLE)
286         return 0; /* Decoder created by user */
287     if (avctx->coded_width == vdctx->width &&
288         avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
289         return 0;
290
291     avctx->hwaccel->uninit(avctx);
292     return avctx->hwaccel->init(avctx);
293 }
294
295 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
296                                 av_unused const uint8_t *buffer,
297                                 av_unused uint32_t size)
298 {
299     pic_ctx->bitstream_buffers_allocated = 0;
300     pic_ctx->bitstream_buffers_used      = 0;
301     pic_ctx->bitstream_buffers           = NULL;
302     return 0;
303 }
304
305 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
306                               struct vdpau_picture_context *pic_ctx)
307 {
308     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
309     VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
310     VdpStatus status;
311     int val;
312
313     val = ff_vdpau_common_reinit(avctx);
314     if (val < 0)
315         return val;
316
317     status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
318                            pic_ctx->bitstream_buffers_used,
319                            pic_ctx->bitstream_buffers);
320
321     av_freep(&pic_ctx->bitstream_buffers);
322     return vdpau_error(status);
323 }
324
325 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
326     CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
327     CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
328 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
329 {
330     MpegEncContext *s = avctx->priv_data;
331     Picture *pic = s->current_picture_ptr;
332     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
333     int val;
334
335     val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
336     if (val < 0)
337         return val;
338
339     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
340     return 0;
341 }
342 #endif
343
344 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
345                         const uint8_t *buf, uint32_t size)
346 {
347     VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
348
349     buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
350                               (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
351     if (!buffers)
352         return AVERROR(ENOMEM);
353
354     pic_ctx->bitstream_buffers = buffers;
355     buffers += pic_ctx->bitstream_buffers_used++;
356
357     buffers->struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
358     buffers->bitstream       = buf;
359     buffers->bitstream_bytes = size;
360     return 0;
361 }
362
363 #if FF_API_VDPAU_PROFILE
364 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
365 {
366 #define PROFILE(prof)                      \
367 do {                                       \
368     *profile = VDP_DECODER_PROFILE_##prof; \
369     return 0;                              \
370 } while (0)
371
372     switch (avctx->codec_id) {
373     case AV_CODEC_ID_MPEG1VIDEO:               PROFILE(MPEG1);
374     case AV_CODEC_ID_MPEG2VIDEO:
375         switch (avctx->profile) {
376         case FF_PROFILE_MPEG2_MAIN:            PROFILE(MPEG2_MAIN);
377         case FF_PROFILE_MPEG2_SIMPLE:          PROFILE(MPEG2_SIMPLE);
378         default:                               return AVERROR(EINVAL);
379         }
380     case AV_CODEC_ID_H263:                     PROFILE(MPEG4_PART2_ASP);
381     case AV_CODEC_ID_MPEG4:
382         switch (avctx->profile) {
383         case FF_PROFILE_MPEG4_SIMPLE:          PROFILE(MPEG4_PART2_SP);
384         case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
385         default:                               return AVERROR(EINVAL);
386         }
387     case AV_CODEC_ID_H264:
388         switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
389         case FF_PROFILE_H264_BASELINE:         PROFILE(H264_BASELINE);
390         case FF_PROFILE_H264_CONSTRAINED_BASELINE:
391         case FF_PROFILE_H264_MAIN:             PROFILE(H264_MAIN);
392         case FF_PROFILE_H264_HIGH:             PROFILE(H264_HIGH);
393 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
394         case FF_PROFILE_H264_EXTENDED:         PROFILE(H264_EXTENDED);
395 #endif
396         default:                               return AVERROR(EINVAL);
397         }
398     case AV_CODEC_ID_WMV3:
399     case AV_CODEC_ID_VC1:
400         switch (avctx->profile) {
401         case FF_PROFILE_VC1_SIMPLE:            PROFILE(VC1_SIMPLE);
402         case FF_PROFILE_VC1_MAIN:              PROFILE(VC1_MAIN);
403         case FF_PROFILE_VC1_ADVANCED:          PROFILE(VC1_ADVANCED);
404         default:                               return AVERROR(EINVAL);
405         }
406     }
407     return AVERROR(EINVAL);
408 #undef PROFILE
409 }
410 #endif /* FF_API_VDPAU_PROFILE */
411
412 AVVDPAUContext *av_vdpau_alloc_context(void)
413 {
414     return av_mallocz(sizeof(AVVDPAUContext));
415 }
416
417 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
418                           VdpGetProcAddress *get_proc, unsigned flags)
419 {
420     VDPAUHWContext *hwctx;
421
422     if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
423         return AVERROR(EINVAL);
424
425     if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
426         return AVERROR(ENOMEM);
427
428     hwctx = avctx->hwaccel_context;
429
430     memset(hwctx, 0, sizeof(*hwctx));
431     hwctx->context.decoder  = VDP_INVALID_HANDLE;
432     hwctx->device           = device;
433     hwctx->get_proc_address = get_proc;
434     hwctx->flags            = flags;
435     hwctx->reset            = 1;
436     return 0;
437 }
438
439 /* @}*/