Replace 5 with AOT_SBR when referring to the MPEG-4 audio object type.
[FFMpeg-mirror/lagarith.git] / libavcodec / vaapi_vc1.c
blobeae3c160fc180a14dd8ff1ea7ab2fc2133892d5b
1 /*
2 * VC-1 HW decode acceleration through VA API
4 * Copyright (C) 2008-2009 Splitted-Desktop Systems
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
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.
13 * FFmpeg is distributed in the hope that it will be useful,
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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "vaapi_internal.h"
24 #include "vc1.h"
25 #include "vc1data.h"
27 /** Translates FFmpeg MV modes to VA API */
28 static int get_VAMvModeVC1(enum MVModes mv_mode)
30 switch (mv_mode) {
31 case MV_PMODE_1MV_HPEL_BILIN: return VAMvMode1MvHalfPelBilinear;
32 case MV_PMODE_1MV: return VAMvMode1Mv;
33 case MV_PMODE_1MV_HPEL: return VAMvMode1MvHalfPel;
34 case MV_PMODE_MIXED_MV: return VAMvModeMixedMv;
35 case MV_PMODE_INTENSITY_COMP: return VAMvModeIntensityCompensation;
37 return 0;
40 /** Checks whether the MVTYPEMB bitplane is present */
41 static inline int vc1_has_MVTYPEMB_bitplane(VC1Context *v)
43 if (v->mv_type_is_raw)
44 return 0;
45 return (v->s.pict_type == FF_P_TYPE &&
46 (v->mv_mode == MV_PMODE_MIXED_MV ||
47 (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
48 v->mv_mode2 == MV_PMODE_MIXED_MV)));
51 /** Checks whether the SKIPMB bitplane is present */
52 static inline int vc1_has_SKIPMB_bitplane(VC1Context *v)
54 if (v->skip_is_raw)
55 return 0;
56 return (v->s.pict_type == FF_P_TYPE ||
57 (v->s.pict_type == FF_B_TYPE && !v->bi_type));
60 /** Checks whether the DIRECTMB bitplane is present */
61 static inline int vc1_has_DIRECTMB_bitplane(VC1Context *v)
63 if (v->dmb_is_raw)
64 return 0;
65 return v->s.pict_type == FF_B_TYPE && !v->bi_type;
68 /** Checks whether the ACPRED bitplane is present */
69 static inline int vc1_has_ACPRED_bitplane(VC1Context *v)
71 if (v->acpred_is_raw)
72 return 0;
73 return (v->profile == PROFILE_ADVANCED &&
74 (v->s.pict_type == FF_I_TYPE ||
75 (v->s.pict_type == FF_B_TYPE && v->bi_type)));
78 /** Check whether the OVERFLAGS bitplane is present */
79 static inline int vc1_has_OVERFLAGS_bitplane(VC1Context *v)
81 if (v->overflg_is_raw)
82 return 0;
83 return (v->profile == PROFILE_ADVANCED &&
84 (v->s.pict_type == FF_I_TYPE ||
85 (v->s.pict_type == FF_B_TYPE && v->bi_type)) &&
86 (v->overlap && v->pq <= 8) &&
87 v->condover == CONDOVER_SELECT);
90 /** Reconstruct bitstream PTYPE (7.1.1.4, index into Table-35) */
91 static int vc1_get_PTYPE(VC1Context *v)
93 MpegEncContext * const s = &v->s;
94 switch (s->pict_type) {
95 case FF_I_TYPE: return 0;
96 case FF_P_TYPE: return v->p_frame_skipped ? 4 : 1;
97 case FF_B_TYPE: return v->bi_type ? 3 : 2;
99 return 0;
102 /** Reconstruct bitstream MVMODE (7.1.1.32) */
103 static inline VAMvModeVC1 vc1_get_MVMODE(VC1Context *v)
105 if (v->s.pict_type == FF_P_TYPE ||
106 (v->s.pict_type == FF_B_TYPE && !v->bi_type))
107 return get_VAMvModeVC1(v->mv_mode);
108 return 0;
111 /** Reconstruct bitstream MVMODE2 (7.1.1.33) */
112 static inline VAMvModeVC1 vc1_get_MVMODE2(VC1Context *v)
114 if (v->s.pict_type == FF_P_TYPE && v->mv_mode == MV_PMODE_INTENSITY_COMP)
115 return get_VAMvModeVC1(v->mv_mode2);
116 return 0;
119 /** Pack FFmpeg bitplanes into a VABitPlaneBuffer element */
120 static inline uint8_t vc1_pack_bitplanes(const uint8_t *ff_bp[3], int x, int y, int stride)
122 const int n = y * stride + x;
123 uint8_t v = 0;
124 if (ff_bp[0])
125 v = ff_bp[0][n];
126 if (ff_bp[1])
127 v |= ff_bp[1][n] << 1;
128 if (ff_bp[2])
129 v |= ff_bp[2][n] << 2;
130 return v;
133 static int vaapi_vc1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
135 VC1Context * const v = avctx->priv_data;
136 MpegEncContext * const s = &v->s;
137 struct vaapi_context * const vactx = avctx->hwaccel_context;
138 VAPictureParameterBufferVC1 *pic_param;
140 dprintf(avctx, "vaapi_vc1_start_frame()\n");
142 vactx->slice_param_size = sizeof(VASliceParameterBufferVC1);
144 /* Fill in VAPictureParameterBufferVC1 */
145 pic_param = ff_vaapi_alloc_picture(vactx, sizeof(VAPictureParameterBufferVC1));
146 if (!pic_param)
147 return -1;
148 pic_param->forward_reference_picture = 0xffffffff;
149 pic_param->backward_reference_picture = 0xffffffff;
150 pic_param->inloop_decoded_picture = 0xffffffff;
151 pic_param->sequence_fields.value = 0; /* reset all bits */
152 pic_param->sequence_fields.bits.pulldown = v->broadcast;
153 pic_param->sequence_fields.bits.interlace = v->interlace;
154 pic_param->sequence_fields.bits.tfcntrflag = v->tfcntrflag;
155 pic_param->sequence_fields.bits.finterpflag = v->finterpflag;
156 pic_param->sequence_fields.bits.psf = v->psf;
157 pic_param->sequence_fields.bits.multires = v->multires;
158 pic_param->sequence_fields.bits.overlap = v->overlap;
159 pic_param->sequence_fields.bits.syncmarker = s->resync_marker;
160 pic_param->sequence_fields.bits.rangered = v->rangered;
161 pic_param->sequence_fields.bits.max_b_frames = s->avctx->max_b_frames;
162 pic_param->coded_width = s->avctx->coded_width;
163 pic_param->coded_height = s->avctx->coded_height;
164 pic_param->entrypoint_fields.value = 0; /* reset all bits */
165 pic_param->entrypoint_fields.bits.broken_link = v->broken_link;
166 pic_param->entrypoint_fields.bits.closed_entry = v->closed_entry;
167 pic_param->entrypoint_fields.bits.panscan_flag = v->panscanflag;
168 pic_param->entrypoint_fields.bits.loopfilter = s->loop_filter;
169 pic_param->conditional_overlap_flag = v->condover;
170 pic_param->fast_uvmc_flag = v->fastuvmc;
171 pic_param->range_mapping_fields.value = 0; /* reset all bits */
172 pic_param->range_mapping_fields.bits.luma_flag = v->range_mapy_flag;
173 pic_param->range_mapping_fields.bits.luma = v->range_mapy;
174 pic_param->range_mapping_fields.bits.chroma_flag = v->range_mapuv_flag;
175 pic_param->range_mapping_fields.bits.chroma = v->range_mapuv;
176 pic_param->b_picture_fraction = v->bfraction_lut_index;
177 pic_param->cbp_table = v->cbpcy_vlc ? v->cbpcy_vlc - ff_vc1_cbpcy_p_vlc : 0;
178 pic_param->mb_mode_table = 0; /* XXX: interlaced frame */
179 pic_param->range_reduction_frame = v->rangeredfrm;
180 pic_param->rounding_control = v->rnd;
181 pic_param->post_processing = v->postproc;
182 pic_param->picture_resolution_index = v->respic;
183 pic_param->luma_scale = v->lumscale;
184 pic_param->luma_shift = v->lumshift;
185 pic_param->picture_fields.value = 0; /* reset all bits */
186 pic_param->picture_fields.bits.picture_type = vc1_get_PTYPE(v);
187 pic_param->picture_fields.bits.frame_coding_mode = v->fcm;
188 pic_param->picture_fields.bits.top_field_first = v->tff;
189 pic_param->picture_fields.bits.is_first_field = v->fcm == 0; /* XXX: interlaced frame */
190 pic_param->picture_fields.bits.intensity_compensation = v->mv_mode == MV_PMODE_INTENSITY_COMP;
191 pic_param->raw_coding.value = 0; /* reset all bits */
192 pic_param->raw_coding.flags.mv_type_mb = v->mv_type_is_raw;
193 pic_param->raw_coding.flags.direct_mb = v->dmb_is_raw;
194 pic_param->raw_coding.flags.skip_mb = v->skip_is_raw;
195 pic_param->raw_coding.flags.field_tx = 0; /* XXX: interlaced frame */
196 pic_param->raw_coding.flags.forward_mb = 0; /* XXX: interlaced frame */
197 pic_param->raw_coding.flags.ac_pred = v->acpred_is_raw;
198 pic_param->raw_coding.flags.overflags = v->overflg_is_raw;
199 pic_param->bitplane_present.value = 0; /* reset all bits */
200 pic_param->bitplane_present.flags.bp_mv_type_mb = vc1_has_MVTYPEMB_bitplane(v);
201 pic_param->bitplane_present.flags.bp_direct_mb = vc1_has_DIRECTMB_bitplane(v);
202 pic_param->bitplane_present.flags.bp_skip_mb = vc1_has_SKIPMB_bitplane(v);
203 pic_param->bitplane_present.flags.bp_field_tx = 0; /* XXX: interlaced frame */
204 pic_param->bitplane_present.flags.bp_forward_mb = 0; /* XXX: interlaced frame */
205 pic_param->bitplane_present.flags.bp_ac_pred = vc1_has_ACPRED_bitplane(v);
206 pic_param->bitplane_present.flags.bp_overflags = vc1_has_OVERFLAGS_bitplane(v);
207 pic_param->reference_fields.value = 0; /* reset all bits */
208 pic_param->reference_fields.bits.reference_distance_flag = v->refdist_flag;
209 pic_param->reference_fields.bits.reference_distance = 0; /* XXX: interlaced frame */
210 pic_param->reference_fields.bits.num_reference_pictures = 0; /* XXX: interlaced frame */
211 pic_param->reference_fields.bits.reference_field_pic_indicator = 0; /* XXX: interlaced frame */
212 pic_param->mv_fields.value = 0; /* reset all bits */
213 pic_param->mv_fields.bits.mv_mode = vc1_get_MVMODE(v);
214 pic_param->mv_fields.bits.mv_mode2 = vc1_get_MVMODE2(v);
215 pic_param->mv_fields.bits.mv_table = s->mv_table_index;
216 pic_param->mv_fields.bits.two_mv_block_pattern_table = 0; /* XXX: interlaced frame */
217 pic_param->mv_fields.bits.four_mv_switch = 0; /* XXX: interlaced frame */
218 pic_param->mv_fields.bits.four_mv_block_pattern_table = 0; /* XXX: interlaced frame */
219 pic_param->mv_fields.bits.extended_mv_flag = v->extended_mv;
220 pic_param->mv_fields.bits.extended_mv_range = v->mvrange;
221 pic_param->mv_fields.bits.extended_dmv_flag = v->extended_dmv;
222 pic_param->mv_fields.bits.extended_dmv_range = 0; /* XXX: interlaced frame */
223 pic_param->pic_quantizer_fields.value = 0; /* reset all bits */
224 pic_param->pic_quantizer_fields.bits.dquant = v->dquant;
225 pic_param->pic_quantizer_fields.bits.quantizer = v->quantizer_mode;
226 pic_param->pic_quantizer_fields.bits.half_qp = v->halfpq;
227 pic_param->pic_quantizer_fields.bits.pic_quantizer_scale = v->pq;
228 pic_param->pic_quantizer_fields.bits.pic_quantizer_type = v->pquantizer;
229 pic_param->pic_quantizer_fields.bits.dq_frame = v->dquantfrm;
230 pic_param->pic_quantizer_fields.bits.dq_profile = v->dqprofile;
231 pic_param->pic_quantizer_fields.bits.dq_sb_edge = v->dqprofile == DQPROFILE_SINGLE_EDGE ? v->dqsbedge : 0;
232 pic_param->pic_quantizer_fields.bits.dq_db_edge = v->dqprofile == DQPROFILE_DOUBLE_EDGES ? v->dqsbedge : 0;
233 pic_param->pic_quantizer_fields.bits.dq_binary_level = v->dqbilevel;
234 pic_param->pic_quantizer_fields.bits.alt_pic_quantizer = v->altpq;
235 pic_param->transform_fields.value = 0; /* reset all bits */
236 pic_param->transform_fields.bits.variable_sized_transform_flag = v->vstransform;
237 pic_param->transform_fields.bits.mb_level_transform_type_flag = v->ttmbf;
238 pic_param->transform_fields.bits.frame_level_transform_type = v->ttfrm;
239 pic_param->transform_fields.bits.transform_ac_codingset_idx1 = v->c_ac_table_index;
240 pic_param->transform_fields.bits.transform_ac_codingset_idx2 = v->y_ac_table_index;
241 pic_param->transform_fields.bits.intra_transform_dc_table = v->s.dc_table_index;
243 switch (s->pict_type) {
244 case FF_B_TYPE:
245 pic_param->backward_reference_picture = ff_vaapi_get_surface(&s->next_picture);
246 // fall-through
247 case FF_P_TYPE:
248 pic_param->forward_reference_picture = ff_vaapi_get_surface(&s->last_picture);
249 break;
252 if (pic_param->bitplane_present.value) {
253 uint8_t *bitplane;
254 const uint8_t *ff_bp[3];
255 int x, y, n;
257 switch (s->pict_type) {
258 case FF_P_TYPE:
259 ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
260 ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
261 ff_bp[2] = pic_param->bitplane_present.flags.bp_mv_type_mb ? v->mv_type_mb_plane : NULL;
262 break;
263 case FF_B_TYPE:
264 if (!v->bi_type) {
265 ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
266 ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
267 ff_bp[2] = NULL; /* XXX: interlaced frame (FORWARD plane) */
268 break;
270 /* fall-through (BI-type) */
271 case FF_I_TYPE:
272 ff_bp[0] = NULL; /* XXX: interlaced frame (FIELDTX plane) */
273 ff_bp[1] = pic_param->bitplane_present.flags.bp_ac_pred ? v->acpred_plane : NULL;
274 ff_bp[2] = pic_param->bitplane_present.flags.bp_overflags ? v->over_flags_plane : NULL;
275 break;
276 default:
277 ff_bp[0] = NULL;
278 ff_bp[1] = NULL;
279 ff_bp[2] = NULL;
280 break;
283 bitplane = ff_vaapi_alloc_bitplane(vactx, s->mb_height * ((s->mb_width + 1) / 2));
284 if (!bitplane)
285 return -1;
287 n = 0;
288 for (y = 0; y < s->mb_height; y++) {
289 for (x = 0; x < s->mb_width; x += 2) {
290 bitplane[n] = vc1_pack_bitplanes(ff_bp, x+1, y, s->mb_stride);
291 bitplane[n] |= (vc1_pack_bitplanes(ff_bp, x, y, s->mb_stride) << 4);
292 ++n;
296 return 0;
299 static int vaapi_vc1_end_frame(AVCodecContext *avctx)
301 VC1Context * const v = avctx->priv_data;
303 return ff_vaapi_common_end_frame(&v->s);
306 static int vaapi_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
308 VC1Context * const v = avctx->priv_data;
309 MpegEncContext * const s = &v->s;
310 VASliceParameterBufferVC1 *slice_param;
312 dprintf(avctx, "vaapi_vc1_decode_slice(): buffer %p, size %d\n", buffer, size);
314 /* Current bit buffer is beyond any marker for VC-1, so skip it */
315 if (avctx->codec_id == CODEC_ID_VC1 && IS_MARKER(AV_RB32(buffer))) {
316 buffer += 4;
317 size -= 4;
320 /* Fill in VASliceParameterBufferVC1 */
321 slice_param = (VASliceParameterBufferVC1 *)ff_vaapi_alloc_slice(avctx->hwaccel_context, buffer, size);
322 if (!slice_param)
323 return -1;
324 slice_param->macroblock_offset = get_bits_count(&s->gb);
325 slice_param->slice_vertical_position = s->mb_y;
326 return 0;
329 #if CONFIG_WMV3_VAAPI_HWACCEL
330 AVHWAccel wmv3_vaapi_hwaccel = {
331 .name = "wmv3_vaapi",
332 .type = CODEC_TYPE_VIDEO,
333 .id = CODEC_ID_WMV3,
334 .pix_fmt = PIX_FMT_VAAPI_VLD,
335 .capabilities = 0,
336 .start_frame = vaapi_vc1_start_frame,
337 .end_frame = vaapi_vc1_end_frame,
338 .decode_slice = vaapi_vc1_decode_slice,
339 .priv_data_size = 0,
341 #endif
343 AVHWAccel vc1_vaapi_hwaccel = {
344 .name = "vc1_vaapi",
345 .type = CODEC_TYPE_VIDEO,
346 .id = CODEC_ID_VC1,
347 .pix_fmt = PIX_FMT_VAAPI_VLD,
348 .capabilities = 0,
349 .start_frame = vaapi_vc1_start_frame,
350 .end_frame = vaapi_vc1_end_frame,
351 .decode_slice = vaapi_vc1_decode_slice,
352 .priv_data_size = 0,