Add a comment that explains why this header has no multiple inclusion guards.
[mplayer/greg.git] / libmpdemux / parse_mp4.c
blob7492e3109a546d2e3513df2b129019b52deaa935
1 /* parse_mp4.c - MP4 file format parser code
2 * This file is part of MPlayer, see http://mplayerhq.hu/ for info.
3 * (c)2002 by Felix Buenemann <atmosfear at users.sourceforge.net>
4 * File licensed under the GPL, see http://www.fsf.org/ for more info.
5 * Code inspired by libmp4 from http://mpeg4ip.sourceforge.net/.
6 */
8 #include <stdio.h>
9 #include <inttypes.h>
10 #ifdef HAVE_MALLOC_H
11 #include <malloc.h>
12 #endif
13 #include <stdlib.h>
14 #include "parse_mp4.h"
15 #include "mp_msg.h"
16 #include "stream/stream.h"
18 //#define MP4_DUMPATOM
20 #define MP4_DL MSGL_V
21 #define freereturn(a,b) free(a); return b
23 int mp4_read_descr_len(stream_t *s) {
24 uint8_t b;
25 uint8_t numBytes = 0;
26 uint32_t length = 0;
28 do {
29 b = stream_read_char(s);
30 numBytes++;
31 length = (length << 7) | (b & 0x7F);
32 } while ((b & 0x80) && numBytes < 4);
34 //printf("MP4 read desc len: %d\n", length);
35 return length;
38 /* parse the data part of MP4 esds atoms */
39 int mp4_parse_esds(unsigned char *data, int datalen, esds_t *esds) {
40 /* create memory stream from data */
41 stream_t *s = new_memory_stream(data, datalen);
42 uint16_t len;
43 #ifdef MP4_DUMPATOM
44 {int i;
45 printf("ESDS Dump (%dbyte):\n", datalen);
46 for(i = 0; i < datalen; i++)
47 printf("%02X ", data[i]);
48 printf("\nESDS Dumped\n");}
49 #endif
50 memset(esds, 0, sizeof(esds_t));
52 esds->version = stream_read_char(s);
53 esds->flags = stream_read_int24(s);
54 mp_msg(MSGT_DEMUX, MP4_DL,
55 "ESDS MPEG4 version: %d flags: 0x%06X\n",
56 esds->version, esds->flags);
58 /* get and verify ES_DescrTag */
59 if (stream_read_char(s) == MP4ESDescrTag) {
60 /* read length */
61 len = mp4_read_descr_len(s);
63 esds->ESId = stream_read_word(s);
64 esds->streamPriority = stream_read_char(s);
65 mp_msg(MSGT_DEMUX, MP4_DL,
66 "ESDS MPEG4 ES Descriptor (%dBytes):\n"
67 " -> ESId: %d\n"
68 " -> streamPriority: %d\n",
69 len, esds->ESId, esds->streamPriority);
71 if (len < (5 + 15)) {
72 freereturn(s,1);
74 } else {
75 esds->ESId = stream_read_word(s);
76 mp_msg(MSGT_DEMUX, MP4_DL,
77 "ESDS MPEG4 ES Descriptor (%dBytes):\n"
78 " -> ESId: %d\n", 2, esds->ESId);
81 /* get and verify DecoderConfigDescrTab */
82 if (stream_read_char(s) != MP4DecConfigDescrTag) {
83 freereturn(s,1);
86 /* read length */
87 len = mp4_read_descr_len(s);
89 esds->objectTypeId = stream_read_char(s);
90 esds->streamType = stream_read_char(s);
91 esds->bufferSizeDB = stream_read_int24(s);
92 esds->maxBitrate = stream_read_dword(s);
93 esds->avgBitrate = stream_read_dword(s);
94 mp_msg(MSGT_DEMUX, MP4_DL,
95 "ESDS MPEG4 Decoder Config Descriptor (%dBytes):\n"
96 " -> objectTypeId: %d\n"
97 " -> streamType: 0x%02X\n"
98 " -> bufferSizeDB: 0x%06X\n"
99 " -> maxBitrate: %.3fkbit/s\n"
100 " -> avgBitrate: %.3fkbit/s\n",
101 len, esds->objectTypeId, esds->streamType,
102 esds->bufferSizeDB, esds->maxBitrate/1000.0,
103 esds->avgBitrate/1000.0);
105 esds->decoderConfigLen=0;
107 if (len < 15) {
108 freereturn(s,0);
111 /* get and verify DecSpecificInfoTag */
112 if (stream_read_char(s) != MP4DecSpecificDescrTag) {
113 freereturn(s,0);
116 /* read length */
117 esds->decoderConfigLen = len = mp4_read_descr_len(s);
119 esds->decoderConfig = malloc(esds->decoderConfigLen);
120 if (esds->decoderConfig) {
121 stream_read(s, esds->decoderConfig, esds->decoderConfigLen);
122 } else {
123 esds->decoderConfigLen = 0;
125 mp_msg(MSGT_DEMUX, MP4_DL,
126 "ESDS MPEG4 Decoder Specific Descriptor (%dBytes)\n", len);
128 /* get and verify SLConfigDescrTag */
129 if(stream_read_char(s) != MP4SLConfigDescrTag) {
130 freereturn(s,0);
133 /* Note: SLConfig is usually constant value 2, size 1Byte */
134 esds->SLConfigLen = len = mp4_read_descr_len(s);
135 esds->SLConfig = malloc(esds->SLConfigLen);
136 if (esds->SLConfig) {
137 stream_read(s, esds->SLConfig, esds->SLConfigLen);
138 } else {
139 esds->SLConfigLen = 0;
141 mp_msg(MSGT_DEMUX, MP4_DL,
142 "ESDS MPEG4 Sync Layer Config Descriptor (%dBytes)\n"
143 " -> predefined: %d\n", len, esds->SLConfig[0]);
145 /* will skip the remainder of the atom */
146 freereturn(s,0);
150 /* cleanup all mem occupied by mp4_parse_esds */
151 void mp4_free_esds(esds_t *esds) {
152 if(esds->decoderConfigLen)
153 free(esds->decoderConfig);
154 if(esds->SLConfigLen)
155 free(esds->SLConfig);
158 #undef freereturn
159 #undef MP4_DL