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

h264_stream.h

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 
00024 #ifndef _H264_STREAM_H
00025 #define _H264_STREAM_H        1
00026 
00027 #include "bs.h"
00028 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif
00032 
00040 typedef struct
00041 {
00042         int profile_idc;
00043         int constraint_set0_flag;
00044         int constraint_set1_flag;
00045         int constraint_set2_flag;
00046         int constraint_set3_flag;
00047         int reserved_zero_4bits;
00048         int level_idc;
00049         int seq_parameter_set_id;
00050         int chroma_format_idc;
00051         int residual_colour_transform_flag;
00052         int bit_depth_luma_minus8;
00053         int bit_depth_chroma_minus8;
00054         int qpprime_y_zero_transform_bypass_flag;
00055         int seq_scaling_matrix_present_flag;
00056           int seq_scaling_list_present_flag[8];
00057           int* ScalingList4x4[6];
00058           int UseDefaultScalingMatrix4x4Flag[6];
00059           int* ScalingList8x8[2];
00060           int UseDefaultScalingMatrix8x8Flag[2];
00061         int log2_max_frame_num_minus4;
00062         int pic_order_cnt_type;
00063           int log2_max_pic_order_cnt_lsb_minus4;
00064           int delta_pic_order_always_zero_flag;
00065           int offset_for_non_ref_pic;
00066           int offset_for_top_to_bottom_field;
00067           int num_ref_frames_in_pic_order_cnt_cycle;
00068           int offset_for_ref_frame[256];
00069         int num_ref_frames;
00070         int gaps_in_frame_num_value_allowed_flag;
00071         int pic_width_in_mbs_minus1;
00072         int pic_height_in_map_units_minus1;
00073         int frame_mbs_only_flag;
00074         int mb_adaptive_frame_field_flag;
00075         int direct_8x8_inference_flag;
00076         int frame_cropping_flag;
00077           int frame_crop_left_offset;
00078           int frame_crop_right_offset;
00079           int frame_crop_top_offset;
00080           int frame_crop_bottom_offset;
00081         int vui_parameters_present_flag;
00082         
00083         struct
00084         {
00085                 int aspect_ratio_info_present_flag;
00086                   int aspect_ratio_idc;
00087                     int sar_width;
00088                     int sar_height;
00089                 int overscan_info_present_flag;
00090                   int overscan_appropriate_flag;
00091                 int video_signal_type_present_flag;
00092                   int video_format;
00093                   int video_full_range_flag;
00094                   int colour_description_present_flag;
00095                     int colour_primaries;
00096                         int transfer_characteristics;
00097                         int matrix_coefficients;
00098                 int chroma_loc_info_present_flag;
00099                   int chroma_sample_loc_type_top_field;
00100                   int chroma_sample_loc_type_bottom_field;
00101                 int timing_info_present_flag;
00102                   int num_units_in_tick;
00103                   int time_scale;
00104                   int fixed_frame_rate_flag;
00105                 int nal_hrd_parameters_present_flag;
00106                 int vcl_hrd_parameters_present_flag;
00107                   int low_delay_hrd_flag;
00108                 int pic_struct_present_flag;
00109                 int bitstream_restriction_flag;
00110                   int motion_vectors_over_pic_boundaries_flag;
00111                   int max_bytes_per_pic_denom;
00112                   int max_bits_per_mb_denom;
00113                   int log2_max_mv_length_horizontal;
00114                   int log2_max_mv_length_vertical;
00115                   int num_reorder_frames;
00116                   int max_dec_frame_buffering;
00117         } vui;
00118 
00119         struct
00120         {
00121                 int cpb_cnt_minus1;
00122                 int bit_rate_scale;
00123                 int cpb_size_scale;
00124                   int bit_rate_value_minus1[32]; // up to cpb_cnt_minus1, which is <= 31
00125                   int cpb_size_value_minus1[32];
00126                   int cbr_flag[32];
00127                 int initial_cpb_removal_delay_length_minus1;
00128                 int cpb_removal_delay_length_minus1;
00129                 int dpb_output_delay_length_minus1;
00130                 int time_offset_length;
00131         } hrd;
00132 
00133 } sps_t;
00134 
00135 
00143 typedef struct 
00144 {
00145         int pic_parameter_set_id;
00146         int seq_parameter_set_id;
00147         int entropy_coding_mode_flag;
00148         int pic_order_present_flag;
00149         int num_slice_groups_minus1;
00150         int slice_group_map_type;
00151           int run_length_minus1[8]; // up to num_slice_groups_minus1, which is <= 7 in Baseline and Extended, 0 otheriwse
00152           int top_left[8];
00153           int bottom_right[8];
00154           int slice_group_change_direction_flag;
00155           int slice_group_change_rate_minus1;
00156           int pic_size_in_map_units_minus1;
00157           int slice_group_id[256]; // FIXME what size?
00158         int num_ref_idx_l0_active_minus1;
00159         int num_ref_idx_l1_active_minus1;
00160         int weighted_pred_flag;
00161         int weighted_bipred_idc;
00162         int pic_init_qp_minus26;
00163         int pic_init_qs_minus26;
00164         int chroma_qp_index_offset;
00165         int deblocking_filter_control_present_flag;
00166         int constrained_intra_pred_flag;
00167         int redundant_pic_cnt_present_flag;
00168         int transform_8x8_mode_flag;
00169         int pic_scaling_matrix_present_flag;
00170           int pic_scaling_list_present_flag[8];
00171           int* ScalingList4x4[6];
00172           int UseDefaultScalingMatrix4x4Flag[6];
00173           int* ScalingList8x8[2];
00174           int UseDefaultScalingMatrix8x8Flag[2];
00175         int second_chroma_qp_index_offset;
00176 } pps_t;
00177 
00178 
00186 typedef struct
00187 {
00188         int first_mb_in_slice;
00189         int slice_type;
00190         int pic_parameter_set_id;
00191         int frame_num;
00192         int field_pic_flag;
00193           int bottom_field_flag;
00194         int idr_pic_id;
00195         int pic_order_cnt_lsb;
00196         int delta_pic_order_cnt_bottom;
00197         int delta_pic_order_cnt[ 2 ];
00198         int redundant_pic_cnt;
00199         int direct_spatial_mv_pred_flag;
00200         int num_ref_idx_active_override_flag;
00201         int num_ref_idx_l0_active_minus1;
00202         int num_ref_idx_l1_active_minus1;
00203         int cabac_init_idc;
00204         int slice_qp_delta;
00205         int sp_for_switch_flag;
00206         int slice_qs_delta;
00207         int disable_deblocking_filter_idc;
00208         int slice_alpha_c0_offset_div2;
00209         int slice_beta_offset_div2;
00210         int slice_group_change_cycle;
00211 
00212 
00213         struct
00214         {
00215                 int luma_log2_weight_denom;
00216                 int chroma_log2_weight_denom;
00217                 int luma_weight_l0_flag;
00218                 int luma_weight_l0[64];
00219                 int luma_offset_l0[64];
00220                 int chroma_weight_l0_flag;
00221                 int chroma_weight_l0[64][2];
00222                 int chroma_offset_l0[64][2];
00223                 int luma_weight_l1_flag;
00224                 int luma_weight_l1[64];
00225                 int luma_offset_l1[64];
00226                 int chroma_weight_l1_flag;
00227                 int chroma_weight_l1[64][2];
00228                 int chroma_offset_l1[64][2];
00229         } pwt; // predictive weight table
00230 
00231         struct // FIXME stack or array
00232         {
00233                 int ref_pic_list_reordering_flag_l0;
00234                 int ref_pic_list_reordering_flag_l1;
00235                 int reordering_of_pic_nums_idc;
00236                 int abs_diff_pic_num_minus1;
00237                 int long_term_pic_num;
00238         } rplr; // ref pic list reorder
00239 
00240         struct // FIXME stack or array
00241         {
00242                 int no_output_of_prior_pics_flag;
00243                 int long_term_reference_flag;
00244                 int adaptive_ref_pic_marking_mode_flag;
00245                 int memory_management_control_operation;
00246                 int difference_of_pic_nums_minus1;
00247                 int long_term_pic_num;
00248                 int long_term_frame_idx;
00249                 int max_long_term_frame_idx_plus1;
00250         } drpm; // decoded ref pic marking
00251 
00252 } slice_header_t;
00253 
00254 
00262 typedef struct
00263 {
00264         int forbidden_zero_bit;
00265         int nal_ref_idc;
00266         int nal_unit_type;
00267         //uint8_t* rbsp_buf;
00268         //int rbsp_size;
00269 } nal_t;
00270 
00271 
00279 typedef struct
00280 {
00281         nal_t* nal;
00282         sps_t* sps;
00283         pps_t* pps;
00284         slice_header_t* sh;
00285 } h264_stream_t;
00286 
00287 
00288 int find_nal_unit(uint8_t* buf, int size, int* nal_start, int* nal_end);
00289 
00290 int read_nal_unit(h264_stream_t* h, uint8_t* buf, int size);
00291 
00292 void read_pic_parameter_set_rbsp(h264_stream_t* h, bs_t* b);
00293 
00294 void read_access_unit_delimiter_rbsp(h264_stream_t* h, bs_t* b);
00295 void read_end_of_seq_rbsp(h264_stream_t* h, bs_t* b);
00296 void read_end_of_stream_rbsp(h264_stream_t* h, bs_t* b);
00297 void read_filler_data_rbsp(h264_stream_t* h, bs_t* b);
00298 void read_slice_layer_rbsp(h264_stream_t* h, bs_t* b);
00299 void read_rbsp_slice_trailing_bits(h264_stream_t* h, bs_t* b);
00300 
00301 void read_rbsp_trailing_bits(h264_stream_t* h, bs_t* b);
00302 
00303 void read_slice_header(h264_stream_t* h, bs_t* b);
00304 void read_ref_pic_list_reordering(h264_stream_t* h, bs_t* b);
00305 void read_pred_weight_table(h264_stream_t* h, bs_t* b);
00306 void read_dec_ref_pic_marking(h264_stream_t* h, bs_t* b);
00307 
00308 void read_seq_parameter_set_rbsp(h264_stream_t* h, bs_t* b);
00309 void read_scaling_list(bs_t* b, int* scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag );
00310 void read_vui_parameters(h264_stream_t* h, bs_t* b);
00311 void read_hrd_parameters(h264_stream_t* h, bs_t* b);
00312 
00313 int more_rbsp_trailing_data(h264_stream_t* h, bs_t* b);
00314 
00315 
00316 int write_nal_unit(h264_stream_t* h, uint8_t* buf, int size);
00317 void write_seq_parameter_set_rbsp(h264_stream_t* h, bs_t* b);
00318 void write_scaling_list(bs_t* b, int* scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag );
00319 void write_vui_parameters(h264_stream_t* h, bs_t* b);
00320 void write_hrd_parameters(h264_stream_t* h, bs_t* b);
00321 void write_pic_parameter_set_rbsp(h264_stream_t* h, bs_t* b);
00322 void write_access_unit_delimiter_rbsp(h264_stream_t* h, bs_t* b);
00323 void write_end_of_seq_rbsp(h264_stream_t* h, bs_t* b);
00324 void write_end_of_stream_rbsp(h264_stream_t* h, bs_t* b);
00325 void write_filler_data_rbsp(h264_stream_t* h, bs_t* b);
00326 void write_slice_layer_rbsp(h264_stream_t* h, bs_t* b);
00327 void write_rbsp_slice_trailing_bits(h264_stream_t* h, bs_t* b);
00328 void write_rbsp_trailing_bits(h264_stream_t* h, bs_t* b);
00329 void write_slice_header(h264_stream_t* h, bs_t* b);
00330 void write_ref_pic_list_reordering(h264_stream_t* h, bs_t* b);
00331 void write_pred_weight_table(h264_stream_t* h, bs_t* b);
00332 void write_dec_ref_pic_marking(h264_stream_t* h, bs_t* b);
00333 
00334 
00335 void debug_sps(sps_t* sps);
00336 void debug_pps(pps_t* pps);
00337 void debug_slice_header(slice_header_t* sh);
00338 void debug_nal(h264_stream_t* h, nal_t* nal);
00339 
00340 void debug_bytes(uint8_t* buf, int len);
00341 void debug_bs(bs_t* b);
00342 
00343 //Table 7-1 NAL unit type codes
00344 #define NAL_UNIT_TYPE_UNSPECIFIED                    0    // Unspecified
00345 #define NAL_UNIT_TYPE_CODED_SLICE_NON_IDR            1    // Coded slice of a non-IDR picture
00346 #define NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_A   2    // Coded slice data partition A
00347 #define NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_B   3    // Coded slice data partition B
00348 #define NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_C   4    // Coded slice data partition C
00349 #define NAL_UNIT_TYPE_CODED_SLICE_IDR                5    // Coded slice of an IDR picture
00350 #define NAL_UNIT_TYPE_SEI                            6    // Supplemental enhancement information (SEI)
00351 #define NAL_UNIT_TYPE_SPS                            7    // Sequence parameter set
00352 #define NAL_UNIT_TYPE_PPS                            8    // Picture parameter set
00353 #define NAL_UNIT_TYPE_AUD                            9    // Access unit delimiter
00354 #define NAL_UNIT_TYPE_END_OF_SEQUENCE               10    // End of sequence
00355 #define NAL_UNIT_TYPE_END_OF_STREAM                 11    // End of stream
00356 #define NAL_UNIT_TYPE_FILLER                        12    // Filler data
00357 #define NAL_UNIT_TYPE_SPS_EXT                       13    // Sequence parameter set extension
00358                                              // 14..18    // Reserved
00359 #define NAL_UNIT_TYPE_CODED_SLICE_AUX               19    // Coded slice of an auxiliary coded picture without partitioning
00360                                              // 20..23    // Reserved
00361                                              // 24..31    // Unspecified
00362 
00363 //7.4.3 Table 7-6. Name association to slice_type
00364 #define SH_SLICE_TYPE_P        0        // P (P slice)
00365 #define SH_SLICE_TYPE_B        1        // B (B slice)
00366 #define SH_SLICE_TYPE_I        2        // I (I slice)
00367 #define SH_SLICE_TYPE_SP       3        // SP (SP slice)
00368 #define SH_SLICE_TYPE_SI       4        // SI (SI slice)
00369 #define SH_SLICE_TYPE_P_ALT    5        // P (P slice)
00370 #define SH_SLICE_TYPE_B_ALT    6        // B (B slice)
00371 #define SH_SLICE_TYPE_I_ALT    7        // I (I slice)
00372 #define SH_SLICE_TYPE_SP_ALT   8        // SP (SP slice)
00373 #define SH_SLICE_TYPE_SI_ALT   9        // SI (SI slice)
00374 
00375 //Appendix E. Table E-1  Meaning of sample aspect ratio indicator
00376 #define SAR_Unspecified  0           // Unspecified
00377 #define SAR_1_1        1             //  1:1
00378 #define SAR_12_11      2             // 12:11
00379 #define SAR_10_11      3             // 10:11
00380 #define SAR_16_11      4             // 16:11
00381 #define SAR_40_33      5             // 40:33
00382 #define SAR_24_11      6             // 24:11
00383 #define SAR_20_11      7             // 20:11
00384 #define SAR_32_11      8             // 32:11
00385 #define SAR_80_33      9             // 80:33
00386 #define SAR_18_11     10             // 18:11
00387 #define SAR_15_11     11             // 15:11
00388 #define SAR_64_33     12             // 64:33
00389 #define SAR_160_99    13             // 160:99
00390                                      // 14..254           Reserved
00391 #define SAR_Extended      255        // Extended_SAR
00392 
00393 
00394 
00395 
00396 #ifdef __cplusplus
00397 }
00398 #endif
00399 
00400 #endif

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