annotate libmpdemux/parse_mp4.c @ 5336:3cc26c4e662b

no native support for I420 in G200
author arpi
date Mon, 25 Mar 2002 16:22:15 +0000
parents c4f6ab72300a
children dba4cecbb4d1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
1 /* parse_mp4.c - MP4 file format parser code
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
2 * This file is part of MPlayer, see http://mplayerhq.hu/ for info.
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
3 * (c)2002 by Felix Buenemann <atmosfear at users.sourceforge.net>
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
4 * File licensed under the GPL, see http://www.fsf.org/ for more info.
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
5 * Code inspired by libmp4 from http://mpeg4ip.sourceforge.net/.
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
6 */
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
7
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
8 #include <stdio.h>
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
9 #include <inttypes.h>
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
10 #include <malloc.h>
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
11 #include "parse_mp4.h"
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
12 #include "mp_msg.h"
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
13 #include "stream.h"
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
14
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
15 //#define MP4_DUMPATOM
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
16
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
17 #define MP4_DL MSGL_V
5303
534f16f50c17 10l fix memory allocation
atmos4
parents: 5301
diff changeset
18 #define freereturn(a,b) free(a); return b
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
19
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
20 int mp4_read_descr_len(stream_t *s) {
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
21 uint8_t b;
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
22 uint8_t numBytes = 0;
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
23 uint32_t length = 0;
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
24
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
25 do {
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
26 b = stream_read_char(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
27 numBytes++;
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
28 length = (length << 7) | (b & 0x7F);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
29 } while ((b & 0x80) && numBytes < 4);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
30
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
31 //printf("MP4 read desc len: %d\n", length);
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
32 return length;
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
33 }
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
34
5305
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
35 /* parse the data part of MP4 esds atoms */
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
36 int mp4_parse_esds(unsigned char *data, int datalen, esds_t *esds) {
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
37 /* create memory stream from data */
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
38 stream_t *s = new_memory_stream(data, datalen);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
39 uint8_t len;
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
40 #ifdef MP4_DUMPATOM
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
41 {int i;
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
42 printf("ESDS Dump (%dbyte):\n", datalen);
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
43 for(i = 0; i < datalen; i++)
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
44 printf("%02X ", data[i]);
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
45 printf("\nESDS Dumped\n");}
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
46 #endif
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
47 memset(esds, 0, sizeof(esds_t));
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
48
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
49 esds->version = stream_read_char(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
50 esds->flags = stream_read_int24(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
51 mp_msg(MSGT_DEMUX, MP4_DL,
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
52 "ESDS MPEG4 version: %d flags: 0x%06X\n",
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
53 esds->version, esds->flags);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
54
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
55 /* get and verify ES_DescrTag */
5305
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
56 if (stream_read_char(s) == MP4ESDescrTag) {
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
57 /* read length */
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
58 len = mp4_read_descr_len(s);
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
59
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
60 esds->ESId = stream_read_word(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
61 esds->streamPriority = stream_read_char(s);
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
62 mp_msg(MSGT_DEMUX, MP4_DL,
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
63 "ESDS MPEG4 ES Descriptor (%dBytes):\n"
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
64 " -> ESId: %d\n"
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
65 " -> streamPriority: %d\n",
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
66 len, esds->ESId, esds->streamPriority);
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
67
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
68 if (len < (5 + 15)) {
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
69 freereturn(s,1);
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
70 }
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
71 } else {
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
72 esds->ESId = stream_read_word(s);
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
73 mp_msg(MSGT_DEMUX, MP4_DL,
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
74 "ESDS MPEG4 ES Descriptor (%dBytes):\n"
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
75 " -> ESId: %d\n", 2, esds->ESId);
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
76 }
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
77
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
78 /* get and verify DecoderConfigDescrTab */
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
79 if (stream_read_char(s) != MP4DecConfigDescrTag) {
5303
534f16f50c17 10l fix memory allocation
atmos4
parents: 5301
diff changeset
80 freereturn(s,1);
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
81 }
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
82
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
83 /* read length */
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
84 len = mp4_read_descr_len(s);
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
85
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
86 esds->objectTypeId = stream_read_char(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
87 esds->streamType = stream_read_char(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
88 esds->bufferSizeDB = stream_read_int24(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
89 esds->maxBitrate = stream_read_dword(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
90 esds->avgBitrate = stream_read_dword(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
91 mp_msg(MSGT_DEMUX, MP4_DL,
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
92 "ESDS MPEG4 Decoder Config Descriptor (%dBytes):\n"
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
93 " -> objectTypeId: %d\n"
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
94 " -> streamType: 0x%02X\n"
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
95 " -> bufferSizeDB: 0x%06X\n"
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
96 " -> maxBitrate: %.3fkbit/s\n"
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
97 " -> avgBitrate: %.3fkbit/s\n",
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
98 len, esds->objectTypeId, esds->streamType,
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
99 esds->bufferSizeDB, esds->maxBitrate/1000.0,
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
100 esds->avgBitrate/1000.0);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
101
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
102 if (len < 15) {
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
103 freereturn(s,1);
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
104 }
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
105
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
106 /* get and verify DecSpecificInfoTag */
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
107 if (stream_read_char(s) != MP4DecSpecificDescrTag) {
5303
534f16f50c17 10l fix memory allocation
atmos4
parents: 5301
diff changeset
108 freereturn(s,1);
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
109 }
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
110
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
111 /* read length */
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
112 esds->decoderConfigLen = len = mp4_read_descr_len(s);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
113
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
114 esds->decoderConfig = malloc(esds->decoderConfigLen);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
115 if (esds->decoderConfig) {
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
116 stream_read(s, esds->decoderConfig, esds->decoderConfigLen);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
117 } else {
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
118 esds->decoderConfigLen = 0;
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
119 }
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
120 mp_msg(MSGT_DEMUX, MP4_DL,
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
121 "ESDS MPEG4 Decoder Specific Descriptor (%dBytes)\n", len);
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
122
5305
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
123 /* get and verify SLConfigDescrTag */
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
124 if(stream_read_char(s) != MP4SLConfigDescrTag) {
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
125 freereturn(s,1);
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
126 }
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
127
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
128 /* Note: SLConfig is usually constant value 2, size 1Byte */
5333
c4f6ab72300a eeh 10l, removed one line too much :(
atmos4
parents: 5332
diff changeset
129 esds->SLConfigLen = len = mp4_read_descr_len(s);
5305
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
130 esds->SLConfig = malloc(esds->SLConfigLen);
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
131 if (esds->SLConfig) {
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
132 stream_read(s, esds->SLConfig, esds->SLConfigLen);
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
133 } else {
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
134 esds->SLConfigLen = 0;
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
135 }
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
136 mp_msg(MSGT_DEMUX, MP4_DL,
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
137 "ESDS MPEG4 Sync Layer Config Descriptor (%dBytes)\n"
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
138 " -> predefined: %d\n", len, esds->SLConfig[0]);
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
139
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
140 /* will skip the remainder of the atom */
5303
534f16f50c17 10l fix memory allocation
atmos4
parents: 5301
diff changeset
141 freereturn(s,0);
5301
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
142
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
143 }
d72c3169a343 Improved MP4 parsing (finally)
atmos4
parents:
diff changeset
144
5305
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
145 /* cleanup all mem occupied by mp4_parse_esds */
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
146 void mp4_free_esds(esds_t *esds) {
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
147 if(esds->decoderConfigLen)
5305
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
148 free(esds->decoderConfig);
5332
a3874da21700 Fix some silly logical bugs and fix memory cleanup in case mp4_parse_es returned with an error by memsetting the esds struct to 0 on init.
atmos4
parents: 5305
diff changeset
149 if(esds->SLConfigLen)
5305
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
150 free(esds->SLConfig);
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
151 }
77ac28af44ec - Add parsing of Sync Layer Descriptor
atmos4
parents: 5303
diff changeset
152
5303
534f16f50c17 10l fix memory allocation
atmos4
parents: 5301
diff changeset
153 #undef freereturn
534f16f50c17 10l fix memory allocation
atmos4
parents: 5301
diff changeset
154 #undef MP4_DL
534f16f50c17 10l fix memory allocation
atmos4
parents: 5301
diff changeset
155