m4a vs. mp4 dissection

[email protected]
Newsgroups gmane.comp.ipod.ephpod
Message-ID <Pine.LNX.4.44.0305020712110.21218-100000@server1.builderadius.com>
Forwarded from a bounce:

---------- Forwarded message ----------

Okay, I guess it's time to share my findings on the aac/mp4/m4a mac/pc 
conundrum.

reference URLs:

http://www.doom9.org/Soft21/Encoders/MPEG4/
http://mpeg4ip.sourceforge.net/docs/
http://rarewares.hydrogenaudio.org/aac.php
http://0.t.free.fr/Son/

My goal was to get a custom-encoded AAC file into iTunes, and in the 
format that it expects for transfer to an iPod.

The first thing I found after scouring the web was the Psytel AAC 
encoder.  I extracted a CD track to raw .wav format, and gave the 
encoder the following command line, which is the configuration I would 
like to encode all my AAC tracks in:

aacenc.exe -vbrhi -br 256 -low_ath -qual 9 -profile 0 -c 44000 -if 
<filename> -of encoded.aac

The meaning of my options are as follows:

-vbr = recommended variable bitrate mode
-br = base bitrate for VBR
-c = lowpass filter cutoff in Hz (44000 is way over the top)
-qual 9 = highest quality processing (highest CPU)
-profile 0 = low complexity (for compatibility with hardware AAC devices)
-low_ath = tells encoder to use highest sensitivity threshold of audibility.
-of = output filename

I then used the MPEG-4 toolkit, and ran:

mp4creator60.exe -create=encoded.aac encoded.mp4

to encapsulate the aac stream into an mpeg-4 header and footer.  At 
first I got funky errors from this ("skipped 32768 bytes", etc).  Then I 
upgraded to a more recent version of mp4creator, and it stopped complaining.

At this point, when I run

mp4info.exe endoded.mp4

I get this:

mp4info.exe version 0.9.8
encoded.mp4:
Track   Type    Info
1       audio   MPEG-2 AAC LC, 254.072 secs, 208 kbps, 44100 Hz
2       od      Object Descriptors
3       scene   BIFS

Now there are two things wrong here.
If you take an .m4a file created by ripping a track in iTunes, and run 
mp4info.exe on it, you get:

mp4info.exe version 0.9.8
from_itunes.m4a:
Track   Type    Info
1       audio   MPEG-4, 97.802 secs, 256 kbps, 44100 Hz

The first thing that's wrong is that the mpeg-4 encapsulation on 
"encoded.mp4" has too many items in it.  So let's delete tracks 2 and 3.

mp4creator60.exe -delete=2 encoded.mp4
mp4creator60.exe -delete=3 encoded.mp4

Easy enough.  Now we get:

mp4info.exe version 0.9.8
encoded.mp4:
Track   Type    Info
1       audio   MPEG-2 AAC LC, 254.072 secs, 208 kbps, 44100 Hz

Now the second problem comes to light.  Apparently the Psytel AAC 
encoder, when given the parameters I like, creates an MPEG-2 stream
instead of an MPEG-4 stream.  I don't know what I'm doing or not doing 
that causes it to create MPEG-2 data instead of MPEG-4.  Perhaps it's 
just mislabeling it's stream.  But anyway, this problem made me switch 
to the FAAC encoder.

So I started again, with faac.  This program apparently doesn't do VBR 
encoding.  I'm not enough of an expert to know why.  But I can make a 
command line that's halfway like what I'm after:

faac.exe -m 4 -o LC -n -c 22050 -q 200 <input wave> encoded.aac

Options:
   -m X   AAC MPEG version, X can be 2 or 4.
   -o X   AAC object type, X can be LC, MAIN or
   -n     Don't use mid/side coding.
   -r     RAW AAC output file.
   -c <bandwidth>        Set the bandwidth in H
   -q <quality>  Set quantizer quality.

This renders a bitrate right around 256kbps in file encoded.aac.
When I encapsulate it with the April 17th build of mp4creator.exe, and 
delete all those extra tracks, I get something that, according to 
mp4info.exe, is packaged exactly like iTunes' track:

encoded.mp4:
1       audio   MPEG-4, 254.072 secs, 256 kbps, 44100 Hz

