• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/vaapi_vc1.c

Go to the documentation of this file.
00001 /*
00002  * VC-1 HW decode acceleration through VA API
00003  *
00004  * Copyright (C) 2008-2009 Splitted-Desktop Systems
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "vaapi_internal.h"
00024 #include "vc1.h"
00025 #include "vc1data.h"
00026 
00028 static int get_VAMvModeVC1(enum MVModes mv_mode)
00029 {
00030     switch (mv_mode) {
00031     case MV_PMODE_1MV_HPEL_BILIN: return VAMvMode1MvHalfPelBilinear;
00032     case MV_PMODE_1MV:            return VAMvMode1Mv;
00033     case MV_PMODE_1MV_HPEL:       return VAMvMode1MvHalfPel;
00034     case MV_PMODE_MIXED_MV:       return VAMvModeMixedMv;
00035     case MV_PMODE_INTENSITY_COMP: return VAMvModeIntensityCompensation;
00036     }
00037     return 0;
00038 }
00039 
00041 static inline int vc1_has_MVTYPEMB_bitplane(VC1Context *v)
00042 {
00043     if (v->mv_type_is_raw)
00044         return 0;
00045     return v->s.pict_type == AV_PICTURE_TYPE_P &&
00046            (v->mv_mode == MV_PMODE_MIXED_MV ||
00047             (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
00048              v->mv_mode2 == MV_PMODE_MIXED_MV));
00049 }
00050 
00052 static inline int vc1_has_SKIPMB_bitplane(VC1Context *v)
00053 {
00054     if (v->skip_is_raw)
00055         return 0;
00056     return v->s.pict_type == AV_PICTURE_TYPE_P ||
00057            (v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type);
00058 }
00059 
00061 static inline int vc1_has_DIRECTMB_bitplane(VC1Context *v)
00062 {
00063     if (v->dmb_is_raw)
00064         return 0;
00065     return v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type;
00066 }
00067 
00069 static inline int vc1_has_ACPRED_bitplane(VC1Context *v)
00070 {
00071     if (v->acpred_is_raw)
00072         return 0;
00073     return v->profile == PROFILE_ADVANCED &&
00074            (v->s.pict_type == AV_PICTURE_TYPE_I ||
00075             (v->s.pict_type == AV_PICTURE_TYPE_B && v->bi_type));
00076 }
00077 
00079 static inline int vc1_has_OVERFLAGS_bitplane(VC1Context *v)
00080 {
00081     if (v->overflg_is_raw)
00082         return 0;
00083     return v->profile == PROFILE_ADVANCED &&
00084            (v->s.pict_type == AV_PICTURE_TYPE_I ||
00085             (v->s.pict_type == AV_PICTURE_TYPE_B && v->bi_type)) &&
00086            (v->overlap && v->pq <= 8) &&
00087            v->condover == CONDOVER_SELECT;
00088 }
00089 
00091 static int vc1_get_PTYPE(VC1Context *v)
00092 {
00093     MpegEncContext * const s = &v->s;
00094     switch (s->pict_type) {
00095     case AV_PICTURE_TYPE_I: return 0;
00096     case AV_PICTURE_TYPE_P: return v->p_frame_skipped ? 4 : 1;
00097     case AV_PICTURE_TYPE_B: return v->bi_type         ? 3 : 2;
00098     }
00099     return 0;
00100 }
00101 
00103 static inline VAMvModeVC1 vc1_get_MVMODE(VC1Context *v)
00104 {
00105     if (v->s.pict_type == AV_PICTURE_TYPE_P ||
00106         (v->s.pict_type == AV_PICTURE_TYPE_B && !v->bi_type))
00107         return get_VAMvModeVC1(v->mv_mode);
00108     return 0;
00109 }
00110 
00112 static inline VAMvModeVC1 vc1_get_MVMODE2(VC1Context *v)
00113 {
00114     if (v->s.pict_type == AV_PICTURE_TYPE_P && v->mv_mode == MV_PMODE_INTENSITY_COMP)
00115         return get_VAMvModeVC1(v->mv_mode2);
00116     return 0;
00117 }
00118 
00120 static inline int vc1_get_TTFRM(VC1Context *v)
00121 {
00122     switch (v->ttfrm) {
00123     case TT_8X8: return 0;
00124     case TT_8X4: return 1;
00125     case TT_4X8: return 2;
00126     case TT_4X4: return 3;
00127     }
00128     return 0;
00129 }
00130 
00132 static inline void vc1_pack_bitplanes(uint8_t *bitplane, int n, const uint8_t *ff_bp[3], int x, int y, int stride)
00133 {
00134     const int bitplane_index = n / 2;
00135     const int ff_bp_index = y * stride + x;
00136     uint8_t v = 0;
00137     if (ff_bp[0])
00138         v = ff_bp[0][ff_bp_index];
00139     if (ff_bp[1])
00140         v |= ff_bp[1][ff_bp_index] << 1;
00141     if (ff_bp[2])
00142         v |= ff_bp[2][ff_bp_index] << 2;
00143     bitplane[bitplane_index] = (bitplane[bitplane_index] << 4) | v;
00144 }
00145 
00146 static int vaapi_vc1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
00147 {
00148     VC1Context * const v = avctx->priv_data;
00149     MpegEncContext * const s = &v->s;
00150     struct vaapi_context * const vactx = avctx->hwaccel_context;
00151     VAPictureParameterBufferVC1 *pic_param;
00152 
00153     av_dlog(avctx, "vaapi_vc1_start_frame()\n");
00154 
00155     vactx->slice_param_size = sizeof(VASliceParameterBufferVC1);
00156 
00157     /* Fill in VAPictureParameterBufferVC1 */
00158     pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferVC1));
00159     if (!pic_param)
00160         return -1;
00161     pic_param->forward_reference_picture                            = VA_INVALID_ID;
00162     pic_param->backward_reference_picture                           = VA_INVALID_ID;
00163     pic_param->inloop_decoded_picture                               = VA_INVALID_ID;
00164     pic_param->sequence_fields.value                                = 0; /* reset all bits */
00165     pic_param->sequence_fields.bits.pulldown                        = v->broadcast;
00166     pic_param->sequence_fields.bits.interlace                       = v->interlace;
00167     pic_param->sequence_fields.bits.tfcntrflag                      = v->tfcntrflag;
00168     pic_param->sequence_fields.bits.finterpflag                     = v->finterpflag;
00169     pic_param->sequence_fields.bits.psf                             = v->psf;
00170     pic_param->sequence_fields.bits.multires                        = v->multires;
00171     pic_param->sequence_fields.bits.overlap                         = v->overlap;
00172     pic_param->sequence_fields.bits.syncmarker                      = s->resync_marker;
00173     pic_param->sequence_fields.bits.rangered                        = v->rangered;
00174     pic_param->sequence_fields.bits.max_b_frames                    = s->avctx->max_b_frames;
00175 #if VA_CHECK_VERSION(0,32,0)
00176     pic_param->sequence_fields.bits.profile                         = v->profile;
00177 #endif
00178     pic_param->coded_width                                          = s->avctx->coded_width;
00179     pic_param->coded_height                                         = s->avctx->coded_height;
00180     pic_param->entrypoint_fields.value                              = 0; /* reset all bits */
00181     pic_param->entrypoint_fields.bits.broken_link                   = v->broken_link;
00182     pic_param->entrypoint_fields.bits.closed_entry                  = v->closed_entry;
00183     pic_param->entrypoint_fields.bits.panscan_flag                  = v->panscanflag;
00184     pic_param->entrypoint_fields.bits.loopfilter                    = s->loop_filter;
00185     pic_param->conditional_overlap_flag                             = v->condover;
00186     pic_param->fast_uvmc_flag                                       = v->fastuvmc;
00187     pic_param->range_mapping_fields.value                           = 0; /* reset all bits */
00188     pic_param->range_mapping_fields.bits.luma_flag                  = v->range_mapy_flag;
00189     pic_param->range_mapping_fields.bits.luma                       = v->range_mapy;
00190     pic_param->range_mapping_fields.bits.chroma_flag                = v->range_mapuv_flag;
00191     pic_param->range_mapping_fields.bits.chroma                     = v->range_mapuv;
00192     pic_param->b_picture_fraction                                   = v->bfraction_lut_index;
00193     pic_param->cbp_table                                            = v->cbpcy_vlc ? v->cbpcy_vlc - ff_vc1_cbpcy_p_vlc : 0;
00194     pic_param->mb_mode_table                                        = 0; /* XXX: interlaced frame */
00195     pic_param->range_reduction_frame                                = v->rangeredfrm;
00196     pic_param->rounding_control                                     = v->rnd;
00197     pic_param->post_processing                                      = v->postproc;
00198     pic_param->picture_resolution_index                             = v->respic;
00199     pic_param->luma_scale                                           = v->lumscale;
00200     pic_param->luma_shift                                           = v->lumshift;
00201     pic_param->picture_fields.value                                 = 0; /* reset all bits */
00202     pic_param->picture_fields.bits.picture_type                     = vc1_get_PTYPE(v);
00203     pic_param->picture_fields.bits.frame_coding_mode                = v->fcm;
00204     pic_param->picture_fields.bits.top_field_first                  = v->tff;
00205     pic_param->picture_fields.bits.is_first_field                   = v->fcm == 0; /* XXX: interlaced frame */
00206     pic_param->picture_fields.bits.intensity_compensation           = v->mv_mode == MV_PMODE_INTENSITY_COMP;
00207     pic_param->raw_coding.value                                     = 0; /* reset all bits */
00208     pic_param->raw_coding.flags.mv_type_mb                          = v->mv_type_is_raw;
00209     pic_param->raw_coding.flags.direct_mb                           = v->dmb_is_raw;
00210     pic_param->raw_coding.flags.skip_mb                             = v->skip_is_raw;
00211     pic_param->raw_coding.flags.field_tx                            = 0; /* XXX: interlaced frame */
00212     pic_param->raw_coding.flags.forward_mb                          = 0; /* XXX: interlaced frame */
00213     pic_param->raw_coding.flags.ac_pred                             = v->acpred_is_raw;
00214     pic_param->raw_coding.flags.overflags                           = v->overflg_is_raw;
00215     pic_param->bitplane_present.value                               = 0; /* reset all bits */
00216     pic_param->bitplane_present.flags.bp_mv_type_mb                 = vc1_has_MVTYPEMB_bitplane(v);
00217     pic_param->bitplane_present.flags.bp_direct_mb                  = vc1_has_DIRECTMB_bitplane(v);
00218     pic_param->bitplane_present.flags.bp_skip_mb                    = vc1_has_SKIPMB_bitplane(v);
00219     pic_param->bitplane_present.flags.bp_field_tx                   = 0; /* XXX: interlaced frame */
00220     pic_param->bitplane_present.flags.bp_forward_mb                 = 0; /* XXX: interlaced frame */
00221     pic_param->bitplane_present.flags.bp_ac_pred                    = vc1_has_ACPRED_bitplane(v);
00222     pic_param->bitplane_present.flags.bp_overflags                  = vc1_has_OVERFLAGS_bitplane(v);
00223     pic_param->reference_fields.value                               = 0; /* reset all bits */
00224     pic_param->reference_fields.bits.reference_distance_flag        = v->refdist_flag;
00225     pic_param->reference_fields.bits.reference_distance             = 0; /* XXX: interlaced frame */
00226     pic_param->reference_fields.bits.num_reference_pictures         = 0; /* XXX: interlaced frame */
00227     pic_param->reference_fields.bits.reference_field_pic_indicator  = 0; /* XXX: interlaced frame */
00228     pic_param->mv_fields.value                                      = 0; /* reset all bits */
00229     pic_param->mv_fields.bits.mv_mode                               = vc1_get_MVMODE(v);
00230     pic_param->mv_fields.bits.mv_mode2                              = vc1_get_MVMODE2(v);
00231     pic_param->mv_fields.bits.mv_table                              = s->mv_table_index;
00232     pic_param->mv_fields.bits.two_mv_block_pattern_table            = 0; /* XXX: interlaced frame */
00233     pic_param->mv_fields.bits.four_mv_switch                        = 0; /* XXX: interlaced frame */
00234     pic_param->mv_fields.bits.four_mv_block_pattern_table           = 0; /* XXX: interlaced frame */
00235     pic_param->mv_fields.bits.extended_mv_flag                      = v->extended_mv;
00236     pic_param->mv_fields.bits.extended_mv_range                     = v->mvrange;
00237     pic_param->mv_fields.bits.extended_dmv_flag                     = v->extended_dmv;
00238     pic_param->mv_fields.bits.extended_dmv_range                    = 0; /* XXX: interlaced frame */
00239     pic_param->pic_quantizer_fields.value                           = 0; /* reset all bits */
00240     pic_param->pic_quantizer_fields.bits.dquant                     = v->dquant;
00241     pic_param->pic_quantizer_fields.bits.quantizer                  = v->quantizer_mode;
00242     pic_param->pic_quantizer_fields.bits.half_qp                    = v->halfpq;
00243     pic_param->pic_quantizer_fields.bits.pic_quantizer_scale        = v->pq;
00244     pic_param->pic_quantizer_fields.bits.pic_quantizer_type         = v->pquantizer;
00245     pic_param->pic_quantizer_fields.bits.dq_frame                   = v->dquantfrm;
00246     pic_param->pic_quantizer_fields.bits.dq_profile                 = v->dqprofile;
00247     pic_param->pic_quantizer_fields.bits.dq_sb_edge                 = v->dqprofile == DQPROFILE_SINGLE_EDGE  ? v->dqsbedge : 0;
00248     pic_param->pic_quantizer_fields.bits.dq_db_edge                 = v->dqprofile == DQPROFILE_DOUBLE_EDGES ? v->dqsbedge : 0;
00249     pic_param->pic_quantizer_fields.bits.dq_binary_level            = v->dqbilevel;
00250     pic_param->pic_quantizer_fields.bits.alt_pic_quantizer          = v->altpq;
00251     pic_param->transform_fields.value                               = 0; /* reset all bits */
00252     pic_param->transform_fields.bits.variable_sized_transform_flag  = v->vstransform;
00253     pic_param->transform_fields.bits.mb_level_transform_type_flag   = v->ttmbf;
00254     pic_param->transform_fields.bits.frame_level_transform_type     = vc1_get_TTFRM(v);
00255     pic_param->transform_fields.bits.transform_ac_codingset_idx1    = v->c_ac_table_index;
00256     pic_param->transform_fields.bits.transform_ac_codingset_idx2    = v->y_ac_table_index;
00257     pic_param->transform_fields.bits.intra_transform_dc_table       = v->s.dc_table_index;
00258 
00259     switch (s->pict_type) {
00260     case AV_PICTURE_TYPE_B:
00261         pic_param->backward_reference_picture = ff_vaapi_get_surface_id(&s->next_picture);
00262         // fall-through
00263     case AV_PICTURE_TYPE_P:
00264         pic_param->forward_reference_picture = ff_vaapi_get_surface_id(&s->last_picture);
00265         break;
00266     }
00267 
00268     if (pic_param->bitplane_present.value) {
00269         uint8_t *bitplane;
00270         const uint8_t *ff_bp[3];
00271         int x, y, n;
00272 
00273         switch (s->pict_type) {
00274         case AV_PICTURE_TYPE_P:
00275             ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb  ? v->direct_mb_plane    : NULL;
00276             ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb    ? s->mbskip_table       : NULL;
00277             ff_bp[2] = pic_param->bitplane_present.flags.bp_mv_type_mb ? v->mv_type_mb_plane   : NULL;
00278             break;
00279         case AV_PICTURE_TYPE_B:
00280             if (!v->bi_type) {
00281                 ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
00282                 ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb   ? s->mbskip_table    : NULL;
00283                 ff_bp[2] = NULL; /* XXX: interlaced frame (FORWARD plane) */
00284                 break;
00285             }
00286             /* fall-through (BI-type) */
00287         case AV_PICTURE_TYPE_I:
00288             ff_bp[0] = NULL; /* XXX: interlaced frame (FIELDTX plane) */
00289             ff_bp[1] = pic_param->bitplane_present.flags.bp_ac_pred    ? v->acpred_plane       : NULL;
00290             ff_bp[2] = pic_param->bitplane_present.flags.bp_overflags  ? v->over_flags_plane   : NULL;
00291             break;
00292         default:
00293             ff_bp[0] = NULL;
00294             ff_bp[1] = NULL;
00295             ff_bp[2] = NULL;
00296             break;
00297         }
00298 
00299         bitplane = ff_vaapi_alloc_bitplane(vactx, (s->mb_width * s->mb_height + 1) / 2);
00300         if (!bitplane)
00301             return -1;
00302 
00303         n = 0;
00304         for (y = 0; y < s->mb_height; y++)
00305             for (x = 0; x < s->mb_width; x++, n++)
00306                 vc1_pack_bitplanes(bitplane, n, ff_bp, x, y, s->mb_stride);
00307         if (n & 1) /* move last nibble to the high order */
00308             bitplane[n/2] <<= 4;
00309     }
00310     return 0;
00311 }
00312 
00313 static int vaapi_vc1_end_frame(AVCodecContext *avctx)
00314 {
00315     VC1Context * const v = avctx->priv_data;
00316 
00317     return ff_vaapi_common_end_frame(&v->s);
00318 }
00319 
00320 static int vaapi_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
00321 {
00322     VC1Context * const v = avctx->priv_data;
00323     MpegEncContext * const s = &v->s;
00324     VASliceParameterBufferVC1 *slice_param;
00325 
00326     av_dlog(avctx, "vaapi_vc1_decode_slice(): buffer %p, size %d\n", buffer, size);
00327 
00328     /* Current bit buffer is beyond any marker for VC-1, so skip it */
00329     if (avctx->codec_id == CODEC_ID_VC1 && IS_MARKER(AV_RB32(buffer))) {
00330         buffer += 4;
00331         size -= 4;
00332     }
00333 
00334     /* Fill in VASliceParameterBufferVC1 */
00335     slice_param = (VASliceParameterBufferVC1 *)ff_vaapi_alloc_slice(avctx->hwaccel_context, buffer, size);
00336     if (!slice_param)
00337         return -1;
00338     slice_param->macroblock_offset       = get_bits_count(&s->gb);
00339     slice_param->slice_vertical_position = s->mb_y;
00340     return 0;
00341 }
00342 
00343 #if CONFIG_WMV3_VAAPI_HWACCEL
00344 AVHWAccel ff_wmv3_vaapi_hwaccel = {
00345     .name           = "wmv3_vaapi",
00346     .type           = AVMEDIA_TYPE_VIDEO,
00347     .id             = CODEC_ID_WMV3,
00348     .pix_fmt        = PIX_FMT_VAAPI_VLD,
00349     .start_frame    = vaapi_vc1_start_frame,
00350     .end_frame      = vaapi_vc1_end_frame,
00351     .decode_slice   = vaapi_vc1_decode_slice,
00352 };
00353 #endif
00354 
00355 AVHWAccel ff_vc1_vaapi_hwaccel = {
00356     .name           = "vc1_vaapi",
00357     .type           = AVMEDIA_TYPE_VIDEO,
00358     .id             = CODEC_ID_VC1,
00359     .pix_fmt        = PIX_FMT_VAAPI_VLD,
00360     .start_frame    = vaapi_vc1_start_frame,
00361     .end_frame      = vaapi_vc1_end_frame,
00362     .decode_slice   = vaapi_vc1_decode_slice,
00363 };
Generated on Thu Jan 24 2013 17:08:54 for Libav by doxygen 1.7.1