1 /*****************************************************************************
2  * Copyright © 2010-2014 VideoLAN
3  *
4  * Authors: Thomas Guillem <thomas.guillem@gmail.com>
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 HEVC_NAL_H
21 # define HEVC_NAL_H
22 
23 # include <vlc_es.h>
24 # include <vlc_bits.h>
25 
26 #define HEVC_VPS_ID_MAX 15
27 #define HEVC_SPS_ID_MAX 15
28 #define HEVC_PPS_ID_MAX 63
29 
30 enum hevc_general_profile_idc_e
31 {
32     HEVC_PROFILE_NONE               = 0,
33     HEVC_PROFILE_MAIN               = 1,
34     HEVC_PROFILE_MAIN_10            = 2,
35     HEVC_PROFILE_MAIN_STILL_PICTURE = 3,
36     HEVC_PROFILE_REXT               = 4, /* range extensions */
37 };
38 
39 
40 /* Values built from 9 bits mapping of the A-2 bitstream indications for conformance */
41 #define HEVC_EXT_PROFILE_MONOCHROME                 0x1F9
42 #define HEVC_EXT_PROFILE_MONOCHROME_12              0x139
43 #define HEVC_EXT_PROFILE_MONOCHROME_16              0x039
44 #define HEVC_EXT_PROFILE_MAIN_12                    0x131
45 #define HEVC_EXT_PROFILE_MAIN_422_10                0x1A1
46 #define HEVC_EXT_PROFILE_MAIN_422_12                0x121
47 #define HEVC_EXT_PROFILE_MAIN_444                   0x1C1
48 #define HEVC_EXT_PROFILE_MAIN_444_10                0x181
49 #define HEVC_EXT_PROFILE_MAIN_444_12                0x101
50 #define HEVC_EXT_PROFILE_MAIN_INTRA                 0x1F4 /* From this one, lowest bit is insignifiant */
51 #define HEVC_EXT_PROFILE_MAIN_10_INTRA              0x1B4
52 #define HEVC_EXT_PROFILE_MAIN_12_INTRA              0x134
53 #define HEVC_EXT_PROFILE_MAIN_422_10_INTRA          0x1A4
54 #define HEVC_EXT_PROFILE_MAIN_422_12_INTRA          0x124
55 #define HEVC_EXT_PROFILE_MAIN_444_INTRA             0x1C4
56 #define HEVC_EXT_PROFILE_MAIN_444_10_INTRA          0x184
57 #define HEVC_EXT_PROFILE_MAIN_444_12_INTRA          0x104
58 #define HEVC_EXT_PROFILE_MAIN_444_16_INTRA          0x004
59 #define HEVC_EXT_PROFILE_MAIN_444_STILL_PICTURE     0x1C6
60 #define HEVC_EXT_PROFILE_MAIN_444_16_STILL_PICTURE  0x006
61 
62 /* NAL types from https://www.itu.int/rec/T-REC-H.265-201504-I */
63 enum hevc_nal_unit_type_e
64 {
65     HEVC_NAL_TRAIL_N    = 0, /* Trailing */
66     HEVC_NAL_TRAIL_R    = 1, /* Trailing Reference */
67     HEVC_NAL_TSA_N      = 2, /* Temporal Sublayer Access */
68     HEVC_NAL_TSA_R      = 3, /* Temporal Sublayer Access Reference */
69     HEVC_NAL_STSA_N     = 4, /* Stepwise Temporal Sublayer Access */
70     HEVC_NAL_STSA_R     = 5, /* Stepwise Temporal Sublayer Access Reference */
71     HEVC_NAL_RADL_N     = 6, /* Random Access Decodable Leading (display order) */
72     HEVC_NAL_RADL_R     = 7, /* Random Access Decodable Leading (display order) Reference */
73     HEVC_NAL_RASL_N     = 8, /* Random Access Skipped Leading (display order) */
74     HEVC_NAL_RASL_R     = 9, /* Random Access Skipped Leading (display order) Reference */
75     /* 10 to 15 reserved */
76     HEVC_NAL_RSV_VCL_N10= 10,
77     HEVC_NAL_RSV_VCL_N12= 12,
78     HEVC_NAL_RSV_VCL_N14= 14,
79     /* Key frames */
80     HEVC_NAL_BLA_W_LP   = 16, /* Broken Link Access with Associated RASL */
81     HEVC_NAL_BLA_W_RADL = 17, /* Broken Link Access with Associated RADL */
82     HEVC_NAL_BLA_N_LP   = 18, /* Broken Link Access */
83     HEVC_NAL_IDR_W_RADL = 19, /* Instantaneous Decoder Refresh with Associated RADL */
84     HEVC_NAL_IDR_N_LP   = 20, /* Instantaneous Decoder Refresh */
85     HEVC_NAL_CRA        = 21, /* Clean Random Access */
86     /* 22 to 31 reserved */
87     HEVC_NAL_IRAP_VCL22 = 22, /* Intra Random Access Point */
88     HEVC_NAL_IRAP_VCL23 = 23,
89     /* Non VCL NAL*/
90     HEVC_NAL_VPS        = 32,
91     HEVC_NAL_SPS        = 33,
92     HEVC_NAL_PPS        = 34,
93     HEVC_NAL_AUD        = 35, /* Access unit delimiter */
94     HEVC_NAL_EOS        = 36, /* End of sequence */
95     HEVC_NAL_EOB        = 37, /* End of bitstream */
96     HEVC_NAL_FD         = 38, /* Filler data*/
97     HEVC_NAL_PREF_SEI   = 39, /* Prefix SEI */
98     HEVC_NAL_SUFF_SEI   = 40, /* Suffix SEI */
99     /* 41 to 47 reserved */
100     HEVC_NAL_RSV_NVCL41 = 41, /* Reserved Non VCL */
101     HEVC_NAL_RSV_NVCL44 = 44,
102     HEVC_NAL_RSV_NVCL45 = 45,
103     HEVC_NAL_RSV_NVCL47 = 47,
104     HEVC_NAL_UNSPEC48   = 48, /* Unspecified (custom) */
105     HEVC_NAL_UNSPEC55   = 55,
106     HEVC_NAL_UNSPEC56   = 56,
107     HEVC_NAL_UNSPEC63   = 63,
108     HEVC_NAL_UNKNOWN
109 };
110 
111 enum hevc_slice_type_e
112 {
113     HEVC_SLICE_TYPE_B = 0,
114     HEVC_SLICE_TYPE_P,
115     HEVC_SLICE_TYPE_I
116 };
117 
118 #define HEVC_MIN_HVCC_SIZE 23
119 
120 /* checks if data is an HEVCDecoderConfigurationRecord */
hevc_ishvcC(const uint8_t * p_buf,size_t i_buf)121 static inline bool hevc_ishvcC( const uint8_t *p_buf, size_t i_buf )
122 {
123     return ( i_buf >= HEVC_MIN_HVCC_SIZE &&
124              p_buf[0] != 0x00
125 /*          /!\Broken quicktime streams does not respect reserved bits
126             (p_buf[13] & 0xF0) == 0xF0 && // Match all reserved bits
127             (p_buf[15] & 0xFC) == 0xFC &&
128             (p_buf[16] & 0xFC) == 0xFC &&
129             (p_buf[17] & 0xF8) == 0xF8 &&
130             (p_buf[18] & 0xF8) == 0xF8 &&
131             (p_buf[21] & 0x03) != 0x02 */
132            );
133 }
134 
hevc_getNALLengthSize(const uint8_t * p_hvcC)135 static inline uint8_t hevc_getNALLengthSize( const uint8_t *p_hvcC )
136 {
137     return (p_hvcC[21] & 0x03) + 1;
138 }
139 
hevc_getNALType(const uint8_t * p_buf)140 static inline uint8_t hevc_getNALType( const uint8_t *p_buf )
141 {
142     return ((p_buf[0] & 0x7E) >> 1);
143 }
144 
hevc_getNALLayer(const uint8_t * p_buf)145 static inline uint8_t hevc_getNALLayer( const uint8_t *p_buf )
146 {
147     return ((p_buf[0] & 0x01) << 6) | (p_buf[1] >> 3);
148 }
149 
150 /* NAL decoding */
151 typedef struct hevc_video_parameter_set_t hevc_video_parameter_set_t;
152 typedef struct hevc_sequence_parameter_set_t hevc_sequence_parameter_set_t;
153 typedef struct hevc_picture_parameter_set_t hevc_picture_parameter_set_t;
154 typedef struct hevc_slice_segment_header_t hevc_slice_segment_header_t;
155 
156 /* Decodes from three bytes emulation prevented or rbsp stream */
157 hevc_video_parameter_set_t *    hevc_decode_vps( const uint8_t *, size_t, bool );
158 hevc_sequence_parameter_set_t * hevc_decode_sps( const uint8_t *, size_t, bool );
159 hevc_picture_parameter_set_t *  hevc_decode_pps( const uint8_t *, size_t, bool );
160 
161 typedef void(*pf_get_matchedxps)(uint8_t i_pps_id, void *priv,
162                                  hevc_picture_parameter_set_t **,
163                                  hevc_sequence_parameter_set_t **,
164                                  hevc_video_parameter_set_t **);
165 hevc_slice_segment_header_t *   hevc_decode_slice_header( const uint8_t *, size_t, bool,
166                                                           pf_get_matchedxps, void *priv );
167 
168 void hevc_rbsp_release_vps( hevc_video_parameter_set_t * );
169 void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t * );
170 void hevc_rbsp_release_pps( hevc_picture_parameter_set_t * );
171 void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t * );
172 
173 /* set specific */
174 uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t * );
175 uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t * );
176 uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t * );
177 
178 bool hevc_get_xps_id(const uint8_t *p_nalbuf, size_t i_nalbuf, uint8_t *pi_id);
179 bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t *,
180                                       uint8_t *pi_profile, uint8_t *pi_level );
181 bool hevc_get_picture_size( const hevc_sequence_parameter_set_t *, unsigned *p_w, unsigned *p_h,
182                             unsigned *p_vw, unsigned *p_vh );
183 bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t *,
184                           const hevc_video_parameter_set_t * /* can be NULL */,
185                           unsigned *pi_num, unsigned *pi_den );
186 bool hevc_get_aspect_ratio( const hevc_sequence_parameter_set_t *,
187                             unsigned *pi_num, unsigned *pi_den );
188 bool hevc_get_chroma_luma( const hevc_sequence_parameter_set_t *, uint8_t *pi_chroma_format,
189                            uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma );
190 bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t *p_sps,
191                            video_color_primaries_t *p_primaries,
192                            video_transfer_func_t *p_transfer,
193                            video_color_space_t *p_colorspace,
194                            bool *p_full_range );
195 uint8_t hevc_get_max_num_reorder( const hevc_video_parameter_set_t *p_vps );
196 bool hevc_get_slice_type( const hevc_slice_segment_header_t *, enum hevc_slice_type_e * );
197 
198 /* Get level and Profile from DecoderConfigurationRecord */
199 bool hevc_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
200                             uint8_t *pi_level, uint8_t *pi_nal_length_size);
201 
202 /*
203  * HEVCDecoderConfigurationRecord related
204  */
205 struct hevc_dcr_values
206 {
207     uint8_t general_configuration[12];
208     uint8_t i_numTemporalLayer;
209     uint8_t i_chroma_idc;
210     uint8_t i_bit_depth_luma_minus8;
211     uint8_t i_bit_depth_chroma_minus8;
212     bool b_temporalIdNested;
213 };
214 
215 #define HEVC_DCR_VPS_COUNT (HEVC_VPS_ID_MAX + 1)
216 #define HEVC_DCR_SPS_COUNT (HEVC_SPS_ID_MAX + 1)
217 #define HEVC_DCR_PPS_COUNT (HEVC_PPS_ID_MAX + 1)
218 #define HEVC_DCR_SEI_COUNT (16)
219 
220 struct hevc_dcr_params
221 {
222     const uint8_t *p_vps[HEVC_DCR_VPS_COUNT],
223                   *p_sps[HEVC_DCR_SPS_COUNT],
224                   *p_pps[HEVC_DCR_VPS_COUNT],
225                   *p_seipref[HEVC_DCR_SEI_COUNT],
226                   *p_seisuff[HEVC_DCR_SEI_COUNT];
227     uint8_t rgi_vps[HEVC_DCR_VPS_COUNT],
228             rgi_sps[HEVC_DCR_SPS_COUNT],
229             rgi_pps[HEVC_DCR_PPS_COUNT],
230             rgi_seipref[HEVC_DCR_SEI_COUNT],
231             rgi_seisuff[HEVC_DCR_SEI_COUNT];
232     uint8_t i_vps_count, i_sps_count, i_pps_count;
233     uint8_t i_seipref_count, i_seisuff_count;
234     struct hevc_dcr_values *p_values;
235 };
236 
237 uint8_t * hevc_create_dcr( const struct hevc_dcr_params *p_params,
238                            uint8_t i_nal_length_size,
239                            bool b_completeness, size_t *pi_size );
240 
241 /* Converts HEVCDecoderConfigurationRecord to Annex B format */
242 uint8_t * hevc_hvcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
243                                    size_t *pi_res, uint8_t *pi_nal_length_size );
244 
245 /*
246  * POC computing
247  */
248 typedef struct
249 {
250     struct
251     {
252         int lsb;
253         int msb;
254     } prevTid0PicOrderCnt;
255 
256     bool HandleCraAsBlaFlag;
257     bool first_picture; /* Must be set on start or on NAL_EOS */
258 } hevc_poc_ctx_t;
259 
hevc_poc_cxt_init(hevc_poc_ctx_t * p_ctx)260 static inline void hevc_poc_cxt_init( hevc_poc_ctx_t *p_ctx )
261 {
262     p_ctx->prevTid0PicOrderCnt.lsb = 0;
263     p_ctx->prevTid0PicOrderCnt.msb = 0;
264     p_ctx->first_picture = true;
265 }
266 
267 int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t *p_sps,
268                                        const hevc_slice_segment_header_t *slice,
269                                        hevc_poc_ctx_t *ctx );
270 
271 typedef struct hevc_sei_pic_timing_t hevc_sei_pic_timing_t;
272 
273 hevc_sei_pic_timing_t * hevc_decode_sei_pic_timing( bs_t *,
274                                                     const hevc_sequence_parameter_set_t * );
275 void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t * );
276 
277 uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t *,
278                                const hevc_sei_pic_timing_t * /* can be NULL */ );
279 bool hevc_frame_is_progressive( const hevc_sequence_parameter_set_t *,
280                                 const hevc_sei_pic_timing_t * /* can be NULL */);
281 
282 #endif /* HEVC_NAL_H */
283