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

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

Issue 2807463003: Simplify checking for MP3 format (Closed)
Patch Set: Simplify Created 3 years, 8 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
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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <cctype> 9 #include <cctype>
10 #include <limits> 10 #include <limits>
(...skipping 1066 matching lines...) Expand 10 before | Expand all | Expand 10 after
1077 else 1077 else
1078 bitrate = kBitRateTableV2L23[bitrate_index]; 1078 bitrate = kBitRateTableV2L23[bitrate_index];
1079 } 1079 }
1080 if (layer == LAYER_1) 1080 if (layer == LAYER_1)
1081 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4; 1081 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4;
1082 else 1082 else
1083 *framesize = (144000 * bitrate) / sampling_rate + padding; 1083 *framesize = (144000 * bitrate) / sampling_rate + padding;
1084 return (bitrate > 0 && sampling_rate > 0); 1084 return (bitrate > 0 && sampling_rate > 0);
1085 } 1085 }
1086 1086
1087 // Extract a size encoded the MP3 way.
1088 static int GetMp3HeaderSize(const uint8_t* buffer, int buffer_size) {
1089 DCHECK_GE(buffer_size, 9);
1090 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) +
1091 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10;
1092 if (buffer[5] & 0x10) // Footer added?
1093 size += 10;
1094 return size;
1095 }
1096
1097 // Additional checks for a MP3 container. 1087 // Additional checks for a MP3 container.
1098 static bool CheckMp3(const uint8_t* buffer, int buffer_size, bool seenHeader) { 1088 static bool CheckMp3(const uint8_t* buffer, int buffer_size) {
1099 RCHECK(buffer_size >= 10); // Must be enough to read the initial header. 1089 // This function assumes that the ID3 header is not present in the file and
1100 1090 // simply checks for several valid MPEG audio buffers after skipping any
1101 int framesize; 1091 // optional padding characters.
1102 int numSeen = 0; 1092 int numSeen = 0;
1103 int offset = 0; 1093 int offset = 0;
1104 if (seenHeader) { 1094
1105 offset = GetMp3HeaderSize(buffer, buffer_size); 1095 // Skip over any padding (0's).
1106 } else { 1096 while (offset < buffer_size && buffer[offset] == 0)
1107 // Skip over leading 0's. 1097 ++offset;
1108 while (offset < buffer_size && buffer[offset] == 0)
1109 ++offset;
1110 }
1111 1098
1112 while (offset + 3 < buffer_size) { 1099 while (offset + 3 < buffer_size) {
1100 int framesize;
1113 RCHECK(ValidMpegAudioFrameHeader( 1101 RCHECK(ValidMpegAudioFrameHeader(
1114 buffer + offset, buffer_size - offset, &framesize)); 1102 buffer + offset, buffer_size - offset, &framesize));
1115 1103
1116 // Have we seen enough valid headers? 1104 // Have we seen enough valid headers?
1117 if (++numSeen > 10) 1105 if (++numSeen > 10)
1118 return true; 1106 return true;
1119 offset += framesize; 1107 offset += framesize;
1120 } 1108 }
1121 // Off the end of the buffer, return success if a few valid headers seen. 1109 // Off the end of the buffer, return success if a few valid headers seen.
1122 return numSeen > 2; 1110 return numSeen > 2;
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
1586 } 1574 }
1587 1575
1588 // Now try a few different ones that look at something other 1576 // Now try a few different ones that look at something other
1589 // than the first 4 bytes. 1577 // than the first 4 bytes.
1590 uint32_t first3 = first4 & 0xffffff00; 1578 uint32_t first3 = first4 & 0xffffff00;
1591 switch (first3) { 1579 switch (first3) {
1592 case TAG('C','W','S',0): 1580 case TAG('C','W','S',0):
1593 case TAG('F','W','S',0): 1581 case TAG('F','W','S',0):
1594 return CONTAINER_SWF; 1582 return CONTAINER_SWF;
1595 1583
1596 case TAG('I','D','3',0): 1584 case TAG('I','D','3',0):
DaleCurtis 2017/04/13 23:25:42 Ah, I think this accidentally completely dropped n
1597 if (CheckMp3(buffer, buffer_size, true)) 1585 return CONTAINER_MP3;
1598 return CONTAINER_MP3;
1599 break;
1600 } 1586 }
1601 1587
1602 // Maybe the first 2 characters are something we can use. 1588 // Maybe the first 2 characters are something we can use.
1603 uint32_t first2 = Read16(buffer); 1589 uint32_t first2 = Read16(buffer);
1604 switch (first2) { 1590 switch (first2) {
1605 case kAc3SyncWord: 1591 case kAc3SyncWord:
1606 if (CheckAc3(buffer, buffer_size)) 1592 if (CheckAc3(buffer, buffer_size))
1607 return CONTAINER_AC3; 1593 return CONTAINER_AC3;
1608 if (CheckEac3(buffer, buffer_size)) 1594 if (CheckEac3(buffer, buffer_size))
1609 return CONTAINER_EAC3; 1595 return CONTAINER_EAC3;
1610 break; 1596 break;
1611 1597
1612 case 0xfff0: 1598 case 0xfff0:
1613 case 0xfff1: 1599 case 0xfff1:
1614 case 0xfff8: 1600 case 0xfff8:
1615 case 0xfff9: 1601 case 0xfff9:
1616 if (CheckAac(buffer, buffer_size)) 1602 if (CheckAac(buffer, buffer_size))
1617 return CONTAINER_AAC; 1603 return CONTAINER_AAC;
1618 break; 1604 break;
1619 } 1605 }
1620 1606
1621 // Check if the file is in MP3 format without the header. 1607 // Check if the file is in MP3 format without the ID3 header.
1622 if (CheckMp3(buffer, buffer_size, false)) 1608 if (CheckMp3(buffer, buffer_size))
1623 return CONTAINER_MP3; 1609 return CONTAINER_MP3;
1624 1610
1625 return CONTAINER_UNKNOWN; 1611 return CONTAINER_UNKNOWN;
1626 } 1612 }
1627 1613
1628 // Attempt to determine the container name from the buffer provided. 1614 // Attempt to determine the container name from the buffer provided.
1629 MediaContainerName DetermineContainer(const uint8_t* buffer, int buffer_size) { 1615 MediaContainerName DetermineContainer(const uint8_t* buffer, int buffer_size) {
1630 DCHECK(buffer); 1616 DCHECK(buffer);
1631 1617
1632 // Since MOV/QuickTime/MPEG4 streams are common, check for them first. 1618 // Since MOV/QuickTime/MPEG4 streams are common, check for them first.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 if (CheckEac3(buffer + offset, buffer_size - offset)) 1658 if (CheckEac3(buffer + offset, buffer_size - offset))
1673 return CONTAINER_EAC3; 1659 return CONTAINER_EAC3;
1674 } 1660 }
1675 1661
1676 return CONTAINER_UNKNOWN; 1662 return CONTAINER_UNKNOWN;
1677 } 1663 }
1678 1664
1679 } // namespace container_names 1665 } // namespace container_names
1680 1666
1681 } // namespace media 1667 } // namespace media
OLDNEW
« no previous file with comments | « no previous file | media/base/container_names_unittest.cc » ('j') | media/base/container_names_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698