5301
|
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 */
|
|
7
|
|
8 #include <stdio.h>
|
|
9 #include <inttypes.h>
|
|
10 #include <malloc.h>
|
|
11 #include "parse_mp4.h"
|
|
12 #include "mp_msg.h"
|
|
13 #include "stream.h"
|
|
14
|
|
15 #define MP4_DL MSGL_V
|
5303
|
16 #define freereturn(a,b) free(a); return b
|
5301
|
17
|
|
18 int mp4_read_descr_len(stream_t *s) {
|
|
19 uint8_t b;
|
|
20 uint8_t numBytes = 0;
|
|
21 uint32_t length = 0;
|
|
22
|
|
23 do {
|
|
24 b = stream_read_char(s);
|
|
25 numBytes++;
|
|
26 length = (length << 7) | (b & 0x7F);
|
|
27 } while ((b & 0x80) && numBytes < 4);
|
|
28
|
|
29 return length;
|
|
30 }
|
|
31
|
5305
|
32 /* parse the data part of MP4 esds atoms */
|
5301
|
33 int mp4_parse_esds(unsigned char *data, int datalen, esds_t *esds) {
|
|
34 /* create memory stream from data */
|
|
35 stream_t *s = new_memory_stream(data, datalen);
|
|
36 uint8_t len;
|
|
37
|
|
38 esds->version = stream_read_char(s);
|
|
39 esds->flags = stream_read_int24(s);
|
|
40 mp_msg(MSGT_DEMUX, MP4_DL,
|
|
41 "ESDS MPEG4 version: %d flags: 0x%06X\n",
|
|
42 esds->version, esds->flags);
|
|
43
|
|
44 /* get and verify ES_DescrTag */
|
5305
|
45 if (stream_read_char(s) == MP4ESDescrTag) {
|
5301
|
46 /* read length */
|
|
47 if ((len = mp4_read_descr_len(s)) < 5 + 15) {
|
5303
|
48 freereturn(s,1);
|
5301
|
49 }
|
|
50 esds->ESId = stream_read_word(s);
|
|
51 esds->streamPriority = stream_read_char(s);
|
|
52 } else {
|
|
53 #if 1 /* 1 == guessed */
|
|
54 esds->ESId = stream_read_word(s);
|
|
55 #else
|
|
56 /* skip 2 bytes */
|
|
57 stream_skip(s, 2);
|
|
58 #endif
|
|
59 }
|
|
60 mp_msg(MSGT_DEMUX, MP4_DL,
|
|
61 "ESDS MPEG4 ES Descriptor (%dBytes):\n"
|
|
62 " -> ESId: %d\n"
|
|
63 " -> streamPriority: %d\n",
|
|
64 len, esds->ESId, esds->streamPriority);
|
|
65
|
|
66 /* get and verify DecoderConfigDescrTab */
|
|
67 if (stream_read_char(s) != MP4DecConfigDescrTag) {
|
5303
|
68 freereturn(s,1);
|
5301
|
69 }
|
|
70
|
|
71 /* read length */
|
|
72 if ((len = mp4_read_descr_len(s)) < 15) {
|
5303
|
73 freereturn(s,1);
|
5301
|
74 }
|
|
75
|
|
76 esds->objectTypeId = stream_read_char(s);
|
|
77 esds->streamType = stream_read_char(s);
|
|
78 esds->bufferSizeDB = stream_read_int24(s);
|
|
79 esds->maxBitrate = stream_read_dword(s);
|
|
80 esds->avgBitrate = stream_read_dword(s);
|
|
81 mp_msg(MSGT_DEMUX, MP4_DL,
|
|
82 "ESDS MPEG4 Decoder Config Descriptor (%dBytes):\n"
|
|
83 " -> objectTypeId: %d\n"
|
|
84 " -> streamType: 0x%02X\n"
|
|
85 " -> bufferSizeDB: 0x%06X\n"
|
|
86 " -> maxBitrate: %.3fkbit/s\n"
|
|
87 " -> avgBitrate: %.3fkbit/s\n",
|
|
88 len, esds->objectTypeId, esds->streamType,
|
|
89 esds->bufferSizeDB, esds->maxBitrate/1000.0,
|
|
90 esds->avgBitrate/1000.0);
|
|
91
|
|
92 /* get and verify DecSpecificInfoTag */
|
|
93 if (stream_read_char(s) != MP4DecSpecificDescrTag) {
|
5303
|
94 freereturn(s,1);
|
5301
|
95 }
|
|
96
|
|
97 /* read length */
|
|
98 esds->decoderConfigLen = len = mp4_read_descr_len(s);
|
|
99
|
|
100 esds->decoderConfig = malloc(esds->decoderConfigLen);
|
|
101 if (esds->decoderConfig) {
|
|
102 stream_read(s, esds->decoderConfig, esds->decoderConfigLen);
|
|
103 } else {
|
|
104 esds->decoderConfigLen = 0;
|
|
105 }
|
|
106 mp_msg(MSGT_DEMUX, MP4_DL,
|
|
107 "ESDS MPEG4 Decoder Specific Descriptor (%dBytes)\n", len);
|
|
108
|
5305
|
109 /* get and verify SLConfigDescrTag */
|
|
110 if(stream_read_char(s) != MP4SLConfigDescrTag) {
|
|
111 freereturn(s,1);
|
|
112 }
|
|
113
|
|
114 if((len = mp4_read_descr_len(s)) < 1) {
|
|
115 freereturn(s,1);
|
|
116 }
|
|
117
|
|
118 /* Note: SLConfig is usually constant value 2 size 1Byte */
|
|
119 esds->SLConfigLen = len;
|
|
120 esds->SLConfig = malloc(esds->SLConfigLen);
|
|
121 if (esds->SLConfig) {
|
|
122 stream_read(s, esds->SLConfig, esds->SLConfigLen);
|
|
123 } else {
|
|
124 esds->SLConfigLen = 0;
|
|
125 }
|
|
126 mp_msg(MSGT_DEMUX, MP4_DL,
|
|
127 "ESDS MPEG4 Sync Layer Config Descriptor (%dBytes)\n"
|
|
128 " -> predefined: %d\n", len, esds->SLConfig[0]);
|
|
129
|
5301
|
130 /* will skip the remainder of the atom */
|
5303
|
131 freereturn(s,0);
|
5301
|
132
|
|
133 }
|
|
134
|
5305
|
135 /* cleanup all mem occupied by mp4_parse_esds */
|
|
136 void mp4_free_esds(esds_t *esds) {
|
|
137 if(esds->decoderConfig)
|
|
138 free(esds->decoderConfig);
|
|
139 if(esds->SLConfig)
|
|
140 free(esds->SLConfig);
|
|
141 }
|
|
142
|
5303
|
143 #undef freereturn
|
|
144 #undef MP4_DL
|
|
145
|