for #310, refine the aac profile for adts and aac object for RTMP sequence header.
正在显示
10 个修改的文件
包含
141 行增加
和
130 行删除
| @@ -60,7 +60,7 @@ SrsStatisticStream::SrsStatisticStream() | @@ -60,7 +60,7 @@ SrsStatisticStream::SrsStatisticStream() | ||
| 60 | acodec = SrsCodecAudioReserved1; | 60 | acodec = SrsCodecAudioReserved1; |
| 61 | asample_rate = SrsCodecAudioSampleRateReserved; | 61 | asample_rate = SrsCodecAudioSampleRateReserved; |
| 62 | asound_type = SrsCodecAudioSoundTypeReserved; | 62 | asound_type = SrsCodecAudioSoundTypeReserved; |
| 63 | - aac_profile = 0; | 63 | + aac_profile = SrsAacProfileReserved; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | SrsStatisticStream::~SrsStatisticStream() | 66 | SrsStatisticStream::~SrsStatisticStream() |
| @@ -128,7 +128,7 @@ int SrsStatistic::on_video_info(SrsRequest* req, | @@ -128,7 +128,7 @@ int SrsStatistic::on_video_info(SrsRequest* req, | ||
| 128 | 128 | ||
| 129 | int SrsStatistic::on_audio_info(SrsRequest* req, | 129 | int SrsStatistic::on_audio_info(SrsRequest* req, |
| 130 | SrsCodecAudio acodec, SrsCodecAudioSampleRate asample_rate, SrsCodecAudioSoundType asound_type, | 130 | SrsCodecAudio acodec, SrsCodecAudioSampleRate asample_rate, SrsCodecAudioSoundType asound_type, |
| 131 | - u_int8_t aac_profile | 131 | + SrsAacProfile aac_profile |
| 132 | ) { | 132 | ) { |
| 133 | int ret = ERROR_SUCCESS; | 133 | int ret = ERROR_SUCCESS; |
| 134 | 134 |
| @@ -73,7 +73,7 @@ public: | @@ -73,7 +73,7 @@ public: | ||
| 73 | * 1.5.1.1 Audio object type definition, page 23, | 73 | * 1.5.1.1 Audio object type definition, page 23, |
| 74 | * in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf. | 74 | * in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf. |
| 75 | */ | 75 | */ |
| 76 | - u_int8_t aac_profile; | 76 | + SrsAacProfile aac_profile; |
| 77 | public: | 77 | public: |
| 78 | SrsStatisticStream(); | 78 | SrsStatisticStream(); |
| 79 | virtual ~SrsStatisticStream(); | 79 | virtual ~SrsStatisticStream(); |
| @@ -120,7 +120,7 @@ public: | @@ -120,7 +120,7 @@ public: | ||
| 120 | */ | 120 | */ |
| 121 | virtual int on_audio_info(SrsRequest* req, | 121 | virtual int on_audio_info(SrsRequest* req, |
| 122 | SrsCodecAudio acodec, SrsCodecAudioSampleRate asample_rate, SrsCodecAudioSoundType asound_type, | 122 | SrsCodecAudio acodec, SrsCodecAudioSampleRate asample_rate, SrsCodecAudioSoundType asound_type, |
| 123 | - u_int8_t aac_profile | 123 | + SrsAacProfile aac_profile |
| 124 | ); | 124 | ); |
| 125 | /** | 125 | /** |
| 126 | * when close stream. | 126 | * when close stream. |
| @@ -43,6 +43,7 @@ SrsAacEncoder::SrsAacEncoder() | @@ -43,6 +43,7 @@ SrsAacEncoder::SrsAacEncoder() | ||
| 43 | _fs = NULL; | 43 | _fs = NULL; |
| 44 | got_sequence_header = false; | 44 | got_sequence_header = false; |
| 45 | tag_stream = new SrsStream(); | 45 | tag_stream = new SrsStream(); |
| 46 | + aac_profile = SrsAacProfileReserved; | ||
| 46 | } | 47 | } |
| 47 | 48 | ||
| 48 | SrsAacEncoder::~SrsAacEncoder() | 49 | SrsAacEncoder::~SrsAacEncoder() |
| @@ -114,7 +115,7 @@ int SrsAacEncoder::write_audio(int64_t timestamp, char* data, int size) | @@ -114,7 +115,7 @@ int SrsAacEncoder::write_audio(int64_t timestamp, char* data, int size) | ||
| 114 | // 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33. | 115 | // 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33. |
| 115 | // | 116 | // |
| 116 | // only need to decode the first 2bytes: | 117 | // only need to decode the first 2bytes: |
| 117 | - // audioObjectType, aac_profile, 5bits. | 118 | + // audioObjectType, 5bits. |
| 118 | // samplingFrequencyIndex, aac_sample_rate, 4bits. | 119 | // samplingFrequencyIndex, aac_sample_rate, 4bits. |
| 119 | // channelConfiguration, aac_channels, 4bits | 120 | // channelConfiguration, aac_channels, 4bits |
| 120 | if (!stream->require(2)) { | 121 | if (!stream->require(2)) { |
| @@ -123,12 +124,14 @@ int SrsAacEncoder::write_audio(int64_t timestamp, char* data, int size) | @@ -123,12 +124,14 @@ int SrsAacEncoder::write_audio(int64_t timestamp, char* data, int size) | ||
| 123 | return ret; | 124 | return ret; |
| 124 | } | 125 | } |
| 125 | 126 | ||
| 126 | - aac_profile = stream->read_1bytes(); | 127 | + int8_t audioObjectType = stream->read_1bytes(); |
| 127 | aac_sample_rate = stream->read_1bytes(); | 128 | aac_sample_rate = stream->read_1bytes(); |
| 128 | 129 | ||
| 129 | aac_channels = (aac_sample_rate >> 3) & 0x0f; | 130 | aac_channels = (aac_sample_rate >> 3) & 0x0f; |
| 130 | - aac_sample_rate = ((aac_profile << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01); | ||
| 131 | - aac_profile = (aac_profile >> 3) & 0x1f; | 131 | + aac_sample_rate = ((audioObjectType << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01); |
| 132 | + | ||
| 133 | + audioObjectType = (audioObjectType >> 3) & 0x1f; | ||
| 134 | + aac_profile = srs_codec_aac_rtmp2ts((SrsAacObjectType)audioObjectType); | ||
| 132 | 135 | ||
| 133 | got_sequence_header = true; | 136 | got_sequence_header = true; |
| 134 | 137 | ||
| @@ -177,16 +180,13 @@ int SrsAacEncoder::write_audio(int64_t timestamp, char* data, int size) | @@ -177,16 +180,13 @@ int SrsAacEncoder::write_audio(int64_t timestamp, char* data, int size) | ||
| 177 | // protection_absent 1 bslbf | 180 | // protection_absent 1 bslbf |
| 178 | *pp++ = 0xf1; | 181 | *pp++ = 0xf1; |
| 179 | 182 | ||
| 180 | - // Profile_ObjectType 2 uimsbf | 183 | + // profile 2 uimsbf |
| 181 | // sampling_frequency_index 4 uimsbf | 184 | // sampling_frequency_index 4 uimsbf |
| 182 | // private_bit 1 bslbf | 185 | // private_bit 1 bslbf |
| 183 | // channel_configuration 3 uimsbf | 186 | // channel_configuration 3 uimsbf |
| 184 | // original/copy 1 bslbf | 187 | // original/copy 1 bslbf |
| 185 | // home 1 bslbf | 188 | // home 1 bslbf |
| 186 | - int8_t fh_Profile_ObjectType = aac_profile - 1; | ||
| 187 | - *pp++ = ((fh_Profile_ObjectType << 6) & 0xc0) | ((aac_sample_rate << 2) & 0x3c) | ((aac_channels >> 2) & 0x01); | ||
| 188 | - // @remark, Emphasis is removed, | ||
| 189 | - // @see https://github.com/winlinvip/simple-rtmp-server/issues/212#issuecomment-64154736 | 189 | + *pp++ = ((aac_profile << 6) & 0xc0) | ((aac_sample_rate << 2) & 0x3c) | ((aac_channels >> 2) & 0x01); |
| 190 | // 4bits left. | 190 | // 4bits left. |
| 191 | // adts_variable_header(), 1.A.2.2.2 Variable Header of ADTS | 191 | // adts_variable_header(), 1.A.2.2.2 Variable Header of ADTS |
| 192 | // copyright_identification_bit 1 bslbf | 192 | // copyright_identification_bit 1 bslbf |
| @@ -31,6 +31,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -31,6 +31,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 31 | 31 | ||
| 32 | #include <string> | 32 | #include <string> |
| 33 | 33 | ||
| 34 | +#include <srs_kernel_codec.hpp> | ||
| 35 | + | ||
| 34 | class SrsStream; | 36 | class SrsStream; |
| 35 | class SrsFileWriter; | 37 | class SrsFileWriter; |
| 36 | class SrsFileReader; | 38 | class SrsFileReader; |
| @@ -43,7 +45,7 @@ class SrsAacEncoder | @@ -43,7 +45,7 @@ class SrsAacEncoder | ||
| 43 | private: | 45 | private: |
| 44 | SrsFileWriter* _fs; | 46 | SrsFileWriter* _fs; |
| 45 | private: | 47 | private: |
| 46 | - int8_t aac_profile; | 48 | + SrsAacProfile aac_profile; |
| 47 | int8_t aac_sample_rate; | 49 | int8_t aac_sample_rate; |
| 48 | int8_t aac_channels; | 50 | int8_t aac_channels; |
| 49 | bool got_sequence_header; | 51 | bool got_sequence_header; |
| @@ -77,16 +77,36 @@ string srs_codec_audio2str(SrsCodecAudio codec) | @@ -77,16 +77,36 @@ string srs_codec_audio2str(SrsCodecAudio codec) | ||
| 77 | } | 77 | } |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | -string srs_codec_aac_profile2str(u_int8_t aac_profile) | 80 | +string srs_codec_aac_profile2str(SrsAacProfile aac_profile) |
| 81 | { | 81 | { |
| 82 | switch (aac_profile) { | 82 | switch (aac_profile) { |
| 83 | - case 1: return "Main"; | ||
| 84 | - case 2: return "LC"; | ||
| 85 | - case 3: return "SSR"; | 83 | + case SrsAacProfileMain: return "Main"; |
| 84 | + case SrsAacProfileLC: return "LC"; | ||
| 85 | + case SrsAacProfileSSR: return "SSR"; | ||
| 86 | default: return "Other"; | 86 | default: return "Other"; |
| 87 | } | 87 | } |
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | +SrsAacObjectType srs_codec_aac_ts2rtmp(SrsAacProfile profile) | ||
| 91 | +{ | ||
| 92 | + switch (profile) { | ||
| 93 | + case SrsAacProfileMain: return SrsAacObjectTypeAacMain; | ||
| 94 | + case SrsAacProfileLC: return SrsAacObjectTypeAacLC; | ||
| 95 | + case SrsAacProfileSSR: return SrsAacObjectTypeAacSSR; | ||
| 96 | + default: return SrsAacObjectTypeReserved; | ||
| 97 | + } | ||
| 98 | +} | ||
| 99 | + | ||
| 100 | +SrsAacProfile srs_codec_aac_rtmp2ts(SrsAacObjectType object_type) | ||
| 101 | +{ | ||
| 102 | + switch (object_type) { | ||
| 103 | + case SrsAacObjectTypeAacMain: return SrsAacProfileMain; | ||
| 104 | + case SrsAacObjectTypeAacLC: return SrsAacProfileLC; | ||
| 105 | + case SrsAacObjectTypeAacSSR: return SrsAacProfileSSR; | ||
| 106 | + default: return SrsAacProfileReserved; | ||
| 107 | + } | ||
| 108 | +} | ||
| 109 | + | ||
| 90 | /** | 110 | /** |
| 91 | * the public data, event HLS disable, others can use it. | 111 | * the public data, event HLS disable, others can use it. |
| 92 | */ | 112 | */ |
| @@ -260,7 +280,7 @@ SrsAvcAacCodec::SrsAvcAacCodec() | @@ -260,7 +280,7 @@ SrsAvcAacCodec::SrsAvcAacCodec() | ||
| 260 | 280 | ||
| 261 | avc_profile = 0; | 281 | avc_profile = 0; |
| 262 | avc_level = 0; | 282 | avc_level = 0; |
| 263 | - aac_profile = 0; | 283 | + aac_profile = SrsAacProfileReserved; |
| 264 | aac_sample_rate = __SRS_AAC_SAMPLE_RATE_UNSET; // sample rate ignored | 284 | aac_sample_rate = __SRS_AAC_SAMPLE_RATE_UNSET; // sample rate ignored |
| 265 | aac_channels = 0; | 285 | aac_channels = 0; |
| 266 | avc_extra_size = 0; | 286 | avc_extra_size = 0; |
| @@ -458,20 +478,9 @@ int SrsAvcAacCodec::audio_aac_sequence_header_demux(char* data, int size) | @@ -458,20 +478,9 @@ int SrsAvcAacCodec::audio_aac_sequence_header_demux(char* data, int size) | ||
| 458 | // set the aac sample rate. | 478 | // set the aac sample rate. |
| 459 | aac_sample_rate = samplingFrequencyIndex; | 479 | aac_sample_rate = samplingFrequencyIndex; |
| 460 | 480 | ||
| 461 | - // the profile = object_id + 1 | ||
| 462 | - // @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78, | ||
| 463 | - // Table 1. A.9 C MPEG-2 Audio profiles and MPEG-4 Audio object types | ||
| 464 | - aac_profile = profile_ObjectType + 1; | ||
| 465 | - | ||
| 466 | - // the valid aac profile: | ||
| 467 | - // MPEG-2 profile | ||
| 468 | - // Main profile (ID == 1) | ||
| 469 | - // Low Complexity profile (LC) (ID == 2) | ||
| 470 | - // Scalable Sampling Rate profile (SSR) (ID == 3) | ||
| 471 | - // (reserved) (ID == 4) | ||
| 472 | - // @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78, | ||
| 473 | - // Table 1. A.9 C MPEG-2 Audio profiles and MPEG-4 Audio object types | ||
| 474 | - if (aac_profile > 4) { | 481 | + // convert the object type in sequence header to aac profile of ADTS. |
| 482 | + aac_profile = srs_codec_aac_rtmp2ts((SrsAacObjectType)profile_ObjectType); | ||
| 483 | + if (aac_profile == SrsAacProfileReserved) { | ||
| 475 | ret = ERROR_HLS_DECODE_ERROR; | 484 | ret = ERROR_HLS_DECODE_ERROR; |
| 476 | srs_error("audio codec decode aac sequence header failed, " | 485 | srs_error("audio codec decode aac sequence header failed, " |
| 477 | "adts object=%d invalid. ret=%d", profile_ObjectType, ret); | 486 | "adts object=%d invalid. ret=%d", profile_ObjectType, ret); |
| @@ -375,20 +375,40 @@ enum SrsAvcPayloadFormat | @@ -375,20 +375,40 @@ enum SrsAvcPayloadFormat | ||
| 375 | SrsAvcPayloadFormatIbmf, | 375 | SrsAvcPayloadFormatIbmf, |
| 376 | }; | 376 | }; |
| 377 | 377 | ||
| 378 | -// the profile = object_id + 1 | ||
| 379 | -// @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78, | ||
| 380 | -// Table 1. A.9 C MPEG-2 Audio profiles and MPEG-4 Audio object types | ||
| 381 | -// the valid object type: | ||
| 382 | -// AAC Main(ID == 0) | ||
| 383 | -// AAC LC(ID == 1) | ||
| 384 | -// AAC SSR(ID == 2) | ||
| 385 | -// AAC LTP(ID == 3) | ||
| 386 | -// the valid aac profile: | ||
| 387 | -// Main profile (ID == 1) | ||
| 388 | -// Low Complexity profile (LC) (ID == 2) | ||
| 389 | -// Scalable Sampling Rate profile (SSR) (ID == 3) | ||
| 390 | -// (reserved) (ID == 4) | ||
| 391 | -std::string srs_codec_aac_profile2str(u_int8_t aac_profile); | 378 | +/** |
| 379 | +* the aac profile, for ADTS(HLS/TS) | ||
| 380 | +* @see https://github.com/winlinvip/simple-rtmp-server/issues/310 | ||
| 381 | +*/ | ||
| 382 | +enum SrsAacProfile | ||
| 383 | +{ | ||
| 384 | + SrsAacProfileReserved = 3, | ||
| 385 | + | ||
| 386 | + // @see 7.1 Profiles, aac-iso-13818-7.pdf, page 40 | ||
| 387 | + SrsAacProfileMain = 0, | ||
| 388 | + SrsAacProfileLC = 1, | ||
| 389 | + SrsAacProfileSSR = 2, | ||
| 390 | +}; | ||
| 391 | +std::string srs_codec_aac_profile2str(SrsAacProfile aac_profile); | ||
| 392 | + | ||
| 393 | +/** | ||
| 394 | +* the aac object type, for RTMP sequence header | ||
| 395 | +* for AudioSpecificConfig, @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33 | ||
| 396 | +* for audioObjectType, @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 23 | ||
| 397 | +*/ | ||
| 398 | +enum SrsAacObjectType | ||
| 399 | +{ | ||
| 400 | + SrsAacObjectTypeReserved = 0, | ||
| 401 | + | ||
| 402 | + // Table 1.1 – Audio Object Type definition | ||
| 403 | + // @see @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 23 | ||
| 404 | + SrsAacObjectTypeAacMain = 1, | ||
| 405 | + SrsAacObjectTypeAacLC = 2, | ||
| 406 | + SrsAacObjectTypeAacSSR = 3, | ||
| 407 | +}; | ||
| 408 | +// ts/hls/adts audio header profile to RTMP sequence header object type. | ||
| 409 | +SrsAacObjectType srs_codec_aac_ts2rtmp(SrsAacProfile profile); | ||
| 410 | +// RTMP sequence header object type to ts/hls/adts audio header profile. | ||
| 411 | +SrsAacProfile srs_codec_aac_rtmp2ts(SrsAacObjectType object_type); | ||
| 392 | 412 | ||
| 393 | /** | 413 | /** |
| 394 | * the h264/avc and aac codec, for media stream. | 414 | * the h264/avc and aac codec, for media stream. |
| @@ -446,7 +466,7 @@ public: | @@ -446,7 +466,7 @@ public: | ||
| 446 | * 1.5.1.1 Audio object type definition, page 23, | 466 | * 1.5.1.1 Audio object type definition, page 23, |
| 447 | * in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf. | 467 | * in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf. |
| 448 | */ | 468 | */ |
| 449 | - u_int8_t aac_profile; | 469 | + SrsAacProfile aac_profile; |
| 450 | /** | 470 | /** |
| 451 | * samplingFrequencyIndex | 471 | * samplingFrequencyIndex |
| 452 | */ | 472 | */ |
| @@ -214,6 +214,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -214,6 +214,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 214 | #define ERROR_HTTP_JSON_REQUIRED 3052 | 214 | #define ERROR_HTTP_JSON_REQUIRED 3052 |
| 215 | #define ERROR_HTTP_DVR_CREATE_REQUEST 3053 | 215 | #define ERROR_HTTP_DVR_CREATE_REQUEST 3053 |
| 216 | #define ERROR_HTTP_DVR_NO_TAEGET 3054 | 216 | #define ERROR_HTTP_DVR_NO_TAEGET 3054 |
| 217 | +#define ERROR_ADTS_ID_NOT_AAC 3055 | ||
| 217 | 218 | ||
| 218 | /////////////////////////////////////////////////////// | 219 | /////////////////////////////////////////////////////// |
| 219 | // HTTP/StreamCaster protocol error. | 220 | // HTTP/StreamCaster protocol error. |
| @@ -2764,20 +2764,6 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample) | @@ -2764,20 +2764,6 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample) | ||
| 2764 | srs_error("invalid aac frame length=%d, ret=%d", size, ret); | 2764 | srs_error("invalid aac frame length=%d, ret=%d", size, ret); |
| 2765 | return ret; | 2765 | return ret; |
| 2766 | } | 2766 | } |
| 2767 | - | ||
| 2768 | - // the profile = object_id + 1 | ||
| 2769 | - // @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78, | ||
| 2770 | - // Table 1. A.9 C MPEG-2 Audio profiles and MPEG-4 Audio object types | ||
| 2771 | - // the valid object type: | ||
| 2772 | - // AAC Main(ID == 0) | ||
| 2773 | - // AAC LC(ID == 1) | ||
| 2774 | - // AAC SSR(ID == 2) | ||
| 2775 | - // AAC LTP(ID == 3) | ||
| 2776 | - u_int8_t profile_ObjectType = codec->aac_profile - 1; | ||
| 2777 | - | ||
| 2778 | - // TODO: FIXME: only support Main or LC. | ||
| 2779 | - // @see https://github.com/winlinvip/simple-rtmp-server/issues/310 | ||
| 2780 | - profile_ObjectType = srs_min(1, profile_ObjectType); | ||
| 2781 | 2767 | ||
| 2782 | // the frame length is the AAC raw data plus the adts header size. | 2768 | // the frame length is the AAC raw data plus the adts header size. |
| 2783 | int32_t frame_length = size + 7; | 2769 | int32_t frame_length = size + 7; |
| @@ -2811,7 +2797,7 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample) | @@ -2811,7 +2797,7 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample) | ||
| 2811 | int8_t number_of_raw_data_blocks_in_frame; //2bits, 0 indicating 1 raw_data_block() | 2797 | int8_t number_of_raw_data_blocks_in_frame; //2bits, 0 indicating 1 raw_data_block() |
| 2812 | */ | 2798 | */ |
| 2813 | // profile, 2bits | 2799 | // profile, 2bits |
| 2814 | - adts_header[2] = (profile_ObjectType << 6) & 0xc0; | 2800 | + adts_header[2] = (codec->aac_profile << 6) & 0xc0; |
| 2815 | // sampling_frequency_index 4bits | 2801 | // sampling_frequency_index 4bits |
| 2816 | adts_header[2] |= (codec->aac_sample_rate << 2) & 0x3c; | 2802 | adts_header[2] |= (codec->aac_sample_rate << 2) & 0x3c; |
| 2817 | // channel_configuration 3bits | 2803 | // channel_configuration 3bits |
| @@ -329,8 +329,8 @@ int SrsRawAacStream::adts_demux(SrsStream* stream, char** pframe, int* pnb_frame | @@ -329,8 +329,8 @@ int SrsRawAacStream::adts_demux(SrsStream* stream, char** pframe, int* pnb_frame | ||
| 329 | int adts_header_start = stream->pos(); | 329 | int adts_header_start = stream->pos(); |
| 330 | 330 | ||
| 331 | // decode the ADTS. | 331 | // decode the ADTS. |
| 332 | - // @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 75, | ||
| 333 | - // 1.A.2.2 Audio_Data_Transport_Stream frame, ADTS | 332 | + // @see aac-iso-13818-7.pdf, page 26 |
| 333 | + // 6.2 Audio Data Transport Stream, ADTS | ||
| 334 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/212#issuecomment-64145885 | 334 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/212#issuecomment-64145885 |
| 335 | // byte_alignment() | 335 | // byte_alignment() |
| 336 | 336 | ||
| @@ -356,55 +356,63 @@ int SrsRawAacStream::adts_demux(SrsStream* stream, char** pframe, int* pnb_frame | @@ -356,55 +356,63 @@ int SrsRawAacStream::adts_demux(SrsStream* stream, char** pframe, int* pnb_frame | ||
| 356 | return ERROR_AAC_REQUIRED_ADTS; | 356 | return ERROR_AAC_REQUIRED_ADTS; |
| 357 | } | 357 | } |
| 358 | 358 | ||
| 359 | - // Syncword 12 bslbf | 359 | + // syncword 12 bslbf |
| 360 | stream->read_1bytes(); | 360 | stream->read_1bytes(); |
| 361 | // 4bits left. | 361 | // 4bits left. |
| 362 | // adts_fixed_header(), 1.A.2.2.1 Fixed Header of ADTS | 362 | // adts_fixed_header(), 1.A.2.2.1 Fixed Header of ADTS |
| 363 | // ID 1 bslbf | 363 | // ID 1 bslbf |
| 364 | - // Layer 2 uimsbf | 364 | + // layer 2 uimsbf |
| 365 | // protection_absent 1 bslbf | 365 | // protection_absent 1 bslbf |
| 366 | - int8_t fh0 = (stream->read_1bytes() & 0x0f); | ||
| 367 | - /*int8_t fh_id = (fh0 >> 3) & 0x01;*/ | ||
| 368 | - /*int8_t fh_layer = (fh0 >> 1) & 0x03;*/ | ||
| 369 | - int8_t fh_protection_absent = fh0 & 0x01; | 366 | + int8_t pav = (stream->read_1bytes() & 0x0f); |
| 367 | + int8_t id = (pav >> 3) & 0x01; | ||
| 368 | + /*int8_t layer = (pav >> 1) & 0x03;*/ | ||
| 369 | + int8_t protection_absent = pav & 0x01; | ||
| 370 | 370 | ||
| 371 | - int16_t fh1 = stream->read_2bytes(); | ||
| 372 | - // Profile_ObjectType 2 uimsbf | 371 | + /** |
| 372 | + * ID: MPEG identifier, set to ‘1’ if the audio data in the ADTS stream are MPEG-2 AAC (See ISO/IEC 13818-7) | ||
| 373 | + * and set to ‘0’ if the audio data are MPEG-4. See also ISO/IEC 11172-3, subclause 2.4.2.3. | ||
| 374 | + */ | ||
| 375 | + if (id != 0x01) { | ||
| 376 | + ret = ERROR_ADTS_ID_NOT_AAC; | ||
| 377 | + srs_warn("adts: id must be 1(aac), actual 0(mp4a). ret=%d", ret); | ||
| 378 | + return ret; | ||
| 379 | + } | ||
| 380 | + | ||
| 381 | + int16_t sfiv = stream->read_2bytes(); | ||
| 382 | + // profile 2 uimsbf | ||
| 373 | // sampling_frequency_index 4 uimsbf | 383 | // sampling_frequency_index 4 uimsbf |
| 374 | // private_bit 1 bslbf | 384 | // private_bit 1 bslbf |
| 375 | // channel_configuration 3 uimsbf | 385 | // channel_configuration 3 uimsbf |
| 376 | // original/copy 1 bslbf | 386 | // original/copy 1 bslbf |
| 377 | // home 1 bslbf | 387 | // home 1 bslbf |
| 378 | - int8_t audioObjectType = (fh1 >> 14) & 0x03; | ||
| 379 | - int8_t samplingFrequencyIndex = (fh1 >> 10) & 0x0f; | ||
| 380 | - /*int8_t fh_private_bit = (fh1 >> 9) & 0x01;*/ | ||
| 381 | - int8_t channelConfiguration = (fh1 >> 6) & 0x07; | ||
| 382 | - /*int8_t fh_original = (fh1 >> 5) & 0x01;*/ | ||
| 383 | - /*int8_t fh_home = (fh1 >> 4) & 0x01;*/ | ||
| 384 | - // @remark, Emphasis is removed, | ||
| 385 | - // @see https://github.com/winlinvip/simple-rtmp-server/issues/212#issuecomment-64154736 | ||
| 386 | - //int8_t fh_Emphasis = (fh1 >> 2) & 0x03; | 388 | + int8_t profile = (sfiv >> 14) & 0x03; |
| 389 | + int8_t sampling_frequency_index = (sfiv >> 10) & 0x0f; | ||
| 390 | + /*int8_t private_bit = (sfiv >> 9) & 0x01;*/ | ||
| 391 | + int8_t channel_configuration = (sfiv >> 6) & 0x07; | ||
| 392 | + /*int8_t original = (sfiv >> 5) & 0x01;*/ | ||
| 393 | + /*int8_t home = (sfiv >> 4) & 0x01;*/ | ||
| 394 | + //int8_t Emphasis; @remark, Emphasis is removed, @see https://github.com/winlinvip/simple-rtmp-server/issues/212#issuecomment-64154736 | ||
| 387 | // 4bits left. | 395 | // 4bits left. |
| 388 | // adts_variable_header(), 1.A.2.2.2 Variable Header of ADTS | 396 | // adts_variable_header(), 1.A.2.2.2 Variable Header of ADTS |
| 389 | // copyright_identification_bit 1 bslbf | 397 | // copyright_identification_bit 1 bslbf |
| 390 | // copyright_identification_start 1 bslbf | 398 | // copyright_identification_start 1 bslbf |
| 391 | /*int8_t fh_copyright_identification_bit = (fh1 >> 3) & 0x01;*/ | 399 | /*int8_t fh_copyright_identification_bit = (fh1 >> 3) & 0x01;*/ |
| 392 | /*int8_t fh_copyright_identification_start = (fh1 >> 2) & 0x01;*/ | 400 | /*int8_t fh_copyright_identification_start = (fh1 >> 2) & 0x01;*/ |
| 393 | - // aac_frame_length 13 bslbf: Length of the frame including headers and error_check in bytes. | 401 | + // frame_length 13 bslbf: Length of the frame including headers and error_check in bytes. |
| 394 | // use the left 2bits as the 13 and 12 bit, | 402 | // use the left 2bits as the 13 and 12 bit, |
| 395 | - // the aac_frame_length is 13bits, so we move 13-2=11. | ||
| 396 | - int16_t fh_aac_frame_length = (fh1 << 11) & 0x1800; | 403 | + // the frame_length is 13bits, so we move 13-2=11. |
| 404 | + int16_t frame_length = (sfiv << 11) & 0x1800; | ||
| 397 | 405 | ||
| 398 | - int32_t fh2 = stream->read_3bytes(); | ||
| 399 | - // aac_frame_length 13 bslbf: consume the first 13-2=11bits | 406 | + int32_t abfv = stream->read_3bytes(); |
| 407 | + // frame_length 13 bslbf: consume the first 13-2=11bits | ||
| 400 | // the fh2 is 24bits, so we move right 24-11=13. | 408 | // the fh2 is 24bits, so we move right 24-11=13. |
| 401 | - fh_aac_frame_length |= (fh2 >> 13) & 0x07ff; | 409 | + frame_length |= (abfv >> 13) & 0x07ff; |
| 402 | // adts_buffer_fullness 11 bslbf | 410 | // adts_buffer_fullness 11 bslbf |
| 403 | - /*int16_t fh_adts_buffer_fullness = (fh2 >> 2) & 0x7ff;*/ | ||
| 404 | - // no_raw_data_blocks_in_frame 2 uimsbf | ||
| 405 | - /*int16_t fh_no_raw_data_blocks_in_frame = fh2 & 0x03;*/ | 411 | + /*int16_t fh_adts_buffer_fullness = (abfv >> 2) & 0x7ff;*/ |
| 412 | + // number_of_raw_data_blocks_in_frame 2 uimsbf | ||
| 413 | + /*int16_t number_of_raw_data_blocks_in_frame = abfv & 0x03;*/ | ||
| 406 | // adts_error_check(), 1.A.2.2.3 Error detection | 414 | // adts_error_check(), 1.A.2.2.3 Error detection |
| 407 | - if (!fh_protection_absent) { | 415 | + if (!protection_absent) { |
| 408 | if (!stream->require(2)) { | 416 | if (!stream->require(2)) { |
| 409 | return ERROR_AAC_ADTS_HEADER; | 417 | return ERROR_AAC_ADTS_HEADER; |
| 410 | } | 418 | } |
| @@ -412,47 +420,38 @@ int SrsRawAacStream::adts_demux(SrsStream* stream, char** pframe, int* pnb_frame | @@ -412,47 +420,38 @@ int SrsRawAacStream::adts_demux(SrsStream* stream, char** pframe, int* pnb_frame | ||
| 412 | /*int16_t crc_check = */stream->read_2bytes(); | 420 | /*int16_t crc_check = */stream->read_2bytes(); |
| 413 | } | 421 | } |
| 414 | 422 | ||
| 415 | - // TODO: check the samplingFrequencyIndex | ||
| 416 | - // TODO: check the channelConfiguration | 423 | + // TODO: check the sampling_frequency_index |
| 424 | + // TODO: check the channel_configuration | ||
| 417 | 425 | ||
| 418 | // raw_data_blocks | 426 | // raw_data_blocks |
| 419 | int adts_header_size = stream->pos() - adts_header_start; | 427 | int adts_header_size = stream->pos() - adts_header_start; |
| 420 | - int raw_data_size = fh_aac_frame_length - adts_header_size; | 428 | + int raw_data_size = frame_length - adts_header_size; |
| 421 | if (!stream->require(raw_data_size)) { | 429 | if (!stream->require(raw_data_size)) { |
| 422 | return ERROR_AAC_ADTS_HEADER; | 430 | return ERROR_AAC_ADTS_HEADER; |
| 423 | } | 431 | } |
| 424 | 432 | ||
| 425 | - // the profile = object_id + 1 | ||
| 426 | - // @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78, | ||
| 427 | - // Table 1. A.9 ¨C MPEG-2 Audio profiles and MPEG-4 Audio object types | ||
| 428 | - char aac_profile = audioObjectType + 1; | ||
| 429 | - | ||
| 430 | // the codec info. | 433 | // the codec info. |
| 431 | - codec.protection_absent = fh_protection_absent; | ||
| 432 | - codec.Profile_ObjectType = audioObjectType; | ||
| 433 | - codec.sampling_frequency_index = samplingFrequencyIndex; | ||
| 434 | - codec.channel_configuration = channelConfiguration; | ||
| 435 | - codec.aac_frame_length = fh_aac_frame_length; | ||
| 436 | - | ||
| 437 | - codec.aac_profile = aac_profile; | ||
| 438 | - codec.aac_samplerate = samplingFrequencyIndex; | ||
| 439 | - codec.aac_channel = channelConfiguration; | 434 | + codec.protection_absent = protection_absent; |
| 435 | + codec.profile = (SrsAacProfile)profile; | ||
| 436 | + codec.sampling_frequency_index = sampling_frequency_index; | ||
| 437 | + codec.channel_configuration = channel_configuration; | ||
| 438 | + codec.frame_length = frame_length; | ||
| 440 | 439 | ||
| 441 | // @see srs_audio_write_raw_frame(). | 440 | // @see srs_audio_write_raw_frame(). |
| 442 | codec.sound_format = 10; // AAC | 441 | codec.sound_format = 10; // AAC |
| 443 | - if (samplingFrequencyIndex <= 0x0c && samplingFrequencyIndex > 0x0a) { | 442 | + if (sampling_frequency_index <= 0x0c && sampling_frequency_index > 0x0a) { |
| 444 | codec.sound_rate = SrsCodecAudioSampleRate5512; | 443 | codec.sound_rate = SrsCodecAudioSampleRate5512; |
| 445 | - } else if (samplingFrequencyIndex <= 0x0a && samplingFrequencyIndex > 0x07) { | 444 | + } else if (sampling_frequency_index <= 0x0a && sampling_frequency_index > 0x07) { |
| 446 | codec.sound_rate = SrsCodecAudioSampleRate11025; | 445 | codec.sound_rate = SrsCodecAudioSampleRate11025; |
| 447 | - } else if (samplingFrequencyIndex <= 0x07 && samplingFrequencyIndex > 0x04) { | 446 | + } else if (sampling_frequency_index <= 0x07 && sampling_frequency_index > 0x04) { |
| 448 | codec.sound_rate = SrsCodecAudioSampleRate22050; | 447 | codec.sound_rate = SrsCodecAudioSampleRate22050; |
| 449 | - } else if (samplingFrequencyIndex <= 0x04) { | 448 | + } else if (sampling_frequency_index <= 0x04) { |
| 450 | codec.sound_rate = SrsCodecAudioSampleRate44100; | 449 | codec.sound_rate = SrsCodecAudioSampleRate44100; |
| 451 | } else { | 450 | } else { |
| 452 | codec.sound_rate = SrsCodecAudioSampleRate44100; | 451 | codec.sound_rate = SrsCodecAudioSampleRate44100; |
| 453 | - srs_warn("adts invalid sample rate for flv, rate=%#x", samplingFrequencyIndex); | 452 | + srs_warn("adts invalid sample rate for flv, rate=%#x", sampling_frequency_index); |
| 454 | } | 453 | } |
| 455 | - codec.sound_size = srs_max(0, srs_min(1, channelConfiguration - 1)); | 454 | + codec.sound_size = srs_max(0, srs_min(1, channel_configuration - 1)); |
| 456 | // TODO: FIXME: finger it out the sound size by adts. | 455 | // TODO: FIXME: finger it out the sound size by adts. |
| 457 | codec.sound_size = 1; // 0(8bits) or 1(16bits). | 456 | codec.sound_size = 1; // 0(8bits) or 1(16bits). |
| 458 | 457 | ||
| @@ -472,16 +471,13 @@ int SrsRawAacStream::mux_sequence_header(SrsRawAacStreamCodec* codec, string& sh | @@ -472,16 +471,13 @@ int SrsRawAacStream::mux_sequence_header(SrsRawAacStreamCodec* codec, string& sh | ||
| 472 | int ret = ERROR_SUCCESS; | 471 | int ret = ERROR_SUCCESS; |
| 473 | 472 | ||
| 474 | // only support aac profile 1-4. | 473 | // only support aac profile 1-4. |
| 475 | - if (codec->aac_profile < 1 || codec->aac_profile > 4) { | 474 | + if (codec->profile == SrsAacProfileReserved) { |
| 476 | return ERROR_AAC_DATA_INVALID; | 475 | return ERROR_AAC_DATA_INVALID; |
| 477 | } | 476 | } |
| 478 | 477 | ||
| 479 | - // the profile = object_id + 1 | ||
| 480 | - // @see aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 78, | ||
| 481 | - // Table 1. A.9 ¨C MPEG-2 Audio profiles and MPEG-4 Audio object types | ||
| 482 | - char profile_ObjectType = codec->aac_profile - 1; | ||
| 483 | - char channelConfiguration = codec->aac_channel; | ||
| 484 | - char samplingFrequencyIndex = codec->aac_samplerate; | 478 | + SrsAacObjectType audioObjectType = srs_codec_aac_ts2rtmp(codec->profile); |
| 479 | + char channelConfiguration = codec->channel_configuration; | ||
| 480 | + char samplingFrequencyIndex = codec->sampling_frequency_index; | ||
| 485 | 481 | ||
| 486 | // override the aac samplerate by user specified. | 482 | // override the aac samplerate by user specified. |
| 487 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/212#issuecomment-64146899 | 483 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/212#issuecomment-64146899 |
| @@ -503,7 +499,7 @@ int SrsRawAacStream::mux_sequence_header(SrsRawAacStreamCodec* codec, string& sh | @@ -503,7 +499,7 @@ int SrsRawAacStream::mux_sequence_header(SrsRawAacStreamCodec* codec, string& sh | ||
| 503 | // AudioSpecificConfig (), page 33 | 499 | // AudioSpecificConfig (), page 33 |
| 504 | // 1.6.2.1 AudioSpecificConfig | 500 | // 1.6.2.1 AudioSpecificConfig |
| 505 | // audioObjectType; 5 bslbf | 501 | // audioObjectType; 5 bslbf |
| 506 | - ch = (profile_ObjectType << 3) & 0xf8; | 502 | + ch = (audioObjectType << 3) & 0xf8; |
| 507 | // 3bits left. | 503 | // 3bits left. |
| 508 | 504 | ||
| 509 | // samplingFrequencyIndex; 4 bslbf | 505 | // samplingFrequencyIndex; 4 bslbf |
| @@ -32,6 +32,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -32,6 +32,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 32 | 32 | ||
| 33 | #include <string> | 33 | #include <string> |
| 34 | 34 | ||
| 35 | +#include <srs_kernel_codec.hpp> | ||
| 36 | + | ||
| 35 | class SrsStream; | 37 | class SrsStream; |
| 36 | 38 | ||
| 37 | /** | 39 | /** |
| @@ -92,15 +94,10 @@ public: | @@ -92,15 +94,10 @@ public: | ||
| 92 | struct SrsRawAacStreamCodec | 94 | struct SrsRawAacStreamCodec |
| 93 | { | 95 | { |
| 94 | int8_t protection_absent; | 96 | int8_t protection_absent; |
| 95 | - int8_t Profile_ObjectType; | 97 | + SrsAacProfile profile; |
| 96 | int8_t sampling_frequency_index; | 98 | int8_t sampling_frequency_index; |
| 97 | int8_t channel_configuration; | 99 | int8_t channel_configuration; |
| 98 | - int16_t aac_frame_length; | ||
| 99 | - | ||
| 100 | - // calc by Profile_ObjectType+1 | ||
| 101 | - char aac_profile; | ||
| 102 | - char aac_samplerate; | ||
| 103 | - char aac_channel; | 100 | + int16_t frame_length; |
| 104 | 101 | ||
| 105 | char sound_format; | 102 | char sound_format; |
| 106 | char sound_rate; | 103 | char sound_rate; |
-
请 注册 或 登录 后发表评论