UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
h264_stream.c
Go to the documentation of this file.
1/*
2 * h264bitstream - a library for reading and writing H.264 video
3 * Copyright (C) 2005-2007 Auroras Entertainment, LLC
4 * Copyright (C) 2008-2011 Avail-TVN
5 *
6 * Written by Alex Izvorski <aizvorski@gmail.com> and Alex Giladi <alex.giladi@gmail.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#include "config_unix.h"
26#include "config_win32.h"
27#endif // defined HAVE_CONFIG_H
28
29#include <stdint.h>
30#include <stdio.h>
31#include <stdlib.h>
32
33#include "debug.h"
34#include "h264_stream.h"
35
36/***************************** reading ******************************/
37
49// 7.3.1 NAL unit syntax
50// 7.4.1.1 Encapsulation of an SODB within an RBSP
51int nal_to_rbsp(const uint8_t* nal_buf, int* nal_size, uint8_t* rbsp_buf, int* rbsp_size)
52{
53 int i;
54 int j = 0;
55 int count = 0;
56
57 for( i = 1; i < *nal_size; i++ )
58 {
59 // in NAL unit, 0x000000, 0x000001 or 0x000002 shall not occur at any byte-aligned position
60 if( ( count == 2 ) && ( nal_buf[i] < 0x03) )
61 {
62 return -1;
63 }
64
65 if( ( count == 2 ) && ( nal_buf[i] == 0x03) )
66 {
67 // check the 4th byte after 0x000003, except when cabac_zero_word is used, in which case the last three bytes of this NAL unit must be 0x000003
68 if((i < *nal_size - 1) && (nal_buf[i+1] > 0x03))
69 {
70 return -1;
71 }
72
73 // if cabac_zero_word is used, the final byte of this NAL unit(0x03) is discarded, and the last two bytes of RBSP must be 0x0000
74 if(i == *nal_size - 1)
75 {
76 break;
77 }
78
79 i++;
80 count = 0;
81 }
82
83 if ( j >= *rbsp_size )
84 {
85 // error, not enough space
86 return -1;
87 }
88
89 rbsp_buf[j] = nal_buf[i];
90 if(nal_buf[i] == 0x00)
91 {
92 count++;
93 }
94 else
95 {
96 count = 0;
97 }
98 j++;
99 }
100
101 *nal_size = i;
102 *rbsp_size = j;
103 return j;
104}
105
106//7.3.2.1 Sequence parameter set RBSP syntax
108
109 int i;
110
111 memset(sps, 0, sizeof(sps_t));
112
113 sps->profile_idc = bs_read_u8(b);
120 sps->reserved_zero_2bits = bs_read_u(b,2); /* all 0's */
121 sps->level_idc = bs_read_u8(b);
123
124 sps->chroma_format_idc = 1;
125 if( sps->profile_idc == 100 || sps->profile_idc == 110 ||
126 sps->profile_idc == 122 || sps->profile_idc == 144 )
127 {
129 if( sps->chroma_format_idc == 3 )
130 {
132 }
138 {
139 for( i = 0; i < 8; i++ )
140 {
142 if( sps->seq_scaling_list_present_flag[ i ] )
143 {
144 if( i < 6 )
145 {
146 read_scaling_list( b, sps->ScalingList4x4[ i ], 16,
148 }
149 else
150 {
151 read_scaling_list( b, sps->ScalingList8x8[ i - 6 ], 64,
152 sps->UseDefaultScalingMatrix8x8Flag[ i - 6 ] );
153 }
154 }
155 }
156 }
157 }
160 if( sps->pic_order_cnt_type == 0 )
161 {
163 }
164 else if( sps->pic_order_cnt_type == 1 )
165 {
170 for( i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++ )
171 {
172 sps->offset_for_ref_frame[ i ] = bs_read_se(b);
173 }
174 }
175 sps->num_ref_frames = bs_read_ue(b);
180 if( !sps->frame_mbs_only_flag )
181 {
183 }
186 if( sps->frame_cropping_flag )
187 {
192 }
195 {
196 read_vui_parameters(sps, b);
197 }
199
200 return 0;
201}
202
203
204//7.3.2.1.1 Scaling list syntax
205void read_scaling_list(bs_t* b, int* scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag )
206{
207 UNUSED(useDefaultScalingMatrixFlag);
208 int j;
209 if(scalingList == NULL)
210 {
211 return;
212 }
213
214 int lastScale = 8;
215 int nextScale = 8;
216 for( j = 0; j < sizeOfScalingList; j++ )
217 {
218 if( nextScale != 0 )
219 {
220 int delta_scale = bs_read_se(b);
221 nextScale = ( lastScale + delta_scale + 256 ) % 256;
222 useDefaultScalingMatrixFlag = ( j == 0 && nextScale == 0 );
223 }
224 scalingList[ j ] = ( nextScale == 0 ) ? lastScale : nextScale;
225 lastScale = scalingList[ j ];
226 }
227}
228
229//Appendix E.1.1 VUI parameters syntax
231{
232
235 {
237 if( sps->vui.aspect_ratio_idc == SAR_Extended )
238 {
239 sps->vui.sar_width = bs_read_u(b,16);
240 sps->vui.sar_height = bs_read_u(b,16);
241 }
242 }
245 {
247 }
250 {
251 sps->vui.video_format = bs_read_u(b,3);
255 {
259 }
260 }
263 {
266 }
269 {
270 sps->vui.num_units_in_tick = bs_read_u(b,32);
271 sps->vui.time_scale = bs_read_u(b,32);
273 }
276 {
277 read_hrd_parameters(sps, b);
278 }
281 {
282 read_hrd_parameters(sps, b);
283 }
285 {
287 }
291 {
299 }
300}
301
302
303//Appendix E.1.2 HRD parameters syntax
305{
306 int SchedSelIdx;
307
308 sps->hrd.cpb_cnt_minus1 = bs_read_ue(b);
309 sps->hrd.bit_rate_scale = bs_read_u(b,4);
310 sps->hrd.cpb_size_scale = bs_read_u(b,4);
311 for( SchedSelIdx = 0; SchedSelIdx <= sps->hrd.cpb_cnt_minus1; SchedSelIdx++ )
312 {
313 sps->hrd.bit_rate_value_minus1[ SchedSelIdx ] = bs_read_ue(b);
314 sps->hrd.cpb_size_value_minus1[ SchedSelIdx ] = bs_read_ue(b);
315 sps->hrd.cbr_flag[ SchedSelIdx ] = bs_read_u1(b);
316 }
320 sps->hrd.time_offset_length = bs_read_u(b,5);
321}
322
323//7.3.2.11 RBSP trailing bits syntax
325{
326 /* int rbsp_stop_one_bit = */ bs_read_u1( b ); // equal to 1
327
328 while( !bs_byte_aligned(b) )
329 {
330 /* int rbsp_alignment_zero_bit = */ bs_read_u1( b ); // equal to 0
331 }
332}
333
334/***************************** debug ******************************/
335
336void debug_sps(sps_t* sps)
337{
338 printf("======= SPS =======\n");
339 printf(" profile_idc : %d \n", sps->profile_idc );
340 printf(" constraint_set0_flag : %d \n", sps->constraint_set0_flag );
341 printf(" constraint_set1_flag : %d \n", sps->constraint_set1_flag );
342 printf(" constraint_set2_flag : %d \n", sps->constraint_set2_flag );
343 printf(" constraint_set3_flag : %d \n", sps->constraint_set3_flag );
344 printf(" constraint_set4_flag : %d \n", sps->constraint_set4_flag );
345 printf(" constraint_set5_flag : %d \n", sps->constraint_set5_flag );
346 printf(" reserved_zero_2bits : %d \n", sps->reserved_zero_2bits );
347 printf(" level_idc : %d \n", sps->level_idc );
348 printf(" seq_parameter_set_id : %d \n", sps->seq_parameter_set_id );
349 printf(" chroma_format_idc : %d \n", sps->chroma_format_idc );
350 printf(" residual_colour_transform_flag : %d \n", sps->residual_colour_transform_flag );
351 printf(" bit_depth_luma_minus8 : %d \n", sps->bit_depth_luma_minus8 );
352 printf(" bit_depth_chroma_minus8 : %d \n", sps->bit_depth_chroma_minus8 );
353 printf(" qpprime_y_zero_transform_bypass_flag : %d \n", sps->qpprime_y_zero_transform_bypass_flag );
354 printf(" seq_scaling_matrix_present_flag : %d \n", sps->seq_scaling_matrix_present_flag );
355 // int seq_scaling_list_present_flag[8];
356 // void* ScalingList4x4[6];
357 // int UseDefaultScalingMatrix4x4Flag[6];
358 // void* ScalingList8x8[2];
359 // int UseDefaultScalingMatrix8x8Flag[2];
360 printf(" log2_max_frame_num_minus4 : %d \n", sps->log2_max_frame_num_minus4 );
361 printf(" pic_order_cnt_type : %d \n", sps->pic_order_cnt_type );
362 printf(" log2_max_pic_order_cnt_lsb_minus4 : %d \n", sps->log2_max_pic_order_cnt_lsb_minus4 );
363 printf(" delta_pic_order_always_zero_flag : %d \n", sps->delta_pic_order_always_zero_flag );
364 printf(" offset_for_non_ref_pic : %d \n", sps->offset_for_non_ref_pic );
365 printf(" offset_for_top_to_bottom_field : %d \n", sps->offset_for_top_to_bottom_field );
366 printf(" num_ref_frames_in_pic_order_cnt_cycle : %d \n", sps->num_ref_frames_in_pic_order_cnt_cycle );
367 // int offset_for_ref_frame[256];
368 printf(" num_ref_frames : %d \n", sps->num_ref_frames );
369 printf(" gaps_in_frame_num_value_allowed_flag : %d \n", sps->gaps_in_frame_num_value_allowed_flag );
370 printf(" pic_width_in_mbs_minus1 : %d \n", sps->pic_width_in_mbs_minus1 );
371 printf(" pic_height_in_map_units_minus1 : %d \n", sps->pic_height_in_map_units_minus1 );
372 printf(" frame_mbs_only_flag : %d \n", sps->frame_mbs_only_flag );
373 printf(" mb_adaptive_frame_field_flag : %d \n", sps->mb_adaptive_frame_field_flag );
374 printf(" direct_8x8_inference_flag : %d \n", sps->direct_8x8_inference_flag );
375 printf(" frame_cropping_flag : %d \n", sps->frame_cropping_flag );
376 printf(" frame_crop_left_offset : %d \n", sps->frame_crop_left_offset );
377 printf(" frame_crop_right_offset : %d \n", sps->frame_crop_right_offset );
378 printf(" frame_crop_top_offset : %d \n", sps->frame_crop_top_offset );
379 printf(" frame_crop_bottom_offset : %d \n", sps->frame_crop_bottom_offset );
380 printf(" vui_parameters_present_flag : %d \n", sps->vui_parameters_present_flag );
381
382 printf("=== VUI ===\n");
383 printf(" aspect_ratio_info_present_flag : %d \n", sps->vui.aspect_ratio_info_present_flag );
384 printf(" aspect_ratio_idc : %d \n", sps->vui.aspect_ratio_idc );
385 printf(" sar_width : %d \n", sps->vui.sar_width );
386 printf(" sar_height : %d \n", sps->vui.sar_height );
387 printf(" overscan_info_present_flag : %d \n", sps->vui.overscan_info_present_flag );
388 printf(" overscan_appropriate_flag : %d \n", sps->vui.overscan_appropriate_flag );
389 printf(" video_signal_type_present_flag : %d \n", sps->vui.video_signal_type_present_flag );
390 printf(" video_format : %d \n", sps->vui.video_format );
391 printf(" video_full_range_flag : %d \n", sps->vui.video_full_range_flag );
392 printf(" colour_description_present_flag : %d \n", sps->vui.colour_description_present_flag );
393 printf(" colour_primaries : %d \n", sps->vui.colour_primaries );
394 printf(" transfer_characteristics : %d \n", sps->vui.transfer_characteristics );
395 printf(" matrix_coefficients : %d \n", sps->vui.matrix_coefficients );
396 printf(" chroma_loc_info_present_flag : %d \n", sps->vui.chroma_loc_info_present_flag );
397 printf(" chroma_sample_loc_type_top_field : %d \n", sps->vui.chroma_sample_loc_type_top_field );
398 printf(" chroma_sample_loc_type_bottom_field : %d \n", sps->vui.chroma_sample_loc_type_bottom_field );
399 printf(" timing_info_present_flag : %d \n", sps->vui.timing_info_present_flag );
400 printf(" num_units_in_tick : %d \n", sps->vui.num_units_in_tick );
401 printf(" time_scale : %d \n", sps->vui.time_scale );
402 printf(" fixed_frame_rate_flag : %d \n", sps->vui.fixed_frame_rate_flag );
403 printf(" nal_hrd_parameters_present_flag : %d \n", sps->vui.nal_hrd_parameters_present_flag );
404 printf(" vcl_hrd_parameters_present_flag : %d \n", sps->vui.vcl_hrd_parameters_present_flag );
405 printf(" low_delay_hrd_flag : %d \n", sps->vui.low_delay_hrd_flag );
406 printf(" pic_struct_present_flag : %d \n", sps->vui.pic_struct_present_flag );
407 printf(" bitstream_restriction_flag : %d \n", sps->vui.bitstream_restriction_flag );
408 printf(" motion_vectors_over_pic_boundaries_flag : %d \n", sps->vui.motion_vectors_over_pic_boundaries_flag );
409 printf(" max_bytes_per_pic_denom : %d \n", sps->vui.max_bytes_per_pic_denom );
410 printf(" max_bits_per_mb_denom : %d \n", sps->vui.max_bits_per_mb_denom );
411 printf(" log2_max_mv_length_horizontal : %d \n", sps->vui.log2_max_mv_length_horizontal );
412 printf(" log2_max_mv_length_vertical : %d \n", sps->vui.log2_max_mv_length_vertical );
413 printf(" num_reorder_frames : %d \n", sps->vui.num_reorder_frames );
414 printf(" max_dec_frame_buffering : %d \n", sps->vui.max_dec_frame_buffering );
415
416 printf("=== HRD ===\n");
417 printf(" cpb_cnt_minus1 : %d \n", sps->hrd.cpb_cnt_minus1 );
418 printf(" bit_rate_scale : %d \n", sps->hrd.bit_rate_scale );
419 printf(" cpb_size_scale : %d \n", sps->hrd.cpb_size_scale );
420 int SchedSelIdx;
421 for( SchedSelIdx = 0; SchedSelIdx <= sps->hrd.cpb_cnt_minus1; SchedSelIdx++ )
422 {
423 printf(" bit_rate_value_minus1[%d] : %d \n", SchedSelIdx, sps->hrd.bit_rate_value_minus1[SchedSelIdx] ); // up to cpb_cnt_minus1, which is <= 31
424 printf(" cpb_size_value_minus1[%d] : %d \n", SchedSelIdx, sps->hrd.cpb_size_value_minus1[SchedSelIdx] );
425 printf(" cbr_flag[%d] : %d \n", SchedSelIdx, sps->hrd.cbr_flag[SchedSelIdx] );
426 }
427 printf(" initial_cpb_removal_delay_length_minus1 : %d \n", sps->hrd.initial_cpb_removal_delay_length_minus1 );
428 printf(" cpb_removal_delay_length_minus1 : %d \n", sps->hrd.cpb_removal_delay_length_minus1 );
429 printf(" dpb_output_delay_length_minus1 : %d \n", sps->hrd.dpb_output_delay_length_minus1 );
430 printf(" time_offset_length : %d \n", sps->hrd.time_offset_length );
431}
432
433
434
#define UNUSED(x)
static uint32_t bs_read_u8(bs_t *b)
Definition bs.h:180
static uint32_t bs_read_u(bs_t *b, int n)
Definition bs.h:158
static uint32_t bs_read_ue(bs_t *b)
Definition bs.h:193
static uint32_t bs_byte_aligned(bs_t *b)
Definition bs.h:111
static uint32_t bs_read_u1(bs_t *b)
Definition bs.h:124
static int32_t bs_read_se(bs_t *b)
Definition bs.h:207
void read_scaling_list(bs_t *b, int *scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag)
void read_hrd_parameters(sps_t *sps, bs_t *b)
void read_vui_parameters(sps_t *sps, bs_t *b)
int nal_to_rbsp(const uint8_t *nal_buf, int *nal_size, uint8_t *rbsp_buf, int *rbsp_size)
Definition h264_stream.c:51
int read_seq_parameter_set_rbsp(sps_t *sps, bs_t *b)
void read_rbsp_trailing_bits(bs_t *b)
void debug_sps(sps_t *sps)
#define SAR_Extended
int count
Definition hd-rum.c:44
Definition bs.h:35
int video_signal_type_present_flag
Definition h264_stream.h:96
int transfer_characteristics
int cpb_cnt_minus1
int matrix_coefficients
int frame_crop_right_offset
Definition h264_stream.h:83
int reserved_zero_2bits
Definition h264_stream.h:52
int frame_crop_left_offset
Definition h264_stream.h:82
int level_idc
Definition h264_stream.h:53
int vui_parameters_present_flag
Definition h264_stream.h:86
int nal_hrd_parameters_present_flag
int timing_info_present_flag
int video_format
Definition h264_stream.h:97
int mb_adaptive_frame_field_flag
Definition h264_stream.h:79
int dpb_output_delay_length_minus1
int direct_8x8_inference_flag
Definition h264_stream.h:80
int pic_height_in_map_units_minus1
Definition h264_stream.h:77
int colour_primaries
int fixed_frame_rate_flag
int video_full_range_flag
Definition h264_stream.h:98
int constraint_set2_flag
Definition h264_stream.h:48
int max_dec_frame_buffering
int vcl_hrd_parameters_present_flag
int num_units_in_tick
int seq_scaling_list_present_flag[8]
Definition h264_stream.h:61
int offset_for_non_ref_pic
Definition h264_stream.h:70
int bit_rate_scale
int colour_description_present_flag
Definition h264_stream.h:99
int log2_max_pic_order_cnt_lsb_minus4
Definition h264_stream.h:68
int sar_width
Definition h264_stream.h:92
struct sps_t::@56 vui
int frame_cropping_flag
Definition h264_stream.h:81
int log2_max_frame_num_minus4
Definition h264_stream.h:66
int overscan_info_present_flag
Definition h264_stream.h:94
int * ScalingList8x8[2]
Definition h264_stream.h:64
int max_bits_per_mb_denom
int constraint_set4_flag
Definition h264_stream.h:50
int seq_parameter_set_id
Definition h264_stream.h:54
int bit_depth_luma_minus8
Definition h264_stream.h:57
int frame_crop_top_offset
Definition h264_stream.h:84
int qpprime_y_zero_transform_bypass_flag
Definition h264_stream.h:59
int time_scale
int motion_vectors_over_pic_boundaries_flag
int bit_depth_chroma_minus8
Definition h264_stream.h:58
int offset_for_top_to_bottom_field
Definition h264_stream.h:71
int cbr_flag[32]
int constraint_set3_flag
Definition h264_stream.h:49
int frame_crop_bottom_offset
Definition h264_stream.h:85
int pic_order_cnt_type
Definition h264_stream.h:67
int num_reorder_frames
int chroma_loc_info_present_flag
int pic_struct_present_flag
int cpb_size_value_minus1[32]
int chroma_sample_loc_type_top_field
int constraint_set5_flag
Definition h264_stream.h:51
int aspect_ratio_idc
Definition h264_stream.h:91
int initial_cpb_removal_delay_length_minus1
int bitstream_restriction_flag
int low_delay_hrd_flag
int UseDefaultScalingMatrix4x4Flag[6]
Definition h264_stream.h:63
int log2_max_mv_length_vertical
int sar_height
Definition h264_stream.h:93
int profile_idc
Definition h264_stream.h:45
int bit_rate_value_minus1[32]
int cpb_removal_delay_length_minus1
int cpb_size_scale
int log2_max_mv_length_horizontal
int num_ref_frames
Definition h264_stream.h:74
int chroma_format_idc
Definition h264_stream.h:55
int gaps_in_frame_num_value_allowed_flag
Definition h264_stream.h:75
int UseDefaultScalingMatrix8x8Flag[2]
Definition h264_stream.h:65
int offset_for_ref_frame[256]
Definition h264_stream.h:73
int chroma_sample_loc_type_bottom_field
int constraint_set0_flag
Definition h264_stream.h:46
int * ScalingList4x4[6]
Definition h264_stream.h:62
int aspect_ratio_info_present_flag
Definition h264_stream.h:90
int delta_pic_order_always_zero_flag
Definition h264_stream.h:69
int overscan_appropriate_flag
Definition h264_stream.h:95
int max_bytes_per_pic_denom
int time_offset_length
struct sps_t::@57 hrd
int constraint_set1_flag
Definition h264_stream.h:47
int pic_width_in_mbs_minus1
Definition h264_stream.h:76
int frame_mbs_only_flag
Definition h264_stream.h:78
int residual_colour_transform_flag
Definition h264_stream.h:56
int seq_scaling_matrix_present_flag
Definition h264_stream.h:60
int num_ref_frames_in_pic_order_cnt_cycle
Definition h264_stream.h:72