aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/mp3/src/mhead.c
blob: d368f095feb743e90ef751606ad17ece6f38499a (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*____________________________________________________________________________
	
	FreeAmp - The Free MP3 Player

        MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
        Corp.  http://www.xingtech.com

	Portions Copyright (C) 1998-1999 EMusic.com

	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) 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, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
	
	$Id$
____________________________________________________________________________*/

/*------------ mhead.c ----------------------------------------------
  mpeg audio
  extract info from mpeg header
  portable version (adapted from c:\eco\mhead.c

  add Layer III

  mods 6/18/97 re mux restart, 32 bit ints

  mod 5/7/98 parse mpeg 2.5

---------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
#include "L3.h"
#include "mhead.h"		/* mpeg header structure */

static int mp_br_table[2][16] =
{{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0}};
static int mp_sr20_table[2][4] =
{{441, 480, 320, -999}, {882, 960, 640, -999}};

static int mp_br_tableL1[2][16] =
{{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},/* mpeg2 */
 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0}};

static int mp_br_tableL3[2][16] =
{{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},      /* mpeg 2 */
 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}};



static int find_sync(unsigned char *buf, int n);
static int sync_scan(unsigned char *buf, int n, int i0);
static int sync_test(unsigned char *buf, int n, int isync, int padbytes);

// jdw
extern unsigned int g_jdw_additional;

/*--------------------------------------------------------------*/
int head_info(unsigned char *buf, unsigned int n, MPEG_HEAD * h)
{
   int framebytes;
   int mpeg25_flag;
  
   if (n > 10000)
      n = 10000;		/* limit scan for free format */



   h->sync = 0;
   //if ((buf[0] == 0xFF) && ((buf[1] & 0xF0) == 0xF0))
   if ((buf[0] == 0xFF) && ((buf[0+1] & 0xF0) == 0xF0))
   {
      mpeg25_flag = 0;		// mpeg 1 & 2

   }
   else if ((buf[0] == 0xFF) && ((buf[0+1] & 0xF0) == 0xE0))
   {
      mpeg25_flag = 1;		// mpeg 2.5

   }
   else
      return 0;			// sync fail

   h->sync = 1;
   if (mpeg25_flag)
      h->sync = 2;		//low bit clear signals mpeg25 (as in 0xFFE)

   h->id = (buf[0+1] & 0x08) >> 3;
   h->option = (buf[0+1] & 0x06) >> 1;
   h->prot = (buf[0+1] & 0x01);

   h->br_index = (buf[0+2] & 0xf0) >> 4;
   h->sr_index = (buf[0+2] & 0x0c) >> 2;
   h->pad = (buf[0+2] & 0x02) >> 1;
   h->private_bit = (buf[0+2] & 0x01);
   h->mode = (buf[0+3] & 0xc0) >> 6;
   h->mode_ext = (buf[0+3] & 0x30) >> 4;
   h->cr = (buf[0+3] & 0x08) >> 3;
   h->original = (buf[0+3] & 0x04) >> 2;
   h->emphasis = (buf[0+3] & 0x03);


// if( mpeg25_flag ) {
 //    if( h->sr_index == 2 ) return 0;   // fail 8khz
 //}


/* compute framebytes for Layer I, II, III */
   if (h->option < 1)
      return 0;
   if (h->option > 3)
      return 0;

   framebytes = 0;

   if (h->br_index > 0)
   {
      if (h->option == 3)
      {				/* layer I */
	 framebytes =
	    240 * mp_br_tableL1[h->id][h->br_index]
	    / mp_sr20_table[h->id][h->sr_index];
	 framebytes = 4 * framebytes;
      }
      else if (h->option == 2)
      {				/* layer II */
	 framebytes =
	    2880 * mp_br_table[h->id][h->br_index]
	    / mp_sr20_table[h->id][h->sr_index];
      }
      else if (h->option == 1)
      {				/* layer III */
	 if (h->id)
	 {			// mpeg1

	    framebytes =
	       2880 * mp_br_tableL3[h->id][h->br_index]
	       / mp_sr20_table[h->id][h->sr_index];
	 }
	 else
	 {			// mpeg2

	    if (mpeg25_flag)
	    {			// mpeg2.2

	       framebytes =
		  2880 * mp_br_tableL3[h->id][h->br_index]
		  / mp_sr20_table[h->id][h->sr_index];
	    }
	    else
	    {
	       framebytes =
		  1440 * mp_br_tableL3[h->id][h->br_index]
		  / mp_sr20_table[h->id][h->sr_index];
	    }
	 }
      }
   }
   else
      framebytes = find_sync(buf, n);	/* free format */

  return framebytes;
}

