e97739dc7a99cca2c77d10158310321df3483d4c
[vlc.git] / modules / packetizer / h264_nal.h
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
25 #define PROFILE_H264_BASELINE             66
26 #define PROFILE_H264_MAIN                 77
27 #define PROFILE_H264_EXTENDED             88
28 #define PROFILE_H264_HIGH                 100
29 #define PROFILE_H264_HIGH_10              110
30 #define PROFILE_H264_HIGH_422             122
31 #define PROFILE_H264_HIGH_444             144
32 #define PROFILE_H264_HIGH_444_PREDICTIVE  244
33
34 #define PROFILE_H264_CAVLC_INTRA          44
35 #define PROFILE_H264_SVC_BASELINE         83
36 #define PROFILE_H264_SVC_HIGH             86
37 #define PROFILE_H264_MVC_STEREO_HIGH      128
38 #define PROFILE_H264_MVC_MULTIVIEW_HIGH   118
39
40 #define PROFILE_H264_MFC_HIGH                          134
41 #define PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH          138
42 #define PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH 139
43
44 #define H264_SPS_MAX (32)
45 #define H264_PPS_MAX (256)
46
47 enum h264_nal_unit_type_e
48 {
49     H264_NAL_UNKNOWN = 0,
50     H264_NAL_SLICE   = 1,
51     H264_NAL_SLICE_DPA   = 2,
52     H264_NAL_SLICE_DPB   = 3,
53     H264_NAL_SLICE_DPC   = 4,
54     H264_NAL_SLICE_IDR   = 5,    /* ref_idc != 0 */
55     H264_NAL_SEI         = 6,    /* ref_idc == 0 */
56     H264_NAL_SPS         = 7,
57     H264_NAL_PPS         = 8,
58     H264_NAL_AU_DELIMITER= 9,
59     /* ref_idc == 0 for 6,9,10,11,12 */
60     H264_NAL_END_OF_SEQ  = 10,
61     H264_NAL_END_OF_STREAM = 11,
62     H264_NAL_FILLER_DATA = 12,
63     H264_NAL_SPS_EXT     = 13,
64     H264_NAL_PREFIX      = 14,
65     H264_NAL_SUBSET_SPS  = 15,
66     H264_NAL_DEPTH_PS    = 16,
67     H264_NAL_RESERVED_17 = 17,
68     H264_NAL_RESERVED_18 = 18,
69     H264_NAL_SLICE_WP    = 19,
70     H264_NAL_SLICE_EXT   = 20,
71     H264_NAL_SLICE_3D_EXT= 21,
72     H264_NAL_RESERVED_22 = 22,
73     H264_NAL_RESERVED_23 = 23,
74 };
75
76 /* Defined in H.264 annex D */
77 enum h264_sei_type_e
78 {
79     H264_SEI_PIC_TIMING = 1,
80     H264_SEI_USER_DATA_REGISTERED_ITU_T_T35 = 4,
81     H264_SEI_RECOVERY_POINT = 6
82 };
83
84 typedef struct h264_sequence_parameter_set_t h264_sequence_parameter_set_t;
85 typedef struct h264_picture_parameter_set_t h264_picture_parameter_set_t;
86
87 h264_sequence_parameter_set_t * h264_decode_sps( const uint8_t *, size_t, bool );
88 h264_picture_parameter_set_t *  h264_decode_pps( const uint8_t *, size_t, bool );
89
90 void h264_release_sps( h264_sequence_parameter_set_t * );
91 void h264_release_pps( h264_picture_parameter_set_t * );
92
93 struct h264_sequence_parameter_set_t
94 {
95     int i_id;
96     uint8_t i_profile, i_level;
97     uint8_t i_constraint_set_flags;
98     /* according to avcC, 3 bits max for those */
99     uint8_t i_chroma_idc;
100     uint8_t i_bit_depth_luma;
101     uint8_t i_bit_depth_chroma;
102
103     uint32_t pic_width_in_mbs_minus1;
104     uint32_t pic_height_in_map_units_minus1;
105     struct
106     {
107         uint32_t left_offset;
108         uint32_t right_offset;
109         uint32_t top_offset;
110         uint32_t bottom_offset;
111     } frame_crop;
112     uint8_t frame_mbs_only_flag;
113     int i_log2_max_frame_num;
114     int i_pic_order_cnt_type;
115     int i_delta_pic_order_always_zero_flag;
116     int i_log2_max_pic_order_cnt_lsb;
117
118     struct {
119         bool b_valid;
120         int i_sar_num, i_sar_den;
121         struct {
122             bool b_full_range;
123             uint8_t i_colour_primaries;
124             uint8_t i_transfer_characteristics;
125             uint8_t i_matrix_coefficients;
126         } colour;
127         bool b_timing_info_present_flag;
128         uint32_t i_num_units_in_tick;
129         uint32_t i_time_scale;
130         bool b_fixed_frame_rate;
131         bool b_pic_struct_present_flag;
132         bool b_cpb_dpb_delays_present_flag;
133         uint8_t i_cpb_removal_delay_length_minus1;
134         uint8_t i_dpb_output_delay_length_minus1;
135     } vui;
136 };
137
138 struct h264_picture_parameter_set_t
139 {
140     int i_id;
141     int i_sps_id;
142     int i_pic_order_present_flag;
143 };
144
145 /*
146     AnnexB : [\x00] \x00 \x00 \x01 Prefixed NAL
147     AVC Sample format : NalLengthSize encoded size prefixed NAL
148     avcC: AVCDecoderConfigurationRecord combining SPS & PPS in AVC Sample Format
149 */
150
151 #define H264_MIN_AVCC_SIZE 7
152
153 bool h264_isavcC( const uint8_t *, size_t );
154
155 /* Convert AVC Sample format to Annex B in-place */
156 void h264_AVC_to_AnnexB( uint8_t *p_buf, uint32_t i_len,
157                          uint8_t i_nal_length_size );
158
159 /* Get the SPS/PPS pointers from an Annex B buffer
160  * Returns 0 if a SPS and/or a PPS is found */
161 int h264_get_spspps( uint8_t *p_buf, size_t i_buf,
162                      uint8_t **pp_sps, size_t *p_sps_size,
163                      uint8_t **pp_pps, size_t *p_pps_size,
164                      uint8_t **pp_ext, size_t *p_ext_size );
165
166 /* Create a AVCDecoderConfigurationRecord from SPS/PPS
167  * Returns a valid block_t on success, must be freed with block_Release */
168 block_t *h264_AnnexB_NAL_to_avcC( uint8_t i_nal_length_size,
169                                   const uint8_t *p_sps_buf,
170                                   size_t i_sps_size,
171                                   const uint8_t *p_pps_buf,
172                                   size_t i_pps_size );
173
174 /* Convert AVCDecoderConfigurationRecord SPS/PPS to Annex B format */
175 uint8_t * h264_avcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
176                                    size_t *pi_result, uint8_t *pi_nal_length_size );
177
178 bool h264_get_picture_size( const h264_sequence_parameter_set_t *, unsigned *p_w, unsigned *p_h,
179                             unsigned *p_vw, unsigned *p_vh );
180 bool h264_get_chroma_luma( const h264_sequence_parameter_set_t *, uint8_t *pi_chroma_format,
181                            uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma );
182
183 /* Get level and Profile */
184 bool h264_get_profile_level(const es_format_t *p_fmt, size_t *p_profile,
185                             size_t *p_level, uint8_t *p_nal_length_size);
186
187 #endif /* H264_NAL_H */