vaapi_vc1: Constify pointers
[libav.git] / libavcodec / vaapi_vc1.c
CommitLineData
68367bde
GB
1/*
2 * VC-1 HW decode acceleration through VA API
3 *
4 * Copyright (C) 2008-2009 Splitted-Desktop Systems
5 *
2912e87a 6 * This file is part of Libav.
68367bde 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
68367bde
GB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
68367bde
GB
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
68367bde
GB
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "vaapi_internal.h"
5aed1d42 24#include "internal.h"
68367bde
GB
25#include "vc1.h"
26#include "vc1data.h"
27
6001dad6 28/** Translate Libav MV modes to VA API */
68367bde
GB
29static int get_VAMvModeVC1(enum MVModes mv_mode)
30{
31 switch (mv_mode) {
32 case MV_PMODE_1MV_HPEL_BILIN: return VAMvMode1MvHalfPelBilinear;
33 case MV_PMODE_1MV: return VAMvMode1Mv;
34 case MV_PMODE_1MV_HPEL: return VAMvMode1MvHalfPel;
35 case MV_PMODE_MIXED_MV: return VAMvModeMixedMv;
36 case MV_PMODE_INTENSITY_COMP: return VAMvModeIntensityCompensation;
37 }
38 return 0;
39}
40
49bd8e4b 41/** Check whether the MVTYPEMB bitplane is present */
01d6f84f 42static inline int vc1_has_MVTYPEMB_bitplane(const VC1Context *v)
68367bde
GB
43{
44 if (v->mv_type_is_raw)
45 return 0;
d4b63054
DB
46 return v->s.pict_type == AV_PICTURE_TYPE_P &&
47 (v->mv_mode == MV_PMODE_MIXED_MV ||
48 (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
49 v->mv_mode2 == MV_PMODE_MIXED_MV));
68367bde
GB
50}
51
49bd8e4b 52/** Check whether the SKIPMB bitplane is present */
01d6f84f 53static inline int vc1_has_SKIPMB_bitplane(const VC1Context *v)
68367bde
GB
54{
55 if (v->skip_is_raw)
56 return 0;
d4b63054
DB
57 return v->s.pict_type == AV_PICTURE_TYPE_P ||
58 (v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type);
68367bde
GB
59}
60
49bd8e4b 61/** Check whether the DIRECTMB bitplane is present */
01d6f84f 62static inline int vc1_has_DIRECTMB_bitplane(const VC1Context *v)
68367bde
GB
63{
64 if (v->dmb_is_raw)
65 return 0;
975a1447 66 return v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type;
68367bde
GB
67}
68
49bd8e4b 69/** Check whether the ACPRED bitplane is present */
01d6f84f 70static inline int vc1_has_ACPRED_bitplane(const VC1Context *v)
68367bde
GB
71{
72 if (v->acpred_is_raw)
73 return 0;
d4b63054
DB
74 return v->profile == PROFILE_ADVANCED &&
75 (v->s.pict_type == AV_PICTURE_TYPE_I ||
76 (v->s.pict_type == AV_PICTURE_TYPE_B && v->bi_type));
68367bde
GB
77}
78
79/** Check whether the OVERFLAGS bitplane is present */
01d6f84f 80static inline int vc1_has_OVERFLAGS_bitplane(const VC1Context *v)
68367bde
GB
81{
82 if (v->overflg_is_raw)
83 return 0;
d4b63054
DB
84 return v->profile == PROFILE_ADVANCED &&
85 (v->s.pict_type == AV_PICTURE_TYPE_I ||
86 (v->s.pict_type == AV_PICTURE_TYPE_B && v->bi_type)) &&
87 (v->overlap && v->pq <= 8) &&
88 v->condover == CONDOVER_SELECT;
68367bde
GB
89}
90
91/** Reconstruct bitstream PTYPE (7.1.1.4, index into Table-35) */
01d6f84f 92static int vc1_get_PTYPE(const VC1Context *v)
68367bde 93{
01d6f84f 94 const MpegEncContext *s = &v->s;
68367bde 95 switch (s->pict_type) {
975a1447
SS
96 case AV_PICTURE_TYPE_I: return 0;
97 case AV_PICTURE_TYPE_P: return v->p_frame_skipped ? 4 : 1;
98 case AV_PICTURE_TYPE_B: return v->bi_type ? 3 : 2;
68367bde
GB
99 }
100 return 0;
101}
102
103/** Reconstruct bitstream MVMODE (7.1.1.32) */
01d6f84f 104static inline VAMvModeVC1 vc1_get_MVMODE(const VC1Context *v)
68367bde 105{
975a1447
SS
106 if (v->s.pict_type == AV_PICTURE_TYPE_P ||
107 (v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type))
68367bde
GB
108 return get_VAMvModeVC1(v->mv_mode);
109 return 0;
110}
111
112/** Reconstruct bitstream MVMODE2 (7.1.1.33) */
01d6f84f 113static inline VAMvModeVC1 vc1_get_MVMODE2(const VC1Context *v)
68367bde 114{
975a1447 115 if (v->s.pict_type == AV_PICTURE_TYPE_P && v->mv_mode == MV_PMODE_INTENSITY_COMP)
68367bde
GB
116 return get_VAMvModeVC1(v->mv_mode2);
117 return 0;
118}
119
53efb758 120/** Reconstruct bitstream TTFRM (7.1.1.41, Table-53) */
01d6f84f 121static inline int vc1_get_TTFRM(const VC1Context *v)
53efb758
GB
122{
123 switch (v->ttfrm) {
124 case TT_8X8: return 0;
125 case TT_8X4: return 1;
126 case TT_4X8: return 2;
127 case TT_4X4: return 3;
128 }
129 return 0;
130}
131
6001dad6 132/** Pack Libav bitplanes into a VABitPlaneBuffer element */
81774dd0 133static inline void vc1_pack_bitplanes(uint8_t *bitplane, int n, const uint8_t *ff_bp[3], int x, int y, int stride)
68367bde 134{
81774dd0
GB
135 const int bitplane_index = n / 2;
136 const int ff_bp_index = y * stride + x;
68367bde
GB
137 uint8_t v = 0;
138 if (ff_bp[0])
81774dd0 139 v = ff_bp[0][ff_bp_index];
68367bde 140 if (ff_bp[1])
81774dd0 141 v |= ff_bp[1][ff_bp_index] << 1;
68367bde 142 if (ff_bp[2])
81774dd0
GB
143 v |= ff_bp[2][ff_bp_index] << 2;
144 bitplane[bitplane_index] = (bitplane[bitplane_index] << 4) | v;
68367bde
GB
145}
146
147static int vaapi_vc1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
148{
01d6f84f
MT
149 const VC1Context *v = avctx->priv_data;
150 const MpegEncContext *s = &v->s;
68367bde
GB
151 struct vaapi_context * const vactx = avctx->hwaccel_context;
152 VAPictureParameterBufferVC1 *pic_param;
153
68367bde
GB
154 vactx->slice_param_size = sizeof(VASliceParameterBufferVC1);
155
156 /* Fill in VAPictureParameterBufferVC1 */
8b086712 157 pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferVC1));
68367bde
GB
158 if (!pic_param)
159 return -1;
e6d3534f
GB
160 pic_param->forward_reference_picture = VA_INVALID_ID;
161 pic_param->backward_reference_picture = VA_INVALID_ID;
162 pic_param->inloop_decoded_picture = VA_INVALID_ID;
68367bde
GB
163 pic_param->sequence_fields.value = 0; /* reset all bits */
164 pic_param->sequence_fields.bits.pulldown = v->broadcast;
165 pic_param->sequence_fields.bits.interlace = v->interlace;
166 pic_param->sequence_fields.bits.tfcntrflag = v->tfcntrflag;
167 pic_param->sequence_fields.bits.finterpflag = v->finterpflag;
168 pic_param->sequence_fields.bits.psf = v->psf;
169 pic_param->sequence_fields.bits.multires = v->multires;
170 pic_param->sequence_fields.bits.overlap = v->overlap;
c2f7417e 171 pic_param->sequence_fields.bits.syncmarker = v->resync_marker;
68367bde
GB
172 pic_param->sequence_fields.bits.rangered = v->rangered;
173 pic_param->sequence_fields.bits.max_b_frames = s->avctx->max_b_frames;
da21440d
GB
174#if VA_CHECK_VERSION(0,32,0)
175 pic_param->sequence_fields.bits.profile = v->profile;
176#endif
68367bde
GB
177 pic_param->coded_width = s->avctx->coded_width;
178 pic_param->coded_height = s->avctx->coded_height;
179 pic_param->entrypoint_fields.value = 0; /* reset all bits */
180 pic_param->entrypoint_fields.bits.broken_link = v->broken_link;
181 pic_param->entrypoint_fields.bits.closed_entry = v->closed_entry;
182 pic_param->entrypoint_fields.bits.panscan_flag = v->panscanflag;
183 pic_param->entrypoint_fields.bits.loopfilter = s->loop_filter;
184 pic_param->conditional_overlap_flag = v->condover;
185 pic_param->fast_uvmc_flag = v->fastuvmc;
186 pic_param->range_mapping_fields.value = 0; /* reset all bits */
187 pic_param->range_mapping_fields.bits.luma_flag = v->range_mapy_flag;
188 pic_param->range_mapping_fields.bits.luma = v->range_mapy;
189 pic_param->range_mapping_fields.bits.chroma_flag = v->range_mapuv_flag;
190 pic_param->range_mapping_fields.bits.chroma = v->range_mapuv;
191 pic_param->b_picture_fraction = v->bfraction_lut_index;
192 pic_param->cbp_table = v->cbpcy_vlc ? v->cbpcy_vlc - ff_vc1_cbpcy_p_vlc : 0;
193 pic_param->mb_mode_table = 0; /* XXX: interlaced frame */
194 pic_param->range_reduction_frame = v->rangeredfrm;
195 pic_param->rounding_control = v->rnd;
196 pic_param->post_processing = v->postproc;
197 pic_param->picture_resolution_index = v->respic;
198 pic_param->luma_scale = v->lumscale;
199 pic_param->luma_shift = v->lumshift;
200 pic_param->picture_fields.value = 0; /* reset all bits */
201 pic_param->picture_fields.bits.picture_type = vc1_get_PTYPE(v);
202 pic_param->picture_fields.bits.frame_coding_mode = v->fcm;
203 pic_param->picture_fields.bits.top_field_first = v->tff;
204 pic_param->picture_fields.bits.is_first_field = v->fcm == 0; /* XXX: interlaced frame */
205 pic_param->picture_fields.bits.intensity_compensation = v->mv_mode == MV_PMODE_INTENSITY_COMP;
206 pic_param->raw_coding.value = 0; /* reset all bits */
207 pic_param->raw_coding.flags.mv_type_mb = v->mv_type_is_raw;
208 pic_param->raw_coding.flags.direct_mb = v->dmb_is_raw;
209 pic_param->raw_coding.flags.skip_mb = v->skip_is_raw;
210 pic_param->raw_coding.flags.field_tx = 0; /* XXX: interlaced frame */
211 pic_param->raw_coding.flags.forward_mb = 0; /* XXX: interlaced frame */
212 pic_param->raw_coding.flags.ac_pred = v->acpred_is_raw;
213 pic_param->raw_coding.flags.overflags = v->overflg_is_raw;
214 pic_param->bitplane_present.value = 0; /* reset all bits */
215 pic_param->bitplane_present.flags.bp_mv_type_mb = vc1_has_MVTYPEMB_bitplane(v);
216 pic_param->bitplane_present.flags.bp_direct_mb = vc1_has_DIRECTMB_bitplane(v);
217 pic_param->bitplane_present.flags.bp_skip_mb = vc1_has_SKIPMB_bitplane(v);
218 pic_param->bitplane_present.flags.bp_field_tx = 0; /* XXX: interlaced frame */
219 pic_param->bitplane_present.flags.bp_forward_mb = 0; /* XXX: interlaced frame */
220 pic_param->bitplane_present.flags.bp_ac_pred = vc1_has_ACPRED_bitplane(v);
221 pic_param->bitplane_present.flags.bp_overflags = vc1_has_OVERFLAGS_bitplane(v);
222 pic_param->reference_fields.value = 0; /* reset all bits */
223 pic_param->reference_fields.bits.reference_distance_flag = v->refdist_flag;
224 pic_param->reference_fields.bits.reference_distance = 0; /* XXX: interlaced frame */
225 pic_param->reference_fields.bits.num_reference_pictures = 0; /* XXX: interlaced frame */
226 pic_param->reference_fields.bits.reference_field_pic_indicator = 0; /* XXX: interlaced frame */
227 pic_param->mv_fields.value = 0; /* reset all bits */
228 pic_param->mv_fields.bits.mv_mode = vc1_get_MVMODE(v);
229 pic_param->mv_fields.bits.mv_mode2 = vc1_get_MVMODE2(v);
230 pic_param->mv_fields.bits.mv_table = s->mv_table_index;
231 pic_param->mv_fields.bits.two_mv_block_pattern_table = 0; /* XXX: interlaced frame */
232 pic_param->mv_fields.bits.four_mv_switch = 0; /* XXX: interlaced frame */
233 pic_param->mv_fields.bits.four_mv_block_pattern_table = 0; /* XXX: interlaced frame */
234 pic_param->mv_fields.bits.extended_mv_flag = v->extended_mv;
235 pic_param->mv_fields.bits.extended_mv_range = v->mvrange;
236 pic_param->mv_fields.bits.extended_dmv_flag = v->extended_dmv;
237 pic_param->mv_fields.bits.extended_dmv_range = 0; /* XXX: interlaced frame */
238 pic_param->pic_quantizer_fields.value = 0; /* reset all bits */
239 pic_param->pic_quantizer_fields.bits.dquant = v->dquant;
240 pic_param->pic_quantizer_fields.bits.quantizer = v->quantizer_mode;
241 pic_param->pic_quantizer_fields.bits.half_qp = v->halfpq;
242 pic_param->pic_quantizer_fields.bits.pic_quantizer_scale = v->pq;
243 pic_param->pic_quantizer_fields.bits.pic_quantizer_type = v->pquantizer;
244 pic_param->pic_quantizer_fields.bits.dq_frame = v->dquantfrm;
245 pic_param->pic_quantizer_fields.bits.dq_profile = v->dqprofile;
246 pic_param->pic_quantizer_fields.bits.dq_sb_edge = v->dqprofile == DQPROFILE_SINGLE_EDGE ? v->dqsbedge : 0;
247 pic_param->pic_quantizer_fields.bits.dq_db_edge = v->dqprofile == DQPROFILE_DOUBLE_EDGES ? v->dqsbedge : 0;
248 pic_param->pic_quantizer_fields.bits.dq_binary_level = v->dqbilevel;
249 pic_param->pic_quantizer_fields.bits.alt_pic_quantizer = v->altpq;
250 pic_param->transform_fields.value = 0; /* reset all bits */
251 pic_param->transform_fields.bits.variable_sized_transform_flag = v->vstransform;
252 pic_param->transform_fields.bits.mb_level_transform_type_flag = v->ttmbf;
53efb758 253 pic_param->transform_fields.bits.frame_level_transform_type = vc1_get_TTFRM(v);
68367bde
GB
254 pic_param->transform_fields.bits.transform_ac_codingset_idx1 = v->c_ac_table_index;
255 pic_param->transform_fields.bits.transform_ac_codingset_idx2 = v->y_ac_table_index;
256 pic_param->transform_fields.bits.intra_transform_dc_table = v->s.dc_table_index;
257
258 switch (s->pict_type) {
975a1447 259 case AV_PICTURE_TYPE_B:
f6774f90 260 pic_param->backward_reference_picture = ff_vaapi_get_surface_id(s->next_picture.f);
68367bde 261 // fall-through
975a1447 262 case AV_PICTURE_TYPE_P:
f6774f90 263 pic_param->forward_reference_picture = ff_vaapi_get_surface_id(s->last_picture.f);
68367bde
GB
264 break;
265 }
266
267 if (pic_param->bitplane_present.value) {
268 uint8_t *bitplane;
269 const uint8_t *ff_bp[3];
270 int x, y, n;
271
272 switch (s->pict_type) {
975a1447 273 case AV_PICTURE_TYPE_P:
68367bde
GB
274 ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
275 ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
276 ff_bp[2] = pic_param->bitplane_present.flags.bp_mv_type_mb ? v->mv_type_mb_plane : NULL;
277 break;
975a1447 278 case AV_PICTURE_TYPE_B:
68367bde
GB
279 if (!v->bi_type) {
280 ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
281 ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
282 ff_bp[2] = NULL; /* XXX: interlaced frame (FORWARD plane) */
283 break;
284 }
285 /* fall-through (BI-type) */
975a1447 286 case AV_PICTURE_TYPE_I:
68367bde
GB
287 ff_bp[0] = NULL; /* XXX: interlaced frame (FIELDTX plane) */
288 ff_bp[1] = pic_param->bitplane_present.flags.bp_ac_pred ? v->acpred_plane : NULL;
289 ff_bp[2] = pic_param->bitplane_present.flags.bp_overflags ? v->over_flags_plane : NULL;
290 break;
291 default:
292 ff_bp[0] = NULL;
293 ff_bp[1] = NULL;
294 ff_bp[2] = NULL;
295 break;
296 }
297
81774dd0 298 bitplane = ff_vaapi_alloc_bitplane(vactx, (s->mb_width * s->mb_height + 1) / 2);
68367bde
GB
299 if (!bitplane)
300 return -1;
301
302 n = 0;
81774dd0
GB
303 for (y = 0; y < s->mb_height; y++)
304 for (x = 0; x < s->mb_width; x++, n++)
305 vc1_pack_bitplanes(bitplane, n, ff_bp, x, y, s->mb_stride);
306 if (n & 1) /* move last nibble to the high order */
307 bitplane[n/2] <<= 4;
68367bde
GB
308 }
309 return 0;
310}
311
68367bde
GB
312static int vaapi_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
313{
01d6f84f
MT
314 const VC1Context *v = avctx->priv_data;
315 const MpegEncContext *s = &v->s;
68367bde
GB
316 VASliceParameterBufferVC1 *slice_param;
317
68367bde 318 /* Current bit buffer is beyond any marker for VC-1, so skip it */
36ef5369 319 if (avctx->codec_id == AV_CODEC_ID_VC1 && IS_MARKER(AV_RB32(buffer))) {
68367bde
GB
320 buffer += 4;
321 size -= 4;
322 }
323
324 /* Fill in VASliceParameterBufferVC1 */
325 slice_param = (VASliceParameterBufferVC1 *)ff_vaapi_alloc_slice(avctx->hwaccel_context, buffer, size);
326 if (!slice_param)
327 return -1;
328 slice_param->macroblock_offset = get_bits_count(&s->gb);
329 slice_param->slice_vertical_position = s->mb_y;
330 return 0;
331}
332
333#if CONFIG_WMV3_VAAPI_HWACCEL
d1b6f33b 334AVHWAccel ff_wmv3_vaapi_hwaccel = {
68367bde 335 .name = "wmv3_vaapi",
72415b2a 336 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 337 .id = AV_CODEC_ID_WMV3,
d264c720 338 .pix_fmt = AV_PIX_FMT_VAAPI,
68367bde 339 .start_frame = vaapi_vc1_start_frame,
2c541554 340 .end_frame = ff_vaapi_mpeg_end_frame,
68367bde 341 .decode_slice = vaapi_vc1_decode_slice,
68367bde
GB
342};
343#endif
344
d1b6f33b 345AVHWAccel ff_vc1_vaapi_hwaccel = {
68367bde 346 .name = "vc1_vaapi",
72415b2a 347 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 348 .id = AV_CODEC_ID_VC1,
d264c720 349 .pix_fmt = AV_PIX_FMT_VAAPI,
68367bde 350 .start_frame = vaapi_vc1_start_frame,
2c541554 351 .end_frame = ff_vaapi_mpeg_end_frame,
68367bde 352 .decode_slice = vaapi_vc1_decode_slice,
68367bde 353};