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