Blame view

trunk/src/core/srs_core_codec.hpp 8.3 KB
winlin authored
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
/*
The MIT License (MIT)

Copyright (c) 2013 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_CORE_CODEC_HPP
#define SRS_CORE_CODEC_HPP

/*
#include <srs_core_codec.hpp>
*/

#include <srs_core.hpp>
33 34
#define SRS_MAX_CODEC_SAMPLE 128
35 36 37 38 39 40 41 42 43 44 45 46 47
class SrsStream;

// E.4.3.1 VIDEODATA
// CodecID UB [4]
// Codec Identifier. The following values are defined:
// 	2 = Sorenson H.263
// 	3 = Screen video
// 	4 = On2 VP6
// 	5 = On2 VP6 with alpha channel
// 	6 = Screen video version 2
// 	7 = AVC
enum SrsCodecVideo
{
winlin authored
48 49
	SrsCodecVideoReserved				= 0,
	
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	SrsCodecVideoSorensonH263 			= 2,
	SrsCodecVideoScreenVideo 			= 3,
	SrsCodecVideoOn2VP6 				= 4,
	SrsCodecVideoOn2VP6WithAlphaChannel = 5,
	SrsCodecVideoScreenVideoVersion2 	= 6,
	SrsCodecVideoAVC 					= 7,
};

// E.4.3.1 VIDEODATA
// Frame Type UB [4]
// Type of video frame. The following values are defined:
// 	1 = key frame (for AVC, a seekable frame)
// 	2 = inter frame (for AVC, a non-seekable frame)
// 	3 = disposable inter frame (H.263 only)
// 	4 = generated key frame (reserved for server use only)
// 	5 = video info/command frame
enum SrsCodecVideoAVCFrame
{
winlin authored
68 69
	SrsCodecVideoAVCFrameReserved					= 0,
	
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	SrsCodecVideoAVCFrameKeyFrame 					= 1,
	SrsCodecVideoAVCFrameInterFrame 				= 2,
	SrsCodecVideoAVCFrameDisposableInterFrame 		= 3,
	SrsCodecVideoAVCFrameGeneratedKeyFrame			= 4,
	SrsCodecVideoAVCFrameVideoInfoFrame				= 5,
};

// AVCPacketType IF CodecID == 7 UI8
// The following values are defined:
// 	0 = AVC sequence header
// 	1 = AVC NALU
// 	2 = AVC end of sequence (lower level NALU sequence ender is
// 		not required or supported)
enum SrsCodecVideoAVCType
{
winlin authored
85 86
	SrsCodecVideoAVCTypeReserved					= -1,
	
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	SrsCodecVideoAVCTypeSequenceHeader 				= 0,
	SrsCodecVideoAVCTypeNALU 						= 1,
	SrsCodecVideoAVCTypeSequenceHeaderEOF 			= 2,
};

// SoundFormat UB [4] 
// Format of SoundData. The following values are defined:
// 	0 = Linear PCM, platform endian
// 	1 = ADPCM
// 	2 = MP3
// 	3 = Linear PCM, little endian
// 	4 = Nellymoser 16 kHz mono
// 	5 = Nellymoser 8 kHz mono
// 	6 = Nellymoser
// 	7 = G.711 A-law logarithmic PCM
// 	8 = G.711 mu-law logarithmic PCM
// 	9 = reserved
// 	10 = AAC
// 	11 = Speex
// 	14 = MP3 8 kHz
// 	15 = Device-specific sound
// Formats 7, 8, 14, and 15 are reserved.
// AAC is supported in Flash Player 9,0,115,0 and higher.
// Speex is supported in Flash Player 10 and higher.
enum SrsCodecAudio
{
	SrsCodecAudioLinearPCMPlatformEndian 			= 0,
	SrsCodecAudioADPCM 								= 1,
	SrsCodecAudioMP3 								= 2,
	SrsCodecAudioLinearPCMLittleEndian 				= 3,
	SrsCodecAudioNellymoser16kHzMono 				= 4,
	SrsCodecAudioNellymoser8kHzMono 				= 5,
	SrsCodecAudioNellymoser 						= 6,
	SrsCodecAudioReservedG711AlawLogarithmicPCM		= 7,
	SrsCodecAudioReservedG711MuLawLogarithmicPCM	= 8,
	SrsCodecAudioReserved 							= 9,
	SrsCodecAudioAAC 								= 10,
	SrsCodecAudioSpeex 								= 11,
	SrsCodecAudioReservedMP3_8kHz 					= 14,
	SrsCodecAudioReservedDeviceSpecificSound 		= 15,
};

// AACPacketType IF SoundFormat == 10 UI8
// The following values are defined:
// 	0 = AAC sequence header
// 	1 = AAC raw
enum SrsCodecAudioType
{
winlin authored
135
	SrsCodecAudioTypeReserved						= -1,
136 137 138 139
	SrsCodecAudioTypeSequenceHeader 				= 0,
	SrsCodecAudioTypeRawData 						= 1,
};
140 141 142 143 144 145 146
// 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
enum SrsCodecAudioSampleRate
{
winlin authored
147 148 149 150 151 152
	SrsCodecAudioSampleRateReserved					= -1,
	
	SrsCodecAudioSampleRate5512 					= 0,
	SrsCodecAudioSampleRate11025 					= 1,
	SrsCodecAudioSampleRate22050 					= 2,
	SrsCodecAudioSampleRate44100 					= 3,
153 154 155 156 157 158 159 160 161
};

