decoder: remove unused variable
[vlc.git] / modules / packetizer / h264_nal.c
blob4499fdeb9733768db211baa76633517c10e4078e
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"
28 #include "hxxx_ep3b.h"
29 #include "iso_color_tables.h"
31 #include <vlc_bits.h>
32 #include <vlc_boxes.h>
33 #include <vlc_es.h>
34 #include <limits.h>
36 /* H264 Level limits from Table A-1 */
37 typedef struct
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,
61 H264_LEVEL_NUMBER_6 = 60,
62 H264_LEVEL_NUMBER_6_1 = 61,
63 H264_LEVEL_NUMBER_6_2 = 62,
66 const struct
68 const uint16_t i_level;
69 const h264_level_limits_t limits;
70 } h264_levels_limits[] = {
71 { H264_LEVEL_NUMBER_1_B, { 396 } },
72 { H264_LEVEL_NUMBER_1, { 396 } },
73 { H264_LEVEL_NUMBER_1_1, { 900 } },
74 { H264_LEVEL_NUMBER_1_2, { 2376 } },
75 { H264_LEVEL_NUMBER_1_3, { 2376 } },
76 { H264_LEVEL_NUMBER_2, { 2376 } },
77 { H264_LEVEL_NUMBER_2_1, { 4752 } },
78 { H264_LEVEL_NUMBER_2_2, { 8100 } },
79 { H264_LEVEL_NUMBER_3, { 8100 } },
80 { H264_LEVEL_NUMBER_3_1, { 18000 } },
81 { H264_LEVEL_NUMBER_3_2, { 20480 } },
82 { H264_LEVEL_NUMBER_4, { 32768 } },
83 { H264_LEVEL_NUMBER_4_1, { 32768 } },
84 { H264_LEVEL_NUMBER_4_2, { 34816 } },
85 { H264_LEVEL_NUMBER_5, { 110400 } },
86 { H264_LEVEL_NUMBER_5_1, { 184320 } },
87 { H264_LEVEL_NUMBER_5_2, { 184320 } },
88 { H264_LEVEL_NUMBER_6, { 696320 } },
89 { H264_LEVEL_NUMBER_6_1, { 696320 } },
90 { H264_LEVEL_NUMBER_6_2, { 696320 } },
94 * For avcC specification, see ISO/IEC 14496-15,
95 * For Annex B specification, see ISO/IEC 14496-10
98 bool h264_isavcC( const uint8_t *p_buf, size_t i_buf )
100 return ( i_buf >= H264_MIN_AVCC_SIZE &&
101 p_buf[0] != 0x00 &&
102 p_buf[1] != 0x00
103 /* /!\Broken quicktime streams does not respect reserved bits
104 (p_buf[4] & 0xFC) == 0xFC &&
105 (p_buf[4] & 0x03) != 0x02 &&
106 (p_buf[5] & 0x1F) > 0x00 */
110 static size_t get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf )
112 size_t i_total = 0;
114 if( i_buf < H264_MIN_AVCC_SIZE )
115 return 0;
117 p_buf += 5;
118 i_buf -= 5;
120 for ( unsigned int j = 0; j < 2; j++ )
122 /* First time is SPS, Second is PPS */
123 const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
124 p_buf++; i_buf--;
126 for ( unsigned int i = 0; i < i_loop_end; i++ )
128 if( i_buf < 2 )
129 return 0;
131 uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
132 if(i_nal_size > i_buf - 2)
133 return 0;
134 i_total += i_nal_size + 4;
135 p_buf += i_nal_size + 2;
136 i_buf -= i_nal_size + 2;
139 if( j == 0 && i_buf < 1 )
140 return 0;
142 return i_total;
145 uint8_t *h264_avcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
146 size_t *pi_result, uint8_t *pi_nal_length_size )
148 *pi_result = get_avcC_to_AnnexB_NAL_size( p_buf, i_buf ); /* Does check min size */
149 if( *pi_result == 0 )
150 return NULL;
152 /* Read infos in first 6 bytes */
153 if ( pi_nal_length_size )
154 *pi_nal_length_size = (p_buf[4] & 0x03) + 1;
156 uint8_t *p_ret;
157 uint8_t *p_out_buf = p_ret = malloc( *pi_result );
158 if( !p_out_buf )
160 *pi_result = 0;
161 return NULL;
164 p_buf += 5;
166 for ( unsigned int j = 0; j < 2; j++ )
168 const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
169 p_buf++;
171 for ( unsigned int i = 0; i < i_loop_end; i++)
173 uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
174 p_buf += 2;
176 memcpy( p_out_buf, annexb_startcode4, 4 );
177 p_out_buf += 4;
179 memcpy( p_out_buf, p_buf, i_nal_size );
180 p_out_buf += i_nal_size;
181 p_buf += i_nal_size;
185 return p_ret;
188 void h264_AVC_to_AnnexB( uint8_t *p_buf, uint32_t i_len,
189 uint8_t i_nal_length_size )
191 uint32_t nal_len = 0;
192 uint8_t nal_pos = 0;
194 if( i_nal_length_size != 4 )
195 return;
197 /* This only works for a NAL length size of 4 */
198 /* TODO: realloc/memmove if i_nal_length_size is 2 or 1 */
199 while( i_len > 0 )
201 if( nal_pos < i_nal_length_size ) {
202 unsigned int i;
203 for( i = 0; nal_pos < i_nal_length_size && i < i_len; i++, nal_pos++ ) {
204 nal_len = (nal_len << 8) | p_buf[i];
205 p_buf[i] = 0;
207 if( nal_pos < i_nal_length_size )
208 return;
209 p_buf[i - 1] = 1;
210 p_buf += i;
211 i_len -= i;
213 if( nal_len > INT_MAX )
214 return;
215 if( nal_len > i_len )
217 nal_len -= i_len;
218 return;
220 else
222 p_buf += nal_len;
223 i_len -= nal_len;
224 nal_len = 0;
225 nal_pos = 0;
230 bool h264_AnnexB_get_spspps( const uint8_t *p_buf, size_t i_buf,
231 const uint8_t **pp_sps, size_t *p_sps_size,
232 const uint8_t **pp_pps, size_t *p_pps_size,
233 const uint8_t **pp_ext, size_t *p_ext_size )
235 if( pp_sps ) { *p_sps_size = 0; *pp_sps = NULL; }
236 if( pp_pps ) { *p_pps_size = 0; *pp_pps = NULL; }
237 if( pp_ext ) { *p_ext_size = 0; *pp_ext = NULL; }
239 hxxx_iterator_ctx_t it;
240 hxxx_iterator_init( &it, p_buf, i_buf, 0 );
242 const uint8_t *p_nal; size_t i_nal;
243 while( hxxx_annexb_iterate_next( &it, &p_nal, &i_nal ) )
245 if( i_nal < 2 )
246 continue;
248 const enum h264_nal_unit_type_e i_nal_type = p_nal[0] & 0x1F;
250 if ( i_nal_type <= H264_NAL_SLICE_IDR && i_nal_type != H264_NAL_UNKNOWN )
251 break;
253 #define IFSET_NAL(type, var) \
254 if( i_nal_type == type && pp_##var && *pp_##var == NULL )\
255 { *pp_##var = p_nal; *p_##var##_size = i_nal; }
257 IFSET_NAL(H264_NAL_SPS, sps)
258 else
259 IFSET_NAL(H264_NAL_PPS, pps)
260 else
261 IFSET_NAL(H264_NAL_SPS_EXT, ext);
262 #undef IFSET_NAL
265 return (pp_sps && *p_sps_size) || (pp_pps && *p_pps_size);
268 void h264_release_sps( h264_sequence_parameter_set_t *p_sps )
270 free( p_sps );
273 #define H264_CONSTRAINT_SET_FLAG(N) (0x80 >> N)
275 static bool h264_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
276 h264_sequence_parameter_set_t *p_sps )
278 int i_tmp;
280 int i_profile_idc = bs_read( p_bs, 8 );
281 p_sps->i_profile = i_profile_idc;
282 p_sps->i_constraint_set_flags = bs_read( p_bs, 8 );
283 p_sps->i_level = bs_read( p_bs, 8 );
284 /* sps id */
285 uint32_t i_sps_id = bs_read_ue( p_bs );
286 if( i_sps_id > H264_SPS_ID_MAX )
287 return false;
288 p_sps->i_id = i_sps_id;
290 if( i_profile_idc == PROFILE_H264_HIGH ||
291 i_profile_idc == PROFILE_H264_HIGH_10 ||
292 i_profile_idc == PROFILE_H264_HIGH_422 ||
293 i_profile_idc == PROFILE_H264_HIGH_444 || /* Old one, no longer on spec */
294 i_profile_idc == PROFILE_H264_HIGH_444_PREDICTIVE ||
295 i_profile_idc == PROFILE_H264_CAVLC_INTRA ||
296 i_profile_idc == PROFILE_H264_SVC_BASELINE ||
297 i_profile_idc == PROFILE_H264_SVC_HIGH ||
298 i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_HIGH ||
299 i_profile_idc == PROFILE_H264_MVC_STEREO_HIGH ||
300 i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH ||
301 i_profile_idc == PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH ||
302 i_profile_idc == PROFILE_H264_MFC_HIGH )
304 /* chroma_format_idc */
305 p_sps->i_chroma_idc = bs_read_ue( p_bs );
306 if( p_sps->i_chroma_idc == 3 )
307 p_sps->b_separate_colour_planes_flag = bs_read1( p_bs );
308 else
309 p_sps->b_separate_colour_planes_flag = 0;
310 /* bit_depth_luma_minus8 */
311 p_sps->i_bit_depth_luma = bs_read_ue( p_bs ) + 8;
312 /* bit_depth_chroma_minus8 */
313 p_sps->i_bit_depth_chroma = bs_read_ue( p_bs ) + 8;
314 /* qpprime_y_zero_transform_bypass_flag */
315 bs_skip( p_bs, 1 );
316 /* seq_scaling_matrix_present_flag */
317 i_tmp = bs_read( p_bs, 1 );
318 if( i_tmp )
320 for( int i = 0; i < ((3 != p_sps->i_chroma_idc) ? 8 : 12); i++ )
322 /* seq_scaling_list_present_flag[i] */
323 i_tmp = bs_read( p_bs, 1 );
324 if( !i_tmp )
325 continue;
326 const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
327 /* scaling_list (...) */
328 int i_lastscale = 8;
329 int i_nextscale = 8;
330 for( int j = 0; j < i_size_of_scaling_list; j++ )
332 if( i_nextscale != 0 )
334 /* delta_scale */
335 i_tmp = bs_read_se( p_bs );
336 i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
337 /* useDefaultScalingMatrixFlag = ... */
339 /* scalinglist[j] */
340 i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
345 else
347 p_sps->i_chroma_idc = 1; /* Not present == inferred to 4:2:0 */
348 p_sps->i_bit_depth_luma = 8;
349 p_sps->i_bit_depth_chroma = 8;
352 /* Skip i_log2_max_frame_num */
353 p_sps->i_log2_max_frame_num = bs_read_ue( p_bs );
354 if( p_sps->i_log2_max_frame_num > 12)
355 p_sps->i_log2_max_frame_num = 12;
356 /* Read poc_type */
357 p_sps->i_pic_order_cnt_type = bs_read_ue( p_bs );
358 if( p_sps->i_pic_order_cnt_type == 0 )
360 /* skip i_log2_max_poc_lsb */
361 p_sps->i_log2_max_pic_order_cnt_lsb = bs_read_ue( p_bs );
362 if( p_sps->i_log2_max_pic_order_cnt_lsb > 12 )
363 p_sps->i_log2_max_pic_order_cnt_lsb = 12;
365 else if( p_sps->i_pic_order_cnt_type == 1 )
367 p_sps->i_delta_pic_order_always_zero_flag = bs_read( p_bs, 1 );
368 p_sps->offset_for_non_ref_pic = bs_read_se( p_bs );
369 p_sps->offset_for_top_to_bottom_field = bs_read_se( p_bs );
370 p_sps->i_num_ref_frames_in_pic_order_cnt_cycle = bs_read_ue( p_bs );
371 if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle > 255 )
372 return false;
373 for( int i=0; i<p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; i++ )
374 p_sps->offset_for_ref_frame[i] = bs_read_se( p_bs );
376 /* i_num_ref_frames */
377 bs_read_ue( p_bs );
378 /* b_gaps_in_frame_num_value_allowed */
379 bs_skip( p_bs, 1 );
381 /* Read size */
382 p_sps->pic_width_in_mbs_minus1 = bs_read_ue( p_bs );
383 p_sps->pic_height_in_map_units_minus1 = bs_read_ue( p_bs );
385 /* b_frame_mbs_only */
386 p_sps->frame_mbs_only_flag = bs_read( p_bs, 1 );
387 if( !p_sps->frame_mbs_only_flag )
388 p_sps->mb_adaptive_frame_field_flag = bs_read( p_bs, 1 );
390 /* b_direct8x8_inference */
391 bs_skip( p_bs, 1 );
393 /* crop */
394 if( bs_read1( p_bs ) ) /* frame_cropping_flag */
396 p_sps->frame_crop.left_offset = bs_read_ue( p_bs );
397 p_sps->frame_crop.right_offset = bs_read_ue( p_bs );
398 p_sps->frame_crop.top_offset = bs_read_ue( p_bs );
399 p_sps->frame_crop.bottom_offset = bs_read_ue( p_bs );
402 /* vui */
403 i_tmp = bs_read( p_bs, 1 );
404 if( i_tmp )
406 p_sps->vui.b_valid = true;
407 /* read the aspect ratio part if any */
408 i_tmp = bs_read( p_bs, 1 );
409 if( i_tmp )
411 static const struct { int w, h; } sar[17] =
413 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
414 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
415 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
416 { 64, 33 }, { 160,99 }, { 4, 3 }, { 3, 2 },
417 { 2, 1 },
419 int i_sar = bs_read( p_bs, 8 );
420 int w, h;
422 if( i_sar < 17 )
424 w = sar[i_sar].w;
425 h = sar[i_sar].h;
427 else if( i_sar == 255 )
429 w = bs_read( p_bs, 16 );
430 h = bs_read( p_bs, 16 );
432 else
434 w = 0;
435 h = 0;
438 if( w != 0 && h != 0 )
440 p_sps->vui.i_sar_num = w;
441 p_sps->vui.i_sar_den = h;
443 else
445 p_sps->vui.i_sar_num = 1;
446 p_sps->vui.i_sar_den = 1;
450 /* overscan */
451 i_tmp = bs_read( p_bs, 1 );
452 if ( i_tmp )
453 bs_read( p_bs, 1 );
455 /* video signal type */
456 i_tmp = bs_read( p_bs, 1 );
457 if( i_tmp )
459 bs_read( p_bs, 3 );
460 p_sps->vui.colour.b_full_range = bs_read( p_bs, 1 );
461 /* colour desc */
462 i_tmp = bs_read( p_bs, 1 );
463 if ( i_tmp )
465 p_sps->vui.colour.i_colour_primaries = bs_read( p_bs, 8 );
466 p_sps->vui.colour.i_transfer_characteristics = bs_read( p_bs, 8 );
467 p_sps->vui.colour.i_matrix_coefficients = bs_read( p_bs, 8 );
469 else
471 p_sps->vui.colour.i_colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
472 p_sps->vui.colour.i_transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
473 p_sps->vui.colour.i_matrix_coefficients = ISO_23001_8_MC_UNSPECIFIED;
477 /* chroma loc info */
478 i_tmp = bs_read( p_bs, 1 );
479 if( i_tmp )
481 bs_read_ue( p_bs );
482 bs_read_ue( p_bs );
485 /* timing info */
486 p_sps->vui.b_timing_info_present_flag = bs_read( p_bs, 1 );
487 if( p_sps->vui.b_timing_info_present_flag )
489 p_sps->vui.i_num_units_in_tick = bs_read( p_bs, 32 );
490 p_sps->vui.i_time_scale = bs_read( p_bs, 32 );
491 p_sps->vui.b_fixed_frame_rate = bs_read( p_bs, 1 );
494 /* Nal hrd & VC1 hrd parameters */
495 p_sps->vui.b_hrd_parameters_present_flag = false;
496 for ( int i=0; i<2; i++ )
498 i_tmp = bs_read( p_bs, 1 );
499 if( i_tmp )
501 p_sps->vui.b_hrd_parameters_present_flag = true;
502 uint32_t count = bs_read_ue( p_bs ) + 1;
503 if( count > 31 )
504 return false;
505 bs_read( p_bs, 4 );
506 bs_read( p_bs, 4 );
507 for( uint32_t j = 0; j < count; j++ )
509 if( bs_remain( p_bs ) < 23 )
510 return false;
511 bs_read_ue( p_bs );
512 bs_read_ue( p_bs );
513 bs_read( p_bs, 1 );
515 bs_read( p_bs, 5 );
516 p_sps->vui.i_cpb_removal_delay_length_minus1 = bs_read( p_bs, 5 );
517 p_sps->vui.i_dpb_output_delay_length_minus1 = bs_read( p_bs, 5 );
518 bs_read( p_bs, 5 );
522 if( p_sps->vui.b_hrd_parameters_present_flag )
523 bs_read( p_bs, 1 ); /* low delay hrd */
525 /* pic struct info */
526 p_sps->vui.b_pic_struct_present_flag = bs_read( p_bs, 1 );
528 p_sps->vui.b_bitstream_restriction_flag = bs_read( p_bs, 1 );
529 if( p_sps->vui.b_bitstream_restriction_flag )
531 bs_read( p_bs, 1 ); /* motion vector pic boundaries */
532 bs_read_ue( p_bs ); /* max bytes per pic */
533 bs_read_ue( p_bs ); /* max bits per mb */
534 bs_read_ue( p_bs ); /* log2 max mv h */
535 bs_read_ue( p_bs ); /* log2 max mv v */
536 p_sps->vui.i_max_num_reorder_frames = bs_read_ue( p_bs );
537 bs_read_ue( p_bs ); /* max dec frame buffering */
541 return true;
544 void h264_release_pps( h264_picture_parameter_set_t *p_pps )
546 free( p_pps );
549 static bool h264_parse_picture_parameter_set_rbsp( bs_t *p_bs,
550 h264_picture_parameter_set_t *p_pps )
552 uint32_t i_pps_id = bs_read_ue( p_bs ); // pps id
553 uint32_t i_sps_id = bs_read_ue( p_bs ); // sps id
554 if( i_pps_id > H264_PPS_ID_MAX || i_sps_id > H264_SPS_ID_MAX )
555 return false;
556 p_pps->i_id = i_pps_id;
557 p_pps->i_sps_id = i_sps_id;
559 bs_skip( p_bs, 1 ); // entropy coding mode flag
560 p_pps->i_pic_order_present_flag = bs_read( p_bs, 1 );
562 unsigned num_slice_groups = bs_read_ue( p_bs ) + 1;
563 if( num_slice_groups > 8 ) /* never has value > 7. Annex A, G & J */
564 return false;
565 if( num_slice_groups > 1 )
567 unsigned slice_group_map_type = bs_read_ue( p_bs );
568 if( slice_group_map_type == 0 )
570 for( unsigned i = 0; i < num_slice_groups; i++ )
571 bs_read_ue( p_bs ); /* run_length_minus1[group] */
573 else if( slice_group_map_type == 2 )
575 for( unsigned i = 0; i < num_slice_groups; i++ )
577 bs_read_ue( p_bs ); /* top_left[group] */
578 bs_read_ue( p_bs ); /* bottom_right[group] */
581 else if( slice_group_map_type > 2 && slice_group_map_type < 6 )
583 bs_read1( p_bs ); /* slice_group_change_direction_flag */
584 bs_read_ue( p_bs ); /* slice_group_change_rate_minus1 */
586 else if( slice_group_map_type == 6 )
588 unsigned pic_size_in_maps_units = bs_read_ue( p_bs ) + 1;
589 unsigned sliceGroupSize = 1;
590 while(num_slice_groups > 1)
592 sliceGroupSize++;
593 num_slice_groups = ((num_slice_groups - 1) >> 1) + 1;
595 for( unsigned i = 0; i < pic_size_in_maps_units; i++ )
597 bs_skip( p_bs, sliceGroupSize );
602 bs_read_ue( p_bs ); /* num_ref_idx_l0_default_active_minus1 */
603 bs_read_ue( p_bs ); /* num_ref_idx_l1_default_active_minus1 */
604 p_pps->weighted_pred_flag = bs_read( p_bs, 1 );
605 p_pps->weighted_bipred_idc = bs_read( p_bs, 2 );
606 bs_read_se( p_bs ); /* pic_init_qp_minus26 */
607 bs_read_se( p_bs ); /* pic_init_qs_minus26 */
608 bs_read_se( p_bs ); /* chroma_qp_index_offset */
609 bs_read( p_bs, 1 ); /* deblocking_filter_control_present_flag */
610 bs_read( p_bs, 1 ); /* constrained_intra_pred_flag */
611 p_pps->i_redundant_pic_present_flag = bs_read( p_bs, 1 );
613 /* TODO */
615 return true;
618 #define IMPL_h264_generic_decode( name, h264type, decode, release ) \
619 h264type * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
621 h264type *p_h264type = calloc(1, sizeof(h264type)); \
622 if(likely(p_h264type)) \
624 bs_t bs; \
625 struct hxxx_bsfw_ep3b_ctx_s bsctx; \
626 if( b_escaped ) \
628 hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
629 bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
631 else bs_init( &bs, p_buf, i_buf ); \
632 bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
633 if( !decode( &bs, p_h264type ) ) \
635 release( p_h264type ); \
636 p_h264type = NULL; \
639 return p_h264type; \
642 IMPL_h264_generic_decode( h264_decode_sps, h264_sequence_parameter_set_t,
643 h264_parse_sequence_parameter_set_rbsp, h264_release_sps )
645 IMPL_h264_generic_decode( h264_decode_pps, h264_picture_parameter_set_t,
646 h264_parse_picture_parameter_set_rbsp, h264_release_pps )
648 block_t *h264_NAL_to_avcC( uint8_t i_nal_length_size,
649 const uint8_t **pp_sps_buf,
650 const size_t *p_sps_size, uint8_t i_sps_count,
651 const uint8_t **pp_pps_buf,
652 const size_t *p_pps_size, uint8_t i_pps_count )
654 /* The length of the NAL size is encoded using 1, 2 or 4 bytes */
655 if( i_nal_length_size != 1 && i_nal_length_size != 2
656 && i_nal_length_size != 4 )
657 return NULL;
658 if( i_sps_count == 0 || i_sps_count > H264_SPS_ID_MAX || i_pps_count == 0 )
659 return NULL;
661 /* Calculate the total size of all SPS and PPS NALs */
662 size_t i_spspps_size = 0;
663 for( size_t i = 0; i < i_sps_count; ++i )
665 assert( pp_sps_buf[i] && p_sps_size[i] );
666 if( p_sps_size[i] < 4 || p_sps_size[i] > UINT16_MAX )
667 return NULL;
668 i_spspps_size += p_sps_size[i] + 2 /* 16be size place holder */;
670 for( size_t i = 0; i < i_pps_count; ++i )
672 assert( pp_pps_buf[i] && p_pps_size[i] );
673 if( p_pps_size[i] > UINT16_MAX)
674 return NULL;
675 i_spspps_size += p_pps_size[i] + 2 /* 16be size place holder */;
678 bo_t bo;
679 /* 1 + 3 + 1 + 1 + 1 + i_spspps_size */
680 if( bo_init( &bo, 7 + i_spspps_size ) != true )
681 return NULL;
683 bo_add_8( &bo, 1 ); /* configuration version */
684 bo_add_mem( &bo, 3, &pp_sps_buf[0][1] ); /* i_profile/profile_compatibility/level */
685 bo_add_8( &bo, 0xfc | (i_nal_length_size - 1) ); /* 0b11111100 | lengthsize - 1*/
687 bo_add_8( &bo, 0xe0 | i_sps_count ); /* 0b11100000 | sps_count */
688 for( size_t i = 0; i < i_sps_count; ++i )
690 bo_add_16be( &bo, p_sps_size[i] );
691 bo_add_mem( &bo, p_sps_size[i], pp_sps_buf[i] );
694 bo_add_8( &bo, i_pps_count ); /* pps_count */
695 for( size_t i = 0; i < i_pps_count; ++i )
697 bo_add_16be( &bo, p_pps_size[i] );
698 bo_add_mem( &bo, p_pps_size[i], pp_pps_buf[i] );
701 return bo.b;
704 static const h264_level_limits_t * h264_get_level_limits( const h264_sequence_parameter_set_t *p_sps )
706 uint16_t i_level_number = p_sps->i_level;
707 if( i_level_number == H264_LEVEL_NUMBER_1_1 &&
708 (p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3)) )
710 i_level_number = H264_LEVEL_NUMBER_1_B;
713 for( size_t i=0; i< ARRAY_SIZE(h264_levels_limits); i++ )
714 if( h264_levels_limits[i].i_level == i_level_number )
715 return & h264_levels_limits[i].limits;
717 return NULL;
720 static uint8_t h264_get_max_dpb_frames( const h264_sequence_parameter_set_t *p_sps )
722 const h264_level_limits_t *limits = h264_get_level_limits( p_sps );
723 if( limits )
725 unsigned i_frame_height_in_mbs = ( p_sps->pic_height_in_map_units_minus1 + 1 ) *
726 ( 2 - p_sps->frame_mbs_only_flag );
727 unsigned i_den = ( p_sps->pic_width_in_mbs_minus1 + 1 ) * i_frame_height_in_mbs;
728 uint8_t i_max_dpb_frames = limits->i_max_dpb_mbs / i_den;
729 if( i_max_dpb_frames < 16 )
730 return i_max_dpb_frames;
732 return 16;
735 bool h264_get_dpb_values( const h264_sequence_parameter_set_t *p_sps,
736 uint8_t *pi_depth, unsigned *pi_delay )
738 uint8_t i_max_num_reorder_frames = p_sps->vui.i_max_num_reorder_frames;
739 if( !p_sps->vui.b_bitstream_restriction_flag )
741 switch( p_sps->i_profile ) /* E-2.1 */
743 case PROFILE_H264_BASELINE:
744 i_max_num_reorder_frames = 0; /* only I & P */
745 break;
746 case PROFILE_H264_CAVLC_INTRA:
747 case PROFILE_H264_SVC_HIGH:
748 case PROFILE_H264_HIGH:
749 case PROFILE_H264_HIGH_10:
750 case PROFILE_H264_HIGH_422:
751 case PROFILE_H264_HIGH_444_PREDICTIVE:
752 if( p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3) )
754 i_max_num_reorder_frames = 0; /* all IDR */
755 break;
757 /* fallthrough */
758 default:
759 i_max_num_reorder_frames = h264_get_max_dpb_frames( p_sps );
760 break;
764 *pi_depth = i_max_num_reorder_frames;
765 *pi_delay = 0;
767 return true;
770 bool h264_get_picture_size( const h264_sequence_parameter_set_t *p_sps, unsigned *p_w, unsigned *p_h,
771 unsigned *p_vw, unsigned *p_vh )
773 unsigned CropUnitX = 1;
774 unsigned CropUnitY = 2 - p_sps->frame_mbs_only_flag;
775 if( p_sps->b_separate_colour_planes_flag != 1 )
777 if( p_sps->i_chroma_idc > 0 )
779 unsigned SubWidthC = 2;
780 unsigned SubHeightC = 2;
781 if( p_sps->i_chroma_idc > 1 )
783 SubHeightC = 1;
784 if( p_sps->i_chroma_idc > 2 )
785 SubWidthC = 1;
787 CropUnitX *= SubWidthC;
788 CropUnitY *= SubHeightC;
792 *p_w = 16 * p_sps->pic_width_in_mbs_minus1 + 16;
793 *p_h = 16 * p_sps->pic_height_in_map_units_minus1 + 16;
794 *p_h *= ( 2 - p_sps->frame_mbs_only_flag );
796 *p_vw = *p_w - ( p_sps->frame_crop.left_offset + p_sps->frame_crop.right_offset ) * CropUnitX;
797 *p_vh = *p_h - ( p_sps->frame_crop.bottom_offset + p_sps->frame_crop.top_offset ) * CropUnitY;
799 return true;
802 bool h264_get_chroma_luma( const h264_sequence_parameter_set_t *p_sps, uint8_t *pi_chroma_format,
803 uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma )
805 *pi_chroma_format = p_sps->i_chroma_idc;
806 *pi_depth_luma = p_sps->i_bit_depth_luma;
807 *pi_depth_chroma = p_sps->i_bit_depth_chroma;
808 return true;
810 bool h264_get_colorimetry( const h264_sequence_parameter_set_t *p_sps,
811 video_color_primaries_t *p_primaries,
812 video_transfer_func_t *p_transfer,
813 video_color_space_t *p_colorspace,
814 video_color_range_t *p_full_range )
816 if( !p_sps->vui.b_valid )
817 return false;
818 *p_primaries =
819 iso_23001_8_cp_to_vlc_primaries( p_sps->vui.colour.i_colour_primaries );
820 *p_transfer =
821 iso_23001_8_tc_to_vlc_xfer( p_sps->vui.colour.i_transfer_characteristics );
822 *p_colorspace =
823 iso_23001_8_mc_to_vlc_coeffs( p_sps->vui.colour.i_matrix_coefficients );
824 *p_full_range = p_sps->vui.colour.b_full_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
825 return true;
829 bool h264_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
830 uint8_t *pi_level, uint8_t *pi_nal_length_size)
832 uint8_t *p = (uint8_t*)p_fmt->p_extra;
833 if(p_fmt->i_extra < 8)
834 return false;
836 /* Check the profile / level */
837 if (p[0] == 1 && p_fmt->i_extra >= 12)
839 if (pi_nal_length_size)
840 *pi_nal_length_size = 1 + (p[4]&0x03);
841 p += 8;
843 else if(!p[0] && !p[1]) /* FIXME: WTH is setting AnnexB data here ? */
845 if (!p[2] && p[3] == 1)
846 p += 4;
847 else if (p[2] == 1)
848 p += 3;
849 else
850 return false;
852 else return false;
854 if ( ((*p++)&0x1f) != 7) return false;
856 if (pi_profile)
857 *pi_profile = p[0];
859 if (pi_level)
860 *pi_level = p[2];
862 return true;