]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_vc1.c
Merge commit '8f144d9e3d5cb2ca92e5bdf7cc9f72effa1bd2ce'
[ffmpeg] / libavcodec / vaapi_vc1.c
1 /*
2  * VC-1 HW decode acceleration through VA API
3  *
4  * Copyright (C) 2008-2009 Splitted-Desktop Systems
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "hwaccel.h"
24 #include "internal.h"
25 #include "vaapi_decode.h"
26 #include "vc1.h"
27 #include "vc1data.h"
28
29 /** Translate FFmpeg MV modes to VA API */
30 static int get_VAMvModeVC1(enum MVModes mv_mode)
31 {
32     switch (mv_mode) {
33     case MV_PMODE_1MV_HPEL_BILIN: return VAMvMode1MvHalfPelBilinear;
34     case MV_PMODE_1MV:            return VAMvMode1Mv;
35     case MV_PMODE_1MV_HPEL:       return VAMvMode1MvHalfPel;
36     case MV_PMODE_MIXED_MV:       return VAMvModeMixedMv;
37     case MV_PMODE_INTENSITY_COMP: return VAMvModeIntensityCompensation;
38     }
39     return 0;
40 }
41
42 /** Check whether the MVTYPEMB bitplane is present */
43 static inline int vc1_has_MVTYPEMB_bitplane(const VC1Context *v)
44 {
45     if (v->mv_type_is_raw)
46         return 0;
47     return v->fcm == PROGRESSIVE &&
48            (v->s.pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) &&
49            (v->mv_mode == MV_PMODE_MIXED_MV ||
50             (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
51              v->mv_mode2 == MV_PMODE_MIXED_MV));
52 }
53
54 /** Check whether the SKIPMB bitplane is present */
55 static inline int vc1_has_SKIPMB_bitplane(const VC1Context *v)
56 {
57     if (v->skip_is_raw)
58         return 0;
59     return (v->fcm == PROGRESSIVE || v->fcm == ILACE_FRAME) &&
60            ((v->s.pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) ||
61             (v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type));
62 }
63
64 /** Check whether the DIRECTMB bitplane is present */
65 static inline int vc1_has_DIRECTMB_bitplane(const VC1Context *v)
66 {
67     if (v->dmb_is_raw)
68         return 0;
69     return (v->fcm == PROGRESSIVE || v->fcm == ILACE_FRAME) &&
70            (v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type);
71 }
72
73 /** Check whether the ACPRED bitplane is present */
74 static inline int vc1_has_ACPRED_bitplane(const VC1Context *v)
75 {
76     if (v->acpred_is_raw)
77         return 0;
78     return v->profile == PROFILE_ADVANCED &&
79            (v->s.pict_type == AV_PICTURE_TYPE_I ||
80             (v->s.pict_type == AV_PICTURE_TYPE_B && v->bi_type));
81 }
82
83 /** Check whether the OVERFLAGS bitplane is present */
84 static inline int vc1_has_OVERFLAGS_bitplane(const VC1Context *v)
85 {
86     if (v->overflg_is_raw)
87         return 0;
88     return v->profile == PROFILE_ADVANCED &&
89            (v->s.pict_type == AV_PICTURE_TYPE_I ||
90             (v->s.pict_type == AV_PICTURE_TYPE_B && v->bi_type)) &&
91            (v->overlap && v->pq <= 8) &&
92            v->condover == CONDOVER_SELECT;
93 }
94
95 /** Check whether the FIELDTX bitplane is present */
96 static inline int vc1_has_FIELDTX_bitplane(const VC1Context *v)
97 {
98     if (v->fieldtx_is_raw)
99         return 0;
100     return v->fcm == ILACE_FRAME &&
101            (v->s.pict_type == AV_PICTURE_TYPE_I ||
102             (v->s.pict_type == AV_PICTURE_TYPE_B && v->bi_type));
103 }
104
105 /** Check whether the FORWARDMB bitplane is present */
106 static inline int vc1_has_FORWARDMB_bitplane(const VC1Context *v)
107 {
108     if (v->fmb_is_raw)
109         return 0;
110     return v->fcm == ILACE_FIELD &&
111            (v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type);
112 }
113
114 /** Reconstruct bitstream PTYPE (7.1.1.4, index into Table-35) */
115 static int vc1_get_PTYPE(const VC1Context *v)
116 {
117     const MpegEncContext *s = &v->s;
118     switch (s->pict_type) {
119     case AV_PICTURE_TYPE_I: return 0;
120     case AV_PICTURE_TYPE_P: return v->p_frame_skipped ? 4 : 1;
121     case AV_PICTURE_TYPE_B: return v->bi_type         ? 3 : 2;
122     }
123     return 0;
124 }
125
126 /** Reconstruct bitstream FPTYPE (9.1.1.42, index into Table-105) */
127 static int vc1_get_FPTYPE(const VC1Context *v)
128 {
129     const MpegEncContext *s = &v->s;
130     switch (s->pict_type) {
131     case AV_PICTURE_TYPE_I: return 0;
132     case AV_PICTURE_TYPE_P: return 3;
133     case AV_PICTURE_TYPE_B: return v->bi_type ? 7 : 4;
134     }
135     return 0;
136 }
137
138 /** Reconstruct bitstream MVMODE (7.1.1.32) */
139 static inline VAMvModeVC1 vc1_get_MVMODE(const VC1Context *v)
140 {
141     if ((v->s.pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) ||
142         (v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type))
143         return get_VAMvModeVC1(v->mv_mode);
144     return 0;
145 }
146
147 /** Reconstruct bitstream MVMODE2 (7.1.1.33) */
148 static inline VAMvModeVC1 vc1_get_MVMODE2(const VC1Context *v)
149 {
150     if ((v->s.pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) &&
151         v->mv_mode == MV_PMODE_INTENSITY_COMP)
152         return get_VAMvModeVC1(v->mv_mode2);
153     return 0;
154 }
155
156 av_unused static inline int vc1_get_INTCOMPFIELD(const VC1Context *v)
157 {
158     if ((v->s.pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) &&
159         v->fcm == ILACE_FIELD &&
160         v->mv_mode == MV_PMODE_INTENSITY_COMP)
161         switch (v->intcompfield) {
162         case 1: return 1;
163         case 2: return 2;
164         case 3: return 0;
165         }
166     return 0;
167 }
168
169 static inline int vc1_get_LUMSCALE(const VC1Context *v)
170 {
171     if (v->s.pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) {
172         if ((v->fcm == PROGRESSIVE && v->mv_mode == MV_PMODE_INTENSITY_COMP) ||
173             (v->fcm == ILACE_FRAME && v->intcomp))
174             return v->lumscale;
175         else if (v->fcm == ILACE_FIELD && v->mv_mode == MV_PMODE_INTENSITY_COMP)
176             switch (v->intcompfield) {
177             case 1: return v->lumscale;
178             case 2: return v->lumscale2;
179             case 3: return v->lumscale;
180         }
181     }
182     return 0;
183 }
184
185 static inline int vc1_get_LUMSHIFT(const VC1Context *v)
186 {
187     if (v->s.pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) {
188         if ((v->fcm == PROGRESSIVE && v->mv_mode == MV_PMODE_INTENSITY_COMP) ||
189             (v->fcm == ILACE_FRAME && v->intcomp))
190             return v->lumshift;
191         else if (v->fcm == ILACE_FIELD && v->mv_mode == MV_PMODE_INTENSITY_COMP)
192             switch (v->intcompfield) {
193             case 1: return v->lumshift;
194             case 2: return v->lumshift2;
195             case 3: return v->lumshift;
196         }
197     }
198     return 0;
199 }
200
201 av_unused static inline int vc1_get_LUMSCALE2(const VC1Context *v)
202 {
203     if ((v->s.pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) &&
204         v->fcm == ILACE_FIELD &&
205         v->mv_mode == MV_PMODE_INTENSITY_COMP &&
206         v->intcompfield == 3)
207         return v->lumscale2;
208     return 0;
209 }
210
211 av_unused static inline int vc1_get_LUMSHIFT2(const VC1Context *v)
212 {
213     if ((v->s.pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) &&
214         v->fcm == ILACE_FIELD &&
215         v->mv_mode == MV_PMODE_INTENSITY_COMP &&
216         v->intcompfield == 3)
217         return v->lumshift2;
218     return 0;
219 }
220
221 /** Reconstruct bitstream TTFRM (7.1.1.41, Table-53) */
222 static inline int vc1_get_TTFRM(const VC1Context *v)
223 {
224     switch (v->ttfrm) {
225     case TT_8X8: return 0;
226     case TT_8X4: return 1;
227     case TT_4X8: return 2;
228     case TT_4X4: return 3;
229     }
230     return 0;
231 }
232
233 /** Pack FFmpeg bitplanes into a VABitPlaneBuffer element */
234 static inline void vc1_pack_bitplanes(uint8_t *bitplane, int n, const uint8_t *ff_bp[3], int x, int y, int stride)
235 {
236     const int bitplane_index = n / 2;
237     const int ff_bp_index = y * stride + x;
238     uint8_t v = 0;
239     if (ff_bp[0])
240         v = ff_bp[0][ff_bp_index];
241     if (ff_bp[1])
242         v |= ff_bp[1][ff_bp_index] << 1;
243     if (ff_bp[2])
244         v |= ff_bp[2][ff_bp_index] << 2;
245     bitplane[bitplane_index] = (bitplane[bitplane_index] << 4) | v;
246 }
247
248 static int vaapi_vc1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
249 {
250     const VC1Context *v = avctx->priv_data;
251     const MpegEncContext *s = &v->s;
252     VAAPIDecodePicture *pic = s->current_picture_ptr->hwaccel_picture_private;
253     VAPictureParameterBufferVC1 pic_param;
254     int err;
255
256     pic->output_surface = ff_vaapi_get_surface_id(s->current_picture_ptr->f);
257
258     pic_param = (VAPictureParameterBufferVC1) {
259         .forward_reference_picture         = VA_INVALID_ID,
260         .backward_reference_picture        = VA_INVALID_ID,
261         .inloop_decoded_picture            = VA_INVALID_ID,
262         .sequence_fields.bits = {
263             .pulldown                      = v->broadcast,
264             .interlace                     = v->interlace,
265             .tfcntrflag                    = v->tfcntrflag,
266             .finterpflag                   = v->finterpflag,
267             .psf                           = v->psf,
268             .multires                      = v->multires,
269             .overlap                       = v->overlap,
270             .syncmarker                    = v->resync_marker,
271             .rangered                      = v->rangered,
272             .max_b_frames                  = s->avctx->max_b_frames,
273             .profile                       = v->profile,
274         },
275         .coded_width                       = s->avctx->coded_width,
276         .coded_height                      = s->avctx->coded_height,
277         .entrypoint_fields.bits = {
278             .broken_link                   = v->broken_link,
279             .closed_entry                  = v->closed_entry,
280             .panscan_flag                  = v->panscanflag,
281             .loopfilter                    = s->loop_filter,
282         },
283         .conditional_overlap_flag          = v->condover,
284         .fast_uvmc_flag                    = v->fastuvmc,
285         .range_mapping_fields.bits = {
286             .luma_flag                     = v->range_mapy_flag,
287             .luma                          = v->range_mapy,
288             .chroma_flag                   = v->range_mapuv_flag,
289             .chroma                        = v->range_mapuv,
290         },
291         .b_picture_fraction                = v->bfraction_lut_index,
292         .cbp_table                         = (v->fcm == PROGRESSIVE ? v->cbptab : v->icbptab),
293         .mb_mode_table                     = v->mbmodetab,
294         .range_reduction_frame             = v->rangeredfrm,
295         .rounding_control                  = v->rnd,
296         .post_processing                   = v->postproc,
297         .picture_resolution_index          = v->respic,
298         .picture_fields.bits = {
299             .picture_type                  = (v->fcm == ILACE_FIELD ? vc1_get_FPTYPE(v) : vc1_get_PTYPE(v)),
300             .frame_coding_mode             = v->fcm,
301             .top_field_first               = v->tff,
302             .is_first_field                = !v->second_field,
303             .intensity_compensation        = v->intcomp,
304         },
305         .luma_scale                        = vc1_get_LUMSCALE(v),
306         .luma_shift                        = vc1_get_LUMSHIFT(v),
307 #if VA_CHECK_VERSION(1, 1, 0)
308         .luma_scale2                       = vc1_get_LUMSCALE2(v),
309         .luma_shift2                       = vc1_get_LUMSHIFT2(v),
310         .intensity_compensation_field      = vc1_get_INTCOMPFIELD(v),
311 #endif
312         .raw_coding.flags = {
313             .mv_type_mb                    = v->mv_type_is_raw,
314             .direct_mb                     = v->dmb_is_raw,
315             .skip_mb                       = v->skip_is_raw,
316             .field_tx                      = v->fieldtx_is_raw,
317             .forward_mb                    = v->fmb_is_raw,
318             .ac_pred                       = v->acpred_is_raw,
319             .overflags                     = v->overflg_is_raw,
320         },
321         .bitplane_present.flags = {
322             .bp_mv_type_mb                 = vc1_has_MVTYPEMB_bitplane(v),
323             .bp_direct_mb                  = vc1_has_DIRECTMB_bitplane(v),
324             .bp_skip_mb                    = vc1_has_SKIPMB_bitplane(v),
325             .bp_field_tx                   = vc1_has_FIELDTX_bitplane(v),
326             .bp_forward_mb                 = vc1_has_FORWARDMB_bitplane(v),
327             .bp_ac_pred                    = vc1_has_ACPRED_bitplane(v),
328             .bp_overflags                  = vc1_has_OVERFLAGS_bitplane(v),
329         },
330         .reference_fields.bits = {
331             .reference_distance_flag       = v->refdist_flag,
332             .reference_distance            = v->refdist,
333             .num_reference_pictures        = v->numref,
334             .reference_field_pic_indicator = v->reffield,
335         },
336         .mv_fields.bits = {
337             .mv_mode                       = vc1_get_MVMODE(v),
338             .mv_mode2                      = vc1_get_MVMODE2(v),
339             .mv_table                      = (v->fcm == PROGRESSIVE ? s->mv_table_index : v->imvtab),
340             .two_mv_block_pattern_table    = v->twomvbptab,
341             .four_mv_switch                = v->fourmvswitch,
342             .four_mv_block_pattern_table   = v->fourmvbptab,
343             .extended_mv_flag              = v->extended_mv,
344             .extended_mv_range             = v->mvrange,
345             .extended_dmv_flag             = v->extended_dmv,
346             .extended_dmv_range            = v->dmvrange,
347         },
348         .pic_quantizer_fields.bits = {
349             .dquant                        = v->dquant,
350             .quantizer                     = v->quantizer_mode,
351             .half_qp                       = v->halfpq,
352             .pic_quantizer_scale           = v->pq,
353             .pic_quantizer_type            = v->pquantizer,
354             .dq_frame                      = v->dquantfrm,
355             .dq_profile                    = v->dqprofile,
356             .dq_sb_edge                    = v->dqprofile == DQPROFILE_SINGLE_EDGE  ? v->dqsbedge : 0,
357             .dq_db_edge                    = v->dqprofile == DQPROFILE_DOUBLE_EDGES ? v->dqsbedge : 0,
358             .dq_binary_level               = v->dqbilevel,
359             .alt_pic_quantizer             = v->altpq,
360         },
361         .transform_fields.bits = {
362             .variable_sized_transform_flag = v->vstransform,
363             .mb_level_transform_type_flag  = v->ttmbf,
364             .frame_level_transform_type    = vc1_get_TTFRM(v),
365             .transform_ac_codingset_idx1   = v->c_ac_table_index,
366             .transform_ac_codingset_idx2   = v->y_ac_table_index,
367             .intra_transform_dc_table      = v->s.dc_table_index,
368         },
369     };
370
371     switch (s->pict_type) {
372     case AV_PICTURE_TYPE_B:
373         pic_param.backward_reference_picture = ff_vaapi_get_surface_id(s->next_picture.f);
374         // fall-through
375     case AV_PICTURE_TYPE_P:
376         pic_param.forward_reference_picture = ff_vaapi_get_surface_id(s->last_picture.f);
377         break;
378     }
379
380     err = ff_vaapi_decode_make_param_buffer(avctx, pic,
381                                             VAPictureParameterBufferType,
382                                             &pic_param, sizeof(pic_param));
383     if (err)
384         goto fail;
385
386     if (pic_param.bitplane_present.value & 0x7f) {
387         uint8_t *bitplane;
388         const uint8_t *ff_bp[3];
389         int x, y, n;
390         size_t size = (s->mb_width * s->mb_height + 1) / 2;
391
392         bitplane = av_mallocz(size);
393         if (!bitplane) {
394             err = AVERROR(ENOMEM);
395             goto fail;
396         }
397
398         switch (s->pict_type) {
399         case AV_PICTURE_TYPE_P:
400             ff_bp[0] = pic_param.bitplane_present.flags.bp_direct_mb  ? v->direct_mb_plane    : NULL;
401             ff_bp[1] = pic_param.bitplane_present.flags.bp_skip_mb    ? s->mbskip_table       : NULL;
402             ff_bp[2] = pic_param.bitplane_present.flags.bp_mv_type_mb ? v->mv_type_mb_plane   : NULL;
403             break;
404         case AV_PICTURE_TYPE_B:
405             if (!v->bi_type) {
406                 ff_bp[0] = pic_param.bitplane_present.flags.bp_direct_mb  ? v->direct_mb_plane  : NULL;
407                 ff_bp[1] = pic_param.bitplane_present.flags.bp_skip_mb    ? s->mbskip_table     : NULL;
408                 ff_bp[2] = pic_param.bitplane_present.flags.bp_forward_mb ? v->forward_mb_plane : NULL;
409                 break;
410             }
411             /* fall-through (BI-type) */
412         case AV_PICTURE_TYPE_I:
413             ff_bp[0] = pic_param.bitplane_present.flags.bp_field_tx   ? v->fieldtx_plane      : NULL;
414             ff_bp[1] = pic_param.bitplane_present.flags.bp_ac_pred    ? v->acpred_plane       : NULL;
415             ff_bp[2] = pic_param.bitplane_present.flags.bp_overflags  ? v->over_flags_plane   : NULL;
416             break;
417         default:
418             ff_bp[0] = NULL;
419             ff_bp[1] = NULL;
420             ff_bp[2] = NULL;
421             break;
422         }
423
424         n = 0;
425         for (y = 0; y < s->mb_height; y++)
426             for (x = 0; x < s->mb_width; x++, n++)
427                 vc1_pack_bitplanes(bitplane, n, ff_bp, x, y, s->mb_stride);
428         if (n & 1) /* move last nibble to the high order */
429             bitplane[n/2] <<= 4;
430
431         err = ff_vaapi_decode_make_param_buffer(avctx, pic,
432                                                 VABitPlaneBufferType,
433                                                 bitplane, size);
434         av_free(bitplane);
435         if (err)
436             goto fail;
437     }
438     return 0;
439
440 fail:
441     ff_vaapi_decode_cancel(avctx, pic);
442     return err;
443 }
444
445 static int vaapi_vc1_end_frame(AVCodecContext *avctx)
446 {
447     VC1Context *v = avctx->priv_data;
448     MpegEncContext *s = &v->s;
449     VAAPIDecodePicture *pic = s->current_picture_ptr->hwaccel_picture_private;
450     int ret;
451
452     ret = ff_vaapi_decode_issue(avctx, pic);
453     if (ret < 0)
454         goto fail;
455
456     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
457
458 fail:
459     return ret;
460 }
461
462 static int vaapi_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
463 {
464     const VC1Context *v = avctx->priv_data;
465     const MpegEncContext *s = &v->s;
466     VAAPIDecodePicture *pic = s->current_picture_ptr->hwaccel_picture_private;
467     VASliceParameterBufferVC1 slice_param;
468     int err;
469
470     /* Current bit buffer is beyond any marker for VC-1, so skip it */
471     if (avctx->codec_id == AV_CODEC_ID_VC1 && IS_MARKER(AV_RB32(buffer))) {
472         buffer += 4;
473         size -= 4;
474     }
475
476     slice_param = (VASliceParameterBufferVC1) {
477         .slice_data_size         = size,
478         .slice_data_offset       = 0,
479         .slice_data_flag         = VA_SLICE_DATA_FLAG_ALL,
480         .macroblock_offset       = get_bits_count(&s->gb),
481         .slice_vertical_position = s->mb_y,
482     };
483
484     err = ff_vaapi_decode_make_slice_buffer(avctx, pic,
485                                             &slice_param, sizeof(slice_param),
486                                             buffer, size);
487     if (err < 0) {
488         ff_vaapi_decode_cancel(avctx, pic);
489         return err;
490     }
491
492     return 0;
493 }
494
495 #if CONFIG_WMV3_VAAPI_HWACCEL
496 const AVHWAccel ff_wmv3_vaapi_hwaccel = {
497     .name                 = "wmv3_vaapi",
498     .type                 = AVMEDIA_TYPE_VIDEO,
499     .id                   = AV_CODEC_ID_WMV3,
500     .pix_fmt              = AV_PIX_FMT_VAAPI,
501     .start_frame          = &vaapi_vc1_start_frame,
502     .end_frame            = &vaapi_vc1_end_frame,
503     .decode_slice         = &vaapi_vc1_decode_slice,
504     .frame_priv_data_size = sizeof(VAAPIDecodePicture),
505     .init                 = &ff_vaapi_decode_init,
506     .uninit               = &ff_vaapi_decode_uninit,
507     .frame_params         = &ff_vaapi_common_frame_params,
508     .priv_data_size       = sizeof(VAAPIDecodeContext),
509     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
510 };
511 #endif
512
513 const AVHWAccel ff_vc1_vaapi_hwaccel = {
514     .name                 = "vc1_vaapi",
515     .type                 = AVMEDIA_TYPE_VIDEO,
516     .id                   = AV_CODEC_ID_VC1,
517     .pix_fmt              = AV_PIX_FMT_VAAPI,
518     .start_frame          = &vaapi_vc1_start_frame,
519     .end_frame            = &vaapi_vc1_end_frame,
520     .decode_slice         = &vaapi_vc1_decode_slice,
521     .frame_priv_data_size = sizeof(VAAPIDecodePicture),
522     .init                 = &ff_vaapi_decode_init,
523     .uninit               = &ff_vaapi_decode_uninit,
524     .frame_params         = &ff_vaapi_common_frame_params,
525     .priv_data_size       = sizeof(VAAPIDecodeContext),
526     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
527 };