1 /*****************************************************************************
2 * Copyright © 2010-2014 VideoLAN
4 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
30 #include <vlc_boxes.h>
34 /* H264 Level limits from Table A-1 */
37 unsigned i_max_dpb_mbs
;
38 } h264_level_limits_t
;
40 enum h264_level_numbers_e
42 H264_LEVEL_NUMBER_1_B
= 9, /* special level not following the 10x rule */
43 H264_LEVEL_NUMBER_1
= 10,
44 H264_LEVEL_NUMBER_1_1
= 11,
45 H264_LEVEL_NUMBER_1_2
= 12,
46 H264_LEVEL_NUMBER_1_3
= 13,
47 H264_LEVEL_NUMBER_2
= 20,
48 H264_LEVEL_NUMBER_2_1
= 21,
49 H264_LEVEL_NUMBER_2_2
= 22,
50 H264_LEVEL_NUMBER_3
= 30,
51 H264_LEVEL_NUMBER_3_1
= 31,
52 H264_LEVEL_NUMBER_3_2
= 32,
53 H264_LEVEL_NUMBER_4
= 40,
54 H264_LEVEL_NUMBER_4_1
= 41,
55 H264_LEVEL_NUMBER_4_2
= 42,
56 H264_LEVEL_NUMBER_5
= 50,
57 H264_LEVEL_NUMBER_5_1
= 51,
58 H264_LEVEL_NUMBER_5_2
= 52,
63 const uint16_t i_level
;
64 const h264_level_limits_t limits
;
65 } h264_levels_limits
[] = {
66 { H264_LEVEL_NUMBER_1_B
, { 396 } },
67 { H264_LEVEL_NUMBER_1
, { 396 } },
68 { H264_LEVEL_NUMBER_1_1
, { 900 } },
69 { H264_LEVEL_NUMBER_1_2
, { 2376 } },
70 { H264_LEVEL_NUMBER_1_3
, { 2376 } },
71 { H264_LEVEL_NUMBER_2
, { 2376 } },
72 { H264_LEVEL_NUMBER_2_1
, { 4752 } },
73 { H264_LEVEL_NUMBER_2_2
, { 8100 } },
74 { H264_LEVEL_NUMBER_3
, { 8100 } },
75 { H264_LEVEL_NUMBER_3_1
, { 18000 } },
76 { H264_LEVEL_NUMBER_3_2
, { 20480 } },
77 { H264_LEVEL_NUMBER_4
, { 32768 } },
78 { H264_LEVEL_NUMBER_4_1
, { 32768 } },
79 { H264_LEVEL_NUMBER_4_2
, { 34816 } },
80 { H264_LEVEL_NUMBER_5
, { 110400 } },
81 { H264_LEVEL_NUMBER_5_1
, { 184320 } },
82 { H264_LEVEL_NUMBER_5_2
, { 184320 } },
86 * For avcC specification, see ISO/IEC 14496-15,
87 * For Annex B specification, see ISO/IEC 14496-10
90 bool h264_isavcC( const uint8_t *p_buf
, size_t i_buf
)
92 return ( i_buf
>= H264_MIN_AVCC_SIZE
&&
95 /* /!\Broken quicktime streams does not respect reserved bits
96 (p_buf[4] & 0xFC) == 0xFC &&
97 (p_buf[4] & 0x03) != 0x02 &&
98 (p_buf[5] & 0x1F) > 0x00 */
102 static size_t get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf
, size_t i_buf
)
106 if( i_buf
< H264_MIN_AVCC_SIZE
)
112 for ( unsigned int j
= 0; j
< 2; j
++ )
114 /* First time is SPS, Second is PPS */
115 const unsigned int i_loop_end
= p_buf
[0] & (j
== 0 ? 0x1f : 0xff);
118 for ( unsigned int i
= 0; i
< i_loop_end
; i
++ )
123 uint16_t i_nal_size
= (p_buf
[0] << 8) | p_buf
[1];
124 if(i_nal_size
> i_buf
- 2)
126 i_total
+= i_nal_size
+ 4;
127 p_buf
+= i_nal_size
+ 2;
128 i_buf
-= i_nal_size
+ 2;
131 if( j
== 0 && i_buf
< 1 )
137 uint8_t *h264_avcC_to_AnnexB_NAL( const uint8_t *p_buf
, size_t i_buf
,
138 size_t *pi_result
, uint8_t *pi_nal_length_size
)
140 *pi_result
= get_avcC_to_AnnexB_NAL_size( p_buf
, i_buf
); /* Does check min size */
141 if( *pi_result
== 0 )
144 /* Read infos in first 6 bytes */
145 if ( pi_nal_length_size
)
146 *pi_nal_length_size
= (p_buf
[4] & 0x03) + 1;
149 uint8_t *p_out_buf
= p_ret
= malloc( *pi_result
);
158 for ( unsigned int j
= 0; j
< 2; j
++ )
160 const unsigned int i_loop_end
= p_buf
[0] & (j
== 0 ? 0x1f : 0xff);
163 for ( unsigned int i
= 0; i
< i_loop_end
; i
++)
165 uint16_t i_nal_size
= (p_buf
[0] << 8) | p_buf
[1];
168 memcpy( p_out_buf
, annexb_startcode4
, 4 );
171 memcpy( p_out_buf
, p_buf
, i_nal_size
);
172 p_out_buf
+= i_nal_size
;
180 void h264_AVC_to_AnnexB( uint8_t *p_buf
, uint32_t i_len
,
181 uint8_t i_nal_length_size
)
183 uint32_t nal_len
= 0;
186 if( i_nal_length_size
!= 4 )
189 /* This only works for a NAL length size of 4 */
190 /* TODO: realloc/memmove if i_nal_length_size is 2 or 1 */
193 if( nal_pos
< i_nal_length_size
) {
195 for( i
= 0; nal_pos
< i_nal_length_size
&& i
< i_len
; i
++, nal_pos
++ ) {
196 nal_len
= (nal_len
<< 8) | p_buf
[i
];
199 if( nal_pos
< i_nal_length_size
)
205 if( nal_len
> INT_MAX
)
207 if( nal_len
> i_len
)
222 bool h264_AnnexB_get_spspps( const uint8_t *p_buf
, size_t i_buf
,
223 const uint8_t **pp_sps
, size_t *p_sps_size
,
224 const uint8_t **pp_pps
, size_t *p_pps_size
,
225 const uint8_t **pp_ext
, size_t *p_ext_size
)
227 if( pp_sps
) { *p_sps_size
= 0; *pp_sps
= NULL
; }
228 if( pp_pps
) { *p_pps_size
= 0; *pp_pps
= NULL
; }
229 if( pp_ext
) { *p_ext_size
= 0; *pp_ext
= NULL
; }
231 hxxx_iterator_ctx_t it
;
232 hxxx_iterator_init( &it
, p_buf
, i_buf
, 0 );
234 const uint8_t *p_nal
; size_t i_nal
;
235 while( hxxx_annexb_iterate_next( &it
, &p_nal
, &i_nal
) )
240 const enum h264_nal_unit_type_e i_nal_type
= p_nal
[0] & 0x1F;
242 if ( i_nal_type
<= H264_NAL_SLICE_IDR
&& i_nal_type
!= H264_NAL_UNKNOWN
)
245 #define IFSET_NAL(type, var) \
246 if( i_nal_type == type && pp_##var && *pp_##var == NULL )\
247 { *pp_##var = p_nal; *p_##var##_size = i_nal; }
249 IFSET_NAL(H264_NAL_SPS
, sps
)
251 IFSET_NAL(H264_NAL_PPS
, pps
)
253 IFSET_NAL(H264_NAL_SPS_EXT
, ext
);
257 return (pp_sps
&& *p_sps_size
) || (pp_pps
&& *p_pps_size
);
260 void h264_release_sps( h264_sequence_parameter_set_t
*p_sps
)
265 #define H264_CONSTRAINT_SET_FLAG(N) (0x80 >> N)
267 static bool h264_parse_sequence_parameter_set_rbsp( bs_t
*p_bs
,
268 h264_sequence_parameter_set_t
*p_sps
)
272 int i_profile_idc
= bs_read( p_bs
, 8 );
273 p_sps
->i_profile
= i_profile_idc
;
274 p_sps
->i_constraint_set_flags
= bs_read( p_bs
, 8 );
275 p_sps
->i_level
= bs_read( p_bs
, 8 );
277 uint32_t i_sps_id
= bs_read_ue( p_bs
);
278 if( i_sps_id
> H264_SPS_ID_MAX
)
280 p_sps
->i_id
= i_sps_id
;
282 if( i_profile_idc
== PROFILE_H264_HIGH
||
283 i_profile_idc
== PROFILE_H264_HIGH_10
||
284 i_profile_idc
== PROFILE_H264_HIGH_422
||
285 i_profile_idc
== PROFILE_H264_HIGH_444
|| /* Old one, no longer on spec */
286 i_profile_idc
== PROFILE_H264_HIGH_444_PREDICTIVE
||
287 i_profile_idc
== PROFILE_H264_CAVLC_INTRA
||
288 i_profile_idc
== PROFILE_H264_SVC_BASELINE
||
289 i_profile_idc
== PROFILE_H264_SVC_HIGH
||
290 i_profile_idc
== PROFILE_H264_MVC_MULTIVIEW_HIGH
||
291 i_profile_idc
== PROFILE_H264_MVC_STEREO_HIGH
||
292 i_profile_idc
== PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH
||
293 i_profile_idc
== PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH
||
294 i_profile_idc
== PROFILE_H264_MFC_HIGH
)
296 /* chroma_format_idc */
297 p_sps
->i_chroma_idc
= bs_read_ue( p_bs
);
298 if( p_sps
->i_chroma_idc
== 3 )
299 p_sps
->b_separate_colour_planes_flag
= bs_read1( p_bs
);
301 p_sps
->b_separate_colour_planes_flag
= 0;
302 /* bit_depth_luma_minus8 */
303 p_sps
->i_bit_depth_luma
= bs_read_ue( p_bs
) + 8;
304 /* bit_depth_chroma_minus8 */
305 p_sps
->i_bit_depth_chroma
= bs_read_ue( p_bs
) + 8;
306 /* qpprime_y_zero_transform_bypass_flag */
308 /* seq_scaling_matrix_present_flag */
309 i_tmp
= bs_read( p_bs
, 1 );
312 for( int i
= 0; i
< ((3 != p_sps
->i_chroma_idc
) ? 8 : 12); i
++ )
314 /* seq_scaling_list_present_flag[i] */
315 i_tmp
= bs_read( p_bs
, 1 );
318 const int i_size_of_scaling_list
= (i
< 6 ) ? 16 : 64;
319 /* scaling_list (...) */
322 for( int j
= 0; j
< i_size_of_scaling_list
; j
++ )
324 if( i_nextscale
!= 0 )
327 i_tmp
= bs_read_se( p_bs
);
328 i_nextscale
= ( i_lastscale
+ i_tmp
+ 256 ) % 256;
329 /* useDefaultScalingMatrixFlag = ... */
332 i_lastscale
= ( i_nextscale
== 0 ) ? i_lastscale
: i_nextscale
;
339 p_sps
->i_chroma_idc
= 1; /* Not present == inferred to 4:2:2 */
342 /* Skip i_log2_max_frame_num */
343 p_sps
->i_log2_max_frame_num
= bs_read_ue( p_bs
);
344 if( p_sps
->i_log2_max_frame_num
> 12)
345 p_sps
->i_log2_max_frame_num
= 12;
347 p_sps
->i_pic_order_cnt_type
= bs_read_ue( p_bs
);
348 if( p_sps
->i_pic_order_cnt_type
== 0 )
350 /* skip i_log2_max_poc_lsb */
351 p_sps
->i_log2_max_pic_order_cnt_lsb
= bs_read_ue( p_bs
);
352 if( p_sps
->i_log2_max_pic_order_cnt_lsb
> 12 )
353 p_sps
->i_log2_max_pic_order_cnt_lsb
= 12;
355 else if( p_sps
->i_pic_order_cnt_type
== 1 )
357 p_sps
->i_delta_pic_order_always_zero_flag
= bs_read( p_bs
, 1 );
358 p_sps
->offset_for_non_ref_pic
= bs_read_se( p_bs
);
359 p_sps
->offset_for_top_to_bottom_field
= bs_read_se( p_bs
);
360 p_sps
->i_num_ref_frames_in_pic_order_cnt_cycle
= bs_read_ue( p_bs
);
361 if( p_sps
->i_num_ref_frames_in_pic_order_cnt_cycle
> 255 )
363 for( int i
=0; i
<p_sps
->i_num_ref_frames_in_pic_order_cnt_cycle
; i
++ )
364 p_sps
->offset_for_ref_frame
[i
] = bs_read_se( p_bs
);
366 /* i_num_ref_frames */
368 /* b_gaps_in_frame_num_value_allowed */
372 p_sps
->pic_width_in_mbs_minus1
= bs_read_ue( p_bs
);
373 p_sps
->pic_height_in_map_units_minus1
= bs_read_ue( p_bs
);
375 /* b_frame_mbs_only */
376 p_sps
->frame_mbs_only_flag
= bs_read( p_bs
, 1 );
377 if( !p_sps
->frame_mbs_only_flag
)
378 p_sps
->mb_adaptive_frame_field_flag
= bs_read( p_bs
, 1 );
380 /* b_direct8x8_inference */
384 if( bs_read1( p_bs
) ) /* frame_cropping_flag */
386 p_sps
->frame_crop
.left_offset
= bs_read_ue( p_bs
);
387 p_sps
->frame_crop
.right_offset
= bs_read_ue( p_bs
);
388 p_sps
->frame_crop
.top_offset
= bs_read_ue( p_bs
);
389 p_sps
->frame_crop
.bottom_offset
= bs_read_ue( p_bs
);
393 i_tmp
= bs_read( p_bs
, 1 );
396 p_sps
->vui
.b_valid
= true;
397 /* read the aspect ratio part if any */
398 i_tmp
= bs_read( p_bs
, 1 );
401 static const struct { int w
, h
; } sar
[17] =
403 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
404 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
405 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
406 { 64, 33 }, { 160,99 }, { 4, 3 }, { 3, 2 },
409 int i_sar
= bs_read( p_bs
, 8 );
417 else if( i_sar
== 255 )
419 w
= bs_read( p_bs
, 16 );
420 h
= bs_read( p_bs
, 16 );
428 if( w
!= 0 && h
!= 0 )
430 p_sps
->vui
.i_sar_num
= w
;
431 p_sps
->vui
.i_sar_den
= h
;
435 p_sps
->vui
.i_sar_num
= 1;
436 p_sps
->vui
.i_sar_den
= 1;
441 i_tmp
= bs_read( p_bs
, 1 );
445 /* video signal type */
446 i_tmp
= bs_read( p_bs
, 1 );
450 p_sps
->vui
.colour
.b_full_range
= bs_read( p_bs
, 1 );
452 i_tmp
= bs_read( p_bs
, 1 );
455 p_sps
->vui
.colour
.i_colour_primaries
= bs_read( p_bs
, 8 );
456 p_sps
->vui
.colour
.i_transfer_characteristics
= bs_read( p_bs
, 8 );
457 p_sps
->vui
.colour
.i_matrix_coefficients
= bs_read( p_bs
, 8 );
461 p_sps
->vui
.colour
.i_colour_primaries
= HXXX_PRIMARIES_UNSPECIFIED
;
462 p_sps
->vui
.colour
.i_transfer_characteristics
= HXXX_TRANSFER_UNSPECIFIED
;
463 p_sps
->vui
.colour
.i_matrix_coefficients
= HXXX_MATRIX_UNSPECIFIED
;
467 /* chroma loc info */
468 i_tmp
= bs_read( p_bs
, 1 );
476 p_sps
->vui
.b_timing_info_present_flag
= bs_read( p_bs
, 1 );
477 if( p_sps
->vui
.b_timing_info_present_flag
)
479 p_sps
->vui
.i_num_units_in_tick
= bs_read( p_bs
, 32 );
480 p_sps
->vui
.i_time_scale
= bs_read( p_bs
, 32 );
481 p_sps
->vui
.b_fixed_frame_rate
= bs_read( p_bs
, 1 );
484 /* Nal hrd & VC1 hrd parameters */
485 p_sps
->vui
.b_hrd_parameters_present_flag
= false;
486 for ( int i
=0; i
<2; i
++ )
488 i_tmp
= bs_read( p_bs
, 1 );
491 p_sps
->vui
.b_hrd_parameters_present_flag
= true;
492 uint32_t count
= bs_read_ue( p_bs
) + 1;
497 for( uint32_t j
= 0; j
< count
; j
++ )
499 if( bs_remain( p_bs
) < 23 )
506 p_sps
->vui
.i_cpb_removal_delay_length_minus1
= bs_read( p_bs
, 5 );
507 p_sps
->vui
.i_dpb_output_delay_length_minus1
= bs_read( p_bs
, 5 );
512 if( p_sps
->vui
.b_hrd_parameters_present_flag
)
513 bs_read( p_bs
, 1 ); /* low delay hrd */
515 /* pic struct info */
516 p_sps
->vui
.b_pic_struct_present_flag
= bs_read( p_bs
, 1 );
518 p_sps
->vui
.b_bitstream_restriction_flag
= bs_read( p_bs
, 1 );
519 if( p_sps
->vui
.b_bitstream_restriction_flag
)
521 bs_read( p_bs
, 1 ); /* motion vector pic boundaries */
522 bs_read_ue( p_bs
); /* max bytes per pic */
523 bs_read_ue( p_bs
); /* max bits per mb */
524 bs_read_ue( p_bs
); /* log2 max mv h */
525 bs_read_ue( p_bs
); /* log2 max mv v */
526 p_sps
->vui
.i_max_num_reorder_frames
= bs_read_ue( p_bs
);
527 bs_read_ue( p_bs
); /* max dec frame buffering */
534 void h264_release_pps( h264_picture_parameter_set_t
*p_pps
)
539 static bool h264_parse_picture_parameter_set_rbsp( bs_t
*p_bs
,
540 h264_picture_parameter_set_t
*p_pps
)
542 uint32_t i_pps_id
= bs_read_ue( p_bs
); // pps id
543 uint32_t i_sps_id
= bs_read_ue( p_bs
); // sps id
544 if( i_pps_id
> H264_PPS_ID_MAX
|| i_sps_id
> H264_SPS_ID_MAX
)
546 p_pps
->i_id
= i_pps_id
;
547 p_pps
->i_sps_id
= i_sps_id
;
549 bs_skip( p_bs
, 1 ); // entropy coding mode flag
550 p_pps
->i_pic_order_present_flag
= bs_read( p_bs
, 1 );
552 unsigned num_slice_groups
= bs_read_ue( p_bs
) + 1;
553 if( num_slice_groups
> 8 ) /* never has value > 7. Annex A, G & J */
555 if( num_slice_groups
> 1 )
557 unsigned slice_group_map_type
= bs_read_ue( p_bs
);
558 if( slice_group_map_type
== 0 )
560 for( unsigned i
= 0; i
< num_slice_groups
; i
++ )
561 bs_read_ue( p_bs
); /* run_length_minus1[group] */
563 else if( slice_group_map_type
== 2 )
565 for( unsigned i
= 0; i
< num_slice_groups
; i
++ )
567 bs_read_ue( p_bs
); /* top_left[group] */
568 bs_read_ue( p_bs
); /* bottom_right[group] */
571 else if( slice_group_map_type
> 2 && slice_group_map_type
< 6 )
573 bs_read1( p_bs
); /* slice_group_change_direction_flag */
574 bs_read_ue( p_bs
); /* slice_group_change_rate_minus1 */
576 else if( slice_group_map_type
== 6 )
578 unsigned pic_size_in_maps_units
= bs_read_ue( p_bs
) + 1;
579 unsigned sliceGroupSize
= 1;
580 while(num_slice_groups
> 1)
583 num_slice_groups
= ((num_slice_groups
- 1) >> 1) + 1;
585 for( unsigned i
= 0; i
< pic_size_in_maps_units
; i
++ )
587 bs_skip( p_bs
, sliceGroupSize
);
592 bs_read_ue( p_bs
); /* num_ref_idx_l0_default_active_minus1 */
593 bs_read_ue( p_bs
); /* num_ref_idx_l1_default_active_minus1 */
594 p_pps
->weighted_pred_flag
= bs_read( p_bs
, 1 );
595 p_pps
->weighted_bipred_idc
= bs_read( p_bs
, 2 );
596 bs_read_se( p_bs
); /* pic_init_qp_minus26 */
597 bs_read_se( p_bs
); /* pic_init_qs_minus26 */
598 bs_read_se( p_bs
); /* chroma_qp_index_offset */
599 bs_read( p_bs
, 1 ); /* deblocking_filter_control_present_flag */
600 bs_read( p_bs
, 1 ); /* constrained_intra_pred_flag */
601 p_pps
->i_redundant_pic_present_flag
= bs_read( p_bs
, 1 );
608 #define IMPL_h264_generic_decode( name, h264type, decode, release ) \
609 h264type * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
611 h264type *p_h264type = calloc(1, sizeof(h264type)); \
612 if(likely(p_h264type)) \
615 bs_init( &bs, p_buf, i_buf ); \
616 unsigned i_bitflow = 0; \
619 bs.p_fwpriv = &i_bitflow; \
620 bs.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */ \
622 else (void) i_bitflow;\
623 bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
624 if( !decode( &bs, p_h264type ) ) \
626 release( p_h264type ); \
633 IMPL_h264_generic_decode( h264_decode_sps
, h264_sequence_parameter_set_t
,
634 h264_parse_sequence_parameter_set_rbsp
, h264_release_sps
)
636 IMPL_h264_generic_decode( h264_decode_pps
, h264_picture_parameter_set_t
,
637 h264_parse_picture_parameter_set_rbsp
, h264_release_pps
)
639 block_t
*h264_NAL_to_avcC( uint8_t i_nal_length_size
,
640 const uint8_t **pp_sps_buf
,
641 const size_t *p_sps_size
, uint8_t i_sps_count
,
642 const uint8_t **pp_pps_buf
,
643 const size_t *p_pps_size
, uint8_t i_pps_count
)
645 /* The length of the NAL size is encoded using 1, 2 or 4 bytes */
646 if( i_nal_length_size
!= 1 && i_nal_length_size
!= 2
647 && i_nal_length_size
!= 4 )
649 if( i_sps_count
== 0 || i_sps_count
> H264_SPS_ID_MAX
|| i_pps_count
== 0 )
652 /* Calculate the total size of all SPS and PPS NALs */
653 size_t i_spspps_size
= 0;
654 for( size_t i
= 0; i
< i_sps_count
; ++i
)
656 assert( pp_sps_buf
[i
] && p_sps_size
[i
] );
657 if( p_sps_size
[i
] < 4 || p_sps_size
[i
] > UINT16_MAX
)
659 i_spspps_size
+= p_sps_size
[i
] + 2 /* 16be size place holder */;
661 for( size_t i
= 0; i
< i_pps_count
; ++i
)
663 assert( pp_pps_buf
[i
] && p_pps_size
[i
] );
664 if( p_pps_size
[i
] > UINT16_MAX
)
666 i_spspps_size
+= p_pps_size
[i
] + 2 /* 16be size place holder */;
670 /* 1 + 3 + 1 + 1 + 1 + i_spspps_size */
671 if( bo_init( &bo
, 7 + i_spspps_size
) != true )
674 bo_add_8( &bo
, 1 ); /* configuration version */
675 bo_add_mem( &bo
, 3, &pp_sps_buf
[0][1] ); /* i_profile/profile_compatibility/level */
676 bo_add_8( &bo
, 0xfc | (i_nal_length_size
- 1) ); /* 0b11111100 | lengthsize - 1*/
678 bo_add_8( &bo
, 0xe0 | i_sps_count
); /* 0b11100000 | sps_count */
679 for( size_t i
= 0; i
< i_sps_count
; ++i
)
681 bo_add_16be( &bo
, p_sps_size
[i
] );
682 bo_add_mem( &bo
, p_sps_size
[i
], pp_sps_buf
[i
] );
685 bo_add_8( &bo
, i_pps_count
); /* pps_count */
686 for( size_t i
= 0; i
< i_pps_count
; ++i
)
688 bo_add_16be( &bo
, p_pps_size
[i
] );
689 bo_add_mem( &bo
, p_pps_size
[i
], pp_pps_buf
[i
] );
695 static const h264_level_limits_t
* h264_get_level_limits( const h264_sequence_parameter_set_t
*p_sps
)
697 uint16_t i_level_number
= p_sps
->i_level
;
698 if( i_level_number
== H264_LEVEL_NUMBER_1_1
&&
699 (p_sps
->i_constraint_set_flags
& H264_CONSTRAINT_SET_FLAG(3)) )
701 i_level_number
= H264_LEVEL_NUMBER_1_B
;
704 for( size_t i
=0; i
< ARRAY_SIZE(h264_levels_limits
); i
++ )
705 if( h264_levels_limits
[i
].i_level
== i_level_number
)
706 return & h264_levels_limits
[i
].limits
;
711 static uint8_t h264_get_max_dpb_frames( const h264_sequence_parameter_set_t
*p_sps
)
713 const h264_level_limits_t
*limits
= h264_get_level_limits( p_sps
);
716 unsigned i_frame_height_in_mbs
= ( p_sps
->pic_height_in_map_units_minus1
+ 1 ) *
717 ( 2 - p_sps
->frame_mbs_only_flag
);
718 unsigned i_den
= ( p_sps
->pic_width_in_mbs_minus1
+ 1 ) * i_frame_height_in_mbs
;
719 uint8_t i_max_dpb_frames
= limits
->i_max_dpb_mbs
/ i_den
;
720 if( i_max_dpb_frames
< 16 )
721 return i_max_dpb_frames
;
726 bool h264_get_dpb_values( const h264_sequence_parameter_set_t
*p_sps
,
727 uint8_t *pi_depth
, unsigned *pi_delay
)
729 uint8_t i_max_num_reorder_frames
= p_sps
->vui
.i_max_num_reorder_frames
;
730 if( !p_sps
->vui
.b_bitstream_restriction_flag
)
732 switch( p_sps
->i_profile
) /* E-2.1 */
734 case PROFILE_H264_CAVLC_INTRA
:
735 case PROFILE_H264_SVC_HIGH
:
736 case PROFILE_H264_HIGH
:
737 case PROFILE_H264_HIGH_10
:
738 case PROFILE_H264_HIGH_422
:
739 case PROFILE_H264_HIGH_444_PREDICTIVE
:
740 if( p_sps
->i_constraint_set_flags
& H264_CONSTRAINT_SET_FLAG(3) )
742 i_max_num_reorder_frames
= 0; /* all IDR */
747 i_max_num_reorder_frames
= h264_get_max_dpb_frames( p_sps
);
752 *pi_depth
= i_max_num_reorder_frames
;
758 bool h264_get_picture_size( const h264_sequence_parameter_set_t
*p_sps
, unsigned *p_w
, unsigned *p_h
,
759 unsigned *p_vw
, unsigned *p_vh
)
761 unsigned CropUnitX
= 1;
762 unsigned CropUnitY
= 2 - p_sps
->frame_mbs_only_flag
;
763 if( p_sps
->b_separate_colour_planes_flag
!= 1 )
765 if( p_sps
->i_chroma_idc
> 0 )
767 unsigned SubWidthC
= 2;
768 unsigned SubHeightC
= 2;
769 if( p_sps
->i_chroma_idc
> 1 )
772 if( p_sps
->i_chroma_idc
> 2 )
775 CropUnitX
*= SubWidthC
;
776 CropUnitY
*= SubHeightC
;
780 *p_w
= 16 * p_sps
->pic_width_in_mbs_minus1
+ 16;
781 *p_h
= 16 * p_sps
->pic_height_in_map_units_minus1
+ 16;
782 *p_h
*= ( 2 - p_sps
->frame_mbs_only_flag
);
784 *p_vw
= *p_w
- ( p_sps
->frame_crop
.left_offset
+ p_sps
->frame_crop
.right_offset
) * CropUnitX
;
785 *p_vh
= *p_h
- ( p_sps
->frame_crop
.bottom_offset
+ p_sps
->frame_crop
.top_offset
) * CropUnitY
;
790 bool h264_get_chroma_luma( const h264_sequence_parameter_set_t
*p_sps
, uint8_t *pi_chroma_format
,
791 uint8_t *pi_depth_luma
, uint8_t *pi_depth_chroma
)
793 if( p_sps
->i_bit_depth_luma
== 0 )
795 *pi_chroma_format
= p_sps
->i_chroma_idc
;
796 *pi_depth_luma
= p_sps
->i_bit_depth_luma
;
797 *pi_depth_chroma
= p_sps
->i_bit_depth_chroma
;
800 bool h264_get_colorimetry( const h264_sequence_parameter_set_t
*p_sps
,
801 video_color_primaries_t
*p_primaries
,
802 video_transfer_func_t
*p_transfer
,
803 video_color_space_t
*p_colorspace
,
806 if( !p_sps
->vui
.b_valid
)
809 hxxx_colour_primaries_to_vlc( p_sps
->vui
.colour
.i_colour_primaries
);
811 hxxx_transfer_characteristics_to_vlc( p_sps
->vui
.colour
.i_transfer_characteristics
);
813 hxxx_matrix_coeffs_to_vlc( p_sps
->vui
.colour
.i_matrix_coefficients
);
814 *p_full_range
= p_sps
->vui
.colour
.b_full_range
;
819 bool h264_get_profile_level(const es_format_t
*p_fmt
, uint8_t *pi_profile
,
820 uint8_t *pi_level
, uint8_t *pi_nal_length_size
)
822 uint8_t *p
= (uint8_t*)p_fmt
->p_extra
;
823 if(p_fmt
->i_extra
< 8)
826 /* Check the profile / level */
827 if (p
[0] == 1 && p_fmt
->i_extra
>= 12)
829 if (pi_nal_length_size
)
830 *pi_nal_length_size
= 1 + (p
[4]&0x03);
833 else if(!p
[0] && !p
[1]) /* FIXME: WTH is setting AnnexB data here ? */
835 if (!p
[2] && p
[3] == 1)
844 if ( ((*p
++)&0x1f) != 7) return false;