1 /*
2 ogmmerge -- utility for splicing together ogg bitstreams
3 from component media subtypes
4
5 mp3_common.cpp
6 common routines for MP3 handling
7
8 Written by Moritz Bunkus <moritz@bunkus.org>
9 Based on Xiph.org's 'oggmerge' found in their CVS repository
10 See http://www.xiph.org
11
12 Distributed under the GPL
13 see the file COPYING for details
14 or visit http://www.gnu.org/copyleft/gpl.html
15 */
16
17 #include "mp3_common.h"
18 #include "common.h"
19
20 int mp3_tabsel[2][16] =
21 {{0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320},
22 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}};
23
24 long mp3_freqs[9] =
25 {44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000};
26
find_mp3_header(char * buf,int size,unsigned long * _header)27 int find_mp3_header(char *buf, int size, unsigned long *_header) {
28 int i;
29 unsigned long header;
30 int pos;
31
32 if (size < 4)
33 return -1;
34
35 for (pos = 0; pos <= (size - 4); pos++) {
36 for (i = 0, header = 0; i < 4; i++) {
37 header <<= 8;
38 header |= (unsigned char)buf[i + pos];
39 }
40
41 if ((header == FOURCC('R', 'I', 'F', 'F')) ||
42 ((header & 0xffffff00) == FOURCC('I', 'D', '3', ' ')))
43 continue;
44
45 if ((header & 0xffe00000) != 0xffe00000)
46 continue;
47 if (!((header >> 17) & 3))
48 continue;
49 if (((header >> 12) & 0xf) == 0xf)
50 continue;
51 if (!((header >> 12) & 0xf))
52 continue;
53 if (((header >> 10) & 0x3) == 0x3)
54 continue;
55 if ((((header >> 19) & 1) == 1) && (((header >> 17) & 3) == 3) &&
56 (((header >> 16) & 1) == 1))
57 continue;
58 if ((header & 0xffff0000) == 0xfffe0000)
59 continue;
60 *_header = header;
61 return pos;
62 }
63 return -1;
64 }
65
decode_mp3_header(unsigned long header,mp3_header_t * h)66 void decode_mp3_header(unsigned long header, mp3_header_t *h) {
67 if (header & (1 << 20)) {
68 h->lsf = (header & (1 << 19)) ? 0 : 1;
69 h->mpeg25 = 0;
70 } else {
71 h->lsf = 1;
72 h->mpeg25 = 1;
73 }
74 h->mode = (header >> 6) & 3;
75 h->error_protection = ((header >> 16) & 1) ^ 1;
76 h->stereo = (h->mode == 3 ? 1 : 2);
77 if (h->lsf)
78 h->ssize = (h->stereo == 1 ? 9 : 17);
79 else
80 h->ssize = (h->stereo == 1 ? 17: 32);
81 if (h->error_protection)
82 h->ssize += 2;
83 h->bitrate_index = (header >> 12) & 15;
84 if (h->mpeg25)
85 h->sampling_frequency = 6 + ((header >> 10) & 3);
86 else
87 h->sampling_frequency = ((header >> 10) & 3) + (h->lsf * 3);
88 h->padding = (header >> 9) & 1;
89 h->framesize = (long)mp3_tabsel[h->lsf][h->bitrate_index] * 144000;
90 h->framesize /= (mp3_freqs[h->sampling_frequency] << h->lsf);
91 h->framesize = h->framesize + h->padding - 4;
92 }
93
94