h264_nal.h 7.47 KB
Newer Older
1
/*****************************************************************************
2
 * Copyright © 2010-2014 VideoLAN
3 4 5
 *
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
 *
6 7
 * 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
8 9 10 11 12
 * 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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15 16 17 18 19
 *
 * 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.
 *****************************************************************************/
Felix Abecassis's avatar
Felix Abecassis committed
20 21 22
#ifndef H264_NAL_H
# define H264_NAL_H

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

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#define PROFILE_H264_BASELINE             66
#define PROFILE_H264_MAIN                 77
#define PROFILE_H264_EXTENDED             88
#define PROFILE_H264_HIGH                 100
#define PROFILE_H264_HIGH_10              110
#define PROFILE_H264_HIGH_422             122
#define PROFILE_H264_HIGH_444             144
#define PROFILE_H264_HIGH_444_PREDICTIVE  244

#define PROFILE_H264_CAVLC_INTRA          44
#define PROFILE_H264_SVC_BASELINE         83
#define PROFILE_H264_SVC_HIGH             86
#define PROFILE_H264_MVC_STEREO_HIGH      128
#define PROFILE_H264_MVC_MULTIVIEW_HIGH   118

41 42 43 44
#define PROFILE_H264_MFC_HIGH                          134
#define PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH          138
#define PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH 139

45 46
#define H264_SPS_ID_MAX (31)
#define H264_PPS_ID_MAX (255)
47

48
enum h264_nal_unit_type_e
49
{
50 51 52 53 54 55 56 57 58
    H264_NAL_UNKNOWN = 0,
    H264_NAL_SLICE   = 1,
    H264_NAL_SLICE_DPA   = 2,
    H264_NAL_SLICE_DPB   = 3,
    H264_NAL_SLICE_DPC   = 4,
    H264_NAL_SLICE_IDR   = 5,    /* ref_idc != 0 */
    H264_NAL_SEI         = 6,    /* ref_idc == 0 */
    H264_NAL_SPS         = 7,
    H264_NAL_PPS         = 8,
59
    H264_NAL_AU_DELIMITER= 9,
60
    /* ref_idc == 0 for 6,9,10,11,12 */
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    H264_NAL_END_OF_SEQ  = 10,
    H264_NAL_END_OF_STREAM = 11,
    H264_NAL_FILLER_DATA = 12,
    H264_NAL_SPS_EXT     = 13,
    H264_NAL_PREFIX      = 14,
    H264_NAL_SUBSET_SPS  = 15,
    H264_NAL_DEPTH_PS    = 16,
    H264_NAL_RESERVED_17 = 17,
    H264_NAL_RESERVED_18 = 18,
    H264_NAL_SLICE_WP    = 19,
    H264_NAL_SLICE_EXT   = 20,
    H264_NAL_SLICE_3D_EXT= 21,
    H264_NAL_RESERVED_22 = 22,
    H264_NAL_RESERVED_23 = 23,
75 76
};

77 78 79 80 81 82 83 84 85 86
typedef struct h264_sequence_parameter_set_t h264_sequence_parameter_set_t;
typedef struct h264_picture_parameter_set_t h264_picture_parameter_set_t;

h264_sequence_parameter_set_t * h264_decode_sps( const uint8_t *, size_t, bool );
h264_picture_parameter_set_t *  h264_decode_pps( const uint8_t *, size_t, bool );

void h264_release_sps( h264_sequence_parameter_set_t * );
void h264_release_pps( h264_picture_parameter_set_t * );

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 92 93 94
    /* according to avcC, 3 bits max for those */
    uint8_t i_chroma_idc;
    uint8_t i_bit_depth_luma;
    uint8_t i_bit_depth_chroma;
95
    uint8_t b_separate_colour_planes_flag;
96

97 98 99 100 101 102 103 104 105 106
    uint32_t pic_width_in_mbs_minus1;
    uint32_t pic_height_in_map_units_minus1;
    struct
    {
        uint32_t left_offset;
        uint32_t right_offset;
        uint32_t top_offset;
        uint32_t bottom_offset;
    } frame_crop;
    uint8_t frame_mbs_only_flag;
107
    uint8_t mb_adaptive_frame_field_flag;
108 109 110
    int i_log2_max_frame_num;
    int i_pic_order_cnt_type;
    int i_delta_pic_order_always_zero_flag;
