Blame view

trunk/src/app/srs_app_avc_aac.hpp 9.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef SRS_APP_AVC_AAC_HPP
#define SRS_APP_AVC_AAC_HPP

/*
#include <srs_app_avc_aac.hpp>
*/

#include <srs_core.hpp>

#include <srs_kernel_codec.hpp>
winlin authored
35
class SrsStream;
36
class SrsAmf0Object;
winlin authored
37
winlin authored
38 39
#define __SRS_SRS_MAX_CODEC_SAMPLE 128
#define __SRS_AAC_SAMPLE_RATE_UNSET 15
40
41 42 43 44 45 46 47 48
/**
* the FLV/RTMP supported audio sample rate.
* Sampling rate. The following values are defined:
* 0 = 5.5 kHz = 5512 Hz
* 1 = 11 kHz = 11025 Hz
* 2 = 22 kHz = 22050 Hz
* 3 = 44 kHz = 44100 Hz
*/
49 50
enum SrsCodecAudioSampleRate
{
51
    // set to the max value to reserved, for array map.
52
    SrsCodecAudioSampleRateReserved                 = 4,
53 54
    
    SrsCodecAudioSampleRate5512                     = 0,
55 56 57
    SrsCodecAudioSampleRate11025                    = 1,
    SrsCodecAudioSampleRate22050                    = 2,
    SrsCodecAudioSampleRate44100                    = 3,
58 59
};
60 61 62 63 64 65 66 67
/**
* the FLV/RTMP supported audio sample size.
* Size of each audio sample. This parameter only pertains to
* uncompressed formats. Compressed formats always decode
* to 16 bits internally.
* 0 = 8-bit samples
* 1 = 16-bit samples
*/
68 69
enum SrsCodecAudioSampleSize
{
70
    // set to the max value to reserved, for array map.
71
    SrsCodecAudioSampleSizeReserved                 = 2,
72 73
    
    SrsCodecAudioSampleSize8bit                     = 0,
74
    SrsCodecAudioSampleSize16bit                    = 1,
75 76
};
77 78 79 80 81 82
/**
* the FLV/RTMP supported audio sound type/channel.
* Mono or stereo sound
* 0 = Mono sound
* 1 = Stereo sound
*/
83 84
enum SrsCodecAudioSoundType
{
85
    // set to the max value to reserved, for array map.
86
    SrsCodecAudioSoundTypeReserved                  = 2, 
87
    
88 89
    SrsCodecAudioSoundTypeMono                      = 0,
    SrsCodecAudioSoundTypeStereo                    = 1,
90 91 92
};

/**
93 94 95 96
* the codec sample unit.
* for h.264 video packet, a NALU is a sample unit.
* for aac raw audio packet, a NALU is the entire aac raw data.
* for sequence header, it's not a sample unit.
97
*/
98
class SrsCodecSampleUnit
99 100 101
{
public:
    /**
102 103
    * the sample bytes is directly ptr to packet bytes,
    * user should never use it when packet destroyed.
104 105 106
    */
    int size;
    char* bytes;
107
public:
108 109
    SrsCodecSampleUnit();
    virtual ~SrsCodecSampleUnit();
110 111 112 113
};

/**
* the samples in the flv audio/video packet.
114 115 116 117 118 119 120 121 122 123 124
* the sample used to analysis a video/audio packet,
* split the h.264 NALUs to buffers, or aac raw data to a buffer,
* and decode the video/audio specified infos.
* 
* the sample unit:
*       a video packet codec in h.264 contains many NALUs, each is a sample unit.
*       a audio packet codec in aac is a sample unit.
* @remark, the video/audio sequence header is not sample unit,
*       all sequence header stores as extra data, 
*       @see SrsAvcAacCodec.avc_extra_data and SrsAvcAacCodec.aac_extra_data
* @remark, user must clear all samples before decode a new video/audio packet.
125 126 127 128 129 130 131 132 133 134
*/
class SrsCodecSample
{
public:
    /**
    * each audio/video raw data packet will dumps to one or multiple buffers,
    * the buffers will write to hls and clear to reset.
    * generally, aac audio packet corresponding to one buffer,
    * where avc/h264 video packet may contains multiple buffer.
    */
135
    int nb_sample_units;
winlin authored
136
    SrsCodecSampleUnit sample_units[__SRS_SRS_MAX_CODEC_SAMPLE];
137
public:
138 139 140
    /**
    * whether the sample is video sample which demux from video packet.
    */
141
    bool is_video;
142 143 144 145
    /**
    * CompositionTime, video_file_format_spec_v10_1.pdf, page 78.
    * cts = pts - dts, where dts = flvheader->timestamp.
    */
146 147 148 149 150 151 152 153 154 155 156 157 158 159
    int32_t cts;
public:
    // video specified
    SrsCodecVideoAVCFrame frame_type;
    SrsCodecVideoAVCType avc_packet_type;
public:
    // audio specified
    SrsCodecAudioSampleRate sound_rate;
    SrsCodecAudioSampleSize sound_size;
    SrsCodecAudioSoundType sound_type;
    SrsCodecAudioType aac_packet_type;
public:
    SrsCodecSample();
    virtual ~SrsCodecSample();
160 161 162 163 164 165 166 167
public:
    /**
    * clear all samples.
    * the sample units never copy the bytes, it directly use the ptr,
    * so when video/audio packet is destroyed, the sample must be clear.
    * in a word, user must clear sample before demux it.
    * @remark demux sample use SrsAvcAacCodec.audio_aac_demux or video_avc_demux.
    */
168
    void clear();
169 170 171 172 173 174 175
    /**
    * add the a sample unit, it's a h.264 NALU or aac raw data.
    * the sample unit directly use the ptr of packet bytes,
    * so user must never use sample unit when packet is destroyed.
    * in a word, user must clear sample before demux it.
    */
    int add_sample_unit(char* bytes, int size);
176 177 178 179
};