int head_info3(unsigned char *buf, unsigned int n, MPEG_HEAD *h, int *br, unsigned int *searchForward) {
	unsigned int pBuf = 0;
	// jdw insertion...
   while ((pBuf < n) && !((buf[pBuf] == 0xFF) && 
          ((buf[pBuf+1] & 0xF0) == 0xF0 || (buf[pBuf+1] & 0xF0) == 0xE0))) 
   {
		pBuf++;
   }

   if (pBuf == n) return 0;

   *searchForward = pBuf;
   return head_info2(&(buf[pBuf]),n,h,br);

}

/*--------------------------------------------------------------*/
int head_info2(unsigned char *buf, unsigned int n, MPEG_HEAD * h, int *br)
{
   int framebytes;

/*---  return br (in bits/sec) in addition to frame bytes ---*/

   *br = 0;
/*-- assume fail --*/
   framebytes = head_info(buf, n, h);

   if (framebytes == 0)
      return 0; 

   if (h->option == 1)
   {				/* layer III */
      if (h->br_index > 0)
	 *br = 1000 * mp_br_tableL3[h->id][h->br_index];
      else
      {
	 if (h->id)		// mpeg1

	    *br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index] / (144 * 20);
	 else
	 {			// mpeg2

	    if ((h->sync & 1) == 0)	//  flags mpeg25

	       *br = 500 * framebytes * mp_sr20_table[h->id][h->sr_index] / (72 * 20);
	    else
	       *br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index] / (72 * 20);
	 }
      }
   }
   if (h->option == 2)
   {				/* layer II */
      if (h->br_index > 0)
	 *br = 1000 * mp_br_table[h->id][h->br_index];
      else
	 *br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index]
	    / (144 * 20);
   }
   if (h->option == 3)
   {				/* layer I */
      if (h->br_index > 0)
	 *br = 1000 * mp_br_tableL1[h->id][h->br_index];
      else
	 *br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index]
	    / (48 * 20);
   }


   return framebytes;
}
/*--------------------------------------------------------------*/
static int compare(unsigned char *buf, unsigned char *buf2)
{
   if (buf[0] != buf2[0])
      return 0;
   if (buf[1] != buf2[1])
      return 0;
   return 1;
}
/*----------------------------------------------------------*/
/*-- does not scan for initial sync, initial sync assumed --*/
static int find_sync(unsigned char *buf, int n)
{
   int i0, isync, nmatch, pad;
   int padbytes, option;

/* mod 4/12/95 i0 change from 72, allows as low as 8kbits for mpeg1 */
   i0 = 24;
   padbytes = 1;
   option = (buf[1] & 0x06) >> 1;
   if (option == 3)
   {
      padbytes = 4;
      i0 = 24;			/* for shorter layer I frames */
   }

   pad = (buf[2] & 0x02) >> 1;

   n -= 3;			/*  need 3 bytes of header  */

   while (i0 < 2000)
   {
      isync = sync_scan(buf, n, i0);
      i0 = isync + 1;
      isync -= pad;
      if (isync <= 0)
	 return 0;
      nmatch = sync_test(buf, n, isync, padbytes);
      if (nmatch > 0)
	 return isync;
   }

   return 0;
}
/*------------------------------------------------------*/
/*---- scan for next sync, assume start is valid -------*/
/*---- return number bytes to next sync ----------------*/
static int sync_scan(unsigned char *buf, int n, int i0)
{
   int i;

   for (i = i0; i < n; i++)
      if (compare(buf, buf + i))
	 return i;

   return 0;
}
/*------------------------------------------------------*/
/*- test consecutative syncs, input isync without pad --*/
static int sync_test(unsigned char *buf, int n, int isync, int padbytes)
{
   int i, nmatch, pad;

   nmatch = 0;
   for (i = 0;;)
   {
      pad = padbytes * ((buf[i + 2] & 0x02) >> 1);
      i += (pad + isync);
      if (i > n)
	 break;
      if (!compare(buf, buf + i))
	 return -nmatch;
      nmatch++;
   }
   return nmatch;
}