111 112 113 114
    int32_t offset_for_non_ref_pic;
    int32_t offset_for_top_to_bottom_field;
    int i_num_ref_frames_in_pic_order_cnt_cycle;
    int32_t offset_for_ref_frame[255];
115 116 117 118 119
    int i_log2_max_pic_order_cnt_lsb;

    struct {
        bool b_valid;
        int i_sar_num, i_sar_den;
120 121 122 123 124 125
        struct {
            bool b_full_range;
            uint8_t i_colour_primaries;
            uint8_t i_transfer_characteristics;
            uint8_t i_matrix_coefficients;
        } colour;
126 127 128 129 130
        bool b_timing_info_present_flag;
        uint32_t i_num_units_in_tick;
        uint32_t i_time_scale;
        bool b_fixed_frame_rate;
        bool b_pic_struct_present_flag;
131
        bool b_hrd_parameters_present_flag; /* CpbDpbDelaysPresentFlag */
132 133
        uint8_t i_cpb_removal_delay_length_minus1;
        uint8_t i_dpb_output_delay_length_minus1;
134 135 136 137

        /* restrictions */
        uint8_t b_bitstream_restriction_flag;
        uint8_t i_max_num_reorder_frames;
138 139 140
    } vui;
};

141
struct h264_picture_parameter_set_t
142
{
143 144
    uint8_t i_id;
    uint8_t i_sps_id;
145 146 147 148
    uint8_t i_pic_order_present_flag;
    uint8_t i_redundant_pic_present_flag;
    uint8_t weighted_pred_flag;
    uint8_t weighted_bipred_idc;
149 150
};

151 152 153 154 155
/*
    AnnexB : [\x00] \x00 \x00 \x01 Prefixed NAL
    AVC Sample format : NalLengthSize encoded size prefixed NAL
    avcC: AVCDecoderConfigurationRecord combining SPS & PPS in AVC Sample Format
*/
156

157 158 159 160
#define H264_MIN_AVCC_SIZE 7

bool h264_isavcC( const uint8_t *, size_t );

161 162 163
/* Convert AVC Sample format to Annex B in-place */
void h264_AVC_to_AnnexB( uint8_t *p_buf, uint32_t i_len,
                         uint8_t i_nal_length_size );
164

165 166 167 168 169 170
/* Get the First SPS/PPS NAL pointers from an Annex B buffer
 * Returns TRUE if a SPS and/or a PPS is found */
bool h264_AnnexB_get_spspps( const uint8_t *p_buf, size_t i_buf,
                             const uint8_t **pp_sps, size_t *p_sps_size,
                             const uint8_t **pp_pps, size_t *p_pps_size,
                             const uint8_t **pp_ext, size_t *p_ext_size );
Thomas Guillem's avatar
Thomas Guillem committed
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 176 177 178
                           const uint8_t **pp_sps_buf,
                           const size_t *p_sps_size, uint8_t i_sps_count,
                           const uint8_t **pp_pps_buf,
                           const size_t *p_pps_size, uint8_t i_pps_count );
179 180

/* Convert AVCDecoderConfigurationRecord SPS/PPS to Annex B format */
181 182
uint8_t * h264_avcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
                                   size_t *pi_result, uint8_t *pi_nal_length_size );
183

184 185 186
bool h264_get_dpb_values( const h264_sequence_parameter_set_t *,
                          uint8_t *pi_depth, unsigned *pi_delay );

187 188
bool h264_get_picture_size( const h264_sequence_parameter_set_t *, unsigned *p_w, unsigned *p_h,
                            unsigned *p_vw, unsigned *p_vh );
189 190
bool h264_get_chroma_luma( const h264_sequence_parameter_set_t *, uint8_t *pi_chroma_format,
                           uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma );
191 192 193 194
bool h264_get_colorimetry( const h264_sequence_parameter_set_t *p_sps,
                           video_color_primaries_t *p_primaries,
                           video_transfer_func_t *p_transfer,
                           video_color_space_t *p_colorspace,
195
                           video_color_range_t *p_full_range );
196

197 198 199
/* Get level and Profile from DecoderConfigurationRecord */
bool h264_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
                            uint8_t *pi_level, uint8_t *p_nal_length_size);
Felix Abecassis's avatar
Felix Abecassis committed
200 201

#endif /* H264_NAL_H */