1
/*____________________________________________________________________________
3
FreeAmp - The Free MP3 Player
5
MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
6
Corp. http://www.xingtech.com
8
Portions Copyright (C) 1998-1999 EMusic.com
10
This program is free software; you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation; either version 2 of the License, or
13
(at your option) any later version.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
$Id: mhead.c,v 1.1 1999/12/07 05:46:02 markster Exp $
25
____________________________________________________________________________*/
27
/*------------ mhead.c ----------------------------------------------
29
extract info from mpeg header
30
portable version (adapted from c:\eco\mhead.c
34
mods 6/18/97 re mux restart, 32 bit ints
36
mod 5/7/98 parse mpeg 2.5
38
---------------------------------------------------------------------*/
44
#include "mhead.h" /* mpeg header structure */
46
static int mp_br_table[2][16] =
47
{{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
48
{0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0}};
49
static int mp_sr20_table[2][4] =
50
{{441, 480, 320, -999}, {882, 960, 640, -999}};
52
static int mp_br_tableL1[2][16] =
53
{{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},/* mpeg2 */
54
{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0}};
56
static int mp_br_tableL3[2][16] =
57
{{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}, /* mpeg 2 */
58
{0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}};
62
static int find_sync(unsigned char *buf, int n);
63
static int sync_scan(unsigned char *buf, int n, int i0);
64
static int sync_test(unsigned char *buf, int n, int isync, int padbytes);
67
extern unsigned int g_jdw_additional;
69
/*--------------------------------------------------------------*/
70
int head_info(unsigned char *buf, unsigned int n, MPEG_HEAD * h)
76
n = 10000; /* limit scan for free format */
81
//if ((buf[0] == 0xFF) && ((buf[1] & 0xF0) == 0xF0))
82
if ((buf[0] == 0xFF) && ((buf[0+1] & 0xF0) == 0xF0))
84
mpeg25_flag = 0; // mpeg 1 & 2
87
else if ((buf[0] == 0xFF) && ((buf[0+1] & 0xF0) == 0xE0))
89
mpeg25_flag = 1; // mpeg 2.5
93
return 0; // sync fail
97
h->sync = 2; //low bit clear signals mpeg25 (as in 0xFFE)
99
h->id = (buf[0+1] & 0x08) >> 3;
100
h->option = (buf[0+1] & 0x06) >> 1;
101
h->prot = (buf[0+1] & 0x01);
103
h->br_index = (buf[0+2] & 0xf0) >> 4;
104
h->sr_index = (buf[0+2] & 0x0c) >> 2;
105
h->pad = (buf[0+2] & 0x02) >> 1;
106
h->private_bit = (buf[0+2] & 0x01);
107
h->mode = (buf[0+3] & 0xc0) >> 6;
108
h->mode_ext = (buf[0+3] & 0x30) >> 4;
109
h->cr = (buf[0+3] & 0x08) >> 3;
110
h->original = (buf[0+3] & 0x04) >> 2;
111
h->emphasis = (buf[0+3] & 0x03);
114
// if( mpeg25_flag ) {
115
// if( h->sr_index == 2 ) return 0; // fail 8khz
119
/* compute framebytes for Layer I, II, III */
132
240 * mp_br_tableL1[h->id][h->br_index]
133
/ mp_sr20_table[h->id][h->sr_index];
134
framebytes = 4 * framebytes;
136
else if (h->option == 2)
139
2880 * mp_br_table[h->id][h->br_index]
140
/ mp_sr20_table[h->id][h->sr_index];
142
else if (h->option == 1)
148
2880 * mp_br_tableL3[h->id][h->br_index]
149
/ mp_sr20_table[h->id][h->sr_index];
158
2880 * mp_br_tableL3[h->id][h->br_index]
159
/ mp_sr20_table[h->id][h->sr_index];
164
1440 * mp_br_tableL3[h->id][h->br_index]
165
/ mp_sr20_table[h->id][h->sr_index];
171
framebytes = find_sync(buf, n); /* free format */
176
int head_info3(unsigned char *buf, unsigned int n, MPEG_HEAD *h, int *br, unsigned int *searchForward) {
177
unsigned int pBuf = 0;
179
while ((pBuf < n) && !((buf[pBuf] == 0xFF) &&
180
((buf[pBuf+1] & 0xF0) == 0xF0 || (buf[pBuf+1] & 0xF0) == 0xE0)))
185
if (pBuf == n) return 0;
187
*searchForward = pBuf;
188
return head_info2(&(buf[pBuf]),n,h,br);
192
/*--------------------------------------------------------------*/
193
int head_info2(unsigned char *buf, unsigned int n, MPEG_HEAD * h, int *br)
197
/*--- return br (in bits/sec) in addition to frame bytes ---*/
200
/*-- assume fail --*/
201
framebytes = head_info(buf, n, h);
209
*br = 1000 * mp_br_tableL3[h->id][h->br_index];
214
*br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index] / (144 * 20);
218
if ((h->sync & 1) == 0) // flags mpeg25
220
*br = 500 * framebytes * mp_sr20_table[h->id][h->sr_index] / (72 * 20);
222
*br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index] / (72 * 20);
229
*br = 1000 * mp_br_table[h->id][h->br_index];
231
*br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index]
237
*br = 1000 * mp_br_tableL1[h->id][h->br_index];
239
*br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index]
246
/*--------------------------------------------------------------*/
247
static int compare(unsigned char *buf, unsigned char *buf2)
249
if (buf[0] != buf2[0])
251
if (buf[1] != buf2[1])
255
/*----------------------------------------------------------*/
256
/*-- does not scan for initial sync, initial sync assumed --*/
257
static int find_sync(unsigned char *buf, int n)
259
int i0, isync, nmatch, pad;
260
int padbytes, option;
262
/* mod 4/12/95 i0 change from 72, allows as low as 8kbits for mpeg1 */
265
option = (buf[1] & 0x06) >> 1;
269
i0 = 24; /* for shorter layer I frames */
272
pad = (buf[2] & 0x02) >> 1;
274
n -= 3; /* need 3 bytes of header */
278
isync = sync_scan(buf, n, i0);
283
nmatch = sync_test(buf, n, isync, padbytes);
290
/*------------------------------------------------------*/
291
/*---- scan for next sync, assume start is valid -------*/
292
/*---- return number bytes to next sync ----------------*/
293
static int sync_scan(unsigned char *buf, int n, int i0)
297
for (i = i0; i < n; i++)
298
if (compare(buf, buf + i))
303
/*------------------------------------------------------*/
304
/*- test consecutative syncs, input isync without pad --*/
305
static int sync_test(unsigned char *buf, int n, int isync, int padbytes)
312
pad = padbytes * ((buf[i + 2] & 0x02) >> 1);
316
if (!compare(buf, buf + i))