Merge commit '5c1585c4c3b5281835d784c5daef0069915ccd57'
[ffmpeg.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 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_compat.h"
32 #include "vdpau_internal.h"
33
34 // XXX: at the time of adding this ifdefery, av_assert* wasn't use outside.
35 // When dropping it, make sure other av_assert* were not added since then.
36
37 #if FF_API_VDPAU
38 #undef NDEBUG
39 #include <assert.h>
40 #endif
41
42 /**
43  * @addtogroup VDPAU_Decoding
44  *
45  * @{
46  */
47
48 static int vdpau_error(VdpStatus status)
49 {
50     switch (status) {
51     case VDP_STATUS_OK:
52         return 0;
53     case VDP_STATUS_NO_IMPLEMENTATION:
54         return AVERROR(ENOSYS);
55     case VDP_STATUS_DISPLAY_PREEMPTED:
56         return AVERROR(EIO);
57     case VDP_STATUS_INVALID_HANDLE:
58         return AVERROR(EBADF);
59     case VDP_STATUS_INVALID_POINTER:
60         return AVERROR(EFAULT);
61     case VDP_STATUS_RESOURCES:
62         return AVERROR(ENOBUFS);
63     case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
64         return AVERROR(EXDEV);
65     case VDP_STATUS_ERROR:
66         return AVERROR(EIO);
67     default:
68         return AVERROR(EINVAL);
69     }
70 }
71
72 AVVDPAUContext *av_alloc_vdpaucontext(void)
73 {
74     return av_vdpau_alloc_context();
75 }
76
77 MAKE_ACCESSORS(AVVDPAUContext, vdpau_hwaccel, AVVDPAU_Render2, render2)
78
79 int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
80                                     VdpChromaType *type,
81                                     uint32_t *width, uint32_t *height)
82 {
83     VdpChromaType t;
84     uint32_t w = avctx->coded_width;
85     uint32_t h = avctx->coded_height;
86
87     /* See <vdpau/vdpau.h> for per-type alignment constraints. */
88     switch (avctx->sw_pix_fmt) {
89     case AV_PIX_FMT_YUV420P:
90     case AV_PIX_FMT_YUVJ420P:
91         t = VDP_CHROMA_TYPE_420;
92         w = (w + 1) & ~1;
93         h = (h + 3) & ~3;
94         break;
95     case AV_PIX_FMT_YUV422P:
96     case AV_PIX_FMT_YUVJ422P:
97         t = VDP_CHROMA_TYPE_422;
98         w = (w + 1) & ~1;
99         h = (h + 1) & ~1;
100         break;
101     case AV_PIX_FMT_YUV444P:
102     case AV_PIX_FMT_YUVJ444P:
103         t = VDP_CHROMA_TYPE_444;
104         h = (h + 1) & ~1;
105         break;
106     default:
107         return AVERROR(ENOSYS);
108     }
109
110     if (type)
111         *type = t;
112     if (width)
113         *width = w;
114     if (height)
115         *height = h;
116     return 0;
117 }
118
119 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
120                          int level)
121 {
122     VDPAUHWContext *hwctx = avctx->hwaccel_context;
123     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
124     VdpVideoSurfaceQueryCapabilities *surface_query_caps;
125     VdpDecoderQueryCapabilities *decoder_query_caps;
126     VdpDecoderCreate *create;
127     VdpGetInformationString *info;
128     const char *info_string;
129     void *func;
130     VdpStatus status;
131     VdpBool supported;
132     uint32_t max_level, max_mb, max_width, max_height;
133     VdpChromaType type;
134     uint32_t width;
135     uint32_t height;
136
137     vdctx->width            = UINT32_MAX;
138     vdctx->height           = UINT32_MAX;
139
140     if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
141         return AVERROR(ENOSYS);
142
143     if (hwctx) {
144         hwctx->reset            = 0;
145
146         if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
147             vdctx->decoder = hwctx->context.decoder;
148             vdctx->render  = hwctx->context.render;
149             vdctx->device  = VDP_INVALID_HANDLE;
150             return 0; /* Decoder created by user */
151         }
152
153         vdctx->device           = hwctx->device;
154         vdctx->get_proc_address = hwctx->get_proc_address;
155
156         if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
157             level = 0;
158
159         if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
160             type != VDP_CHROMA_TYPE_420)
161             return AVERROR(ENOSYS);
162     } else {
163         AVHWFramesContext *frames_ctx = NULL;
164         AVVDPAUDeviceContext *dev_ctx;
165
166         // We assume the hw_frames_ctx always survives until ff_vdpau_common_uninit
167         // is called. This holds true as the user is not allowed to touch
168         // hw_device_ctx, or hw_frames_ctx after get_format (and ff_get_format
169         // itself also uninits before unreffing hw_frames_ctx).
170         if (avctx->hw_frames_ctx) {
171             frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
172         } else if (avctx->hw_device_ctx) {
173             int ret;
174
175             avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
176             if (!avctx->hw_frames_ctx)
177                 return AVERROR(ENOMEM);
178
179             frames_ctx            = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
180             frames_ctx->format    = AV_PIX_FMT_VDPAU;
181             frames_ctx->sw_format = avctx->sw_pix_fmt;
182             frames_ctx->width     = avctx->coded_width;
183             frames_ctx->height    = avctx->coded_height;
184
185             ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
186             if (ret < 0) {
187                 av_buffer_unref(&avctx->hw_frames_ctx);
188                 return ret;
189             }
190         }
191
192         if (!frames_ctx) {
193             av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
194                    "required for VDPAU decoding.\n");
195             return AVERROR(EINVAL);
196         }
197
198         dev_ctx = frames_ctx->device_ctx->hwctx;
199
200         vdctx->device           = dev_ctx->device;
201         vdctx->get_proc_address = dev_ctx->get_proc_address;
202
203         if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
204             level = 0;
205     }
206
207     if (level < 0)
208         return AVERROR(ENOTSUP);
209
210     status = vdctx->get_proc_address(vdctx->device,
211                                      VDP_FUNC_ID_GET_INFORMATION_STRING,
212                                      &func);
213     if (status != VDP_STATUS_OK)
214         return vdpau_error(status);
215     else
216         info = func;
217
218     status = info(&info_string);
219     if (status != VDP_STATUS_OK)
220         return vdpau_error(status);
221     if (avctx->codec_id == AV_CODEC_ID_HEVC && strncmp(info_string, "NVIDIA ", 7) == 0 &&
222         !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
223         av_log(avctx, AV_LOG_VERBOSE, "HEVC with NVIDIA VDPAU drivers is buggy, skipping.\n");
224         return AVERROR(ENOTSUP);
225     }
226
227     status = vdctx->get_proc_address(vdctx->device,
228                                      VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
229                                      &func);
230     if (status != VDP_STATUS_OK)
231         return vdpau_error(status);
232     else
233         surface_query_caps = func;
234
235     status = surface_query_caps(vdctx->device, type, &supported,
236                                 &max_width, &max_height);
237     if (status != VDP_STATUS_OK)
238         return vdpau_error(status);
239     if (supported != VDP_TRUE ||
240         max_width < width || max_height < height)
241         return AVERROR(ENOTSUP);
242
243     status = vdctx->get_proc_address(vdctx->device,
244                                      VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
245                                      &func);
246     if (status != VDP_STATUS_OK)
247         return vdpau_error(status);
248     else
249         decoder_query_caps = func;
250
251     status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
252                                 &max_mb, &max_width, &max_height);
253 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
254     if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
255         profile = VDP_DECODER_PROFILE_H264_MAIN;
256         status = decoder_query_caps(vdctx->device, profile, &supported,
257                                     &max_level, &max_mb,
258                                     &max_width, &max_height);
259     }
260 #endif
261     if (status != VDP_STATUS_OK)
262         return vdpau_error(status);
263
264     if (supported != VDP_TRUE || max_level < level ||
265         max_width < width || max_height < height)
266         return AVERROR(ENOTSUP);
267
268     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
269                                      &func);
270     if (status != VDP_STATUS_OK)
271         return vdpau_error(status);
272     else
273         create = func;
274
275     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
276                                      &func);
277     if (status != VDP_STATUS_OK)
278         return vdpau_error(status);
279     else
280         vdctx->render = func;
281
282     status = create(vdctx->device, profile, width, height, avctx->refs,
283                     &vdctx->decoder);
284     if (status == VDP_STATUS_OK) {
285         vdctx->width  = avctx->coded_width;
286         vdctx->height = avctx->coded_height;
287     }
288
289     return vdpau_error(status);
290 }
291
292 int ff_vdpau_common_uninit(AVCodecContext *avctx)
293 {
294     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
295     VdpDecoderDestroy *destroy;
296     void *func;
297     VdpStatus status;
298
299     if (vdctx->device == VDP_INVALID_HANDLE)
300         return 0; /* Decoder created and destroyed by user */
301     if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
302         return 0;
303
304     status = vdctx->get_proc_address(vdctx->device,
305                                      VDP_FUNC_ID_DECODER_DESTROY, &func);
306     if (status != VDP_STATUS_OK)
307         return vdpau_error(status);
308     else
309         destroy = func;
310
311     status = destroy(vdctx->decoder);
312     return vdpau_error(status);
313 }
314
315 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
316 {
317     VDPAUHWContext *hwctx = avctx->hwaccel_context;
318     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
319
320     if (vdctx->device == VDP_INVALID_HANDLE)
321         return 0; /* Decoder created by user */
322     if (avctx->coded_width == vdctx->width &&
323         avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
324         return 0;
325
326     avctx->hwaccel->uninit(avctx);
327     return avctx->hwaccel->init(avctx);
328 }
329
330 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
331                                 av_unused const uint8_t *buffer,
332                                 av_unused uint32_t size)
333 {
334     pic_ctx->bitstream_buffers_allocated = 0;
335     pic_ctx->bitstream_buffers_used      = 0;
336     pic_ctx->bitstream_buffers           = NULL;
337     return 0;
338 }
339
340 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
341                               struct vdpau_picture_context *pic_ctx)
342 {
343     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
344     AVVDPAUContext *hwctx = avctx->hwaccel_context;
345     VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
346     VdpStatus status;
347     int val;
348
349     val = ff_vdpau_common_reinit(avctx);
350     if (val < 0)
351         return val;
352
353     if (hwctx && !hwctx->render && hwctx->render2) {
354         status = hwctx->render2(avctx, frame, (void *)&pic_ctx->info,
355                                 pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
356     } else
357     status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
358                            pic_ctx->bitstream_buffers_used,
359                            pic_ctx->bitstream_buffers);
360
361     av_freep(&pic_ctx->bitstream_buffers);
362
363     return vdpau_error(status);
364 }
365
366 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
367     CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
368     CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
369 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
370 {
371     MpegEncContext *s = avctx->priv_data;
372     Picture *pic = s->current_picture_ptr;
373     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
374     int val;
375
376     val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
377     if (val < 0)
378         return val;
379
380     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
381     return 0;
382 }
383 #endif
384
385 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
386                         const uint8_t *buf, uint32_t size)
387 {
388     VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
389
390     buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
391                               (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
392     if (!buffers)
393         return AVERROR(ENOMEM);
394
395     pic_ctx->bitstream_buffers = buffers;
396     buffers += pic_ctx->bitstream_buffers_used++;
397
398     buffers->struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
399     buffers->bitstream       = buf;
400     buffers->bitstream_bytes = size;
401     return 0;
402 }
403
404 /* Obsolete non-hwaccel VDPAU support below... */
405
406 #if FF_API_VDPAU
407 void ff_vdpau_add_data_chunk(uint8_t *data, const uint8_t *buf, int buf_size)
408 {
409     struct vdpau_render_state *render = (struct vdpau_render_state*)data;
410     assert(render);
411
412     render->bitstream_buffers= av_fast_realloc(
413         render->bitstream_buffers,
414         &render->bitstream_buffers_allocated,
415         sizeof(*render->bitstream_buffers)*(render->bitstream_buffers_used + 1)
416     );
417
418     render->bitstream_buffers[render->bitstream_buffers_used].struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
419     render->bitstream_buffers[render->bitstream_buffers_used].bitstream       = buf;
420     render->bitstream_buffers[render->bitstream_buffers_used].bitstream_bytes = buf_size;
421     render->bitstream_buffers_used++;
422 }
423
424 #if CONFIG_H264_VDPAU_DECODER
425 void ff_vdpau_h264_set_reference_frames(H264Context *h)
426 {
427     struct vdpau_render_state *render, *render_ref;
428     VdpReferenceFrameH264 *rf, *rf2;
429     H264Picture *pic;
430     int i, list, pic_frame_idx;
431
432     render = (struct vdpau_render_state *)h->cur_pic_ptr->f->data[0];
433     assert(render);
434
435     rf = &render->info.h264.referenceFrames[0];
436 #define H264_RF_COUNT FF_ARRAY_ELEMS(render->info.h264.referenceFrames)
437
438     for (list = 0; list < 2; ++list) {
439         H264Picture **lp = list ? h->long_ref : h->short_ref;
440         int ls = list ? 16 : h->short_ref_count;
441
442         for (i = 0; i < ls; ++i) {
443             pic = lp[i];
444             if (!pic || !pic->reference)
445                 continue;
446             pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
447
448             render_ref = (struct vdpau_render_state *)pic->f->data[0];
449             assert(render_ref);
450
451             rf2 = &render->info.h264.referenceFrames[0];
452             while (rf2 != rf) {
453                 if (
454                     (rf2->surface == render_ref->surface)
455                     && (rf2->is_long_term == pic->long_ref)
456                     && (rf2->frame_idx == pic_frame_idx)
457                 )
458                     break;
459                 ++rf2;
460             }
461             if (rf2 != rf) {
462                 rf2->top_is_reference    |= (pic->reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
463                 rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
464                 continue;
465             }
466
467             if (rf >= &render->info.h264.referenceFrames[H264_RF_COUNT])
468                 continue;
469
470             rf->surface             = render_ref->surface;
471             rf->is_long_term        = pic->long_ref;
472             rf->top_is_reference    = (pic->reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
473             rf->bottom_is_reference = (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
474             rf->field_order_cnt[0]  = pic->field_poc[0];
475             rf->field_order_cnt[1]  = pic->field_poc[1];
476             rf->frame_idx           = pic_frame_idx;
477
478             ++rf;
479         }
480     }
481
482     for (; rf < &render->info.h264.referenceFrames[H264_RF_COUNT]; ++rf) {
483         rf->surface             = VDP_INVALID_HANDLE;
484         rf->is_long_term        = 0;
485         rf->top_is_reference    = 0;
486         rf->bottom_is_reference = 0;
487         rf->field_order_cnt[0]  = 0;
488         rf->field_order_cnt[1]  = 0;
489         rf->frame_idx           = 0;
490     }
491 }
492
493 void ff_vdpau_h264_picture_start(H264Context *h)
494 {
495     struct vdpau_render_state *render;
496     int i;
497
498     render = (struct vdpau_render_state *)h->cur_pic_ptr->f->data[0];
499     assert(render);
500
501     for (i = 0; i < 2; ++i) {
502         int foc = h->cur_pic_ptr->field_poc[i];
503         if (foc == INT_MAX)
504             foc = 0;
505         render->info.h264.field_order_cnt[i] = foc;
506     }
507
508     render->info.h264.frame_num = h->poc.frame_num;
509 }
510
511 void ff_vdpau_h264_picture_complete(H264Context *h)
512 {
513     struct vdpau_render_state *render;
514
515     render = (struct vdpau_render_state *)h->cur_pic_ptr->f->data[0];
516     assert(render);
517
518     render->info.h264.slice_count = h->current_slice;
519     if (render->info.h264.slice_count < 1)
520         return;
521
522     render->info.h264.is_reference                           = (h->cur_pic_ptr->reference & 3) ? VDP_TRUE : VDP_FALSE;
523     render->info.h264.field_pic_flag                         = h->picture_structure != PICT_FRAME;
524     render->info.h264.bottom_field_flag                      = h->picture_structure == PICT_BOTTOM_FIELD;
525     render->info.h264.num_ref_frames                         = h->ps.sps->ref_frame_count;
526     render->info.h264.mb_adaptive_frame_field_flag           = h->ps.sps->mb_aff && !render->info.h264.field_pic_flag;
527     render->info.h264.constrained_intra_pred_flag            = h->ps.pps->constrained_intra_pred;
528     render->info.h264.weighted_pred_flag                     = h->ps.pps->weighted_pred;
529     render->info.h264.weighted_bipred_idc                    = h->ps.pps->weighted_bipred_idc;
530     render->info.h264.frame_mbs_only_flag                    = h->ps.sps->frame_mbs_only_flag;
531     render->info.h264.transform_8x8_mode_flag                = h->ps.pps->transform_8x8_mode;
532     render->info.h264.chroma_qp_index_offset                 = h->ps.pps->chroma_qp_index_offset[0];
533     render->info.h264.second_chroma_qp_index_offset          = h->ps.pps->chroma_qp_index_offset[1];
534     render->info.h264.pic_init_qp_minus26                    = h->ps.pps->init_qp - 26;
535     render->info.h264.num_ref_idx_l0_active_minus1           = h->ps.pps->ref_count[0] - 1;
536     render->info.h264.num_ref_idx_l1_active_minus1           = h->ps.pps->ref_count[1] - 1;
537     render->info.h264.log2_max_frame_num_minus4              = h->ps.sps->log2_max_frame_num - 4;
538     render->info.h264.pic_order_cnt_type                     = h->ps.sps->poc_type;
539     render->info.h264.log2_max_pic_order_cnt_lsb_minus4      = h->ps.sps->poc_type ? 0 : h->ps.sps->log2_max_poc_lsb - 4;
540     render->info.h264.delta_pic_order_always_zero_flag       = h->ps.sps->delta_pic_order_always_zero_flag;
541     render->info.h264.direct_8x8_inference_flag              = h->ps.sps->direct_8x8_inference_flag;
542     render->info.h264.entropy_coding_mode_flag               = h->ps.pps->cabac;
543     render->info.h264.pic_order_present_flag                 = h->ps.pps->pic_order_present;
544     render->info.h264.deblocking_filter_control_present_flag = h->ps.pps->deblocking_filter_parameters_present;
545     render->info.h264.redundant_pic_cnt_present_flag         = h->ps.pps->redundant_pic_cnt_present;
546     memcpy(render->info.h264.scaling_lists_4x4, h->ps.pps->scaling_matrix4, sizeof(render->info.h264.scaling_lists_4x4));
547     memcpy(render->info.h264.scaling_lists_8x8[0], h->ps.pps->scaling_matrix8[0], sizeof(render->info.h264.scaling_lists_8x8[0]));
548     memcpy(render->info.h264.scaling_lists_8x8[1], h->ps.pps->scaling_matrix8[3], sizeof(render->info.h264.scaling_lists_8x8[0]));
549
550     ff_h264_draw_horiz_band(h, &h->slice_ctx[0], 0, h->avctx->height);
551     render->bitstream_buffers_used = 0;
552 }
553 #endif /* CONFIG_H264_VDPAU_DECODER */
554
555 #if CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER
556 void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf,
557                                     int buf_size, int slice_count)
558 {
559     struct vdpau_render_state *render, *last, *next;
560     int i;
561
562     if (!s->current_picture_ptr) return;
563
564     render = (struct vdpau_render_state *)s->current_picture_ptr->f->data[0];
565     assert(render);
566
567     /* fill VdpPictureInfoMPEG1Or2 struct */
568     render->info.mpeg.picture_structure          = s->picture_structure;
569     render->info.mpeg.picture_coding_type        = s->pict_type;
570     render->info.mpeg.intra_dc_precision         = s->intra_dc_precision;
571     render->info.mpeg.frame_pred_frame_dct       = s->frame_pred_frame_dct;
572     render->info.mpeg.concealment_motion_vectors = s->concealment_motion_vectors;
573     render->info.mpeg.intra_vlc_format           = s->intra_vlc_format;
574     render->info.mpeg.alternate_scan             = s->alternate_scan;
575     render->info.mpeg.q_scale_type               = s->q_scale_type;
576     render->info.mpeg.top_field_first            = s->top_field_first;
577     render->info.mpeg.full_pel_forward_vector    = s->full_pel[0]; // MPEG-1 only.  Set 0 for MPEG-2
578     render->info.mpeg.full_pel_backward_vector   = s->full_pel[1]; // MPEG-1 only.  Set 0 for MPEG-2
579     render->info.mpeg.f_code[0][0]               = s->mpeg_f_code[0][0]; // For MPEG-1 fill both horiz. & vert.
580     render->info.mpeg.f_code[0][1]               = s->mpeg_f_code[0][1];
581     render->info.mpeg.f_code[1][0]               = s->mpeg_f_code[1][0];
582     render->info.mpeg.f_code[1][1]               = s->mpeg_f_code[1][1];
583     for (i = 0; i < 64; ++i) {
584         render->info.mpeg.intra_quantizer_matrix[i]     = s->intra_matrix[i];
585         render->info.mpeg.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
586     }
587
588     render->info.mpeg.forward_reference          = VDP_INVALID_HANDLE;
589     render->info.mpeg.backward_reference         = VDP_INVALID_HANDLE;
590
591     switch(s->pict_type){
592     case  AV_PICTURE_TYPE_B:
593         next = (struct vdpau_render_state *)s->next_picture.f->data[0];
594         assert(next);
595         render->info.mpeg.backward_reference     = next->surface;
596         // no return here, going to set forward prediction
597     case  AV_PICTURE_TYPE_P:
598         last = (struct vdpau_render_state *)s->last_picture.f->data[0];
599         if (!last) // FIXME: Does this test make sense?
600             last = render; // predict second field from the first
601         render->info.mpeg.forward_reference      = last->surface;
602     }
603
604     ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
605
606     render->info.mpeg.slice_count                = slice_count;
607
608     if (slice_count)
609         ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
610     render->bitstream_buffers_used               = 0;
611 }
612 #endif /* CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER */
613
614 #if CONFIG_VC1_VDPAU_DECODER
615 void ff_vdpau_vc1_decode_picture(MpegEncContext *s, const uint8_t *buf,
616                                  int buf_size)
617 {
618     VC1Context *v = s->avctx->priv_data;
619     struct vdpau_render_state *render, *last, *next;
620
621     render = (struct vdpau_render_state *)s->current_picture.f->data[0];
622     assert(render);
623
624     /*  fill LvPictureInfoVC1 struct */
625     render->info.vc1.frame_coding_mode  = v->fcm ? v->fcm + 1 : 0;
626     render->info.vc1.postprocflag       = v->postprocflag;
627     render->info.vc1.pulldown           = v->broadcast;
628     render->info.vc1.interlace          = v->interlace;
629     render->info.vc1.tfcntrflag         = v->tfcntrflag;
630     render->info.vc1.finterpflag        = v->finterpflag;
631     render->info.vc1.psf                = v->psf;
632     render->info.vc1.dquant             = v->dquant;
633     render->info.vc1.panscan_flag       = v->panscanflag;
634     render->info.vc1.refdist_flag       = v->refdist_flag;
635     render->info.vc1.quantizer          = v->quantizer_mode;
636     render->info.vc1.extended_mv        = v->extended_mv;
637     render->info.vc1.extended_dmv       = v->extended_dmv;
638     render->info.vc1.overlap            = v->overlap;
639     render->info.vc1.vstransform        = v->vstransform;
640     render->info.vc1.loopfilter         = v->s.loop_filter;
641     render->info.vc1.fastuvmc           = v->fastuvmc;
642     render->info.vc1.range_mapy_flag    = v->range_mapy_flag;
643     render->info.vc1.range_mapy         = v->range_mapy;
644     render->info.vc1.range_mapuv_flag   = v->range_mapuv_flag;
645     render->info.vc1.range_mapuv        = v->range_mapuv;
646     /* Specific to simple/main profile only */
647     render->info.vc1.multires           = v->multires;
648     render->info.vc1.syncmarker         = v->resync_marker;
649     render->info.vc1.rangered           = v->rangered | (v->rangeredfrm << 1);
650     render->info.vc1.maxbframes         = v->s.max_b_frames;
651
652     render->info.vc1.deblockEnable      = v->postprocflag & 1;
653     render->info.vc1.pquant             = v->pq;
654
655     render->info.vc1.forward_reference  = VDP_INVALID_HANDLE;
656     render->info.vc1.backward_reference = VDP_INVALID_HANDLE;
657
658     if (v->bi_type)
659         render->info.vc1.picture_type = 4;
660     else
661         render->info.vc1.picture_type = s->pict_type - 1 + s->pict_type / 3;
662
663     switch(s->pict_type){
664     case  AV_PICTURE_TYPE_B:
665         next = (struct vdpau_render_state *)s->next_picture.f->data[0];
666         assert(next);
667         render->info.vc1.backward_reference = next->surface;
668         // no break here, going to set forward prediction
669     case  AV_PICTURE_TYPE_P:
670         last = (struct vdpau_render_state *)s->last_picture.f->data[0];
671         if (!last) // FIXME: Does this test make sense?
672             last = render; // predict second field from the first
673         render->info.vc1.forward_reference = last->surface;
674     }
675
676     ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
677
678     render->info.vc1.slice_count          = 1;
679
680     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
681     render->bitstream_buffers_used        = 0;
682 }
683 #endif /* (CONFIG_VC1_VDPAU_DECODER */
684
685 #if CONFIG_MPEG4_VDPAU_DECODER
686 void ff_vdpau_mpeg4_decode_picture(Mpeg4DecContext *ctx, const uint8_t *buf,
687                                    int buf_size)
688 {
689     MpegEncContext *s = &ctx->m;
690     struct vdpau_render_state *render, *last, *next;
691     int i;
692
693     if (!s->current_picture_ptr) return;
694
695     render = (struct vdpau_render_state *)s->current_picture_ptr->f->data[0];
696     assert(render);
697
698     /* fill VdpPictureInfoMPEG4Part2 struct */
699     render->info.mpeg4.trd[0]                            = s->pp_time;
700     render->info.mpeg4.trb[0]                            = s->pb_time;
701     render->info.mpeg4.trd[1]                            = s->pp_field_time >> 1;
702     render->info.mpeg4.trb[1]                            = s->pb_field_time >> 1;
703     render->info.mpeg4.vop_time_increment_resolution     = s->avctx->time_base.den;
704     render->info.mpeg4.vop_coding_type                   = 0;
705     render->info.mpeg4.vop_fcode_forward                 = s->f_code;
706     render->info.mpeg4.vop_fcode_backward                = s->b_code;
707     render->info.mpeg4.resync_marker_disable             = !ctx->resync_marker;
708     render->info.mpeg4.interlaced                        = !s->progressive_sequence;
709     render->info.mpeg4.quant_type                        = s->mpeg_quant;
710     render->info.mpeg4.quarter_sample                    = s->quarter_sample;
711     render->info.mpeg4.short_video_header                = s->avctx->codec->id == AV_CODEC_ID_H263;
712     render->info.mpeg4.rounding_control                  = s->no_rounding;
713     render->info.mpeg4.alternate_vertical_scan_flag      = s->alternate_scan;
714     render->info.mpeg4.top_field_first                   = s->top_field_first;
715     for (i = 0; i < 64; ++i) {
716         render->info.mpeg4.intra_quantizer_matrix[i]     = s->intra_matrix[i];
717         render->info.mpeg4.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
718     }
719     render->info.mpeg4.forward_reference                 = VDP_INVALID_HANDLE;
720     render->info.mpeg4.backward_reference                = VDP_INVALID_HANDLE;
721
722     switch (s->pict_type) {
723     case AV_PICTURE_TYPE_B:
724         next = (struct vdpau_render_state *)s->next_picture.f->data[0];
725         assert(next);
726         render->info.mpeg4.backward_reference     = next->surface;
727         render->info.mpeg4.vop_coding_type        = 2;
728         // no break here, going to set forward prediction
729     case AV_PICTURE_TYPE_P:
730         last = (struct vdpau_render_state *)s->last_picture.f->data[0];
731         assert(last);
732         render->info.mpeg4.forward_reference      = last->surface;
733     }
734
735     ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
736
737     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
738     render->bitstream_buffers_used = 0;
739 }
740 #endif /* CONFIG_MPEG4_VDPAU_DECODER */
741 #endif /* FF_API_VDPAU */
742
743 #if FF_API_VDPAU_PROFILE
744 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
745 {
746 #define PROFILE(prof)                      \
747 do {                                       \
748     *profile = VDP_DECODER_PROFILE_##prof; \
749     return 0;                              \
750 } while (0)
751
752     switch (avctx->codec_id) {
753     case AV_CODEC_ID_MPEG1VIDEO:               PROFILE(MPEG1);
754     case AV_CODEC_ID_MPEG2VIDEO:
755         switch (avctx->profile) {
756         case FF_PROFILE_MPEG2_MAIN:            PROFILE(MPEG2_MAIN);
757         case FF_PROFILE_MPEG2_SIMPLE:          PROFILE(MPEG2_SIMPLE);
758         default:                               return AVERROR(EINVAL);
759         }
760     case AV_CODEC_ID_H263:                     PROFILE(MPEG4_PART2_ASP);
761     case AV_CODEC_ID_MPEG4:
762         switch (avctx->profile) {
763         case FF_PROFILE_MPEG4_SIMPLE:          PROFILE(MPEG4_PART2_SP);
764         case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
765         default:                               return AVERROR(EINVAL);
766         }
767     case AV_CODEC_ID_H264:
768         switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
769         case FF_PROFILE_H264_BASELINE:         PROFILE(H264_BASELINE);
770         case FF_PROFILE_H264_CONSTRAINED_BASELINE:
771         case FF_PROFILE_H264_MAIN:             PROFILE(H264_MAIN);
772         case FF_PROFILE_H264_HIGH:             PROFILE(H264_HIGH);
773 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
774         case FF_PROFILE_H264_EXTENDED:         PROFILE(H264_EXTENDED);
775 #endif
776         default:                               return AVERROR(EINVAL);
777         }
778     case AV_CODEC_ID_WMV3:
779     case AV_CODEC_ID_VC1:
780         switch (avctx->profile) {
781         case FF_PROFILE_VC1_SIMPLE:            PROFILE(VC1_SIMPLE);
782         case FF_PROFILE_VC1_MAIN:              PROFILE(VC1_MAIN);
783         case FF_PROFILE_VC1_ADVANCED:          PROFILE(VC1_ADVANCED);
784         default:                               return AVERROR(EINVAL);
785         }
786     }
787     return AVERROR(EINVAL);
788 #undef PROFILE
789 }
790 #endif /* FF_API_VDPAU_PROFILE */
791
792 AVVDPAUContext *av_vdpau_alloc_context(void)
793 {
794     return av_mallocz(sizeof(VDPAUHWContext));
795 }
796
797 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
798                           VdpGetProcAddress *get_proc, unsigned flags)
799 {
800     VDPAUHWContext *hwctx;
801
802     if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
803         return AVERROR(EINVAL);
804
805     if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
806         return AVERROR(ENOMEM);
807
808     hwctx = avctx->hwaccel_context;
809
810     memset(hwctx, 0, sizeof(*hwctx));
811     hwctx->context.decoder  = VDP_INVALID_HANDLE;
812     hwctx->device           = device;
813     hwctx->get_proc_address = get_proc;
814     hwctx->flags            = flags;
815     hwctx->reset            = 1;
816     return 0;
817 }
818
819 /* @}*/