/**
* the h264/avc and aac codec, for media stream.
180 181 182 183 184 185 186 187 188 189 190
*
* to demux the FLV/RTMP video/audio packet to sample,
* add each NALUs of h.264 as a sample unit to sample,
* while the entire aac raw data as a sample unit.
*
* for sequence header,
* demux it and save it in the avc_extra_data and aac_extra_data,
* 
* for the codec info, such as audio sample rate,
* decode from FLV/RTMP header, then use codec info in sequence 
* header to override it.
191 192 193 194 195 196 197
*/
class SrsAvcAacCodec
{
private:
    SrsStream* stream;
public:
    /**
198
    * metadata specified
199
    */
200 201 202 203
    int             duration;
    int             width;
    int             height;
    int             frame_rate;
204
    // @see: SrsCodecVideo
205 206 207 208 209 210 211 212 213
    int             video_codec_id;
    int             video_data_rate; // in bps
    // @see: SrsCod ecAudioType
    int             audio_codec_id;
    int             audio_data_rate; // in bps
public:
    /**
    * video specified
    */
214
    // profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
215
    u_int8_t        avc_profile; 
216
    // level_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
217
    u_int8_t        avc_level; 
218
    // lengthSizeMinusOne, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
219 220 221 222 223
    int8_t          NAL_unit_length;
    u_int16_t       sequenceParameterSetLength;
    char*           sequenceParameterSetNALUnit;
    u_int16_t       pictureParameterSetLength;
    char*           pictureParameterSetNALUnit;
224 225 226
public:
    /**
    * audio specified
227 228
    * 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.
    * audioObjectType, value defines in 7.1 Profiles, aac-iso-13818-7.pdf, page 40.
229
    */
230
    u_int8_t        aac_profile; 
231 232 233
    /**
    * samplingFrequencyIndex
    */
234
    u_int8_t        aac_sample_rate;
235 236 237
    /**
    * channelConfiguration
    */
238
    u_int8_t        aac_channels;
239
public:
240 241 242 243 244
    /**
    * the avc extra data, the AVC sequence header,
    * without the flv codec header,
    * @see: ffmpeg, AVCodecContext::extradata
    */
245 246
    int             avc_extra_size;
    char*           avc_extra_data;
247 248 249 250 251
    /**
    * the aac extra data, the AAC sequence header,
    * without the flv codec header,
    * @see: ffmpeg, AVCodecContext::extradata
    */
252 253
    int             aac_extra_size;
    char*           aac_extra_data;
254 255 256
public:
    SrsAvcAacCodec();
    virtual ~SrsAvcAacCodec();
257
// the following function used for hls to build the sample and codec.
258
public:
259 260 261 262 263 264 265 266 267 268 269 270 271
    /**
    * demux the metadata, to to get the stream info,
    * for instance, the width/height, sample rate.
    * @param metadata, the metadata amf0 object. assert not NULL.
    */
    virtual int metadata_demux(SrsAmf0Object* metadata);
    /**
    * demux the audio packet in aac codec.
    * the packet mux in FLV/RTMP format defined in flv specification.
    * demux the audio speicified data(sound_format, sound_size, ...) to sample.
    * demux the aac specified data(aac_profile, ...) to codec from sequence header.
    * demux the aac raw to sample units.
    */
272
    virtual int audio_aac_demux(char* data, int size, SrsCodecSample* sample);
273 274 275 276 277 278 279
    /**
    * demux the video packet in h.264 codec.
    * the packet mux in FLV/RTMP format defined in flv specification.
    * demux the video specified data(frame_type, codec_id, ...) to sample.
    * demux the h.264 sepcified data(avc_profile, ...) to codec from sequence header.
    * demux the h.264 NALUs to sampe units.
    */
280
    virtual int video_avc_demux(char* data, int size, SrsCodecSample* sample);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
private:
    /**
    * when avc packet type is SrsCodecVideoAVCTypeSequenceHeader,
    * decode the sps and pps.
    */
    virtual int avc_demux_sps_pps(SrsStream* stream);
    /**
    * demux the avc NALU in "AnnexB" 
    * from H.264-AVC-ISO_IEC_14496-10.pdf, page 211.
    */
    virtual int avc_demux_annexb_format(SrsStream* stream, SrsCodecSample* sample);
    /**
    * demux the avc NALU in "ISO Base Media File Format" 
    * from H.264-AVC-ISO_IEC_14496-15.pdf, page 20
    */
    virtual int avc_demux_ibmf_format(SrsStream* stream, SrsCodecSample* sample);
297 298 299
};

#endif
300