Replace QuickTime.h #include with Carbon.h, which is really needed.
[mplayer/glamo.git] / libmpdemux / parse_mp4.c
blobf2a39abe6ed80c924b2acc06130800fd373c2162
1 /*
2 * MP4 file format parser code
4 * Copyright (C) 2002 Felix Buenemann <atmosfear at users.sourceforge.net>
5 * Code inspired by libmp4 from http://mpeg4ip.sourceforge.net/.
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <stdio.h>
25 #include <inttypes.h>
26 #include <stdlib.h>
27 #include "parse_mp4.h"
28 #include "mp_msg.h"
29 #include "stream/stream.h"
31 //#define MP4_DUMPATOM
33 #define MP4_DL MSGL_V
34 #define freereturn(a,b) free(a); return b
36 int mp4_read_descr_len(stream_t *s) {
37 uint8_t b;
38 uint8_t numBytes = 0;
39 uint32_t length = 0;
41 do {
42 b = stream_read_char(s);
43 numBytes++;
44 length = (length << 7) | (b & 0x7F);
45 } while ((b & 0x80) && numBytes < 4);
47 //printf("MP4 read desc len: %d\n", length);
48 return length;
51 /* parse the data part of MP4 esds atoms */
52 int mp4_parse_esds(unsigned char *data, int datalen, esds_t *esds) {
53 /* create memory stream from data */
54 stream_t *s = new_memory_stream(data, datalen);
55 uint16_t len;
56 #ifdef MP4_DUMPATOM
57 {int i;
58 printf("ESDS Dump (%dbyte):\n", datalen);
59 for(i = 0; i < datalen; i++)
60 printf("%02X ", data[i]);
61 printf("\nESDS Dumped\n");}
62 #endif
63 memset(esds, 0, sizeof(esds_t));
65 esds->version = stream_read_char(s);
66 esds->flags = stream_read_int24(s);
67 mp_msg(MSGT_DEMUX, MP4_DL,
68 "ESDS MPEG4 version: %d flags: 0x%06X\n",
69 esds->version, esds->flags);
71 /* get and verify ES_DescrTag */
72 if (stream_read_char(s) == MP4ESDescrTag) {
73 /* read length */
74 len = mp4_read_descr_len(s);
76 esds->ESId = stream_read_word(s);
77 esds->streamPriority = stream_read_char(s);
78 mp_msg(MSGT_DEMUX, MP4_DL,
79 "ESDS MPEG4 ES Descriptor (%dBytes):\n"
80 " -> ESId: %d\n"
81 " -> streamPriority: %d\n",
82 len, esds->ESId, esds->streamPriority);
84 if (len < (5 + 15)) {
85 freereturn(s,1);
87 } else {
88 esds->ESId = stream_read_word(s);
89 mp_msg(MSGT_DEMUX, MP4_DL,
90 "ESDS MPEG4 ES Descriptor (%dBytes):\n"
91 " -> ESId: %d\n", 2, esds->ESId);
94 /* get and verify DecoderConfigDescrTab */
95 if (stream_read_char(s) != MP4DecConfigDescrTag) {
96 freereturn(s,1);
99 /* read length */
100 len = mp4_read_descr_len(s);
102 esds->objectTypeId = stream_read_char(s);
103 esds->streamType = stream_read_char(s);
104 esds->bufferSizeDB = stream_read_int24(s);
105 esds->maxBitrate = stream_read_dword(s);
106 esds->avgBitrate = stream_read_dword(s);
107 mp_msg(MSGT_DEMUX, MP4_DL,
108 "ESDS MPEG4 Decoder Config Descriptor (%dBytes):\n"
109 " -> objectTypeId: %d\n"
110 " -> streamType: 0x%02X\n"
111 " -> bufferSizeDB: 0x%06X\n"
112 " -> maxBitrate: %.3fkbit/s\n"
113 " -> avgBitrate: %.3fkbit/s\n",
114 len, esds->objectTypeId, esds->streamType,
115 esds->bufferSizeDB, esds->maxBitrate/1000.0,
116 esds->avgBitrate/1000.0);
118 esds->decoderConfigLen=0;
120 if (len < 15) {
121 freereturn(s,0);
124 /* get and verify DecSpecificInfoTag */
125 if (stream_read_char(s) != MP4DecSpecificDescrTag) {
126 freereturn(s,0);
129 /* read length */
130 esds->decoderConfigLen = len = mp4_read_descr_len(s);
132 esds->decoderConfig = malloc(esds->decoderConfigLen);
133 if (esds->decoderConfig) {
134 stream_read(s, esds->decoderConfig, esds->decoderConfigLen);
135 } else {
136 esds->decoderConfigLen = 0;
138 mp_msg(MSGT_DEMUX, MP4_DL,
139 "ESDS MPEG4 Decoder Specific Descriptor (%dBytes)\n", len);
141 /* get and verify SLConfigDescrTag */
142 if(stream_read_char(s) != MP4SLConfigDescrTag) {
143 freereturn(s,0);
146 /* Note: SLConfig is usually constant value 2, size 1Byte */
147 esds->SLConfigLen = len = mp4_read_descr_len(s);
148 esds->SLConfig = malloc(esds->SLConfigLen);
149 if (esds->SLConfig) {
150 stream_read(s, esds->SLConfig, esds->SLConfigLen);
151 } else {
152 esds->SLConfigLen = 0;
154 mp_msg(MSGT_DEMUX, MP4_DL,
155 "ESDS MPEG4 Sync Layer Config Descriptor (%dBytes)\n"
156 " -> predefined: %d\n", len, esds->SLConfig[0]);
158 /* will skip the remainder of the atom */
159 freereturn(s,0);
163 /* cleanup all mem occupied by mp4_parse_esds */
164 void mp4_free_esds(esds_t *esds) {
165 if(esds->decoderConfigLen)
166 free(esds->decoderConfig);
167 if(esds->SLConfigLen)
168 free(esds->SLConfig);
171 #undef freereturn
172 #undef MP4_DL