access: srt: add support stream encryption
[vlc.git] / modules / packetizer / h264_nal.c
blob847f976a44b698a805d45f0a7c9a73ee7b27af12
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 *****************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <assert.h>
26 #include "h264_nal.h"
27 #include "hxxx_nal.h"
29 #include <vlc_bits.h>
30 #include <vlc_boxes.h>
31 #include <vlc_es.h>
32 #include <limits.h>
34 /* H264 Level limits from Table A-1 */
35 typedef struct
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,
61 const struct
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 &&
93 p_buf[0] != 0x00 &&
94 p_buf[1] != 0x00
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 )
104 size_t i_total = 0;
106 if( i_buf < H264_MIN_AVCC_SIZE )
107 return 0;
109 p_buf += 5;
110 i_buf -= 5;
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);
116 p_buf++; i_buf--;
118 for ( unsigned int i = 0; i < i_loop_end; i++ )
120 if( i_buf < 2 )
121 return 0;
123 uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
124 if(i_nal_size > i_buf - 2)
125 return 0;
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 )
132 return 0;
134 return i_total;
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 )
142 return NULL;
144 /* Read infos in first 6 bytes */
145 if ( pi_nal_length_size )
146 *pi_nal_length_size = (p_buf[4] & 0x03) + 1;
148 uint8_t *p_ret;
149 uint8_t *p_out_buf = p_ret = malloc( *pi_result );
150 if( !p_out_buf )
152 *pi_result = 0;
153 return NULL;
156 p_buf += 5;
158 for ( unsigned int j = 0; j < 2; j++ )
160 const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
161 p_buf++;
163 for ( unsigned int i = 0; i < i_loop_end; i++)
165 uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
166 p_buf += 2;
168 memcpy( p_out_buf, annexb_startcode4, 4 );
169 p_out_buf += 4;
171 memcpy( p_out_buf, p_buf, i_nal_size );
172 p_out_buf += i_nal_size;
173 p_buf += i_nal_size;
177 return p_ret;
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;
184 uint8_t nal_pos = 0;
186 if( i_nal_length_size != 4 )
187 return;
189 /* This only works for a NAL length size of 4 */
190 /* TODO: realloc/memmove if i_nal_length_size is 2 or 1 */
191 while( i_len > 0 )
193 if( nal_pos < i_nal_length_size ) {
194 unsigned int i;
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];
197 p_buf[i] = 0;
199 if( nal_pos < i_nal_length_size )
200 return;
201 p_buf[i - 1] = 1;
202 p_buf += i;
203 i_len -= i;
205 if( nal_len > INT_MAX )
206 return;
207 if( nal_len > i_len )
209 nal_len -= i_len;
210 return;
212 else
214 p_buf += nal_len;
215 i_len -= nal_len;
216 nal_len = 0;
217 nal_pos = 0;
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 ) )
237 if( i_nal < 2 )
238 continue;
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 )
243 break;
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)
250 else
251 IFSET_NAL(H264_NAL_PPS, pps)
252 else
253 IFSET_NAL(H264_NAL_SPS_EXT, ext);
254 #undef IFSET_NAL
257 return (pp_sps && *p_sps_size) || (pp_pps && *p_pps_size);
260 void h264_release_sps( h264_sequence_parameter_set_t *p_sps )
262 free( 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 )
270 int i_tmp;
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 );
276 /* sps id */
277 uint32_t i_sps_id = bs_read_ue( p_bs );
278 if( i_sps_id > H264_SPS_ID_MAX )
279 return false;
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 );
300 else
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 */
307 bs_skip( p_bs, 1 );
308 /* seq_scaling_matrix_present_flag */
309 i_tmp = bs_read( p_bs, 1 );
310 if( i_tmp )
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 );
316 if( !i_tmp )
317 continue;
318 const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
319 /* scaling_list (...) */
320 int i_lastscale = 8;
321 int i_nextscale = 8;
322 for( int j = 0; j < i_size_of_scaling_list; j++ )
324 if( i_nextscale != 0 )
326 /* delta_scale */
327 i_tmp = bs_read_se( p_bs );
328 i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
329 /* useDefaultScalingMatrixFlag = ... */
331 /* scalinglist[j] */
332 i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
337 else
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;
346 /* Read poc_type */
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 )
362 return false;
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 */
367 bs_read_ue( p_bs );
368 /* b_gaps_in_frame_num_value_allowed */
369 bs_skip( p_bs, 1 );
371 /* Read size */
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 */
381 bs_skip( p_bs, 1 );
383 /* crop */
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 );
392 /* vui */
393 i_tmp = bs_read( p_bs, 1 );
394 if( i_tmp )
396 p_sps->vui.b_valid = true;
397 /* read the aspect ratio part if any */
398 i_tmp = bs_read( p_bs, 1 );
399 if( i_tmp )
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 },
407 { 2, 1 },
409 int i_sar = bs_read( p_bs, 8 );
410 int w, h;
412 if( i_sar < 17 )
414 w = sar[i_sar].w;
415 h = sar[i_sar].h;
417 else if( i_sar == 255 )
419 w = bs_read( p_bs, 16 );
420 h = bs_read( p_bs, 16 );
422 else
424 w = 0;
425 h = 0;
428 if( w != 0 && h != 0 )
430 p_sps->vui.i_sar_num = w;
431 p_sps->vui.i_sar_den = h;
433 else
435 p_sps->vui.i_sar_num = 1;
436 p_sps->vui.i_sar_den = 1;
440 /* overscan */
441 i_tmp = bs_read( p_bs, 1 );
442 if ( i_tmp )
443 bs_read( p_bs, 1 );
445 /* video signal type */
446 i_tmp = bs_read( p_bs, 1 );
447 if( i_tmp )
449 bs_read( p_bs, 3 );
450 p_sps->vui.colour.b_full_range = bs_read( p_bs, 1 );
451 /* colour desc */
452 i_tmp = bs_read( p_bs, 1 );
453 if ( i_tmp )
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 );
459 else
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 );
469 if( i_tmp )
471 bs_read_ue( p_bs );
472 bs_read_ue( p_bs );
475 /* timing info */
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 );
489 if( i_tmp )
491 p_sps->vui.b_hrd_parameters_present_flag = true;
492 uint32_t count = bs_read_ue( p_bs ) + 1;
493 if( count > 31 )
494 return false;
495 bs_read( p_bs, 4 );
496 bs_read( p_bs, 4 );
497 for( uint32_t j = 0; j < count; j++ )
499 if( bs_remain( p_bs ) < 23 )
500 return false;
501 bs_read_ue( p_bs );
502 bs_read_ue( p_bs );
503 bs_read( p_bs, 1 );
505 bs_read( p_bs, 5 );
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 );
508 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 */
531 return true;
534 void h264_release_pps( h264_picture_parameter_set_t *p_pps )
536 free( 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 )
545 return false;
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 */
554 return false;
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)
582 sliceGroupSize++;
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 );
603 /* TODO */
605 return true;
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)) \
614 bs_t bs; \
615 bs_init( &bs, p_buf, i_buf ); \
616 unsigned i_bitflow = 0; \
617 if( b_escaped ) \
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 ); \
627 p_h264type = NULL; \
630 return 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 )
648 return NULL;
649 if( i_sps_count == 0 || i_sps_count > H264_SPS_ID_MAX || i_pps_count == 0 )
650 return NULL;
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 )
658 return NULL;
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)
665 return NULL;
666 i_spspps_size += p_pps_size[i] + 2 /* 16be size place holder */;
669 bo_t bo;
670 /* 1 + 3 + 1 + 1 + 1 + i_spspps_size */
671 if( bo_init( &bo, 7 + i_spspps_size ) != true )
672 return NULL;
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] );
692 return bo.b;
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;
708 return NULL;
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 );
714 if( limits )
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;
723 return 16;
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 */
743 break;
745 /* fallthrough */
746 default:
747 i_max_num_reorder_frames = h264_get_max_dpb_frames( p_sps );
748 break;
752 *pi_depth = i_max_num_reorder_frames;
753 *pi_delay = 0;
755 return true;
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 )
771 SubHeightC = 1;
772 if( p_sps->i_chroma_idc > 2 )
773 SubWidthC = 1;
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;
787 return true;
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 )
794 return false;
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;
798 return true;
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,
804 bool *p_full_range )
806 if( !p_sps->vui.b_valid )
807 return false;
808 *p_primaries =
809 hxxx_colour_primaries_to_vlc( p_sps->vui.colour.i_colour_primaries );
810 *p_transfer =
811 hxxx_transfer_characteristics_to_vlc( p_sps->vui.colour.i_transfer_characteristics );
812 *p_colorspace =
813 hxxx_matrix_coeffs_to_vlc( p_sps->vui.colour.i_matrix_coefficients );
814 *p_full_range = p_sps->vui.colour.b_full_range;
815 return true;
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)
824 return false;
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);
831 p += 8;
833 else if(!p[0] && !p[1]) /* FIXME: WTH is setting AnnexB data here ? */
835 if (!p[2] && p[3] == 1)
836 p += 4;
837 else if (p[2] == 1)
838 p += 3;
839 else
840 return false;
842 else return false;
844 if ( ((*p++)&0x1f) != 7) return false;
846 if (pi_profile)
847 *pi_profile = p[0];
849 if (pi_level)
850 *pi_level = p[2];
852 return true;