Main Page | Data Structures | File List | Data Fields | Globals

h264_stream.c

Go to the documentation of this file.
00001 /* 
00002  * h264bitstream - a library for reading and writing H.264 video
00003  * Copyright (C) 2005-2006 Auroras Entertainment, LLC
00004  * 
00005  * Written by Alex Izvorski <aizvorski@gmail.com>
00006  * 
00007  * This library 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  * This library 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 this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #include <stdint.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <math.h>
00026 
00027 #include "bs.h"
00028 #include "h264_stream.h"
00029 
00030 
00031 #define log2(x) ( (1/log(2)) * log( (x) ) )
00032 
00033 
00034 /****** reading ******/
00035 
00040 h264_stream_t* h264_new()
00041 {
00042         h264_stream_t* h = (h264_stream_t*)malloc(sizeof(h264_stream_t));
00043         h->nal = (nal_t*)malloc(sizeof(nal_t));
00044         h->sps = (sps_t*)malloc(sizeof(sps_t));
00045         h->pps = (pps_t*)malloc(sizeof(pps_t));
00046         h->sh = (slice_header_t*)malloc(sizeof(slice_header_t));
00047     return h;
00048 }
00049 
00050 
00055 void h264_free(h264_stream_t* h)
00056 {
00057     free(h->nal);
00058     free(h->sps);
00059     free(h->pps);
00060     free(h->sh);
00061     free(h);
00062 }
00063 
00072 // DEPRECATED - this will be replaced by a similar function with a slightly different API
00073 int find_nal_unit(uint8_t* buf, int size, int* nal_start, int* nal_end)
00074 {
00075     int i;
00076     // find start
00077     *nal_start = 0;
00078     *nal_end = 0;
00079     
00080     i = 0;
00081     while (   //( next_bits( 24 ) != 0x000001 && next_bits( 32 ) != 0x00000001 )
00082         (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) && 
00083         (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0 || buf[i+3] != 0x01) 
00084         )
00085     {
00086         i++; // skip leading zero
00087         if (i+4 >= size) { return 0; } // did not find nal start
00088     }
00089 
00090     if  (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) // ( next_bits( 24 ) != 0x000001 )
00091     {
00092         i++;
00093     }
00094 
00095     if  (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) { /* error, should never happen */ return 0; }
00096     i+= 3;
00097     *nal_start = i;
00098     
00099     while (   //( next_bits( 24 ) != 0x000000 && next_bits( 24 ) != 0x000001 )
00100         (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0) && 
00101         (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) 
00102         )
00103     {
00104         i++;
00105         // FIXME the next line fails when reading a nal that ends exactly at the end of the data
00106         if (i+3 >= size) { *nal_end = size; return -1; } // did not find nal end, stream ended first
00107     }
00108     
00109     *nal_end = i;
00110     return (*nal_end - *nal_start);
00111 }
00112 
00113 
00114 int more_rbsp_data(h264_stream_t* h, bs_t* b) { return !bs_eof(b); };
00115 
00116 uint32_t next_bits(bs_t* b, int n) { return 0; }; // FIXME UNIMPLEMENTED
00117 
00118 
00128 //7.3.1 NAL unit syntax
00129 int read_nal_unit(h264_stream_t* h, uint8_t* buf, int size)
00130 {
00131     nal_t* nal = h->nal;
00132 
00133     bs_t* b = (bs_t*)malloc(sizeof(bs_t));;
00134     bs_init(b, buf, size);
00135 
00136     nal->forbidden_zero_bit = bs_read_f(b,1);
00137     nal->nal_ref_idc = bs_read_u(b,2);
00138     nal->nal_unit_type = bs_read_u(b,5);
00139 
00140     uint8_t* rbsp_buf = (uint8_t*)malloc(size);
00141     int rbsp_size = 0;
00142     int read_size = 0;
00143     int i, j;
00144 
00145     // 7.4.1.1 Encapsulation of an SODB within an RBSP
00146 
00147     i = 1; // NOTE omits first byte which contains nal_ref_idc and nal_unit_type, already read
00148     j = 0;
00149     while( i < size )
00150     {
00151         if( i + 2 < size && 
00152             buf[i] == 0x00 && buf[i+1] == 0x00 && buf[i+2] == 0x03 ) // next_bits( 24 ) == 0x000003
00153         {
00154             rbsp_buf[ j   ] = buf[ i   ];
00155             rbsp_buf[ j+1 ] = buf[ i+1 ];
00156             // buf[ i+2 ] == 0x03  // emulation_prevention_three_byte equal to 0x03 // this is guaranteed from the above condition
00157             i += 3; j += 2;
00158         }
00159         else if (i + 2 < size && 
00160             buf[i] == 0x00 && buf[i+1] == 0x00 && buf[i+2] == 0x01 ) // next_bits( 24 ) == 0x000001 // start of next nal, we're done
00161         {
00162             break;
00163         }
00164         else
00165         {
00166             rbsp_buf[ j ] = buf[ i ];
00167             i += 1; j += 1;
00168         }
00169     }
00170     read_size = i;
00171     rbsp_size = j;
00172     
00173     // end 7.4.1.1
00174 
00175     bs_init(b, rbsp_buf, rbsp_size); // FIXME DEPRECATED reinit of an already inited bs
00176     
00177     if( nal->nal_unit_type == 0) { }                                 //  0    Unspecified
00178     else if( nal->nal_unit_type == 1) { read_slice_layer_rbsp(h, b); }       //  1    Coded slice of a non-IDR picture
00179     else if( nal->nal_unit_type == 2) {  }                           //  2    Coded slice data partition A
00180     else if( nal->nal_unit_type == 3) {  }                           //  3    Coded slice data partition B
00181     else if( nal->nal_unit_type == 4) {  }                           //  4    Coded slice data partition C
00182     else if( nal->nal_unit_type == 5) { read_slice_layer_rbsp(h, b); }       //  5    Coded slice of an IDR picture
00183     else if( nal->nal_unit_type == 6) { /* sei_rbsp( ); */ }         //  6    Supplemental enhancement information (SEI)
00184     else if( nal->nal_unit_type == 7) { read_seq_parameter_set_rbsp(h, b); } //  7    Sequence parameter set
00185     else if( nal->nal_unit_type == 8) { read_pic_parameter_set_rbsp(h, b); } //  8    Picture parameter set
00186     else if( nal->nal_unit_type == 9) { read_access_unit_delimiter_rbsp(h, b); } //  9    Access unit delimiter
00187     else if( nal->nal_unit_type == 10) { read_end_of_seq_rbsp(h, b); }       // 10    End of sequence       
00188     else if( nal->nal_unit_type == 11) { read_end_of_stream_rbsp(h, b); }    // 11    End of stream
00189     else if( nal->nal_unit_type == 12) { /* read_filler_data_rbsp(h, b); */ }      // 12    Filler data
00190     else if( nal->nal_unit_type == 13) { /* seq_parameter_set_extension_rbsp( ) */ } // 13    Sequence parameter set extension
00191                                                                      //14..18 Reserved
00192     else if( nal->nal_unit_type == 19) { read_slice_layer_rbsp(h, b); }      // 19    Coded slice of an auxiliary coded picture without partitioning
00193                                                                       //20..23 Reserved
00194                                                                      //24..31 Unspecified
00195 
00196     free(rbsp_buf);
00197     free(b);
00198 
00199     return read_size;
00200 }
00201 
00202 
00203 //7.3.2.1 Sequence parameter set RBSP syntax
00204 void read_seq_parameter_set_rbsp(h264_stream_t* h, bs_t* b)
00205 {
00206     sps_t* sps = h->sps;
00207 
00208     int i;
00209 
00210     sps->profile_idc = bs_read_u8(b);
00211     sps->constraint_set0_flag = bs_read_u1(b);
00212     sps->constraint_set1_flag = bs_read_u1(b);
00213     sps->constraint_set2_flag = bs_read_u1(b);
00214     sps->constraint_set3_flag = bs_read_u1(b);
00215     sps->reserved_zero_4bits = bs_read_u(b,4);  /* all 0's */
00216     sps->level_idc = bs_read_u8(b);
00217     sps->seq_parameter_set_id = bs_read_ue(b);
00218     if( sps->profile_idc == 100 || sps->profile_idc == 110 ||
00219         sps->profile_idc == 122 || sps->profile_idc == 144 )
00220     {
00221         sps->chroma_format_idc = bs_read_ue(b);
00222         if( sps->chroma_format_idc == 3 )
00223         {
00224             sps->residual_colour_transform_flag = bs_read_u1(b);
00225         }
00226         sps->bit_depth_luma_minus8 = bs_read_ue(b);
00227         sps->bit_depth_chroma_minus8 = bs_read_ue(b);
00228         sps->qpprime_y_zero_transform_bypass_flag = bs_read_u1(b);
00229         sps->seq_scaling_matrix_present_flag = bs_read_u1(b);
00230         if( sps->seq_scaling_matrix_present_flag )
00231         {
00232             for( i = 0; i < 8; i++ )
00233             {
00234                 sps->seq_scaling_list_present_flag[ i ] = bs_read_u1(b);
00235                 if( sps->seq_scaling_list_present_flag[ i ] )
00236                 {
00237                     if( i < 6 )
00238                     {
00239                         read_scaling_list( b, sps->ScalingList4x4[ i ], 16,
00240                                       sps->UseDefaultScalingMatrix4x4Flag[ i ]);
00241                     }
00242                     else
00243                     {
00244                         read_scaling_list( b, sps->ScalingList8x8[ i - 6 ], 64,
00245                                       sps->UseDefaultScalingMatrix8x8Flag[ i - 6 ] );
00246                     }
00247                 }
00248             }
00249         }
00250     }
00251     sps->log2_max_frame_num_minus4 = bs_read_ue(b);
00252     sps->pic_order_cnt_type = bs_read_ue(b);
00253     if( sps->pic_order_cnt_type == 0 )
00254     {
00255         sps->log2_max_pic_order_cnt_lsb_minus4 = bs_read_ue(b);
00256     }
00257     else if( sps->pic_order_cnt_type == 1 )
00258     {
00259         sps->delta_pic_order_always_zero_flag = bs_read_u1(b);
00260         sps->offset_for_non_ref_pic = bs_read_se(b);
00261         sps->offset_for_top_to_bottom_field = bs_read_se(b);
00262         sps->num_ref_frames_in_pic_order_cnt_cycle = bs_read_ue(b);
00263         for( i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++ )
00264         {
00265             sps->offset_for_ref_frame[ i ] = bs_read_se(b);
00266         }
00267     }
00268     sps->num_ref_frames = bs_read_ue(b);
00269     sps->gaps_in_frame_num_value_allowed_flag = bs_read_u1(b);
00270     sps->pic_width_in_mbs_minus1 = bs_read_ue(b);
00271     sps->pic_height_in_map_units_minus1 = bs_read_ue(b);
00272     sps->frame_mbs_only_flag = bs_read_u1(b);
00273     if( !sps->frame_mbs_only_flag )
00274     {
00275         sps->mb_adaptive_frame_field_flag = bs_read_u1(b);
00276     }
00277     sps->direct_8x8_inference_flag = bs_read_u1(b);
00278     sps->frame_cropping_flag = bs_read_u1(b);
00279     if( sps->frame_cropping_flag )
00280     {
00281         sps->frame_crop_left_offset = bs_read_ue(b);
00282         sps->frame_crop_right_offset = bs_read_ue(b);
00283         sps->frame_crop_top_offset = bs_read_ue(b);
00284         sps->frame_crop_bottom_offset = bs_read_ue(b);
00285     }
00286     sps->vui_parameters_present_flag = bs_read_u1(b);
00287     if( sps->vui_parameters_present_flag )
00288     {
00289         read_vui_parameters(h, b);
00290     }
00291     read_rbsp_trailing_bits(h, b);
00292 }
00293 
00294 
00295 //7.3.2.1.1 Scaling list syntax
00296 void read_scaling_list(bs_t* b, int* scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag )
00297 {
00298     int j;
00299 
00300     int lastScale = 8;
00301     int nextScale = 8;
00302     for( j = 0; j < sizeOfScalingList; j++ )
00303     {
00304         if( nextScale != 0 )
00305         {
00306             int delta_scale = bs_read_se(b);
00307             nextScale = ( lastScale + delta_scale + 256 ) % 256;
00308             useDefaultScalingMatrixFlag = ( j == 0 && nextScale == 0 );
00309         }
00310         scalingList[ j ] = ( nextScale == 0 ) ? lastScale : nextScale;
00311         lastScale = scalingList[ j ];
00312     }
00313 }
00314 
00315 //Appendix E.1.1 VUI parameters syntax
00316 void read_vui_parameters(h264_stream_t* h, bs_t* b)
00317 {
00318     sps_t* sps = h->sps;
00319 
00320     sps->vui.aspect_ratio_info_present_flag = bs_read_u1(b);
00321     if( sps->vui.aspect_ratio_info_present_flag )
00322     {
00323         sps->vui.aspect_ratio_idc = bs_read_u8(b);
00324         if( sps->vui.aspect_ratio_idc == SAR_Extended )
00325         {
00326             sps->vui.sar_width = bs_read_u(b,16);
00327             sps->vui.sar_height = bs_read_u(b,16);
00328         }
00329     }
00330     sps->vui.overscan_info_present_flag = bs_read_u1(b);
00331     if( sps->vui.overscan_info_present_flag )
00332     {
00333         sps->vui.overscan_appropriate_flag = bs_read_u1(b);
00334     }
00335     sps->vui.video_signal_type_present_flag = bs_read_u1(b);
00336     if( sps->vui.video_signal_type_present_flag )
00337     {
00338         sps->vui.video_format = bs_read_u(b,3);
00339         sps->vui.video_full_range_flag = bs_read_u1(b);
00340         sps->vui.colour_description_present_flag = bs_read_u1(b);
00341         if( sps->vui.colour_description_present_flag )
00342         {
00343             sps->vui.colour_primaries = bs_read_u8(b);
00344             sps->vui.transfer_characteristics = bs_read_u8(b);
00345             sps->vui.matrix_coefficients = bs_read_u8(b);
00346         }
00347     }
00348     sps->vui.chroma_loc_info_present_flag = bs_read_u1(b);
00349     if( sps->vui.chroma_loc_info_present_flag )
00350     {
00351         sps->vui.chroma_sample_loc_type_top_field = bs_read_ue(b);
00352         sps->vui.chroma_sample_loc_type_bottom_field = bs_read_ue(b);
00353     }
00354     sps->vui.timing_info_present_flag = bs_read_u1(b);
00355     if( sps->vui.timing_info_present_flag )
00356     {
00357         sps->vui.num_units_in_tick = bs_read_u(b,32);
00358         sps->vui.time_scale = bs_read_u(b,32);
00359         sps->vui.fixed_frame_rate_flag = bs_read_u1(b);
00360     }
00361     sps->vui.nal_hrd_parameters_present_flag = bs_read_u1(b);
00362     if( sps->vui.nal_hrd_parameters_present_flag )
00363     {
00364         read_hrd_parameters(h, b);
00365     }
00366     sps->vui.vcl_hrd_parameters_present_flag = bs_read_u1(b);
00367     if( sps->vui.vcl_hrd_parameters_present_flag )
00368     {
00369         read_hrd_parameters(h, b);
00370     }
00371     if( sps->vui.nal_hrd_parameters_present_flag || sps->vui.vcl_hrd_parameters_present_flag )
00372     {
00373         sps->vui.low_delay_hrd_flag = bs_read_u1(b);
00374     }
00375     sps->vui.pic_struct_present_flag = bs_read_u1(b);
00376     sps->vui.bitstream_restriction_flag = bs_read_u1(b);
00377     if( sps->vui.bitstream_restriction_flag )
00378     {
00379         sps->vui.motion_vectors_over_pic_boundaries_flag = bs_read_u1(b);
00380         sps->vui.max_bytes_per_pic_denom = bs_read_ue(b);
00381         sps->vui.max_bits_per_mb_denom = bs_read_ue(b);
00382         sps->vui.log2_max_mv_length_horizontal = bs_read_ue(b);
00383         sps->vui.log2_max_mv_length_vertical = bs_read_ue(b);
00384         sps->vui.num_reorder_frames = bs_read_ue(b);
00385         sps->vui.max_dec_frame_buffering = bs_read_ue(b);
00386     }
00387 }
00388 
00389 
00390 //Appendix E.1.2 HRD parameters syntax
00391 void read_hrd_parameters(h264_stream_t* h, bs_t* b)
00392 {
00393     sps_t* sps = h->sps;
00394     int SchedSelIdx;
00395 
00396     sps->hrd.cpb_cnt_minus1 = bs_read_ue(b);
00397     sps->hrd.bit_rate_scale = bs_read_u(b,4);
00398     sps->hrd.cpb_size_scale = bs_read_u(b,4);
00399     for( SchedSelIdx = 0; SchedSelIdx <= sps->hrd.cpb_cnt_minus1; SchedSelIdx++ )
00400     {
00401         sps->hrd.bit_rate_value_minus1[ SchedSelIdx ] = bs_read_ue(b);
00402         sps->hrd.cpb_size_value_minus1[ SchedSelIdx ] = bs_read_ue(b);
00403         sps->hrd.cbr_flag[ SchedSelIdx ] = bs_read_u1(b);
00404     }
00405     sps->hrd.initial_cpb_removal_delay_length_minus1 = bs_read_u(b,5);
00406     sps->hrd.cpb_removal_delay_length_minus1 = bs_read_u(b,5);
00407     sps->hrd.dpb_output_delay_length_minus1 = bs_read_u(b,5);
00408     sps->hrd.time_offset_length = bs_read_u(b,5);
00409 }
00410 
00411 
00412 /*
00413 UNIMPLEMENTED
00414 //7.3.2.1.2 Sequence parameter set extension RBSP syntax
00415 int read_seq_parameter_set_extension_rbsp(bs_t* b, sps_ext_t* sps_ext) {
00416     seq_parameter_set_id = bs_read_ue(b);
00417     aux_format_idc = bs_read_ue(b);
00418     if( aux_format_idc != 0 ) {
00419         bit_depth_aux_minus8 = bs_read_ue(b);
00420         alpha_incr_flag = bs_read_u1(b);
00421         alpha_opaque_value = bs_read_u(v);
00422         alpha_transparent_value = bs_read_u(v);
00423     }
00424     additional_extension_flag = bs_read_u1(b);
00425     read_rbsp_trailing_bits();
00426 }
00427 */
00428 
00429 //7.3.2.2 Picture parameter set RBSP syntax
00430 void read_pic_parameter_set_rbsp(h264_stream_t* h, bs_t* b)
00431 {
00432     pps_t* pps = h->pps;
00433 
00434     int i;
00435     int i_group;
00436 
00437     pps->pic_parameter_set_id = bs_read_ue(b);
00438     pps->seq_parameter_set_id = bs_read_ue(b);
00439     pps->entropy_coding_mode_flag = bs_read_u1(b);
00440     pps->pic_order_present_flag = bs_read_u1(b);
00441     pps->num_slice_groups_minus1 = bs_read_ue(b);
00442     if( pps->num_slice_groups_minus1 > 0 )
00443     {
00444         pps->slice_group_map_type = bs_read_ue(b);
00445         if( pps->slice_group_map_type == 0 )
00446         {
00447             for( i_group = 0; i_group <= pps->num_slice_groups_minus1; i_group++ )
00448             {
00449                 pps->run_length_minus1[ i_group ] = bs_read_ue(b);
00450             }
00451         }
00452         else if( pps->slice_group_map_type == 2 )
00453         {
00454             for( i_group = 0; i_group < pps->num_slice_groups_minus1; i_group++ )
00455             {
00456                 pps->top_left[ i_group ] = bs_read_ue(b);
00457                 pps->bottom_right[ i_group ] = bs_read_ue(b);
00458             }
00459         }
00460         else if( pps->slice_group_map_type == 3 ||
00461                  pps->slice_group_map_type == 4 ||
00462                  pps->slice_group_map_type == 5 )
00463         {
00464             pps->slice_group_change_direction_flag = bs_read_u1(b);
00465             pps->slice_group_change_rate_minus1 = bs_read_ue(b);
00466         }
00467         else if( pps->slice_group_map_type == 6 )
00468         {
00469             pps->pic_size_in_map_units_minus1 = bs_read_ue(b);
00470             for( i = 0; i <= pps->pic_size_in_map_units_minus1; i++ )
00471             {
00472                 pps->slice_group_id[ i ] = bs_read_u(b, ceil( log2( pps->num_slice_groups_minus1 + 1 ) ) ); // was u(v)
00473             }
00474         }
00475     }
00476     pps->num_ref_idx_l0_active_minus1 = bs_read_ue(b);
00477     pps->num_ref_idx_l1_active_minus1 = bs_read_ue(b);
00478     pps->weighted_pred_flag = bs_read_u1(b);
00479     pps->weighted_bipred_idc = bs_read_u(b,2);
00480     pps->pic_init_qp_minus26 = bs_read_se(b);
00481     pps->pic_init_qs_minus26 = bs_read_se(b);
00482     pps->chroma_qp_index_offset = bs_read_se(b);
00483     pps->deblocking_filter_control_present_flag = bs_read_u1(b);
00484     pps->constrained_intra_pred_flag = bs_read_u1(b);
00485     pps->redundant_pic_cnt_present_flag = bs_read_u1(b);
00486     if( more_rbsp_data(h, b) )
00487     {
00488         pps->transform_8x8_mode_flag = bs_read_u1(b);
00489         pps->pic_scaling_matrix_present_flag = bs_read_u1(b);
00490         if( pps->pic_scaling_matrix_present_flag )
00491         {
00492             for( i = 0; i < 6 + 2* pps->transform_8x8_mode_flag; i++ )
00493             {
00494                 pps->pic_scaling_list_present_flag[ i ] = bs_read_u1(b);
00495                 if( pps->pic_scaling_list_present_flag[ i ] )
00496                 {
00497                     if( i < 6 )
00498                     {
00499                         read_scaling_list( b, pps->ScalingList4x4[ i ], 16,
00500                                       pps->UseDefaultScalingMatrix4x4Flag[ i ] );
00501                     }
00502                     else
00503                     {
00504                         read_scaling_list( b, pps->ScalingList8x8[ i - 6 ], 64,
00505                                       pps->UseDefaultScalingMatrix8x8Flag[ i - 6 ] );
00506                     }
00507                 }
00508             }
00509         }
00510         pps->second_chroma_qp_index_offset = bs_read_se(b);
00511     }
00512     read_rbsp_trailing_bits(h, b);
00513 }
00514 
00515 /*
00516 // UNIMPLEMENTED
00517 //7.3.2.3 Supplemental enhancement information RBSP syntax
00518 read_sei_rbsp( ) {
00519     do {
00520         read_sei_message( );
00521     } while( more_rbsp_data( ) );
00522     read_rbsp_trailing_bits( );
00523 }
00524 
00525 //7.3.2.3.1 Supplemental enhancement information message syntax
00526 read_sei_message( ) {
00527     payloadType = 0;
00528     while( next_bits( 8 ) == 0xFF ) {
00529         ff_byte = bs_read_f(8); // equal to 0xFF
00530         payloadType += 255;
00531     }
00532     last_payload_type_byte = bs_read_u8(b);
00533     payloadType += last_payload_type_byte;
00534     payloadSize = 0;
00535     while( next_bits( 8 ) == 0xFF ) {
00536         ff_byte = bs_read_f(8); // equal to 0xFF
00537         payloadSize += 255;
00538     }
00539     last_payload_size_byte = bs_read_u8(b);
00540     payloadSize += last_payload_size_byte;
00541     sei_payload( payloadType, payloadSize );
00542 }
00543 */
00544 
00545 //7.3.2.4 Access unit delimiter RBSP syntax
00546 void read_access_unit_delimiter_rbsp(h264_stream_t* h, bs_t* b)
00547 {
00548     int primary_pic_type = bs_read_u(b,3); //FIXME
00549     read_rbsp_trailing_bits(h, b);
00550 }
00551 
00552 //7.3.2.5 End of sequence RBSP syntax
00553 void read_end_of_seq_rbsp(h264_stream_t* h, bs_t* b)
00554 {
00555 }
00556 
00557 //7.3.2.6 End of stream RBSP syntax
00558 void read_end_of_stream_rbsp(h264_stream_t* h, bs_t* b)
00559 {
00560 }
00561 
00562 //7.3.2.7 Filler data RBSP syntax
00563 void read_filler_data_rbsp(h264_stream_t* h, bs_t* b)
00564 {
00565     int ff_byte; //FIXME
00566     while( next_bits(b, 8) == 0xFF )
00567     {
00568         ff_byte = bs_read_f(b,8);  /* equal to 0xFF */ 
00569     }
00570     read_rbsp_trailing_bits(h, b);
00571 }
00572 
00573 //7.3.2.8 Slice layer without partitioning RBSP syntax
00574 void read_slice_layer_rbsp(h264_stream_t* h, bs_t* b)
00575 {
00576     read_slice_header(h, b);
00577 
00578     // DEBUG
00579     //printf("slice data: \n");
00580     //debug_bytes(b->p, b->end - b->p);
00581     //printf("bits left in front: %d \n", b->bits_left);
00582 
00583     // FIXME should read or skip data
00584     //slice_data( ); /* all categories of slice_data( ) syntax */  
00585     //read_rbsp_slice_trailing_bits(h, b);
00586 }
00587 
00588 /*
00589 // UNIMPLEMENTED
00590 //7.3.2.9.1 Slice data partition A RBSP syntax
00591 slice_data_partition_a_layer_rbsp( ) {
00592     read_slice_header( );             // only category 2
00593     slice_id = bs_read_ue(b)
00594     read_slice_data( );               // only category 2
00595     read_rbsp_slice_trailing_bits( ); // only category 2
00596 }
00597 
00598 //7.3.2.9.2 Slice data partition B RBSP syntax
00599 slice_data_partition_b_layer_rbsp( ) {
00600     slice_id = bs_read_ue(b);    // only category 3
00601     if( redundant_pic_cnt_present_flag )
00602         redundant_pic_cnt = bs_read_ue(b);
00603     read_slice_data( );               // only category 3
00604     read_rbsp_slice_trailing_bits( ); // only category 3
00605 }
00606 
00607 //7.3.2.9.3 Slice data partition C RBSP syntax
00608 slice_data_partition_c_layer_rbsp( ) {
00609     slice_id = bs_read_ue(b);    // only category 4
00610     if( redundant_pic_cnt_present_flag )
00611         redundant_pic_cnt = bs_read_ue(b);
00612     read_slice_data( );               // only category 4
00613     rbsp_slice_trailing_bits( ); // only category 4
00614 }
00615 */
00616 
00617 int
00618 more_rbsp_trailing_data(h264_stream_t* h, bs_t* b) { return !bs_eof(b); }
00619 
00620 //7.3.2.10 RBSP slice trailing bits syntax
00621 void read_rbsp_slice_trailing_bits(h264_stream_t* h, bs_t* b)
00622 {
00623     read_rbsp_trailing_bits(h, b);
00624     int cabac_zero_word;
00625     if( h->pps->entropy_coding_mode_flag )
00626     {
00627         while( more_rbsp_trailing_data(h, b) )
00628         {
00629             cabac_zero_word = bs_read_f(b,16); /* equal to 0x0000 */ 
00630         }
00631     }
00632 }
00633 
00634 //7.3.2.11 RBSP trailing bits syntax
00635 void read_rbsp_trailing_bits(h264_stream_t* h, bs_t* b)
00636 {
00637     int rbsp_stop_one_bit;
00638     int rbsp_alignment_zero_bit;
00639     if( !bs_byte_aligned(b) )
00640     {
00641         rbsp_stop_one_bit = bs_read_f(b,1); /* equal to 1 */
00642         while( !bs_byte_aligned(b) )
00643         {
00644             rbsp_alignment_zero_bit = bs_read_f(b,1); /* equal to 0 */ 
00645         }
00646     }
00647 }
00648 
00649 //7.3.3 Slice header syntax
00650 void read_slice_header(h264_stream_t* h, bs_t* b)
00651 {
00652     slice_header_t* sh = h->sh;
00653     sps_t* sps = h->sps;
00654     pps_t* pps = h->pps;
00655     nal_t* nal = h->nal;
00656 
00657     sh->first_mb_in_slice = bs_read_ue(b);
00658     sh->slice_type = bs_read_ue(b);
00659     if (sh->slice_type > 4) { sh->slice_type -= 5; } // FIXME this changes slicetypes to (supposedly) equivalent ones
00660     sh->pic_parameter_set_id = bs_read_ue(b);
00661     sh->frame_num = bs_read_u(b, sps->log2_max_frame_num_minus4 + 4 ); // was u(v)
00662     if( !sps->frame_mbs_only_flag )
00663     {
00664         sh->field_pic_flag = bs_read_u1(b);
00665         if( sh->field_pic_flag )
00666         {
00667             sh->bottom_field_flag = bs_read_u1(b);
00668         }
00669     }
00670     if( nal->nal_unit_type == 5 )
00671     {
00672         sh->idr_pic_id = bs_read_ue(b);
00673     }
00674     if( sps->pic_order_cnt_type == 0 )
00675     {
00676         sh->pic_order_cnt_lsb = bs_read_u(b, sps->log2_max_pic_order_cnt_lsb_minus4 + 4 ); // was u(v)
00677         if( pps->pic_order_present_flag && !sh->field_pic_flag )
00678         {
00679             sh->delta_pic_order_cnt_bottom = bs_read_se(b);
00680         }
00681     }
00682     if( sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag )
00683     {
00684         sh->delta_pic_order_cnt[ 0 ] = bs_read_se(b);
00685         if( pps->pic_order_present_flag && !sh->field_pic_flag )
00686         {
00687             sh->delta_pic_order_cnt[ 1 ] = bs_read_se(b);
00688         }
00689     }
00690     if( pps->redundant_pic_cnt_present_flag )
00691     {
00692         sh->redundant_pic_cnt = bs_read_ue(b);
00693     }
00694     if( sh->slice_type == SH_SLICE_TYPE_B )
00695     {
00696         sh->direct_spatial_mv_pred_flag = bs_read_u1(b);
00697     }
00698     if( sh->slice_type == SH_SLICE_TYPE_P || sh->slice_type == SH_SLICE_TYPE_SP || sh->slice_type == SH_SLICE_TYPE_B )
00699     {
00700         sh->num_ref_idx_active_override_flag = bs_read_u1(b);
00701         if( sh->num_ref_idx_active_override_flag )
00702         {
00703             sh->num_ref_idx_l0_active_minus1 = bs_read_ue(b); // FIXME does this modify the pps?
00704             if( sh->slice_type == SH_SLICE_TYPE_B )
00705             {
00706                 sh->num_ref_idx_l1_active_minus1 = bs_read_ue(b);
00707             }
00708         }
00709     }
00710     read_ref_pic_list_reordering(h, b);
00711     if( ( pps->weighted_pred_flag && ( sh->slice_type == SH_SLICE_TYPE_P || sh->slice_type == SH_SLICE_TYPE_SP ) ) ||
00712         ( pps->weighted_bipred_idc == 1 && sh->slice_type == SH_SLICE_TYPE_B ) )
00713     {
00714         read_pred_weight_table(h, b);
00715     }
00716     if( nal->nal_ref_idc != 0 )
00717     {
00718         read_dec_ref_pic_marking(h, b);
00719     }
00720     if( pps->entropy_coding_mode_flag && sh->slice_type != SH_SLICE_TYPE_I && sh->slice_type != SH_SLICE_TYPE_SI )
00721     {
00722         sh->cabac_init_idc = bs_read_ue(b);
00723     }
00724     sh->slice_qp_delta = bs_read_se(b);
00725     if( sh->slice_type == SH_SLICE_TYPE_SP || sh->slice_type == SH_SLICE_TYPE_SI )
00726     {
00727         if( sh->slice_type == SH_SLICE_TYPE_SP )
00728         {
00729             sh->sp_for_switch_flag = bs_read_u1(b);
00730         }
00731         sh->slice_qs_delta = bs_read_se(b);
00732     }
00733     if( pps->deblocking_filter_control_present_flag )
00734     {
00735         sh->disable_deblocking_filter_idc = bs_read_ue(b);
00736         if( sh->disable_deblocking_filter_idc != 1 )
00737         {
00738             sh->slice_alpha_c0_offset_div2 = bs_read_se(b);
00739             sh->slice_beta_offset_div2 = bs_read_se(b);
00740         }
00741     }
00742     if( pps->num_slice_groups_minus1 > 0 &&
00743         pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5)
00744     {
00745         sh->slice_group_change_cycle = 
00746             bs_read_u(b, ceil( log2( pps->pic_size_in_map_units_minus1 +  
00747                                      pps->slice_group_change_rate_minus1 + 1 ) ) ); // was u(v) // FIXME add 2?
00748     }
00749 }
00750 
00751 //7.3.3.1 Reference picture list reordering syntax
00752 void read_ref_pic_list_reordering(h264_stream_t* h, bs_t* b)
00753 {
00754     slice_header_t* sh = h->sh;
00755 
00756     if( sh->slice_type != SH_SLICE_TYPE_I && sh->slice_type != SH_SLICE_TYPE_SI )
00757     {
00758         sh->rplr.ref_pic_list_reordering_flag_l0 = bs_read_u1(b);
00759         if( sh->rplr.ref_pic_list_reordering_flag_l0 )
00760         {
00761             do
00762             {
00763                 sh->rplr.reordering_of_pic_nums_idc = bs_read_ue(b);
00764                 if( sh->rplr.reordering_of_pic_nums_idc == 0 ||
00765                     sh->rplr.reordering_of_pic_nums_idc == 1 )
00766                 {
00767                     sh->rplr.abs_diff_pic_num_minus1 = bs_read_ue(b);
00768                 }
00769                 else if( sh->rplr.reordering_of_pic_nums_idc == 2 )
00770                 {
00771                     sh->rplr.long_term_pic_num = bs_read_ue(b);
00772                 }
00773             } while( sh->rplr.reordering_of_pic_nums_idc != 3 && ! bs_eof(b) );
00774         }
00775     }
00776     if( sh->slice_type == SH_SLICE_TYPE_B )
00777     {
00778         sh->rplr.ref_pic_list_reordering_flag_l1 = bs_read_u1(b);
00779         if( sh->rplr.ref_pic_list_reordering_flag_l1 )
00780         {
00781             do 
00782             {
00783                 sh->rplr.reordering_of_pic_nums_idc = bs_read_ue(b);
00784                 if( sh->rplr.reordering_of_pic_nums_idc == 0 ||
00785                     sh->rplr.reordering_of_pic_nums_idc == 1 )
00786                 {
00787                     sh->rplr.abs_diff_pic_num_minus1 = bs_read_ue(b);
00788                 }
00789                 else if( sh->rplr.reordering_of_pic_nums_idc == 2 )
00790                 {
00791                     sh->rplr.long_term_pic_num = bs_read_ue(b);
00792                 }
00793             } while( sh->rplr.reordering_of_pic_nums_idc != 3 && ! bs_eof(b) );
00794         }
00795     }
00796 }
00797 
00798 //7.3.3.2 Prediction weight table syntax
00799 void read_pred_weight_table(h264_stream_t* h, bs_t* b)
00800 {
00801     slice_header_t* sh = h->sh;
00802     sps_t* sps = h->sps;
00803     pps_t* pps = h->pps;
00804 
00805     int i, j;
00806 
00807     sh->pwt.luma_log2_weight_denom = bs_read_ue(b);
00808     if( sps->chroma_format_idc != 0 )
00809     {
00810         sh->pwt.chroma_log2_weight_denom = bs_read_ue(b);
00811     }
00812     for( i = 0; i <= pps->num_ref_idx_l0_active_minus1; i++ )
00813     {
00814         sh->pwt.luma_weight_l0_flag = bs_read_u1(b);
00815         if( sh->pwt.luma_weight_l0_flag )
00816         {
00817             sh->pwt.luma_weight_l0[ i ] = bs_read_se(b);
00818             sh->pwt.luma_offset_l0[ i ] = bs_read_se(b);
00819         }
00820         if ( sps->chroma_format_idc != 0 )
00821         {
00822             sh->pwt.chroma_weight_l0_flag = bs_read_u1(b);
00823             if( sh->pwt.chroma_weight_l0_flag )
00824             {
00825                 for( j =0; j < 2; j++ )
00826                 {
00827                     sh->pwt.chroma_weight_l0[ i ][ j ] = bs_read_se(b);
00828                     sh->pwt.chroma_offset_l0[ i ][ j ] = bs_read_se(b);
00829                 }
00830             }
00831         }
00832     }
00833     if( sh->slice_type == SH_SLICE_TYPE_B )
00834     {
00835         for( i = 0; i <= pps->num_ref_idx_l1_active_minus1; i++ )
00836         {
00837             sh->pwt.luma_weight_l1_flag = bs_read_u1(b);
00838             if( sh->pwt.luma_weight_l1_flag )
00839             {
00840                 sh->pwt.luma_weight_l1[ i ] = bs_read_se(b);
00841                 sh->pwt.luma_offset_l1[ i ] = bs_read_se(b);
00842             }
00843             if( sps->chroma_format_idc != 0 )
00844             {
00845                 sh->pwt.chroma_weight_l1_flag = bs_read_u1(b);
00846                 if( sh->pwt.chroma_weight_l1_flag )
00847                 {
00848                     for( j = 0; j < 2; j++ )
00849                     {
00850                         sh->pwt.chroma_weight_l1[ i ][ j ] = bs_read_se(b);
00851                         sh->pwt.chroma_offset_l1[ i ][ j ] = bs_read_se(b);
00852                     }
00853                 }
00854             }
00855         }
00856     }
00857 }
00858 
00859 //7.3.3.3 Decoded reference picture marking syntax
00860 void read_dec_ref_pic_marking(h264_stream_t* h, bs_t* b)
00861 {
00862     slice_header_t* sh = h->sh;
00863 
00864     if( h->nal->nal_unit_type == 5 )
00865     {
00866         sh->drpm.no_output_of_prior_pics_flag = bs_read_u1(b);
00867         sh->drpm.long_term_reference_flag = bs_read_u1(b);
00868     }
00869     else
00870     {
00871         sh->drpm.adaptive_ref_pic_marking_mode_flag = bs_read_u1(b);
00872         if( sh->drpm.adaptive_ref_pic_marking_mode_flag )
00873         {
00874             do
00875             {
00876                 sh->drpm.memory_management_control_operation = bs_read_ue(b);
00877                 if( sh->drpm.memory_management_control_operation == 1 ||
00878                     sh->drpm.memory_management_control_operation == 3 )
00879                 {
00880                     sh->drpm.difference_of_pic_nums_minus1 = bs_read_ue(b);
00881                 }
00882                 if(sh->drpm.memory_management_control_operation == 2 )
00883                 {
00884                     sh->drpm.long_term_pic_num = bs_read_ue(b);
00885                 }
00886                 if( sh->drpm.memory_management_control_operation == 3 ||
00887                     sh->drpm.memory_management_control_operation == 6 )
00888                 {
00889                     sh->drpm.long_term_frame_idx = bs_read_ue(b);
00890                 }
00891                 if( sh->drpm.memory_management_control_operation == 4 )
00892                 {
00893                     sh->drpm.max_long_term_frame_idx_plus1 = bs_read_ue(b);
00894                 }
00895             } while( sh->drpm.memory_management_control_operation != 0 && ! bs_eof(b) );
00896         }
00897     }
00898 }
00899 
00900 /*
00901 // UNIMPLEMENTED
00902 //7.3.4 Slice data syntax
00903 slice_data( )
00904 {
00905     if( pps->entropy_coding_mode_flag )
00906         while( !byte_aligned( ) )
00907             cabac_alignment_one_bit = bs_read_f(1);
00908     CurrMbAddr = first_mb_in_slice * ( 1 + MbaffFrameFlag );
00909     moreDataFlag = 1;
00910     prevMbSkipped = 0;
00911     do {
00912         if( sh->slice_type != SH_SLICE_TYPE_I && ssh->lice_type != SH_SLICE_TYPE_SI )
00913             if( !pps->entropy_coding_mode_flag ) {
00914                 mb_skip_run = bs_read_ue(b);
00915                 prevMbSkipped = ( mb_skip_run > 0 );
00916                 for( i=0; i<mb_skip_run; i++ )
00917                     CurrMbAddr = NextMbAddress( CurrMbAddr );
00918                 moreDataFlag = more_rbsp_data( );
00919             } else {
00920                 mb_skip_flag = bs_read_ae(v);
00921                 moreDataFlag = !mb_skip_flag;
00922             }
00923         if( moreDataFlag ) {
00924             if( MbaffFrameFlag && ( CurrMbAddr % 2 == 0 ||
00925                                     ( CurrMbAddr % 2 == 1 && prevMbSkipped ) ) )
00926                 mb_field_decoding_flag = bs_read_u1(b) | bs_read_ae(v);
00927             macroblock_layer( );
00928         }
00929         if( !pps->entropy_coding_mode_flag )
00930             moreDataFlag = more_rbsp_data( );
00931         else {
00932             if( sh->slice_type != SH_SLICE_TYPE_I && sh->slice_type != SH_SLICE_TYPE_SI )
00933                 prevMbSkipped = mb_skip_flag;
00934             if( MbaffFrameFlag && CurrMbAddr % 2 == 0 )
00935                 moreDataFlag = 1;
00936             else {
00937                 end_of_slice_flag = bs_read_ae(v);
00938                 moreDataFlag = !end_of_slice_flag;
00939             }
00940         }
00941         CurrMbAddr = NextMbAddress( CurrMbAddr );
00942     } while( moreDataFlag );
00943 }
00944 */
00945 
00946 
00947 /****** writing ******/
00948 
00949 #define DBG_START \
00950     bs_t* b2 = (bs_t*)malloc(sizeof(bs_t)); \
00951     bs_init(b2, b->p, b->end - b->p); \
00952     h264_stream_t* h2 = (h264_stream_t*)malloc(sizeof(h264_stream_t));\
00953     h2->sps=h->sps; h2->pps=h->pps; h2->nal=h->nal; h2->sh=h->sh;  \
00954 
00955 #define DBG_END \
00956     free(h2); \
00957     free(b2); \
00958 
00959 #define DBG \
00960   printf("line %d:",  __LINE__ ); \
00961   debug_bs(b); \
00962   b2->p = b2->start; b2->bits_left = 8; \
00963   /* read_slice_header(h2, b2); */\
00964   /* if (h2->sh->drpm.adaptive_ref_pic_marking_mode_flag) { printf(" X"); }; */ \
00965   printf("\n"); \
00966 
00967 
00975 //7.3.1 NAL unit syntax
00976 int write_nal_unit(h264_stream_t* h, uint8_t* buf, int size)
00977 {
00978     nal_t* nal = h->nal;
00979 
00980     bs_t* b = (bs_t*)malloc(sizeof(bs_t));;
00981     bs_init(b, buf, size);
00982 
00983     bs_write_f(b,1, nal->forbidden_zero_bit);
00984     bs_write_u(b,2, nal->nal_ref_idc);
00985     bs_write_u(b,5, nal->nal_unit_type);
00986 
00987     uint8_t* rbsp_buf = (uint8_t*)malloc(size); // NOTE this may have to be slightly larger (4/3 larger, worst case)
00988     int rbsp_size = 0;
00989     int write_size = 0;
00990     int i, j;
00991 
00992     bs_init(b, rbsp_buf, size); // FIXME DEPRECATED reinit of an already inited bs
00993     
00994     if( nal->nal_unit_type == 0) { }                                 //  0    Unspecified
00995     else if( nal->nal_unit_type == 1) { write_slice_layer_rbsp(h, b); }       //  1    Coded slice of a non-IDR picture
00996     else if( nal->nal_unit_type == 2) {  }                           //  2    Coded slice data partition A
00997     else if( nal->nal_unit_type == 3) {  }                           //  3    Coded slice data partition B
00998     else if( nal->nal_unit_type == 4) {  }                           //  4    Coded slice data partition C
00999     else if( nal->nal_unit_type == 5) { write_slice_layer_rbsp(h, b); }       //  5    Coded slice of an IDR picture
01000     else if( nal->nal_unit_type == 6) { /* sei_rbsp( ); */ }         //  6    Supplemental enhancement information (SEI)
01001     else if( nal->nal_unit_type == 7) { write_seq_parameter_set_rbsp(h, b); } //  7    Sequence parameter set
01002     else if( nal->nal_unit_type == 8) { write_pic_parameter_set_rbsp(h, b); } //  8    Picture parameter set
01003     else if( nal->nal_unit_type == 9) { write_access_unit_delimiter_rbsp(h, b); } //  9    Access unit delimiter
01004     else if( nal->nal_unit_type == 10) { write_end_of_seq_rbsp(h, b); }       // 10    End of sequence       
01005     else if( nal->nal_unit_type == 11) { write_end_of_stream_rbsp(h, b); }    // 11    End of stream
01006     else if( nal->nal_unit_type == 12) { /* write_filler_data_rbsp(h, b); */ }      // 12    Filler data
01007     else if( nal->nal_unit_type == 13) { /* seq_parameter_set_extension_rbsp( ) */ } // 13    Sequence parameter set extension
01008                                                                      //14..18 Reserved
01009     else if( nal->nal_unit_type == 19) { write_slice_layer_rbsp(h, b); }      // 19    Coded slice of an auxiliary coded picture without partitioning
01010                                                                       //20..23 Reserved
01011                                                                      //24..31 Unspecified
01012 
01013     rbsp_size = bs_pos(b);
01014 
01015     // 7.4.1.1 Encapsulation of an SODB within an RBSP
01016 
01017     i = 1; // NOTE omits first byte which contains nal_ref_idc and nal_unit_type, already written
01018     j = 0;
01019     while( i < size && j < rbsp_size )
01020     {
01021         if( i + 3 < size && j + 2 < rbsp_size &&
01022             rbsp_buf[j] == 0x00 && rbsp_buf[j+1] == 0x00 && 
01023             ( rbsp_buf[j+2] == 0x01 || rbsp_buf[j+2] == 0x02 || rbsp_buf[j+2] == 0x03 ) ) // next_bits( 24 ) == 0x000001 or 0x000002 or 0x000003
01024         {
01025             buf[ i   ] = rbsp_buf[ j   ];
01026             buf[ i+1 ] = rbsp_buf[ j+1 ];
01027             buf[ i+2 ] = 0x03;  // emulation_prevention_three_byte equal to 0x03
01028             buf[ i+3 ] = rbsp_buf[ j+2 ];
01029             i += 3; j += 2;
01030         }
01031         else if ( j == rbsp_size - 1 && 
01032                   rbsp_buf[ j ] == 0x00 )
01033         {
01034             buf[i] == 0x03; // emulation_prevention_three_byte equal to 0x03 in trailing position
01035             i += 1;
01036         }
01037         else
01038         {
01039             buf[i] = rbsp_buf[ j ];
01040             i += 1; j+= 1;
01041         }
01042     }
01043     write_size = i;
01044 
01045     // end 7.4.1.1
01046 
01047     free(rbsp_buf);
01048     free(b);
01049 
01050     return write_size;
01051 }
01052 
01053 
01054 //7.3.2.1 Sequence parameter set RBSP syntax
01055 void write_seq_parameter_set_rbsp(h264_stream_t* h, bs_t* b)
01056 {
01057     sps_t* sps = h->sps;
01058 
01059     int i;
01060 
01061     bs_write_u8(b, sps->profile_idc);
01062     bs_write_u1(b, sps->constraint_set0_flag);
01063     bs_write_u1(b, sps->constraint_set1_flag);
01064     bs_write_u1(b, sps->constraint_set2_flag);
01065     bs_write_u1(b, sps->constraint_set3_flag);
01066     bs_write_u(b,4, sps->reserved_zero_4bits);  /* all 0's */
01067     bs_write_u8(b, sps->level_idc);
01068     bs_write_ue(b, sps->seq_parameter_set_id);
01069     if( sps->profile_idc == 100 || sps->profile_idc == 110 ||
01070         sps->profile_idc == 122 || sps->profile_idc == 144 )
01071     {
01072         bs_write_ue(b, sps->chroma_format_idc);
01073         if( sps->chroma_format_idc == 3 )
01074         {
01075             bs_write_u1(b, sps->residual_colour_transform_flag);
01076         }
01077         bs_write_ue(b, sps->bit_depth_luma_minus8);
01078         bs_write_ue(b, sps->bit_depth_chroma_minus8);
01079         bs_write_u1(b, sps->qpprime_y_zero_transform_bypass_flag);
01080         bs_write_u1(b, sps->seq_scaling_matrix_present_flag);
01081         if( sps->seq_scaling_matrix_present_flag )
01082         {
01083             for( i = 0; i < 8; i++ )
01084             {
01085                 bs_write_u1(b, sps->seq_scaling_list_present_flag[ i ]);
01086                 if( sps->seq_scaling_list_present_flag[ i ] )
01087                 {
01088                     if( i < 6 )
01089                     {
01090                         write_scaling_list( b, sps->ScalingList4x4[ i ], 16,
01091                                       sps->UseDefaultScalingMatrix4x4Flag[ i ]);
01092                     }
01093                     else
01094                     {
01095                         write_scaling_list( b, sps->ScalingList8x8[ i - 6 ], 64,
01096                                       sps->UseDefaultScalingMatrix8x8Flag[ i - 6 ] );
01097                     }
01098                 }
01099             }
01100         }
01101     }
01102     bs_write_ue(b, sps->log2_max_frame_num_minus4);
01103     bs_write_ue(b, sps->pic_order_cnt_type);
01104     if( sps->pic_order_cnt_type == 0 )
01105     {
01106         bs_write_ue(b, sps->log2_max_pic_order_cnt_lsb_minus4);
01107     }
01108     else if( sps->pic_order_cnt_type == 1 )
01109     {
01110         bs_write_u1(b, sps->delta_pic_order_always_zero_flag);
01111         bs_write_se(b, sps->offset_for_non_ref_pic);
01112         bs_write_se(b, sps->offset_for_top_to_bottom_field);
01113         bs_write_ue(b, sps->num_ref_frames_in_pic_order_cnt_cycle);
01114         for( i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++ )
01115         {
01116             bs_write_se(b, sps->offset_for_ref_frame[ i ]);
01117         }
01118     }
01119     bs_write_ue(b, sps->num_ref_frames);
01120     bs_write_u1(b, sps->gaps_in_frame_num_value_allowed_flag);
01121     bs_write_ue(b, sps->pic_width_in_mbs_minus1);
01122     bs_write_ue(b, sps->pic_height_in_map_units_minus1);
01123     bs_write_u1(b, sps->frame_mbs_only_flag);
01124     if( !sps->frame_mbs_only_flag )
01125     {
01126         bs_write_u1(b, sps->mb_adaptive_frame_field_flag);
01127     }
01128     bs_write_u1(b, sps->direct_8x8_inference_flag);
01129     bs_write_u1(b, sps->frame_cropping_flag);
01130     if( sps->frame_cropping_flag )
01131     {
01132         bs_write_ue(b, sps->frame_crop_left_offset);
01133         bs_write_ue(b, sps->frame_crop_right_offset);
01134         bs_write_ue(b, sps->frame_crop_top_offset);
01135         bs_write_ue(b, sps->frame_crop_bottom_offset);
01136     }
01137     bs_write_u1(b, sps->vui_parameters_present_flag);
01138     if( sps->vui_parameters_present_flag )
01139     {
01140         write_vui_parameters(h, b);
01141     }
01142     write_rbsp_trailing_bits(h, b);
01143 }
01144 
01145 //7.3.2.1.1 Scaling list syntax
01146 void write_scaling_list(bs_t* b, int* scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag )
01147 {
01148     int j;
01149 
01150     int lastScale = 8;
01151     int nextScale = 8;
01152     for( j = 0; j < sizeOfScalingList; j++ )
01153     {
01154         int delta_scale;
01155         if( nextScale != 0 )
01156         {
01157             // FIXME
01158             /*
01159             nextScale = ( lastScale + delta_scale + 256 ) % 256;
01160             useDefaultScalingMatrixFlag = ( j == 0 && nextScale == 0 );
01161             */
01162             bs_write_se(b, delta_scale);
01163         }
01164         scalingList[ j ] = ( nextScale == 0 ) ? lastScale : nextScale;
01165         lastScale = scalingList[ j ];
01166     }
01167 }
01168 
01169 //Appendix E.1.1 VUI parameters syntax
01170 void
01171 write_vui_parameters(h264_stream_t* h, bs_t* b)
01172 {
01173     sps_t* sps = h->sps;
01174 
01175     bs_write_u1(b, sps->vui.aspect_ratio_info_present_flag);
01176     if( sps->vui.aspect_ratio_info_present_flag )
01177     {
01178         bs_write_u8(b, sps->vui.aspect_ratio_idc);
01179         if( sps->vui.aspect_ratio_idc == SAR_Extended )
01180         {
01181             bs_write_u(b,16, sps->vui.sar_width);
01182             bs_write_u(b,16, sps->vui.sar_height);
01183         }
01184     }
01185     bs_write_u1(b, sps->vui.overscan_info_present_flag);
01186     if( sps->vui.overscan_info_present_flag )
01187     {
01188         bs_write_u1(b, sps->vui.overscan_appropriate_flag);
01189     }
01190     bs_write_u1(b, sps->vui.video_signal_type_present_flag);
01191     if( sps->vui.video_signal_type_present_flag )
01192     {
01193         bs_write_u(b,3, sps->vui.video_format);
01194         bs_write_u1(b, sps->vui.video_full_range_flag);
01195         bs_write_u1(b, sps->vui.colour_description_present_flag);
01196         if( sps->vui.colour_description_present_flag )
01197         {
01198             bs_write_u8(b, sps->vui.colour_primaries);
01199             bs_write_u8(b, sps->vui.transfer_characteristics);
01200             bs_write_u8(b, sps->vui.matrix_coefficients);
01201         }
01202     }
01203     bs_write_u1(b, sps->vui.chroma_loc_info_present_flag);
01204     if( sps->vui.chroma_loc_info_present_flag )
01205     {
01206         bs_write_ue(b, sps->vui.chroma_sample_loc_type_top_field);
01207         bs_write_ue(b, sps->vui.chroma_sample_loc_type_bottom_field);
01208     }
01209     bs_write_u1(b, sps->vui.timing_info_present_flag);
01210     if( sps->vui.timing_info_present_flag )
01211     {
01212         bs_write_u(b,32, sps->vui.num_units_in_tick);
01213         bs_write_u(b,32, sps->vui.time_scale);
01214         bs_write_u1(b, sps->vui.fixed_frame_rate_flag);
01215     }
01216     bs_write_u1(b, sps->vui.nal_hrd_parameters_present_flag);
01217     if( sps->vui.nal_hrd_parameters_present_flag )
01218     {
01219         write_hrd_parameters(h, b);
01220     }
01221     bs_write_u1(b, sps->vui.vcl_hrd_parameters_present_flag);
01222     if( sps->vui.vcl_hrd_parameters_present_flag )
01223     {
01224         write_hrd_parameters(h, b);
01225     }
01226     if( sps->vui.nal_hrd_parameters_present_flag || sps->vui.vcl_hrd_parameters_present_flag )
01227     {
01228         bs_write_u1(b, sps->vui.low_delay_hrd_flag);
01229     }
01230     bs_write_u1(b, sps->vui.pic_struct_present_flag);
01231     bs_write_u1(b, sps->vui.bitstream_restriction_flag);
01232     if( sps->vui.bitstream_restriction_flag )
01233     {
01234         bs_write_u1(b, sps->vui.motion_vectors_over_pic_boundaries_flag);
01235         bs_write_ue(b, sps->vui.max_bytes_per_pic_denom);
01236         bs_write_ue(b, sps->vui.max_bits_per_mb_denom);
01237         bs_write_ue(b, sps->vui.log2_max_mv_length_horizontal);
01238         bs_write_ue(b, sps->vui.log2_max_mv_length_vertical);
01239         bs_write_ue(b, sps->vui.num_reorder_frames);
01240         bs_write_ue(b, sps->vui.max_dec_frame_buffering);
01241     }
01242 }
01243 
01244 //Appendix E.1.2 HRD parameters syntax
01245 void
01246 write_hrd_parameters(h264_stream_t* h, bs_t* b)
01247 {
01248     sps_t* sps = h->sps;
01249     int SchedSelIdx;
01250 
01251     bs_write_ue(b, sps->hrd.cpb_cnt_minus1);
01252     bs_write_u(b,4, sps->hrd.bit_rate_scale);
01253     bs_write_u(b,4, sps->hrd.cpb_size_scale);
01254     for( SchedSelIdx = 0; SchedSelIdx <= sps->hrd.cpb_cnt_minus1; SchedSelIdx++ )
01255     {
01256         bs_write_ue(b, sps->hrd.bit_rate_value_minus1[ SchedSelIdx ]);
01257         bs_write_ue(b, sps->hrd.cpb_size_value_minus1[ SchedSelIdx ]);
01258         bs_write_u1(b, sps->hrd.cbr_flag[ SchedSelIdx ]);
01259     }
01260     bs_write_u(b,5, sps->hrd.initial_cpb_removal_delay_length_minus1);
01261     bs_write_u(b,5, sps->hrd.cpb_removal_delay_length_minus1);
01262     bs_write_u(b,5, sps->hrd.dpb_output_delay_length_minus1);
01263     bs_write_u(b,5, sps->hrd.time_offset_length);
01264 }
01265 
01266 /*
01267 UNIMPLEMENTED
01268 //7.3.2.1.2 Sequence parameter set extension RBSP syntax
01269 int write_seq_parameter_set_extension_rbsp(bs_t* b, sps_ext_t* sps_ext) {
01270     bs_write_ue(b, seq_parameter_set_id);
01271     bs_write_ue(b, aux_format_idc);
01272     if( aux_format_idc != 0 ) {
01273         bs_write_ue(b, bit_depth_aux_minus8);
01274         bs_write_u1(b, alpha_incr_flag);
01275         bs_write_u(v, alpha_opaque_value);
01276         bs_write_u(v, alpha_transparent_value);
01277     }
01278     bs_write_u1(b, additional_extension_flag);
01279     write_rbsp_trailing_bits();
01280 }
01281 */
01282 
01283 //7.3.2.2 Picture parameter set RBSP syntax
01284 void write_pic_parameter_set_rbsp(h264_stream_t* h, bs_t* b) {
01285     pps_t* pps = h->pps;
01286 
01287     int i;
01288     int i_group;
01289 
01290     bs_write_ue(b, pps->pic_parameter_set_id);
01291     bs_write_ue(b, pps->seq_parameter_set_id);
01292     bs_write_u1(b, pps->entropy_coding_mode_flag);
01293     bs_write_u1(b, pps->pic_order_present_flag);
01294     bs_write_ue(b, pps->num_slice_groups_minus1);
01295     if( pps->num_slice_groups_minus1 > 0 )
01296     {
01297         bs_write_ue(b, pps->slice_group_map_type);
01298         if( pps->slice_group_map_type == 0 )
01299         {
01300             for( i_group = 0; i_group <= pps->num_slice_groups_minus1; i_group++ )
01301             {
01302                 bs_write_ue(b, pps->run_length_minus1[ i_group ]);
01303             }
01304         }
01305         else if( pps->slice_group_map_type == 2 )
01306         {
01307             for( i_group = 0; i_group < pps->num_slice_groups_minus1; i_group++ )
01308             {
01309                 bs_write_ue(b, pps->top_left[ i_group ]);
01310                 bs_write_ue(b, pps->bottom_right[ i_group ]);
01311             }
01312         }
01313         else if( pps->slice_group_map_type == 3 ||
01314                  pps->slice_group_map_type == 4 ||
01315                  pps->slice_group_map_type == 5 )
01316         {
01317             bs_write_u1(b, pps->slice_group_change_direction_flag);
01318             bs_write_ue(b, pps->slice_group_change_rate_minus1);
01319         }
01320         else if( pps->slice_group_map_type == 6 )
01321         {
01322             bs_write_ue(b, pps->pic_size_in_map_units_minus1);
01323             for( i = 0; i <= pps->pic_size_in_map_units_minus1; i++ )
01324             {
01325                 bs_write_u(b, ceil( log2( pps->num_slice_groups_minus1 + 1 ) ), pps->slice_group_id[ i ] ); // was u(v)
01326             }
01327         }
01328     }
01329     bs_write_ue(b, pps->num_ref_idx_l0_active_minus1);
01330     bs_write_ue(b, pps->num_ref_idx_l1_active_minus1);
01331     bs_write_u1(b, pps->weighted_pred_flag);
01332     bs_write_u(b,2, pps->weighted_bipred_idc);
01333     bs_write_se(b, pps->pic_init_qp_minus26);
01334     bs_write_se(b, pps->pic_init_qs_minus26);
01335     bs_write_se(b, pps->chroma_qp_index_offset);
01336     bs_write_u1(b, pps->deblocking_filter_control_present_flag);
01337     bs_write_u1(b, pps->constrained_intra_pred_flag);
01338     bs_write_u1(b, pps->redundant_pic_cnt_present_flag);
01339     if( more_rbsp_data(h, b) )
01340     {
01341         bs_write_u1(b, pps->transform_8x8_mode_flag);
01342         bs_write_u1(b, pps->pic_scaling_matrix_present_flag);
01343         if( pps->pic_scaling_matrix_present_flag )
01344         {
01345             for( i = 0; i < 6 + 2* pps->transform_8x8_mode_flag; i++ )
01346             {
01347                 bs_write_u1(b, pps->pic_scaling_list_present_flag[ i ]);
01348                 if( pps->pic_scaling_list_present_flag[ i ] )
01349                 {
01350                     if( i < 6 )
01351                     {
01352                         write_scaling_list( b, pps->ScalingList4x4[ i ], 16,
01353                                       pps->UseDefaultScalingMatrix4x4Flag[ i ] );
01354                     }
01355                     else
01356                     {
01357                         write_scaling_list( b, pps->ScalingList8x8[ i - 6 ], 64,
01358                                       pps->UseDefaultScalingMatrix8x8Flag[ i - 6 ] );
01359                     }
01360                 }
01361             }
01362         }
01363         bs_write_se(b, pps->second_chroma_qp_index_offset);
01364     }
01365     write_rbsp_trailing_bits(h, b);
01366 }
01367 
01368 /*
01369 // UNIMPLEMENTED
01370 //7.3.2.3 Supplemental enhancement information RBSP syntax
01371 write_sei_rbsp( ) {
01372     do {
01373         write_sei_message( );
01374     } while( more_rbsp_data( ) );
01375     write_rbsp_trailing_bits( );
01376 }
01377 
01378 //7.3.2.3.1 Supplemental enhancement information message syntax
01379 write_sei_message( ) {
01380     payloadType = 0;
01381     while( next_bits( 8 ) == 0xFF ) {
01382         bs_write_f(8, ff_byte); // equal to 0xFF
01383         payloadType += 255;
01384     }
01385     bs_write_u8(b, last_payload_type_byte);
01386     payloadType += last_payload_type_byte;
01387     payloadSize = 0;
01388     while( next_bits( 8 ) == 0xFF ) {
01389         bs_write_f(8, ff_byte); // equal to 0xFF
01390         payloadSize += 255;
01391     }
01392     bs_write_u8(b, last_payload_size_byte);
01393     payloadSize += last_payload_size_byte;
01394     sei_payload( payloadType, payloadSize );
01395 }
01396 */
01397 
01398 //7.3.2.4 Access unit delimiter RBSP syntax
01399 void write_access_unit_delimiter_rbsp(h264_stream_t* h, bs_t* b)
01400 {
01401     int primary_pic_type; //FIXME
01402     bs_write_u(b,3, primary_pic_type);
01403     write_rbsp_trailing_bits(h, b);
01404 }
01405 
01406 //7.3.2.5 End of sequence RBSP syntax
01407 void write_end_of_seq_rbsp(h264_stream_t* h, bs_t* b)
01408 {
01409 }
01410 
01411 //7.3.2.6 End of stream RBSP syntax
01412 void write_end_of_stream_rbsp(h264_stream_t* h, bs_t* b)
01413 {
01414 }
01415 
01416 //7.3.2.7 Filler data RBSP syntax
01417 void write_filler_data_rbsp(h264_stream_t* h, bs_t* b)
01418 {
01419     int ff_byte; //FIXME
01420     while( next_bits(b, 8) == 0xFF )
01421     {
01422         bs_write_f(b,8, ff_byte);  /* equal to 0xFF */ 
01423     }
01424     write_rbsp_trailing_bits(h, b);
01425 }
01426 
01427 //7.3.2.8 Slice layer without partitioning RBSP syntax
01428 void write_slice_layer_rbsp(h264_stream_t* h, bs_t* b)
01429 {
01430     write_slice_header(h, b);
01431     //slice_data( ); /* all categories of slice_data( ) syntax */
01432     write_rbsp_slice_trailing_bits(h, b);
01433 }
01434 
01435 /*
01436 // UNIMPLEMENTED
01437 //7.3.2.9.1 Slice data partition A RBSP syntax
01438 slice_data_partition_a_layer_rbsp( )
01439 {
01440     write_slice_header( );             // only category 2
01441     bs_write_ue(b, slice_id)
01442     write_slice_data( );               // only category 2
01443     write_rbsp_slice_trailing_bits( ); // only category 2
01444 }
01445 
01446 //7.3.2.9.2 Slice data partition B RBSP syntax
01447 slice_data_partition_b_layer_rbsp( )
01448 {
01449     bs_write_ue(b, slice_id);    // only category 3
01450     if( redundant_pic_cnt_present_flag )
01451         bs_write_ue(b, redundant_pic_cnt);
01452     write_slice_data( );               // only category 3
01453     write_rbsp_slice_trailing_bits( ); // only category 3
01454 }
01455 
01456 //7.3.2.9.3 Slice data partition C RBSP syntax
01457 slice_data_partition_c_layer_rbsp( )
01458 {
01459     bs_write_ue(b, slice_id);    // only category 4
01460     if( redundant_pic_cnt_present_flag )
01461         bs_write_ue(b, redundant_pic_cnt);
01462     write_slice_data( );               // only category 4
01463     rbsp_slice_trailing_bits( ); // only category 4
01464 }
01465 */
01466 
01467 //7.3.2.10 RBSP slice trailing bits syntax
01468 void write_rbsp_slice_trailing_bits(h264_stream_t* h, bs_t* b)
01469 {
01470     write_rbsp_trailing_bits(h, b);
01471     int cabac_zero_word;
01472     if( h->pps->entropy_coding_mode_flag )
01473     {
01474         while( more_rbsp_trailing_data(h, b) ) // FIXME when should we write cabac_zero_word ?
01475         {
01476             bs_write_f(b,16, cabac_zero_word); /* equal to 0x0000 */ 
01477         }
01478     }
01479 }
01480 
01481 //7.3.2.11 RBSP trailing bits syntax
01482 void write_rbsp_trailing_bits(h264_stream_t* h, bs_t* b)
01483 {
01484     int rbsp_stop_one_bit = 1;
01485     int rbsp_alignment_zero_bit = 0;
01486     if( !bs_byte_aligned(b) )
01487     {
01488         bs_write_f(b,1, rbsp_stop_one_bit); /* equal to 1 */
01489         while( !bs_byte_aligned(b) )
01490         {
01491             bs_write_f(b,1, rbsp_alignment_zero_bit); /* equal to 0 */ 
01492         }
01493     }
01494 }
01495 
01496 //7.3.3 Slice header syntax
01497 void write_slice_header(h264_stream_t* h, bs_t* b)
01498 {
01499     slice_header_t* sh = h->sh;
01500     sps_t* sps = h->sps;
01501     pps_t* pps = h->pps;
01502     nal_t* nal = h->nal;
01503 
01504     //DBG_START
01505     //h2->sh = (slice_header_t*)malloc(sizeof(slice_header_t));
01506 
01507     bs_write_ue(b, sh->first_mb_in_slice);
01508     bs_write_ue(b, sh->slice_type);
01509     bs_write_ue(b, sh->pic_parameter_set_id);
01510     bs_write_u(b, sps->log2_max_frame_num_minus4 + 4, sh->frame_num ); // was u(v)
01511     if( !sps->frame_mbs_only_flag )
01512     {
01513         bs_write_u1(b, sh->field_pic_flag);
01514         if( sh->field_pic_flag )
01515         {
01516             bs_write_u1(b, sh->bottom_field_flag);
01517         }
01518     }
01519     if( nal->nal_unit_type == 5 )
01520     {
01521         bs_write_ue(b, sh->idr_pic_id);
01522     }
01523     if( sps->pic_order_cnt_type == 0 )
01524     {
01525         bs_write_u(b, sps->log2_max_pic_order_cnt_lsb_minus4 + 4, sh->pic_order_cnt_lsb ); // was u(v)
01526         if( pps->pic_order_present_flag && !sh->field_pic_flag )
01527         {
01528             bs_write_se(b, sh->delta_pic_order_cnt_bottom);
01529         }
01530     }
01531     if( sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag )
01532     {
01533         bs_write_se(b, sh->delta_pic_order_cnt[ 0 ]);
01534         if( pps->pic_order_present_flag && !sh->field_pic_flag )
01535         {
01536             bs_write_se(b, sh->delta_pic_order_cnt[ 1 ]);
01537         }
01538     }
01539     if( pps->redundant_pic_cnt_present_flag )
01540     {
01541         bs_write_ue(b, sh->redundant_pic_cnt);
01542     }
01543     if( sh->slice_type == SH_SLICE_TYPE_B )
01544     {
01545         bs_write_u1(b, sh->direct_spatial_mv_pred_flag);
01546     }
01547     if( sh->slice_type == SH_SLICE_TYPE_P || sh->slice_type == SH_SLICE_TYPE_SP || sh->slice_type == SH_SLICE_TYPE_B )
01548     {
01549         bs_write_u1(b, sh->num_ref_idx_active_override_flag);
01550         if( sh->num_ref_idx_active_override_flag )
01551         {
01552             bs_write_ue(b, sh->num_ref_idx_l0_active_minus1); // FIXME does this modify the pps?
01553             if( sh->slice_type == SH_SLICE_TYPE_B )
01554             {
01555                 bs_write_ue(b, sh->num_ref_idx_l1_active_minus1);
01556             }
01557         }
01558     }
01559     write_ref_pic_list_reordering(h, b);
01560     if( ( pps->weighted_pred_flag && ( sh->slice_type == SH_SLICE_TYPE_P || sh->slice_type == SH_SLICE_TYPE_SP ) ) ||
01561         ( pps->weighted_bipred_idc == 1 && sh->slice_type == SH_SLICE_TYPE_B ) )
01562     {
01563         write_pred_weight_table(h, b);
01564     }
01565     if( nal->nal_ref_idc != 0 )
01566     {
01567         write_dec_ref_pic_marking(h, b);
01568     }
01569     if( pps->entropy_coding_mode_flag && sh->slice_type != SH_SLICE_TYPE_I && sh->slice_type != SH_SLICE_TYPE_SI )
01570     {
01571         bs_write_ue(b, sh->cabac_init_idc);
01572     }
01573     bs_write_se(b, sh->slice_qp_delta);
01574     if( sh->slice_type == SH_SLICE_TYPE_SP || sh->slice_type == SH_SLICE_TYPE_SI )
01575     {
01576         if( sh->slice_type == SH_SLICE_TYPE_SP )
01577         {
01578             bs_write_u1(b, sh->sp_for_switch_flag);
01579         }
01580         bs_write_se(b, sh->slice_qs_delta);
01581     }
01582     if( pps->deblocking_filter_control_present_flag )
01583     {
01584         bs_write_ue(b, sh->disable_deblocking_filter_idc);
01585         if( sh->disable_deblocking_filter_idc != 1 )
01586         {
01587             bs_write_se(b, sh->slice_alpha_c0_offset_div2);
01588             bs_write_se(b, sh->slice_beta_offset_div2);
01589         }
01590     }
01591     if( pps->num_slice_groups_minus1 > 0 &&
01592         pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5)
01593     {
01594         bs_write_u(b, ceil( log2( pps->pic_size_in_map_units_minus1 +  
01595                                   pps->slice_group_change_rate_minus1 + 1 ) ),
01596                    sh->slice_group_change_cycle ); // was u(v) // FIXME add 2?
01597     }
01598 
01599     //free(h2->sh);
01600     //DBG_END
01601 }
01602 
01603 //7.3.3.1 Reference picture list reordering syntax
01604 void write_ref_pic_list_reordering(h264_stream_t* h, bs_t* b)
01605 {
01606     slice_header_t* sh = h->sh;
01607 
01608     if( sh->slice_type != SH_SLICE_TYPE_I && sh->slice_type != SH_SLICE_TYPE_SI )
01609     {
01610         bs_write_u1(b, sh->rplr.ref_pic_list_reordering_flag_l0);
01611         if( sh->rplr.ref_pic_list_reordering_flag_l0 )
01612         {
01613             do
01614             {
01615                 bs_write_ue(b, sh->rplr.reordering_of_pic_nums_idc);
01616                 if( sh->rplr.reordering_of_pic_nums_idc == 0 ||
01617                     sh->rplr.reordering_of_pic_nums_idc == 1 )
01618                     bs_write_ue(b, sh->rplr.abs_diff_pic_num_minus1);
01619                 else if( sh->rplr.reordering_of_pic_nums_idc == 2 )
01620                     bs_write_ue(b, sh->rplr.long_term_pic_num);
01621             } while( sh->rplr.reordering_of_pic_nums_idc != 3 );
01622         }
01623     }
01624     if( sh->slice_type == SH_SLICE_TYPE_B )
01625     {
01626         bs_write_u1(b, sh->rplr.ref_pic_list_reordering_flag_l1);
01627         if( sh->rplr.ref_pic_list_reordering_flag_l1 )
01628         {
01629             do
01630             {
01631                 bs_write_ue(b, sh->rplr.reordering_of_pic_nums_idc);
01632                 if( sh->rplr.reordering_of_pic_nums_idc == 0 ||
01633                     sh->rplr.reordering_of_pic_nums_idc == 1 )
01634                 {
01635                     bs_write_ue(b, sh->rplr.abs_diff_pic_num_minus1);
01636                 }
01637                 else if( sh->rplr.reordering_of_pic_nums_idc == 2 )
01638                 {
01639                     bs_write_ue(b, sh->rplr.long_term_pic_num);
01640                 }
01641             } while( sh->rplr.reordering_of_pic_nums_idc != 3 );
01642         }
01643     }
01644 }
01645 
01646 //7.3.3.2 Prediction weight table syntax
01647 void write_pred_weight_table(h264_stream_t* h, bs_t* b)
01648 {
01649     slice_header_t* sh = h->sh;
01650     sps_t* sps = h->sps;
01651     pps_t* pps = h->pps;
01652 
01653     int i, j;
01654 
01655     bs_write_ue(b, sh->pwt.luma_log2_weight_denom);
01656     if( sps->chroma_format_idc != 0 )
01657     {
01658         bs_write_ue(b, sh->pwt.chroma_log2_weight_denom);
01659     }
01660     for( i = 0; i <= pps->num_ref_idx_l0_active_minus1; i++ )
01661     {
01662         bs_write_u1(b, sh->pwt.luma_weight_l0_flag);
01663         if( sh->pwt.luma_weight_l0_flag )
01664         {
01665             bs_write_se(b, sh->pwt.luma_weight_l0[ i ]);
01666             bs_write_se(b, sh->pwt.luma_offset_l0[ i ]);
01667         }
01668         if ( sps->chroma_format_idc != 0 )
01669         {
01670             bs_write_u1(b, sh->pwt.chroma_weight_l0_flag);
01671             if( sh->pwt.chroma_weight_l0_flag )
01672             {
01673                 for( j =0; j < 2; j++ )
01674                 {
01675                     bs_write_se(b, sh->pwt.chroma_weight_l0[ i ][ j ]);
01676                     bs_write_se(b, sh->pwt.chroma_offset_l0[ i ][ j ]);
01677                 }
01678             }
01679         }
01680     }
01681     if( sh->slice_type == SH_SLICE_TYPE_B )
01682     {
01683         for( i = 0; i <= pps->num_ref_idx_l1_active_minus1; i++ )
01684         {
01685             bs_write_u1(b, sh->pwt.luma_weight_l1_flag);
01686             if( sh->pwt.luma_weight_l1_flag )
01687             {
01688                 bs_write_se(b, sh->pwt.luma_weight_l1[ i ]);
01689                 bs_write_se(b, sh->pwt.luma_offset_l1[ i ]);
01690             }
01691             if( sps->chroma_format_idc != 0 )
01692             {
01693                 bs_write_u1(b, sh->pwt.chroma_weight_l1_flag);
01694                 if( sh->pwt.chroma_weight_l1_flag )
01695                 {
01696                     for( j = 0; j < 2; j++ )
01697                     {
01698                         bs_write_se(b, sh->pwt.chroma_weight_l1[ i ][ j ]);
01699                         bs_write_se(b, sh->pwt.chroma_offset_l1[ i ][ j ]);
01700                     }
01701                 }
01702             }
01703         }
01704     }
01705 }
01706 
01707 //7.3.3.3 Decoded reference picture marking syntax
01708 void write_dec_ref_pic_marking(h264_stream_t* h, bs_t* b)
01709 {
01710     slice_header_t* sh = h->sh;
01711 
01712     if( h->nal->nal_unit_type == 5 )
01713     {
01714         bs_write_u1(b, sh->drpm.no_output_of_prior_pics_flag);
01715         bs_write_u1(b, sh->drpm.long_term_reference_flag);
01716     }
01717     else
01718     {
01719         bs_write_u1(b, sh->drpm.adaptive_ref_pic_marking_mode_flag);
01720         if( sh->drpm.adaptive_ref_pic_marking_mode_flag )
01721         {
01722             do
01723             {
01724                 bs_write_ue(b, sh->drpm.memory_management_control_operation);
01725                 if( sh->drpm.memory_management_control_operation == 1 ||
01726                     sh->drpm.memory_management_control_operation == 3 )
01727                 {
01728                     bs_write_ue(b, sh->drpm.difference_of_pic_nums_minus1);
01729                 }
01730                 if(sh->drpm.memory_management_control_operation == 2 )
01731                 {
01732                     bs_write_ue(b, sh->drpm.long_term_pic_num);
01733                 }
01734                 if( sh->drpm.memory_management_control_operation == 3 ||
01735                     sh->drpm.memory_management_control_operation == 6 )
01736                 {
01737                     bs_write_ue(b, sh->drpm.long_term_frame_idx);
01738                 }
01739                 if( sh->drpm.memory_management_control_operation == 4 )
01740                 {
01741                     bs_write_ue(b, sh->drpm.max_long_term_frame_idx_plus1);
01742                 }
01743             } while( sh->drpm.memory_management_control_operation != 0 );
01744         }
01745     }
01746 }
01747 
01748 /*
01749 // UNIMPLEMENTED
01750 //7.3.4 Slice data syntax
01751 slice_data( )
01752 {
01753     if( pps->entropy_coding_mode_flag )
01754         while( !byte_aligned( ) )
01755             bs_write_f(1, cabac_alignment_one_bit);
01756     CurrMbAddr = first_mb_in_slice * ( 1 + MbaffFrameFlag );
01757     moreDataFlag = 1;
01758     prevMbSkipped = 0;
01759     do {
01760         if( sh->slice_type != SH_SLICE_TYPE_I && ssh->lice_type != SH_SLICE_TYPE_SI )
01761             if( !pps->entropy_coding_mode_flag ) {
01762                 bs_write_ue(b, mb_skip_run);
01763                 prevMbSkipped = ( mb_skip_run > 0 );
01764                 for( i=0; i<mb_skip_run; i++ )
01765                     CurrMbAddr = NextMbAddress( CurrMbAddr );
01766                 moreDataFlag = more_rbsp_data( );
01767             } else {
01768                 bs_write_ae(v, mb_skip_flag);
01769                 moreDataFlag = !mb_skip_flag;
01770             }
01771         if( moreDataFlag ) {
01772             if( MbaffFrameFlag && ( CurrMbAddr % 2 == 0 ||
01773                                     ( CurrMbAddr % 2 == 1 && prevMbSkipped ) ) )
01774                 bs_write_u1(b) | bs_write_ae(v, mb_field_decoding_flag);
01775             macroblock_layer( );
01776         }
01777         if( !pps->entropy_coding_mode_flag )
01778             moreDataFlag = more_rbsp_data( );
01779         else {
01780             if( sh->slice_type != SH_SLICE_TYPE_I && sh->slice_type != SH_SLICE_TYPE_SI )
01781                 prevMbSkipped = mb_skip_flag;
01782             if( MbaffFrameFlag && CurrMbAddr % 2 == 0 )
01783                 moreDataFlag = 1;
01784             else {
01785                 bs_write_ae(v, end_of_slice_flag);
01786                 moreDataFlag = !end_of_slice_flag;
01787             }
01788         }
01789         CurrMbAddr = NextMbAddress( CurrMbAddr );
01790     } while( moreDataFlag );
01791 }
01792 */
01793 
01794 
01795 
01796 
01797 /************** debug **************/
01798 
01799 void debug_sps(sps_t* sps)
01800 {
01801     printf("======= SPS =======\n");
01802     printf(" profile_idc : %02x \n", sps->profile_idc );
01803     printf(" constraint_set0_flag : %02x \n", sps->constraint_set0_flag );
01804     printf(" constraint_set1_flag : %02x \n", sps->constraint_set1_flag );
01805     printf(" constraint_set2_flag : %02x \n", sps->constraint_set2_flag );
01806     printf(" constraint_set3_flag : %02x \n", sps->constraint_set3_flag );
01807     printf(" reserved_zero_4bits : %02x \n", sps->reserved_zero_4bits );
01808     printf(" level_idc : %02x \n", sps->level_idc );
01809     printf(" seq_parameter_set_id : %02x \n", sps->seq_parameter_set_id );
01810     printf(" chroma_format_idc : %02x \n", sps->chroma_format_idc );
01811     printf(" residual_colour_transform_flag : %02x \n", sps->residual_colour_transform_flag );
01812     printf(" bit_depth_luma_minus8 : %02x \n", sps->bit_depth_luma_minus8 );
01813     printf(" bit_depth_chroma_minus8 : %02x \n", sps->bit_depth_chroma_minus8 );
01814     printf(" qpprime_y_zero_transform_bypass_flag : %02x \n", sps->qpprime_y_zero_transform_bypass_flag );
01815     printf(" seq_scaling_matrix_present_flag : %02x \n", sps->seq_scaling_matrix_present_flag );
01816     //  int seq_scaling_list_present_flag[8];
01817     //  void* ScalingList4x4[6];
01818     //  int UseDefaultScalingMatrix4x4Flag[6];
01819     //  void* ScalingList8x8[2];
01820     //  int UseDefaultScalingMatrix8x8Flag[2];
01821     printf(" log2_max_frame_num_minus4 : %02x \n", sps->log2_max_frame_num_minus4 );
01822     printf(" pic_order_cnt_type : %02x \n", sps->pic_order_cnt_type );
01823       printf("   log2_max_pic_order_cnt_lsb_minus4 : %02x \n", sps->log2_max_pic_order_cnt_lsb_minus4 );
01824       printf("   delta_pic_order_always_zero_flag : %02x \n", sps->delta_pic_order_always_zero_flag );
01825       printf("   offset_for_non_ref_pic : %02x \n", sps->offset_for_non_ref_pic );
01826       printf("   offset_for_top_to_bottom_field : %02x \n", sps->offset_for_top_to_bottom_field );
01827       printf("   num_ref_frames_in_pic_order_cnt_cycle : %02x \n", sps->num_ref_frames_in_pic_order_cnt_cycle );
01828     //  int offset_for_ref_frame[256];
01829     printf(" num_ref_frames : %02x \n", sps->num_ref_frames );
01830     printf(" gaps_in_frame_num_value_allowed_flag : %02x \n", sps->gaps_in_frame_num_value_allowed_flag );
01831     printf(" pic_width_in_mbs_minus1 : %02x \n", sps->pic_width_in_mbs_minus1 );
01832     printf(" pic_height_in_map_units_minus1 : %02x \n", sps->pic_height_in_map_units_minus1 );
01833     printf(" frame_mbs_only_flag : %02x \n", sps->frame_mbs_only_flag );
01834     printf(" mb_adaptive_frame_field_flag : %02x \n", sps->mb_adaptive_frame_field_flag );
01835     printf(" direct_8x8_inference_flag : %02x \n", sps->direct_8x8_inference_flag );
01836     printf(" frame_cropping_flag : %02x \n", sps->frame_cropping_flag );
01837       printf("   frame_crop_left_offset : %02x \n", sps->frame_crop_left_offset );
01838       printf("   frame_crop_right_offset : %02x \n", sps->frame_crop_right_offset );
01839       printf("   frame_crop_top_offset : %02x \n", sps->frame_crop_top_offset );
01840       printf("   frame_crop_bottom_offset : %02x \n", sps->frame_crop_bottom_offset );
01841     printf(" vui_parameters_present_flag : %02x \n", sps->vui_parameters_present_flag );
01842 
01843     printf("=== VUI ===\n");
01844     printf(" aspect_ratio_info_present_flag : %02x \n", sps->vui.aspect_ratio_info_present_flag );
01845       printf("   aspect_ratio_idc : %02x \n", sps->vui.aspect_ratio_idc );
01846         printf("     sar_width : %02x \n", sps->vui.sar_width );
01847         printf("     sar_height : %02x \n", sps->vui.sar_height );
01848     printf(" overscan_info_present_flag : %02x \n", sps->vui.overscan_info_present_flag );
01849       printf("   overscan_appropriate_flag : %02x \n", sps->vui.overscan_appropriate_flag );
01850     printf(" video_signal_type_present_flag : %02x \n", sps->vui.video_signal_type_present_flag );
01851       printf("   video_format : %02x \n", sps->vui.video_format );
01852       printf("   video_full_range_flag : %02x \n", sps->vui.video_full_range_flag );
01853       printf("   colour_description_present_flag : %02x \n", sps->vui.colour_description_present_flag );
01854         printf("     colour_primaries : %02x \n", sps->vui.colour_primaries );
01855         printf("   transfer_characteristics : %02x \n", sps->vui.transfer_characteristics );
01856         printf("   matrix_coefficients : %02x \n", sps->vui.matrix_coefficients );
01857     printf(" chroma_loc_info_present_flag : %02x \n", sps->vui.chroma_loc_info_present_flag );
01858       printf("   chroma_sample_loc_type_top_field : %02x \n", sps->vui.chroma_sample_loc_type_top_field );
01859       printf("   chroma_sample_loc_type_bottom_field : %02x \n", sps->vui.chroma_sample_loc_type_bottom_field );
01860     printf(" timing_info_present_flag : %02x \n", sps->vui.timing_info_present_flag );
01861       printf("   num_units_in_tick : %02x \n", sps->vui.num_units_in_tick );
01862       printf("   time_scale : %02x \n", sps->vui.time_scale );
01863       printf("   fixed_frame_rate_flag : %02x \n", sps->vui.fixed_frame_rate_flag );
01864     printf(" nal_hrd_parameters_present_flag : %02x \n", sps->vui.nal_hrd_parameters_present_flag );
01865     printf(" vcl_hrd_parameters_present_flag : %02x \n", sps->vui.vcl_hrd_parameters_present_flag );
01866       printf("   low_delay_hrd_flag : %02x \n", sps->vui.low_delay_hrd_flag );
01867     printf(" pic_struct_present_flag : %02x \n", sps->vui.pic_struct_present_flag );
01868     printf(" bitstream_restriction_flag : %02x \n", sps->vui.bitstream_restriction_flag );
01869       printf("   motion_vectors_over_pic_boundaries_flag : %02x \n", sps->vui.motion_vectors_over_pic_boundaries_flag );
01870       printf("   max_bytes_per_pic_denom : %02x \n", sps->vui.max_bytes_per_pic_denom );
01871       printf("   max_bits_per_mb_denom : %02x \n", sps->vui.max_bits_per_mb_denom );
01872       printf("   log2_max_mv_length_horizontal : %02x \n", sps->vui.log2_max_mv_length_horizontal );
01873       printf("   log2_max_mv_length_vertical : %02x \n", sps->vui.log2_max_mv_length_vertical );
01874       printf("   num_reorder_frames : %02x \n", sps->vui.num_reorder_frames );
01875       printf("   max_dec_frame_buffering : %02x \n", sps->vui.max_dec_frame_buffering );
01876 
01877     printf("=== HRD ===\n");
01878     printf(" cpb_cnt_minus1 : %02x \n", sps->hrd.cpb_cnt_minus1 );
01879     printf(" bit_rate_scale : %02x \n", sps->hrd.bit_rate_scale );
01880     printf(" cpb_size_scale : %02x \n", sps->hrd.cpb_size_scale );
01881     //  printf("bit_rate_value_minus1[32] : %02x \n", sps->hrd.bit_rate_value_minus1[32] ); // up to cpb_cnt_minus1, which is <= 31
01882     //  printf("cpb_size_value_minus1[32] : %02x \n", sps->hrd.cpb_size_value_minus1[32] );
01883     //  printf("cbr_flag[32] : %02x \n", sps->hrd.cbr_flag[32] );
01884     printf(" initial_cpb_removal_delay_length_minus1 : %02x \n", sps->hrd.initial_cpb_removal_delay_length_minus1 );
01885     printf(" cpb_removal_delay_length_minus1 : %02x \n", sps->hrd.cpb_removal_delay_length_minus1 );
01886     printf(" dpb_output_delay_length_minus1 : %02x \n", sps->hrd.dpb_output_delay_length_minus1 );
01887     printf(" time_offset_length : %02x \n", sps->hrd.time_offset_length );
01888 }
01889 
01890 
01891 void debug_pps(pps_t* pps)
01892 {
01893     printf("======= PPS =======\n");
01894     printf(" pic_parameter_set_id : %02x \n", pps->pic_parameter_set_id );
01895     printf(" seq_parameter_set_id : %02x \n", pps->seq_parameter_set_id );
01896     printf(" entropy_coding_mode_flag : %02x \n", pps->entropy_coding_mode_flag );
01897     printf(" pic_order_present_flag : %02x \n", pps->pic_order_present_flag );
01898     printf(" num_slice_groups_minus1 : %02x \n", pps->num_slice_groups_minus1 );
01899     printf(" slice_group_map_type : %02x \n", pps->slice_group_map_type );
01900     //  int run_length_minus1[8]; // up to num_slice_groups_minus1, which is <= 7 in Baseline and Extended, 0 otheriwse
01901     //  int top_left[8];
01902     //  int bottom_right[8];
01903     //  int slice_group_change_direction_flag;
01904     //  int slice_group_change_rate_minus1;
01905     //  int pic_size_in_map_units_minus1;
01906     //  int slice_group_id[256]; // FIXME what size?
01907     printf(" num_ref_idx_l0_active_minus1 : %02x \n", pps->num_ref_idx_l0_active_minus1 );
01908     printf(" num_ref_idx_l1_active_minus1 : %02x \n", pps->num_ref_idx_l1_active_minus1 );
01909     printf(" weighted_pred_flag : %02x \n", pps->weighted_pred_flag );
01910     printf(" weighted_bipred_idc : %02x \n", pps->weighted_bipred_idc );
01911     printf(" pic_init_qp_minus26 : %02x \n", pps->pic_init_qp_minus26 );
01912     printf(" pic_init_qs_minus26 : %02x \n", pps->pic_init_qs_minus26 );
01913     printf(" chroma_qp_index_offset : %02x \n", pps->chroma_qp_index_offset );
01914     printf(" deblocking_filter_control_present_flag : %02x \n", pps->deblocking_filter_control_present_flag );
01915     printf(" constrained_intra_pred_flag : %02x \n", pps->constrained_intra_pred_flag );
01916     printf(" redundant_pic_cnt_present_flag : %02x \n", pps->redundant_pic_cnt_present_flag );
01917     printf(" transform_8x8_mode_flag : %02x \n", pps->transform_8x8_mode_flag );
01918     printf(" pic_scaling_matrix_present_flag : %02x \n", pps->pic_scaling_matrix_present_flag );
01919     //  int pic_scaling_list_present_flag[8];
01920     //  void* ScalingList4x4[6];
01921     //  int UseDefaultScalingMatrix4x4Flag[6];
01922     //  void* ScalingList8x8[2];
01923     //  int UseDefaultScalingMatrix8x8Flag[2];
01924     printf(" second_chroma_qp_index_offset : %02x \n", pps->second_chroma_qp_index_offset );
01925 }
01926 
01927 void debug_slice_header(slice_header_t* sh)
01928 {
01929     printf("======= Slice Header =======\n");
01930     printf(" first_mb_in_slice : %02x \n", sh->first_mb_in_slice );
01931     char* slice_type_name;
01932     if (sh->slice_type == SH_SLICE_TYPE_I) { slice_type_name = "I Frame"; }
01933     else if (sh->slice_type == SH_SLICE_TYPE_P) { slice_type_name = "P Frame"; }
01934     else if (sh->slice_type == SH_SLICE_TYPE_B) { slice_type_name = "B Frame"; }
01935     else { slice_type_name = "?"; }
01936     printf(" slice_type : %02x ( %s ) \n", sh->slice_type, slice_type_name );
01937     printf(" pic_parameter_set_id : %02x \n", sh->pic_parameter_set_id );
01938     printf(" frame_num : %02x \n", sh->frame_num );
01939     printf(" field_pic_flag : %02x \n", sh->field_pic_flag );
01940       printf(" bottom_field_flag : %02x \n", sh->bottom_field_flag );
01941     printf(" idr_pic_id : %02x \n", sh->idr_pic_id );
01942     printf(" pic_order_cnt_lsb : %02x \n", sh->pic_order_cnt_lsb );
01943     printf(" delta_pic_order_cnt_bottom : %02x \n", sh->delta_pic_order_cnt_bottom );
01944     // int delta_pic_order_cnt[ 2 ];
01945     printf(" redundant_pic_cnt : %02x \n", sh->redundant_pic_cnt );
01946     printf(" direct_spatial_mv_pred_flag : %02x \n", sh->direct_spatial_mv_pred_flag );
01947     printf(" num_ref_idx_active_override_flag : %02x \n", sh->num_ref_idx_active_override_flag );
01948     printf(" num_ref_idx_l0_active_minus1 : %02x \n", sh->num_ref_idx_l0_active_minus1 );
01949     printf(" num_ref_idx_l1_active_minus1 : %02x \n", sh->num_ref_idx_l1_active_minus1 );
01950     printf(" cabac_init_idc : %02x \n", sh->cabac_init_idc );
01951     printf(" slice_qp_delta : %02x \n", sh->slice_qp_delta );
01952     printf(" sp_for_switch_flag : %02x \n", sh->sp_for_switch_flag );
01953     printf(" slice_qs_delta : %02x \n", sh->slice_qs_delta );
01954     printf(" disable_deblocking_filter_idc : %02x \n", sh->disable_deblocking_filter_idc );
01955     printf(" slice_alpha_c0_offset_div2 : %02x \n", sh->slice_alpha_c0_offset_div2 );
01956     printf(" slice_beta_offset_div2 : %02x \n", sh->slice_beta_offset_div2 );
01957     printf(" slice_group_change_cycle : %02x \n", sh->slice_group_change_cycle );
01958 
01959     printf("=== Prediction Weight Table ===\n");
01960         printf(" luma_log2_weight_denom : %02x \n", sh->pwt.luma_log2_weight_denom );
01961         printf(" chroma_log2_weight_denom : %02x \n", sh->pwt.chroma_log2_weight_denom );
01962         printf(" luma_weight_l0_flag : %02x \n", sh->pwt.luma_weight_l0_flag );
01963         // int luma_weight_l0[64];
01964         // int luma_offset_l0[64];
01965         printf(" chroma_weight_l0_flag : %02x \n", sh->pwt.chroma_weight_l0_flag );
01966         // int chroma_weight_l0[64][2];
01967         // int chroma_offset_l0[64][2];
01968         printf(" luma_weight_l1_flag : %02x \n", sh->pwt.luma_weight_l1_flag );
01969         // int luma_weight_l1[64];
01970         // int luma_offset_l1[64];
01971         printf(" chroma_weight_l1_flag : %02x \n", sh->pwt.chroma_weight_l1_flag );
01972         // int chroma_weight_l1[64][2];
01973         // int chroma_offset_l1[64][2];
01974 
01975     printf("=== Ref Pic List Reordering ===\n");
01976         printf(" ref_pic_list_reordering_flag_l0 : %02x \n", sh->rplr.ref_pic_list_reordering_flag_l0 );
01977         printf(" ref_pic_list_reordering_flag_l1 : %02x \n", sh->rplr.ref_pic_list_reordering_flag_l1 );
01978         // int reordering_of_pic_nums_idc;
01979         // int abs_diff_pic_num_minus1;
01980         // int long_term_pic_num;
01981 
01982     printf("=== Decoded Ref Pic Marking ===\n");
01983         printf(" no_output_of_prior_pics_flag : %02x \n", sh->drpm.no_output_of_prior_pics_flag );
01984         printf(" long_term_reference_flag : %02x \n", sh->drpm.long_term_reference_flag );
01985         printf(" adaptive_ref_pic_marking_mode_flag : %02x \n", sh->drpm.adaptive_ref_pic_marking_mode_flag );
01986         // int memory_management_control_operation;
01987         // int difference_of_pic_nums_minus1;
01988         // int long_term_pic_num;
01989         // int long_term_frame_idx;
01990         // int max_long_term_frame_idx_plus1;
01991 
01992 }
01993 
02000 void debug_nal(h264_stream_t* h, nal_t* nal)
02001 {
02002     printf("==================== NAL ====================\n");
02003     printf(" forbidden_zero_bit : %02x \n", nal->forbidden_zero_bit );
02004     printf(" nal_ref_idc : %02x \n", nal->nal_ref_idc );
02005     printf(" nal_unit_type : %02x \n", nal->nal_unit_type );
02006     //uint8_t* rbsp_buf;
02007     //int rbsp_size;
02008 
02009     if( nal->nal_unit_type == 0) { }                                 //  0    Unspecified
02010     else if( nal->nal_unit_type == 1) { debug_slice_header(h->sh); }       //  1    Coded slice of a non-IDR picture
02011     else if( nal->nal_unit_type == 5) { debug_slice_header(h->sh); }       //  5    Coded slice of an IDR picture
02012     else if( nal->nal_unit_type == 7) { debug_sps(h->sps); } //  7    Sequence parameter set
02013     else if( nal->nal_unit_type == 8) { debug_pps(h->pps); } //  8    Picture parameter set
02014 }
02015 
02016 void debug_bytes(uint8_t* buf, int len)
02017 {
02018     int i;
02019     for (i = 0; i < len; i++)
02020     {
02021         printf("%02X ", buf[i]);
02022         if ((i+1) % 16 == 0) { printf ("\n"); }
02023     }
02024     printf("\n");
02025 }
02026 
02027 void debug_bs(bs_t* b)
02028 {
02029     bs_t* b2 = (bs_t*)malloc(sizeof(bs_t));
02030     bs_init(b2, b->start, b->end - b->start);
02031 
02032     while (b2->p < b->p || 
02033            (b2->p == b->p && b2->bits_left > b->bits_left))
02034     {
02035         printf("%d", bs_read_u1(b2));
02036         if (b2->bits_left == 8) { printf(" "); }
02037     }
02038 
02039     free(b2);
02040 }

Generated on Sat Jul 22 22:05:00 2006 by  doxygen 1.4.4