hevc_nal.h 9.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Copyright © 2010-2014 VideoLAN
 *
 * Authors: Thomas Guillem <thomas.guillem@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#ifndef HEVC_NAL_H
# define HEVC_NAL_H

23
# include <vlc_es.h>
24
# include <vlc_bits.h>
25

26 27 28
#define HEVC_VPS_ID_MAX 15
#define HEVC_SPS_ID_MAX 15
#define HEVC_PPS_ID_MAX 63
29

30 31 32 33 34 35 36 37 38
enum hevc_general_profile_idc_e
{
    HEVC_PROFILE_NONE               = 0,
    HEVC_PROFILE_MAIN               = 1,
    HEVC_PROFILE_MAIN_10            = 2,
    HEVC_PROFILE_MAIN_STILL_PICTURE = 3,
    HEVC_PROFILE_REXT               = 4, /* range extensions */
};

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

/* Values built from 9 bits mapping of the A-2 bitstream indications for conformance */
#define HEVC_EXT_PROFILE_MONOCHROME                 0x1F9
#define HEVC_EXT_PROFILE_MONOCHROME_12              0x139
#define HEVC_EXT_PROFILE_MONOCHROME_16              0x039
#define HEVC_EXT_PROFILE_MAIN_12                    0x131
#define HEVC_EXT_PROFILE_MAIN_422_10                0x1A1
#define HEVC_EXT_PROFILE_MAIN_422_12                0x121
#define HEVC_EXT_PROFILE_MAIN_444                   0x1C1
#define HEVC_EXT_PROFILE_MAIN_444_10                0x181
#define HEVC_EXT_PROFILE_MAIN_444_12                0x101
#define HEVC_EXT_PROFILE_MAIN_INTRA                 0x1F4 /* From this one, lowest bit is insignifiant */
#define HEVC_EXT_PROFILE_MAIN_10_INTRA              0x1B4
#define HEVC_EXT_PROFILE_MAIN_12_INTRA              0x134
#define HEVC_EXT_PROFILE_MAIN_422_10_INTRA          0x1A4
#define HEVC_EXT_PROFILE_MAIN_422_12_INTRA          0x124
#define HEVC_EXT_PROFILE_MAIN_444_INTRA             0x1C4
#define HEVC_EXT_PROFILE_MAIN_444_10_INTRA          0x184
#define HEVC_EXT_PROFILE_MAIN_444_12_INTRA          0x104
#define HEVC_EXT_PROFILE_MAIN_444_16_INTRA          0x004
#define HEVC_EXT_PROFILE_MAIN_444_STILL_PICTURE     0x1C6
#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 64
enum hevc_nal_unit_type_e
{
65 66 67 68 69 70 71 72 73 74
    HEVC_NAL_TRAIL_N    = 0, /* Trailing */
    HEVC_NAL_TRAIL_R    = 1, /* Trailing Reference */
    HEVC_NAL_TSA_N      = 2, /* Temporal Sublayer Access */
    HEVC_NAL_TSA_R      = 3, /* Temporal Sublayer Access Reference */
    HEVC_NAL_STSA_N     = 4, /* Stepwise Temporal Sublayer Access */
    HEVC_NAL_STSA_R     = 5, /* Stepwise Temporal Sublayer Access Reference */
    HEVC_NAL_RADL_N     = 6, /* Random Access Decodable Leading (display order) */
    HEVC_NAL_RADL_R     = 7, /* Random Access Decodable Leading (display order) Reference */
    HEVC_NAL_RASL_N     = 8, /* Random Access Skipped Leading (display order) */
    HEVC_NAL_RASL_R     = 9, /* Random Access Skipped Leading (display order) Reference */
75
    /* 10 to 15 reserved */
76 77 78
    HEVC_NAL_RSV_VCL_N10= 10,
    HEVC_NAL_RSV_VCL_N12= 12,
    HEVC_NAL_RSV_VCL_N14= 14,
79
    /* Key frames */
80 81 82 83 84 85
    HEVC_NAL_BLA_W_LP   = 16, /* Broken Link Access with Associated RASL */
    HEVC_NAL_BLA_W_RADL = 17, /* Broken Link Access with Associated RADL */
    HEVC_NAL_BLA_N_LP   = 18, /* Broken Link Access */
    HEVC_NAL_IDR_W_RADL = 19, /* Instantaneous Decoder Refresh with Associated RADL */
    HEVC_NAL_IDR_N_LP   = 20, /* Instantaneous Decoder Refresh */
    HEVC_NAL_CRA        = 21, /* Clean Random Access */
86
    /* 22 to 31 reserved */
87 88
    HEVC_NAL_IRAP_VCL22 = 22, /* Intra Random Access Point */
    HEVC_NAL_IRAP_VCL23 = 23,
89 90 91 92 93 94 95 96 97 98
    /* Non VCL NAL*/
    HEVC_NAL_VPS        = 32,
    HEVC_NAL_SPS        = 33,
    HEVC_NAL_PPS        = 34,
    HEVC_NAL_AUD        = 35, /* Access unit delimiter */
    HEVC_NAL_EOS        = 36, /* End of sequence */
    HEVC_NAL_EOB        = 37, /* End of bitstream */
    HEVC_NAL_FD         = 38, /* Filler data*/
    HEVC_NAL_PREF_SEI   = 39, /* Prefix SEI */
    HEVC_NAL_SUFF_SEI   = 40, /* Suffix SEI */
99 100 101 102 103 104 105 106 107
    /* 41 to 47 reserved */
    HEVC_NAL_RSV_NVCL41 = 41, /* Reserved Non VCL */
    HEVC_NAL_RSV_NVCL44 = 44,
    HEVC_NAL_RSV_NVCL45 = 45,
    HEVC_NAL_RSV_NVCL47 = 47,
    HEVC_NAL_UNSPEC48   = 48, /* Unspecified (custom) */
    HEVC_NAL_UNSPEC55   = 55,
    HEVC_NAL_UNSPEC56   = 56,
    HEVC_NAL_UNSPEC63   = 63,
108 109 110
    HEVC_NAL_UNKNOWN
};

