]> git.sesse.net Git - ffmpeg/blob - libavcodec/vdpau.c
hevc: change the stride of the MC buffer to be in bytes instead of elements
[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 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 || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
177         profile = VDP_DECODER_PROFILE_H264_MAIN;
178         status = decoder_query_caps(vdctx->device, profile, &supported,
179                                     &max_level, &max_mb,
180                                     &max_width, &max_height);
181     }
182 #endif
183     if (status != VDP_STATUS_OK)
184         return vdpau_error(status);
185
186     if (supported != VDP_TRUE || max_level < level ||
187         max_width < width || max_height < height)
188         return AVERROR(ENOTSUP);
189
190     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
191                                      &func);
192     if (status != VDP_STATUS_OK)
193         return vdpau_error(status);
194     else
195         create = func;
196
197     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
198                                      &func);
199     if (status != VDP_STATUS_OK)
200         return vdpau_error(status);
201     else
202         vdctx->render = func;
203
204     status = create(vdctx->device, profile, width, height, avctx->refs,
205                     &vdctx->decoder);
206     if (status == VDP_STATUS_OK) {
207         vdctx->width  = avctx->coded_width;
208         vdctx->height = avctx->coded_height;
209     }
210
211     return vdpau_error(status);
212 }
213
214 int ff_vdpau_common_uninit(AVCodecContext *avctx)
215 {
216     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
217     VdpDecoderDestroy *destroy;
218     void *func;
219     VdpStatus status;
220
221     if (vdctx->device == VDP_INVALID_HANDLE)
222         return 0; /* Decoder created and destroyed by user */
223     if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
224         return 0;
225
226     status = vdctx->get_proc_address(vdctx->device,
227                                      VDP_FUNC_ID_DECODER_DESTROY, &func);
228     if (status != VDP_STATUS_OK)
229         return vdpau_error(status);
230     else
231         destroy = func;
232
233     status = destroy(vdctx->decoder);
234     return vdpau_error(status);
235 }
236
237 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
238 {
239     VDPAUHWContext *hwctx = avctx->hwaccel_context;
240     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
241
242     if (vdctx->device == VDP_INVALID_HANDLE)
243         return 0; /* Decoder created by user */
244     if (avctx->coded_width == vdctx->width &&
245         avctx->coded_height == vdctx->height && !hwctx->reset)
246         return 0;
247
248     avctx->hwaccel->uninit(avctx);
249     return avctx->hwaccel->init(avctx);
250 }
251
252 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
253                                 av_unused const uint8_t *buffer,
254                                 av_unused uint32_t size)
255 {
256     pic_ctx->bitstream_buffers_allocated = 0;
257     pic_ctx->bitstream_buffers_used      = 0;
258     pic_ctx->bitstream_buffers           = NULL;
259     return 0;
260 }
261
262 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
263                               struct vdpau_picture_context *pic_ctx)
264 {
265     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
266     VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
267     VdpStatus status;
268     int val;
269
270     val = ff_vdpau_common_reinit(avctx);
271     if (val < 0)
272         return val;
273
274     status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
275                            pic_ctx->bitstream_buffers_used,
276                            pic_ctx->bitstream_buffers);
277
278     av_freep(&pic_ctx->bitstream_buffers);
279     return vdpau_error(status);
280 }
281
282 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
283     CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
284     CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
285 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
286 {
287     MpegEncContext *s = avctx->priv_data;
288     Picture *pic = s->current_picture_ptr;
289     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
290     int val;
291
292     val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
293     if (val < 0)
294         return val;
295
296     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
297     return 0;
298 }
299 #endif
300
301 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
302                         const uint8_t *buf, uint32_t size)
303 {
304     VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
305
306     buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
307                               (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
308     if (!buffers)
309         return AVERROR(ENOMEM);
310
311     pic_ctx->bitstream_buffers = buffers;
312     buffers += pic_ctx->bitstream_buffers_used++;
313
314     buffers->struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
315     buffers->bitstream       = buf;
316     buffers->bitstream_bytes = size;
317     return 0;
318 }
319
320 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
321 {
322 #define PROFILE(prof)                      \
323 do {                                       \
324     *profile = VDP_DECODER_PROFILE_##prof; \
325     return 0;                              \
326 } while (0)
327
328     switch (avctx->codec_id) {
329     case AV_CODEC_ID_MPEG1VIDEO:               PROFILE(MPEG1);
330     case AV_CODEC_ID_MPEG2VIDEO:
331         switch (avctx->profile) {
332         case FF_PROFILE_MPEG2_MAIN:            PROFILE(MPEG2_MAIN);
333         case FF_PROFILE_MPEG2_SIMPLE:          PROFILE(MPEG2_SIMPLE);
334         default:                               return AVERROR(EINVAL);
335         }
336     case AV_CODEC_ID_H263:                     PROFILE(MPEG4_PART2_ASP);
337     case AV_CODEC_ID_MPEG4:
338         switch (avctx->profile) {
339         case FF_PROFILE_MPEG4_SIMPLE:          PROFILE(MPEG4_PART2_SP);
340         case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
341         default:                               return AVERROR(EINVAL);
342         }
343     case AV_CODEC_ID_H264:
344         switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
345         case FF_PROFILE_H264_BASELINE:         PROFILE(H264_BASELINE);
346         case FF_PROFILE_H264_CONSTRAINED_BASELINE:
347         case FF_PROFILE_H264_MAIN:             PROFILE(H264_MAIN);
348         case FF_PROFILE_H264_HIGH:             PROFILE(H264_HIGH);
349 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
350         case FF_PROFILE_H264_EXTENDED:         PROFILE(H264_EXTENDED);
351 #endif
352         default:                               return AVERROR(EINVAL);
353         }
354     case AV_CODEC_ID_WMV3:
355     case AV_CODEC_ID_VC1:
356         switch (avctx->profile) {
357         case FF_PROFILE_VC1_SIMPLE:            PROFILE(VC1_SIMPLE);
358         case FF_PROFILE_VC1_MAIN:              PROFILE(VC1_MAIN);
359         case FF_PROFILE_VC1_ADVANCED:          PROFILE(VC1_ADVANCED);
360         default:                               return AVERROR(EINVAL);
361         }
362     }
363     return AVERROR(EINVAL);
364 #undef PROFILE
365 }
366
367 AVVDPAUContext *av_vdpau_alloc_context(void)
368 {
369     return av_mallocz(sizeof(AVVDPAUContext));
370 }
371
372 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
373                           VdpGetProcAddress *get_proc, unsigned flags)
374 {
375     VDPAUHWContext *hwctx;
376
377     if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
378         return AVERROR(EINVAL);
379
380     if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
381         return AVERROR(ENOMEM);
382
383     hwctx = avctx->hwaccel_context;
384
385     memset(hwctx, 0, sizeof(*hwctx));
386     hwctx->context.decoder  = VDP_INVALID_HANDLE;
387     hwctx->device           = device;
388     hwctx->get_proc_address = get_proc;
389     hwctx->flags            = flags;
390     hwctx->reset            = 1;
391     return 0;
392 }
393
394 /* @}*/