Skip to content Skip to sidebar Skip to footer

How To Initialize Mediaformat To Configure A Mediacodec To Decode Raw Aac Data?

I have a strange issue with my StreamPlayer and I need any help I can get. The main goal I need to achieve is StreamPlayer which is able to play back MPEG-2 Transport Streams with

Solution 1:

I really don't know why, but it turns out that initializing the "csd-0" ByteBuffer this way

mediaFormat.setByteBuffer("csd-0", ByteBuffer.allocate(2).put(newbyte[]{(byte) 0x11, (byte)0x90}));

doesn't work, but initializing it this way

byte[] bytes = newbyte[]{(byte) 0x11, (byte)0x90};
ByteBuffer bb = ByteBuffer.wrap(bytes);
mediaFormat.setByteBuffer("csd-0", bb);

does.

BTW, comparing these two byteBuffers using

bb1.equals(bb2);

returns true.

Very strange!

Solution 2:

Values in csd-0 depends on ADTS header.

ADTS header length is up to 9 bytes. To generate csd-0 you need the second and the third byte of header.

int profile = (header[2] & 0xC0) >> 6;
int srate = (header[2] & 0x3C) >> 2;
int channel = ((header[2] & 0x01) << 2) | ((header.[3] & 0xC0) >> 6)

ByteBuffer csd = ByteBuffer.allocate(2);
csd.put(0, (byte)( ((profile + 1) << 3) | srate >> 1 ) );
csd.put(1, (byte)( ((srate << 7) & 0x80) | channel << 3 ) );

Now you got valid csd-0 for this aac audio stream.

Solution 3:

In the failing case u may need to call ByteBuffer's rewind method first. If u look carefully u'll see the position is different between the MediaExtractor and the TSExtractor:

csd-0=java.nio.ByteArrayBuffer[position=0,limit=2,capacity=2]

vs

csd-0=java.nio.ByteArrayBuffer[position=2,limit=2,capacity=2]

ByteBuffer's equals only compares the bytes after position until a mismatch; in ur case one buffer is already positioned at the end hence no mismatch.

Solution 4:

Thanks for the above code for calculating CSD. Unfortunately this was not working for me. My decoder was failing with the above csd setting. Finally I found the issue. According to the documentation first "5 bits " of CSD is the object type ( Profile). Above code profile is added to only 4 bits. So changing the code as below works fine for me

  int profile = (header[2] & 0xC0) >> 6;
  int srate = (header[2] & 0x3C) >> 2;
  int channel = ((header[2] & 0x01) << 2) | ((header.[3] & 0xC0) >> 6)

  ByteBuffer csd = ByteBuffer.allocate(2);
  csd.put(0, (byte)(profile << 3 | srate >> 1));
  csd.put(1, (byte)((srate & 0x01) << 7 | channel << 3)); 

Post a Comment for "How To Initialize Mediaformat To Configure A Mediacodec To Decode Raw Aac Data?"