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.
5 * Copyright (c) 2008 NVIDIA
7 * This file is part of FFmpeg.
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.
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.
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
31 #include "vdpau_compat.h"
32 #include "vdpau_internal.h"
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.
43 * @addtogroup VDPAU_Decoding
48 static int vdpau_error(VdpStatus status)
53 case VDP_STATUS_NO_IMPLEMENTATION:
54 return AVERROR(ENOSYS);
55 case VDP_STATUS_DISPLAY_PREEMPTED:
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:
68 return AVERROR(EINVAL);
72 AVVDPAUContext *av_alloc_vdpaucontext(void)
74 return av_vdpau_alloc_context();
77 MAKE_ACCESSORS(AVVDPAUContext, vdpau_hwaccel, AVVDPAU_Render2, render2)
79 int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
81 uint32_t *width, uint32_t *height)
84 uint32_t w = avctx->coded_width;
85 uint32_t h = avctx->coded_height;
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;
95 case AV_PIX_FMT_YUV422P:
96 case AV_PIX_FMT_YUVJ422P:
97 t = VDP_CHROMA_TYPE_422;
101 case AV_PIX_FMT_YUV444P:
102 case AV_PIX_FMT_YUVJ444P:
103 t = VDP_CHROMA_TYPE_444;
107 return AVERROR(ENOSYS);
119 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
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;
132 uint32_t max_level, max_mb, max_width, max_height;
137 vdctx->width = UINT32_MAX;
138 vdctx->height = UINT32_MAX;
140 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
141 return AVERROR(ENOSYS);
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 */
153 vdctx->device = hwctx->device;
154 vdctx->get_proc_address = hwctx->get_proc_address;
156 if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
159 if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
160 type != VDP_CHROMA_TYPE_420)
161 return AVERROR(ENOSYS);
163 AVHWFramesContext *frames_ctx = NULL;
164 AVVDPAUDeviceContext *dev_ctx;
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) {
175 avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
176 if (!avctx->hw_frames_ctx)
177 return AVERROR(ENOMEM);
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;
185 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
187 av_buffer_unref(&avctx->hw_frames_ctx);
193 av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
194 "required for VDPAU decoding.\n");
195 return AVERROR(EINVAL);
198 dev_ctx = frames_ctx->device_ctx->hwctx;
200 vdctx->device = dev_ctx->device;
201 vdctx->get_proc_address = dev_ctx->get_proc_address;
203 if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
208 return AVERROR(ENOTSUP);
210 status = vdctx->get_proc_address(vdctx->device,
211 VDP_FUNC_ID_GET_INFORMATION_STRING,
213 if (status != VDP_STATUS_OK)
214 return vdpau_error(status);
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);
227 status = vdctx->get_proc_address(vdctx->device,
228 VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
230 if (status != VDP_STATUS_OK)
231 return vdpau_error(status);
233 surface_query_caps = func;
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);
243 status = vdctx->get_proc_address(vdctx->device,
244 VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
246 if (status != VDP_STATUS_OK)
247 return vdpau_error(status);
249 decoder_query_caps = func;
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,
258 &max_width, &max_height);
261 if (status != VDP_STATUS_OK)
262 return vdpau_error(status);
264 if (supported != VDP_TRUE || max_level < level ||
265 max_width < width || max_height < height)
266 return AVERROR(ENOTSUP);
268 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
270 if (status != VDP_STATUS_OK)
271 return vdpau_error(status);
275 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
277 if (status != VDP_STATUS_OK)
278 return vdpau_error(status);
280 vdctx->render = func;
282 status = create(vdctx->device, profile, width, height, avctx->refs,
284 if (status == VDP_STATUS_OK) {
285 vdctx->width = avctx->coded_width;
286 vdctx->height = avctx->coded_height;
289 return vdpau_error(status);
292 int ff_vdpau_common_uninit(AVCodecContext *avctx)
294 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
295 VdpDecoderDestroy *destroy;
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)
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);
311 status = destroy(vdctx->decoder);
312 return vdpau_error(status);
315 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
317 VDPAUHWContext *hwctx = avctx->hwaccel_context;
318 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
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))
326 avctx->hwaccel->uninit(avctx);
327 return avctx->hwaccel->init(avctx);
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)
334 pic_ctx->bitstream_buffers_allocated = 0;
335 pic_ctx->bitstream_buffers_used = 0;
336 pic_ctx->bitstream_buffers = NULL;
340 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
341 struct vdpau_picture_context *pic_ctx)
343 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
344 AVVDPAUContext *hwctx = avctx->hwaccel_context;
345 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
349 val = ff_vdpau_common_reinit(avctx);
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);
357 status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
358 pic_ctx->bitstream_buffers_used,
359 pic_ctx->bitstream_buffers);
361 av_freep(&pic_ctx->bitstream_buffers);
363 return vdpau_error(status);
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)
371 MpegEncContext *s = avctx->priv_data;
372 Picture *pic = s->current_picture_ptr;
373 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
376 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
380 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
385 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
386 const uint8_t *buf, uint32_t size)
388 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
390 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
391 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
393 return AVERROR(ENOMEM);
395 pic_ctx->bitstream_buffers = buffers;
396 buffers += pic_ctx->bitstream_buffers_used++;
398 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
399 buffers->bitstream = buf;
400 buffers->bitstream_bytes = size;
404 /* Obsolete non-hwaccel VDPAU support below... */
407 void ff_vdpau_add_data_chunk(uint8_t *data, const uint8_t *buf, int buf_size)
409 struct vdpau_render_state *render = (struct vdpau_render_state*)data;
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)
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++;
424 #if CONFIG_H264_VDPAU_DECODER
425 void ff_vdpau_h264_set_reference_frames(H264Context *h)
427 struct vdpau_render_state *render, *render_ref;
428 VdpReferenceFrameH264 *rf, *rf2;
430 int i, list, pic_frame_idx;
432 render = (struct vdpau_render_state *)h->cur_pic_ptr->f->data[0];
435 rf = &render->info.h264.referenceFrames[0];
436 #define H264_RF_COUNT FF_ARRAY_ELEMS(render->info.h264.referenceFrames)
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;
442 for (i = 0; i < ls; ++i) {
444 if (!pic || !pic->reference)
446 pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
448 render_ref = (struct vdpau_render_state *)pic->f->data[0];
451 rf2 = &render->info.h264.referenceFrames[0];
454 (rf2->surface == render_ref->surface)
455 && (rf2->is_long_term == pic->long_ref)
456 && (rf2->frame_idx == pic_frame_idx)
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;
467 if (rf >= &render->info.h264.referenceFrames[H264_RF_COUNT])
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;
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;
493 void ff_vdpau_h264_picture_start(H264Context *h)
495 struct vdpau_render_state *render;
498 render = (struct vdpau_render_state *)h->cur_pic_ptr->f->data[0];
501 for (i = 0; i < 2; ++i) {
502 int foc = h->cur_pic_ptr->field_poc[i];
505 render->info.h264.field_order_cnt[i] = foc;
508 render->info.h264.frame_num = h->poc.frame_num;
511 void ff_vdpau_h264_picture_complete(H264Context *h)
513 struct vdpau_render_state *render;
515 render = (struct vdpau_render_state *)h->cur_pic_ptr->f->data[0];
518 render->info.h264.slice_count = h->current_slice;
519 if (render->info.h264.slice_count < 1)
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]));
550 ff_h264_draw_horiz_band(h, &h->slice_ctx[0], 0, h->avctx->height);
551 render->bitstream_buffers_used = 0;
553 #endif /* CONFIG_H264_VDPAU_DECODER */
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)
559 struct vdpau_render_state *render, *last, *next;
562 if (!s->current_picture_ptr) return;
564 render = (struct vdpau_render_state *)s->current_picture_ptr->f->data[0];
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];
588 render->info.mpeg.forward_reference = VDP_INVALID_HANDLE;
589 render->info.mpeg.backward_reference = VDP_INVALID_HANDLE;
591 switch(s->pict_type){
592 case AV_PICTURE_TYPE_B:
593 next = (struct vdpau_render_state *)s->next_picture.f->data[0];
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;
604 ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
606 render->info.mpeg.slice_count = slice_count;
609 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
610 render->bitstream_buffers_used = 0;
612 #endif /* CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER */
614 #if CONFIG_VC1_VDPAU_DECODER
615 void ff_vdpau_vc1_decode_picture(MpegEncContext *s, const uint8_t *buf,
618 VC1Context *v = s->avctx->priv_data;
619 struct vdpau_render_state *render, *last, *next;
621 render = (struct vdpau_render_state *)s->current_picture.f->data[0];
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;
652 render->info.vc1.deblockEnable = v->postprocflag & 1;
653 render->info.vc1.pquant = v->pq;
655 render->info.vc1.forward_reference = VDP_INVALID_HANDLE;
656 render->info.vc1.backward_reference = VDP_INVALID_HANDLE;
659 render->info.vc1.picture_type = 4;
661 render->info.vc1.picture_type = s->pict_type - 1 + s->pict_type / 3;
663 switch(s->pict_type){
664 case AV_PICTURE_TYPE_B:
665 next = (struct vdpau_render_state *)s->next_picture.f->data[0];
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;
676 ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
678 render->info.vc1.slice_count = 1;
680 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
681 render->bitstream_buffers_used = 0;
683 #endif /* (CONFIG_VC1_VDPAU_DECODER */
685 #if CONFIG_MPEG4_VDPAU_DECODER
686 void ff_vdpau_mpeg4_decode_picture(Mpeg4DecContext *ctx, const uint8_t *buf,
689 MpegEncContext *s = &ctx->m;
690 struct vdpau_render_state *render, *last, *next;
693 if (!s->current_picture_ptr) return;
695 render = (struct vdpau_render_state *)s->current_picture_ptr->f->data[0];
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];
719 render->info.mpeg4.forward_reference = VDP_INVALID_HANDLE;
720 render->info.mpeg4.backward_reference = VDP_INVALID_HANDLE;
722 switch (s->pict_type) {
723 case AV_PICTURE_TYPE_B:
724 next = (struct vdpau_render_state *)s->next_picture.f->data[0];
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];
732 render->info.mpeg4.forward_reference = last->surface;
735 ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
737 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
738 render->bitstream_buffers_used = 0;
740 #endif /* CONFIG_MPEG4_VDPAU_DECODER */
741 #endif /* FF_API_VDPAU */
743 #if FF_API_VDPAU_PROFILE
744 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
746 #define PROFILE(prof) \
748 *profile = VDP_DECODER_PROFILE_##prof; \
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);
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);
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);
776 default: return AVERROR(EINVAL);
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);
787 return AVERROR(EINVAL);
790 #endif /* FF_API_VDPAU_PROFILE */
792 AVVDPAUContext *av_vdpau_alloc_context(void)
794 return av_mallocz(sizeof(VDPAUHWContext));
797 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
798 VdpGetProcAddress *get_proc, unsigned flags)
800 VDPAUHWContext *hwctx;
802 if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
803 return AVERROR(EINVAL);
805 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
806 return AVERROR(ENOMEM);
808 hwctx = avctx->hwaccel_context;
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;