~ubuntu-branches/ubuntu/karmic/asterisk/karmic

« back to all changes in this revision

Viewing changes to codecs/mp3/src/mhead.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2002-04-27 21:19:32 UTC
  • Revision ID: james.westby@ubuntu.com-20020427211932-kqaertc4bg7ss5mc
Tags: upstream-0.1.11
ImportĀ upstreamĀ versionĀ 0.1.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*____________________________________________________________________________
 
2
        
 
3
        FreeAmp - The Free MP3 Player
 
4
 
 
5
        MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
 
6
        Corp.  http://www.xingtech.com
 
7
 
 
8
        Portions Copyright (C) 1998-1999 EMusic.com
 
9
 
 
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.
 
14
 
 
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.
 
19
 
 
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.
 
23
        
 
24
        $Id: mhead.c,v 1.1 1999/12/07 05:46:02 markster Exp $
 
25
____________________________________________________________________________*/
 
26
 
 
27
/*------------ mhead.c ----------------------------------------------
 
28
  mpeg audio
 
29
  extract info from mpeg header
 
30
  portable version (adapted from c:\eco\mhead.c
 
31
 
 
32
  add Layer III
 
33
 
 
34
  mods 6/18/97 re mux restart, 32 bit ints
 
35
 
 
36
  mod 5/7/98 parse mpeg 2.5
 
37
 
 
38
---------------------------------------------------------------------*/
 
39
#include <stdlib.h>
 
40
#include <stdio.h>
 
41
#include <float.h>
 
42
#include <math.h>
 
43
#include "L3.h"
 
44
#include "mhead.h"              /* mpeg header structure */
 
45
 
 
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}};
 
51
 
 
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}};
 
55
 
 
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}};
 
59
 
 
60
 
 
61
 
 
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);
 
65
 
 
66
// jdw
 
67
extern unsigned int g_jdw_additional;
 
68
 
 
69
/*--------------------------------------------------------------*/
 
70
int head_info(unsigned char *buf, unsigned int n, MPEG_HEAD * h)
 
71
{
 
72
   int framebytes;
 
73
   int mpeg25_flag;
 
74
  
 
75
   if (n > 10000)
 
76
      n = 10000;                /* limit scan for free format */
 
77
 
 
78
 
 
79
 
 
80
   h->sync = 0;
 
81
   //if ((buf[0] == 0xFF) && ((buf[1] & 0xF0) == 0xF0))
 
82
   if ((buf[0] == 0xFF) && ((buf[0+1] & 0xF0) == 0xF0))
 
83
   {
 
84
      mpeg25_flag = 0;          // mpeg 1 & 2
 
85
 
 
86
   }
 
87
   else if ((buf[0] == 0xFF) && ((buf[0+1] & 0xF0) == 0xE0))
 
88
   {
 
89
      mpeg25_flag = 1;          // mpeg 2.5
 
90
 
 
91
   }
 
92
   else
 
93
      return 0;                 // sync fail
 
94
 
 
95
   h->sync = 1;
 
96
   if (mpeg25_flag)
 
97
      h->sync = 2;              //low bit clear signals mpeg25 (as in 0xFFE)
 
98
 
 
99
   h->id = (buf[0+1] & 0x08) >> 3;
 
100
   h->option = (buf[0+1] & 0x06) >> 1;
 
101
   h->prot = (buf[0+1] & 0x01);
 
102
 
 
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);
 
112
 
 
113
 
 
114
// if( mpeg25_flag ) {
 
115
 //    if( h->sr_index == 2 ) return 0;   // fail 8khz
 
116
 //}
 
117
 
 
118
 
 
119
/* compute framebytes for Layer I, II, III */
 
120
   if (h->option < 1)
 
121
      return 0;
 
122
   if (h->option > 3)
 
123
      return 0;
 
124
 
 
125
   framebytes = 0;
 
126
 
 
127
   if (h->br_index > 0)
 
128
   {
 
129
      if (h->option == 3)
 
130
      {                         /* layer I */
 
131
         framebytes =
 
132
            240 * mp_br_tableL1[h->id][h->br_index]
 
133
            / mp_sr20_table[h->id][h->sr_index];
 
134
         framebytes = 4 * framebytes;
 
135
      }
 
136
      else if (h->option == 2)
 
137
      {                         /* layer II */
 
138
         framebytes =
 
139
            2880 * mp_br_table[h->id][h->br_index]
 
140
            / mp_sr20_table[h->id][h->sr_index];
 
141
      }
 
142
      else if (h->option == 1)
 
143
      {                         /* layer III */
 
144
         if (h->id)
 
145
         {                      // mpeg1
 
146
 
 
147
            framebytes =
 
148
               2880 * mp_br_tableL3[h->id][h->br_index]
 
149
               / mp_sr20_table[h->id][h->sr_index];
 
150
         }
 
151
         else
 
152
         {                      // mpeg2
 
153
 
 
154
            if (mpeg25_flag)
 
155
            {                   // mpeg2.2
 
156
 
 
157
               framebytes =
 
158
                  2880 * mp_br_tableL3[h->id][h->br_index]
 
159
                  / mp_sr20_table[h->id][h->sr_index];
 
160
            }
 
161
            else
 
162
            {
 
163
               framebytes =
 
164
                  1440 * mp_br_tableL3[h->id][h->br_index]
 
165
                  / mp_sr20_table[h->id][h->sr_index];
 
166
            }
 
167
         }
 
168
      }
 
169
   }
 
170
   else
 
