innoHeim/Rsupport Screen Capture Codec decoder
[ffmpeg.git] / libavcodec / vdpau_mpeg12.c
1 /*
2  * MPEG-1/2 HW decode acceleration through VDPAU
3  *
4  * Copyright (c) 2008 NVIDIA
5  * Copyright (c) 2013 RĂ©mi Denis-Courmont
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <vdpau/vdpau.h>
25
26 #include "avcodec.h"
27 #include "mpegvideo.h"
28 #include "vdpau.h"
29 #include "vdpau_internal.h"
30
31 static int vdpau_mpeg_start_frame(AVCodecContext *avctx,
32                                   const uint8_t *buffer, uint32_t size)
33 {
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;
38     VdpVideoSurface ref;
39     int i;
40
41     /* fill VdpPictureInfoMPEG1Or2 struct */
42     info->forward_reference  = VDP_INVALID_HANDLE;
43     info->backward_reference = VDP_INVALID_HANDLE;
44
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;
54     }
55
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];
77     }
78
79     return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
80 }
81
82 static int vdpau_mpeg_decode_slice(AVCodecContext *avctx,
83                                    const uint8_t *buffer, uint32_t size)
84 {
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;
88     int val;
89
90     val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
91     if (val < 0)
92         return val;
93
94     pic_ctx->info.mpeg.slice_count++;
95     return 0;
96 }
97
98 #if CONFIG_MPEG1_VDPAU_HWACCEL
99 static int vdpau_mpeg1_init(AVCodecContext *avctx)
100 {
101     return ff_vdpau_common_init(avctx, VDP_DECODER_PROFILE_MPEG1,
102                                 VDP_DECODER_LEVEL_MPEG1_NA);
103 }
104
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),
117 };
118 #endif
119
120 #if CONFIG_MPEG2_VDPAU_HWACCEL
121 static int vdpau_mpeg2_init(AVCodecContext *avctx)
122 {
123     VdpDecoderProfile profile;
124
125     switch (avctx->profile) {
126     case FF_PROFILE_MPEG2_MAIN:
127         profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
128         break;
129     case FF_PROFILE_MPEG2_SIMPLE:
130         profile = VDP_DECODER_PROFILE_MPEG2_SIMPLE;
131         break;
132     default:
133         return AVERROR(EINVAL);
134     }
135
136     return ff_vdpau_common_init(avctx, profile, VDP_DECODER_LEVEL_MPEG2_HL);
137 }
138
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),
151 };
152 #endif