111 112 113 114 115 116 117
enum hevc_slice_type_e
{
    HEVC_SLICE_TYPE_B = 0,
    HEVC_SLICE_TYPE_P,
    HEVC_SLICE_TYPE_I
};

118
#define HEVC_MIN_HVCC_SIZE 23
119 120 121 122 123

/* checks if data is an HEVCDecoderConfigurationRecord */
static inline bool hevc_ishvcC( const uint8_t *p_buf, size_t i_buf )
{
    return ( i_buf >= HEVC_MIN_HVCC_SIZE &&
124
             p_buf[0] != 0x00
125 126
/*          /!\Broken quicktime streams does not respect reserved bits
            (p_buf[13] & 0xF0) == 0xF0 && // Match all reserved bits
127 128 129 130
            (p_buf[15] & 0xFC) == 0xFC &&
            (p_buf[16] & 0xFC) == 0xFC &&
            (p_buf[17] & 0xF8) == 0xF8 &&
            (p_buf[18] & 0xF8) == 0xF8 &&
131
            (p_buf[21] & 0x03) != 0x02 */
132 133 134
           );
}

135 136 137 138 139 140 141 142 143 144
static inline uint8_t hevc_getNALLengthSize( const uint8_t *p_hvcC )
{
    return (p_hvcC[21] & 0x03) + 1;
}

static inline uint8_t hevc_getNALType( const uint8_t *p_buf )
{
    return ((p_buf[0] & 0x7E) >> 1);
}

145 146 147 148 149
static inline uint8_t hevc_getNALLayer( const uint8_t *p_buf )
{
    return ((p_buf[0] & 0x01) << 6) | (p_buf[1] >> 3);
}

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 157 158 159
/* Decodes from three bytes emulation prevented or rbsp stream */
hevc_video_parameter_set_t *    hevc_decode_vps( const uint8_t *, size_t, bool );
hevc_sequence_parameter_set_t * hevc_decode_sps( const uint8_t *, size_t, bool );
hevc_picture_parameter_set_t *  hevc_decode_pps( const uint8_t *, size_t, bool );
160 161 162 163 164

typedef void(*pf_get_matchedxps)(uint8_t i_pps_id, void *priv,
                                 hevc_picture_parameter_set_t **,
                                 hevc_sequence_parameter_set_t **,
                                 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 174 175
/* set specific */
uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t * );
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
/* Converts HEVCDecoderConfigurationRecord to Annex B format */
179 180
uint8_t * hevc_hvcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
                                   size_t *pi_res, uint8_t *pi_nal_length_size );
181

182
bool hevc_get_xps_id(const uint8_t *p_buf, size_t i_buf, uint8_t *pi_id);
183 184
bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t *,
                                      uint8_t *pi_profile, uint8_t *pi_level );
185 186
bool hevc_get_picture_size( const hevc_sequence_parameter_set_t *, unsigned *p_w, unsigned *p_h,
                            unsigned *p_vw, unsigned *p_vh );
187
bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t *,
François Cartegnie's avatar
François Cartegnie committed
188
                          hevc_video_parameter_set_t ** /* HEVC_MAX_VPS || NULL */,
189
                          unsigned *pi_num, unsigned *pi_den );
190 191 192 193 194
bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t *p_sps,
                           video_color_primaries_t *p_primaries,
                           video_transfer_func_t *p_transfer,
                           video_color_space_t *p_colorspace,
                           bool *p_full_range );
195
bool hevc_get_slice_type( const hevc_slice_segment_header_t *, enum hevc_slice_type_e * );
196

197 198 199 200
/* Get level and Profile from DecoderConfigurationRecord */
bool hevc_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
                            uint8_t *pi_level, uint8_t *pi_nal_length_size);

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
typedef struct
{
    struct
    {
        int lsb;
        int msb;
    } prevPicOrderCnt, prevTid0PicOrderCnt;

    bool first_picture; /* Must be set on start or on NAL_EOS */
} hevc_poc_ctx_t;

int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t *p_sps,
                                       const hevc_slice_segment_header_t *slice,
                                       hevc_poc_ctx_t *ctx );

216 217 218 219 220 221 222 223 224
typedef struct hevc_sei_pic_timing_t hevc_sei_pic_timing_t;

hevc_sei_pic_timing_t * hevc_decode_sei_pic_timing( bs_t *,
                                                    const hevc_sequence_parameter_set_t * );
void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t * );

uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t *,
                               const hevc_sei_pic_timing_t * /* can be NULL */ );

225
#endif /* HEVC_NAL_H */