from_itunes.m4a:
1       audio   MPEG-4, 97.802 secs, 256 kbps, 44100 Hz

Okay, so.  I copy this over to the mac, and open it up.
Quicktime opens it up, and plays it.  All well and good.  When I drop it 
into iTunes, however, it calls it a Quicktime movie file, and won't let 
it onto the iPod.

iTunes expects AAC files to have a creator of "hook" and a file type of 
"M4A ".  If I change the file to this, and put it in iTunes, it suddenly 
refuses to recognize the file at all.  Interesting.

After some reading, I discover this:

The MPEG-4 file format is based very heavily on the Quicktime file 
format.  An mp4 file can contain a great many 'atoms', which can have 
various and sundry data embedded in them.  The most important of these 
is the "moov" atom, which helps to describe the contents of the rest of 
the file.  Sometimes this atom is kept in the resource fork of the file, 
but usually nowadays it's "flattened" into the data fork, along with all 
the other atoms.

If you open an apple m4a file in a hex editor, and then an mp4 file 
generated with my above method, you can see the atoms and their data.

Here's a list of the differences and similarities I've noted:

* The "mdat" atom seems to be the bulk of the audio data.  The mp4 
starts right in with the "mdat" atom - it doesn't even wait for an 
"moov" atom.  In the iTunes m4a, the "mdat" atom comes later.

* The "stsz" atom seems to be a table of frame or time indexes, four 
bytes per entry.  The mp4's "stsz" atom is near the end of the file.  In 
the m4a, it's near the beginning.  The good news I guess it that both at 
least HAVE this atom. Heh.

* Both files contain the "moov" atom identifier -- that is, the text 
characters "m" "o" "o" "v" -- followed by three zeros, then the sequence 
"lmvhd", then four zeros.  After that comes 16 bytes I can't decipher, a 
sparse field of zeros spotted by 1's and 2's, and then the next atom. 
(A different one each.)

