Update help output with previous --extra-ldflags change.
[mplayer/glamo.git] / libmpdemux / parse_mp4.c
blob79770f821c3dfe19c57a7ce37dcceab83055a202
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 #if HAVE_MALLOC_H
27 #include <malloc.h>
28 #endif
29 #include <stdlib.h>
30 #include "parse_mp4.h"
31 #include "mp_msg.h"
32 #include "stream/stream.h"
34 //#define MP4_DUMPATOM
36 #define MP4_DL MSGL_V
37 #define freereturn(a,b) free(a); return b
39 int mp4_read_descr_len(stream_t *s) {
40 uint8_t b;
41 uint8_t numBytes = 0;
42 uint32_t length = 0;
44 do {
45 b = stream_read_char(s);
46 numBytes++;
47 length = (length << 7) | (b & 0x7F);
48 } while ((b & 0x80) && numBytes < 4);
50 //printf("MP4 read desc len: %d\n", length);
51 return length;
54 /* parse the data part of MP4 esds atoms */
55 int mp4_parse_esds(unsigned char *data, int datalen, esds_t *esds) {
56 /* create memory stream from data */
57 stream_t *s = new_memory_stream(data, datalen);
58 uint16_t len;
59 #ifdef MP4_DUMPATOM
60 {int i;
61 printf("ESDS Dump (%dbyte):\n", datalen);
62 for(i = 0; i < datalen; i++)
63 printf("%02X ", data[i]);
64 printf("\nESDS Dumped\n");}
65 #endif
66 memset(esds, 0, sizeof(esds_t));
68 esds->version = stream_read_char(s);
69 esds->flags = stream_read_int24(s);
70 mp_msg(MSGT_DEMUX, MP4_DL,
71 "ESDS MPEG4 version: %d flags: 0x%06X\n",
72 esds->version, esds->flags);
74 /* get and verify ES_DescrTag */
75 if (stream_read_char(s) == MP4ESDescrTag) {
76 /* read length */
77 len = mp4_read_descr_len(s);
79 esds->ESId = stream_read_word(s);
80 esds->streamPriority = stream_read_char(s);
81 mp_msg(MSGT_DEMUX, MP4_DL,
82 "ESDS MPEG4 ES Descriptor (%dBytes):\n"
83 " -> ESId: %d\n"
84 " -> streamPriority: %d\n",
85 len, esds->ESId, esds->streamPriority);
87 if (len < (5 + 15)) {
88 freereturn(s,1);
90 } else {
91 esds->ESId = stream_read_word(s);
92 mp_msg(MSGT_DEMUX, MP4_DL,
93 "ESDS MPEG4 ES Descriptor (%dBytes):\n"
94 " -> ESId: %d\n", 2, esds->ESId);
97 /* get and verify DecoderConfigDescrTab */
98 if (stream_read_char(s) != MP4DecConfigDescrTag) {
99 freereturn(s,1);
102 /* read length */
103 len = mp4_read_descr_len(s);
105 esds->objectTypeId = stream_read_char(s);
106 esds->streamType = stream_read_char(s);
107 esds->bufferSizeDB = stream_read_int24(s);
108 esds->maxBitrate = stream_read_dword(s);
109 esds->avgBitrate = stream_read_dword(s);
110 mp_msg(MSGT_DEMUX, MP4_DL,
111 "ESDS MPEG4 Decoder Config Descriptor (%dBytes):\n"
112 " -> objectTypeId: %d\n"
113 " -> streamType: 0x%02X\n"
114 " -> bufferSizeDB: 0x%06X\n"
115 " -> maxBitrate: %.3fkbit/s\n"
116 " -> avgBitrate: %.3fkbit/s\n",
117 len, esds->objectTypeId, esds->streamType,
118 esds->bufferSizeDB, esds->maxBitrate/1000.0,
119 esds->avgBitrate/1000.0);
121 esds->decoderConfigLen=0;
123 if (len < 15) {
124 freereturn(s,0);
127 /* get and verify DecSpecificInfoTag */
128 if (stream_read_char(s) != MP4DecSpecificDescrTag) {
129 freereturn(s,0);
132 /* read length */
133 esds->decoderConfigLen = len = mp4_read_descr_len(s);
135 esds->decoderConfig = malloc(esds->decoderConfigLen);
136 if (esds->decoderConfig) {
137 stream_read(s, esds->decoderConfig, esds->decoderConfigLen);
138 } else {
139 esds->decoderConfigLen = 0;
141 mp_msg(MSGT_DEMUX, MP4_DL,
142 "ESDS MPEG4 Decoder Specific Descriptor (%dBytes)\n", len);
144 /* get and verify SLConfigDescrTag */
145 if(stream_read_char(s) != MP4SLConfigDescrTag) {
146 freereturn(s,0);
149 /* Note: SLConfig is usually constant value 2, size 1Byte */
150 esds->SLConfigLen = len = mp4_read_descr_len(s);
151 esds->SLConfig = malloc(esds->SLConfigLen);
152 if (esds->SLConfig) {
153 stream_read(s, esds->SLConfig, esds->SLConfigLen);
154 } else {
155 esds->SLConfigLen = 0;
157 mp_msg(MSGT_DEMUX, MP4_DL,
158 "ESDS MPEG4 Sync Layer Config Descriptor (%dBytes)\n"
159 " -> predefined: %d\n", len, esds->SLConfig[0]);
161 /* will skip the remainder of the atom */
162 freereturn(s,0);
166 /* cleanup all mem occupied by mp4_parse_esds */
167 void mp4_free_esds(esds_t *esds) {
168 if(esds->decoderConfigLen)
169 free(esds->decoderConfig);
170 if(esds->SLConfigLen)
171 free(esds->SLConfig);
174 #undef freereturn
175 #undef MP4_DL