OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/container_names.h" | 5 #include "media/base/container_names.h" |
6 | 6 |
7 #include <cctype> | 7 #include <cctype> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "media/base/bit_reader.h" | 11 #include "media/base/bit_reader.h" |
12 | 12 |
13 namespace media { | 13 namespace media { |
14 | 14 |
15 namespace container_names { | 15 namespace container_names { |
16 | 16 |
17 #define TAG(a, b, c, d) \ | 17 #define TAG(a, b, c, d) \ |
18 ((static_cast<uint32>(static_cast<uint8>(a)) << 24) | \ | 18 ((static_cast<uint32_t>(static_cast<uint8_t>(a)) << 24) | \ |
19 (static_cast<uint32>(static_cast<uint8>(b)) << 16) | \ | 19 (static_cast<uint32_t>(static_cast<uint8_t>(b)) << 16) | \ |
20 (static_cast<uint32>(static_cast<uint8>(c)) << 8) | \ | 20 (static_cast<uint32_t>(static_cast<uint8_t>(c)) << 8) | \ |
21 (static_cast<uint32>(static_cast<uint8>(d)))) | 21 (static_cast<uint32_t>(static_cast<uint8_t>(d)))) |
22 | 22 |
23 #define RCHECK(x) \ | 23 #define RCHECK(x) \ |
24 do { \ | 24 do { \ |
25 if (!(x)) \ | 25 if (!(x)) \ |
26 return false; \ | 26 return false; \ |
27 } while (0) | 27 } while (0) |
28 | 28 |
29 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf" | 29 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf" |
30 | 30 |
31 // Helper function to read 2 bytes (16 bits, big endian) from a buffer. | 31 // Helper function to read 2 bytes (16 bits, big endian) from a buffer. |
32 static int Read16(const uint8* p) { | 32 static int Read16(const uint8_t* p) { |
33 return p[0] << 8 | p[1]; | 33 return p[0] << 8 | p[1]; |
34 } | 34 } |
35 | 35 |
36 // Helper function to read 3 bytes (24 bits, big endian) from a buffer. | 36 // Helper function to read 3 bytes (24 bits, big endian) from a buffer. |
37 static uint32 Read24(const uint8* p) { | 37 static uint32_t Read24(const uint8_t* p) { |
38 return p[0] << 16 | p[1] << 8 | p[2]; | 38 return p[0] << 16 | p[1] << 8 | p[2]; |
39 } | 39 } |
40 | 40 |
41 // Helper function to read 4 bytes (32 bits, big endian) from a buffer. | 41 // Helper function to read 4 bytes (32 bits, big endian) from a buffer. |
42 static uint32 Read32(const uint8* p) { | 42 static uint32_t Read32(const uint8_t* p) { |
43 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]; | 43 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]; |
44 } | 44 } |
45 | 45 |
46 // Helper function to read 4 bytes (32 bits, little endian) from a buffer. | 46 // Helper function to read 4 bytes (32 bits, little endian) from a buffer. |
47 static uint32 Read32LE(const uint8* p) { | 47 static uint32_t Read32LE(const uint8_t* p) { |
48 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0]; | 48 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0]; |
49 } | 49 } |
50 | 50 |
51 // Helper function to do buffer comparisons with a string without going off the | 51 // Helper function to do buffer comparisons with a string without going off the |
52 // end of the buffer. | 52 // end of the buffer. |
53 static bool StartsWith(const uint8* buffer, | 53 static bool StartsWith(const uint8_t* buffer, |
54 size_t buffer_size, | 54 size_t buffer_size, |
55 const char* prefix) { | 55 const char* prefix) { |
56 size_t prefix_size = strlen(prefix); | 56 size_t prefix_size = strlen(prefix); |
57 return (prefix_size <= buffer_size && | 57 return (prefix_size <= buffer_size && |
58 memcmp(buffer, prefix, prefix_size) == 0); | 58 memcmp(buffer, prefix, prefix_size) == 0); |
59 } | 59 } |
60 | 60 |
61 // Helper function to do buffer comparisons with another buffer (to allow for | 61 // Helper function to do buffer comparisons with another buffer (to allow for |
62 // embedded \0 in the comparison) without going off the end of the buffer. | 62 // embedded \0 in the comparison) without going off the end of the buffer. |
63 static bool StartsWith(const uint8* buffer, | 63 static bool StartsWith(const uint8_t* buffer, |
64 size_t buffer_size, | 64 size_t buffer_size, |
65 const uint8* prefix, | 65 const uint8_t* prefix, |
66 size_t prefix_size) { | 66 size_t prefix_size) { |
67 return (prefix_size <= buffer_size && | 67 return (prefix_size <= buffer_size && |
68 memcmp(buffer, prefix, prefix_size) == 0); | 68 memcmp(buffer, prefix, prefix_size) == 0); |
69 } | 69 } |
70 | 70 |
71 // Helper function to read up to 64 bits from a bit stream. | 71 // Helper function to read up to 64 bits from a bit stream. |
72 static uint64 ReadBits(BitReader* reader, int num_bits) { | 72 static uint64_t ReadBits(BitReader* reader, int num_bits) { |
73 DCHECK_GE(reader->bits_available(), num_bits); | 73 DCHECK_GE(reader->bits_available(), num_bits); |
74 DCHECK((num_bits > 0) && (num_bits <= 64)); | 74 DCHECK((num_bits > 0) && (num_bits <= 64)); |
75 uint64 value; | 75 uint64_t value; |
76 reader->ReadBits(num_bits, &value); | 76 reader->ReadBits(num_bits, &value); |
77 return value; | 77 return value; |
78 } | 78 } |
79 | 79 |
80 const int kAc3FrameSizeTable[38][3] = { | 80 const int kAc3FrameSizeTable[38][3] = { |
81 { 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 }, | 81 { 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 }, |
82 { 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 }, | 82 { 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 }, |
83 { 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 }, | 83 { 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 }, |
84 { 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 }, | 84 { 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 }, |
85 { 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 }, | 85 { 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 }, |
86 { 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 }, | 86 { 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 }, |
87 { 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 }, | 87 { 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 }, |
88 { 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 }, | 88 { 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 }, |
89 { 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 }, | 89 { 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 }, |
90 { 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 }, | 90 { 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 }, |
91 { 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 } | 91 { 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 } |
92 }; | 92 }; |
93 | 93 |
94 // Checks for an ADTS AAC container. | 94 // Checks for an ADTS AAC container. |
95 static bool CheckAac(const uint8* buffer, int buffer_size) { | 95 static bool CheckAac(const uint8_t* buffer, int buffer_size) { |
96 // Audio Data Transport Stream (ADTS) header is 7 or 9 bytes | 96 // Audio Data Transport Stream (ADTS) header is 7 or 9 bytes |
97 // (from http://wiki.multimedia.cx/index.php?title=ADTS) | 97 // (from http://wiki.multimedia.cx/index.php?title=ADTS) |
98 RCHECK(buffer_size > 6); | 98 RCHECK(buffer_size > 6); |
99 | 99 |
100 int offset = 0; | 100 int offset = 0; |
101 while (offset + 6 < buffer_size) { | 101 while (offset + 6 < buffer_size) { |
102 BitReader reader(buffer + offset, 6); | 102 BitReader reader(buffer + offset, 6); |
103 | 103 |
104 // Syncword must be 0xfff. | 104 // Syncword must be 0xfff. |
105 RCHECK(ReadBits(&reader, 12) == 0xfff); | 105 RCHECK(ReadBits(&reader, 12) == 0xfff); |
(...skipping 15 matching lines...) Expand all Loading... |
121 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1); | 121 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1); |
122 | 122 |
123 // Get frame length (includes header). | 123 // Get frame length (includes header). |
124 int size = ReadBits(&reader, 13); | 124 int size = ReadBits(&reader, 13); |
125 RCHECK(size > 0); | 125 RCHECK(size > 0); |
126 offset += size; | 126 offset += size; |
127 } | 127 } |
128 return true; | 128 return true; |
129 } | 129 } |
130 | 130 |
131 const uint16 kAc3SyncWord = 0x0b77; | 131 const uint16_t kAc3SyncWord = 0x0b77; |
132 | 132 |
133 // Checks for an AC3 container. | 133 // Checks for an AC3 container. |
134 static bool CheckAc3(const uint8* buffer, int buffer_size) { | 134 static bool CheckAc3(const uint8_t* buffer, int buffer_size) { |
135 // Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3) | 135 // Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3) |
136 // Doc. A/52:2012 | 136 // Doc. A/52:2012 |
137 // (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf) | 137 // (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf) |
138 | 138 |
139 // AC3 container looks like syncinfo | bsi | audblk * 6 | aux | check. | 139 // AC3 container looks like syncinfo | bsi | audblk * 6 | aux | check. |
140 RCHECK(buffer_size > 6); | 140 RCHECK(buffer_size > 6); |
141 | 141 |
142 int offset = 0; | 142 int offset = 0; |
143 while (offset + 6 < buffer_size) { | 143 while (offset + 6 < buffer_size) { |
144 BitReader reader(buffer + offset, 6); | 144 BitReader reader(buffer + offset, 6); |
(...skipping 14 matching lines...) Expand all Loading... |
159 | 159 |
160 // Verify bsid. | 160 // Verify bsid. |
161 RCHECK(ReadBits(&reader, 5) < 10); // Normally 8 or 6, 16 used by EAC3. | 161 RCHECK(ReadBits(&reader, 5) < 10); // Normally 8 or 6, 16 used by EAC3. |
162 | 162 |
163 offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code]; | 163 offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code]; |
164 } | 164 } |
165 return true; | 165 return true; |
166 } | 166 } |
167 | 167 |
168 // Checks for an EAC3 container (very similar to AC3) | 168 // Checks for an EAC3 container (very similar to AC3) |
169 static bool CheckEac3(const uint8* buffer, int buffer_size) { | 169 static bool CheckEac3(const uint8_t* buffer, int buffer_size) { |
170 // Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3) | 170 // Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3) |
171 // Doc. A/52:2012 | 171 // Doc. A/52:2012 |
172 // (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf) | 172 // (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf) |
173 | 173 |
174 // EAC3 container looks like syncinfo | bsi | audfrm | audblk* | aux | check. | 174 // EAC3 container looks like syncinfo | bsi | audfrm | audblk* | aux | check. |
175 RCHECK(buffer_size > 6); | 175 RCHECK(buffer_size > 6); |
176 | 176 |
177 int offset = 0; | 177 int offset = 0; |
178 while (offset + 6 < buffer_size) { | 178 while (offset + 6 < buffer_size) { |
179 BitReader reader(buffer + offset, 6); | 179 BitReader reader(buffer + offset, 6); |
(...skipping 17 matching lines...) Expand all Loading... |
197 // Verify bsid. | 197 // Verify bsid. |
198 int bit_stream_id = ReadBits(&reader, 5); | 198 int bit_stream_id = ReadBits(&reader, 5); |
199 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16); | 199 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16); |
200 | 200 |
201 offset += frame_size; | 201 offset += frame_size; |
202 } | 202 } |
203 return true; | 203 return true; |
204 } | 204 } |
205 | 205 |
206 // Additional checks for a BINK container. | 206 // Additional checks for a BINK container. |
207 static bool CheckBink(const uint8* buffer, int buffer_size) { | 207 static bool CheckBink(const uint8_t* buffer, int buffer_size) { |
208 // Reference: http://wiki.multimedia.cx/index.php?title=Bink_Container | 208 // Reference: http://wiki.multimedia.cx/index.php?title=Bink_Container |
209 RCHECK(buffer_size >= 44); | 209 RCHECK(buffer_size >= 44); |
210 | 210 |
211 // Verify number of frames specified. | 211 // Verify number of frames specified. |
212 RCHECK(Read32LE(buffer + 8) > 0); | 212 RCHECK(Read32LE(buffer + 8) > 0); |
213 | 213 |
214 // Verify width in range. | 214 // Verify width in range. |
215 int width = Read32LE(buffer + 20); | 215 int width = Read32LE(buffer + 20); |
216 RCHECK(width > 0 && width <= 32767); | 216 RCHECK(width > 0 && width <= 32767); |
217 | 217 |
218 // Verify height in range. | 218 // Verify height in range. |
219 int height = Read32LE(buffer + 24); | 219 int height = Read32LE(buffer + 24); |
220 RCHECK(height > 0 && height <= 32767); | 220 RCHECK(height > 0 && height <= 32767); |
221 | 221 |
222 // Verify frames per second specified. | 222 // Verify frames per second specified. |
223 RCHECK(Read32LE(buffer + 28) > 0); | 223 RCHECK(Read32LE(buffer + 28) > 0); |
224 | 224 |
225 // Verify video frames per second specified. | 225 // Verify video frames per second specified. |
226 RCHECK(Read32LE(buffer + 32) > 0); | 226 RCHECK(Read32LE(buffer + 32) > 0); |
227 | 227 |
228 // Number of audio tracks must be 256 or less. | 228 // Number of audio tracks must be 256 or less. |
229 return (Read32LE(buffer + 40) <= 256); | 229 return (Read32LE(buffer + 40) <= 256); |
230 } | 230 } |
231 | 231 |
232 // Additional checks for a CAF container. | 232 // Additional checks for a CAF container. |
233 static bool CheckCaf(const uint8* buffer, int buffer_size) { | 233 static bool CheckCaf(const uint8_t* buffer, int buffer_size) { |
234 // Reference: Apple Core Audio Format Specification 1.0 | 234 // Reference: Apple Core Audio Format Specification 1.0 |
235 // (https://developer.apple.com/library/mac/#documentation/MusicAudio/Referenc
e/CAFSpec/CAF_spec/CAF_spec.html) | 235 // (https://developer.apple.com/library/mac/#documentation/MusicAudio/Referenc
e/CAFSpec/CAF_spec/CAF_spec.html) |
236 RCHECK(buffer_size >= 52); | 236 RCHECK(buffer_size >= 52); |
237 BitReader reader(buffer, buffer_size); | 237 BitReader reader(buffer, buffer_size); |
238 | 238 |
239 // mFileType should be "caff". | 239 // mFileType should be "caff". |
240 RCHECK(ReadBits(&reader, 32) == TAG('c', 'a', 'f', 'f')); | 240 RCHECK(ReadBits(&reader, 32) == TAG('c', 'a', 'f', 'f')); |
241 | 241 |
242 // mFileVersion should be 1. | 242 // mFileVersion should be 1. |
243 RCHECK(ReadBits(&reader, 16) == 1); | 243 RCHECK(ReadBits(&reader, 16) == 1); |
(...skipping 20 matching lines...) Expand all Loading... |
264 } | 264 } |
265 | 265 |
266 static bool kSamplingFrequencyValid[16] = { false, true, true, true, false, | 266 static bool kSamplingFrequencyValid[16] = { false, true, true, true, false, |
267 false, true, true, true, false, | 267 false, true, true, true, false, |
268 false, true, true, true, false, | 268 false, true, true, true, false, |
269 false }; | 269 false }; |
270 static bool kExtAudioIdValid[8] = { true, false, true, false, false, false, | 270 static bool kExtAudioIdValid[8] = { true, false, true, false, false, false, |
271 true, false }; | 271 true, false }; |
272 | 272 |
273 // Additional checks for a DTS container. | 273 // Additional checks for a DTS container. |
274 static bool CheckDts(const uint8* buffer, int buffer_size) { | 274 static bool CheckDts(const uint8_t* buffer, int buffer_size) { |
275 // Reference: ETSI TS 102 114 V1.3.1 (2011-08) | 275 // Reference: ETSI TS 102 114 V1.3.1 (2011-08) |
276 // (http://www.etsi.org/deliver/etsi_ts/102100_102199/102114/01.03.01_60/ts_10
2114v010301p.pdf) | 276 // (http://www.etsi.org/deliver/etsi_ts/102100_102199/102114/01.03.01_60/ts_10
2114v010301p.pdf) |
277 RCHECK(buffer_size > 11); | 277 RCHECK(buffer_size > 11); |
278 | 278 |
279 int offset = 0; | 279 int offset = 0; |
280 while (offset + 11 < buffer_size) { | 280 while (offset + 11 < buffer_size) { |
281 BitReader reader(buffer + offset, 11); | 281 BitReader reader(buffer + offset, 11); |
282 | 282 |
283 // Verify sync word. | 283 // Verify sync word. |
284 RCHECK(ReadBits(&reader, 32) == 0x7ffe8001); | 284 RCHECK(ReadBits(&reader, 32) == 0x7ffe8001); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 | 319 |
320 // Verify low frequency effects flag is an allowed value. | 320 // Verify low frequency effects flag is an allowed value. |
321 RCHECK(ReadBits(&reader, 2) != 3); | 321 RCHECK(ReadBits(&reader, 2) != 3); |
322 | 322 |
323 offset += frame_size + 1; | 323 offset += frame_size + 1; |
324 } | 324 } |
325 return true; | 325 return true; |
326 } | 326 } |
327 | 327 |
328 // Checks for a DV container. | 328 // Checks for a DV container. |
329 static bool CheckDV(const uint8* buffer, int buffer_size) { | 329 static bool CheckDV(const uint8_t* buffer, int buffer_size) { |
330 // Reference: SMPTE 314M (Annex A has differences with IEC 61834). | 330 // Reference: SMPTE 314M (Annex A has differences with IEC 61834). |
331 // (http://standards.smpte.org/content/978-1-61482-454-1/st-314-2005/SEC1.body
.pdf) | 331 // (http://standards.smpte.org/content/978-1-61482-454-1/st-314-2005/SEC1.body
.pdf) |
332 RCHECK(buffer_size > 11); | 332 RCHECK(buffer_size > 11); |
333 | 333 |
334 int offset = 0; | 334 int offset = 0; |
335 int current_sequence_number = -1; | 335 int current_sequence_number = -1; |
336 int last_block_number[6] = {0}; | 336 int last_block_number[6] = {0}; |
337 while (offset + 11 < buffer_size) { | 337 while (offset + 11 < buffer_size) { |
338 BitReader reader(buffer + offset, 11); | 338 BitReader reader(buffer + offset, 11); |
339 | 339 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 } | 382 } |
383 | 383 |
384 // Move to next block. | 384 // Move to next block. |
385 offset += 80; | 385 offset += 80; |
386 } | 386 } |
387 return true; | 387 return true; |
388 } | 388 } |
389 | 389 |
390 | 390 |
391 // Checks for a GSM container. | 391 // Checks for a GSM container. |
392 static bool CheckGsm(const uint8* buffer, int buffer_size) { | 392 static bool CheckGsm(const uint8_t* buffer, int buffer_size) { |
393 // Reference: ETSI EN 300 961 V8.1.1 | 393 // Reference: ETSI EN 300 961 V8.1.1 |
394 // (http://www.etsi.org/deliver/etsi_en/300900_300999/300961/08.01.01_60/en_30
0961v080101p.pdf) | 394 // (http://www.etsi.org/deliver/etsi_en/300900_300999/300961/08.01.01_60/en_30
0961v080101p.pdf) |
395 // also http://tools.ietf.org/html/rfc3551#page-24 | 395 // also http://tools.ietf.org/html/rfc3551#page-24 |
396 // GSM files have a 33 byte block, only first 4 bits are fixed. | 396 // GSM files have a 33 byte block, only first 4 bits are fixed. |
397 RCHECK(buffer_size >= 1024); // Need enough data to do a decent check. | 397 RCHECK(buffer_size >= 1024); // Need enough data to do a decent check. |
398 | 398 |
399 int offset = 0; | 399 int offset = 0; |
400 while (offset < buffer_size) { | 400 while (offset < buffer_size) { |
401 // First 4 bits of each block are xD. | 401 // First 4 bits of each block are xD. |
402 RCHECK((buffer[offset] & 0xf0) == 0xd0); | 402 RCHECK((buffer[offset] & 0xf0) == 0xd0); |
403 offset += 33; | 403 offset += 33; |
404 } | 404 } |
405 return true; | 405 return true; |
406 } | 406 } |
407 | 407 |
408 // Advance to the first set of |num_bits| bits that match |start_code|. |offset| | 408 // Advance to the first set of |num_bits| bits that match |start_code|. |offset| |
409 // is the current location in the buffer, and is updated. |bytes_needed| is the | 409 // is the current location in the buffer, and is updated. |bytes_needed| is the |
410 // number of bytes that must remain in the buffer when |start_code| is found. | 410 // number of bytes that must remain in the buffer when |start_code| is found. |
411 // Returns true if start_code found (and enough space in the buffer after it), | 411 // Returns true if start_code found (and enough space in the buffer after it), |
412 // false otherwise. | 412 // false otherwise. |
413 static bool AdvanceToStartCode(const uint8* buffer, | 413 static bool AdvanceToStartCode(const uint8_t* buffer, |
414 int buffer_size, | 414 int buffer_size, |
415 int* offset, | 415 int* offset, |
416 int bytes_needed, | 416 int bytes_needed, |
417 int num_bits, | 417 int num_bits, |
418 uint32 start_code) { | 418 uint32_t start_code) { |
419 DCHECK_GE(bytes_needed, 3); | 419 DCHECK_GE(bytes_needed, 3); |
420 DCHECK_LE(num_bits, 24); // Only supports up to 24 bits. | 420 DCHECK_LE(num_bits, 24); // Only supports up to 24 bits. |
421 | 421 |
422 // Create a mask to isolate |num_bits| bits, once shifted over. | 422 // Create a mask to isolate |num_bits| bits, once shifted over. |
423 uint32 bits_to_shift = 24 - num_bits; | 423 uint32_t bits_to_shift = 24 - num_bits; |
424 uint32 mask = (1 << num_bits) - 1; | 424 uint32_t mask = (1 << num_bits) - 1; |
425 while (*offset + bytes_needed < buffer_size) { | 425 while (*offset + bytes_needed < buffer_size) { |
426 uint32 next = Read24(buffer + *offset); | 426 uint32_t next = Read24(buffer + *offset); |
427 if (((next >> bits_to_shift) & mask) == start_code) | 427 if (((next >> bits_to_shift) & mask) == start_code) |
428 return true; | 428 return true; |
429 ++(*offset); | 429 ++(*offset); |
430 } | 430 } |
431 return false; | 431 return false; |
432 } | 432 } |
433 | 433 |
434 // Checks for an H.261 container. | 434 // Checks for an H.261 container. |
435 static bool CheckH261(const uint8* buffer, int buffer_size) { | 435 static bool CheckH261(const uint8_t* buffer, int buffer_size) { |
436 // Reference: ITU-T Recommendation H.261 (03/1993) | 436 // Reference: ITU-T Recommendation H.261 (03/1993) |
437 // (http://www.itu.int/rec/T-REC-H.261-199303-I/en) | 437 // (http://www.itu.int/rec/T-REC-H.261-199303-I/en) |
438 RCHECK(buffer_size > 16); | 438 RCHECK(buffer_size > 16); |
439 | 439 |
440 int offset = 0; | 440 int offset = 0; |
441 bool seen_start_code = false; | 441 bool seen_start_code = false; |
442 while (true) { | 442 while (true) { |
443 // Advance to picture_start_code, if there is one. | 443 // Advance to picture_start_code, if there is one. |
444 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 20, 0x10)) { | 444 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 20, 0x10)) { |
445 // No start code found (or off end of buffer), so success if | 445 // No start code found (or off end of buffer), so success if |
(...skipping 27 matching lines...) Expand all Loading... |
473 return seen_start_code; | 473 return seen_start_code; |
474 RCHECK(next == 1); | 474 RCHECK(next == 1); |
475 | 475 |
476 // Move to the next block. | 476 // Move to the next block. |
477 seen_start_code = true; | 477 seen_start_code = true; |
478 offset += 4; | 478 offset += 4; |
479 } | 479 } |
480 } | 480 } |
481 | 481 |
482 // Checks for an H.263 container. | 482 // Checks for an H.263 container. |
483 static bool CheckH263(const uint8* buffer, int buffer_size) { | 483 static bool CheckH263(const uint8_t* buffer, int buffer_size) { |
484 // Reference: ITU-T Recommendation H.263 (01/2005) | 484 // Reference: ITU-T Recommendation H.263 (01/2005) |
485 // (http://www.itu.int/rec/T-REC-H.263-200501-I/en) | 485 // (http://www.itu.int/rec/T-REC-H.263-200501-I/en) |
486 // header is PSC(22b) + TR(8b) + PTYPE(8+b). | 486 // header is PSC(22b) + TR(8b) + PTYPE(8+b). |
487 RCHECK(buffer_size > 16); | 487 RCHECK(buffer_size > 16); |
488 | 488 |
489 int offset = 0; | 489 int offset = 0; |
490 bool seen_start_code = false; | 490 bool seen_start_code = false; |
491 while (true) { | 491 while (true) { |
492 // Advance to picture_start_code, if there is one. | 492 // Advance to picture_start_code, if there is one. |
493 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 9, 22, 0x20)) { | 493 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 9, 22, 0x20)) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 RCHECK(ReadBits(&reader, 3) == 1); // Not allowed. | 541 RCHECK(ReadBits(&reader, 3) == 1); // Not allowed. |
542 } | 542 } |
543 | 543 |
544 // Move to the next block. | 544 // Move to the next block. |
545 seen_start_code = true; | 545 seen_start_code = true; |
546 offset += 9; | 546 offset += 9; |
547 } | 547 } |
548 } | 548 } |
549 | 549 |
550 // Checks for an H.264 container. | 550 // Checks for an H.264 container. |
551 static bool CheckH264(const uint8* buffer, int buffer_size) { | 551 static bool CheckH264(const uint8_t* buffer, int buffer_size) { |
552 // Reference: ITU-T Recommendation H.264 (01/2012) | 552 // Reference: ITU-T Recommendation H.264 (01/2012) |
553 // (http://www.itu.int/rec/T-REC-H.264) | 553 // (http://www.itu.int/rec/T-REC-H.264) |
554 // Section B.1: Byte stream NAL unit syntax and semantics. | 554 // Section B.1: Byte stream NAL unit syntax and semantics. |
555 RCHECK(buffer_size > 4); | 555 RCHECK(buffer_size > 4); |
556 | 556 |
557 int offset = 0; | 557 int offset = 0; |
558 int parameter_count = 0; | 558 int parameter_count = 0; |
559 while (true) { | 559 while (true) { |
560 // Advance to picture_start_code, if there is one. | 560 // Advance to picture_start_code, if there is one. |
561 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 24, 1)) { | 561 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 24, 1)) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 offset += 4; | 597 offset += 4; |
598 } | 598 } |
599 } | 599 } |
600 | 600 |
601 static const char kHlsSignature[] = "#EXTM3U"; | 601 static const char kHlsSignature[] = "#EXTM3U"; |
602 static const char kHls1[] = "#EXT-X-STREAM-INF:"; | 602 static const char kHls1[] = "#EXT-X-STREAM-INF:"; |
603 static const char kHls2[] = "#EXT-X-TARGETDURATION:"; | 603 static const char kHls2[] = "#EXT-X-TARGETDURATION:"; |
604 static const char kHls3[] = "#EXT-X-MEDIA-SEQUENCE:"; | 604 static const char kHls3[] = "#EXT-X-MEDIA-SEQUENCE:"; |
605 | 605 |
606 // Additional checks for a HLS container. | 606 // Additional checks for a HLS container. |
607 static bool CheckHls(const uint8* buffer, int buffer_size) { | 607 static bool CheckHls(const uint8_t* buffer, int buffer_size) { |
608 // HLS is simply a play list used for Apple HTTP Live Streaming. | 608 // HLS is simply a play list used for Apple HTTP Live Streaming. |
609 // Reference: Apple HTTP Live Streaming Overview | 609 // Reference: Apple HTTP Live Streaming Overview |
610 // (http://goo.gl/MIwxj) | 610 // (http://goo.gl/MIwxj) |
611 | 611 |
612 if (StartsWith(buffer, buffer_size, kHlsSignature)) { | 612 if (StartsWith(buffer, buffer_size, kHlsSignature)) { |
613 // Need to find "#EXT-X-STREAM-INF:", "#EXT-X-TARGETDURATION:", or | 613 // Need to find "#EXT-X-STREAM-INF:", "#EXT-X-TARGETDURATION:", or |
614 // "#EXT-X-MEDIA-SEQUENCE:" somewhere in the buffer. Other playlists (like | 614 // "#EXT-X-MEDIA-SEQUENCE:" somewhere in the buffer. Other playlists (like |
615 // WinAmp) only have additional lines with #EXTINF | 615 // WinAmp) only have additional lines with #EXTINF |
616 // (http://en.wikipedia.org/wiki/M3U). | 616 // (http://en.wikipedia.org/wiki/M3U). |
617 int offset = strlen(kHlsSignature); | 617 int offset = strlen(kHlsSignature); |
618 while (offset < buffer_size) { | 618 while (offset < buffer_size) { |
619 if (buffer[offset] == '#') { | 619 if (buffer[offset] == '#') { |
620 if (StartsWith(buffer + offset, buffer_size - offset, kHls1) || | 620 if (StartsWith(buffer + offset, buffer_size - offset, kHls1) || |
621 StartsWith(buffer + offset, buffer_size - offset, kHls2) || | 621 StartsWith(buffer + offset, buffer_size - offset, kHls2) || |
622 StartsWith(buffer + offset, buffer_size - offset, kHls3)) { | 622 StartsWith(buffer + offset, buffer_size - offset, kHls3)) { |
623 return true; | 623 return true; |
624 } | 624 } |
625 } | 625 } |
626 ++offset; | 626 ++offset; |
627 } | 627 } |
628 } | 628 } |
629 return false; | 629 return false; |
630 } | 630 } |
631 | 631 |
632 // Checks for a MJPEG stream. | 632 // Checks for a MJPEG stream. |
633 static bool CheckMJpeg(const uint8* buffer, int buffer_size) { | 633 static bool CheckMJpeg(const uint8_t* buffer, int buffer_size) { |
634 // Reference: ISO/IEC 10918-1 : 1993(E), Annex B | 634 // Reference: ISO/IEC 10918-1 : 1993(E), Annex B |
635 // (http://www.w3.org/Graphics/JPEG/itu-t81.pdf) | 635 // (http://www.w3.org/Graphics/JPEG/itu-t81.pdf) |
636 RCHECK(buffer_size >= 16); | 636 RCHECK(buffer_size >= 16); |
637 | 637 |
638 int offset = 0; | 638 int offset = 0; |
639 int last_restart = -1; | 639 int last_restart = -1; |
640 int num_codes = 0; | 640 int num_codes = 0; |
641 while (offset + 5 < buffer_size) { | 641 while (offset + 5 < buffer_size) { |
642 // Marker codes are always a two byte code with the first byte xFF. | 642 // Marker codes are always a two byte code with the first byte xFF. |
643 RCHECK(buffer[offset] == 0xff); | 643 RCHECK(buffer[offset] == 0xff); |
644 uint8 code = buffer[offset + 1]; | 644 uint8_t code = buffer[offset + 1]; |
645 RCHECK(code >= 0xc0 || code == 1); | 645 RCHECK(code >= 0xc0 || code == 1); |
646 | 646 |
647 // Skip sequences of xFF. | 647 // Skip sequences of xFF. |
648 if (code == 0xff) { | 648 if (code == 0xff) { |
649 ++offset; | 649 ++offset; |
650 continue; | 650 continue; |
651 } | 651 } |
652 | 652 |
653 // Success if the next marker code is EOI (end of image) | 653 // Success if the next marker code is EOI (end of image) |
654 if (code == 0xd9) | 654 if (code == 0xd9) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 } | 692 } |
693 return (num_codes > 1); | 693 return (num_codes > 1); |
694 } | 694 } |
695 | 695 |
696 enum Mpeg2StartCodes { | 696 enum Mpeg2StartCodes { |
697 PROGRAM_END_CODE = 0xb9, | 697 PROGRAM_END_CODE = 0xb9, |
698 PACK_START_CODE = 0xba | 698 PACK_START_CODE = 0xba |
699 }; | 699 }; |
700 | 700 |
701 // Checks for a MPEG2 Program Stream. | 701 // Checks for a MPEG2 Program Stream. |
702 static bool CheckMpeg2ProgramStream(const uint8* buffer, int buffer_size) { | 702 static bool CheckMpeg2ProgramStream(const uint8_t* buffer, int buffer_size) { |
703 // Reference: ISO/IEC 13818-1 : 2000 (E) / ITU-T Rec. H.222.0 (2000 E). | 703 // Reference: ISO/IEC 13818-1 : 2000 (E) / ITU-T Rec. H.222.0 (2000 E). |
704 RCHECK(buffer_size > 14); | 704 RCHECK(buffer_size > 14); |
705 | 705 |
706 int offset = 0; | 706 int offset = 0; |
707 while (offset + 14 < buffer_size) { | 707 while (offset + 14 < buffer_size) { |
708 BitReader reader(buffer + offset, 14); | 708 BitReader reader(buffer + offset, 14); |
709 | 709 |
710 // Must start with pack_start_code. | 710 // Must start with pack_start_code. |
711 RCHECK(ReadBits(&reader, 24) == 1); | 711 RCHECK(ReadBits(&reader, 24) == 1); |
712 RCHECK(ReadBits(&reader, 8) == PACK_START_CODE); | 712 RCHECK(ReadBits(&reader, 8) == PACK_START_CODE); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 int pes_length = Read16(buffer + offset + 4); | 787 int pes_length = Read16(buffer + offset + 4); |
788 RCHECK(pes_length > 0); | 788 RCHECK(pes_length > 0); |
789 offset = offset + 6 + pes_length; | 789 offset = offset + 6 + pes_length; |
790 } | 790 } |
791 } | 791 } |
792 // Success as we are off the end of the buffer and liked everything | 792 // Success as we are off the end of the buffer and liked everything |
793 // in the buffer. | 793 // in the buffer. |
794 return true; | 794 return true; |
795 } | 795 } |
796 | 796 |
797 const uint8 kMpeg2SyncWord = 0x47; | 797 const uint8_t kMpeg2SyncWord = 0x47; |
798 | 798 |
799 // Checks for a MPEG2 Transport Stream. | 799 // Checks for a MPEG2 Transport Stream. |
800 static bool CheckMpeg2TransportStream(const uint8* buffer, int buffer_size) { | 800 static bool CheckMpeg2TransportStream(const uint8_t* buffer, int buffer_size) { |
801 // Spec: ISO/IEC 13818-1 : 2000 (E) / ITU-T Rec. H.222.0 (2000 E). | 801 // Spec: ISO/IEC 13818-1 : 2000 (E) / ITU-T Rec. H.222.0 (2000 E). |
802 // Normal packet size is 188 bytes. However, some systems add various error | 802 // Normal packet size is 188 bytes. However, some systems add various error |
803 // correction data at the end, resulting in packet of length 192/204/208 | 803 // correction data at the end, resulting in packet of length 192/204/208 |
804 // (https://en.wikipedia.org/wiki/MPEG_transport_stream). Determine the | 804 // (https://en.wikipedia.org/wiki/MPEG_transport_stream). Determine the |
805 // length with the first packet. | 805 // length with the first packet. |
806 RCHECK(buffer_size >= 250); // Want more than 1 packet to check. | 806 RCHECK(buffer_size >= 250); // Want more than 1 packet to check. |
807 | 807 |
808 int offset = 0; | 808 int offset = 0; |
809 int packet_length = -1; | 809 int packet_length = -1; |
810 while (buffer[offset] != kMpeg2SyncWord && offset < 20) { | 810 while (buffer[offset] != kMpeg2SyncWord && offset < 20) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
863 } | 863 } |
864 | 864 |
865 enum Mpeg4StartCodes { | 865 enum Mpeg4StartCodes { |
866 VISUAL_OBJECT_SEQUENCE_START_CODE = 0xb0, | 866 VISUAL_OBJECT_SEQUENCE_START_CODE = 0xb0, |
867 VISUAL_OBJECT_SEQUENCE_END_CODE = 0xb1, | 867 VISUAL_OBJECT_SEQUENCE_END_CODE = 0xb1, |
868 VISUAL_OBJECT_START_CODE = 0xb5, | 868 VISUAL_OBJECT_START_CODE = 0xb5, |
869 VOP_START_CODE = 0xb6 | 869 VOP_START_CODE = 0xb6 |
870 }; | 870 }; |
871 | 871 |
872 // Checks for a raw MPEG4 bitstream container. | 872 // Checks for a raw MPEG4 bitstream container. |
873 static bool CheckMpeg4BitStream(const uint8* buffer, int buffer_size) { | 873 static bool CheckMpeg4BitStream(const uint8_t* buffer, int buffer_size) { |
874 // Defined in ISO/IEC 14496-2:2001. | 874 // Defined in ISO/IEC 14496-2:2001. |
875 // However, no length ... simply scan for start code values. | 875 // However, no length ... simply scan for start code values. |
876 // Note tags are very similar to H.264. | 876 // Note tags are very similar to H.264. |
877 RCHECK(buffer_size > 4); | 877 RCHECK(buffer_size > 4); |
878 | 878 |
879 int offset = 0; | 879 int offset = 0; |
880 int sequence_start_count = 0; | 880 int sequence_start_count = 0; |
881 int sequence_end_count = 0; | 881 int sequence_end_count = 0; |
882 int visual_object_count = 0; | 882 int visual_object_count = 0; |
883 int vop_count = 0; | 883 int vop_count = 0; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
939 case VOP_START_CODE: | 939 case VOP_START_CODE: |
940 RCHECK(++vop_count <= visual_object_count); | 940 RCHECK(++vop_count <= visual_object_count); |
941 break; | 941 break; |
942 } | 942 } |
943 // Skip this block. | 943 // Skip this block. |
944 offset += 6; | 944 offset += 6; |
945 } | 945 } |
946 } | 946 } |
947 | 947 |
948 // Additional checks for a MOV/QuickTime/MPEG4 container. | 948 // Additional checks for a MOV/QuickTime/MPEG4 container. |
949 static bool CheckMov(const uint8* buffer, int buffer_size) { | 949 static bool CheckMov(const uint8_t* buffer, int buffer_size) { |
950 // Reference: ISO/IEC 14496-12:2005(E). | 950 // Reference: ISO/IEC 14496-12:2005(E). |
951 // (http://standards.iso.org/ittf/PubliclyAvailableStandards/c061988_ISO_IEC_1
4496-12_2012.zip) | 951 // (http://standards.iso.org/ittf/PubliclyAvailableStandards/c061988_ISO_IEC_1
4496-12_2012.zip) |
952 RCHECK(buffer_size > 8); | 952 RCHECK(buffer_size > 8); |
953 | 953 |
954 int offset = 0; | 954 int offset = 0; |
955 while (offset + 8 < buffer_size) { | 955 while (offset + 8 < buffer_size) { |
956 uint32 atomsize = Read32(buffer + offset); | 956 uint32_t atomsize = Read32(buffer + offset); |
957 uint32 atomtype = Read32(buffer + offset + 4); | 957 uint32_t atomtype = Read32(buffer + offset + 4); |
958 // Only need to check for ones that are valid at the top level. | 958 // Only need to check for ones that are valid at the top level. |
959 switch (atomtype) { | 959 switch (atomtype) { |
960 case TAG('f','t','y','p'): | 960 case TAG('f','t','y','p'): |
961 case TAG('p','d','i','n'): | 961 case TAG('p','d','i','n'): |
962 case TAG('m','o','o','v'): | 962 case TAG('m','o','o','v'): |
963 case TAG('m','o','o','f'): | 963 case TAG('m','o','o','f'): |
964 case TAG('m','f','r','a'): | 964 case TAG('m','f','r','a'): |
965 case TAG('m','d','a','t'): | 965 case TAG('m','d','a','t'): |
966 case TAG('f','r','e','e'): | 966 case TAG('f','r','e','e'): |
967 case TAG('s','k','i','p'): | 967 case TAG('s','k','i','p'): |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1014 288, 320, 352, 384, 416, 448, 0 }; | 1014 288, 320, 352, 384, 416, 448, 0 }; |
1015 static int kBitRateTableV1L2[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, | 1015 static int kBitRateTableV1L2[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, |
1016 192, 224, 256, 320, 384, 0 }; | 1016 192, 224, 256, 320, 384, 0 }; |
1017 static int kBitRateTableV1L3[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, | 1017 static int kBitRateTableV1L3[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, |
1018 160, 192, 224, 256, 320, 0 }; | 1018 160, 192, 224, 256, 320, 0 }; |
1019 static int kBitRateTableV2L1[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, | 1019 static int kBitRateTableV2L1[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, |
1020 160, 176, 192, 224, 256, 0 }; | 1020 160, 176, 192, 224, 256, 0 }; |
1021 static int kBitRateTableV2L23[16] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, | 1021 static int kBitRateTableV2L23[16] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, |
1022 112, 128, 144, 160, 0 }; | 1022 112, 128, 144, 160, 0 }; |
1023 | 1023 |
1024 static bool ValidMpegAudioFrameHeader(const uint8* header, | 1024 static bool ValidMpegAudioFrameHeader(const uint8_t* header, |
1025 int header_size, | 1025 int header_size, |
1026 int* framesize) { | 1026 int* framesize) { |
1027 // Reference: http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm. | 1027 // Reference: http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm. |
1028 DCHECK_GE(header_size, 4); | 1028 DCHECK_GE(header_size, 4); |
1029 *framesize = 0; | 1029 *framesize = 0; |
1030 BitReader reader(header, 4); // Header can only be 4 bytes long. | 1030 BitReader reader(header, 4); // Header can only be 4 bytes long. |
1031 | 1031 |
1032 // Verify frame sync (11 bits) are all set. | 1032 // Verify frame sync (11 bits) are all set. |
1033 RCHECK(ReadBits(&reader, 11) == 0x7ff); | 1033 RCHECK(ReadBits(&reader, 11) == 0x7ff); |
1034 | 1034 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1074 bitrate = kBitRateTableV2L23[bitrate_index]; | 1074 bitrate = kBitRateTableV2L23[bitrate_index]; |
1075 } | 1075 } |
1076 if (layer == LAYER_1) | 1076 if (layer == LAYER_1) |
1077 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4; | 1077 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4; |
1078 else | 1078 else |
1079 *framesize = (144000 * bitrate) / sampling_rate + padding; | 1079 *framesize = (144000 * bitrate) / sampling_rate + padding; |
1080 return (bitrate > 0 && sampling_rate > 0); | 1080 return (bitrate > 0 && sampling_rate > 0); |
1081 } | 1081 } |
1082 | 1082 |
1083 // Extract a size encoded the MP3 way. | 1083 // Extract a size encoded the MP3 way. |
1084 static int GetMp3HeaderSize(const uint8* buffer, int buffer_size) { | 1084 static int GetMp3HeaderSize(const uint8_t* buffer, int buffer_size) { |
1085 DCHECK_GE(buffer_size, 9); | 1085 DCHECK_GE(buffer_size, 9); |
1086 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) + | 1086 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) + |
1087 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10; | 1087 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10; |
1088 if (buffer[5] & 0x10) // Footer added? | 1088 if (buffer[5] & 0x10) // Footer added? |
1089 size += 10; | 1089 size += 10; |
1090 return size; | 1090 return size; |
1091 } | 1091 } |
1092 | 1092 |
1093 // Additional checks for a MP3 container. | 1093 // Additional checks for a MP3 container. |
1094 static bool CheckMp3(const uint8* buffer, int buffer_size, bool seenHeader) { | 1094 static bool CheckMp3(const uint8_t* buffer, int buffer_size, bool seenHeader) { |
1095 RCHECK(buffer_size >= 10); // Must be enough to read the initial header. | 1095 RCHECK(buffer_size >= 10); // Must be enough to read the initial header. |
1096 | 1096 |
1097 int framesize; | 1097 int framesize; |
1098 int numSeen = 0; | 1098 int numSeen = 0; |
1099 int offset = 0; | 1099 int offset = 0; |
1100 if (seenHeader) { | 1100 if (seenHeader) { |
1101 offset = GetMp3HeaderSize(buffer, buffer_size); | 1101 offset = GetMp3HeaderSize(buffer, buffer_size); |
1102 } else { | 1102 } else { |
1103 // Skip over leading 0's. | 1103 // Skip over leading 0's. |
1104 while (offset < buffer_size && buffer[offset] == 0) | 1104 while (offset < buffer_size && buffer[offset] == 0) |
(...skipping 10 matching lines...) Expand all Loading... |
1115 offset += framesize; | 1115 offset += framesize; |
1116 } | 1116 } |
1117 // Off the end of the buffer, return success if a few valid headers seen. | 1117 // Off the end of the buffer, return success if a few valid headers seen. |
1118 return numSeen > 2; | 1118 return numSeen > 2; |
1119 } | 1119 } |
1120 | 1120 |
1121 // Check that the next characters in |buffer| represent a number. The format | 1121 // Check that the next characters in |buffer| represent a number. The format |
1122 // accepted is optional whitespace followed by 1 or more digits. |max_digits| | 1122 // accepted is optional whitespace followed by 1 or more digits. |max_digits| |
1123 // specifies the maximum number of digits to process. Returns true if a valid | 1123 // specifies the maximum number of digits to process. Returns true if a valid |
1124 // number is found, false otherwise. | 1124 // number is found, false otherwise. |
1125 static bool VerifyNumber(const uint8* buffer, | 1125 static bool VerifyNumber(const uint8_t* buffer, |
1126 int buffer_size, | 1126 int buffer_size, |
1127 int* offset, | 1127 int* offset, |
1128 int max_digits) { | 1128 int max_digits) { |
1129 RCHECK(*offset < buffer_size); | 1129 RCHECK(*offset < buffer_size); |
1130 | 1130 |
1131 // Skip over any leading space. | 1131 // Skip over any leading space. |
1132 while (isspace(buffer[*offset])) { | 1132 while (isspace(buffer[*offset])) { |
1133 ++(*offset); | 1133 ++(*offset); |
1134 RCHECK(*offset < buffer_size); | 1134 RCHECK(*offset < buffer_size); |
1135 } | 1135 } |
1136 | 1136 |
1137 // Need to process up to max_digits digits. | 1137 // Need to process up to max_digits digits. |
1138 int numSeen = 0; | 1138 int numSeen = 0; |
1139 while (--max_digits >= 0 && isdigit(buffer[*offset])) { | 1139 while (--max_digits >= 0 && isdigit(buffer[*offset])) { |
1140 ++numSeen; | 1140 ++numSeen; |
1141 ++(*offset); | 1141 ++(*offset); |
1142 if (*offset >= buffer_size) | 1142 if (*offset >= buffer_size) |
1143 return true; // Out of space but seen a digit. | 1143 return true; // Out of space but seen a digit. |
1144 } | 1144 } |
1145 | 1145 |
1146 // Success if at least one digit seen. | 1146 // Success if at least one digit seen. |
1147 return (numSeen > 0); | 1147 return (numSeen > 0); |
1148 } | 1148 } |
1149 | 1149 |
1150 // Check that the next character in |buffer| is one of |c1| or |c2|. |c2| is | 1150 // Check that the next character in |buffer| is one of |c1| or |c2|. |c2| is |
1151 // optional. Returns true if there is a match, false if no match or out of | 1151 // optional. Returns true if there is a match, false if no match or out of |
1152 // space. | 1152 // space. |
1153 static inline bool VerifyCharacters(const uint8* buffer, | 1153 static inline bool VerifyCharacters(const uint8_t* buffer, |
1154 int buffer_size, | 1154 int buffer_size, |
1155 int* offset, | 1155 int* offset, |
1156 char c1, | 1156 char c1, |
1157 char c2) { | 1157 char c2) { |
1158 RCHECK(*offset < buffer_size); | 1158 RCHECK(*offset < buffer_size); |
1159 char c = static_cast<char>(buffer[(*offset)++]); | 1159 char c = static_cast<char>(buffer[(*offset)++]); |
1160 return (c == c1 || (c == c2 && c2 != 0)); | 1160 return (c == c1 || (c == c2 && c2 != 0)); |
1161 } | 1161 } |
1162 | 1162 |
1163 // Checks for a SRT container. | 1163 // Checks for a SRT container. |
1164 static bool CheckSrt(const uint8* buffer, int buffer_size) { | 1164 static bool CheckSrt(const uint8_t* buffer, int buffer_size) { |
1165 // Reference: http://en.wikipedia.org/wiki/SubRip | 1165 // Reference: http://en.wikipedia.org/wiki/SubRip |
1166 RCHECK(buffer_size > 20); | 1166 RCHECK(buffer_size > 20); |
1167 | 1167 |
1168 // First line should just be the subtitle sequence number. | 1168 // First line should just be the subtitle sequence number. |
1169 int offset = StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0; | 1169 int offset = StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0; |
1170 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100)); | 1170 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100)); |
1171 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, '\n', '\r')); | 1171 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, '\n', '\r')); |
1172 | 1172 |
1173 // Skip any additional \n\r. | 1173 // Skip any additional \n\r. |
1174 while (VerifyCharacters(buffer, buffer_size, &offset, '\n', '\r')) {} | 1174 while (VerifyCharacters(buffer, buffer_size, &offset, '\n', '\r')) {} |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1215 // prefix[] adds back the bits read individually. | 1215 // prefix[] adds back the bits read individually. |
1216 return ReadBits(reader, num_bits_to_read) | prefix[i]; | 1216 return ReadBits(reader, num_bits_to_read) | prefix[i]; |
1217 } | 1217 } |
1218 } | 1218 } |
1219 } | 1219 } |
1220 // Invalid encoding, return something not expected. | 1220 // Invalid encoding, return something not expected. |
1221 return -1; | 1221 return -1; |
1222 } | 1222 } |
1223 | 1223 |
1224 // Read a Matroska Unsigned Integer (VINT). | 1224 // Read a Matroska Unsigned Integer (VINT). |
1225 static uint64 GetVint(BitReader* reader) { | 1225 static uint64_t GetVint(BitReader* reader) { |
1226 // Values are coded with the leading zero bits (max 7) determining size. | 1226 // Values are coded with the leading zero bits (max 7) determining size. |
1227 // If it is an invalid coding or the end of the buffer is reached, | 1227 // If it is an invalid coding or the end of the buffer is reached, |
1228 // return something that will go off the end of the buffer. | 1228 // return something that will go off the end of the buffer. |
1229 if (reader->bits_available() >= 8) { | 1229 if (reader->bits_available() >= 8) { |
1230 int num_bits_to_read = 0; | 1230 int num_bits_to_read = 0; |
1231 for (int i = 0; i < 8; ++i) { | 1231 for (int i = 0; i < 8; ++i) { |
1232 num_bits_to_read += 7; | 1232 num_bits_to_read += 7; |
1233 if (ReadBits(reader, 1) == 1) { | 1233 if (ReadBits(reader, 1) == 1) { |
1234 if (reader->bits_available() < num_bits_to_read) | 1234 if (reader->bits_available() < num_bits_to_read) |
1235 break; | 1235 break; |
1236 return ReadBits(reader, num_bits_to_read); | 1236 return ReadBits(reader, num_bits_to_read); |
1237 } | 1237 } |
1238 } | 1238 } |
1239 } | 1239 } |
1240 // Incorrect format (more than 7 leading 0's) or off the end of the buffer. | 1240 // Incorrect format (more than 7 leading 0's) or off the end of the buffer. |
1241 // Since the return value is used as a byte size, return a value that will | 1241 // Since the return value is used as a byte size, return a value that will |
1242 // cause a failure when used. | 1242 // cause a failure when used. |
1243 return (reader->bits_available() / 8) + 2; | 1243 return (reader->bits_available() / 8) + 2; |
1244 } | 1244 } |
1245 | 1245 |
1246 // Additional checks for a WEBM container. | 1246 // Additional checks for a WEBM container. |
1247 static bool CheckWebm(const uint8* buffer, int buffer_size) { | 1247 static bool CheckWebm(const uint8_t* buffer, int buffer_size) { |
1248 // Reference: http://www.matroska.org/technical/specs/index.html | 1248 // Reference: http://www.matroska.org/technical/specs/index.html |
1249 RCHECK(buffer_size > 12); | 1249 RCHECK(buffer_size > 12); |
1250 | 1250 |
1251 BitReader reader(buffer, buffer_size); | 1251 BitReader reader(buffer, buffer_size); |
1252 | 1252 |
1253 // Verify starting Element Id. | 1253 // Verify starting Element Id. |
1254 RCHECK(GetElementId(&reader) == 0x1a45dfa3); | 1254 RCHECK(GetElementId(&reader) == 0x1a45dfa3); |
1255 | 1255 |
1256 // Get the header size, and ensure there are enough bits to check. | 1256 // Get the header size, and ensure there are enough bits to check. |
1257 int header_size = GetVint(&reader); | 1257 int header_size = GetVint(&reader); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1290 return false; | 1290 return false; |
1291 } | 1291 } |
1292 | 1292 |
1293 enum VC1StartCodes { | 1293 enum VC1StartCodes { |
1294 VC1_FRAME_START_CODE = 0x0d, | 1294 VC1_FRAME_START_CODE = 0x0d, |
1295 VC1_ENTRY_POINT_START_CODE = 0x0e, | 1295 VC1_ENTRY_POINT_START_CODE = 0x0e, |
1296 VC1_SEQUENCE_START_CODE = 0x0f | 1296 VC1_SEQUENCE_START_CODE = 0x0f |
1297 }; | 1297 }; |
1298 | 1298 |
1299 // Checks for a VC1 bitstream container. | 1299 // Checks for a VC1 bitstream container. |
1300 static bool CheckVC1(const uint8* buffer, int buffer_size) { | 1300 static bool CheckVC1(const uint8_t* buffer, int buffer_size) { |
1301 // Reference: SMPTE 421M | 1301 // Reference: SMPTE 421M |
1302 // (http://standards.smpte.org/content/978-1-61482-555-5/st-421-2006/SEC1.body
.pdf) | 1302 // (http://standards.smpte.org/content/978-1-61482-555-5/st-421-2006/SEC1.body
.pdf) |
1303 // However, no length ... simply scan for start code values. | 1303 // However, no length ... simply scan for start code values. |
1304 // Expect to see SEQ | [ [ ENTRY ] PIC* ]* | 1304 // Expect to see SEQ | [ [ ENTRY ] PIC* ]* |
1305 // Note tags are very similar to H.264. | 1305 // Note tags are very similar to H.264. |
1306 | 1306 |
1307 RCHECK(buffer_size >= 24); | 1307 RCHECK(buffer_size >= 24); |
1308 | 1308 |
1309 // First check for Bitstream Metadata Serialization (Annex L) | 1309 // First check for Bitstream Metadata Serialization (Annex L) |
1310 if (buffer[0] == 0xc5 && | 1310 if (buffer[0] == 0xc5 && |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1398 ++frame_start_code; | 1398 ++frame_start_code; |
1399 break; | 1399 break; |
1400 } | 1400 } |
1401 offset += 5; | 1401 offset += 5; |
1402 } | 1402 } |
1403 } | 1403 } |
1404 | 1404 |
1405 // For some formats the signature is a bunch of characters. They are defined | 1405 // For some formats the signature is a bunch of characters. They are defined |
1406 // below. Note that the first 4 characters of the string may be used as a TAG | 1406 // below. Note that the first 4 characters of the string may be used as a TAG |
1407 // in LookupContainerByFirst4. For signatures that contain embedded \0, use | 1407 // in LookupContainerByFirst4. For signatures that contain embedded \0, use |
1408 // uint8[]. | 1408 // uint8_t[]. |
1409 static const char kAmrSignature[] = "#!AMR"; | 1409 static const char kAmrSignature[] = "#!AMR"; |
1410 static const uint8 kAsfSignature[] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66, 0xcf, | 1410 static const uint8_t kAsfSignature[] = {0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66, |
1411 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, | 1411 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, |
1412 0xce, 0x6c }; | 1412 0x00, 0x62, 0xce, 0x6c}; |
1413 static const char kAssSignature[] = "[Script Info]"; | 1413 static const char kAssSignature[] = "[Script Info]"; |
1414 static const char kAssBomSignature[] = UTF8_BYTE_ORDER_MARK "[Script Info]"; | 1414 static const char kAssBomSignature[] = UTF8_BYTE_ORDER_MARK "[Script Info]"; |
1415 static const uint8 kWtvSignature[] = { 0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49, 0xda, | 1415 static const uint8_t kWtvSignature[] = {0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49, |
1416 0x11, 0xa6, 0x4e, 0x00, 0x07, 0xe9, 0x5e, | 1416 0xda, 0x11, 0xa6, 0x4e, 0x00, 0x07, |
1417 0xad, 0x8d }; | 1417 0xe9, 0x5e, 0xad, 0x8d}; |
1418 | 1418 |
1419 // Attempt to determine the container type from the buffer provided. This is | 1419 // Attempt to determine the container type from the buffer provided. This is |
1420 // a simple pass, that uses the first 4 bytes of the buffer as an index to get | 1420 // a simple pass, that uses the first 4 bytes of the buffer as an index to get |
1421 // a rough idea of the container format. | 1421 // a rough idea of the container format. |
1422 static MediaContainerName LookupContainerByFirst4(const uint8* buffer, | 1422 static MediaContainerName LookupContainerByFirst4(const uint8_t* buffer, |
1423 int buffer_size) { | 1423 int buffer_size) { |
1424 // Minimum size that the code expects to exist without checking size. | 1424 // Minimum size that the code expects to exist without checking size. |
1425 if (buffer_size < 12) | 1425 if (buffer_size < 12) |
1426 return CONTAINER_UNKNOWN; | 1426 return CONTAINER_UNKNOWN; |
1427 | 1427 |
1428 uint32 first4 = Read32(buffer); | 1428 uint32_t first4 = Read32(buffer); |
1429 switch (first4) { | 1429 switch (first4) { |
1430 case 0x1a45dfa3: | 1430 case 0x1a45dfa3: |
1431 if (CheckWebm(buffer, buffer_size)) | 1431 if (CheckWebm(buffer, buffer_size)) |
1432 return CONTAINER_WEBM; | 1432 return CONTAINER_WEBM; |
1433 break; | 1433 break; |
1434 | 1434 |
1435 case 0x3026b275: | 1435 case 0x3026b275: |
1436 if (StartsWith(buffer, | 1436 if (StartsWith(buffer, |
1437 buffer_size, | 1437 buffer_size, |
1438 kAsfSignature, | 1438 kAsfSignature, |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1571 buffer_size, | 1571 buffer_size, |
1572 kWtvSignature, | 1572 kWtvSignature, |
1573 sizeof(kWtvSignature))) { | 1573 sizeof(kWtvSignature))) { |
1574 return CONTAINER_WTV; | 1574 return CONTAINER_WTV; |
1575 } | 1575 } |
1576 break; | 1576 break; |
1577 } | 1577 } |
1578 | 1578 |
1579 // Now try a few different ones that look at something other | 1579 // Now try a few different ones that look at something other |
1580 // than the first 4 bytes. | 1580 // than the first 4 bytes. |
1581 uint32 first3 = first4 & 0xffffff00; | 1581 uint32_t first3 = first4 & 0xffffff00; |
1582 switch (first3) { | 1582 switch (first3) { |
1583 case TAG('C','W','S',0): | 1583 case TAG('C','W','S',0): |
1584 case TAG('F','W','S',0): | 1584 case TAG('F','W','S',0): |
1585 return CONTAINER_SWF; | 1585 return CONTAINER_SWF; |
1586 | 1586 |
1587 case TAG('I','D','3',0): | 1587 case TAG('I','D','3',0): |
1588 if (CheckMp3(buffer, buffer_size, true)) | 1588 if (CheckMp3(buffer, buffer_size, true)) |
1589 return CONTAINER_MP3; | 1589 return CONTAINER_MP3; |
1590 break; | 1590 break; |
1591 } | 1591 } |
1592 | 1592 |
1593 // Maybe the first 2 characters are something we can use. | 1593 // Maybe the first 2 characters are something we can use. |
1594 uint32 first2 = Read16(buffer); | 1594 uint32_t first2 = Read16(buffer); |
1595 switch (first2) { | 1595 switch (first2) { |
1596 case kAc3SyncWord: | 1596 case kAc3SyncWord: |
1597 if (CheckAc3(buffer, buffer_size)) | 1597 if (CheckAc3(buffer, buffer_size)) |
1598 return CONTAINER_AC3; | 1598 return CONTAINER_AC3; |
1599 if (CheckEac3(buffer, buffer_size)) | 1599 if (CheckEac3(buffer, buffer_size)) |
1600 return CONTAINER_EAC3; | 1600 return CONTAINER_EAC3; |
1601 break; | 1601 break; |
1602 | 1602 |
1603 case 0xfff0: | 1603 case 0xfff0: |
1604 case 0xfff1: | 1604 case 0xfff1: |
1605 case 0xfff8: | 1605 case 0xfff8: |
1606 case 0xfff9: | 1606 case 0xfff9: |
1607 if (CheckAac(buffer, buffer_size)) | 1607 if (CheckAac(buffer, buffer_size)) |
1608 return CONTAINER_AAC; | 1608 return CONTAINER_AAC; |
1609 break; | 1609 break; |
1610 } | 1610 } |
1611 | 1611 |
1612 // Check if the file is in MP3 format without the header. | 1612 // Check if the file is in MP3 format without the header. |
1613 if (CheckMp3(buffer, buffer_size, false)) | 1613 if (CheckMp3(buffer, buffer_size, false)) |
1614 return CONTAINER_MP3; | 1614 return CONTAINER_MP3; |
1615 | 1615 |
1616 return CONTAINER_UNKNOWN; | 1616 return CONTAINER_UNKNOWN; |
1617 } | 1617 } |
1618 | 1618 |
1619 // Attempt to determine the container name from the buffer provided. | 1619 // Attempt to determine the container name from the buffer provided. |
1620 MediaContainerName DetermineContainer(const uint8* buffer, int buffer_size) { | 1620 MediaContainerName DetermineContainer(const uint8_t* buffer, int buffer_size) { |
1621 DCHECK(buffer); | 1621 DCHECK(buffer); |
1622 | 1622 |
1623 // Since MOV/QuickTime/MPEG4 streams are common, check for them first. | 1623 // Since MOV/QuickTime/MPEG4 streams are common, check for them first. |
1624 if (CheckMov(buffer, buffer_size)) | 1624 if (CheckMov(buffer, buffer_size)) |
1625 return CONTAINER_MOV; | 1625 return CONTAINER_MOV; |
1626 | 1626 |
1627 // Next attempt the simple checks, that typically look at just the | 1627 // Next attempt the simple checks, that typically look at just the |
1628 // first few bytes of the file. | 1628 // first few bytes of the file. |
1629 MediaContainerName result = LookupContainerByFirst4(buffer, buffer_size); | 1629 MediaContainerName result = LookupContainerByFirst4(buffer, buffer_size); |
1630 if (result != CONTAINER_UNKNOWN) | 1630 if (result != CONTAINER_UNKNOWN) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1663 if (CheckEac3(buffer + offset, buffer_size - offset)) | 1663 if (CheckEac3(buffer + offset, buffer_size - offset)) |
1664 return CONTAINER_EAC3; | 1664 return CONTAINER_EAC3; |
1665 } | 1665 } |
1666 | 1666 |
1667 return CONTAINER_UNKNOWN; | 1667 return CONTAINER_UNKNOWN; |
1668 } | 1668 } |
1669 | 1669 |
1670 } // namespace container_names | 1670 } // namespace container_names |
1671 | 1671 |
1672 } // namespace media | 1672 } // namespace media |
OLD | NEW |