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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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[5][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         0,  1, 2, 3,  4,  5,
00088         6,  7, 8, 9, 10, 11,
00089         12,13,14,15, 16, 17,
00090         CHROMA_QP_TABLE_END(11)
00091     },
00092     {
00093         0,  1, 2, 3,  4,  5,
00094         6,  7, 8, 9, 10, 11,
00095         12,13,14,15, 16, 17,
00096         18,19,20,21, 22, 23,
00097         CHROMA_QP_TABLE_END(12)
00098     },
00099 };
00100 
00101 static const uint8_t default_scaling4[2][16]={
00102 {   6,13,20,28,
00103    13,20,28,32,
00104    20,28,32,37,
00105    28,32,37,42
00106 },{
00107    10,14,20,24,
00108    14,20,24,27,
00109    20,24,27,30,
00110    24,27,30,34
00111 }};
00112 
00113 static const uint8_t default_scaling8[2][64]={
00114 {   6,10,13,16,18,23,25,27,
00115    10,11,16,18,23,25,27,29,
00116    13,16,18,23,25,27,29,31,
00117    16,18,23,25,27,29,31,33,
00118    18,23,25,27,29,31,33,36,
00119    23,25,27,29,31,33,36,38,
00120    25,27,29,31,33,36,38,40,
00121    27,29,31,33,36,38,40,42
00122 },{
00123     9,13,15,17,19,21,22,24,
00124    13,13,17,19,21,22,24,25,
00125    15,17,19,21,22,24,25,27,
00126    17,19,21,22,24,25,27,28,
00127    19,21,22,24,25,27,28,30,
00128    21,22,24,25,27,28,30,32,
00129    22,24,25,27,28,30,32,33,
00130    24,25,27,28,30,32,33,35
00131 }};
00132 
00133 static inline int decode_hrd_parameters(H264Context *h, SPS *sps){
00134     MpegEncContext * const s = &h->s;
00135     int cpb_count, i;
00136     cpb_count = get_ue_golomb_31(&s->gb) + 1;
00137 
00138     if(cpb_count > 32U){
00139         av_log(h->s.avctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
00140         return -1;
00141     }
00142 
00143     get_bits(&s->gb, 4); /* bit_rate_scale */
00144     get_bits(&s->gb, 4); /* cpb_size_scale */
00145     for(i=0; i<cpb_count; i++){
00146         get_ue_golomb_long(&s->gb); /* bit_rate_value_minus1 */
00147         get_ue_golomb_long(&s->gb); /* cpb_size_value_minus1 */
00148         get_bits1(&s->gb);     /* cbr_flag */
00149     }
00150     sps->initial_cpb_removal_delay_length = get_bits(&s->gb, 5) + 1;
00151     sps->cpb_removal_delay_length = get_bits(&s->gb, 5) + 1;
00152     sps->dpb_output_delay_length = get_bits(&s->gb, 5) + 1;
00153     sps->time_offset_length = get_bits(&s->gb, 5);
00154     sps->cpb_cnt = cpb_count;
00155     return 0;
00156 }
00157 
00158 static inline int decode_vui_parameters(H264Context *h, SPS *sps){
00159     MpegEncContext * const s = &h->s;
00160     int aspect_ratio_info_present_flag;
00161     unsigned int aspect_ratio_idc;
00162 
00163     aspect_ratio_info_present_flag= get_bits1(&s->gb);
00164 
00165     if( aspect_ratio_info_present_flag ) {
00166         aspect_ratio_idc= get_bits(&s->gb, 8);
00167         if( aspect_ratio_idc == EXTENDED_SAR ) {
00168             sps->sar.num= get_bits(&s->gb, 16);
00169             sps->sar.den= get_bits(&s->gb, 16);
00170         }else if(aspect_ratio_idc < FF_ARRAY_ELEMS(pixel_aspect)){
00171             sps->sar=  pixel_aspect[aspect_ratio_idc];
00172         }else{
00173             av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
00174             return -1;
00175         }
00176     }else{
00177         sps->sar.num=
00178         sps->sar.den= 0;
00179     }
00180 //            s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
00181 
00182     if(get_bits1(&s->gb)){      /* overscan_info_present_flag */
00183         get_bits1(&s->gb);      /* overscan_appropriate_flag */
00184     }
00185 
00186     sps->video_signal_type_present_flag = get_bits1(&s->gb);
00187     if(sps->video_signal_type_present_flag){
00188         get_bits(&s->gb, 3);    /* video_format */
00189         sps->full_range = get_bits1(&s->gb); /* video_full_range_flag */
00190 
00191         sps->colour_description_present_flag = get_bits1(&s->gb);
00192         if(sps->colour_description_present_flag){
00193             sps->color_primaries = get_bits(&s->gb, 8); /* colour_primaries */
00194             sps->color_trc       = get_bits(&s->gb, 8); /* transfer_characteristics */
00195             sps->colorspace      = get_bits(&s->gb, 8); /* matrix_coefficients */
00196             if (sps->color_primaries >= AVCOL_PRI_NB)
00197                 sps->color_primaries  = AVCOL_PRI_UNSPECIFIED;
00198             if (sps->color_trc >= AVCOL_TRC_NB)
00199                 sps->color_trc  = AVCOL_TRC_UNSPECIFIED;
00200             if (sps->colorspace >= AVCOL_SPC_NB)
00201                 sps->colorspace  = AVCOL_SPC_UNSPECIFIED;
00202         }
00203     }
00204 
00205     if(get_bits1(&s->gb)){      /* chroma_location_info_present_flag */
00206         s->avctx->chroma_sample_location = get_ue_golomb(&s->gb)+1;  /* chroma_sample_location_type_top_field */
00207         get_ue_golomb(&s->gb);  /* chroma_sample_location_type_bottom_field */
00208     }
00209 
00210     sps->timing_info_present_flag = get_bits1(&s->gb);
00211     if(sps->timing_info_present_flag){
00212         sps->num_units_in_tick = get_bits_long(&s->gb, 32);
00213         sps->time_scale = get_bits_long(&s->gb, 32);
00214         if(!sps->num_units_in_tick || !sps->time_scale){
00215             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);
00216             return -1;
00217         }
00218         sps->fixed_frame_rate_flag = get_bits1(&s->gb);
00219     }
00220 
00221     sps->nal_hrd_parameters_present_flag = get_bits1(&s->gb);
00222     if(sps->nal_hrd_parameters_present_flag)
00223         if(decode_hrd_parameters(h, sps) < 0)
00224             return -1;
00225     sps->vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
00226     if(sps->vcl_hrd_parameters_present_flag)
00227         if(decode_hrd_parameters(h, sps) < 0)
00228             return -1;
00229     if(sps->nal_hrd_parameters_present_flag || sps->vcl_hrd_parameters_present_flag)
00230         get_bits1(&s->gb);     /* low_delay_hrd_flag */
00231     sps->pic_struct_present_flag = get_bits1(&s->gb);
00232     if(!get_bits_left(&s->gb))
00233         return 0;
00234     sps->bitstream_restriction_flag = get_bits1(&s->gb);
00235     if(sps->bitstream_restriction_flag){
00236         get_bits1(&s->gb);     /* motion_vectors_over_pic_boundaries_flag */
00237         get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
00238         get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
00239         get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
00240         get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
00241         sps->num_reorder_frames= get_ue_golomb(&s->gb);
00242         get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
00243 
00244         if (get_bits_left(&s->gb) < 0) {
00245             sps->num_reorder_frames=0;
00246             sps->bitstream_restriction_flag= 0;
00247         }
00248 
00249         if(sps->num_reorder_frames > 16U /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
00250             av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", sps->num_reorder_frames);
00251             return -1;
00252         }
00253     }
00254     if (get_bits_left(&s->gb) < 0) {
00255         av_log(h->s.avctx, AV_LOG_ERROR, "Overread VUI by %d bits\n", -get_bits_left(&s->gb));
00256         return AVERROR_INVALIDDATA;
00257     }
00258 
00259     return 0;
00260 }
00261 
00262 static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
00263                                 const uint8_t *jvt_list, const uint8_t *fallback_list){
00264     MpegEncContext * const s = &h->s;
00265     int i, last = 8, next = 8;
00266     const uint8_t *scan = size == 16 ? zigzag_scan : ff_zigzag_direct;
00267     if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
00268         memcpy(factors, fallback_list, size*sizeof(uint8_t));
00269     else
00270     for(i=0;i<size;i++){
00271         if(next)
00272             next = (last + get_se_golomb(&s->gb)) & 0xff;
00273         if(!i && !next){ /* matrix not written, we use the preset one */
00274             memcpy(factors, jvt_list, size*sizeof(uint8_t));
00275             break;
00276         }
00277         last = factors[scan[i]] = next ? next : last;
00278     }
00279 }
00280 
00281 static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
00282                                    uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
00283     MpegEncContext * const s = &h->s;
00284     int fallback_sps = !is_sps && sps->scaling_matrix_present;
00285     const uint8_t *fallback[4] = {
00286         fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
00287         fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
00288         fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
00289         fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1]
00290     };
00291     if(get_bits1(&s->gb)){
00292         sps->scaling_matrix_present |= is_sps;
00293         decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
00294         decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
00295         decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
00296         decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
00297         decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
00298         decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
00299         if(is_sps || pps->transform_8x8_mode){
00300             decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]);  // Intra, Y
00301             if(sps->chroma_format_idc == 3){
00302                 decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[0],scaling_matrix8[0]);  // Intra, Cr
00303                 decode_scaling_list(h,scaling_matrix8[2],64,default_scaling8[0],scaling_matrix8[1]);  // Intra, Cb
00304             }
00305             decode_scaling_list(h,scaling_matrix8[3],64,default_scaling8[1],fallback[3]);  // Inter, Y
00306             if(sps->chroma_format_idc == 3){
00307                 decode_scaling_list(h,scaling_matrix8[4],64,default_scaling8[1],scaling_matrix8[3]);  // Inter, Cr
00308                 decode_scaling_list(h,scaling_matrix8[5],64,default_scaling8[1],scaling_matrix8[4]);  // Inter, Cb
00309             }
00310         }
00311     }
00312 }
00313 
00314 int ff_h264_decode_seq_parameter_set(H264Context *h){
00315     MpegEncContext * const s = &h->s;
00316     int profile_idc, level_idc, constraint_set_flags = 0;
00317     unsigned int sps_id;
00318     int i;
00319     SPS *sps;
00320 
00321     profile_idc= get_bits(&s->gb, 8);
00322     constraint_set_flags |= get_bits1(&s->gb) << 0;   //constraint_set0_flag
00323     constraint_set_flags |= get_bits1(&s->gb) << 1;   //constraint_set1_flag
00324     constraint_set_flags |= get_bits1(&s->gb) << 2;   //constraint_set2_flag
00325     constraint_set_flags |= get_bits1(&s->gb) << 3;   //constraint_set3_flag
00326     get_bits(&s->gb, 4); // reserved
00327     level_idc= get_bits(&s->gb, 8);
00328     sps_id= get_ue_golomb_31(&s->gb);
00329 
00330     if(sps_id >= MAX_SPS_COUNT) {
00331         av_log(h->s.avctx, AV_LOG_ERROR, "sps_id (%d) out of range\n", sps_id);
00332         return -1;
00333     }
00334     sps= av_mallocz(sizeof(SPS));
00335     if(sps == NULL)
00336         return -1;
00337 
00338     sps->time_offset_length = 24;
00339     sps->profile_idc= profile_idc;
00340     sps->constraint_set_flags = constraint_set_flags;
00341     sps->level_idc= level_idc;
00342     sps->full_range = -1;
00343 
00344     memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
00345     memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
00346     sps->scaling_matrix_present = 0;
00347     sps->colorspace = 2; //AVCOL_SPC_UNSPECIFIED
00348 
00349     if(sps->profile_idc >= 100){ //high profile
00350         sps->chroma_format_idc= get_ue_golomb_31(&s->gb);
00351         if (sps->chroma_format_idc > 3U) {
00352             av_log(h->s.avctx, AV_LOG_ERROR, "chroma_format_idc %d is illegal\n", sps->chroma_format_idc);
00353             goto fail;
00354         }
00355         if(sps->chroma_format_idc == 3)
00356             sps->residual_color_transform_flag = get_bits1(&s->gb);
00357         sps->bit_depth_luma   = get_ue_golomb(&s->gb) + 8;
00358         sps->bit_depth_chroma = get_ue_golomb(&s->gb) + 8;
00359         if (sps->bit_depth_luma > 12U || sps->bit_depth_chroma > 12U) {
00360             av_log(h->s.avctx, AV_LOG_ERROR, "illegal bit depth value (%d, %d)\n",
00361                    sps->bit_depth_luma, sps->bit_depth_chroma);
00362             goto fail;
00363         }
00364         sps->transform_bypass = get_bits1(&s->gb);
00365         decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
00366     }else{
00367         sps->chroma_format_idc= 1;
00368         sps->bit_depth_luma   = 8;
00369         sps->bit_depth_chroma = 8;
00370     }
00371 
00372     sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
00373     sps->poc_type= get_ue_golomb_31(&s->gb);
00374 
00375     if(sps->poc_type == 0){ //FIXME #define
00376         sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
00377     } else if(sps->poc_type == 1){//FIXME #define
00378         sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
00379         sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
00380         sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
00381         sps->poc_cycle_length                = get_ue_golomb(&s->gb);
00382 
00383         if((unsigned)sps->poc_cycle_length >= FF_ARRAY_ELEMS(sps->offset_for_ref_frame)){
00384             av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", sps->poc_cycle_length);
00385             goto fail;
00386         }
00387 
00388         for(i=0; i<sps->poc_cycle_length; i++)
00389             sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
00390     }else if(sps->poc_type != 2){
00391         av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
00392         goto fail;
00393     }
00394 
00395     sps->ref_frame_count= get_ue_golomb_31(&s->gb);
00396     if(sps->ref_frame_count > MAX_PICTURE_COUNT-2 || sps->ref_frame_count > 16U){
00397         av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
00398         goto fail;
00399     }
00400     sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
00401     sps->mb_width = get_ue_golomb(&s->gb) + 1;
00402     sps->mb_height= get_ue_golomb(&s->gb) + 1;
00403     if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
00404        av_image_check_size(16*sps->mb_width, 16*sps->mb_height, 0, h->s.avctx)){
00405         av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
00406         goto fail;
00407     }
00408 
00409     sps->frame_mbs_only_flag= get_bits1(&s->gb);
00410     if(!sps->frame_mbs_only_flag)
00411         sps->mb_aff= get_bits1(&s->gb);
00412     else
00413         sps->mb_aff= 0;
00414 
00415     sps->direct_8x8_inference_flag= get_bits1(&s->gb);
00416 
00417 #ifndef ALLOW_INTERLACE
00418     if(sps->mb_aff)
00419         av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
00420 #endif
00421     sps->crop= get_bits1(&s->gb);
00422     if(sps->crop){
00423         int crop_vertical_limit   = sps->chroma_format_idc  & 2 ? 16 : 8;
00424         int crop_horizontal_limit = sps->chroma_format_idc == 3 ? 16 : 8;
00425         sps->crop_left  = get_ue_golomb(&s->gb);
00426         sps->crop_right = get_ue_golomb(&s->gb);
00427         sps->crop_top   = get_ue_golomb(&s->gb);
00428         sps->crop_bottom= get_ue_golomb(&s->gb);
00429         if(sps->crop_left || sps->crop_top){
00430             av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ... (left: %d, top: %d)\n", sps->crop_left, sps->crop_top);
00431         }
00432         if(sps->crop_right >= crop_horizontal_limit || sps->crop_bottom >= crop_vertical_limit){
00433             av_log(h->s.avctx, AV_LOG_ERROR, "brainfart cropping not supported, cropping disabled (right: %d, bottom: %d)\n", sps->crop_right, sps->crop_bottom);
00434         /* It is very unlikely that partial cropping will make anybody happy.
00435          * Not cropping at all fixes for example playback of Sisvel 3D streams
00436          * in applications supporting Sisvel 3D. */
00437         sps->crop_left  =
00438         sps->crop_right =
00439         sps->crop_top   =
00440         sps->crop_bottom= 0;
00441         }
00442     }else{
00443         sps->crop_left  =
00444         sps->crop_right =
00445         sps->crop_top   =
00446         sps->crop_bottom= 0;
00447     }
00448 
00449     sps->vui_parameters_present_flag= get_bits1(&s->gb);
00450     if( sps->vui_parameters_present_flag )
00451         if (decode_vui_parameters(h, sps) < 0)
00452             goto fail;
00453 
00454     if(!sps->sar.den)
00455         sps->sar.den= 1;
00456 
00457     if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00458         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 b%d\n",
00459                sps_id, sps->profile_idc, sps->level_idc,
00460                sps->poc_type,
00461                sps->ref_frame_count,
00462                sps->mb_width, sps->mb_height,
00463                sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
00464                sps->direct_8x8_inference_flag ? "8B8" : "",
00465                sps->crop_left, sps->crop_right,
00466                sps->crop_top, sps->crop_bottom,
00467                sps->vui_parameters_present_flag ? "VUI" : "",
00468                ((const char*[]){"Gray","420","422","444"})[sps->chroma_format_idc],
00469                sps->timing_info_present_flag ? sps->num_units_in_tick : 0,
00470                sps->timing_info_present_flag ? sps->time_scale : 0,
00471                sps->bit_depth_luma
00472                );
00473     }
00474 
00475     av_free(h->sps_buffers[sps_id]);
00476     h->sps_buffers[sps_id]= sps;
00477     h->sps = *sps;
00478     return 0;
00479 fail:
00480     av_free(sps);
00481     return -1;
00482 }
00483 
00484 static void
00485 build_qp_table(PPS *pps, int t, int index, const int depth)
00486 {
00487     int i;
00488     const int max_qp = 51 + 6*(depth-8);
00489     for(i = 0; i < max_qp+1; i++)
00490         pps->chroma_qp_table[t][i] = ff_h264_chroma_qp[depth-8][av_clip(i + index, 0, max_qp)];
00491 }
00492 
00493 static int more_rbsp_data_in_pps(H264Context *h, PPS *pps)
00494 {
00495     const SPS *sps = h->sps_buffers[pps->sps_id];
00496     int profile_idc = sps->profile_idc;
00497 
00498     if ((profile_idc == 66 || profile_idc == 77 ||
00499          profile_idc == 88) && (sps->constraint_set_flags & 7)) {
00500         av_log(h->s.avctx, AV_LOG_VERBOSE,
00501                "Current profile doesn't provide more RBSP data in PPS, skipping\n");
00502         return 0;
00503     }
00504 
00505     return 1;
00506 }
00507 
00508 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length){
00509     MpegEncContext * const s = &h->s;
00510     unsigned int pps_id= get_ue_golomb(&s->gb);
00511     PPS *pps;
00512     const int qp_bd_offset = 6*(h->sps.bit_depth_luma-8);
00513     int bits_left;
00514 
00515     if(pps_id >= MAX_PPS_COUNT) {
00516         av_log(h->s.avctx, AV_LOG_ERROR, "pps_id (%d) out of range\n", pps_id);
00517         return -1;
00518     } else if (h->sps.bit_depth_luma > 10) {
00519         av_log(h->s.avctx, AV_LOG_ERROR, "Unimplemented luma bit depth=%d (max=10)\n", h->sps.bit_depth_luma);
00520         return AVERROR_PATCHWELCOME;
00521     }
00522 
00523     pps= av_mallocz(sizeof(PPS));
00524     if(pps == NULL)
00525         return -1;
00526     pps->sps_id= get_ue_golomb_31(&s->gb);
00527     if((unsigned)pps->sps_id>=MAX_SPS_COUNT || h->sps_buffers[pps->sps_id] == NULL){
00528         av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
00529         goto fail;
00530     }
00531 
00532     pps->cabac= get_bits1(&s->gb);
00533     pps->pic_order_present= get_bits1(&s->gb);
00534     pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
00535     if(pps->slice_group_count > 1 ){
00536         pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
00537         av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
00538         switch(pps->mb_slice_group_map_type){
00539         case 0:
00540 #if 0
00541 |   for( i = 0; i <= num_slice_groups_minus1; i++ ) |   |        |
00542 |    run_length[ i ]                                |1  |ue(v)   |
00543 #endif
00544             break;
00545         case 2:
00546 #if 0
00547 |   for( i = 0; i < num_slice_groups_minus1; i++ )  |   |        |
00548 |{                                                  |   |        |
00549 |    top_left_mb[ i ]                               |1  |ue(v)   |
00550 |    bottom_right_mb[ i ]                           |1  |ue(v)   |
00551 |   }                                               |   |        |
00552 #endif
00553             break;
00554         case 3:
00555         case 4:
00556         case 5:
00557 #if 0
00558 |   slice_group_change_direction_flag               |1  |u(1)    |
00559 |   slice_group_change_rate_minus1                  |1  |ue(v)   |
00560 #endif
00561             break;
00562         case 6:
00563 #if 0
00564 |   slice_group_id_cnt_minus1                       |1  |ue(v)   |
00565 |   for( i = 0; i <= slice_group_id_cnt_minus1; i++ |   |        |
00566 |)                                                  |   |        |
00567 |    slice_group_id[ i ]                            |1  |u(v)    |
00568 #endif
00569             break;
00570         }
00571     }
00572     pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
00573     pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
00574     if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
00575         av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
00576         goto fail;
00577     }
00578 
00579     pps->weighted_pred= get_bits1(&s->gb);
00580     pps->weighted_bipred_idc= get_bits(&s->gb, 2);
00581     pps->init_qp= get_se_golomb(&s->gb) + 26 + qp_bd_offset;
00582     pps->init_qs= get_se_golomb(&s->gb) + 26 + qp_bd_offset;
00583     pps->chroma_qp_index_offset[0]= get_se_golomb(&s->gb);
00584     pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
00585     pps->constrained_intra_pred= get_bits1(&s->gb);
00586     pps->redundant_pic_cnt_present = get_bits1(&s->gb);
00587 
00588     pps->transform_8x8_mode= 0;
00589     h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
00590     memcpy(pps->scaling_matrix4, h->sps_buffers[pps->sps_id]->scaling_matrix4, sizeof(pps->scaling_matrix4));
00591     memcpy(pps->scaling_matrix8, h->sps_buffers[pps->sps_id]->scaling_matrix8, sizeof(pps->scaling_matrix8));
00592 
00593     bits_left = bit_length - get_bits_count(&s->gb);
00594     if(bits_left > 0 && more_rbsp_data_in_pps(h, pps)){
00595         pps->transform_8x8_mode= get_bits1(&s->gb);
00596         decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
00597         pps->chroma_qp_index_offset[1]= get_se_golomb(&s->gb); //second_chroma_qp_index_offset
00598     } else {
00599         pps->chroma_qp_index_offset[1]= pps->chroma_qp_index_offset[0];
00600     }
00601 
00602     build_qp_table(pps, 0, pps->chroma_qp_index_offset[0], h->sps.bit_depth_luma);
00603     build_qp_table(pps, 1, pps->chroma_qp_index_offset[1], h->sps.bit_depth_luma);
00604     if(pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
00605         pps->chroma_qp_diff= 1;
00606 
00607     if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00608         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",
00609                pps_id, pps->sps_id,
00610                pps->cabac ? "CABAC" : "CAVLC",
00611                pps->slice_group_count,
00612                pps->ref_count[0], pps->ref_count[1],
00613                pps->weighted_pred ? "weighted" : "",
00614                pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
00615                pps->deblocking_filter_parameters_present ? "LPAR" : "",
00616                pps->constrained_intra_pred ? "CONSTR" : "",
00617                pps->redundant_pic_cnt_present ? "REDU" : "",
00618                pps->transform_8x8_mode ? "8x8DCT" : ""
00619                );
00620     }
00621 
00622     av_free(h->pps_buffers[pps_id]);
00623     h->pps_buffers[pps_id]= pps;
00624     return 0;
00625 fail:
00626     av_free(pps);
00627     return -1;
00628 }