Merge commit '502cde409ca5ee97ef70c2cdede88b9101746ff6'
[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 #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 AVVDPAUContext *av_alloc_vdpaucontext(void)
68 {
69     return av_vdpau_alloc_context();
70 }
71
72 MAKE_ACCESSORS(AVVDPAUContext, vdpau_hwaccel, AVVDPAU_Render2, render2)
73
74 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
75                          int level)
76 {
77     VDPAUHWContext *hwctx = avctx->hwaccel_context;
78     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
79     VdpDecoderCreate *create;
80     void *func;
81     VdpStatus status;
82     /* See vdpau/vdpau.h for alignment constraints. */
83     uint32_t width  = (avctx->coded_width + 1) & ~1;
84     uint32_t height = (avctx->coded_height + 3) & ~3;
85
86     vdctx->width            = UINT32_MAX;
87     vdctx->height           = UINT32_MAX;
88
89     if (!hwctx) {
90         vdctx->device  = VDP_INVALID_HANDLE;
91         av_log(avctx, AV_LOG_WARNING, "hwaccel_context has not been setup by the user application, cannot initialize\n");
92         return 0;
93     }
94
95     if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
96         vdctx->decoder = hwctx->context.decoder;
97         vdctx->render  = hwctx->context.render;
98         vdctx->device  = VDP_INVALID_HANDLE;
99         return 0; /* Decoder created by user */
100     }
101
102     vdctx->device           = hwctx->device;
103     vdctx->get_proc_address = hwctx->get_proc_address;
104
105     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
106                                      &func);
107     if (status != VDP_STATUS_OK)
108         return vdpau_error(status);
109     else
110         create = func;
111
112     status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
113                                      &func);
114     if (status != VDP_STATUS_OK)
115         return vdpau_error(status);
116     else
117         vdctx->render = func;
118
119     status = create(vdctx->device, profile, width, height, avctx->refs,
120                     &vdctx->decoder);
121     if (status == VDP_STATUS_OK) {
122         vdctx->width  = avctx->coded_width;
123         vdctx->height = avctx->coded_height;
124     }
125
126     return vdpau_error(status);
127 }
128
129 int ff_vdpau_common_uninit(AVCodecContext *avctx)
130 {
131     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
132     VdpDecoderDestroy *destroy;
133     void *func;
134     VdpStatus status;
135
136     if (vdctx->device == VDP_INVALID_HANDLE)
137         return 0; /* Decoder created and destroyed by user */
138     if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
139         return 0;
140
141     status = vdctx->get_proc_address(vdctx->device,
142                                      VDP_FUNC_ID_DECODER_DESTROY, &func);
143     if (status != VDP_STATUS_OK)
144         return vdpau_error(status);
145     else
146         destroy = func;
147
148     status = destroy(vdctx->decoder);
149     return vdpau_error(status);
150 }
151
152 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
153 {
154     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
155
156     if (vdctx->device == VDP_INVALID_HANDLE)
157         return 0; /* Decoder created by user */
158     if (avctx->coded_width == vdctx->width &&
159         avctx->coded_height == vdctx->height)
160         return 0;
161
162     avctx->hwaccel->uninit(avctx);
163     return avctx->hwaccel->init(avctx);
164 }
165
166 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
167                                 av_unused const uint8_t *buffer,
168                                 av_unused uint32_t size)
169 {
170     pic_ctx->bitstream_buffers_allocated = 0;
171     pic_ctx->bitstream_buffers_used      = 0;
172     pic_ctx->bitstream_buffers           = NULL;
173     return 0;
174 }
175
176 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
177                               struct vdpau_picture_context *pic_ctx)
178 {
179     VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
180     AVVDPAUContext *hwctx = avctx->hwaccel_context;
181     VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
182     VdpStatus status;
183     int val;
184
185     val = ff_vdpau_common_reinit(avctx);
186     if (val < 0)
187         return val;
188
189 #if FF_API_BUFS_VDPAU
190 FF_DISABLE_DEPRECATION_WARNINGS
191     hwctx->info = pic_ctx->info;
192     hwctx->bitstream_buffers = pic_ctx->bitstream_buffers;
193     hwctx->bitstream_buffers_used = pic_ctx->bitstream_buffers_used;
194     hwctx->bitstream_buffers_allocated = pic_ctx->bitstream_buffers_allocated;
195 FF_ENABLE_DEPRECATION_WARNINGS
196 #endif
197
198     if (!hwctx->render) {
199         status = hwctx->render2(avctx, frame, (void *)&pic_ctx->info,
200                                 pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
201     } else
202     status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
203                            pic_ctx->bitstream_buffers_used,
204                            pic_ctx->bitstream_buffers);
205
206     av_freep(&pic_ctx->bitstream_buffers);
207
208 #if FF_API_BUFS_VDPAU
209 FF_DISABLE_DEPRECATION_WARNINGS
210     hwctx->bitstream_buffers = NULL;
211     hwctx->bitstream_buffers_used = 0;
212     hwctx->bitstream_buffers_allocated = 0;
213 FF_ENABLE_DEPRECATION_WARNINGS
214 #endif
215
216     return vdpau_error(status);
217 }
218
219 #if CONFIG_H263_VDPAU_HWACCEL  || CONFIG_MPEG1_VDPAU_HWACCEL || \
220     CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
221     CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
222 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
223 {
224     MpegEncContext *s = avctx->priv_data;
225     Picture *pic = s->current_picture_ptr;
226     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
227     int val;
228
229     val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
230     if (val < 0)
231         return val;
232
233     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
234     return 0;
235 }
236 #endif
237
238 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
239                         const uint8_t *buf, uint32_t size)
240 {
241     VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
242
243     buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
244                               (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
245     if (!buffers)
246         return AVERROR(ENOMEM);
247
248     pic_ctx->bitstream_buffers = buffers;
249     buffers += pic_ctx->bitstream_buffers_used++;
250
251     buffers->struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
252     buffers->bitstream       = buf;
253     buffers->bitstream_bytes = size;
254     return 0;
255 }
256
257 /* Obsolete non-hwaccel VDPAU support below... */
258
259 void ff_vdpau_h264_set_reference_frames(H264Context *h)
260 {
261     struct vdpau_render_state *render, *render_ref;
262     VdpReferenceFrameH264 *rf, *rf2;
263     H264Picture *pic;
264     int i, list, pic_frame_idx;
265
266     render = (struct vdpau_render_state *)h->cur_pic_ptr->f.data[0];
267     assert(render);
268
269     rf = &render->info.h264.referenceFrames[0];
270 #define H264_RF_COUNT FF_ARRAY_ELEMS(render->info.h264.referenceFrames)
271
272     for (list = 0; list < 2; ++list) {
273         H264Picture **lp = list ? h->long_ref : h->short_ref;
274         int ls = list ? 16 : h->short_ref_count;
275
276         for (i = 0; i < ls; ++i) {
277             pic = lp[i];
278             if (!pic || !pic->reference)
279                 continue;
280             pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
281
282             render_ref = (struct vdpau_render_state *)pic->f.data[0];
283             assert(render_ref);
284
285             rf2 = &render->info.h264.referenceFrames[0];
286             while (rf2 != rf) {
287                 if (
288                     (rf2->surface == render_ref->surface)
289                     && (rf2->is_long_term == pic->long_ref)
290                     && (rf2->frame_idx == pic_frame_idx)
291                 )
292                     break;
293                 ++rf2;
294             }
295             if (rf2 != rf) {
296                 rf2->top_is_reference    |= (pic->reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
297                 rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
298                 continue;
299             }
300
301             if (rf >= &render->info.h264.referenceFrames[H264_RF_COUNT])
302                 continue;
303
304             rf->surface             = render_ref->surface;
305             rf->is_long_term        = pic->long_ref;
306             rf->top_is_reference    = (pic->reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
307             rf->bottom_is_reference = (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
308             rf->field_order_cnt[0]  = pic->field_poc[0];
309             rf->field_order_cnt[1]  = pic->field_poc[1];
310             rf->frame_idx           = pic_frame_idx;
311
312             ++rf;
313         }
314     }
315
316     for (; rf < &render->info.h264.referenceFrames[H264_RF_COUNT]; ++rf) {
317         rf->surface             = VDP_INVALID_HANDLE;
318         rf->is_long_term        = 0;
319         rf->top_is_reference    = 0;
320         rf->bottom_is_reference = 0;
321         rf->field_order_cnt[0]  = 0;
322         rf->field_order_cnt[1]  = 0;
323         rf->frame_idx           = 0;
324     }
325 }
326
327 void ff_vdpau_add_data_chunk(uint8_t *data, const uint8_t *buf, int buf_size)
328 {
329     struct vdpau_render_state *render = (struct vdpau_render_state*)data;
330     assert(render);
331
332     render->bitstream_buffers= av_fast_realloc(
333         render->bitstream_buffers,
334         &render->bitstream_buffers_allocated,
335         sizeof(*render->bitstream_buffers)*(render->bitstream_buffers_used + 1)
336     );
337
338     render->bitstream_buffers[render->bitstream_buffers_used].struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
339     render->bitstream_buffers[render->bitstream_buffers_used].bitstream       = buf;
340     render->bitstream_buffers[render->bitstream_buffers_used].bitstream_bytes = buf_size;
341     render->bitstream_buffers_used++;
342 }
343
344 #if CONFIG_H264_VDPAU_DECODER
345 void ff_vdpau_h264_picture_start(H264Context *h)
346 {
347     struct vdpau_render_state *render;
348     int i;
349
350     render = (struct vdpau_render_state *)h->cur_pic_ptr->f.data[0];
351     assert(render);
352
353     for (i = 0; i < 2; ++i) {
354         int foc = h->cur_pic_ptr->field_poc[i];
355         if (foc == INT_MAX)
356             foc = 0;
357         render->info.h264.field_order_cnt[i] = foc;
358     }
359
360     render->info.h264.frame_num = h->frame_num;
361 }
362
363 void ff_vdpau_h264_picture_complete(H264Context *h)
364 {
365     struct vdpau_render_state *render;
366
367     render = (struct vdpau_render_state *)h->cur_pic_ptr->f.data[0];
368     assert(render);
369
370     render->info.h264.slice_count = h->slice_num;
371     if (render->info.h264.slice_count < 1)
372         return;
373
374     render->info.h264.is_reference                           = (h->cur_pic_ptr->reference & 3) ? VDP_TRUE : VDP_FALSE;
375     render->info.h264.field_pic_flag                         = h->picture_structure != PICT_FRAME;
376     render->info.h264.bottom_field_flag                      = h->picture_structure == PICT_BOTTOM_FIELD;
377     render->info.h264.num_ref_frames                         = h->sps.ref_frame_count;
378     render->info.h264.mb_adaptive_frame_field_flag           = h->sps.mb_aff && !render->info.h264.field_pic_flag;
379     render->info.h264.constrained_intra_pred_flag            = h->pps.constrained_intra_pred;
380     render->info.h264.weighted_pred_flag                     = h->pps.weighted_pred;
381     render->info.h264.weighted_bipred_idc                    = h->pps.weighted_bipred_idc;
382     render->info.h264.frame_mbs_only_flag                    = h->sps.frame_mbs_only_flag;
383     render->info.h264.transform_8x8_mode_flag                = h->pps.transform_8x8_mode;
384     render->info.h264.chroma_qp_index_offset                 = h->pps.chroma_qp_index_offset[0];
385     render->info.h264.second_chroma_qp_index_offset          = h->pps.chroma_qp_index_offset[1];
386     render->info.h264.pic_init_qp_minus26                    = h->pps.init_qp - 26;
387     render->info.h264.num_ref_idx_l0_active_minus1           = h->pps.ref_count[0] - 1;
388     render->info.h264.num_ref_idx_l1_active_minus1           = h->pps.ref_count[1] - 1;
389     render->info.h264.log2_max_frame_num_minus4              = h->sps.log2_max_frame_num - 4;
390     render->info.h264.pic_order_cnt_type                     = h->sps.poc_type;
391     render->info.h264.log2_max_pic_order_cnt_lsb_minus4      = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
392     render->info.h264.delta_pic_order_always_zero_flag       = h->sps.delta_pic_order_always_zero_flag;
393     render->info.h264.direct_8x8_inference_flag              = h->sps.direct_8x8_inference_flag;
394     render->info.h264.entropy_coding_mode_flag               = h->pps.cabac;
395     render->info.h264.pic_order_present_flag                 = h->pps.pic_order_present;
396     render->info.h264.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
397     render->info.h264.redundant_pic_cnt_present_flag         = h->pps.redundant_pic_cnt_present;
398     memcpy(render->info.h264.scaling_lists_4x4, h->pps.scaling_matrix4, sizeof(render->info.h264.scaling_lists_4x4));
399     memcpy(render->info.h264.scaling_lists_8x8[0], h->pps.scaling_matrix8[0], sizeof(render->info.h264.scaling_lists_8x8[0]));
400     memcpy(render->info.h264.scaling_lists_8x8[1], h->pps.scaling_matrix8[3], sizeof(render->info.h264.scaling_lists_8x8[0]));
401
402     ff_h264_draw_horiz_band(h, 0, h->avctx->height);
403     render->bitstream_buffers_used = 0;
404 }
405 #endif /* CONFIG_H264_VDPAU_DECODER */
406
407 #if CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER
408 void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf,
409                                     int buf_size, int slice_count)
410 {
411     struct vdpau_render_state *render, *last, *next;
412     int i;
413
414     if (!s->current_picture_ptr) return;
415
416     render = (struct vdpau_render_state *)s->current_picture_ptr->f->data[0];
417     assert(render);
418
419     /* fill VdpPictureInfoMPEG1Or2 struct */
420     render->info.mpeg.picture_structure          = s->picture_structure;
421     render->info.mpeg.picture_coding_type        = s->pict_type;
422     render->info.mpeg.intra_dc_precision         = s->intra_dc_precision;
423     render->info.mpeg.frame_pred_frame_dct       = s->frame_pred_frame_dct;
424     render->info.mpeg.concealment_motion_vectors = s->concealment_motion_vectors;
425     render->info.mpeg.intra_vlc_format           = s->intra_vlc_format;
426     render->info.mpeg.alternate_scan             = s->alternate_scan;
427     render->info.mpeg.q_scale_type               = s->q_scale_type;
428     render->info.mpeg.top_field_first            = s->top_field_first;
429     render->info.mpeg.full_pel_forward_vector    = s->full_pel[0]; // MPEG-1 only.  Set 0 for MPEG-2
430     render->info.mpeg.full_pel_backward_vector   = s->full_pel[1]; // MPEG-1 only.  Set 0 for MPEG-2
431     render->info.mpeg.f_code[0][0]               = s->mpeg_f_code[0][0]; // For MPEG-1 fill both horiz. & vert.
432     render->info.mpeg.f_code[0][1]               = s->mpeg_f_code[0][1];
433     render->info.mpeg.f_code[1][0]               = s->mpeg_f_code[1][0];
434     render->info.mpeg.f_code[1][1]               = s->mpeg_f_code[1][1];
435     for (i = 0; i < 64; ++i) {
436         render->info.mpeg.intra_quantizer_matrix[i]     = s->intra_matrix[i];
437         render->info.mpeg.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
438     }
439
440     render->info.mpeg.forward_reference          = VDP_INVALID_HANDLE;
441     render->info.mpeg.backward_reference         = VDP_INVALID_HANDLE;
442
443     switch(s->pict_type){
444     case  AV_PICTURE_TYPE_B:
445         next = (struct vdpau_render_state *)s->next_picture.f->data[0];
446         assert(next);
447         render->info.mpeg.backward_reference     = next->surface;
448         // no return here, going to set forward prediction
449     case  AV_PICTURE_TYPE_P:
450         last = (struct vdpau_render_state *)s->last_picture.f->data[0];
451         if (!last) // FIXME: Does this test make sense?
452             last = render; // predict second field from the first
453         render->info.mpeg.forward_reference      = last->surface;
454     }
455
456     ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
457
458     render->info.mpeg.slice_count                = slice_count;
459
460     if (slice_count)
461         ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
462     render->bitstream_buffers_used               = 0;
463 }
464 #endif /* CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER */
465
466 #if CONFIG_VC1_VDPAU_DECODER
467 void ff_vdpau_vc1_decode_picture(MpegEncContext *s, const uint8_t *buf,
468                                  int buf_size)
469 {
470     VC1Context *v = s->avctx->priv_data;
471     struct vdpau_render_state *render, *last, *next;
472
473     render = (struct vdpau_render_state *)s->current_picture.f->data[0];
474     assert(render);
475
476     /*  fill LvPictureInfoVC1 struct */
477     render->info.vc1.frame_coding_mode  = v->fcm ? v->fcm + 1 : 0;
478     render->info.vc1.postprocflag       = v->postprocflag;
479     render->info.vc1.pulldown           = v->broadcast;
480     render->info.vc1.interlace          = v->interlace;
481     render->info.vc1.tfcntrflag         = v->tfcntrflag;
482     render->info.vc1.finterpflag        = v->finterpflag;
483     render->info.vc1.psf                = v->psf;
484     render->info.vc1.dquant             = v->dquant;
485     render->info.vc1.panscan_flag       = v->panscanflag;
486     render->info.vc1.refdist_flag       = v->refdist_flag;
487     render->info.vc1.quantizer          = v->quantizer_mode;
488     render->info.vc1.extended_mv        = v->extended_mv;
489     render->info.vc1.extended_dmv       = v->extended_dmv;
490     render->info.vc1.overlap            = v->overlap;
491     render->info.vc1.vstransform        = v->vstransform;
492     render->info.vc1.loopfilter         = v->s.loop_filter;
493     render->info.vc1.fastuvmc           = v->fastuvmc;
494     render->info.vc1.range_mapy_flag    = v->range_mapy_flag;
495     render->info.vc1.range_mapy         = v->range_mapy;
496     render->info.vc1.range_mapuv_flag   = v->range_mapuv_flag;
497     render->info.vc1.range_mapuv        = v->range_mapuv;
498     /* Specific to simple/main profile only */
499     render->info.vc1.multires           = v->multires;
500     render->info.vc1.syncmarker         = v->resync_marker;
501     render->info.vc1.rangered           = v->rangered | (v->rangeredfrm << 1);
502     render->info.vc1.maxbframes         = v->s.max_b_frames;
503
504     render->info.vc1.deblockEnable      = v->postprocflag & 1;
505     render->info.vc1.pquant             = v->pq;
506
507     render->info.vc1.forward_reference  = VDP_INVALID_HANDLE;
508     render->info.vc1.backward_reference = VDP_INVALID_HANDLE;
509
510     if (v->bi_type)
511         render->info.vc1.picture_type = 4;
512     else
513         render->info.vc1.picture_type = s->pict_type - 1 + s->pict_type / 3;
514
515     switch(s->pict_type){
516     case  AV_PICTURE_TYPE_B:
517         next = (struct vdpau_render_state *)s->next_picture.f->data[0];
518         assert(next);
519         render->info.vc1.backward_reference = next->surface;
520         // no break here, going to set forward prediction
521     case  AV_PICTURE_TYPE_P:
522         last = (struct vdpau_render_state *)s->last_picture.f->data[0];
523         if (!last) // FIXME: Does this test make sense?
524             last = render; // predict second field from the first
525         render->info.vc1.forward_reference = last->surface;
526     }
527
528     ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
529
530     render->info.vc1.slice_count          = 1;
531
532     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
533     render->bitstream_buffers_used        = 0;
534 }
535 #endif /* (CONFIG_VC1_VDPAU_DECODER */
536
537 #if CONFIG_MPEG4_VDPAU_DECODER
538 void ff_vdpau_mpeg4_decode_picture(Mpeg4DecContext *ctx, const uint8_t *buf,
539                                    int buf_size)
540 {
541     MpegEncContext *s = &ctx->m;
542     struct vdpau_render_state *render, *last, *next;
543     int i;
544
545     if (!s->current_picture_ptr) return;
546
547     render = (struct vdpau_render_state *)s->current_picture_ptr->f->data[0];
548     assert(render);
549
550     /* fill VdpPictureInfoMPEG4Part2 struct */
551     render->info.mpeg4.trd[0]                            = s->pp_time;
552     render->info.mpeg4.trb[0]                            = s->pb_time;
553     render->info.mpeg4.trd[1]                            = s->pp_field_time >> 1;
554     render->info.mpeg4.trb[1]                            = s->pb_field_time >> 1;
555     render->info.mpeg4.vop_time_increment_resolution     = s->avctx->time_base.den;
556     render->info.mpeg4.vop_coding_type                   = 0;
557     render->info.mpeg4.vop_fcode_forward                 = s->f_code;
558     render->info.mpeg4.vop_fcode_backward                = s->b_code;
559     render->info.mpeg4.resync_marker_disable             = !ctx->resync_marker;
560     render->info.mpeg4.interlaced                        = !s->progressive_sequence;
561     render->info.mpeg4.quant_type                        = s->mpeg_quant;
562     render->info.mpeg4.quarter_sample                    = s->quarter_sample;
563     render->info.mpeg4.short_video_header                = s->avctx->codec->id == AV_CODEC_ID_H263;
564     render->info.mpeg4.rounding_control                  = s->no_rounding;
565     render->info.mpeg4.alternate_vertical_scan_flag      = s->alternate_scan;
566     render->info.mpeg4.top_field_first                   = s->top_field_first;
567     for (i = 0; i < 64; ++i) {
568         render->info.mpeg4.intra_quantizer_matrix[i]     = s->intra_matrix[i];
569         render->info.mpeg4.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
570     }
571     render->info.mpeg4.forward_reference                 = VDP_INVALID_HANDLE;
572     render->info.mpeg4.backward_reference                = VDP_INVALID_HANDLE;
573
574     switch (s->pict_type) {
575     case AV_PICTURE_TYPE_B:
576         next = (struct vdpau_render_state *)s->next_picture.f->data[0];
577         assert(next);
578         render->info.mpeg4.backward_reference     = next->surface;
579         render->info.mpeg4.vop_coding_type        = 2;
580         // no break here, going to set forward prediction
581     case AV_PICTURE_TYPE_P:
582         last = (struct vdpau_render_state *)s->last_picture.f->data[0];
583         assert(last);
584         render->info.mpeg4.forward_reference      = last->surface;
585     }
586
587     ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
588
589     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
590     render->bitstream_buffers_used = 0;
591 }
592 #endif /* CONFIG_MPEG4_VDPAU_DECODER */
593
594 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
595 {
596 #define PROFILE(prof)       \
597 do {                        \
598     *profile = prof;        \
599     return 0;               \
600 } while (0)
601
602     switch (avctx->codec_id) {
603     case AV_CODEC_ID_MPEG1VIDEO:               PROFILE(VDP_DECODER_PROFILE_MPEG1);
604     case AV_CODEC_ID_MPEG2VIDEO:
605         switch (avctx->profile) {
606         case FF_PROFILE_MPEG2_MAIN:            PROFILE(VDP_DECODER_PROFILE_MPEG2_MAIN);
607         case FF_PROFILE_MPEG2_SIMPLE:          PROFILE(VDP_DECODER_PROFILE_MPEG2_SIMPLE);
608         default:                               return AVERROR(EINVAL);
609         }
610     case AV_CODEC_ID_H263:                     PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
611     case AV_CODEC_ID_MPEG4:
612         switch (avctx->profile) {
613         case FF_PROFILE_MPEG4_SIMPLE:          PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_SP);
614         case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
615         default:                               return AVERROR(EINVAL);
616         }
617     case AV_CODEC_ID_H264:
618         switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
619         case FF_PROFILE_H264_CONSTRAINED_BASELINE:
620         case FF_PROFILE_H264_BASELINE:         PROFILE(VDP_DECODER_PROFILE_H264_BASELINE);
621         case FF_PROFILE_H264_MAIN:             PROFILE(VDP_DECODER_PROFILE_H264_MAIN);
622         case FF_PROFILE_H264_HIGH:             PROFILE(VDP_DECODER_PROFILE_H264_HIGH);
623         default:                               return AVERROR(EINVAL);
624         }
625     case AV_CODEC_ID_WMV3:
626     case AV_CODEC_ID_VC1:
627         switch (avctx->profile) {
628         case FF_PROFILE_VC1_SIMPLE:            PROFILE(VDP_DECODER_PROFILE_VC1_SIMPLE);
629         case FF_PROFILE_VC1_MAIN:              PROFILE(VDP_DECODER_PROFILE_VC1_MAIN);
630         case FF_PROFILE_VC1_ADVANCED:          PROFILE(VDP_DECODER_PROFILE_VC1_ADVANCED);
631         default:                               return AVERROR(EINVAL);
632         }
633     }
634     return AVERROR(EINVAL);
635 }
636
637 AVVDPAUContext *av_vdpau_alloc_context(void)
638 {
639     return av_mallocz(sizeof(AVVDPAUContext));
640 }
641
642 /* @}*/