Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(573)

Side by Side Diff: media/base/container_names.cc

Issue 14495010: Add UMA stats for audio/video containers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "media/base/container_names.h"
6
7 #include <cctype>
8 #include <limits>
xhwang 2013/05/06 23:51:27 nit: extra line after this
jrummell 2013/05/16 23:48:01 Done.
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/metrics/sparse_histogram.h"
12
13 namespace container_names {
14
15 #define TAG(a, b, c, d) \
16 ((static_cast<uint8_t>(a) << 24) | (static_cast<uint8_t>(b) << 16) | \
xhwang 2013/05/06 23:51:27 sorry for not mentioning this earlier. we use uint
scherkus (not reviewing) 2013/05/07 00:50:20 nit: this should be a 4-space indent
jrummell 2013/05/16 23:48:01 Done.
jrummell 2013/05/16 23:48:01 Done. I just let clang-format do it's thing.
17 (static_cast<uint8_t>(c) << 8) | (static_cast<uint8_t>(d)))
18
19 // Helper function to read 2 bytes (16 bits, big endian) from a buffer.
20 static uint32_t Read16(const uint8_t* p) {
21 return p[0] << 8 | p[1];
22 }
23
24 // Helper function to read 3 bytes (24 bits, big endian) from a buffer.
25 static uint32_t Read24(const uint8_t* p) {
26 return p[0] << 16 | p[1] << 8 | p[2];
27 }
28
29 // Helper function to read 4 bytes (32 bits, big endian) from a buffer.
30 static uint32_t Read32(const uint8_t* p) {
31 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
32 }
33
34 // Helper function to do buffer comparisons without going off the end
35 // of the buffer.
36 static bool StartsWith(const uint8_t* buffer,
37 size_t buffer_size,
38 const char* search,
39 size_t search_size) {
40 return (search_size <= buffer_size &&
41 memcmp(buffer, search, search_size) == 0);
xhwang 2013/05/06 23:51:27 It looks like we always call StartWith with an C s
jrummell 2013/05/16 23:48:01 Done. However, some of the "strings" contain embed
42 }
43
44 // Comparison function used by lower_bound which returns true if the
45 // first argument is less than the second in lexicographical order.
46 static bool ContainerNameMappingComparer(const ContainerNameMapping& a,
47 const char* b) {
48 return strcasecmp(a.name, b) < 0;
49 }
50
51 // Output a container value to the histogram.
52 static void LogContainerToHistogram(FFmpegContainerName container,
53 bool is_guess) {
54 int metric = 2 * container;
55 if (is_guess)
scherkus (not reviewing) 2013/05/07 00:50:20 I don't think we're gaining much by logging both w
jrummell 2013/05/16 23:48:01 Done.
56 ++metric;
57
58 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedContainer", metric);
59 }
60
61 // For some formats the signature is a bunch of characters. They are defined
62 // below. Note that the first 4 characters of the string may be used as a TAG
63 // in LookupContainerByFirst4.
64 #define BYTE_ORDER_MARK "\xef\xbb\xbf"
65
66 static const char kAmrSignature[] = "#!AMR";
67 static const char kApcSignature[] = "CRYO_APC";
68 static const char kAsfSignature[] =
69 "\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00\xaa\x00\x62\xce\x6c";
70 static const char kAssSignature[] = "[Script Info]";
71 static const char kAssBomSignature[] = BYTE_ORDER_MARK "[Script Info]";
72 static const char kConcatSignature[] = "ffconcat version 1.0";
73 static const char kDnxhdSignature[] = "\x00\x00\x02\x80\x01";
74 static const char kFfSignature[] = ";FFMETADATA";
75 static const char kHlsSignature[] = "#EXTM3U";
76 static const char kIdfSignature[] =
77 "\x04\x31\x2e\x34\x00\x00\x00\x00\x4f\x00\x15\x00";
78 static const char kIlbcSignature[] = "#!iLBC";
79 static const char kIssSignature[] = "IMA_ADPCM_Sound";
80 static const char kIv8Signature[] = "\x01\x01\x03\xb8\x80\x60";
81 static const char kJvSignature[] = " Compression by John M Phillips Copyright "
82 "(C) 1995 The Bitmap Brothers Ltd.";
83 static const char kLibnutSignature[] = "nut/multimedia container";
84 static const char kLxfSignature[] = "LEITCH\x00\x00";
85 static const char kNuv1Signature[] = "NuppelVideo";
86 static const char kNuv2Signature[] = "MythTVVideo";
87 static const char kPafSignature[] =
88 "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a";
89 static const char kRealSignature[] = "<window";
90 static const char kRealBomSignature[] = BYTE_ORDER_MARK "<window";
91 static const char kRplSignature[] = "ARMovie\x0A";
92 static const char kSamiSignature[] = "<SAMI>";
93 static const char kSamiBomSignature[] = BYTE_ORDER_MARK "<SAMI>";
94 static const char kSmjpegSignature[] = "\x00\x0aSMJPEG";
95 static const char kVivoSignature[] = "\r\nVersion:Vivo/";
96 static const char kVobsubSignature[] = "# VobSub index file,";
97 static const char kVocSignature[] = "Creative Voice File\x1A";
98 static const char kW64Signature[] =
99 "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1\x00\x00";
100 static const char kW64Signature2[] =
101 "wave\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a";
102 static const char kWebvttSignature[] = "WEBVTT";
103 static const char kWebvttBomSignature[] = BYTE_ORDER_MARK "WEBVTT";
104 static const char kWtvSignature[] =
105 "\xb7\xd8\x00\x20\x37\x49\xda\x11\xa6\x4e\x00\x07\xe9\x5e\xad\x8d";
106 static const char kYuv4Signature[] = "YUV4MPEG2";
107
108 const int kAc3FrameSizeTable[38][3] = {
109 { 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 },
110 { 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 },
111 { 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 },
112 { 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 },
113 { 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 },
114 { 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 },
115 { 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 },
116 { 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 },
117 { 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 },
118 { 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 },
119 { 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 },
120 };
121
122 // Checks for an ADTS AAC container.
123 static bool CheckAac(const uint8_t* buffer, int buffer_size) {
124 // ADTS header is 7 or 9 bytes
125 // (from http://wiki.multimedia.cx/index.php?title=ADTS)
126 int offset = 0;
127 while (offset + 5 < buffer_size) {
128 int syncword = (Read16(buffer + offset) >> 4) & 0xfff;
129 int layer = (buffer[offset + 1] >> 1) & 0x3;
130 int frequency_index = (buffer[offset + 2] >> 2) & 0xf;
131 int size = (Read24(buffer + offset + 3) >> 5) & 0x1fff;
xhwang 2013/05/06 23:51:27 shall we make sure size>0 here? Otherwise we'll ha
jrummell 2013/05/16 23:48:01 Done.
132 if (syncword != 0xfff || layer != 0 || frequency_index == 15)
133 return false;
134 offset += size;
135 }
136 return (offset > 0);
xhwang 2013/05/06 23:51:27 In a lot of functions we are checking the "buffer_
jrummell 2013/05/16 23:48:01 Done.
137 }
138
139 // Checks for an AC3 container.
140 static bool CheckAc3(const uint8_t* buffer, int buffer_size) {
141 // AC3 container looks like syncinfo | bsi | audblk * 6 | aux | check.
142 // from spec @ http://www.atsc.org/cms/standards/A52-2012(12-17).pdf
143 int offset = 0;
144
145 while (offset + 6 < buffer_size) {
146 // Verify syncinfo (5 bytes)
147 if (Read16(buffer + offset) != 0x0b77)
148 return false;
149 int sample_rate_code = (buffer[offset + 4] >> 6) & 0x03;
150 if (sample_rate_code == 3) // reserved
151 return false;
152 int frame_size_code = buffer[offset + 4] & 0x3f;
153 if (frame_size_code >= 38)
154 return false;
155
156 // Verify bsi (no fixed alignment)
157 int bit_stream_id = (buffer[offset + 5] >> 3) & 0x1f;
158 if (bit_stream_id >= 10) // normally 8 or 6
159 return false;
160
161 offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code];
162 }
163 return (offset > 0);
164 }
165
166 // Checks for an EAC3 container (very similar to AC3)
167 static bool CheckEac3(const uint8_t* buffer, int buffer_size) {
168 // EAC3 container looks like syncinfo | bsi | audfrm | audblk* | aux | check.
169 // from spec @ http://www.atsc.org/cms/standards/A52-2012(12-17).pdf
170 int offset = 0;
171
172 while (offset + 6 < buffer_size) {
173 // Verify syncinfo (5 bytes)
174 if (Read16(buffer + offset) != 0x0b77)
175 return false;
176
177 // Verify bsi (no fixed alignment)
178 int stream_type = (buffer[offset + 2] > 6) & 0x3;
179 if (stream_type == 3)
180 return false;
181 int frame_size = ((Read16(buffer + offset + 2) & 0x7ff) + 1) * 2;
182 if (frame_size < 7)
183 return false;
184 int bit_stream_id = (buffer[offset + 5] >> 3) & 0x1f;
185 if (bit_stream_id != 16)
186 return false;
187
188 offset += frame_size;
189 }
190 return (offset > 0);
191 }
192
193 // Additional checks for an ACT container.
194 static bool CheckAct(const uint8_t* buffer, int buffer_size) {
195 if (buffer_size < 512 || Read32(buffer + 16) != 16)
196 return false;
197 // Most of the first 512 bytes should be 0.
198 for (int i = 44; i < 256; ++i)
199 if (buffer[i] != 0)
200 return false;
201 if (buffer[256] != 0x84)
202 return false;
203 for (int i = 264; i < 512; ++i)
204 if (buffer[i] != 0)
205 return false;
206 return true;
207 }
208
209 // Additional checks for an AEA container.
210 static bool CheckAea(const uint8_t* buffer, int buffer_size) {
211 if (buffer_size < 2260)
212 return false;
213 int channels = buffer[264];
214 return ((channels == 1 || channels == 2) &&
215 buffer[2048] == buffer[2259] &&
216 buffer[2049] == buffer[2258]);
217 }
218
219 // Additional checks for a BINK container.
220 static bool CheckBink(const uint8_t* buffer, int buffer_size) {
221 if (buffer_size < 36)
222 return false;
223 int frames = Read32(buffer + 8);
224 int width = Read32(buffer + 20);
225 int height = Read32(buffer + 24);
226 int fps = Read32(buffer + 28);
227 int den = Read32(buffer + 32);
228 return (frames > 0 && fps > 0 && den > 0 &&
229 (width > 0 && width <= 7680) &&
230 (height > 0 && height <= 4800));
231 }
232
233 // Additional checks for a C93 container.
234 static bool CheckC93(const uint8_t* buffer, int buffer_size) {
235 if (buffer_size < 20)
236 return false;
237 uint16_t index = 1;
238 for (int i = 0; i < 16; i += 4) {
239 if (Read16(buffer + i) != index || !buffer[i + 2] || !buffer[i + 3])
240 return false;
241 index += buffer[i + 2];
242 }
243 return true;
244 }
245
246 // Additional checks for a CDXL container.
247 static bool CheckCdxl(const uint8_t* buffer, int buffer_size) {
248 if (buffer_size < 32)
249 return false;
250 int type = buffer[0];
251 int current_size = Read32(buffer + 2);
252 int width = Read16(buffer + 14);
253 int height = Read16(buffer + 16);
254 int plane1 = buffer[18];
255 int plane2 = buffer[19];
256 int palette_size = Read16(buffer + 20);
257 int audio_size = Read16(buffer + 22);
258 int image_size = width * height * plane2 / 8;
259 return (type == 1 &&
260 palette_size <= 512 &&
261 plane1 != 0 &&
262 plane2 != 0 &&
263 width != 0 &&
264 height != 0 &&
265 current_size >= audio_size + palette_size + image_size + 32 &&
266 Read32(buffer + 24) == 0 &&
267 Read32(buffer + 28) == 0 &&
268 Read16(buffer + 10) == 0);
269 }
270
271 // Additional checks for a DNXHD container.
272 static bool CheckDnxhd(const uint8_t* buffer, int buffer_size) {
273 if (buffer_size < 42)
274 return false;
275 int height = Read16(buffer + 24);
276 int width = Read16(buffer + 26);
277 int compression = Read16(buffer + 40);
278 return (StartsWith(buffer,
279 buffer_size,
280 kDnxhdSignature,
281 sizeof(kDnxhdSignature) - 1) &&
282 height > 0 && width > 0 && compression >= 1235 &&
283 compression <= 1253);
284 }
285
286 // Additional checks for a DSICIN container.
287 static bool CheckDsicin(const uint8_t* buffer, int buffer_size) {
288 return (buffer_size > 17 &&
289 Read32(buffer + 12) == 22050 &&
290 buffer[16] == 16 &&
291 buffer[17] == 0);
292 }
293
294 // Additional checks for an IDCIN container.
295 static bool CheckIdcin(const uint8_t* buffer, int buffer_size) {
296 if (buffer_size < 20)
297 return false;
298 int width = Read32(buffer);
299 int height = Read32(buffer + 4);
300 int rate = Read32(buffer + 8);
301 int bytes = Read32(buffer + 12);
302 int channels = Read32(buffer + 16);
303 return (width > 0 && width <= 1024 &&
304 height > 0 && height <= 1024 &&
305 rate >= 8000 && rate <= 48000 &&
306 bytes >= 0 && bytes <= 2 &&
307 channels >= 0 && channels <= 2);
308 }
309
310 static const char kHls1[] = "#EXT-X-STREAM-INF:";
311 static const char kHls2[] = "#EXT-X-TARGETDURATION:";
312 static const char kHls3[] = "#EXT-X-MEDIA-SEQUENCE:";
313
314 // Additional checks for a HLS container.
315 static bool CheckHls(const uint8_t* buffer, int buffer_size) {
316 if (StartsWith(buffer,
317 buffer_size,
318 kHlsSignature,
319 sizeof(kHlsSignature) - 1)) {
320 // Need to find "#EXT-X-STREAM-INF:", "#EXT-X-TARGETDURATION:",
321 // or "#EXT-X-MEDIA-SEQUENCE:" somewhere in the buffer
322 int offset = sizeof(kHlsSignature) - 1;
323 while (offset < buffer_size) {
324 if (buffer[offset] == '#') {
325 if (StartsWith(buffer + offset,
326 buffer_size - offset,
327 kHls1,
328 sizeof(kHls1) - 1) ||
329 StartsWith(buffer + offset,
330 buffer_size - offset,
331 kHls2,
332 sizeof(kHls2) - 1) ||
333 StartsWith(buffer + offset,
334 buffer_size - offset,
335 kHls3,
336 sizeof(kHls3) - 1))
337 return true;
338 }
339 ++offset;
340 }
341 }
342 return false;
343 }
344
345 // Checks for a LOAS container.
346 static bool CheckLoas(const uint8_t* buffer, int buffer_size) {
347 // LOAS header is 3 bytes.
348 // (from ISO/IEC 14496-3:2005, page 51)
349 int offset = 0;
350 while (offset + 3 < buffer_size) {
351 int header = Read24(buffer);
352 int syncword = (header >> 13) & 0x7ff;
353 int audio_length = (header & 0x1fff);
354 if (syncword != 0x2b7 || audio_length < 4)
355 return false;
356 offset += audio_length + 3 /* header */;
357 }
358 return (offset > 0);
359 }
360
361 #define VISUAL_OBJECT_SEQUENCE_START_CODE 0xb0
362 #define VISUAL_OBJECT_SEQUENCE_END_CODE 0xb1
363 #define VISUAL_OBJECT_START_CODE 0xb5
364 #define VOP_START_CODE 0xb6
365
366 // Checks for a M4V (raw MPEG4) container.
367 static bool CheckM4v(const uint8_t* buffer, int buffer_size) {
368 // Defined in ISO/IEC 14496-2:2001.
369 // However, no length ... simply scan for start code values
370 // Expect to see SEQ | VO1 | VOL* | VO2 ...
371 int offset = 0;
372 int sequence_start_count = 0;
373 int sequence_end_count = 0;
374 int visual_object_count = 0;
375 int vop_count = 0;
376 while (offset + 4 < buffer_size) {
377 int start_code = Read24(buffer + offset);
378 if (start_code == 1) {
379 // Fail if it is a reserved value.
380 if (buffer[offset] >= 0x30 && buffer[offset] <= 0xaf)
381 return false;
382 if (buffer[offset] >= 0xb7 && buffer[offset] <= 0xb9)
383 return false;
384
385 switch (buffer[offset]) {
386 case VISUAL_OBJECT_SEQUENCE_START_CODE:
387 ++sequence_start_count;
388 break;
389 case VISUAL_OBJECT_SEQUENCE_END_CODE:
390 if (++sequence_end_count > sequence_start_count)
391 return false;
392 break;
393 case VISUAL_OBJECT_START_CODE:
394 ++visual_object_count;
395 break;
396 case VOP_START_CODE:
397 if (++vop_count > visual_object_count)
398 return false;
399 break;
400 }
401 offset += 4;
402 }
403 else {
404 // Start codes can start on any byte boundary
405 ++offset;
406 }
407 }
408 // Not a complete sequence in memory, so return true if we've seen a
409 // visual_object_sequence_start_code and a visual_object_start_code.
410 return (sequence_start_count > 0 && visual_object_count > 0);
411 }
412
413 // Additional checks for a MM container.
414 static bool CheckMm(const uint8_t* buffer, int buffer_size) {
415 int length = Read32(buffer + 2);
416 if (length < 0 || buffer_size < length + 2)
417 return false;
418 int fps = Read16(buffer + 8);
419 int width = Read16(buffer + 12);
420 int height = Read16(buffer + 14);
421 int type = Read16(buffer + length);
422 return ((length == 22 || length == 24) &&
423 fps > 0 && fps <= 60 &&
424 width > 0 && width <= 2048 &&
425 height > 0 && height <= 2048 &&
426 type > 0 && type < 50);
427 }
428
429 // Additional checks for a MOV container.
430 static bool CheckMov(const uint8_t* buffer, int buffer_size) {
431 int offset = 0;
432 while (offset + 16 < buffer_size) {
433 int atomsize = Read32(buffer + offset);
434 uint32_t atomtype = Read32(buffer + offset + 4);
435 // Valid atoms from http://www.mp4ra.org/atoms.html
436 switch (atomtype) {
437 case TAG('a','i','n','f'):
438 case TAG('a','v','c','n'):
439 case TAG('b','l','o','c'):
440 case TAG('b','p','c','c'):
441 case TAG('b','u','f','f'):
442 case TAG('b','x','m','l'):
443 case TAG('c','c','i','d'):
444 case TAG('c','d','e','f'):
445 case TAG('c','m','a','p'):
446 case TAG('c','o','6','4'):
447 case TAG('c','o','l','r'):
448 case TAG('c','r','h','d'):
449 case TAG('c','s','l','g'):
450 case TAG('c','t','t','s'):
451 case TAG('c','v','r','u'):
452 case TAG('d','i','n','f'):
453 case TAG('d','r','e','f'):
454 case TAG('d','s','g','d'):
455 case TAG('d','s','t','g'):
456 case TAG('e','d','t','s'):
457 case TAG('e','l','s','t'):
458 case TAG('f','e','c','i'):
459 case TAG('f','e','c','r'):
460 case TAG('f','i','i','n'):
461 case TAG('f','i','r','e'):
462 case TAG('f','p','a','r'):
463 case TAG('f','r','e','e'):
464 case TAG('f','r','m','a'):
465 case TAG('f','t','y','p'):
466 case TAG('g','i','t','n'):
467 case TAG('g','r','p','i'):
468 case TAG('h','d','l','r'):
469 case TAG('h','m','h','d'):
470 case TAG('h','p','i','x'):
471 case TAG('i','c','n','u'):
472 case TAG('I','D','3','2'):
473 case TAG('i','d','a','t'):
474 case TAG('i','h','d','r'):
475 case TAG('i','i','n','f'):
476 case TAG('i','l','o','c'):
477 case TAG('i','m','i','f'):
478 case TAG('i','n','f','u'):
479 case TAG('i','o','d','s'):
480 case TAG('i','p','h','d'):
481 case TAG('i','p','m','c'):
482 case TAG('i','p','r','o'):
483 case TAG('i','r','e','f'):
484 case TAG('j','P',' ',' '):
485 case TAG('j','p','2','c'):
486 case TAG('j','p','2','h'):
487 case TAG('j','p','2','i'):
488 case TAG('l','r','c','u'):
489 case TAG('m','7','h','d'):
490 case TAG('m','d','a','t'):
491 case TAG('m','d','h','d'):
492 case TAG('m','d','i','a'):
493 case TAG('m','d','r','i'):
494 case TAG('m','e','c','o'):
495 case TAG('m','e','h','d'):
496 case TAG('m','e','r','e'):
497 case TAG('m','e','t','a'):
498 case TAG('m','f','h','d'):
499 case TAG('m','f','r','a'):
500 case TAG('m','f','r','o'):
501 case TAG('m','i','n','f'):
502 case TAG('m','j','h','d'):
503 case TAG('m','o','o','f'):
504 case TAG('m','o','o','v'):
505 case TAG('m','v','c','g'):
506 case TAG('m','v','c','i'):
507 case TAG('m','v','e','x'):
508 case TAG('m','v','h','d'):
509 case TAG('m','v','r','a'):
510 case TAG('n','m','h','d'):
511 case TAG('o','c','h','d'):
512 case TAG('o','d','a','f'):
513 case TAG('o','d','d','a'):
514 case TAG('o','d','h','d'):
515 case TAG('o','d','h','e'):
516 case TAG('o','d','r','b'):
517 case TAG('o','d','r','m'):
518 case TAG('o','d','t','t'):
519 case TAG('o','h','d','r'):
520 case TAG('p','a','d','b'):
521 case TAG('p','a','e','n'):
522 case TAG('p','c','l','r'):
523 case TAG('p','d','i','n'):
524 case TAG('p','i','t','m'):
525 case TAG('r','e','s',' '):
526 case TAG('r','e','s','c'):
527 case TAG('r','e','s','d'):
528 case TAG('s','b','g','p'):
529 case TAG('s','c','h','i'):
530 case TAG('s','c','h','m'):
531 case TAG('s','d','e','p'):
532 case TAG('s','d','h','d'):
533 case TAG('s','d','t','p'):
534 case TAG('s','d','v','p'):
535 case TAG('s','e','g','r'):
536 case TAG('s','e','n','c'):
537 case TAG('s','g','p','d'):
538 case TAG('s','i','d','x'):
539 case TAG('s','i','n','f'):
540 case TAG('s','k','i','p'):
541 case TAG('s','m','h','d'):
542 case TAG('s','r','m','b'):
543 case TAG('s','r','m','c'):
544 case TAG('s','r','p','p'):
545 case TAG('s','t','b','l'):
546 case TAG('s','t','c','o'):
547 case TAG('s','t','d','p'):
548 case TAG('s','t','h','d'):
549 case TAG('s','t','s','c'):
550 case TAG('s','t','s','d'):
551 case TAG('s','t','s','h'):
552 case TAG('s','t','s','s'):
553 case TAG('s','t','s','z'):
554 case TAG('s','t','t','s'):
555 case TAG('s','t','y','p'):
556 case TAG('s','t','z','2'):
557 case TAG('s','u','b','s'):
558 case TAG('s','w','t','c'):
559 case TAG('t','f','a','d'):
560 case TAG('t','f','h','d'):
561 case TAG('t','f','m','a'):
562 case TAG('t','f','r','a'):
563 case TAG('t','i','b','r'):
564 case TAG('t','i','r','i'):
565 case TAG('t','k','h','d'):
566 case TAG('t','r','a','f'):
567 case TAG('t','r','a','k'):
568 case TAG('t','r','e','f'):
569 case TAG('t','r','e','x'):
570 case TAG('t','r','g','r'):
571 case TAG('t','r','i','k'):
572 case TAG('t','r','u','n'):
573 case TAG('u','d','t','a'):
574 case TAG('u','i','n','f'):
575 case TAG('U','I','T','S'):
576 case TAG('u','l','s','t'):
577 case TAG('u','r','l',' '):
578 case TAG('u','u','i','d'):
579 case TAG('v','m','h','d'):
580 case TAG('v','w','d','i'):
581 case TAG('x','m','l',' '):
582 case TAG('C','o','d','e'):
583 case TAG('a','l','b','m'):
584 case TAG('a','n','g','l'):
585 case TAG('a','u','t','h'):
586 case TAG('c','l','f','n'):
587 case TAG('c','l','i','d'):
588 case TAG('c','l','s','f'):
589 case TAG('c','m','i','d'):
590 case TAG('c','m','n','m'):
591 case TAG('c','o','l','l'):
592 case TAG('c','p','r','t'):
593 case TAG('d','a','t','e'):
594 case TAG('d','s','c','p'):
595 case TAG('g','n','r','e'):
596 case TAG('h','n','t','i'):
597 case TAG('k','y','w','d'):
598 case TAG('l','o','c','i'):
599 case TAG('m','a','n','u'):
600 case TAG('m','o','d','l'):
601 case TAG('p','e','r','f'):
602 case TAG('r','e','e','l'):
603 case TAG('r','t','n','g'):
604 case TAG('s','c','e','n'):
605 case TAG('s','h','o','t'):
606 case TAG('s','l','n','o'):
607 case TAG('s','t','r','k'):
608 case TAG('t','h','m','b'):
609 case TAG('t','s','e','l'):
610 case TAG('t','i','t','l'):
611 case TAG('u','r','a','t'):
612 case TAG('y','r','r','c'):
613 case TAG('c','l','i','p'):
614 case TAG('c','r','g','n'):
615 case TAG('c','t','a','b'):
616 case TAG('d','c','f','D'):
617 case TAG('e','l','n','g'):
618 case TAG('i','m','a','p'):
619 case TAG('k','m','a','t'):
620 case TAG('l','o','a','d'):
621 case TAG('m','a','t','t'):
622 case TAG('p','n','o','t'):
623 case TAG('w','i','d','e'):
624 break;
625 default:
626 return false;
627 }
628 if (atomsize <= 0)
629 break; // indicates the last atom or length too big
630 if (atomsize == 1) {
631 // Indicates that the length is the next 64bits.
632 if (Read32(buffer + offset + 8) != 0)
633 break; // offset is way past buffer size
634 atomsize = Read32(buffer + offset + 12);
635 }
636 offset += atomsize;
637 }
638 return (offset > 0);
639 }
640
641 enum MPEGVersion {
642 Version25 = 0,
643 v_reserved,
644 Version2,
645 Version1
646 };
647 enum MPEGlayer {
648 l_reserved = 0,
649 Layer3,
650 Layer2,
651 Layer1
652 };
653
654 static int kSampleRateTable[4][4] = { { 11025, 12000, 8000, 0 }, // v2.5
655 { 0, 0, 0, 0 }, // not used
656 { 22050, 24000, 16000, 0 }, // v2
657 { 44100, 48000, 32000, 0 } // v1
658 };
659
660 static int kBitRateTableV1L1[16] = { 0, 32, 64, 96, 128, 160, 192, 224, 256,
661 288, 320, 352, 384, 416, 448, 0 };
662 static int kBitRateTableV1L2[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160,
663 192, 224, 256, 320, 384, 0 };
664 static int kBitRateTableV1L3[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128,
665 160, 192, 224, 256, 320, 0 };
666 static int kBitRateTableV2L1[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
667 160, 176, 192, 224, 256, 0 };
668 static int kBitRateTableV2L23[16] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,
669 112, 128, 144, 160, 0 };
670
671 static bool ValidMpegAudioFrameHeader(uint32_t header, int* framesize) {
672 *framesize = 0;
673 // first 11 bits must be all set
674 if ((header & 0xffe00000) != 0xffe00000)
675 return false;
676 // version (bits 20-19) can not be 01
677 int version = (header >> 19) & 0x3;
678 if (version == 1)
679 return false;
680 // layer (bits 18-17) can not be 00
681 int layer = (header >> 17) & 0x3;
682 if (layer == 0)
683 return false;
684 // bitrate (bits 15-12) can not be 1111
685 int bitrate_index = (header >> 12) & 0xf;
686 if (bitrate_index == 0xf)
687 return false;
688 // sampling (bits 11-10) can not be 11
689 int sampling_index = (header >> 10) & 0x3;
690 if (sampling_index == 3)
691 return false;
692
693 // Frame size:
694 // For Layer I files = (12 * BitRate / SampleRate + Padding) * 4
695 // For others = 144 * BitRate / SampleRate + Padding
696 // Unfortunately, BitRate and SampleRate are coded
697 int padding = (header >> 9) & 0x1;
698 int sampling_rate = kSampleRateTable[version][sampling_index];
699 int bitrate;
700 if (version == Version1) {
701 if (layer == Layer1)
702 bitrate = kBitRateTableV1L1[bitrate_index];
703 else if (layer == Layer2)
704 bitrate = kBitRateTableV1L2[bitrate_index];
705 else
706 bitrate = kBitRateTableV1L3[bitrate_index];
707 }
708 else {
709 if (layer == Layer1)
710 bitrate = kBitRateTableV2L1[bitrate_index];
711 else
712 bitrate = kBitRateTableV2L23[bitrate_index];
713 }
714 if (layer == Layer1)
715 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4;
716 else
717 *framesize = (144000 * bitrate) / sampling_rate + padding;
718 return (bitrate > 0 && sampling_rate > 0);
719 }
720
721 // Extract a size encoded the MP3 way
722 static int GetMp3HeaderSize(const uint8_t* buffer) {
723 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) +
724 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10;
725 if (buffer[5] & 0x10) // footer added?
726 size += 10;
727 return size;
728 }
729
730 // Additional checks for a MP3 container.
731 static bool CheckMp3(const uint8_t* buffer, int buffer_size, bool seenHeader) {
732 if (buffer_size < 10)
733 return false;
734 int framesize;
735 int numSeen = 0;
736 int offset = 0;
737 if (seenHeader)
738 offset = GetMp3HeaderSize(buffer);
739 while (offset + 3 <= buffer_size &&
740 ValidMpegAudioFrameHeader(Read32(buffer + offset), &framesize)) {
741 ++numSeen;
742 offset += framesize;
743 }
744 return (numSeen > 10 || offset >= buffer_size);
745 }
746
747 // Extract an encoded MPC size, returning the value and
748 // the number of characters used by the size.
749 static int64_t GetMpc8HeaderSize(const uint8_t* buffer, int* headerSize) {
750 int64_t size = 0;
751 uint8_t c;
752 *headerSize = 0;
753 do {
754 c = *buffer++;
755 if (++(*headerSize) > 10)
756 return -1;
757 size = (size << 7) | (c & 0x7f);
758 } while (c & 0x80);
759 return size;
760 }
761
762 // Additional checks for a MPC8 container.
763 static bool CheckMpc8(const uint8_t* buffer, int buffer_size) {
764 int offset = 4;
765 while (offset + 14 < buffer_size) {
766 if (!isupper(buffer[offset]) || !isupper(buffer[offset + 1]))
767 return false;
768
769 int size_characters;
770 int64_t size = GetMpc8HeaderSize(buffer + offset, &size_characters);
771 if (size < 2)
772 return false;
773 if (buffer[offset] == 'S' && buffer[offset + 1] == 'H') {
774 return (size >= 11 && size <= 28 &&
775 Read32(buffer + offset + size_characters + 2) != 0);
776 }
777 offset = offset + size + size_characters;
778 }
779 return false;
780 }
781
782 // Additional checks for a MSNWCTCP container.
783 static bool CheckMsnwctcp(const uint8_t* buffer, int buffer_size) {
784 // The FFmpeg probe scans through the complete buffer looking for
785 // a matching header. This code only looks for it at the beginning
786 // of the buffer.
787 if (buffer_size < 16)
788 return false;
789 int width = Read16(buffer + 2);
790 int height = Read16(buffer + 4);
791 if (!(width == 320 && height == 240) && !(width == 160 && height == 120))
792 return false;
793 return (Read32(buffer + 12) != TAG('M','L','2','0'));
794 }
795
796 // Additional checks for a MTV container.
797 static bool CheckMtv(const uint8_t* buffer, int buffer_size) {
798 if (buffer_size < 58)
799 return false;
800 int bpp = Read16(buffer + 50);
801 if (bpp != 16)
802 return false;
803 int width = Read16(buffer + 52);
804 int height = Read16(buffer + 54);
805 int segment_size = Read16(buffer + 56);
806 return ((width != 0 && height != 0) || (segment_size != 0));
807 }
808
809 // Additional checks for a NC container.
810 static bool CheckNc(const uint8_t *buffer, int buffer_size) {
811 if (buffer_size < 7)
812 return false;
813 int size = Read16(buffer + 5);
814 return (size + 20 < buffer_size && Read32(buffer + size + 16) == 0x1a5);
815 }
816
817 // Additional checks for a NSV container, only if the header isn't
818 // at the start of the file.
819 static bool CheckNsv(const uint8_t* buffer, int buffer_size) {
820 // Get the chunk size and check if at the end we are getting 0xBEEF
821 if (buffer_size < 24)
822 return false;
823 size_t vsize = Read24(buffer + 19) >> 4;
824 size_t asize = Read16(buffer + 22);
825 int offset = 24 + asize + vsize;
826 return (offset + 2 <= buffer_size && Read16(buffer + offset) == 0xbeef);
827 }
828
829 // Additional checks for an OMA container.
830 static bool CheckOma(const uint8_t* buffer, int buffer_size) {
831 if (buffer_size < 10)
832 return false;
833 if (buffer[4] != 0)
834 return false;
835 int tag_len = GetMp3HeaderSize(buffer);
836 if (tag_len + 6 > buffer_size)
837 return false;
838 return (Read24(buffer + tag_len) == TAG(0,'E','A','3') &&
839 buffer[tag_len + 4] == 0 && buffer[tag_len + 5] == 96);
840 }
841
842 // Additional checks for a PJS container.
843 static bool CheckPjs(const uint8_t* buffer, int buffer_size) {
844 // Movie subtitles file created in the Phoenix Japanimation Society (JPS)
845 // format. It uses a simple text format like:
846 // 1234, 5678, "My subtitle."
847 // Already checked for the 2 numbers, now verify a quoted string exists
848 int offset = 0;
849 // search for starting "
850 while (offset < buffer_size && buffer[offset] != '"') {
851 if (buffer[offset] == '\n' || buffer[offset] == '\r')
852 return false;
853 ++offset;
854 }
855 ++offset;
856 if (offset > buffer_size) // no starting "
857 return false;
858 while (offset < buffer_size && buffer[offset] != '"') {
859 if (buffer[offset] == '\n' || buffer[offset] == '\r')
860 return false;
861 ++offset;
862 }
863 return (offset < buffer_size);
864 }
865
866 // Additional checks for a PVA container.
867 static bool CheckPva(const uint8_t* buffer, int buffer_size) {
868 if (buffer_size < 8)
869 return false;
870 int length = Read16(buffer + 6);
871 if (buffer[4] != 0x55 || (buffer[5] & 0xe0) != 0 || length > 6136)
872 return false;
873 if (buffer_size < length + 8)
874 return false;
875 return (buffer[length] == 'A' &&
876 buffer[length + 1] == 'V' &&
877 (buffer[length + 2] == 1 || buffer[length + 2] == 2) &&
878 buffer[length + 4] == 0x55 &&
879 (buffer[5] & 0xe0) == 0 &&
880 Read16(buffer + length + 6) <= 6136);
881 }
882
883 // Additional checks for a TIERTEXSEQ container.
884 static bool CheckTiertexseq(const uint8_t* buffer, int buffer_size) {
885 // No real header, but first 256 bytes are always 0
886 if (buffer_size < 257)
887 return false;
888 for (int i = 0; i < 256; ++i)
889 if (buffer[i] != 0)
890 return false;
891 return (buffer[256] != 0 && buffer[257] != 0);
892 }
893
894 // Additional checks for a TMV container.
895 static bool CheckTmv(const uint8_t* buffer, int buffer_size) {
896 return (buffer_size > 10 &&
897 Read16(buffer + 4) >= 5000 && // sample rate
898 Read16(buffer + 6) >= 41 && // audio size
899 buffer[8] == 0 && // compression method
900 buffer[9] != 0 && // width
901 buffer[10] != 0); // height
902 }
903
904 // Additional checks for a VIVO container.
905 static bool CheckVivo(const uint8_t *buffer, int buffer_size) {
906 if (buffer_size < 3)
907 return false;
908 if (buffer[0] != 0)
909 return false;
910 int length = buffer[1] & 0x7f;
911 int offset = 2;
912 if ((buffer[1] & 0x80) != 0) {
913 if (buffer[2] & 0x80)
914 return false;
915 length = (length << 7) + (buffer[2] & 0x7f);
916 offset = 3;
917 }
918 if (length < 21 || length > 1024 || offset + 16 > buffer_size)
919 return false;
920 return (StartsWith(buffer + offset,
921 buffer_size - offset,
922 kVivoSignature,
923 sizeof(kVivoSignature) - 1) &&
924 buffer[offset + 16] >= '0' && buffer[offset + 16] <= '2');
925 }
926
927 // Additional checks for a VMD container.
928 static bool CheckVmd(const uint8_t* buffer, int buffer_size) {
929 if (buffer_size < 16)
930 return false;
931 int width = Read16(buffer + 12);
932 int height = Read16(buffer + 14);
933 return (width > 0 && width <= 2048 && height > 0 && height <= 2048);
934 }
935
936 // Additional checks for a VQF container.
937 static bool CheckVqf(const uint8_t* buffer, int buffer_size) {
938 return (buffer_size > 4 &&
939 (StartsWith(buffer + 4, buffer_size - 4, "97012000", 8) ||
940 StartsWith(buffer + 4, buffer_size - 4, "00052200", 8)));
941 }
942
943 // Read a Matroska TAG, updating offset to point past it.
944 static int GetVtag(const uint8_t* buffer, int buffer_size, int* offset) {
945 // Size of the tag is determined the same way as VINT,
946 // but the bit is not removed. Maximum of 4 bytes.
947 if (*offset >= buffer_size)
948 return -1;
949 int remaining = buffer_size - *offset;
950 int result = buffer[*offset];
951 if ((result & 0x80) != 0) {
952 // It is a one byte tag
953 ++*offset;
954 }
955 else if ((result & 0x40) != 0 && remaining >= 2) {
956 // It is a 2 byte tag
957 result = Read16(buffer + *offset);
958 *offset += 2;
959 }
960 else if ((result & 0x20) != 0 && remaining >= 3) {
961 // It is a 3 byte tag
962 result = Read24(buffer + *offset);
963 *offset += 3;
964 }
965 else if (remaining >= 4) {
966 // It is a 4 byte tag
967 result = Read32(buffer + *offset);
968 *offset += 4;
969 }
970 return result;
971 }
972
973 // Read a Matroska VINT, updating offset to point past it.
974 static int GetVint(const uint8_t* buffer, int buffer_size, int* offset) {
975 // Length = 1 + [number_of_leading_zero_bits].
976 if (*offset >= buffer_size) {
977 // return something big so it goes off the end of the buffer
978 return buffer_size;
979 }
980 int remaining = buffer_size - *offset;
981 int size = 1;
982 int mask = 0x80;
983 uint8_t b = buffer[*offset];
984 while (mask > 0 && (b & mask) == 0) {
985 ++size;
986 mask = mask >> 1;
987 }
988
989 // Now that we know the size, use the remaining bits plus
990 // following bytes to get the value.
991 if (size > remaining)
992 return buffer_size;
993 int result = buffer[(*offset)++] & (mask - 1);
994 while (--size > 0)
995 result = (result << 8) | buffer[(*offset)++];
996 return result;
997 }
998
999 // Additional checks for a WEBM container.
1000 static bool CheckWebm(const uint8_t* buffer, int buffer_size) {
1001 if (buffer_size < 12)
1002 return false;
1003 int offset = 4;
1004 int header_size = GetVint(buffer, buffer_size, &offset);
1005 int lastoffset = offset + header_size;
1006 if (lastoffset > buffer_size)
1007 return false;
1008 while (offset < lastoffset) {
1009 int tag = GetVtag(buffer, buffer_size, &offset);
1010 int tagsize = GetVint(buffer, buffer_size, &offset);
1011 switch (tag) {
1012 case 0x4286: // EBMLVersion
1013 case 0x42f7: // EBMLReadVersion
1014 case 0x42f2: // EBMLMaxIdLength
1015 case 0x42f3: // EBMLMaxSizeLength
1016 case 0x4287: // DocTypeVersion
1017 case 0x4285: // DocTypeReadVersion
1018 case 0xec: // void
1019 case 0xbf: // CRC32
1020 offset += tagsize;
1021 break;
1022 case 0x4282: // EBMLDocType
1023 return (
1024 (tagsize >= 4 &&
1025 offset < buffer_size &&
1026 StartsWith(buffer + offset, buffer_size - offset, "webm", 4)) ||
1027 (tagsize >= 8 &&
1028 offset < buffer_size &&
1029 StartsWith(
1030 buffer + offset, buffer_size - offset, "matroska", 8)));
1031 default:
1032 // Unrecognized tag
1033 return false;
1034 }
1035 }
1036 return false;
1037 }
1038
1039 // Additional checks for a WSAUD container.
1040 static bool CheckWsaud(const uint8_t* buffer, int buffer_size) {
1041 if (buffer_size < 20)
1042 return false;
1043 int sample_rate = Read16(buffer);
1044 if (sample_rate < 8000 || sample_rate > 48000)
1045 return false;
1046 if ((buffer[10] & 0xfc) != 0 || (buffer[11] != 1 && buffer[11] != 99))
1047 return false;
1048 return (Read32(buffer + 16) == 0x0000deaf);
1049 }
1050
1051 // Additional checks for a XA container.
1052 static bool CheckXa(const uint8_t* buffer, int buffer_size) {
1053 if (buffer_size < 14)
1054 return false;
1055 int channels = Read16(buffer + 10);
1056 if (channels < 1 || channels > 8)
1057 return false;
1058 int srate = Read16(buffer + 12);
1059 if (srate < 1 || srate > 192000)
1060 return false;
1061 int bits_per_sample = Read16(buffer + 10);
1062 return (bits_per_sample >= 4 || bits_per_sample <= 32);
1063 }
1064
1065 // Additional checks for a XBIN container.
1066 static bool CheckXbin(const uint8_t* buffer, int buffer_size) {
1067 if (buffer_size < 10)
1068 return false;
1069 int x = Read16(buffer + 5);
1070 return (buffer[4] == 0x1a &&
1071 x > 0 && x <= 160 &&
1072 buffer[9] > 0 && buffer[9] <= 32);
1073 }
1074
1075 // Additional checks for a XMV container.
1076 static bool CheckXmv(const uint8_t* buffer, int buffer_size) {
1077 if (buffer_size < 18)
1078 return false;
1079 int version = Read16(buffer + 16);
1080 return (Read32(buffer + 12) == TAG('x','o','b','X') &&
1081 version > 0 && version <= 4);
1082 }
1083
1084 // Additional checks for a YOP container.
1085 static bool CheckYop(const uint8_t* buffer, int buffer_size) {
1086 return (buffer_size > 20 &&
1087 buffer[2] < 10 &&
1088 buffer[3] < 10 &&
1089 buffer[6] != 0 &&
1090 buffer[7] != 0 &&
1091 (buffer[8] & 1) == 0 &&
1092 (buffer[10] & 1) == 0 &&
1093 Read16(buffer + 18) >= 920 &&
1094 Read16(buffer + 18) <
1095 static_cast<uint32_t>(buffer[12] * 3 + 4 + buffer[7] * 2048));
1096 }
1097
1098 // Attempt to determine the container type from the buffer provided. This is
1099 // a simple pass, that uses the first 4 bytes of the buffer as an index to get
1100 // a rough idea of the container format. It covers the following containers
1101 // (those with * are not fully covered):
1102 // 4xm, act, aea, aiff, amr, anm, apc, ape, aqtitle, asf, ass, ast, au, avi,
1103 // avr, avs, bethsoftvid, bfi, bink, bit, brstm, c93, caff, cdxl, concat,
1104 // dfa, dnxhd, dsicin, dtshd, dxa, ea, epaf, ffm, ffmetadata, file_cpk, flac,
1105 // flic, flv, frm, gif, gxf, hls, ico, idf, iff, ilbc, ircam, iss, iv8, ivf,
1106 // jv, libnut, lmlm4, lvf, lxf, mgsts, mm, mmf, mov, mp3*, mpc, mpc8,
1107 // msnwctcp*, mtv, mv, nc, nistsphere, nsv*, nut, nuv, ogg, oma, paf, pmp,
1108 // pva, pvf, qcp, r3d, realtext, rl2, rm, roq, rpl, sami, siff, smjpeg, smk,
1109 // smush, sol, sox, swf, tak, thp, tiertexseq, tmv, tta, txd, vc1test, vivo,
1110 // vmd, vobsub, voc, vqf, w64, wav, wc3movie, webm, webvtt, wsaud, wsvqa,
1111 // wtv, wv, xa, xbin, xmv, xwma, yop, yuv4mpegpipe
1112 static FFmpegContainerName LookupContainerByFirst4(const uint8_t* buffer,
1113 int buffer_size) {
1114 // Minimum size that the code expects to exist without checking size.
1115 if (buffer_size < 12)
1116 return CONTAINER_UNKNOWN;
1117
1118 uint32_t first4 = Read32(buffer);
1119 uint32_t second4 = Read32(buffer + 4);
1120 uint32_t third4 = Read32(buffer + 8);
1121
1122 switch (first4) {
1123 case 0:
1124 if (buffer_size > 16 &&
1125 Read16(buffer + 4) == 0x1bc &&
1126 Read32(buffer + 10) == 0 &&
1127 Read16(buffer + 14) == 0xe1e2)
1128 return CONTAINER_GXF;
1129 if (CheckMm(buffer, buffer_size))
1130 return CONTAINER_MM;
1131 if (second4 > 0 && second4 <= 1024 * 1024 && Read24(buffer + 8) == 1)
1132 return CONTAINER_LMLM4;
1133 if (CheckTiertexseq(buffer, buffer_size))
1134 return CONTAINER_TIERTEXSEQ;
1135 break;
1136 case 1:
1137 if (second4 > 0 && second4 <= 1024 * 1024 && Read24(buffer + 8) == 1)
1138 return CONTAINER_LMLM4;
1139 if (Read16(buffer + 4) != 0)
1140 return CONTAINER_ICO;
1141 break;
1142 case 2:
1143 if (second4 > 0 && second4 <= 1024 * 1024 && Read24(buffer + 8) == 1)
1144 return CONTAINER_LMLM4;
1145 break;
1146 case 4:
1147 if (second4 > 0 && second4 <= 1024 * 1024 &&
1148 (Read16(buffer + 8) & 0xfffe) == 0xfffc)
1149 return CONTAINER_LMLM4;
1150 break;
1151 case 0xe:
1152 if (buffer_size > 16 && second4 == 0x50 && Read32(buffer + 12) == 0x34)
1153 return CONTAINER_MGSTS;
1154 break;
1155 case 0x16:
1156 if (third4 == 0x1803ffff || third4 == 0x1003ffff)
1157 return CONTAINER_TXD;
1158 break;
1159 case 0x1a5:
1160 if (CheckNc(buffer, buffer_size))
1161 return CONTAINER_NC;
1162 break;
1163 case TAG('\x00','\x00','\x02','\x80'):
1164 if (CheckDnxhd(buffer, buffer_size))
1165 return CONTAINER_DNXHD;
1166 break;
1167 case 0x800:
1168 if (CheckAea(buffer, buffer_size))
1169 return CONTAINER_AEA;
1170 break;
1171 case 0x001800a0:
1172 case 0x00180140:
1173 if (CheckMsnwctcp(buffer, buffer_size))
1174 return CONTAINER_MSNWCTCP;
1175 break;
1176 case TAG('\x00','\x0a','S','M'):
1177 if (StartsWith(buffer,
1178 buffer_size,
1179 kSmjpegSignature,
1180 sizeof(kSmjpegSignature) - 1))
xhwang 2013/05/06 23:51:27 "conditional or loop statements with complex condi
jrummell 2013/05/16 23:48:01 Done.
1181 return CONTAINER_SMJPEG;
1182 break;
1183 case 0x1084ffff:
1184 if (Read16(buffer + 4) == 0xffff)
1185 return CONTAINER_ROQ;
1186 break;
1187 case TAG('\x01','\x01','\x03','\xb8'):
1188 if (StartsWith(buffer,
1189 buffer_size,
1190 kIv8Signature,
1191 sizeof(kIv8Signature) - 1))
1192 return CONTAINER_IV8;
1193 break;
1194 case TAG('\x04','\x31','\x2e','\x34'):
1195 if (StartsWith(buffer,
1196 buffer_size,
1197 kIdfSignature,
1198 sizeof(kIdfSignature) - 1))
1199 return CONTAINER_IDF;
1200 break;
1201 case TAG('\x0b','\x8d','S','O'):
1202 case TAG('\x0c','\x0d','S','O'):
1203 case TAG('\x0c','\x8d','S','O'):
1204 if (buffer[4] == 'L' && buffer[5] == 0)
1205 return CONTAINER_SOL;
1206 break;
1207 case 0x1a45dfa3:
1208 if (CheckWebm(buffer, buffer_size))
1209 return CONTAINER_WEBM;
1210 break;
1211 case TAG('.','s','n','d'):
1212 return CONTAINER_AU;
1213 case TAG('\x30','\x26','\xB2','\x75'):
1214 if (StartsWith(buffer,
1215 buffer_size,
1216 kAsfSignature,
1217 sizeof(kAsfSignature) - 1))
1218 return CONTAINER_ASF;
1219 break;
1220 case TAG(' ','p','a','f'):
1221 if (buffer_size > 24 &&
1222 second4 == 0 && third4 == 0 &&
1223 Read32(buffer + 12) != 0 &&
1224 Read32(buffer + 20) != 0)
1225 return CONTAINER_EPAF;
1226 break;
1227 case TAG('#','!','A','M'):
1228 if (StartsWith(buffer,
1229 buffer_size,
1230 kAmrSignature,
1231 sizeof(kAmrSignature) - 1))
1232 return CONTAINER_AMR;
1233 break;
1234 case TAG('#','!','i','L'):
1235 if (StartsWith(buffer,
1236 buffer_size,
1237 kIlbcSignature,
1238 sizeof(kIlbcSignature) - 1))
1239 return CONTAINER_ILBC;
1240 break;
1241 case TAG('#','E','X','T'):
1242 if (CheckHls(buffer, buffer_size))
1243 return CONTAINER_HLS;
1244 break;
1245 case TAG('#',' ','V','o'):
1246 if (StartsWith(buffer,
1247 buffer_size,
1248 kVobsubSignature,
1249 sizeof(kVobsubSignature) - 1))
1250 return CONTAINER_VOBSUB;
1251 break;
1252 case TAG('.','R','M','F'):
1253 if (buffer[4] == 0 && buffer[5] == 0)
1254 return CONTAINER_RM;
1255 break;
1256 case TAG('.','r','a','\xfd'):
1257 return CONTAINER_RM;
1258 case TAG('.','S','o','X'):
1259 case TAG('X','o','S','.'):
1260 return CONTAINER_SOX;
1261 case TAG('1','S','N','h'):
1262 case TAG('S','C','H','l'):
1263 case TAG('S','E','A','D'):
1264 case TAG('S','H','E','N'):
1265 case TAG('k','V','G','T'):
1266 case TAG('M','A','D','k'):
1267 case TAG('M','P','C','h'):
1268 case TAG('M','V','h','d'):
1269 if ((second4 > 0x0fffff) && ((second4 & 0x0f0ff) != 0))
1270 return CONTAINER_EA;
1271 break;
1272 case TAG('2','B','I','T'):
1273 return CONTAINER_AVR;
1274 case TAG('A','N','I','M'):
1275 if (third4 == TAG('A','H','D','R'))
1276 return CONTAINER_SMUSH;
1277 break;
1278 case TAG('A','R','M','o'):
1279 if (StartsWith(buffer,
1280 buffer_size,
1281 kRplSignature,
1282 sizeof(kRplSignature) - 1))
1283 return CONTAINER_RPL;
1284 break;
1285 case TAG('B','B','C','D'):
1286 return CONTAINER_DIRAC;
1287 case TAG('B','F','&','I'):
1288 return CONTAINER_BFI;
1289 case TAG(';','F','F','M'):
1290 if (StartsWith(buffer,
1291 buffer_size,
1292 kFfSignature,
1293 sizeof(kFfSignature) - 1))
1294 return CONTAINER_FFMETADATA;
1295 break;
1296 case TAG('B','I','K','b'):
1297 case TAG('B','I','K','f'):
1298 case TAG('B','I','K','g'):
1299 case TAG('B','I','K','h'):
1300 case TAG('B','I','K','i'):
1301 if (CheckBink(buffer, buffer_size))
1302 return CONTAINER_BINK;
1303 break;
1304 case TAG('c','a','f','f'):
1305 if (Read16(buffer + 4) == 1)
1306 return CONTAINER_CAF;
1307 break;
1308 case TAG('C','r','e','a'):
1309 if (StartsWith(buffer,
1310 buffer_size,
1311 kVocSignature,
1312 sizeof(kVocSignature) - 1))
1313 return CONTAINER_VOC;
1314 break;
1315 case TAG('C','R','Y','O'):
1316 if (StartsWith(buffer,
1317 buffer_size,
1318 kApcSignature,
1319 sizeof(kApcSignature) - 1))
1320 return CONTAINER_APC;
1321 break;
1322 case TAG('D','E','X','A'):
1323 if (buffer_size > 15 &&
1324 Read16(buffer + 11) <= 2048 &&
1325 Read16(buffer + 13) <= 2048)
1326 return CONTAINER_DXA;
1327 break;
1328 case TAG('D','K','I','F'):
1329 if (second4 == 32)
1330 return CONTAINER_IVF;
1331 break;
1332 case TAG('D','T','S','H'):
1333 if (second4 == TAG('D','H','D','R'))
1334 return CONTAINER_DTSHD;
1335 break;
1336 case TAG('D','F','I','A'):
1337 return CONTAINER_DFA;
1338 case TAG('\x64','\xa3','\x01','\x00'):
1339 case TAG('\x64','\xa3','\x02','\x00'):
1340 case TAG('\x64','\xa3','\x03','\x00'):
1341 case TAG('\x64','\xa3','\x04','\x00'):
1342 case TAG('\x00','\x01','\xa3','\x64'):
1343 case TAG('\x00','\x02','\xa3','\x64'):
1344 case TAG('\x00','\x03','\xa3','\x64'):
1345 if (second4 != 0 && third4 != 0)
1346 return CONTAINER_IRCAM;
1347 break;
1348 case TAG('e','a','3','\x03'):
1349 if (CheckOma(buffer, buffer_size))
1350 return CONTAINER_OMA;
1351 break;
1352 case TAG('f','a','p',' '):
1353 if (buffer_size > 24 &&
1354 second4 == 0 && third4 == 1 &&
1355 Read32(buffer + 12) != 0 &&
1356 Read32(buffer + 20) != 0)
1357 return CONTAINER_EPAF;
1358 break;
1359 case TAG('f','f','c','o'):
1360 if (StartsWith(buffer,
1361 buffer_size,
1362 kConcatSignature,
1363 sizeof(kConcatSignature) - 1))
1364 return CONTAINER_CONCAT;
1365 break;
1366 case TAG('F','F','M','1'):
1367 case TAG('F','F','M','2'):
1368 return CONTAINER_FFM;
1369 case TAG('F','I','L','M'):
1370 return CONTAINER_FILM_CPK;
1371 case TAG('f','L','a','C'):
1372 return CONTAINER_FLAC;
1373 case TAG('F','L','V','\x00'):
1374 case TAG('F','L','V','\x01'):
1375 case TAG('F','L','V','\x02'):
1376 case TAG('F','L','V','\x03'):
1377 case TAG('F','L','V','\x04'):
1378 if (buffer[5] == 0 && Read32(buffer + 5) > 8)
1379 return CONTAINER_FLV;
1380 break;
1381 case TAG('F','O','R','M'):
1382 switch (third4) {
1383 case TAG('A','I','F','F'):
1384 case TAG('A','I','F','C'):
1385 return CONTAINER_AIFF;
1386 case TAG('8','S','V','X'):
1387 case TAG('1','6','S','V'):
1388 case TAG('M','A','U','D'):
1389 case TAG('P','B','M',' '):
1390 case TAG('A','C','B','M'):
1391 case TAG('D','E','E','P'):
1392 case TAG('I','L','B','M'):
1393 case TAG('R','G','B','8'):
1394 case TAG('R','G','B','N'):
1395 return CONTAINER_IFF;
1396 case TAG('M','O','V','E'):
1397 return CONTAINER_WC3MOVIE;
1398 case TAG('R','L','V','2'):
1399 case TAG('R','L','V','3'):
1400 return CONTAINER_RL2;
1401 case TAG('W','V','Q','A'):
1402 return CONTAINER_WSVQA;
1403 }
1404 break;
1405 case TAG('G','I','F','8'):
1406 if ((buffer[4] == '7' || buffer[4] == '9') &&
1407 buffer[5] == 'a' &&
1408 Read16(buffer + 6) != 0 &&
1409 Read16(buffer + 8) != 0)
1410 return CONTAINER_GIF;
1411 break;
1412 case TAG('I','M','A','_'):
1413 if (StartsWith(buffer,
1414 buffer_size,
1415 kIssSignature,
1416 sizeof(kIssSignature) - 1))
1417 return CONTAINER_ISS;
1418 break;
1419 case TAG('k','!','\x00','\x40'):
1420 case TAG('k','!','\x00','\x50'):
1421 return CONTAINER_BIT;
1422 case TAG('L','E','I','T'):
1423 if (StartsWith(buffer,
1424 buffer_size,
1425 kLxfSignature,
1426 sizeof(kLxfSignature) - 1))
1427 return CONTAINER_LXF;
1428 break;
1429 case TAG('L','P','F',' '):
1430 if (buffer_size > 24 &&
1431 Read32(buffer + 16) == TAG('A','N','I','M') &&
1432 Read16(buffer + 20) != 0 &&
1433 Read16(buffer + 22) != 0)
1434 return CONTAINER_ANM;
1435 break;
1436 case TAG('L','V','F','F'):
1437 return CONTAINER_LVF;
1438 case TAG('M','A','C',' '):
1439 return CONTAINER_APE;
1440 case TAG('M','M','M','D'):
1441 if (third4 == TAG('C','N','T','I'))
1442 return CONTAINER_MMF;
1443 break;
1444 case TAG('M','O','V','I'):
1445 if (Read16(buffer + 4) < 3)
1446 return CONTAINER_MV;
1447 break;
1448 case TAG('M','P','+','\x07'):
1449 case TAG('M','P','+','\x17'):
1450 return CONTAINER_MPC;
1451 case TAG('M','P','C','K'):
1452 if (CheckMpc8(buffer, buffer_size))
1453 return CONTAINER_MPC8;
1454 break;
1455 case TAG('N','I','S','T'):
1456 if (second4 == TAG('_','1','A','\x0a'))
1457 return CONTAINER_NISTSPHERE;
1458 break;
1459 case TAG('N','M','\x7a','\x56'):
1460 if (second4 == TAG('\x1F','\x5F','\x04','\xAD'))
1461 return CONTAINER_NUT;
1462 break;
1463 case TAG('N','S','V','f'):
1464 case TAG('N','S','V','s'):
1465 return CONTAINER_NSV;
1466 case TAG('n','u','t','/'):
1467 if (StartsWith(buffer,
1468 buffer_size,
1469 kLibnutSignature,
1470 sizeof(kLibnutSignature) - 1))
1471 return CONTAINER_LIBNUT;
1472 break;
1473 case TAG('N','u','p','p'):
1474 if (StartsWith(buffer,
1475 buffer_size,
1476 kNuv1Signature,
1477 sizeof(kNuv1Signature) - 1))
1478 return CONTAINER_NUV;
1479 break;
1480 case TAG('M','y','t','h'):
1481 if (StartsWith(buffer,
1482 buffer_size,
1483 kNuv2Signature,
1484 sizeof(kNuv2Signature) - 1))
1485 return CONTAINER_NUV;
1486 break;
1487 case TAG('O','N','2',' '):
1488 if (third4 == TAG('O','N','2','f'))
1489 return CONTAINER_AVI;
1490 case TAG('O','g','g','S'):
1491 if (buffer[5] <= 7)
1492 return CONTAINER_OGG;
1493 break;
1494 case TAG('P','a','c','k'):
1495 if (StartsWith(buffer,
1496 buffer_size,
1497 kPafSignature,
1498 sizeof(kPafSignature) - 1))
1499 return CONTAINER_PAF;
1500 break;
1501 case TAG('p','m','p','m'):
1502 if (Read32(buffer + 4) == 1)
1503 return CONTAINER_PMP;
1504 break;
1505 case TAG('P','V','F','1'):
1506 if (buffer[4] == '\n')
1507 return CONTAINER_PVF;
1508 break;
1509 case TAG('R','F','6','4'):
1510 if (buffer_size > 16 && Read32(buffer + 12) == TAG('d','s','6','4'))
1511 return CONTAINER_WAV;
1512 break;
1513 case TAG('r','i','f','f'):
1514 if (buffer_size > 24 &&
1515 StartsWith(buffer,
1516 buffer_size,
1517 kW64Signature,
1518 sizeof(kW64Signature) - 1) &&
1519 StartsWith(buffer + 24,
1520 buffer_size - 24,
1521 kW64Signature2,
1522 sizeof(kW64Signature2) - 1))
1523 return CONTAINER_W64;
1524 break;
1525 case TAG('R','I','F','F'):
1526 switch (third4) {
1527 case TAG('4','X','M','V'):
1528 return CONTAINER_4XM;
1529 case TAG('A','V','I',' '):
1530 case TAG('A','V','I','X'):
1531 case TAG('A','V','I','\x19'):
1532 case TAG('A','M','V',' '):
1533 return CONTAINER_AVI;
1534 case TAG('Q','L','C','M'):
1535 if (buffer_size > 16 && Read32(buffer + 12) == TAG('f','m','t',' '))
1536 return CONTAINER_QCP;
1537 break;
1538 case TAG('W','A','V','E'):
1539 // possibly ACT or WAV
1540 return (CheckAct(buffer, buffer_size)) ?
1541 CONTAINER_ACT : CONTAINER_WAV;
1542 case TAG('X','W','M','A'):
1543 return CONTAINER_XWMA;
1544 }
1545 break;
1546 case TAG('R','S','T','M'):
1547 if ((second4 & 0xffff0000) == 0xfffe0000 ||
1548 (second4 & 0xffff0000) == 0xfeff0000)
1549 return CONTAINER_BRSTM;
1550 break;
1551 case TAG('S','A','N','M'):
1552 if (third4 == TAG('S','H','D','R'))
1553 return CONTAINER_SMUSH;
1554 break;
1555 case TAG('S','I','F','F'):
1556 if (third4 == TAG('V','B','V','1') || third4 == TAG('S','O','U','N'))
1557 return CONTAINER_SIFF;
1558 break;
1559 case TAG('S','M','K','2'):
1560 case TAG('S','M','K','4'):
1561 return CONTAINER_SMK;
1562 case TAG('S','T','R','M'):
1563 if (buffer_size > 18 && Read16(buffer + 10) && Read16(buffer + 12) &&
1564 Read16(buffer + 16))
1565 return CONTAINER_AST;
1566 break;
1567 case TAG('t','B','a','K'):
1568 return CONTAINER_TAK;
1569 case TAG('T','H','P','\x00'):
1570 return CONTAINER_THP;
1571 case TAG('T','M','A','V'):
1572 if (CheckTmv(buffer, buffer_size))
1573 return CONTAINER_TMV;
1574 break;
1575 case TAG('T','T','A','1'):
1576 return CONTAINER_TTA;
1577 case TAG('\x55','\xaa','\x00','\x00'):
1578 if (CheckDsicin(buffer, buffer_size))
1579 return CONTAINER_DSICIN;
1580 break;
1581 case TAG('T','W','I','N'):
1582 if (CheckVqf(buffer, buffer_size))
1583 return CONTAINER_VQF;
1584 break;
1585 case TAG('V','I','D','\x00'):
1586 return CONTAINER_BETHSOFTVID;
1587 case TAG('w','W','\x10','\x00'):
1588 return CONTAINER_AVS;
1589 case TAG('w','v','p','k'):
1590 return CONTAINER_WV;
1591 case TAG('X','A','\x00','\x00'):
1592 case TAG('X','A','I','\x00'):
1593 case TAG('X','A','J','\x00'):
1594 if (CheckXa(buffer, buffer_size))
1595 return CONTAINER_XA;
1596 break;
1597 case TAG('X','B','I','N'):
1598 if (CheckXbin(buffer, buffer_size))
1599 return CONTAINER_XBIN;
1600 break;
1601 case TAG('Y','U','V','4'):
1602 if (StartsWith(buffer,
1603 buffer_size,
1604 kYuv4Signature,
1605 sizeof(kYuv4Signature) - 1))
1606 return CONTAINER_YUV4MPEGPIPE;
1607 break;
1608 case TAG('W','E','B','V'):
1609 if (StartsWith(buffer,
1610 buffer_size,
1611 kWebvttSignature,
1612 sizeof(kWebvttSignature) - 1))
1613 return CONTAINER_WEBVTT;
1614 break;
1615 case TAG('\xef','\xbb','\xbf','W'):
1616 if (StartsWith(buffer,
1617 buffer_size,
1618 kWebvttBomSignature,
1619 sizeof(kWebvttBomSignature) - 1))
1620 return CONTAINER_WEBVTT;
1621 break;
1622 case TAG('[','S','c','r'):
1623 if (StartsWith(buffer,
1624 buffer_size,
1625 kAssSignature,
1626 sizeof(kAssSignature) - 1))
1627 return CONTAINER_ASS;
1628 break;
1629 case TAG('\xef','\xbb','\xbf','['):
1630 if (StartsWith(buffer,
1631 buffer_size,
1632 kAssBomSignature,
1633 sizeof(kAssBomSignature) - 1))
1634 return CONTAINER_ASS;
1635 break;
1636 case TAG('<','w','i','n'):
1637 if (StartsWith(buffer,
1638 buffer_size,
1639 kRealSignature,
1640 sizeof(kRealSignature) - 1))
1641 return CONTAINER_REALTEXT;
1642 break;
1643 case TAG('<','S','A','M'):
1644 if (StartsWith(buffer,
1645 buffer_size,
1646 kSamiSignature,
1647 sizeof(kSamiSignature) - 1))
1648 return CONTAINER_SAMI;
1649 break;
1650 case TAG('\xef','\xbb','\xbf','<'):
1651 if (StartsWith(buffer,
1652 buffer_size,
1653 kRealBomSignature,
1654 sizeof(kRealBomSignature) - 1))
1655 return CONTAINER_REALTEXT;
1656 if (StartsWith(buffer,
1657 buffer_size,
1658 kSamiBomSignature,
1659 sizeof(kSamiBomSignature) - 1))
1660 return CONTAINER_SAMI;
1661 break;
1662 case TAG('\xb7','\xd8','\x00','\x20'):
1663 if (StartsWith(buffer,
1664 buffer_size,
1665 kWtvSignature,
1666 sizeof(kWtvSignature) - 1))
1667 return CONTAINER_WTV;
1668 break;
1669 }
1670
1671 // Now try a few different ones that look at something other
1672 // than the first 4 bytes
1673 uint32_t first3 = first4 & 0xffffff00;
1674 switch (first3) {
1675 case TAG('A','M','V',0):
1676 if (CheckMtv(buffer, buffer_size))
1677 return CONTAINER_MTV;
1678 break;
1679 case TAG('C','W','S',0):
1680 case TAG('F','W','S',0):
1681 return CONTAINER_SWF;
1682 case TAG('F','R','M',0):
1683 if (Read16(buffer + 4) != 0 && Read16(buffer + 6) != 0)
1684 return CONTAINER_FRM;
1685 break;
1686 case TAG('A','V','\x01',0):
1687 case TAG('A','V','\x02',0):
1688 if (CheckPva(buffer, buffer_size))
1689 return CONTAINER_PVA;
1690 break;
1691 case TAG('I','D','3',0):
1692 if (CheckMp3(buffer, buffer_size, true))
1693 return CONTAINER_MP3;
1694 break;
1695 }
1696
1697 // Maybe the first 2 characters are something we can use.
1698 uint32_t first2 = first4 & 0xffff0000;
1699 switch (first2) {
1700 case 0x032e0000:
1701 if (CheckVmd(buffer, buffer_size))
1702 return CONTAINER_VMD;
1703 break;
1704 case 0x04000000:
1705 case 0x04040000:
1706 case 0x040c0000:
1707 case 0x04140000:
1708 return CONTAINER_EA_CDATA;
1709 case TAG('J','V',0,0):
1710 if (StartsWith(buffer + 4,
1711 buffer_size - 4,
1712 kJvSignature,
1713 sizeof(kJvSignature) - 1))
1714 return CONTAINER_JV;
1715 break;
1716 case 0x0b770000:
1717 if (CheckAc3(buffer, buffer_size))
1718 return CONTAINER_AC3;
1719 if (CheckEac3(buffer, buffer_size))
1720 return CONTAINER_EAC3;
1721 break;
1722 case TAG('Y','O',0,0):
1723 if (CheckYop(buffer, buffer_size))
1724 return CONTAINER_JV;
1725 break;
1726 case 0xfff00000:
1727 case 0xfff10000:
1728 case 0xfff80000:
1729 case 0xfff90000:
1730 if (CheckAac(buffer, buffer_size))
1731 return CONTAINER_AAC;
1732 break;
1733 }
1734
1735 // Now try the second set of 4 characters.
1736 switch (second4) {
1737 case 4:
1738 if (buffer_size > 24 && buffer[3] == 0xc5 && Read32(buffer + 20) == 0xc)
1739 return CONTAINER_VC1TEST;
1740 break;
1741 case TAG('R','E','D','1'):
1742 return CONTAINER_R3D;
1743 }
1744
1745 switch (Read16(buffer + 4)) {
1746 case 0xaf11:
1747 case 0xaf12:
1748 case 0xaf13:
1749 if (buffer_size > 20 &&
1750 third4 <= 4096 &&
1751 Read16(buffer + 10) <= 4096 &&
1752 (Read16(buffer + 16) == 0xf1fa || Read32(buffer + 16) <= 2000))
1753 return CONTAINER_FLIC;
1754 break;
1755 }
1756
1757 // Lastly, there are some that are other simple checks but don't fit
1758 // the above case statements.
1759 if (CheckC93(buffer, buffer_size))
1760 return CONTAINER_C93;
1761 if (CheckCdxl(buffer, buffer_size))
1762 return CONTAINER_CDXL;
1763 if (CheckIdcin(buffer, buffer_size))
1764 return CONTAINER_IDCIN;
1765 if (CheckLoas(buffer, buffer_size))
1766 return CONTAINER_LOAS;
1767 if (CheckM4v(buffer, buffer_size))
1768 return CONTAINER_M4V;
1769 if (CheckMov(buffer, buffer_size))
1770 return CONTAINER_MOV;
1771 if (CheckVivo(buffer, buffer_size))
1772 return CONTAINER_VIVO;
1773 if (CheckWsaud(buffer, buffer_size))
1774 return CONTAINER_WSAUD;
1775 if (CheckXmv(buffer, buffer_size))
1776 return CONTAINER_XMV;
1777 // Check if the file is in MP3 format without the header
1778 if (CheckMp3(buffer, buffer_size, false))
1779 return CONTAINER_MP3;
1780
1781 // skip over starting 0's, and see if it is MP3/AC3/EAC3
1782 if (buffer[0] == 0) {
1783 size_t offset = 1;
1784 size_t remaining = buffer_size - 1;
1785 while (remaining > 0 && buffer[offset] == 0) {
1786 ++offset;
1787 --remaining;
1788 }
1789 if (remaining > 32) {
1790 // not worth trying if only a small number of bytes left
1791 if (Read16(buffer + offset) == 0x0b77) {
1792 if (CheckAc3(buffer + offset, remaining))
1793 return CONTAINER_AC3;
1794 if (CheckEac3(buffer + offset, remaining))
1795 return CONTAINER_EAC3;
1796 }
1797 else if (CheckMp3(buffer + offset, remaining, false))
1798 return CONTAINER_MP3;
1799 }
1800 }
1801
1802 return CONTAINER_UNKNOWN;
1803 }
1804
1805 static const char kIpmString[] = "Interplay MVE File\x1A\0\x1A";
1806 static const char kMxfString[] =
1807 "\x06\x0e\x2b\x34\x02\x05\x01\x01\x0d\x01\x02\x01\x01\x02";
1808 static const char kSub1String[] = "******** START SCRIPT ********";
1809
1810 // Attempt to determine the container type by scanning for a set of strings,
1811 // character by character. It covers the following containers:
1812 // ipmovie, mxf, subviewer1
scherkus (not reviewing) 2013/05/07 00:50:20 I can say with a good deal of certainty that we do
jrummell 2013/05/16 23:48:01 Partially done. Still need LookupContainerByString
1813 static FFmpegContainerName LookupContainerByStringScan(const uint8_t* buffer,
1814 int buffer_size) {
1815 int offset = 0;
1816 for (int remaining = buffer_size; remaining > 4; --remaining) {
1817 uint32_t tag = Read32(buffer + offset);
1818 switch (tag) {
1819 case TAG('I','n','t','e'):
1820 if (StartsWith(buffer + offset,
1821 remaining,
1822 kIpmString,
1823 sizeof(kIpmString) - 1))
1824 return CONTAINER_IPMOVIE;
1825 break;
1826 case 0x060e2b34:
1827 if (StartsWith(buffer + offset,
1828 remaining,
1829 kMxfString,
1830 sizeof(kMxfString) - 1))
1831 return CONTAINER_MXF;
1832 break;
1833 case TAG('*','*','*','*'):
1834 if (StartsWith(buffer + offset,
1835 remaining,
1836 kSub1String,
1837 sizeof(kSub1String) - 1))
1838 return CONTAINER_SUBVIEWER1;
1839 break;
1840 case TAG('N','S','V','s'):
1841 if (CheckNsv(buffer + offset, remaining))
1842 return CONTAINER_NSV;
1843 break;
1844 case 0x001800a0:
1845 case 0x00180140:
1846 if (CheckMsnwctcp(buffer + offset, remaining))
1847 return CONTAINER_MSNWCTCP;
1848 break;
1849 }
1850 // Not found, move forward to next character.
1851 ++offset;
1852 }
1853
1854 // didn't find a string match for any of the formats
1855 return CONTAINER_UNKNOWN;
1856 }
1857
1858 // Helper function to do limited scanf functionality without going off
1859 // the end of the buffer.
1860 static bool SaferScanf(const uint8_t* buffer,
1861 int buffer_size,
1862 const char* format) {
1863 // This function only supports the following items in the format string:
1864 // %<maxlength>d -- integer (can start with +/-, maxlength is optional)
1865 // %<maxlength>u -- unsigned integer (only digits, maxlength is optional)
1866 // %c -- any single character
1867 // %[<characters>] -- character must be one of the set
1868 // -- everything else is a literal
1869 // This code assumes that format is correct.
1870 int offset = 0;
1871 while (offset < buffer_size) {
1872 // Determine next format item.
1873 if (*format == '\0') {
1874 // End of format string, success.
1875 return true;
1876 } else if (*format != '%') {
1877 // Not a specifier, so it must match exactly
1878 if (buffer[offset] != *format)
1879 return false;
1880 ++offset;
1881 } else {
1882 ++format;
1883 int maxLength = 0;
1884 int numSeen = 0;
1885 while (isdigit(*format)) {
1886 maxLength = maxLength * 10 + (*format - '0');
1887 ++format;
1888 }
1889 if (maxLength < 1) // If not specified set it to 100 to simplify below
1890 maxLength = 100;
1891 switch (*format) {
1892 case 'c':
1893 ++offset; // Don't care what the character is
1894 break;
1895 case 'd':
1896 case 'u':
1897 while (offset < buffer_size && isspace(buffer[offset]))
1898 ++offset;
1899 if (offset >= buffer_size)
1900 return false;
1901 if (buffer[offset] == '+' || buffer[offset] == '-')
1902 ++offset;
1903 // need to process up to maxLength digits
1904 while (offset < buffer_size &&
1905 --maxLength >= 0 &&
1906 isdigit(buffer[offset])) {
1907 ++numSeen;
1908 ++offset;
1909 }
1910 if (numSeen == 0) // No digits, so it is not a match for %d/%u
1911 return false;
1912 break;
1913 case '[':
1914 ++format; // Skip [
1915 while (*format != ']') {
1916 if (buffer[offset] == *format) {
1917 ++numSeen;
1918 }
1919 ++format;
1920 }
1921 if (numSeen == 0) // No character match
1922 return false;
1923 ++offset; // Skip the character matched
1924 break;
1925 default:
1926 NOTREACHED();
1927 }
1928 }
1929 // Move to the next format specification.
1930 ++format;
1931 }
1932 // Out of buffer, so it doesn't match
1933 return false;
1934 }
1935
1936 // Attempt to determine the container type by scanning for a set of strings,
1937 // line by line. It covers the following containers:
1938 // microdvd, mpl2, mpsub, pjs, sdp, srt, subviewer, vplayer
1939 static FFmpegContainerName LookupContainerByStringLine(const uint8_t* buffer,
1940 int buffer_size) {
1941 int offset = StartsWith(buffer, buffer_size, BYTE_ORDER_MARK, 3) ? 3 : 0;
1942 int lines = 0;
1943
1944 // PJS is a scan from the beginning only.
1945 if (SaferScanf(buffer, buffer_size, "%d,%d,%c"))
1946 if (CheckPjs(buffer, buffer_size))
1947 return CONTAINER_PJS;
1948
1949 // Same for VPLAYER.
1950 if (SaferScanf(buffer, buffer_size, "%d:%d:%d.%d%[: =]"))
1951 return CONTAINER_VPLAYER;
1952
1953 // Same for AQTITLE
1954 if (SaferScanf(buffer, buffer_size, "-->> %d"))
1955 return CONTAINER_AQTITLE;
1956
1957 while (offset < buffer_size) {
1958 const uint8_t* ptr = buffer + offset;
1959 int remaining = buffer_size - offset;;
1960
1961 if (*ptr == '{' && lines < 3) {
1962 if (SaferScanf(ptr, remaining, "{%d}{}%c") ||
1963 SaferScanf(ptr, remaining, "{%d}{%d}%c") ||
1964 SaferScanf(ptr, remaining, "{DEFAULT}{}%c"))
1965 return CONTAINER_MICRODVD;
1966 }
1967
1968 if (*ptr == '[' && lines < 3) {
1969 if (SaferScanf(ptr, remaining, "[%64d][%64d]%c") ||
1970 SaferScanf(ptr, remaining, "[%64d][]%c"))
1971 return CONTAINER_MPL2;
1972 }
1973
1974 if (*ptr == 'F') {
1975 if (SaferScanf(ptr, remaining, "FORMAT=%d") ||
1976 SaferScanf(ptr, remaining, "FORMAT=TIME"))
1977 return CONTAINER_MPSUB;
1978 }
1979
1980 if (*ptr == 'c') {
1981 if (StartsWith(ptr, remaining, "c=IN IP", 7))
1982 return CONTAINER_SDP;
1983 }
1984
1985 if (isdigit(*ptr) && lines < 3) {
1986 if (SaferScanf(
1987 ptr, remaining, "%d:%2d:%2d%[,.]%3d --> %d:%2d:%2d%[,.]%3d"))
1988 return CONTAINER_SRT;
1989 }
1990
1991 if (isdigit(*ptr) && lines < 1) {
1992 if (SaferScanf(ptr, remaining, "%u:%u:%u.%u,%u:%u:%u.%u%c") ||
1993 StartsWith(ptr, remaining, "[INFORMATION]", 13))
1994 return CONTAINER_SUBVIEWER;
1995 }
1996
1997 // Find the end of the line.
1998 while (buffer[offset] != '\n' && buffer[offset] != '\r' &&
1999 offset < buffer_size)
2000 ++offset;
2001
2002 // Skip the \n\r.
2003 while ((buffer[offset] == '\n' || buffer[offset] == '\r') &&
2004 offset < buffer_size)
2005 ++offset;
2006 ++lines;
2007 }
2008
2009 // Didn't find a string match for any of the formats.
2010 return CONTAINER_UNKNOWN;
2011 }
2012
2013 // The strings in the list below are the ones returned by FFmpeg.
2014 // This would be the string from AVInputFormat.name. The list is
2015 // sorted by string alphabetically, so that we can use a binary
2016 // search when looking for a container by name.
2017 static const ContainerNameMapping kContainerNameMapping[] = {
2018 { CONTAINER_4XM, "4xm" },
2019 { CONTAINER_AAC, "aac" },
2020 { CONTAINER_AC3, "ac3" },
2021 { CONTAINER_ACT, "act" },
2022 { CONTAINER_ADF, "adf" },
2023 { CONTAINER_ADX, "adx" },
2024 { CONTAINER_AEA, "aea" },
2025 { CONTAINER_AFC, "afc" },
2026 { CONTAINER_AIFF, "aiff" },
2027 { CONTAINER_ALAW, "alaw" },
2028 { CONTAINER_ALSA, "alsa" },
2029 { CONTAINER_AMR, "amr" },
2030 { CONTAINER_ANM, "anm" },
2031 { CONTAINER_APC, "apc" },
2032 { CONTAINER_APE, "ape" },
2033 { CONTAINER_AQTITLE, "aqtitle" },
2034 { CONTAINER_ASF, "asf" },
2035 { CONTAINER_ASS, "ass" },
2036 { CONTAINER_AST, "ast" },
2037 { CONTAINER_AU, "au" },
2038 { CONTAINER_AVI, "avi" },
2039 { CONTAINER_AVISYNTH, "avisynth" },
2040 { CONTAINER_AVR, "avr" },
2041 { CONTAINER_AVS, "avs" },
2042 { CONTAINER_BETHSOFTVID, "bethsoftvid" },
2043 { CONTAINER_BFI, "bfi" },
2044 { CONTAINER_BIN, "bin" },
2045 { CONTAINER_BINK, "bink" },
2046 { CONTAINER_BIT, "bit" },
2047 { CONTAINER_BKTR, "bktr" },
2048 { CONTAINER_BMV, "bmv" },
2049 { CONTAINER_BRSTM, "brstm" },
2050 { CONTAINER_C93, "c93" },
2051 { CONTAINER_CAF, "caf" },
2052 { CONTAINER_CAVSVIDEO, "cavsvideo" },
2053 { CONTAINER_CDG, "cdg" },
2054 { CONTAINER_CDXL, "cdxl" },
2055 { CONTAINER_CONCAT, "concat" },
2056 { CONTAINER_DAUD, "daud" },
2057 { CONTAINER_DFA, "dfa" },
2058 { CONTAINER_DIRAC, "dirac" },
2059 { CONTAINER_DNXHD, "dnxhd" },
2060 { CONTAINER_DSHOW, "dshow" },
2061 { CONTAINER_DSICIN, "dsicin" },
2062 { CONTAINER_DTS, "dts" },
2063 { CONTAINER_DTSHD, "dtshd" },
2064 { CONTAINER_DV, "dv" },
2065 { CONTAINER_DV1394, "dv1394" },
2066 { CONTAINER_DXA, "dxa" },
2067 { CONTAINER_EA, "ea" },
2068 { CONTAINER_EA_CDATA, "ea_cdata" },
2069 { CONTAINER_EAC3, "eac3" },
2070 { CONTAINER_EPAF, "epaf" },
2071 { CONTAINER_F32BE, "f32be" },
2072 { CONTAINER_F32LE, "f32le" },
2073 { CONTAINER_F64BE, "f64be" },
2074 { CONTAINER_F64LE, "f64le" },
2075 { CONTAINER_FBDEV, "fbdev" },
2076 { CONTAINER_FFM, "ffm" },
2077 { CONTAINER_FFMETADATA, "ffmetadata" },
2078 { CONTAINER_FILM_CPK, "film_cpk" },
2079 { CONTAINER_FILMSTRIP, "filmstrip" },
2080 { CONTAINER_FLAC, "flac" },
2081 { CONTAINER_FLIC, "flic" },
2082 { CONTAINER_FLV, "flv" },
2083 { CONTAINER_FRM, "frm" },
2084 { CONTAINER_G722, "g722" },
2085 { CONTAINER_G723_1, "g723_1" },
2086 { CONTAINER_G729, "g729" },
2087 { CONTAINER_GIF, "gif" },
2088 { CONTAINER_GSM, "gsm" },
2089 { CONTAINER_GXF, "gxf" },
2090 { CONTAINER_H261, "h261" },
2091 { CONTAINER_H263, "h263" },
2092 { CONTAINER_H264, "h264" },
2093 { CONTAINER_HLS, "hls,applehttp" },
2094 { CONTAINER_ICO, "ico" },
2095 { CONTAINER_IDCIN, "idcin" },
2096 { CONTAINER_IDF, "idf" },
2097 { CONTAINER_IEC61883, "iec61883" },
2098 { CONTAINER_IFF, "iff" },
2099 { CONTAINER_ILBC, "ilbc" },
2100 { CONTAINER_IMAGE2, "image2" },
2101 { CONTAINER_IMAGE2PIPE, "image2pipe" },
2102 { CONTAINER_INGENIENT, "ingenient" },
2103 { CONTAINER_IPMOVIE, "ipmovie" },
2104 { CONTAINER_IRCAM, "ircam" },
2105 { CONTAINER_ISS, "iss" },
2106 { CONTAINER_IV8, "iv8" },
2107 { CONTAINER_IVF, "ivf" },
2108 { CONTAINER_JACK, "jack" },
2109 { CONTAINER_JACOSUB, "jacosub" },
2110 { CONTAINER_JV, "jv" },
2111 { CONTAINER_LATM, "latm" },
2112 { CONTAINER_LAVFI, "lavfi" },
2113 { CONTAINER_LIBCDIO, "libcdio" },
2114 { CONTAINER_LIBDC1394, "libdc1394" },
2115 { CONTAINER_LIBMODPLUG, "libmodplug" },
2116 { CONTAINER_LIBNUT, "libnut" },
2117 { CONTAINER_LMLM4, "lmlm4" },
2118 { CONTAINER_LOAS, "loas" },
2119 { CONTAINER_LVF, "lvf" },
2120 { CONTAINER_LXF, "lxf" },
2121 { CONTAINER_M4V, "m4v" },
2122 { CONTAINER_WEBM, "matroska,webm" },
2123 { CONTAINER_MGSTS, "mgsts" },
2124 { CONTAINER_MICRODVD, "microdvd" },
2125 { CONTAINER_MJPEG, "mjpeg" },
2126 { CONTAINER_MLP, "mlp" },
2127 { CONTAINER_MM, "mm" },
2128 { CONTAINER_MMF, "mmf" },
2129 { CONTAINER_MOV, "mov,mp4,m4a,3gp,3g2,mj2" },
2130 { CONTAINER_MP3, "mp3" },
2131 { CONTAINER_MPC, "mpc" },
2132 { CONTAINER_MPC8, "mpc8" },
2133 { CONTAINER_MPEG, "mpeg" },
2134 { CONTAINER_MPEGTS, "mpegts" },
2135 { CONTAINER_MPEGTSRAW, "mpegtsraw" },
2136 { CONTAINER_MPEGVIDEO, "mpegvideo" },
2137 { CONTAINER_MPL2, "mpl2" },
2138 { CONTAINER_MPSUB, "mpsub" },
2139 { CONTAINER_MSNWCTCP, "msnwctcp" },
2140 { CONTAINER_MTV, "mtv" },
2141 { CONTAINER_MULAW, "mulaw" },
2142 { CONTAINER_MV, "mv" },
2143 { CONTAINER_MVI, "mvi" },
2144 { CONTAINER_MXF, "mxf" },
2145 { CONTAINER_MXG, "mxg" },
2146 { CONTAINER_NC, "nc" },
2147 { CONTAINER_NISTSPHERE, "nistsphere" },
2148 { CONTAINER_NSV, "nsv" },
2149 { CONTAINER_NUT, "nut" },
2150 { CONTAINER_NUV, "nuv" },
2151 { CONTAINER_OGG, "ogg" },
2152 { CONTAINER_OMA, "oma" },
2153 { CONTAINER_OPENAL, "openal" },
2154 { CONTAINER_OSS, "oss" },
2155 { CONTAINER_PAF, "paf" },
2156 { CONTAINER_PJS, "pjs" },
2157 { CONTAINER_PMP, "pmp" },
2158 { CONTAINER_PSXSTR, "psxstr" },
2159 { CONTAINER_PULSE, "pulse" },
2160 { CONTAINER_PVA, "pva" },
2161 { CONTAINER_PVF, "pvf" },
2162 { CONTAINER_QCP, "qcp" },
2163 { CONTAINER_R3D, "r3d" },
2164 { CONTAINER_RAWVIDEO, "rawvideo" },
2165 { CONTAINER_RDT, "rdt" },
2166 { CONTAINER_REALTEXT, "realtext" },
2167 { CONTAINER_RL2, "rl2" },
2168 { CONTAINER_RM, "rm" },
2169 { CONTAINER_ROQ, "roq" },
2170 { CONTAINER_RPL, "rpl" },
2171 { CONTAINER_RSO, "rso" },
2172 { CONTAINER_RTP, "rtp" },
2173 { CONTAINER_RTSP, "rtsp" },
2174 { CONTAINER_S16BE, "s16be" },
2175 { CONTAINER_S16LE, "s16le" },
2176 { CONTAINER_S24BE, "s24be" },
2177 { CONTAINER_S24LE, "s24le" },
2178 { CONTAINER_S32BE, "s32be" },
2179 { CONTAINER_S32LE, "s32le" },
2180 { CONTAINER_S8, "s8" },
2181 { CONTAINER_SAMI, "sami" },
2182 { CONTAINER_SAP, "sap" },
2183 { CONTAINER_SBG, "sbg" },
2184 { CONTAINER_SDP, "sdp" },
2185 { CONTAINER_SHN, "shn" },
2186 { CONTAINER_SIFF, "siff" },
2187 { CONTAINER_SMJPEG, "smjpeg" },
2188 { CONTAINER_SMK, "smk" },
2189 { CONTAINER_SMUSH, "smush" },
2190 { CONTAINER_SNDIO, "sndio" },
2191 { CONTAINER_SOL, "sol" },
2192 { CONTAINER_SOX, "sox" },
2193 { CONTAINER_SPDIF, "spdif" },
2194 { CONTAINER_SRT, "srt" },
2195 { CONTAINER_SUBVIEWER, "subviewer" },
2196 { CONTAINER_SUBVIEWER1, "subviewer1" },
2197 { CONTAINER_SWF, "swf" },
2198 { CONTAINER_TAK, "tak" },
2199 { CONTAINER_TEDCAPTIONS, "tedcaptions" },
2200 { CONTAINER_THP, "thp" },
2201 { CONTAINER_TIERTEXSEQ, "tiertexseq" },
2202 { CONTAINER_TMV, "tmv" },
2203 { CONTAINER_TRUEHD, "truehd" },
2204 { CONTAINER_TTA, "tta" },
2205 { CONTAINER_TTY, "tty" },
2206 { CONTAINER_TXD, "txd" },
2207 { CONTAINER_VC1, "vc1" },
2208 { CONTAINER_VC1TEST, "vc1test" },
2209 { CONTAINER_VFWCAP, "vfwcap" },
2210 { CONTAINER_V4L, "video4linux,v4l" },
2211 { CONTAINER_V4L2, "video4linux2,v4l2" },
2212 { CONTAINER_VIVO, "vivo" },
2213 { CONTAINER_VMD, "vmd" },
2214 { CONTAINER_VOBSUB, "vobsub" },
2215 { CONTAINER_VOC, "voc" },
2216 { CONTAINER_VPLAYER, "vplayer" },
2217 { CONTAINER_VQF, "vqf" },
2218 { CONTAINER_W64, "w64" },
2219 { CONTAINER_WAV, "wav" },
2220 { CONTAINER_WC3MOVIE, "wc3movie" },
2221 { CONTAINER_WEBVTT, "webvtt" },
2222 { CONTAINER_WSAUD, "wsaud" },
2223 { CONTAINER_WSVQA, "wsvqa" },
2224 { CONTAINER_WTV, "wtv" },
2225 { CONTAINER_WV, "wv" },
2226 { CONTAINER_X11GRAB, "x11grab" },
2227 { CONTAINER_XA, "xa" },
2228 { CONTAINER_XBIN, "xbin" },
2229 { CONTAINER_XMV, "xmv" },
2230 { CONTAINER_XWMA, "xwma" },
2231 { CONTAINER_YOP, "yop" },
2232 { CONTAINER_YUV4MPEGPIPE, "yuv4mpegpipe" }
2233 };
2234
2235 // Mapping from the strings returned by FFmpeg to container ID.
2236 const ContainerNameMapping ContainerNameMappingItem(int index) {
2237 return kContainerNameMapping[index];
2238 }
2239
2240 // Returns the number of elements in kContainerNameMapping.
2241 const int ContainerNameMappingSize() {
2242 return arraysize(kContainerNameMapping);
2243 }
2244
2245 // Lookup a container name using the list above (kContainerNameMapping)
2246 // to determine the container enum. If not found (recent addition to
2247 // FFmpeg or a name change), return CONTAINER_UNKNOWN.
2248 FFmpegContainerName LookupContainer(
2249 const char* container_name) {
2250 DCHECK(container_name);
2251 const ContainerNameMapping* found =
2252 std::lower_bound(kContainerNameMapping,
2253 kContainerNameMapping + ContainerNameMappingSize() - 1,
2254 container_name,
2255 ContainerNameMappingComparer);
2256 return (strcasecmp(found->name, container_name) == 0) ? found->id
2257 : CONTAINER_UNKNOWN;
2258 }
2259
2260 // Attempt to determine the container name from the buffer provided.
2261 FFmpegContainerName DetermineContainer(const uint8_t* buffer,
2262 size_t buffer_size) {
xhwang 2013/05/06 23:51:27 indent
jrummell 2013/05/16 23:48:01 Done.
2263 DCHECK(buffer);
2264 DCHECK_LE(buffer_size, static_cast<size_t>(std::numeric_limits<int>::max()));
2265 // TODO(jrummell): The following formats are not scanned for
2266 // cavsvideo, dts, dv, h261, h263, h264, jacosub,
2267 // mpeg, mpegts, mpegvideo, psxstr, sbg, spdif, tedcaptions
2268
2269 // First attempt the simple checks, that typically look at just the
2270 // first few bytes of the file.
2271 FFmpegContainerName result = LookupContainerByFirst4(buffer, buffer_size);
2272 if (result != CONTAINER_UNKNOWN)
2273 return result;
2274
2275 // No success with simple test, so attempt to determine the container by
2276 // looking for strings in the buffer.
2277 result = LookupContainerByStringScan(buffer, buffer_size);
2278 if (result != CONTAINER_UNKNOWN)
2279 return result;
2280 return LookupContainerByStringLine(buffer, buffer_size);
2281 }
2282
2283 // Log the container based on the name returned by FFmpeg.
2284 void LogContainer(const char* container_name) {
2285 FFmpegContainerName container = LookupContainer(container_name);
2286 LogContainerToHistogram(container, false);
2287 }
2288
2289 // Log the container by examining the first part of the stream.
2290 void LogContainer(const uint8_t* buffer, size_t buffer_size) {
2291 FFmpegContainerName container = DetermineContainer(buffer, buffer_size);
2292 LogContainerToHistogram(container, true);
2293 }
2294
2295 } // namespace container_names
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698