* The m4a, the iTunes file, starts out with the sequence
"... ftypM4A ....M4A mp42isom...." (all .'s are zeros)
   The mp4, the file made by mp4creator.exe, starts out with the sequence
"...^ftypisom....mp41. " (all .'s are zeros.  The ^ is hex $14, or 20.)
The distinction of note is the "mp42" versus the "mp41".  Are we dealing 
with slightly different file formats here?

-------------------

After a more thorough examination I've realized that the "... " and the 
"...^" that start the above two strings are actually big-endian 
four-byte hex addresses that refer to the length of the subsequent atom, 
including it's four-character code name.  From this I can deduce that 
those above sequences are each _one_atom_, the "ftyp" atom.

So, for the iTunes audio file, the m4a, the "ftyp" atom is
"M4A ....M4A mp42isom...."
While the faac-generated, mp3creator encapsulated audio file, the mp4, 
has an "ftyp" atom containing simply
"isom....mp41"

Here's a table, in order, of all the atoms I can see in both files, plus
the reported length of the data following them (in hex).  I suspect that 
some sequences of tags are meant to be contained within other tags, 
because the reported length in the file is often larger than the data 
leading up to the next tag.

Mind you, these files are NOT encoded from the same raw source.  They're 
from different tracks on different CDs.

mp4creator mp4
----------
ftyp 14
mdat 203F05
moov 3767
mvhd 6C
iods 2A

trak 31D8
tkhd 5C
mdia 3174
mdhd 20
hdlr 21
soun 0
minf 312B
smhd 10
dinf 24
dref 1C
url  C
stbl 30EF
stsd 67
mp4a 57
esds 33
stts 18
stsz 2F1C
stsc 28
stco 124

trak 1AD
tkhd 5C
mdia 131
mdhd 20
hdlr 21
odsm 0
minf E8
nmhd C
dinf 24
dref 1C
url  C
stbl B0
stsd 4C
mp4s 3C
esds 2C
stts 18
stsz 14
stsc 1C
stco 14

tref 18
mpod 10

trak 19d
tkhd 5C
mdia 139
mdhd 20
hdlr 21
sdsm 0
minf F0
nmhd C
dinf 24
dref 1C
url C
stbl B8
stsd 54
mp4s 44
esds 34
stts 18
stsz 14
stsc 1C
stco 14

udta 1A7
hnti 19F
rtp  197

a text string: "
sdp a=mpeg4-iod: "data:application/mpeg4-iod;base64,AoCAgXsAT///D//
/A4CAgQwAAkBoZGF0YTphcHBsaWNhdGlvbi9tcGVnNC1vZC1hdTtiYXNlNjQsQVlDQW
dDNEJnSUNBS1FLZkE0Q0FnQ0lBQVFBRWdJQ0FGR2NWQUFRQkFBUWRVQUFEcjIwRmdJQ
0FBaElRQm9DQWdBRUEEgICADQEFAAAzAAAAAAAAAAAGgICACQEAAAAAAAAAAAOAgIBe
AANAMmRhdGE6YXBwbGljYXRpb24vbXBlZzQtYmlmcy1hdTtiYXNlNjQsd0JBU2dUQXF
CVzNBBICAgBUCDQAACQAAAAAAAAAABYCAgAMAAGAGgICACQEAAAAAAAAAAA=="
"

And finally, hex: 0D0A
(which, if I'm not mistaken, is "carriage return, line feed".)

iTunes m4a
----------
(I've placed a dash next to atoms that are the same length as they were 
in the mp4:)

ftyp 20
moov 5090
mvhd 6C       -
trak 46B0
tkhd 5C       -
mdia 464C
mdhd 20       -
hdlr 21       -
soun 0        -
minf 4603
smhd 10       -
dinf 24       -
dref 1C       -
url  C        -
stbl 45C7
stsd 67       -
mp4a 57       -
esds 33       -
stts 18       -
stsc 28       -(order swapped with stsz atom!)
stsz 41E4

udta 96C
meta 964
hdlr 22
mdir 0
appl -- directly follows the mdir atom- no size value.
ilst 252

What follows is hard to describe ... a sequence of atoms,
(C)nam, (C)ART, (C)alb, gnre, trkn, (C)day, cpil, tmpo, (C)too.
(C) is the copyright symbol in my hex editor -- it's hex '$A9'.
After each of these atoms is a 'data' atom, followed by, in most cases, 
a text string.

Of these,
(C)nam is the track name (text),
(C)ART is the artist (text),
(C)alb is the album (text),
   gnre is a genre id number in hex,
   trkn is a track number in hex--
        two bytes for track, two bytes for total tracks,
(C)day is the year (text),
   cpil is ...?,
   tmpo is the bpm I assume, and
(C)too is the string "iTunes v4.0, QuickTime 6.2".

Then the file continues, with two more strange atoms.

Each of the two is an atom named "----", which acts as a container 
judging by it's size.  Immediately inside a "----" atom is an atom named 
"mean", then an atom named "name", then an atom named "data".

Of these two "----" atoms, the first contains:

mean: com.apple.iTunes
name: iTunNORM
data: 0000001F 00000023 0000016C 0000020E 00052468 000449A8 00001A74 
00001C5B 000474A0 000474A0

And the second contains:

mean: com.apple.iTunes
name: iTunes_CDDB_IDs
data: 17+A079B9C5509BE1BF0778726E68BC11B9+545746

Right away after these mysterious tags is the tag "free", which in this 
particular m4a is $6E4 long, and filled entirely with zeros.

Then the file starts to resemble the mp4 again, with

mdat 2FC0B8

Directly after that, the file ends.

-----------------------------

So what have we learned?

Some interesting info, but nothing that points the way to AAC from 
outside sources.  We've deciphered the m4a file format almost enough to 
let 3rd-party folks manipulate it like iTunes does -- tag reading and 
changing, for example. ... But the innards of the mdat track are still 
mysterious, and the "mp42" versus "mp41" tag is discouraging in this regard.

I have hope for incorporating m4a files into ephpod, but it looks like 
we've got a ways to go before we incorporate external AAC tracks into 
iTunes.  Drat.

-g

http://garote.bdmonkeys.net/







// eompost 3EB22A77:658E.1:rcucbq



------------------------------------------------------
ephPod Mailing List 
FAQ and HomePage http://www.ephpod.com 
mailto:[email protected] to susbcribe 
mailto:[email protected] to unsusbcribe 
mailto:[email protected] the mailing-list itself
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.