// 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
enum SrsCodecAudioSampleSize
{
winlin authored
162 163 164 165
	SrsCodecAudioSampleSizeReserved					= -1,
	
	SrsCodecAudioSampleSize8bit 					= 0,
	SrsCodecAudioSampleSize16bit 					= 1,
166 167 168 169 170 171 172
};

// Mono or stereo sound
// 0 = Mono sound
// 1 = Stereo sound
enum SrsCodecAudioSoundType
{
winlin authored
173 174 175 176
	SrsCodecAudioSoundTypeReserved					= -1, 
	
	SrsCodecAudioSoundTypeMono 						= 0,
	SrsCodecAudioSoundTypeStereo 					= 1,
177 178
};
winlin authored
179
/**
180 181 182 183
* buffer indicates the position and size.
*/
struct SrsCodecBuffer
{
winlin authored
184 185 186
	/**
	* @remark user must manage the bytes.
	*/
187 188
	int size;
	char* bytes;
winlin authored
189 190 191 192 193 194 195 196 197 198
	
	SrsCodecBuffer();
	void append(void* data, int len);
	
	/**
	* free the bytes, 
	* user can invoke it to free the bytes,
	* the SrsCodecBuffer never free automatically.
	*/
	void free();
199 200 201 202 203 204 205 206 207 208
};

/**
* the samples in the flv audio/video packet.
*/
class SrsCodecSample
{
public:
	int nb_buffers;
	SrsCodecBuffer buffers[SRS_MAX_CODEC_SAMPLE];
winlin authored
209 210
public:
	bool is_video;
winlin authored
211 212 213
	// video specified
	SrsCodecVideoAVCFrame frame_type;
	SrsCodecVideoAVCType avc_packet_type;
214 215 216
	// CompositionTime, video_file_format_spec_v10_1.pdf, page 78.
	// cts = pts - dts, where dts = flvheader->timestamp.
	int32_t cts;
winlin authored
217 218 219 220 221
	// audio specified
	SrsCodecAudioSampleRate sound_rate;
	SrsCodecAudioSampleSize sound_size;
	SrsCodecAudioSoundType sound_type;
	SrsCodecAudioType aac_packet_type;
222 223 224 225 226 227 228 229
public:
	SrsCodecSample();
	virtual ~SrsCodecSample();
	void clear();
	int add_sample(char* bytes, int size);
};

/**
winlin authored
230 231 232 233
* Annex E. The FLV File Format
*/
class SrsCodec
{
234 235 236 237 238 239 240 241
private:
	SrsStream* stream;
public:
	/**
	* video specified
	*/
	// @see: SrsCodecVideo
	int			video_codec_id;
winlin authored
242
	// profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
winlin authored
243
	u_int8_t	avc_profile; 
winlin authored
244
	// level_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
winlin authored
245
	u_int8_t	avc_level; 
246 247 248 249 250
	int			width;
	int			height;
	int			video_data_rate; // in bps
	int			frame_rate;
	int			duration;
winlin authored
251 252
	// lengthSizeMinusOne, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
	int8_t 		NAL_unit_length;
winlin authored
253 254 255 256
	u_int16_t 	sequenceParameterSetLength;
	char* 		sequenceParameterSetNALUnit;
	u_int16_t 	pictureParameterSetLength;
	char* 		pictureParameterSetNALUnit;
257 258 259
	/**
	* audio specified
	*/
260
	// @see: SrsCodecAudioType
261 262
	int			audio_codec_id;
	int			audio_data_rate; // in bps
winlin authored
263 264 265 266 267 268 269
	// 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.
	u_int8_t	aac_profile; 
	// samplingFrequencyIndex
	u_int8_t	aac_sample_rate;
	// channelConfiguration
	u_int8_t	aac_channels;
270
	// the avc extra data, the AVC sequence header,
271 272
	// without the flv codec header,
	// @see: ffmpeg, AVCodecContext::extradata
273 274 275
	int 		avc_extra_size;
	char*		avc_extra_data;
	// the aac extra data, the AAC sequence header,
276 277
	// without the flv codec header,
	// @see: ffmpeg, AVCodecContext::extradata
278 279
	int 		aac_extra_size;
	char*		aac_extra_data;
winlin authored
280 281 282
public:
	SrsCodec();
	virtual ~SrsCodec();
283 284
// the following function used for hls to build the codec info.
public:
285 286
	virtual int audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample);
	virtual int video_avc_demux(int8_t* data, int size, SrsCodecSample* sample);
287
// the following function used to finger out the flv/rtmp packet detail.
winlin authored
288 289 290 291
public:
	/**
	* only check the frame_type, not check the codec type.
	*/
292
	static bool video_is_keyframe(int8_t* data, int size);
winlin authored
293 294 295
	/**
	* check codec h264, keyframe, sequence header
	*/
296
	static bool video_is_sequence_header(int8_t* data, int size);
winlin authored
297 298 299
	/**
	* check codec aac, sequence header
	*/
300
	static bool audio_is_sequence_header(int8_t* data, int size);
winlin authored
301 302 303
	/**
	* check codec h264.
	*/
304
	static bool video_is_h264(int8_t* data, int size);
winlin authored
305 306 307 308
private:
	/**
	* check codec aac.
	*/
309
	static bool audio_is_aac(int8_t* data, int size);
winlin authored
310 311
};
312
#endif