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