]> git.sesse.net Git - ffmpeg/blob - libavcodec/vdpau.c
Merge commit '8c616b3b8996bd4f9b117496b66b16cc625d7d24'
[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 FFmpeg.
8  *
9  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 // XXX: at the time of adding this ifdefery, av_assert* wasn't use outside.
34 // When dropping it, make sure other av_assert* were not added since then.
35
36 /**
37  * @addtogroup VDPAU_Decoding
38  *
39  * @{
40  */
41
42 static int vdpau_error(VdpStatus status)
43 {
44     switch (status) {
45     case VDP_STATUS_OK:
46         return 0;
47     case VDP_STATUS_NO_IMPLEMENTATION:
48         return AVERROR(ENOSYS);
49     case VDP_STATUS_DISPLAY_PREEMPTED:
50         return AVERROR(EIO);
51     case VDP_STATUS_INVALID_HANDLE:
52         return AVERROR(EBADF);
53     case VDP_STATUS_INVALID_POINTER:
54         return AVERROR(EFAULT);
55     case VDP_STATUS_RESOURCES:
56         return AVERROR(ENOBUFS);
57     case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
58         return AVERROR(EXDEV);
59     case VDP_STATUS_ERROR:
60         return AVERROR(EIO);
61     default:
62         return AVERROR(EINVAL);
63     }
64 }
65
66 AVVDPAUContext *av_alloc_vdpaucontext(void)
67 {
68     return av_vdpau_alloc_context();
69 }
70
71 MAKE_ACCESSORS(AVVDPAUContext, vdpau_hwaccel, AVVDPAU_Render2, render2)
72
73 int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
74                                     VdpChromaType *type,
75                                     uint32_t *width, uint32_t *height)
76 {
77     VdpChromaType t;
78     uint32_t w = avctx->coded_width;
79     uint32_t h = avctx->coded_height;
80
81     /* See <vdpau/vdpau.h> for per-type alignment constraints. */
82     switch (avctx->sw_pix_fmt) {
83     case AV_PIX_FMT_YUV420P:
84     case AV_PIX_FMT_YUVJ420P:
85         t = VDP_CHROMA_TYPE_420;
86         w = (w + 1) & ~1;
87         h = (h + 3) & ~3;
88         break;
89     case AV_PIX_FMT_YUV422P:
90     case AV_PIX_FMT_YUVJ422P:
91         t = VDP_CHROMA_TYPE_422;
92         w = (w + 1) & ~1;
93         h = (h + 1) & ~1;
94         break;
95     case AV_PIX_FMT_YUV444P:
96     case AV_PIX_FMT_YUVJ444P:
97         t = VDP_CHROMA_TYPE_444;
98         h = (h + 1) & ~1;
99         break;
100     default:
101         return AVERROR(ENOSYS);
102     }
103
104     if (type)
105         *type = t;
106     if (width)
107         *width = w;
108     if (height)
109         *height = h;
110     return 0;
111 }
112
113 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
114                          int level)
115 {
116     VDPAUHWContext *hwctx = avctx->hwaccel_context;
117     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
118     VdpVideoSurfaceQueryCapabilities *surface_query_caps;
119     VdpDecoderQueryCapabilities *decoder_query_caps;
120     VdpDecoderCreate *create;
121     VdpGetInformationString *info;
122     const char *info_string;
123     void *func;
124     VdpStatus status;
125     VdpBool supported;
126     uint32_t max_level, max_mb, max_width, max_height;
127     VdpChromaType type;
128     uint32_t width;
129     uint32_t height;
130
131     vdctx->width            = UINT32_MAX;
132     vdctx->height           = UINT32_MAX;
133
134     if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
135         return AVERROR(ENOSYS);
136
137     if (hwctx) {
138         hwctx->reset            = 0;
139
140         if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
141             vdctx->decoder = hwctx->context.decoder;
142             vdctx->render  = hwctx->context.render;
143             vdctx->device  = VDP_INVALID_HANDLE;
144             return 0; /* Decoder created by user */
145         }
146
147         vdctx->device           = hwctx->device;
148         vdctx->get_proc_address = hwctx->get_proc_address;
149
150         if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
151             level = 0;
152
153         if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
154             type != VDP_CHROMA_TYPE_420)
155             return AVERROR(ENOSYS);
156     } else {
157         AVHWFramesContext *frames_ctx = NULL;
158         AVVDPAUDeviceContext *dev_ctx;
159
160         // We assume the hw_frames_ctx always survives until ff_vdpau_common_uninit
161         // is called. This holds true as the user is not allowed to touch
162         // hw_device_ctx, or hw_frames_ctx after get_format (and ff_get_format
163         // itself also uninits before unreffing hw_frames_ctx).
164         if (avctx->hw_frames_ctx) {
165             frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
166         } else if (avctx->hw_device_ctx) {
167             int ret;
168
169             avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
170             if (!avctx->hw_frames_ctx)
171                 return AVERROR(ENOMEM);
172
173             frames_ctx            = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
174             frames_ctx->format    = AV_PIX_FMT_VDPAU;
175             frames_ctx->sw_format = avctx->sw_pix_fmt;
176             frames_ctx->width     = avctx->coded_width;
177             frames_ctx->height    = avctx->coded_height;
178
179             ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
180             if (ret < 0) {
181                 av_buffer_unref(&avctx->hw_frames_ctx);
182                 return ret;
183             }
184         }
185
186         if (!frames_ctx) {
187             av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
188                    "required for VDPAU decoding.\n");
189             return AVERROR(EINVAL);
190         }
191
192         dev_ctx = frames_ctx->device_ctx->hwctx;
193
194         vdctx->device           = dev_ctx->device;
195         vdctx->get_proc_address = dev_ctx->get_proc_address;
196
197         if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
198             level = 0;
199     }
200
201     if (level < 0)
202         return AVERROR(ENOTSUP);
203
204     status = vdctx->get_proc_address(vdctx->device,
205                                      VDP_FUNC_ID_GET_INFORMATION_STRING,
206                                      &func);
207     if (status != VDP_STATUS_OK)
208         return vdpau_error(status);
209     else
210         info = func;
211
212     status = info(&info_string);
213     if (status != VDP_STATUS_OK)
214         return vdpau_error(status);
215     if (avctx->codec_id == AV_CODEC_ID_HEVC && strncmp(info_string, "NVIDIA ", 7) == 0 &&
216         !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
217         av_log(avctx, AV_LOG_VERBOSE, "HEVC with NVIDIA VDPAU drivers is buggy, skipping.\n");
218         return AVERROR(ENOTSUP);
219     }
220
221     status = vdctx->get_proc_address(vdctx->device,
222                                      VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
223                                      &func);
224     if (status != VDP_STATUS_OK)
225         return vdpau_error(status);
226     else
227         surface_query_caps = func;
228
229     status = surface_query_caps(vdctx->device, type, &supported,
230                                 &max_width, &max_height);
231     if (status != VDP_STATUS_OK)
232         return vdpau_error(status);
233     if (supported != VDP_TRUE ||
234         max_width < width || max_height < height)
235         return AVERROR(ENOTSUP);
236
237     status = vdctx->get_proc_address(vdctx->device,
238                                      VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
239                                      &func);
240     if (status != VDP_STATUS_OK)
241         return vdpau_error(status);
242     else
243         decoder_query_caps = func;
244
245     status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
246                                 &max_mb, &max_width, &max_height);
247 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
248     if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
249         profile = VDP_DECODER_PROFILE_H264_MAIN;
250         status = decoder_query_caps(vdctx->device, profile, &supported,
251                                     &max_level, &max_mb,
252                                     &max_width, &max_height);
253     }
254 #endif
255     if (status != VDP_STATUS_OK)
256         return vdpau_error(status);
257
258     if (supported != VDP_TRUE || max_level < level ||
259         max_width < width || max_height < height)
260         return AVERROR(ENOTSUP);
261
262     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
263                                      &func);
264     if (status != VDP_STATUS_OK)
265         return vdpau_error(status);
266     else
267         create = func;
268
269     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
270                                      &func);
271     if (status != VDP_STATUS_OK)
272         return vdpau_error(status);
273     else
274         vdctx->render = func;
275
276     status = create(vdctx->device, profile, width, height, avctx->refs,
277                     &vdctx->decoder);
278     if (status == VDP_STATUS_OK) {
279         vdctx->width  = avctx->coded_width;
280         vdctx->height = avctx->coded_height;
281     }
282
283     return vdpau_error(status);
284 }
285
286 int ff_vdpau_common_uninit(AVCodecContext *avctx)
287 {
288     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
289     VdpDecoderDestroy *destroy;
290     void *func;
291     VdpStatus status;
292
293     if (vdctx->device == VDP_INVALID_HANDLE)
294         return 0; /* Decoder created and destroyed by user */
295     if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
296         return 0;
297
298     status = vdctx->get_proc_address(vdctx->device,
299                                      VDP_FUNC_ID_DECODER_DESTROY, &func);
300     if (status != VDP_STATUS_OK)
301         return vdpau_error(status);
302     else
303         destroy = func;
304
305     status = destroy(vdctx->decoder);
306     return vdpau_error(status);
307 }
308
309 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
310 {
311     VDPAUHWContext *hwctx = avctx->hwaccel_context;
312     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
313
314     if (vdctx->device == VDP_INVALID_HANDLE)
315         return 0; /* Decoder created by user */
316     if (avctx->coded_width == vdctx->width &&
317         avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
318         return 0;
319
320     avctx->hwaccel->uninit(avctx);
321     return avctx->hwaccel->init(avctx);
322 }
323
324 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
325                                 av_unused const uint8_t *buffer,
326                                 av_unused uint32_t size)
327 {
328     pic_ctx->bitstream_buffers_allocated = 0;
329     pic_ctx->bitstream_buffers_used      = 0;
330     pic_ctx->bitstream_buffers           = NULL;
331     return 0;
332 }
333
334 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
335                               struct vdpau_picture_context *pic_ctx)
336 {
337     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
338     AVVDPAUContext *hwctx = avctx->hwaccel_context;
339     VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
340     VdpStatus status;
341     int val;
342
343     val = ff_vdpau_common_reinit(avctx);
344     if (val < 0)
345         return val;
346
347     if (hwctx && !hwctx->render && hwctx->render2) {
348         status = hwctx->render2(avctx, frame, (void *)&pic_ctx->info,
349                                 pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
350     } else
351     status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
352                            pic_ctx->bitstream_buffers_used,
353                            pic_ctx->bitstream_buffers);
354
355     av_freep(&pic_ctx->bitstream_buffers);
356
357     return vdpau_error(status);
358 }
359
360 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
361     CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
362     CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
363 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
364 {
365     MpegEncContext *s = avctx->priv_data;
366     Picture *pic = s->current_picture_ptr;
367     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
368     int val;
369
370     val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
371     if (val < 0)
372         return val;
373
374     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
375     return 0;
376 }
377 #endif
378
379 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
380                         const uint8_t *buf, uint32_t size)
381 {
382     VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
383
384     buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
385                               (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
386     if (!buffers)
387         return AVERROR(ENOMEM);
388
389     pic_ctx->bitstream_buffers = buffers;
390     buffers += pic_ctx->bitstream_buffers_used++;
391
392     buffers->struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
393     buffers->bitstream       = buf;
394     buffers->bitstream_bytes = size;
395     return 0;
396 }
397
398 #if FF_API_VDPAU_PROFILE
399 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
400 {
401 #define PROFILE(prof)                      \
402 do {                                       \
403     *profile = VDP_DECODER_PROFILE_##prof; \
404     return 0;                              \
405 } while (0)
406
407     switch (avctx->codec_id) {
408     case AV_CODEC_ID_MPEG1VIDEO:               PROFILE(MPEG1);
409     case AV_CODEC_ID_MPEG2VIDEO:
410         switch (avctx->profile) {
411         case FF_PROFILE_MPEG2_MAIN:            PROFILE(MPEG2_MAIN);
412         case FF_PROFILE_MPEG2_SIMPLE:          PROFILE(MPEG2_SIMPLE);
413         default:                               return AVERROR(EINVAL);
414         }
415     case AV_CODEC_ID_H263:                     PROFILE(MPEG4_PART2_ASP);
416     case AV_CODEC_ID_MPEG4:
417         switch (avctx->profile) {
418         case FF_PROFILE_MPEG4_SIMPLE:          PROFILE(MPEG4_PART2_SP);
419         case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
420         default:                               return AVERROR(EINVAL);
421         }
422     case AV_CODEC_ID_H264:
423         switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
424         case FF_PROFILE_H264_BASELINE:         PROFILE(H264_BASELINE);
425         case FF_PROFILE_H264_CONSTRAINED_BASELINE:
426         case FF_PROFILE_H264_MAIN:             PROFILE(H264_MAIN);
427         case FF_PROFILE_H264_HIGH:             PROFILE(H264_HIGH);
428 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
429         case FF_PROFILE_H264_EXTENDED:         PROFILE(H264_EXTENDED);
430 #endif
431         default:                               return AVERROR(EINVAL);
432         }
433     case AV_CODEC_ID_WMV3:
434     case AV_CODEC_ID_VC1:
435         switch (avctx->profile) {
436         case FF_PROFILE_VC1_SIMPLE:            PROFILE(VC1_SIMPLE);
437         case FF_PROFILE_VC1_MAIN:              PROFILE(VC1_MAIN);
438         case FF_PROFILE_VC1_ADVANCED:          PROFILE(VC1_ADVANCED);
439         default:                               return AVERROR(EINVAL);
440         }
441     }
442     return AVERROR(EINVAL);
443 #undef PROFILE
444 }
445 #endif /* FF_API_VDPAU_PROFILE */
446
447 AVVDPAUContext *av_vdpau_alloc_context(void)
448 {
449     return av_mallocz(sizeof(VDPAUHWContext));
450 }
451
452 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
453                           VdpGetProcAddress *get_proc, unsigned flags)
454 {
455     VDPAUHWContext *hwctx;
456
457     if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
458         return AVERROR(EINVAL);
459
460     if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
461         return AVERROR(ENOMEM);
462
463     hwctx = avctx->hwaccel_context;
464
465     memset(hwctx, 0, sizeof(*hwctx));
466     hwctx->context.decoder  = VDP_INVALID_HANDLE;
467     hwctx->device           = device;
468     hwctx->get_proc_address = get_proc;
469     hwctx->flags            = flags;
470     hwctx->reset            = 1;
471     return 0;
472 }
473
474 /* @}*/