-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPEGRipper.cpp
136 lines (108 loc) · 3.55 KB
/
MPEGRipper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/***********************************************************************
* Copyright 2007-2010 Michael Drueing <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include <cstring>
#include <cstdio>
#include "GlobalDefs.h"
#include "MPEGRipper.h"
/*
* TODO: Make this aware of LayerI / LayerII files also
*/
const char *MPEGRipper::s_name = "MPEG Layer 3 Ripper v1.0";
const HeaderStruct MPEGRipper::s_headers[] = {
HS_F("\xff\xfb", 2, 0, (void *)0) // MPEG I Layer III, no CRC
HS_F("\xff\xfa", 2, 0, (void *)1) // MPEG I Layer III, with 16-bit CRC
// HS_F("\x00\x00\x01\xba", 4, 0, (void *)2) // MPEG I sequence, not handled yet
HS_END
};
static const int MPEG1Layer3Bitrates[] = {
-1, 32, 40, 48,
56, 64, 80, 96,
112, 128, 160, 192,
224, 256, 320, -1,
};
static const int MPEG1SampleRates[] = {
44100, 48000, 32000, -1,
};
bool MPEGRipper::isValidMP3Header(unsigned char **pos, const HeaderStruct *header)
{
uint16 CRC;
bool hasCRC;
unsigned char frameHeader[4];
int frameLength;
int bitRate;
int sampleRate;
int padding;
int priv;
if (*pos + 4 >= m_start + m_length)
return false;
hasCRC = (int)(header->user_data) == 1;
memcpy(frameHeader, *pos, 4);
*pos += 4;
bitRate = MPEG1Layer3Bitrates[(frameHeader[2] >> 4) & 0x0f];
sampleRate = MPEG1SampleRates[(frameHeader[2] >> 2) & 0x03];
padding = (frameHeader[2] >> 1) & 0x01;
priv = frameHeader[2] & 0x01;
// check for correct SYNC bits
if (frameHeader[0] != 0xff)
return false;
if (((frameHeader[1] >> 5) & 0x07) != 7)
return false;
// read CRC if needed
if (hasCRC)
{
if (*pos + 2 >= m_start + m_length)
return false;
CRC = *((uint16 *)(*pos));
*pos += 2;
}
if ((bitRate == -1) || (sampleRate == -1))
return false;
// calculate frame length (this is for MPEG I layer 3 only!)
frameLength = 144 * bitRate * 1000 / sampleRate + padding;
if (*pos + frameLength >= m_start + m_length)
return false;
// TODO: Check CRC if present
// frame length includes the 4 header and two (if present) crc bytes
*pos += frameLength - 4 - (hasCRC ? 2 : 0);
//printf("DEBUG: crc=0x%04x, length %d, bitrate= %d, samplerate = %d, priv=%d, pad=%d\n", hasCRC ? CRC : 0, frameLength, bitRate, sampleRate, priv, padding);
return true;
}
bool MPEGRipper::checkLocation(unsigned char *pos, const HeaderStruct *header, FoundStruct *found)
{
unsigned char *pos1, *pos2;
int numFrames = 0;
pos1 = pos;
numFrames = 0;
while (isValidMP3Header(&pos, header))
{
numFrames++;
}
pos2 = pos;
// reject MP3s with fewer than 3 valid frame headers (arbitrary decision!)
if (numFrames < 3)
return false;
// if fewer than 20 frames (that's about 1 second best case with 22khz mono,
// 44.1khz stereo brings it down to 0.25 seconds)
if (numFrames < 20)
found->criterium = CRIT_WEAK;
else
found->criterium = CRIT_STRONG;
found->length = pos2-pos1;
found->startoffset = pos1;
strcpy(found->extension, "mp3");
return true;
}