AAC raw code stream can not play directly, generally need to be encapsulated into ADTS format to use again, the blogger in Android with MEDIACODEC encoded AAC is RAW format, In order to save as. AAC format, you need to increase the ADTS header so that it can be played directly via VLC or Windows Media player. Now the online collection of information and code summed up, in preparation for their future reference, but also to share with the needs of colleagues.
Source code from:
Http://stackoverflow.com/questions/18862715/how-to-generate-the-aac-adts-elementary-stream-with-android-mediacodec
Implementation function:/**
* ADD ADTS Header at the beginning and every AAC packet.
* This is needed as MEDIACODEC encoder generates a packet of raw
* AAC data.
*
* The Packetlen must count in the ADTS header itself!!! .
* Note that the Packetlen parameter here is raw AAC Packet Len + 7; 7 bytes Adts Header
**/
private void Addadtstopacket (Byte [] packet, int packetlen) {
int profile = 2; AAC LC,MEDIACODECINFO.CODECPROFILELEVEL.AACOBJECTLC;
int freqidx = 5; 32K, see the following annotation avpriv_mpeg4audio_sample_rates 32000 corresponding array subscript, from the ffmpeg source
int chancfg = 2; See back note channel_configuration,stero two-channel stereo
/*int avpriv_mpeg4audio_sample_rates[] = {
96000, 88200, 64000, 48000, 44100, 32000,
24000, 22050, 16000, 12000, 11025, 8000, 7350
};
Channel_configuration: Indicates the number of channels Chancfg
0:defined in AOT specifc Config
1:1 Channel:front-center
2:2 Channels:front-left, Front-right
3:3 Channels:front-center, Front-left, Front-right
4:4 Channels:front-center, Front-left, Front-right, Back-center
5:5 Channels:front-center, Front-left, Front-right, Back-left, Back-right
6:6 Channels:front-center, Front-left, Front-right, Back-left, Back-right, Lfe-channel
7:8 Channels:front-center, Front-left, Front-right, Side-left, Side-right, Back-left, Back-right, LFE-channel
8-15:reserved
*/
Fill in ADTS data
packet [0] = (byte) 0xFF;
packet [1] = (byte) 0xf9;
packet [2] = (byte) ((profile-1) << 6) + (Freqidx << 2) + (Chancfg >> 2));
packet [3] = (byte) ((Chancfg & 3) << 6) + (Packetlen >> 11));
packet [4] = (byte) ((Packetlen &0x7ff) >> 3);
packet [5] = (byte) ((Packetlen & 7) << 5) + 0x1F);
packet [6] = (byte) 0xFC;
}
The following theory is reproduced from blog: Http://wiki.multimedia.cx/index.php?title=ADTS
Audio Data Transport Stream (ADTS) is a format, used by MPEG TS or Shoutcast to Stream Audio, usually AAC.
Structure
AAAAAAAA AAAABCCD eeffffgh hhijklmm mmmmmmmm mmmooooo oooooopp (QQQQQQQQ qqqqqqqq)
Header consists of 7 or 9 bytes (without or with CRC).
Letter Length (BITS) Description
A Syncword 0xFFF, all bits must 1
B 1 MPEG version:0 for MPEG-4, 1 for MPEG-2
C 2 Layer:always 0
D 1 Protection absent, Warning, set to 1 if there are no CRC and 0 if there is CRC
E 2 profile, the MPEG-4 Audio Object Type minus 1
F 4 MPEG-4 Sampling Frequency Index (is forbidden)
G 1 private stream, set to 0 when encoding, ignore when decoding
H 3 MPEG-4 Channel Configuration (in the case of 0, the Channel Configuration are sent via an Inband
PCE)
I 1 originality, set to 0 then encoding, ignore when decoding
J 1 home, set to 0 when encoding, ignore when decoding
K 1 copyrighted stream, set to 0 when encoding, ignore when decoding
L 1 Copyright Start, set to 0 when encoding, ignore when decoding
M frame length, this value must include 7 or 9 bytes of header Length:framelength =
(Protectionabsent = = 1 7:9) + size (aacframe)
O-One Buffer fullness
P 2 Number of AAC frames (RDBS) in ADTS frame minus 1, for maximum compatibility always use 1 AAC frame
Per ADTS frame
Q CRC If protection absent is 0
Usage in Mpeg-ts
ADTS packet must is a content of PES packet. Pack AAC data inside ADTS frame, than pack inside PES packet, then MUX
by TS Packetizer.
Usage in Shoutcast
ADTS frames goes one by one in TCP stream. Look for Syncword, parse headers and look for next syncword.