171
      framebytes = find_sync(buf, n);   /* free format */
 
172
 
 
173
  return framebytes;
 
174
}
 
175
 
 
176
int head_info3(unsigned char *buf, unsigned int n, MPEG_HEAD *h, int *br, unsigned int *searchForward) {
 
177
        unsigned int pBuf = 0;
 
178
        // jdw insertion...
 
179
   while ((pBuf < n) && !((buf[pBuf] == 0xFF) && 
 
180
          ((buf[pBuf+1] & 0xF0) == 0xF0 || (buf[pBuf+1] & 0xF0) == 0xE0))) 
 
181
   {
 
182
                pBuf++;
 
183
   }
 
184
 
 
185
   if (pBuf == n) return 0;
 
186
 
 
187
   *searchForward = pBuf;
 
188
   return head_info2(&(buf[pBuf]),n,h,br);
 
189
 
 
190
}
 
191
 
 
192
/*--------------------------------------------------------------*/
 
193
int head_info2(unsigned char *buf, unsigned int n, MPEG_HEAD * h, int *br)
 
194
{
 
195
   int framebytes;
 
196
 
 
197
/*---  return br (in bits/sec) in addition to frame bytes ---*/
 
198
 
 
199
   *br = 0;
 
200
/*-- assume fail --*/
 
201
   framebytes = head_info(buf, n, h);
 
202
 
 
203
   if (framebytes == 0)
 
204
      return 0; 
 
205
 
 
206
   if (h->option == 1)
 
207
   {                            /* layer III */
 
208
      if (h->br_index > 0)
 
209
         *br = 1000 * mp_br_tableL3[h->id][h->br_index];
 
210
      else
 
211
      {
 
212
         if (h->id)             // mpeg1
 
213
 
 
214
            *br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index] / (144 * 20);
 
215
         else
 
216
         {                      // mpeg2
 
217
 
 
218
            if ((h->sync & 1) == 0)     //  flags mpeg25
 
219
 
 
220
               *br = 500 * framebytes * mp_sr20_table[h->id][h->sr_index] / (72 * 20);
 
221
            else
 
222
               *br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index] / (72 * 20);
 
223
         }
 
224
      }
 
225
   }
 
226
   if (h->option == 2)
 
227
   {                            /* layer II */
 
228
      if (h->br_index > 0)
 
229
         *br = 1000 * mp_br_table[h->id][h->br_index];
 
230
      else
 
231
         *br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index]
 
232
            / (144 * 20);
 
233
   }
 
234
   if (h->option == 3)
 
235
   {                            /* layer I */
 
236
      if (h->br_index > 0)
 
237
         *br = 1000 * mp_br_tableL1[h->id][h->br_index];
 
238
      else
 
239
         *br = 1000 * framebytes * mp_sr20_table[h->id][h->sr_index]
 
240
            / (48 * 20);
 
241
   }
 
242
 
 
243
 
 
244
   return framebytes;
 
245
}
 
246
/*--------------------------------------------------------------*/
 
247
static int compare(unsigned char *buf, unsigned char *buf2)
 
248
{
 
249
   if (buf[0] != buf2[0])
 
250
      return 0;
 
251
   if (buf[1] != buf2[1])
 
252
      return 0;
 
253
   return 1;
 
254
}
 
255
/*----------------------------------------------------------*/
 
256
/*-- does not scan for initial sync, initial sync assumed --*/
 
257
static int find_sync(unsigned char *buf, int n)
 
258
{
 
259
   int i0, isync, nmatch, pad;
 
260
   int padbytes, option;
 
261
 
 
262
/* mod 4/12/95 i0 change from 72, allows as low as 8kbits for mpeg1 */
 
263
   i0 = 24;
 
264
   padbytes = 1;
 
265
   option = (buf[1] & 0x06) >> 1;
 
266
   if (option == 3)
 
267
   {
 
268
      padbytes = 4;
 
269
      i0 = 24;                  /* for shorter layer I frames */
 
270
   }
 
271
 
 
272
   pad = (buf[2] & 0x02) >> 1;
 
273
 
 
274
   n -= 3;                      /*  need 3 bytes of header  */
 
275
 
 
276
   while (i0 < 2000)
 
277
   {
 
278
      isync = sync_scan(buf, n, i0);
 
279
      i0 = isync + 1;
 
280
      isync -= pad;
 
281
      if (isync <= 0)
 
282
         return 0;
 
283
      nmatch = sync_test(buf, n, isync, padbytes);
 
284
      if (nmatch > 0)
 
285
         return isync;
 
286
   }
 
287
 
 
288
   return 0;
 
289
}
 
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)
 
294
{
 
295
   int i;
 
296
 
 
297
   for (i = i0; i < n; i++)
 
298
      if (compare(buf, buf + i))
 
299
         return i;
 
300
 
 
301
   return 0;
 
302
}
 
303
/*------------------------------------------------------*/
 
304
/*- test consecutative syncs, input isync without pad --*/
 
305
static int sync_test(unsigned char *buf, int n, int isync, int padbytes)
 
306
{
 
307
   int i, nmatch, pad;
 
308
 
 
309
   nmatch = 0;
 
310
   for (i = 0;;)
 
311
   {
 
312
      pad = padbytes * ((buf[i + 2] & 0x02) >> 1);
 
313
      i += (pad + isync);
 
314
      if (i > n)
 
315
         break;
 
316
      if (!compare(buf, buf + i))
 
317
         return -nmatch;
 
318
      nmatch++;
 
319
   }
 
320
   return nmatch;
 
321
}