2 * MPEG-1/2 HW decode acceleration through VDPAU
4 * Copyright (c) 2008 NVIDIA
5 * Copyright (c) 2013 RĂ©mi Denis-Courmont
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 Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <vdpau/vdpau.h>
27 #include "mpegvideo.h"
29 #include "vdpau_internal.h"
31 static int vdpau_mpeg_start_frame(AVCodecContext *avctx,
32 const uint8_t *buffer, uint32_t size)
34 MpegEncContext * const s = avctx->priv_data;
35 Picture *pic = s->current_picture_ptr;
36 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
37 VdpPictureInfoMPEG1Or2 *info = &pic_ctx->info.mpeg;
41 /* fill VdpPictureInfoMPEG1Or2 struct */
42 info->forward_reference = VDP_INVALID_HANDLE;
43 info->backward_reference = VDP_INVALID_HANDLE;
45 switch (s->pict_type) {
46 case AV_PICTURE_TYPE_B:
47 ref = ff_vdpau_get_surface_id(s->next_picture.f);
48 assert(ref != VDP_INVALID_HANDLE);
49 info->backward_reference = ref;
50 /* fall through to forward prediction */
51 case AV_PICTURE_TYPE_P:
52 ref = ff_vdpau_get_surface_id(s->last_picture.f);
53 info->forward_reference = ref;
56 info->slice_count = 0;
57 info->picture_structure = s->picture_structure;
58 info->picture_coding_type = s->pict_type;
59 info->intra_dc_precision = s->intra_dc_precision;
60 info->frame_pred_frame_dct = s->frame_pred_frame_dct;
61 info->concealment_motion_vectors = s->concealment_motion_vectors;
62 info->intra_vlc_format = s->intra_vlc_format;
63 info->alternate_scan = s->alternate_scan;
64 info->q_scale_type = s->q_scale_type;
65 info->top_field_first = s->top_field_first;
66 // Both for MPEG-1 only, zero for MPEG-2:
67 info->full_pel_forward_vector = s->full_pel[0];
68 info->full_pel_backward_vector = s->full_pel[1];
69 // For MPEG-1 fill both horizontal & vertical:
70 info->f_code[0][0] = s->mpeg_f_code[0][0];
71 info->f_code[0][1] = s->mpeg_f_code[0][1];
72 info->f_code[1][0] = s->mpeg_f_code[1][0];
73 info->f_code[1][1] = s->mpeg_f_code[1][1];
74 for (i = 0; i < 64; ++i) {
75 info->intra_quantizer_matrix[i] = s->intra_matrix[i];
76 info->non_intra_quantizer_matrix[i] = s->inter_matrix[i];
79 return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
82 static int vdpau_mpeg_decode_slice(AVCodecContext *avctx,
83 const uint8_t *buffer, uint32_t size)
85 MpegEncContext * const s = avctx->priv_data;
86 Picture *pic = s->current_picture_ptr;
87 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
90 val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
94 pic_ctx->info.mpeg.slice_count++;
98 #if CONFIG_MPEG1_VDPAU_HWACCEL
99 static int vdpau_mpeg1_init(AVCodecContext *avctx)
101 return ff_vdpau_common_init(avctx, VDP_DECODER_PROFILE_MPEG1,
102 VDP_DECODER_LEVEL_MPEG1_NA);
105 AVHWAccel ff_mpeg1_vdpau_hwaccel = {
106 .name = "mpeg1_vdpau",
107 .type = AVMEDIA_TYPE_VIDEO,
108 .id = AV_CODEC_ID_MPEG1VIDEO,
109 .pix_fmt = AV_PIX_FMT_VDPAU,
110 .start_frame = vdpau_mpeg_start_frame,
111 .end_frame = ff_vdpau_mpeg_end_frame,
112 .decode_slice = vdpau_mpeg_decode_slice,
113 .frame_priv_data_size = sizeof(struct vdpau_picture_context),
114 .init = vdpau_mpeg1_init,
115 .uninit = ff_vdpau_common_uninit,
116 .priv_data_size = sizeof(VDPAUContext),
120 #if CONFIG_MPEG2_VDPAU_HWACCEL
121 static int vdpau_mpeg2_init(AVCodecContext *avctx)
123 VdpDecoderProfile profile;
125 switch (avctx->profile) {
126 case FF_PROFILE_MPEG2_MAIN:
127 profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
129 case FF_PROFILE_MPEG2_SIMPLE:
130 profile = VDP_DECODER_PROFILE_MPEG2_SIMPLE;
133 return AVERROR(EINVAL);
136 return ff_vdpau_common_init(avctx, profile, VDP_DECODER_LEVEL_MPEG2_HL);
139 AVHWAccel ff_mpeg2_vdpau_hwaccel = {
140 .name = "mpeg2_vdpau",
141 .type = AVMEDIA_TYPE_VIDEO,
142 .id = AV_CODEC_ID_MPEG2VIDEO,
143 .pix_fmt = AV_PIX_FMT_VDPAU,
144 .start_frame = vdpau_mpeg_start_frame,
145 .end_frame = ff_vdpau_mpeg_end_frame,
146 .decode_slice = vdpau_mpeg_decode_slice,
147 .frame_priv_data_size = sizeof(struct vdpau_picture_context),
148 .init = vdpau_mpeg2_init,
149 .uninit = ff_vdpau_common_uninit,
150 .priv_data_size = sizeof(VDPAUContext),