1 /*****************************************************************************
2  * Copyright © 2010-2014 VideoLAN
3  *
4  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
5  *
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.
10  *
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.
15  *
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 #ifndef H264_NAL_H
21 # define H264_NAL_H
22 
23 # include <vlc_common.h>
24 # include <vlc_es.h>
25 
26 #define PROFILE_H264_BASELINE             66
27 #define PROFILE_H264_MAIN                 77
28 #define PROFILE_H264_EXTENDED             88
29 #define PROFILE_H264_HIGH                 100
30 #define PROFILE_H264_HIGH_10              110
31 #define PROFILE_H264_HIGH_422             122
32 #define PROFILE_H264_HIGH_444             144
33 #define PROFILE_H264_HIGH_444_PREDICTIVE  244
34 
35 #define PROFILE_H264_CAVLC_INTRA          44
36 #define PROFILE_H264_SVC_BASELINE         83
37 #define PROFILE_H264_SVC_HIGH             86
38 #define PROFILE_H264_MVC_STEREO_HIGH      128
39 #define PROFILE_H264_MVC_MULTIVIEW_HIGH   118
40 
41 #define PROFILE_H264_MFC_HIGH                          134
42 #define PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH          138
43 #define PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH 139
44 
45 #define H264_SPS_ID_MAX (31)
46 #define H264_PPS_ID_MAX (255)
47 
48 enum h264_nal_unit_type_e
49 {
50     H264_NAL_UNKNOWN = 0,
51     H264_NAL_SLICE   = 1,
52     H264_NAL_SLICE_DPA   = 2,
53     H264_NAL_SLICE_DPB   = 3,
54     H264_NAL_SLICE_DPC   = 4,
55     H264_NAL_SLICE_IDR   = 5,    /* ref_idc != 0 */
56     H264_NAL_SEI         = 6,    /* ref_idc == 0 */
57     H264_NAL_SPS         = 7,
58     H264_NAL_PPS         = 8,
59     H264_NAL_AU_DELIMITER= 9,
60     /* ref_idc == 0 for 6,9,10,11,12 */
61     H264_NAL_END_OF_SEQ  = 10,
62     H264_NAL_END_OF_STREAM = 11,
63     H264_NAL_FILLER_DATA = 12,
64     H264_NAL_SPS_EXT     = 13,
65     H264_NAL_PREFIX      = 14,
66     H264_NAL_SUBSET_SPS  = 15,
67     H264_NAL_DEPTH_PS    = 16,
68     H264_NAL_RESERVED_17 = 17,
69     H264_NAL_RESERVED_18 = 18,
70     H264_NAL_SLICE_WP    = 19,
71     H264_NAL_SLICE_EXT   = 20,
72     H264_NAL_SLICE_3D_EXT= 21,
73     H264_NAL_RESERVED_22 = 22,
74     H264_NAL_RESERVED_23 = 23,
75 };
76 
77 typedef struct h264_sequence_parameter_set_t h264_sequence_parameter_set_t;
78 typedef struct h264_picture_parameter_set_t h264_picture_parameter_set_t;
79 
80 h264_sequence_parameter_set_t * h264_decode_sps( const uint8_t *, size_t, bool );
81 h264_picture_parameter_set_t *  h264_decode_pps( const uint8_t *, size_t, bool );
82 
83 void h264_release_sps( h264_sequence_parameter_set_t * );
84 void h264_release_pps( h264_picture_parameter_set_t * );
85 
86 struct h264_sequence_parameter_set_t
87 {
88     uint8_t i_id;
89     uint8_t i_profile, i_level;
90     uint8_t i_constraint_set_flags;
91     /* according to avcC, 3 bits max for those */
92     uint8_t i_chroma_idc;
93     uint8_t i_bit_depth_luma;
94     uint8_t i_bit_depth_chroma;
95     uint8_t b_separate_colour_planes_flag;
96 
97     uint32_t pic_width_in_mbs_minus1;
98     uint32_t pic_height_in_map_units_minus1;
99     struct
100     {
101         uint32_t left_offset;
102         uint32_t right_offset;
103         uint32_t top_offset;
104         uint32_t bottom_offset;
105     } frame_crop;
106     uint8_t frame_mbs_only_flag;
107     uint8_t mb_adaptive_frame_field_flag;
108     int i_log2_max_frame_num;
109     int i_pic_order_cnt_type;
110     int i_delta_pic_order_always_zero_flag;
111     int32_t offset_for_non_ref_pic;
112     int32_t offset_for_top_to_bottom_field;
113     int i_num_ref_frames_in_pic_order_cnt_cycle;
114     int32_t offset_for_ref_frame[255];
115     int i_log2_max_pic_order_cnt_lsb;
116 
117     struct {
118         bool b_valid;
119         int i_sar_num, i_sar_den;
120         struct {
121             bool b_full_range;
122             uint8_t i_colour_primaries;
123             uint8_t i_transfer_characteristics;
124             uint8_t i_matrix_coefficients;
125         } colour;
126         bool b_timing_info_present_flag;
127         uint32_t i_num_units_in_tick;
128         uint32_t i_time_scale;
129         bool b_fixed_frame_rate;
130         bool b_pic_struct_present_flag;
131         bool b_hrd_parameters_present_flag; /* CpbDpbDelaysPresentFlag */
132         uint8_t i_cpb_removal_delay_length_minus1;
133         uint8_t i_dpb_output_delay_length_minus1;
134 
135         /* restrictions */
136         uint8_t b_bitstream_restriction_flag;
137         uint8_t i_max_num_reorder_frames;
138     } vui;
139 };
140 
141 struct h264_picture_parameter_set_t
142 {
143     uint8_t i_id;
144     uint8_t i_sps_id;
145     uint8_t i_pic_order_present_flag;
146     uint8_t i_redundant_pic_present_flag;
147     uint8_t weighted_pred_flag;
148     uint8_t weighted_bipred_idc;
149 };
150 
151 /*
152     AnnexB : [\x00] \x00 \x00 \x01 Prefixed NAL
153     AVC Sample format : NalLengthSize encoded size prefixed NAL
154     avcC: AVCDecoderConfigurationRecord combining SPS & PPS in AVC Sample Format
155 */
156 
157 #define H264_MIN_AVCC_SIZE 7
158 
159 bool h264_isavcC( const uint8_t *, size_t );
160 
161 /* Convert AVC Sample format to Annex B in-place */
162 void h264_AVC_to_AnnexB( uint8_t *p_buf, uint32_t i_len,
163                          uint8_t i_nal_length_size );
164 
165 /* Get the First SPS/PPS NAL pointers from an Annex B buffer
166  * Returns TRUE if a SPS and/or a PPS is found */
167 bool h264_AnnexB_get_spspps( const uint8_t *p_buf, size_t i_buf,
168                              const uint8_t **pp_sps, size_t *p_sps_size,
169                              const uint8_t **pp_pps, size_t *p_pps_size,
170                              const uint8_t **pp_ext, size_t *p_ext_size );
171 
172 /* Create a AVCDecoderConfigurationRecord from non prefixed SPS/PPS
173  * Returns a valid block_t on success, must be freed with block_Release */
174 block_t *h264_NAL_to_avcC( uint8_t i_nal_length_size,
175                            const uint8_t **pp_sps_buf,
176                            const size_t *p_sps_size, uint8_t i_sps_count,
177                            const uint8_t **pp_pps_buf,
178                            const size_t *p_pps_size, uint8_t i_pps_count );
179 
180 /* Convert AVCDecoderConfigurationRecord SPS/PPS to Annex B format */
181 uint8_t * h264_avcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
182                                    size_t *pi_result, uint8_t *pi_nal_length_size );
183 
184 bool h264_get_dpb_values( const h264_sequence_parameter_set_t *,
185                           uint8_t *pi_depth, unsigned *pi_delay );
186 
187 bool h264_get_picture_size( const h264_sequence_parameter_set_t *, unsigned *p_w, unsigned *p_h,
188                             unsigned *p_vw, unsigned *p_vh );
189 bool h264_get_chroma_luma( const h264_sequence_parameter_set_t *, uint8_t *pi_chroma_format,
190                            uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma );
191 bool h264_get_colorimetry( const h264_sequence_parameter_set_t *p_sps,
192                            video_color_primaries_t *p_primaries,
193                            video_transfer_func_t *p_transfer,
194                            video_color_space_t *p_colorspace,
195                            bool *p_full_range );
196 
197 /* Get level and Profile from DecoderConfigurationRecord */
198 bool h264_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
199                             uint8_t *pi_level, uint8_t *p_nal_length_size);
200 
201 #endif /* H264_NAL_H */
202