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 *****************************************************************************/
28 #include "hxxx_ep3b.h"
29 #include "iso_color_tables.h"
32 #include <vlc_boxes.h>
36 /* H264 Level limits from Table A-1 */
39 unsigned i_max_dpb_mbs
;
40 } h264_level_limits_t
;
42 enum h264_level_numbers_e
44 H264_LEVEL_NUMBER_1_B
= 9, /* special level not following the 10x rule */
45 H264_LEVEL_NUMBER_1
= 10,
46 H264_LEVEL_NUMBER_1_1
= 11,
47 H264_LEVEL_NUMBER_1_2
= 12,
48 H264_LEVEL_NUMBER_1_3
= 13,
49 H264_LEVEL_NUMBER_2
= 20,
50 H264_LEVEL_NUMBER_2_1
= 21,
51 H264_LEVEL_NUMBER_2_2
= 22,
52 H264_LEVEL_NUMBER_3
= 30,
53 H264_LEVEL_NUMBER_3_1
= 31,
54 H264_LEVEL_NUMBER_3_2
= 32,
55 H264_LEVEL_NUMBER_4
= 40,
56 H264_LEVEL_NUMBER_4_1
= 41,
57 H264_LEVEL_NUMBER_4_2
= 42,
58 H264_LEVEL_NUMBER_5
= 50,
59 H264_LEVEL_NUMBER_5_1
= 51,
60 H264_LEVEL_NUMBER_5_2
= 52,
65 const uint16_t i_level
;
66 const h264_level_limits_t limits
;
67 } h264_levels_limits
[] = {
68 { H264_LEVEL_NUMBER_1_B
, { 396 } },
69 { H264_LEVEL_NUMBER_1
, { 396 } },
70 { H264_LEVEL_NUMBER_1_1
, { 900 } },
71 { H264_LEVEL_NUMBER_1_2
, { 2376 } },
72 { H264_LEVEL_NUMBER_1_3
, { 2376 } },
73 { H264_LEVEL_NUMBER_2
, { 2376 } },
74 { H264_LEVEL_NUMBER_2_1
, { 4752 } },
75 { H264_LEVEL_NUMBER_2_2
, { 8100 } },
76 { H264_LEVEL_NUMBER_3
, { 8100 } },
77 { H264_LEVEL_NUMBER_3_1
, { 18000 } },
78 { H264_LEVEL_NUMBER_3_2
, { 20480 } },
79 { H264_LEVEL_NUMBER_4
, { 32768 } },
80 { H264_LEVEL_NUMBER_4_1
, { 32768 } },
81 { H264_LEVEL_NUMBER_4_2
, { 34816 } },
82 { H264_LEVEL_NUMBER_5
, { 110400 } },
83 { H264_LEVEL_NUMBER_5_1
, { 184320 } },
84 { H264_LEVEL_NUMBER_5_2
, { 184320 } },
88 * For avcC specification, see ISO/IEC 14496-15,
89 * For Annex B specification, see ISO/IEC 14496-10
92 bool h264_isavcC( const uint8_t *p_buf
, size_t i_buf
)
94 return ( i_buf
>= H264_MIN_AVCC_SIZE
&&
97 /* /!\Broken quicktime streams does not respect reserved bits
98 (p_buf[4] & 0xFC) == 0xFC &&
99 (p_buf[4] & 0x03) != 0x02 &&
100 (p_buf[5] & 0x1F) > 0x00 */
104 static size_t get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf
, size_t i_buf
)
108 if( i_buf
< H264_MIN_AVCC_SIZE
)
114 for ( unsigned int j
= 0; j
< 2; j
++ )
116 /* First time is SPS, Second is PPS */
117 const unsigned int i_loop_end
= p_buf
[0] & (j
== 0 ? 0x1f : 0xff);
120 for ( unsigned int i
= 0; i
< i_loop_end
; i
++ )
125 uint16_t i_nal_size
= (p_buf
[0] << 8) | p_buf
[1];
126 if(i_nal_size
> i_buf
- 2)
128 i_total
+= i_nal_size
+ 4;
129 p_buf
+= i_nal_size
+ 2;
130 i_buf
-= i_nal_size
+ 2;
133 if( j
== 0 && i_buf
< 1 )
139 uint8_t *h264_avcC_to_AnnexB_NAL( const uint8_t *p_buf
, size_t i_buf
,
140 size_t *pi_result
, uint8_t *pi_nal_length_size
)
142 *pi_result
= get_avcC_to_AnnexB_NAL_size( p_buf
, i_buf
); /* Does check min size */
143 if( *pi_result
== 0 )
146 /* Read infos in first 6 bytes */
147 if ( pi_nal_length_size
)
148 *pi_nal_length_size
= (p_buf
[4] & 0x03) + 1;
151 uint8_t *p_out_buf
= p_ret
= malloc( *pi_result
);
160 for ( unsigned int j
= 0; j
< 2; j
++ )
162 const unsigned int i_loop_end
= p_buf
[0] & (j
== 0 ? 0x1f : 0xff);
165 for ( unsigned int i
= 0; i
< i_loop_end
; i
++)
167 uint16_t i_nal_size
= (p_buf
[0] << 8) | p_buf
[1];
170 memcpy( p_out_buf
, annexb_startcode4
, 4 );
173 memcpy( p_out_buf
, p_buf
, i_nal_size
);
174 p_out_buf
+= i_nal_size
;
182 void h264_AVC_to_AnnexB( uint8_t *p_buf
, uint32_t i_len
,
183 uint8_t i_nal_length_size
)
185 uint32_t nal_len
= 0;
188 if( i_nal_length_size
!= 4 )
191 /* This only works for a NAL length size of 4 */
192 /* TODO: realloc/memmove if i_nal_length_size is 2 or 1 */
195 if( nal_pos
< i_nal_length_size
) {
197 for( i
= 0; nal_pos
< i_nal_length_size
&& i
< i_len
; i
++, nal_pos
++ ) {
198 nal_len
= (nal_len
<< 8) | p_buf
[i
];
201 if( nal_pos
< i_nal_length_size
)
207 if( nal_len
> INT_MAX
)
209 if( nal_len
> i_len
)
224 bool h264_AnnexB_get_spspps( const uint8_t *p_buf
, size_t i_buf
,
225 const uint8_t **pp_sps
, size_t *p_sps_size
,
226 const uint8_t **pp_pps
, size_t *p_pps_size
,
227 const uint8_t **pp_ext
, size_t *p_ext_size
)
229 if( pp_sps
) { *p_sps_size
= 0; *pp_sps
= NULL
; }
230 if( pp_pps
) { *p_pps_size
= 0; *pp_pps
= NULL
; }
231 if( pp_ext
) { *p_ext_size
= 0; *pp_ext
= NULL
; }
233 hxxx_iterator_ctx_t it
;
234 hxxx_iterator_init( &it
, p_buf
, i_buf
, 0 );
236 const uint8_t *p_nal
; size_t i_nal
;
237 while( hxxx_annexb_iterate_next( &it
, &p_nal
, &i_nal
) )
242 const enum h264_nal_unit_type_e i_nal_type
= p_nal
[0] & 0x1F;
244 if ( i_nal_type
<= H264_NAL_SLICE_IDR
&& i_nal_type
!= H264_NAL_UNKNOWN
)
247 #define IFSET_NAL(type, var) \
248 if( i_nal_type == type && pp_##var && *pp_##var == NULL )\
249 { *pp_##var = p_nal; *p_##var##_size = i_nal; }
251 IFSET_NAL(H264_NAL_SPS
, sps
)
253 IFSET_NAL(H264_NAL_PPS
, pps
)
255 IFSET_NAL(H264_NAL_SPS_EXT
, ext
);
259 return (pp_sps
&& *p_sps_size
) || (pp_pps
&& *p_pps_size
);
262 void h264_release_sps( h264_sequence_parameter_set_t
*p_sps
)
267 #define H264_CONSTRAINT_SET_FLAG(N) (0x80 >> N)
269 static bool h264_parse_sequence_parameter_set_rbsp( bs_t
*p_bs
,
270 h264_sequence_parameter_set_t
*p_sps
)
274 int i_profile_idc
= bs_read( p_bs
, 8 );
275 p_sps
->i_profile
= i_profile_idc
;
276 p_sps
->i_constraint_set_flags
= bs_read( p_bs
, 8 );
277 p_sps
->i_level
= bs_read( p_bs
, 8 );
279 uint32_t i_sps_id
= bs_read_ue( p_bs
);
280 if( i_sps_id
> H264_SPS_ID_MAX
)
282 p_sps
->i_id
= i_sps_id
;
284 if( i_profile_idc
== PROFILE_H264_HIGH
||
285 i_profile_idc
== PROFILE_H264_HIGH_10
||
286 i_profile_idc
== PROFILE_H264_HIGH_422
||
287 i_profile_idc
== PROFILE_H264_HIGH_444
|| /* Old one, no longer on spec */
288 i_profile_idc
== PROFILE_H264_HIGH_444_PREDICTIVE
||
289 i_profile_idc
== PROFILE_H264_CAVLC_INTRA
||
290 i_profile_idc
== PROFILE_H264_SVC_BASELINE
||
291 i_profile_idc
== PROFILE_H264_SVC_HIGH
||
292 i_profile_idc
== PROFILE_H264_MVC_MULTIVIEW_HIGH
||
293 i_profile_idc
== PROFILE_H264_MVC_STEREO_HIGH
||
294 i_profile_idc
== PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH
||
295 i_profile_idc
== PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH
||
296 i_profile_idc
== PROFILE_H264_MFC_HIGH
)
298 /* chroma_format_idc */
299 p_sps
->i_chroma_idc
= bs_read_ue( p_bs
);
300 if( p_sps
->i_chroma_idc
== 3 )
301 p_sps
->b_separate_colour_planes_flag
= bs_read1( p_bs
);
303 p_sps
->b_separate_colour_planes_flag
= 0;
304 /* bit_depth_luma_minus8 */
305 p_sps
->i_bit_depth_luma
= bs_read_ue( p_bs
) + 8;
306 /* bit_depth_chroma_minus8 */
307 p_sps
->i_bit_depth_chroma
= bs_read_ue( p_bs
) + 8;
308 /* qpprime_y_zero_transform_bypass_flag */
310 /* seq_scaling_matrix_present_flag */
311 i_tmp
= bs_read( p_bs
, 1 );
314 for( int i
= 0; i
< ((3 != p_sps
->i_chroma_idc
) ? 8 : 12); i
++ )
316 /* seq_scaling_list_present_flag[i] */
317 i_tmp
= bs_read( p_bs
, 1 );
320 const int i_size_of_scaling_list
= (i
< 6 ) ? 16 : 64;
321 /* scaling_list (...) */
324 for( int j
= 0; j
< i_size_of_scaling_list
; j
++ )
326 if( i_nextscale
!= 0 )
329 i_tmp
= bs_read_se( p_bs
);
330 i_nextscale
= ( i_lastscale
+ i_tmp
+ 256 ) % 256;
331 /* useDefaultScalingMatrixFlag = ... */
334 i_lastscale
= ( i_nextscale
== 0 ) ? i_lastscale
: i_nextscale
;
341 p_sps
->i_chroma_idc
= 1; /* Not present == inferred to 4:2:0 */
342 p_sps
->i_bit_depth_luma
= 8;
343 p_sps
->i_bit_depth_chroma
= 8;
346 /* Skip i_log2_max_frame_num */
347 p_sps
->i_log2_max_frame_num
= bs_read_ue( p_bs
);
348 if( p_sps
->i_log2_max_frame_num
> 12)
349 p_sps
->i_log2_max_frame_num
= 12;
351 p_sps
->i_pic_order_cnt_type
= bs_read_ue( p_bs
);
352 if( p_sps
->i_pic_order_cnt_type
== 0 )
354 /* skip i_log2_max_poc_lsb */
355 p_sps
->i_log2_max_pic_order_cnt_lsb
= bs_read_ue( p_bs
);
356 if( p_sps
->i_log2_max_pic_order_cnt_lsb
> 12 )
357 p_sps
->i_log2_max_pic_order_cnt_lsb
= 12;
359 else if( p_sps
->i_pic_order_cnt_type
== 1 )
361 p_sps
->i_delta_pic_order_always_zero_flag
= bs_read( p_bs
, 1 );
362 p_sps
->offset_for_non_ref_pic
= bs_read_se( p_bs
);
363 p_sps
->offset_for_top_to_bottom_field
= bs_read_se( p_bs
);
364 p_sps
->i_num_ref_frames_in_pic_order_cnt_cycle
= bs_read_ue( p_bs
);
365 if( p_sps
->i_num_ref_frames_in_pic_order_cnt_cycle
> 255 )
367 for( int i
=0; i
<p_sps
->i_num_ref_frames_in_pic_order_cnt_cycle
; i
++ )
368 p_sps
->offset_for_ref_frame
[i
] = bs_read_se( p_bs
);
370 /* i_num_ref_frames */
372 /* b_gaps_in_frame_num_value_allowed */
376 p_sps
->pic_width_in_mbs_minus1
= bs_read_ue( p_bs
);
377 p_sps
->pic_height_in_map_units_minus1
= bs_read_ue( p_bs
);
379 /* b_frame_mbs_only */
380 p_sps
->frame_mbs_only_flag
= bs_read( p_bs
, 1 );
381 if( !p_sps
->frame_mbs_only_flag
)
382 p_sps
->mb_adaptive_frame_field_flag
= bs_read( p_bs
, 1 );
384 /* b_direct8x8_inference */
388 if( bs_read1( p_bs
) ) /* frame_cropping_flag */
390 p_sps
->frame_crop
.left_offset
= bs_read_ue( p_bs
);
391 p_sps
->frame_crop
.right_offset
= bs_read_ue( p_bs
);
392 p_sps
->frame_crop
.top_offset
= bs_read_ue( p_bs
);
393 p_sps
->frame_crop
.bottom_offset
= bs_read_ue( p_bs
);
397 i_tmp
= bs_read( p_bs
, 1 );
400 p_sps
->vui
.b_valid
= true;
401 /* read the aspect ratio part if any */
402 i_tmp
= bs_read( p_bs
, 1 );
405 static const struct { int w
, h
; } sar
[17] =
407 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
408 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
409 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
410 { 64, 33 }, { 160,99 }, { 4, 3 }, { 3, 2 },
413 int i_sar
= bs_read( p_bs
, 8 );
421 else if( i_sar
== 255 )
423 w
= bs_read( p_bs
, 16 );
424 h
= bs_read( p_bs
, 16 );
432 if( w
!= 0 && h
!= 0 )
434 p_sps
->vui
.i_sar_num
= w
;
435 p_sps
->vui
.i_sar_den
= h
;
439 p_sps
->vui
.i_sar_num
= 1;
440 p_sps
->vui
.i_sar_den
= 1;
445 i_tmp
= bs_read( p_bs
, 1 );
449 /* video signal type */
450 i_tmp
= bs_read( p_bs
, 1 );
454 p_sps
->vui
.colour
.b_full_range
= bs_read( p_bs
, 1 );
456 i_tmp
= bs_read( p_bs
, 1 );
459 p_sps
->vui
.colour
.i_colour_primaries
= bs_read( p_bs
, 8 );
460 p_sps
->vui
.colour
.i_transfer_characteristics
= bs_read( p_bs
, 8 );
461 p_sps
->vui
.colour
.i_matrix_coefficients
= bs_read( p_bs
, 8 );
465 p_sps
->vui
.colour
.i_colour_primaries
= ISO_23001_8_CP_UNSPECIFIED
;
466 p_sps
->vui
.colour
.i_transfer_characteristics
= ISO_23001_8_TC_UNSPECIFIED
;
467 p_sps
->vui
.colour
.i_matrix_coefficients
= ISO_23001_8_MC_UNSPECIFIED
;
471 /* chroma loc info */
472 i_tmp
= bs_read( p_bs
, 1 );
480 p_sps
->vui
.b_timing_info_present_flag
= bs_read( p_bs
, 1 );
481 if( p_sps
->vui
.b_timing_info_present_flag
)
483 p_sps
->vui
.i_num_units_in_tick
= bs_read( p_bs
, 32 );
484 p_sps
->vui
.i_time_scale
= bs_read( p_bs
, 32 );
485 p_sps
->vui
.b_fixed_frame_rate
= bs_read( p_bs
, 1 );
488 /* Nal hrd & VC1 hrd parameters */
489 p_sps
->vui
.b_hrd_parameters_present_flag
= false;
490 for ( int i
=0; i
<2; i
++ )
492 i_tmp
= bs_read( p_bs
, 1 );
495 p_sps
->vui
.b_hrd_parameters_present_flag
= true;
496 uint32_t count
= bs_read_ue( p_bs
) + 1;
501 for( uint32_t j
= 0; j
< count
; j
++ )
503 if( bs_remain( p_bs
) < 23 )
510 p_sps
->vui
.i_cpb_removal_delay_length_minus1
= bs_read( p_bs
, 5 );
511 p_sps
->vui
.i_dpb_output_delay_length_minus1
= bs_read( p_bs
, 5 );
516 if( p_sps
->vui
.b_hrd_parameters_present_flag
)
517 bs_read( p_bs
, 1 ); /* low delay hrd */
519 /* pic struct info */
520 p_sps
->vui
.b_pic_struct_present_flag
= bs_read( p_bs
, 1 );
522 p_sps
->vui
.b_bitstream_restriction_flag
= bs_read( p_bs
, 1 );
523 if( p_sps
->vui
.b_bitstream_restriction_flag
)
525 bs_read( p_bs
, 1 ); /* motion vector pic boundaries */
526 bs_read_ue( p_bs
); /* max bytes per pic */
527 bs_read_ue( p_bs
); /* max bits per mb */
528 bs_read_ue( p_bs
); /* log2 max mv h */
529 bs_read_ue( p_bs
); /* log2 max mv v */
530 p_sps
->vui
.i_max_num_reorder_frames
= bs_read_ue( p_bs
);
531 bs_read_ue( p_bs
); /* max dec frame buffering */
538 void h264_release_pps( h264_picture_parameter_set_t
*p_pps
)
543 static bool h264_parse_picture_parameter_set_rbsp( bs_t
*p_bs
,
544 h264_picture_parameter_set_t
*p_pps
)
546 uint32_t i_pps_id
= bs_read_ue( p_bs
); // pps id
547 uint32_t i_sps_id
= bs_read_ue( p_bs
); // sps id
548 if( i_pps_id
> H264_PPS_ID_MAX
|| i_sps_id
> H264_SPS_ID_MAX
)
550 p_pps
->i_id
= i_pps_id
;
551 p_pps
->i_sps_id
= i_sps_id
;
553 bs_skip( p_bs
, 1 ); // entropy coding mode flag
554 p_pps
->i_pic_order_present_flag
= bs_read( p_bs
, 1 );
556 unsigned num_slice_groups
= bs_read_ue( p_bs
) + 1;
557 if( num_slice_groups
> 8 ) /* never has value > 7. Annex A, G & J */
559 if( num_slice_groups
> 1 )
561 unsigned slice_group_map_type
= bs_read_ue( p_bs
);
562 if( slice_group_map_type
== 0 )
564 for( unsigned i
= 0; i
< num_slice_groups
; i
++ )
565 bs_read_ue( p_bs
); /* run_length_minus1[group] */
567 else if( slice_group_map_type
== 2 )
569 for( unsigned i
= 0; i
< num_slice_groups
; i
++ )
571 bs_read_ue( p_bs
); /* top_left[group] */
572 bs_read_ue( p_bs
); /* bottom_right[group] */
575 else if( slice_group_map_type
> 2 && slice_group_map_type
< 6 )
577 bs_read1( p_bs
); /* slice_group_change_direction_flag */
578 bs_read_ue( p_bs
); /* slice_group_change_rate_minus1 */
580 else if( slice_group_map_type
== 6 )
582 unsigned pic_size_in_maps_units
= bs_read_ue( p_bs
) + 1;
583 unsigned sliceGroupSize
= 1;
584 while(num_slice_groups
> 1)
587 num_slice_groups
= ((num_slice_groups
- 1) >> 1) + 1;
589 for( unsigned i
= 0; i
< pic_size_in_maps_units
; i
++ )
591 bs_skip( p_bs
, sliceGroupSize
);
596 bs_read_ue( p_bs
); /* num_ref_idx_l0_default_active_minus1 */
597 bs_read_ue( p_bs
); /* num_ref_idx_l1_default_active_minus1 */
598 p_pps
->weighted_pred_flag
= bs_read( p_bs
, 1 );
599 p_pps
->weighted_bipred_idc
= bs_read( p_bs
, 2 );
600 bs_read_se( p_bs
); /* pic_init_qp_minus26 */
601 bs_read_se( p_bs
); /* pic_init_qs_minus26 */
602 bs_read_se( p_bs
); /* chroma_qp_index_offset */
603 bs_read( p_bs
, 1 ); /* deblocking_filter_control_present_flag */
604 bs_read( p_bs
, 1 ); /* constrained_intra_pred_flag */
605 p_pps
->i_redundant_pic_present_flag
= bs_read( p_bs
, 1 );
612 #define IMPL_h264_generic_decode( name, h264type, decode, release ) \
613 h264type * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
615 h264type *p_h264type = calloc(1, sizeof(h264type)); \
616 if(likely(p_h264type)) \
619 struct hxxx_bsfw_ep3b_ctx_s bsctx; \
622 hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
623 bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
625 else bs_init( &bs, p_buf, i_buf ); \
626 bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
627 if( !decode( &bs, p_h264type ) ) \
629 release( p_h264type ); \
636 IMPL_h264_generic_decode( h264_decode_sps
, h264_sequence_parameter_set_t
,
637 h264_parse_sequence_parameter_set_rbsp
, h264_release_sps
)
639 IMPL_h264_generic_decode( h264_decode_pps
, h264_picture_parameter_set_t
,
640 h264_parse_picture_parameter_set_rbsp
, h264_release_pps
)
642 block_t
*h264_NAL_to_avcC( uint8_t i_nal_length_size
,
643 const uint8_t **pp_sps_buf
,
644 const size_t *p_sps_size
, uint8_t i_sps_count
,
645 const uint8_t **pp_pps_buf
,
646 const size_t *p_pps_size
, uint8_t i_pps_count
)
648 /* The length of the NAL size is encoded using 1, 2 or 4 bytes */
649 if( i_nal_length_size
!= 1 && i_nal_length_size
!= 2
650 && i_nal_length_size
!= 4 )
652 if( i_sps_count
== 0 || i_sps_count
> H264_SPS_ID_MAX
|| i_pps_count
== 0 )
655 /* Calculate the total size of all SPS and PPS NALs */
656 size_t i_spspps_size
= 0;
657 for( size_t i
= 0; i
< i_sps_count
; ++i
)
659 assert( pp_sps_buf
[i
] && p_sps_size
[i
] );
660 if( p_sps_size
[i
] < 4 || p_sps_size
[i
] > UINT16_MAX
)
662 i_spspps_size
+= p_sps_size
[i
] + 2 /* 16be size place holder */;
664 for( size_t i
= 0; i
< i_pps_count
; ++i
)
666 assert( pp_pps_buf
[i
] && p_pps_size
[i
] );
667 if( p_pps_size
[i
] > UINT16_MAX
)
669 i_spspps_size
+= p_pps_size
[i
] + 2 /* 16be size place holder */;
673 /* 1 + 3 + 1 + 1 + 1 + i_spspps_size */
674 if( bo_init( &bo
, 7 + i_spspps_size
) != true )
677 bo_add_8( &bo
, 1 ); /* configuration version */
678 bo_add_mem( &bo
, 3, &pp_sps_buf
[0][1] ); /* i_profile/profile_compatibility/level */
679 bo_add_8( &bo
, 0xfc | (i_nal_length_size
- 1) ); /* 0b11111100 | lengthsize - 1*/
681 bo_add_8( &bo
, 0xe0 | i_sps_count
); /* 0b11100000 | sps_count */
682 for( size_t i
= 0; i
< i_sps_count
; ++i
)
684 bo_add_16be( &bo
, p_sps_size
[i
] );
685 bo_add_mem( &bo
, p_sps_size
[i
], pp_sps_buf
[i
] );
688 bo_add_8( &bo
, i_pps_count
); /* pps_count */
689 for( size_t i
= 0; i
< i_pps_count
; ++i
)
691 bo_add_16be( &bo
, p_pps_size
[i
] );
692 bo_add_mem( &bo
, p_pps_size
[i
], pp_pps_buf
[i
] );
698 static const h264_level_limits_t
* h264_get_level_limits( const h264_sequence_parameter_set_t
*p_sps
)
700 uint16_t i_level_number
= p_sps
->i_level
;
701 if( i_level_number
== H264_LEVEL_NUMBER_1_1
&&
702 (p_sps
->i_constraint_set_flags
& H264_CONSTRAINT_SET_FLAG(3)) )
704 i_level_number
= H264_LEVEL_NUMBER_1_B
;
707 for( size_t i
=0; i
< ARRAY_SIZE(h264_levels_limits
); i
++ )
708 if( h264_levels_limits
[i
].i_level
== i_level_number
)
709 return & h264_levels_limits
[i
].limits
;
714 static uint8_t h264_get_max_dpb_frames( const h264_sequence_parameter_set_t
*p_sps
)
716 const h264_level_limits_t
*limits
= h264_get_level_limits( p_sps
);
719 unsigned i_frame_height_in_mbs
= ( p_sps
->pic_height_in_map_units_minus1
+ 1 ) *
720 ( 2 - p_sps
->frame_mbs_only_flag
);
721 unsigned i_den
= ( p_sps
->pic_width_in_mbs_minus1
+ 1 ) * i_frame_height_in_mbs
;
722 uint8_t i_max_dpb_frames
= limits
->i_max_dpb_mbs
/ i_den
;
723 if( i_max_dpb_frames
< 16 )
724 return i_max_dpb_frames
;
729 bool h264_get_dpb_values( const h264_sequence_parameter_set_t
*p_sps
,
730 uint8_t *pi_depth
, unsigned *pi_delay
)
732 uint8_t i_max_num_reorder_frames
= p_sps
->vui
.i_max_num_reorder_frames
;
733 if( !p_sps
->vui
.b_bitstream_restriction_flag
)
735 switch( p_sps
->i_profile
) /* E-2.1 */
737 case PROFILE_H264_CAVLC_INTRA
:
738 case PROFILE_H264_SVC_HIGH
:
739 case PROFILE_H264_HIGH
:
740 case PROFILE_H264_HIGH_10
:
741 case PROFILE_H264_HIGH_422
:
742 case PROFILE_H264_HIGH_444_PREDICTIVE
:
743 if( p_sps
->i_constraint_set_flags
& H264_CONSTRAINT_SET_FLAG(3) )
745 i_max_num_reorder_frames
= 0; /* all IDR */
750 i_max_num_reorder_frames
= h264_get_max_dpb_frames( p_sps
);
755 *pi_depth
= i_max_num_reorder_frames
;
761 bool h264_get_picture_size( const h264_sequence_parameter_set_t
*p_sps
, unsigned *p_w
, unsigned *p_h
,
762 unsigned *p_vw
, unsigned *p_vh
)
764 unsigned CropUnitX
= 1;
765 unsigned CropUnitY
= 2 - p_sps
->frame_mbs_only_flag
;
766 if( p_sps
->b_separate_colour_planes_flag
!= 1 )
768 if( p_sps
->i_chroma_idc
> 0 )
770 unsigned SubWidthC
= 2;
771 unsigned SubHeightC
= 2;
772 if( p_sps
->i_chroma_idc
> 1 )
775 if( p_sps
->i_chroma_idc
> 2 )
778 CropUnitX
*= SubWidthC
;
779 CropUnitY
*= SubHeightC
;
783 *p_w
= 16 * p_sps
->pic_width_in_mbs_minus1
+ 16;
784 *p_h
= 16 * p_sps
->pic_height_in_map_units_minus1
+ 16;
785 *p_h
*= ( 2 - p_sps
->frame_mbs_only_flag
);
787 *p_vw
= *p_w
- ( p_sps
->frame_crop
.left_offset
+ p_sps
->frame_crop
.right_offset
) * CropUnitX
;
788 *p_vh
= *p_h
- ( p_sps
->frame_crop
.bottom_offset
+ p_sps
->frame_crop
.top_offset
) * CropUnitY
;
793 bool h264_get_chroma_luma( const h264_sequence_parameter_set_t
*p_sps
, uint8_t *pi_chroma_format
,
794 uint8_t *pi_depth_luma
, uint8_t *pi_depth_chroma
)
796 *pi_chroma_format
= p_sps
->i_chroma_idc
;
797 *pi_depth_luma
= p_sps
->i_bit_depth_luma
;
798 *pi_depth_chroma
= p_sps
->i_bit_depth_chroma
;
801 bool h264_get_colorimetry( const h264_sequence_parameter_set_t
*p_sps
,
802 video_color_primaries_t
*p_primaries
,
803 video_transfer_func_t
*p_transfer
,
804 video_color_space_t
*p_colorspace
,
807 if( !p_sps
->vui
.b_valid
)
810 iso_23001_8_cp_to_vlc_primaries( p_sps
->vui
.colour
.i_colour_primaries
);
812 iso_23001_8_tc_to_vlc_xfer( p_sps
->vui
.colour
.i_transfer_characteristics
);
814 iso_23001_8_mc_to_vlc_coeffs( p_sps
->vui
.colour
.i_matrix_coefficients
);
815 *p_full_range
= p_sps
->vui
.colour
.b_full_range
;
820 bool h264_get_profile_level(const es_format_t
*p_fmt
, uint8_t *pi_profile
,
821 uint8_t *pi_level
, uint8_t *pi_nal_length_size
)
823 uint8_t *p
= (uint8_t*)p_fmt
->p_extra
;
824 if(p_fmt
->i_extra
< 8)
827 /* Check the profile / level */
828 if (p
[0] == 1 && p_fmt
->i_extra
>= 12)
830 if (pi_nal_length_size
)
831 *pi_nal_length_size
= 1 + (p
[4]&0x03);
834 else if(!p
[0] && !p
[1]) /* FIXME: WTH is setting AnnexB data here ? */
836 if (!p
[2] && p
[3] == 1)
845 if ( ((*p
++)&0x1f) != 7) return false;