libavcodec/h264_ps.c
Go to the documentation of this file.
00001 /*
00002  * H.26L/H.264/AVC/JVT/14496-10/... parameter set decoding
00003  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "libavutil/imgutils.h"
00029 #include "internal.h"
00030 #include "dsputil.h"
00031 #include "avcodec.h"
00032 #include "h264.h"
00033 #include "h264data.h" //FIXME FIXME FIXME (just for zigzag_scan)
00034 #include "golomb.h"
00035 
00036 
00037 //#undef NDEBUG
00038 #include <assert.h>
00039 
00040 static const AVRational pixel_aspect[17]={
00041  {0, 1},
00042  {1, 1},
00043  {12, 11},
00044  {10, 11},
00045  {16, 11},
00046  {40, 33},
00047  {24, 11},
00048  {20, 11},
00049  {32, 11},
00050  {80, 33},
00051  {18, 11},
00052  {15, 11},
00053  {64, 33},
00054  {160,99},
00055  {4, 3},
00056  {3, 2},
00057  {2, 1},
00058 };
00059 
00060 #define QP(qP,depth) ( (qP)+6*((depth)-8) )
00061 
00062 #define CHROMA_QP_TABLE_END(d) \
00063      QP(0,d),  QP(1,d),  QP(2,d),  QP(3,d),  QP(4,d),  QP(5,d),\
00064      QP(6,d),  QP(7,d),  QP(8,d),  QP(9,d), QP(10,d), QP(11,d),\
00065     QP(12,d), QP(13,d), QP(14,d), QP(15,d), QP(16,d), QP(17,d),\
00066     QP(18,d), QP(19,d), QP(20,d), QP(21,d), QP(22,d), QP(23,d),\
00067     QP(24,d), QP(25,d), QP(26,d), QP(27,d), QP(28,d), QP(29,d),\
00068     QP(29,d), QP(30,d), QP(31,d), QP(32,d), QP(32,d), QP(33,d),\
00069     QP(34,d), QP(34,d), QP(35,d), QP(35,d), QP(36,d), QP(36,d),\
00070     QP(37,d), QP(37,d), QP(37,d), QP(38,d), QP(38,d), QP(38,d),\
00071     QP(39,d), QP(39,d), QP(39,d), QP(39,d)
00072 
00073 const uint8_t ff_h264_chroma_qp[3][QP_MAX_NUM+1] = {
00074     {
00075         CHROMA_QP_TABLE_END(8)
00076     },
00077     {
00078         0, 1, 2, 3, 4, 5,
00079         CHROMA_QP_TABLE_END(9)
00080     },
00081     {
00082         0, 1, 2, 3,  4,  5,
00083         6, 7, 8, 9, 10, 11,
00084         CHROMA_QP_TABLE_END(10)
00085     },
00086 };
00087 
00088 static const uint8_t default_scaling4[2][16]={
00089 {   6,13,20,28,
00090    13,20,28,32,
00091    20,28,32,37,
00092    28,32,37,42
00093 },{
00094    10,14,20,24,
00095    14,20,24,27,
00096    20,24,27,30,
00097    24,27,30,34
00098 }};
00099 
00100 static const uint8_t default_scaling8[2][64]={
00101 {   6,10,13,16,18,23,25,27,
00102    10,11,16,18,23,25,27,29,
00103    13,16,18,23,25,27,29,31,
00104    16,18,23,25,27,29,31,33,
00105    18,23,25,27,29,31,33,36,
00106    23,25,27,29,31,33,36,38,
00107    25,27,29,31,33,36,38,40,
00108    27,29,31,33,36,38,40,42
00109 },{
00110     9,13,15,17,19,21,22,24,
00111    13,13,17,19,21,22,24,25,
00112    15,17,19,21,22,24,25,27,
00113    17,19,21,22,24,25,27,28,
00114    19,21,22,24,25,27,28,30,
00115    21,22,24,25,27,28,30,32,
00116    22,24,25,27,28,30,32,33,
00117    24,25,27,28,30,32,33,35
00118 }};
00119 
00120 static inline int decode_hrd_parameters(H264Context *h, SPS *sps){
00121     MpegEncContext * const s = &h->s;
00122     int cpb_count, i;
00123     cpb_count = get_ue_golomb_31(&s->gb) + 1;
00124 
00125     if(cpb_count > 32U){
00126         av_log(h->s.avctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
00127         return -1;
00128     }
00129 
00130     get_bits(&s->gb, 4); /* bit_rate_scale */
00131     get_bits(&s->gb, 4); /* cpb_size_scale */
00132     for(i=0; i<cpb_count; i++){
00133         get_ue_golomb_long(&s->gb); /* bit_rate_value_minus1 */
00134         get_ue_golomb_long(&s->gb); /* cpb_size_value_minus1 */
00135         get_bits1(&s->gb);     /* cbr_flag */
00136     }
00137     sps->initial_cpb_removal_delay_length = get_bits(&s->gb, 5) + 1;
00138     sps->cpb_removal_delay_length = get_bits(&s->gb, 5) + 1;
00139     sps->dpb_output_delay_length = get_bits(&s->gb, 5) + 1;
00140     sps->time_offset_length = get_bits(&s->gb, 5);
00141     sps->cpb_cnt = cpb_count;
00142     return 0;
00143 }
00144 
00145 static inline int decode_vui_parameters(H264Context *h, SPS *sps){
00146     MpegEncContext * const s = &h->s;
00147     int aspect_ratio_info_present_flag;
00148     unsigned int aspect_ratio_idc;
00149 
00150     aspect_ratio_info_present_flag= get_bits1(&s->gb);
00151 
00152     if( aspect_ratio_info_present_flag ) {
00153         aspect_ratio_idc= get_bits(&s->gb, 8);
00154         if( aspect_ratio_idc == EXTENDED_SAR ) {
00155             sps->sar.num= get_bits(&s->gb, 16);
00156             sps->sar.den= get_bits(&s->gb, 16);
00157         }else if(aspect_ratio_idc < FF_ARRAY_ELEMS(pixel_aspect)){
00158             sps->sar=  pixel_aspect[aspect_ratio_idc];
00159         }else{
00160             av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
00161             return -1;
00162         }
00163     }else{
00164         sps->sar.num=
00165         sps->sar.den= 0;
00166     }
00167 //            s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
00168 
00169     if(get_bits1(&s->gb)){      /* overscan_info_present_flag */
00170         get_bits1(&s->gb);      /* overscan_appropriate_flag */
00171     }
00172 
00173     sps->video_signal_type_present_flag = get_bits1(&s->gb);
00174     if(sps->video_signal_type_present_flag){
00175         get_bits(&s->gb, 3);    /* video_format */
00176         sps->full_range = get_bits1(&s->gb); /* video_full_range_flag */
00177 
00178         sps->colour_description_present_flag = get_bits1(&s->gb);
00179         if(sps->colour_description_present_flag){
00180             sps->color_primaries = get_bits(&s->gb, 8); /* colour_primaries */
00181             sps->color_trc       = get_bits(&s->gb, 8); /* transfer_characteristics */
00182             sps->colorspace      = get_bits(&s->gb, 8); /* matrix_coefficients */
00183             if (sps->color_primaries >= AVCOL_PRI_NB)
00184                 sps->color_primaries  = AVCOL_PRI_UNSPECIFIED;
00185             if (sps->color_trc >= AVCOL_TRC_NB)
00186                 sps->color_trc  = AVCOL_TRC_UNSPECIFIED;
00187             if (sps->colorspace >= AVCOL_SPC_NB)
00188                 sps->colorspace  = AVCOL_SPC_UNSPECIFIED;
00189         }
00190     }
00191 
00192     if(get_bits1(&s->gb)){      /* chroma_location_info_present_flag */
00193         s->avctx->chroma_sample_location = get_ue_golomb(&s->gb)+1;  /* chroma_sample_location_type_top_field */
00194         get_ue_golomb(&s->gb);  /* chroma_sample_location_type_bottom_field */
00195     }
00196 
00197     sps->timing_info_present_flag = get_bits1(&s->gb);
00198     if(sps->timing_info_present_flag){
00199         sps->num_units_in_tick = get_bits_long(&s->gb, 32);
00200         sps->time_scale = get_bits_long(&s->gb, 32);
00201         if(!sps->num_units_in_tick || !sps->time_scale){
00202             av_log(h->s.avctx, AV_LOG_ERROR, "time_scale/num_units_in_tick invalid or unsupported (%d/%d)\n", sps->time_scale, sps->num_units_in_tick);
00203             return -1;
00204         }
00205         sps->fixed_frame_rate_flag = get_bits1(&s->gb);
00206     }
00207 
00208     sps->nal_hrd_parameters_present_flag = get_bits1(&s->gb);
00209     if(sps->nal_hrd_parameters_present_flag)
00210         if(decode_hrd_parameters(h, sps) < 0)
00211             return -1;
00212     sps->vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
00213     if(sps->vcl_hrd_parameters_present_flag)
00214         if(decode_hrd_parameters(h, sps) < 0)
00215             return -1;
00216     if(sps->nal_hrd_parameters_present_flag || sps->vcl_hrd_parameters_present_flag)
00217         get_bits1(&s->gb);     /* low_delay_hrd_flag */
00218     sps->pic_struct_present_flag = get_bits1(&s->gb);
00219 
00220     sps->bitstream_restriction_flag = get_bits1(&s->gb);
00221     if(sps->bitstream_restriction_flag){
00222         get_bits1(&s->gb);     /* motion_vectors_over_pic_boundaries_flag */
00223         get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
00224         get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
00225         get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
00226         get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
00227         sps->num_reorder_frames= get_ue_golomb(&s->gb);
00228         get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
00229 
00230         if (get_bits_left(&s->gb) < 0) {
00231             sps->num_reorder_frames=0;
00232             sps->bitstream_restriction_flag= 0;
00233         }
00234 
00235         if(sps->num_reorder_frames > 16U /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
00236             av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", sps->num_reorder_frames);
00237             return -1;
00238         }
00239     }
00240     if (get_bits_left(&s->gb) < 0) {
00241         av_log(h->s.avctx, AV_LOG_ERROR, "Overread VUI by %d bits\n", -get_bits_left(&s->gb));
00242         return AVERROR_INVALIDDATA;
00243     }
00244 
00245     return 0;
00246 }
00247 
00248 static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
00249                                 const uint8_t *jvt_list, const uint8_t *fallback_list){
00250     MpegEncContext * const s = &h->s;
00251     int i, last = 8, next = 8;
00252     const uint8_t *scan = size == 16 ? zigzag_scan : ff_zigzag_direct;
00253     if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
00254         memcpy(factors, fallback_list, size*sizeof(uint8_t));
00255     else
00256     for(i=0;i<size;i++){
00257         if(next)
00258             next = (last + get_se_golomb(&s->gb)) & 0xff;
00259         if(!i && !next){ /* matrix not written, we use the preset one */
00260             memcpy(factors, jvt_list, size*sizeof(uint8_t));
00261             break;
00262         }
00263         last = factors[scan[i]] = next ? next : last;
00264     }
00265 }
00266 
00267 static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
00268                                    uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
00269     MpegEncContext * const s = &h->s;
00270     int fallback_sps = !is_sps && sps->scaling_matrix_present;
00271     const uint8_t *fallback[4] = {
00272         fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
00273         fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
00274         fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
00275         fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1]
00276     };
00277     if(get_bits1(&s->gb)){
00278         sps->scaling_matrix_present |= is_sps;
00279         decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
00280         decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
00281         decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
00282         decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
00283         decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
00284         decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
00285         if(is_sps || pps->transform_8x8_mode){
00286             decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]);  // Intra, Y
00287             if(sps->chroma_format_idc == 3){
00288                 decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[0],scaling_matrix8[0]);  // Intra, Cr
00289                 decode_scaling_list(h,scaling_matrix8[2],64,default_scaling8[0],scaling_matrix8[1]);  // Intra, Cb
00290             }
00291             decode_scaling_list(h,scaling_matrix8[3],64,default_scaling8[1],fallback[3]);  // Inter, Y
00292             if(sps->chroma_format_idc == 3){
00293                 decode_scaling_list(h,scaling_matrix8[4],64,default_scaling8[1],scaling_matrix8[3]);  // Inter, Cr
00294                 decode_scaling_list(h,scaling_matrix8[5],64,default_scaling8[1],scaling_matrix8[4]);  // Inter, Cb
00295             }
00296         }
00297     }
00298 }
00299 
00300 int ff_h264_decode_seq_parameter_set(H264Context *h){
00301     MpegEncContext * const s = &h->s;
00302     int profile_idc, level_idc, constraint_set_flags = 0;
00303     unsigned int sps_id;
00304     int i;
00305     SPS *sps;
00306 
00307     profile_idc= get_bits(&s->gb, 8);
00308     constraint_set_flags |= get_bits1(&s->gb) << 0;   //constraint_set0_flag
00309     constraint_set_flags |= get_bits1(&s->gb) << 1;   //constraint_set1_flag
00310     constraint_set_flags |= get_bits1(&s->gb) << 2;   //constraint_set2_flag
00311     constraint_set_flags |= get_bits1(&s->gb) << 3;   //constraint_set3_flag
00312     get_bits(&s->gb, 4); // reserved
00313     level_idc= get_bits(&s->gb, 8);
00314     sps_id= get_ue_golomb_31(&s->gb);
00315 
00316     if(sps_id >= MAX_SPS_COUNT) {
00317         av_log(h->s.avctx, AV_LOG_ERROR, "sps_id (%d) out of range\n", sps_id);
00318         return -1;
00319     }
00320     sps= av_mallocz(sizeof(SPS));
00321     if(sps == NULL)
00322         return -1;
00323 
00324     sps->time_offset_length = 24;
00325     sps->profile_idc= profile_idc;
00326     sps->constraint_set_flags = constraint_set_flags;
00327     sps->level_idc= level_idc;
00328 
00329     memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
00330     memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
00331     sps->scaling_matrix_present = 0;
00332 
00333     if(sps->profile_idc >= 100){ //high profile
00334         sps->chroma_format_idc= get_ue_golomb_31(&s->gb);
00335         if(sps->chroma_format_idc > 3) {
00336             av_log(h->s.avctx, AV_LOG_ERROR, "chroma_format_idc (%u) out of range\n", sps->chroma_format_idc);
00337             return -1;
00338         } else if(sps->chroma_format_idc == 3) {
00339             sps->residual_color_transform_flag = get_bits1(&s->gb);
00340         }
00341         sps->bit_depth_luma   = get_ue_golomb(&s->gb) + 8;
00342         sps->bit_depth_chroma = get_ue_golomb(&s->gb) + 8;
00343         sps->transform_bypass = get_bits1(&s->gb);
00344         decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
00345     }else{
00346         sps->chroma_format_idc= 1;
00347         sps->bit_depth_luma   = 8;
00348         sps->bit_depth_chroma = 8;
00349     }
00350 
00351     sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
00352     sps->poc_type= get_ue_golomb_31(&s->gb);
00353 
00354     if(sps->poc_type == 0){ //FIXME #define
00355         sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
00356     } else if(sps->poc_type == 1){//FIXME #define
00357         sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
00358         sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
00359         sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
00360         sps->poc_cycle_length                = get_ue_golomb(&s->gb);
00361 
00362         if((unsigned)sps->poc_cycle_length >= FF_ARRAY_ELEMS(sps->offset_for_ref_frame)){
00363             av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", sps->poc_cycle_length);
00364             goto fail;
00365         }
00366 
00367         for(i=0; i<sps->poc_cycle_length; i++)
00368             sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
00369     }else if(sps->poc_type != 2){
00370         av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
00371         goto fail;
00372     }
00373 
00374     sps->ref_frame_count= get_ue_golomb_31(&s->gb);
00375     if(sps->ref_frame_count > MAX_PICTURE_COUNT-2 || sps->ref_frame_count >= 32U){
00376         av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
00377         goto fail;
00378     }
00379     sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
00380     sps->mb_width = get_ue_golomb(&s->gb) + 1;
00381     sps->mb_height= get_ue_golomb(&s->gb) + 1;
00382     if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
00383        av_image_check_size(16*sps->mb_width, 16*sps->mb_height, 0, h->s.avctx)){
00384         av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
00385         goto fail;
00386     }
00387 
00388     sps->frame_mbs_only_flag= get_bits1(&s->gb);
00389     if(!sps->frame_mbs_only_flag)
00390         sps->mb_aff= get_bits1(&s->gb);
00391     else
00392         sps->mb_aff= 0;
00393 
00394     sps->direct_8x8_inference_flag= get_bits1(&s->gb);
00395     if(!sps->frame_mbs_only_flag && !sps->direct_8x8_inference_flag){
00396         av_log(h->s.avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
00397         goto fail;
00398     }
00399 
00400 #ifndef ALLOW_INTERLACE
00401     if(sps->mb_aff)
00402         av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
00403 #endif
00404     sps->crop= get_bits1(&s->gb);
00405     if(sps->crop){
00406         int crop_vertical_limit   = sps->chroma_format_idc  & 2 ? 16 : 8;
00407         int crop_horizontal_limit = sps->chroma_format_idc == 3 ? 16 : 8;
00408         sps->crop_left  = get_ue_golomb(&s->gb);
00409         sps->crop_right = get_ue_golomb(&s->gb);
00410         sps->crop_top   = get_ue_golomb(&s->gb);
00411         sps->crop_bottom= get_ue_golomb(&s->gb);
00412         if(sps->crop_left || sps->crop_top){
00413             av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
00414         }
00415         if(sps->crop_right >= crop_horizontal_limit || sps->crop_bottom >= crop_vertical_limit){
00416             av_log(h->s.avctx, AV_LOG_ERROR, "brainfart cropping not supported, this could look slightly wrong ...\n");
00417         }
00418     }else{
00419         sps->crop_left  =
00420         sps->crop_right =
00421         sps->crop_top   =
00422         sps->crop_bottom= 0;
00423     }
00424 
00425     sps->vui_parameters_present_flag= get_bits1(&s->gb);
00426     if( sps->vui_parameters_present_flag )
00427         if (decode_vui_parameters(h, sps) < 0)
00428             goto fail;
00429 
00430     if(!sps->sar.den)
00431         sps->sar.den= 1;
00432 
00433     if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00434         av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s %s %d/%d\n",
00435                sps_id, sps->profile_idc, sps->level_idc,
00436                sps->poc_type,
00437                sps->ref_frame_count,
00438                sps->mb_width, sps->mb_height,
00439                sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
00440                sps->direct_8x8_inference_flag ? "8B8" : "",
00441                sps->crop_left, sps->crop_right,
00442                sps->crop_top, sps->crop_bottom,
00443                sps->vui_parameters_present_flag ? "VUI" : "",
00444                ((const char*[]){"Gray","420","422","444"})[sps->chroma_format_idc],
00445                sps->timing_info_present_flag ? sps->num_units_in_tick : 0,
00446                sps->timing_info_present_flag ? sps->time_scale : 0
00447                );
00448     }
00449 
00450     av_free(h->sps_buffers[sps_id]);
00451     h->sps_buffers[sps_id]= sps;
00452     h->sps = *sps;
00453     return 0;
00454 fail:
00455     av_free(sps);
00456     return -1;
00457 }
00458 
00459 static void
00460 build_qp_table(PPS *pps, int t, int index, const int depth)
00461 {
00462     int i;
00463     const int max_qp = 51 + 6*(depth-8);
00464     for(i = 0; i < max_qp+1; i++)
00465         pps->chroma_qp_table[t][i] = ff_h264_chroma_qp[depth-8][av_clip(i + index, 0, max_qp)];
00466 }
00467 
00468 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length){
00469     MpegEncContext * const s = &h->s;
00470     unsigned int pps_id= get_ue_golomb(&s->gb);
00471     PPS *pps;
00472     const int qp_bd_offset = 6*(h->sps.bit_depth_luma-8);
00473     int bits_left;
00474 
00475     if(pps_id >= MAX_PPS_COUNT) {
00476         av_log(h->s.avctx, AV_LOG_ERROR, "pps_id (%d) out of range\n", pps_id);
00477         return -1;
00478     } else if (h->sps.bit_depth_luma > 10) {
00479         av_log(h->s.avctx, AV_LOG_ERROR, "Unimplemented luma bit depth=%d (max=10)\n", h->sps.bit_depth_luma);
00480         return AVERROR_PATCHWELCOME;
00481     }
00482 
00483     pps= av_mallocz(sizeof(PPS));
00484     if(pps == NULL)
00485         return -1;
00486     pps->sps_id= get_ue_golomb_31(&s->gb);
00487     if((unsigned)pps->sps_id>=MAX_SPS_COUNT || h->sps_buffers[pps->sps_id] == NULL){
00488         av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
00489         goto fail;
00490     }
00491 
00492     pps->cabac= get_bits1(&s->gb);
00493     pps->pic_order_present= get_bits1(&s->gb);
00494     pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
00495     if(pps->slice_group_count > 1 ){
00496         pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
00497         av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
00498         switch(pps->mb_slice_group_map_type){
00499         case 0:
00500 #if 0
00501 |   for( i = 0; i <= num_slice_groups_minus1; i++ ) |   |        |
00502 |    run_length[ i ]                                |1  |ue(v)   |
00503 #endif
00504             break;
00505         case 2:
00506 #if 0
00507 |   for( i = 0; i < num_slice_groups_minus1; i++ )  |   |        |
00508 |{                                                  |   |        |
00509 |    top_left_mb[ i ]                               |1  |ue(v)   |
00510 |    bottom_right_mb[ i ]                           |1  |ue(v)   |
00511 |   }                                               |   |        |
00512 #endif
00513             break;
00514         case 3:
00515         case 4:
00516         case 5:
00517 #if 0
00518 |   slice_group_change_direction_flag               |1  |u(1)    |
00519 |   slice_group_change_rate_minus1                  |1  |ue(v)   |
00520 #endif
00521             break;
00522         case 6:
00523 #if 0
00524 |   slice_group_id_cnt_minus1                       |1  |ue(v)   |
00525 |   for( i = 0; i <= slice_group_id_cnt_minus1; i++ |   |        |
00526 |)                                                  |   |        |
00527 |    slice_group_id[ i ]                            |1  |u(v)    |
00528 #endif
00529             break;
00530         }
00531     }
00532     pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
00533     pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
00534     if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
00535         av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
00536         goto fail;
00537     }
00538 
00539     pps->weighted_pred= get_bits1(&s->gb);
00540     pps->weighted_bipred_idc= get_bits(&s->gb, 2);
00541     pps->init_qp= get_se_golomb(&s->gb) + 26 + qp_bd_offset;
00542     pps->init_qs= get_se_golomb(&s->gb) + 26 + qp_bd_offset;
00543     pps->chroma_qp_index_offset[0]= get_se_golomb(&s->gb);
00544     pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
00545     pps->constrained_intra_pred= get_bits1(&s->gb);
00546     pps->redundant_pic_cnt_present = get_bits1(&s->gb);
00547 
00548     pps->transform_8x8_mode= 0;
00549     h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
00550     memcpy(pps->scaling_matrix4, h->sps_buffers[pps->sps_id]->scaling_matrix4, sizeof(pps->scaling_matrix4));
00551     memcpy(pps->scaling_matrix8, h->sps_buffers[pps->sps_id]->scaling_matrix8, sizeof(pps->scaling_matrix8));
00552 
00553     bits_left = bit_length - get_bits_count(&s->gb);
00554     if (bits_left && (bits_left > 8 ||
00555                       show_bits(&s->gb, bits_left) != 1 << (bits_left - 1))) {
00556         pps->transform_8x8_mode= get_bits1(&s->gb);
00557         decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
00558         pps->chroma_qp_index_offset[1]= get_se_golomb(&s->gb); //second_chroma_qp_index_offset
00559     } else {
00560         pps->chroma_qp_index_offset[1]= pps->chroma_qp_index_offset[0];
00561     }
00562 
00563     build_qp_table(pps, 0, pps->chroma_qp_index_offset[0], h->sps.bit_depth_luma);
00564     build_qp_table(pps, 1, pps->chroma_qp_index_offset[1], h->sps.bit_depth_luma);
00565     if(pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
00566         pps->chroma_qp_diff= 1;
00567 
00568     if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00569         av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%u sps:%u %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d/%d %s %s %s %s\n",
00570                pps_id, pps->sps_id,
00571                pps->cabac ? "CABAC" : "CAVLC",
00572                pps->slice_group_count,
00573                pps->ref_count[0], pps->ref_count[1],
00574                pps->weighted_pred ? "weighted" : "",
00575                pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
00576                pps->deblocking_filter_parameters_present ? "LPAR" : "",
00577                pps->constrained_intra_pred ? "CONSTR" : "",
00578                pps->redundant_pic_cnt_present ? "REDU" : "",
00579                pps->transform_8x8_mode ? "8x8DCT" : ""
00580                );
00581     }
00582 
00583     av_free(h->pps_buffers[pps_id]);
00584     h->pps_buffers[pps_id]= pps;
00585     return 0;
00586 fail:
00587     av_free(pps);
00588     return -1;
00589 }