1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

for #310, refine the aac profile for adts and aac object for RTMP sequence header.

This commit is contained in:
winlin 2015-03-08 17:33:52 +08:00
parent 04f3f2a8f8
commit d3e2838fc6
10 changed files with 141 additions and 130 deletions

View file

@ -43,6 +43,7 @@ SrsAacEncoder::SrsAacEncoder()
_fs = NULL;
got_sequence_header = false;
tag_stream = new SrsStream();
aac_profile = SrsAacProfileReserved;
}
SrsAacEncoder::~SrsAacEncoder()
@ -114,7 +115,7 @@ int SrsAacEncoder::write_audio(int64_t timestamp, char* data, int size)
// 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.
//
// only need to decode the first 2bytes:
// audioObjectType, aac_profile, 5bits.
// audioObjectType, 5bits.
// samplingFrequencyIndex, aac_sample_rate, 4bits.
// channelConfiguration, aac_channels, 4bits
if (!stream->require(2)) {
@ -123,12 +124,14 @@ int SrsAacEncoder::write_audio(int64_t timestamp, char* data, int size)
return ret;
}
aac_profile = stream->read_1bytes();
int8_t audioObjectType = stream->read_1bytes();
aac_sample_rate = stream->read_1bytes();
aac_channels = (aac_sample_rate >> 3) & 0x0f;
aac_sample_rate = ((aac_profile << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01);
aac_profile = (aac_profile >> 3) & 0x1f;
aac_sample_rate = ((audioObjectType << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01);
audioObjectType = (audioObjectType >> 3) & 0x1f;
aac_profile = srs_codec_aac_rtmp2ts((SrsAacObjectType)audioObjectType);
got_sequence_header = true;
@ -177,16 +180,13 @@ int SrsAacEncoder::write_audio(int64_t timestamp, char* data, int size)
// protection_absent 1 bslbf
*pp++ = 0xf1;
// Profile_ObjectType 2 uimsbf
// profile 2 uimsbf
// sampling_frequency_index 4 uimsbf
// private_bit 1 bslbf
// channel_configuration 3 uimsbf
// original/copy 1 bslbf
// home 1 bslbf
int8_t fh_Profile_ObjectType = aac_profile - 1;
*pp++ = ((fh_Profile_ObjectType << 6) & 0xc0) | ((aac_sample_rate << 2) & 0x3c) | ((aac_channels >> 2) & 0x01);
// @remark, Emphasis is removed,
// @see https://github.com/winlinvip/simple-rtmp-server/issues/212#issuecomment-64154736
*pp++ = ((aac_profile << 6) & 0xc0) | ((aac_sample_rate << 2) & 0x3c) | ((aac_channels >> 2) & 0x01);
// 4bits left.
// adts_variable_header(), 1.A.2.2.2 Variable Header of ADTS
// copyright_identification_bit 1 bslbf

View file

@ -31,6 +31,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <string>
#include <srs_kernel_codec.hpp>
class SrsStream;
class SrsFileWriter;
class SrsFileReader;
@ -43,7 +45,7 @@ class SrsAacEncoder
private:
SrsFileWriter* _fs;
private:
int8_t aac_profile;
SrsAacProfile aac_profile;
int8_t aac_sample_rate;
int8_t aac_channels;
bool got_sequence_header;

View file

@ -77,16 +77,36 @@ string srs_codec_audio2str(SrsCodecAudio codec)
}
}
string srs_codec_aac_profile2str(u_int8_t aac_profile)
string srs_codec_aac_profile2str(SrsAacProfile aac_profile)
{
switch (aac_profile) {
case 1: return "Main";
case 2: return "LC";
case 3: return "SSR";
case SrsAacProfileMain: return "Main";
case SrsAacProfileLC: return "LC";
case SrsAacProfileSSR: return "SSR";
default: return "Other";
}
}
SrsAacObjectType srs_codec_aac_ts2rtmp(SrsAacProfile profile)
{
switch (profile) {
case SrsAacProfileMain: return SrsAacObjectTypeAacMain;
case SrsAacProfileLC: return SrsAacObjectTypeAacLC;
case SrsAacProfileSSR: return SrsAacObjectTypeAacSSR;
default: return SrsAacObjectTypeReserved;
}
}
SrsAacProfile srs_codec_aac_rtmp2ts(SrsAacObjectType object_type)
{
switch (object_type) {
case SrsAacObjectTypeAacMain: return SrsAacProfileMain;
case SrsAacObjectTypeAacLC: return SrsAacProfileLC;
case SrsAacObjectTypeAacSSR: return SrsAacProfileSSR;
default: return SrsAacProfileReserved;
}
}
/**
* the public data, event HLS disable, others can use it.
*/
@ -260,7 +280,7 @@ SrsAvcAacCodec::SrsAvcAacCodec()
avc_profile = 0;
avc_level = 0;
aac_profile = 0;
aac_profile = SrsAacProfileReserved;
aac_sample_rate = __SRS_AAC_SAMPLE_RATE_UNSET; // sample rate ignored
aac_channels = 0;
avc_extra_size = 0;
@ -458,20 +478,9 @@ int SrsAvcAacCodec::audio_aac_sequence_header_demux(char* data, int size)
// set the aac sample rate.
aac_sample_rate = samplingFrequencyIndex;
// the profile = object_id + 1
// @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78,
// Table 1. A.9 ¨C MPEG-2 Audio profiles and MPEG-4 Audio object types
aac_profile = profile_ObjectType + 1;
// the valid aac profile:
// MPEG-2 profile
// Main profile (ID == 1)
// Low Complexity profile (LC) (ID == 2)
// Scalable Sampling Rate profile (SSR) (ID == 3)
// (reserved) (ID == 4)
// @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78,
// Table 1. A.9 ¨C MPEG-2 Audio profiles and MPEG-4 Audio object types
if (aac_profile > 4) {
// convert the object type in sequence header to aac profile of ADTS.
aac_profile = srs_codec_aac_rtmp2ts((SrsAacObjectType)profile_ObjectType);
if (aac_profile == SrsAacProfileReserved) {
ret = ERROR_HLS_DECODE_ERROR;
srs_error("audio codec decode aac sequence header failed, "
"adts object=%d invalid. ret=%d", profile_ObjectType, ret);

View file

@ -375,20 +375,40 @@ enum SrsAvcPayloadFormat
SrsAvcPayloadFormatIbmf,
};
// the profile = object_id + 1
// @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78,
// Table 1. A.9 C MPEG-2 Audio profiles and MPEG-4 Audio object types
// the valid object type:
// AAC Main(ID == 0)
// AAC LC(ID == 1)
// AAC SSR(ID == 2)
// AAC LTP(ID == 3)
// the valid aac profile:
// Main profile (ID == 1)
// Low Complexity profile (LC) (ID == 2)
// Scalable Sampling Rate profile (SSR) (ID == 3)
// (reserved) (ID == 4)
std::string srs_codec_aac_profile2str(u_int8_t aac_profile);
/**
* the aac profile, for ADTS(HLS/TS)
* @see https://github.com/winlinvip/simple-rtmp-server/issues/310
*/
enum SrsAacProfile
{
SrsAacProfileReserved = 3,
// @see 7.1 Profiles, aac-iso-13818-7.pdf, page 40
SrsAacProfileMain = 0,
SrsAacProfileLC = 1,
SrsAacProfileSSR = 2,
};
std::string srs_codec_aac_profile2str(SrsAacProfile aac_profile);
/**
* the aac object type, for RTMP sequence header
* for AudioSpecificConfig, @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33
* for audioObjectType, @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 23
*/
enum SrsAacObjectType
{
SrsAacObjectTypeReserved = 0,
// Table 1.1 Audio Object Type definition
// @see @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 23
SrsAacObjectTypeAacMain = 1,
SrsAacObjectTypeAacLC = 2,
SrsAacObjectTypeAacSSR = 3,
};
// ts/hls/adts audio header profile to RTMP sequence header object type.
SrsAacObjectType srs_codec_aac_ts2rtmp(SrsAacProfile profile);
// RTMP sequence header object type to ts/hls/adts audio header profile.
SrsAacProfile srs_codec_aac_rtmp2ts(SrsAacObjectType object_type);
/**
* the h264/avc and aac codec, for media stream.
@ -446,7 +466,7 @@ public:
* 1.5.1.1 Audio object type definition, page 23,
* in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf.
*/
u_int8_t aac_profile;
SrsAacProfile aac_profile;
/**
* samplingFrequencyIndex
*/

View file

@ -214,6 +214,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define ERROR_HTTP_JSON_REQUIRED 3052
#define ERROR_HTTP_DVR_CREATE_REQUEST 3053
#define ERROR_HTTP_DVR_NO_TAEGET 3054
#define ERROR_ADTS_ID_NOT_AAC 3055
///////////////////////////////////////////////////////
// HTTP/StreamCaster protocol error.

View file

@ -2764,20 +2764,6 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample)
srs_error("invalid aac frame length=%d, ret=%d", size, ret);
return ret;
}
// the profile = object_id + 1
// @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78,
// Table 1. A.9 ¨C MPEG-2 Audio profiles and MPEG-4 Audio object types
// the valid object type:
// AAC Main(ID == 0)
// AAC LC(ID == 1)
// AAC SSR(ID == 2)
// AAC LTP(ID == 3)
u_int8_t profile_ObjectType = codec->aac_profile - 1;
// TODO: FIXME: only support Main or LC.
// @see https://github.com/winlinvip/simple-rtmp-server/issues/310
profile_ObjectType = srs_min(1, profile_ObjectType);
// the frame length is the AAC raw data plus the adts header size.
int32_t frame_length = size + 7;
@ -2811,7 +2797,7 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample)
int8_t number_of_raw_data_blocks_in_frame; //2bits, 0 indicating 1 raw_data_block()
*/
// profile, 2bits
adts_header[2] = (profile_ObjectType << 6) & 0xc0;
adts_header[2] = (codec->aac_profile << 6) & 0xc0;
// sampling_frequency_index 4bits
adts_header[2] |= (codec->aac_sample_rate << 2) & 0x3c;
// channel_configuration 3bits