~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/lib/splay/mpeglayer2.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* MPEG/WAVE Sound library
 
2
 
 
3
   (C) 1997 by Jung woo-jae */
 
4
 
 
5
// Mpeglayer2.cc
 
6
// It's for MPEG Layer 2
 
7
 
 
8
 
 
9
#include "mpegsound.h"
 
10
#include "synthesis.h"
 
11
 
 
12
 
 
13
#define BUGFIX
 
14
#include "mpeg2tables.h"
 
15
 
 
16
 
 
17
// workaround for buggy mpeg2 streams.
 
18
// tested with 12 monkey cdi, worgked fine.
 
19
// problem was: the stream produced ints
 
20
// with access out of the tables
 
21
// if we have such an access we set it to a zero entry
 
22
#ifdef BUGFIX
 
23
static int checkCodeRange(int code,const REAL* group) {
 
24
  int back=0;
 
25
  if (group == NULL) {
 
26
    cout << "group null"<<endl;
 
27
    return 0;
 
28
  }
 
29
  back=code;
 
30
 
 
31
  if (group == group5bits) {
 
32
    if (back > 27*3) {
 
33
      // redirect to zero value
 
34
      back=3;
 
35
    }
 
36
    return back;
 
37
  }
 
38
  if (group == group7bits) {
 
39
    if (back > 125*3) {
 
40
      back=6;
 
41
    }
 
42
    return back;
 
43
  }  
 
44
  if (group == group10bits) {
 
45
    if (back > 729*3) {
 
46
      back=12;
 
47
    }
 
48
    return back;
 
49
  }
 
50
  DEBUG_LAYER(cout << "unknown group found!"<<endl;)
 
51
  return -1;
 
52
}
 
53
 
 
54
 
 
55
#endif
 
56
 
 
57
 
 
58
 
 
59
 
 
60
 
 
61
 
 
62
 
 
63
 
 
64
// Mpeg layer 2
 
65
void Mpegtoraw::extractlayer2(void) {
 
66
  int inputstereo=mpegAudioHeader->getInputstereo();
 
67
  int tableindex=mpegAudioHeader->getTableindex();
 
68
  int subbandnumber=mpegAudioHeader->getSubbandnumber();
 
69
  int stereobound=mpegAudioHeader->getStereobound();
 
70
 
 
71
  REAL fraction[MAXCHANNEL][3][MAXSUBBAND];
 
72
  unsigned int bitalloc[MAXCHANNEL][MAXSUBBAND],
 
73
               scaleselector[MAXCHANNEL][MAXSUBBAND];
 
74
  REAL scalefactor[2][3][MAXSUBBAND];
 
75
 
 
76
  const REAL *group[MAXCHANNEL][MAXSUBBAND];
 
77
  unsigned int codelength[MAXCHANNEL][MAXSUBBAND];
 
78
  REAL factor[MAXCHANNEL][MAXSUBBAND];
 
79
  REAL c[MAXCHANNEL][MAXSUBBAND],d[MAXCHANNEL][MAXSUBBAND];
 
80
 
 
81
  int s=stereobound,n=subbandnumber;
 
82
  
 
83
 
 
84
  // Bitalloc
 
85
  {
 
86
    register int i;
 
87
    register const int *t=bitalloclengthtable[tableindex];
 
88
    for(i=0;i<s;i++,t++)
 
89
    {
 
90
      bitalloc[LS][i]=getbits(*t);
 
91
      bitalloc[RS][i]=getbits(*t);
 
92
    }
 
93
    for(;i<n;i++,t++) {
 
94
      bitalloc[LS][i]=bitalloc[RS][i]=getbits(*t);
 
95
    }
 
96
  }
 
97
 
 
98
 
 
99
 
 
100
  // Scale selector
 
101
  if(inputstereo)
 
102
    for(register int i=0;i<n;i++)
 
103
    {
 
104
      if(bitalloc[LS][i])scaleselector[LS][i]=getbits(2);
 
105
      if(bitalloc[RS][i])scaleselector[RS][i]=getbits(2);
 
106
    }
 
107
  else
 
108
    for(register int i=0;i<n;i++)
 
109
      if(bitalloc[LS][i])scaleselector[LS][i]=getbits(2);
 
110
 
 
111
  // Scale index
 
112
  {
 
113
    register int i,j;
 
114
 
 
115
 
 
116
    for(i=0;i<n;i++)
 
117
    {
 
118
      if((j=bitalloc[LS][i]))
 
119
      {
 
120
        if(!tableindex)
 
121
        {
 
122
          group[LS][i]=grouptableA[j];
 
123
          codelength[LS][i]=codelengthtableA[j];
 
124
          factor[LS][i]=factortableA[j];
 
125
          c[LS][i]=ctableA[j];
 
126
          d[LS][i]=dtableA[j];
 
127
        }
 
128
        else
 
129
        {
 
130
          if(i<=2)
 
131
          {
 
132
            group[LS][i]=grouptableB1[j];
 
133
            codelength[LS][i]=codelengthtableB1[j];
 
134
            factor[LS][i]=factortableB1[j];
 
135
            c[LS][i]=ctableB1[j];
 
136
            d[LS][i]=dtableB1[j];
 
137
          }
 
138
          else
 
139
          {
 
140
            group[LS][i]=grouptableB234[j];
 
141
            if(i<=10)
 
142
            {
 
143
              codelength[LS][i]=codelengthtableB2[j];
 
144
              factor[LS][i]=factortableB2[j];
 
145
              c[LS][i]=ctableB2[j];
 
146
              d[LS][i]=dtableB2[j];
 
147
            }
 
148
            else if(i<=22)
 
149
            {
 
150
              codelength[LS][i]=codelengthtableB3[j];
 
151
              factor[LS][i]=factortableB3[j];
 
152
              c[LS][i]=ctableB3[j];
 
153
              d[LS][i]=dtableB3[j];
 
154
            }
 
155
            else
 
156
            {
 
157
              codelength[LS][i]=codelengthtableB4[j];
 
158
              factor[LS][i]=factortableB4[j];
 
159
              c[LS][i]=ctableB4[j];
 
160
              d[LS][i]=dtableB4[j];
 
161
            }
 
162
          }
 
163
        }
 
164
 
 
165
 
 
166
        switch(scaleselector[LS][i])
 
167
        {
 
168
          case 0:scalefactor[LS][0][i]=scalefactorstable[getbits(6)];
 
169
                 scalefactor[LS][1][i]=scalefactorstable[getbits(6)];
 
170
                 scalefactor[LS][2][i]=scalefactorstable[getbits(6)];
 
171
                 break;
 
172
          case 1:scalefactor[LS][0][i]=
 
173
                 scalefactor[LS][1][i]=scalefactorstable[getbits(6)];
 
174
                 scalefactor[LS][2][i]=scalefactorstable[getbits(6)];
 
175
                 break;
 
176
          case 2:scalefactor[LS][0][i]=
 
177
                 scalefactor[LS][1][i]=
 
178
                 scalefactor[LS][2][i]=scalefactorstable[getbits(6)];
 
179
                 break;
 
180
          case 3:scalefactor[LS][0][i]=scalefactorstable[getbits(6)];
 
181
                 scalefactor[LS][1][i]=
 
182
                 scalefactor[LS][2][i]=scalefactorstable[getbits(6)];
 
183
                 break;
 
184
        default:
 
185
          cout << "scaleselector left default never happens"<<endl;
 
186
          break;
 
187
        }
 
188
      }
 
189
 
 
190
 
 
191
      if(inputstereo && (j=bitalloc[RS][i]))
 
192
      {
 
193
        if(!tableindex)
 
194
        {
 
195
          group[RS][i]=grouptableA[j];
 
196
          codelength[RS][i]=codelengthtableA[j];
 
197
          factor[RS][i]=factortableA[j];
 
198
          c[RS][i]=ctableA[j];
 
199
          d[RS][i]=dtableA[j];
 
200
        }
 
201
        else
 
202
        {
 
203
          if(i<=2)
 
204
          {
 
205
            group[RS][i]=grouptableB1[j];
 
206
            codelength[RS][i]=codelengthtableB1[j];
 
207
            factor[RS][i]=factortableB1[j];
 
208
            c[RS][i]=ctableB1[j];
 
209
            d[RS][i]=dtableB1[j];
 
210
          }
 
211
          else
 
212
          {
 
213
            group[RS][i]=grouptableB234[j];
 
214
            if(i<=10)
 
215
            {
 
216
              codelength[RS][i]=codelengthtableB2[j];
 
217
              factor[RS][i]=factortableB2[j];
 
218
              c[RS][i]=ctableB2[j];
 
219
              d[RS][i]=dtableB2[j];
 
220
            }
 
221
            else if(i<=22)
 
222
            {
 
223
              codelength[RS][i]=codelengthtableB3[j];
 
224
              factor[RS][i]=factortableB3[j];
 
225
              c[RS][i]=ctableB3[j];
 
226
              d[RS][i]=dtableB3[j];
 
227
            }
 
228
            else
 
229
            {
 
230
              codelength[RS][i]=codelengthtableB4[j];
 
231
              factor[RS][i]=factortableB4[j];
 
232
              c[RS][i]=ctableB4[j];
 
233
              d[RS][i]=dtableB4[j];
 
234
            }
 
235
          }
 
236
        }
 
237
 
 
238
  
 
239
        switch(scaleselector[RS][i])
 
240
        {
 
241
          case 0 : scalefactor[RS][0][i]=scalefactorstable[getbits(6)];
 
242
                   scalefactor[RS][1][i]=scalefactorstable[getbits(6)];
 
243
                   scalefactor[RS][2][i]=scalefactorstable[getbits(6)];
 
244
                   break;
 
245
          case 1 : scalefactor[RS][0][i]=
 
246
                   scalefactor[RS][1][i]=scalefactorstable[getbits(6)];
 
247
                   scalefactor[RS][2][i]=scalefactorstable[getbits(6)];
 
248
                   break;
 
249
          case 2 : scalefactor[RS][0][i]=
 
250
                   scalefactor[RS][1][i]=
 
251
                   scalefactor[RS][2][i]=scalefactorstable[getbits(6)];
 
252
                   break;
 
253
          case 3 : scalefactor[RS][0][i]=scalefactorstable[getbits(6)];
 
254
                   scalefactor[RS][1][i]=
 
255
                   scalefactor[RS][2][i]=scalefactorstable[getbits(6)];
 
256
                   break;
 
257
        default:
 
258
          cout << "scaleselector right default never happens"<<endl;
 
259
          break;
 
260
        }
 
261
 
 
262
 
 
263
      }
 
264
    }
 
265
  }
 
266
 
 
267
 
 
268
 
 
269
// Read Sample
 
270
  {
 
271
    register int i;
 
272
 
 
273
    for(int l=0;l<SCALEBLOCK;l++)
 
274
    {
 
275
      // Read Sample
 
276
      for(i=0;i<s;i++)
 
277
      {
 
278
        if(bitalloc[LS][i])
 
279
        {
 
280
          if(group[LS][i])
 
281
          {
 
282
            register const REAL *s;
 
283
            int code=getbits(codelength[LS][i]);
 
284
 
 
285
 
 
286
 
 
287
            code+=code<<1;
 
288
#ifdef BUGFIX
 
289
            // bugfix for bad streams
 
290
            code=checkCodeRange(code,group[LS][i]);
 
291
            if (code == -1) return;
 
292
#endif
 
293
            s=group[LS][i]+code;
 
294
 
 
295
            fraction[LS][0][i]=s[0];
 
296
            fraction[LS][1][i]=s[1];
 
297
            fraction[LS][2][i]=s[2];
 
298
          }
 
299
          else
 
300
          {
 
301
            fraction[LS][0][i]=
 
302
              REAL(getbits(codelength[LS][i]))*factor[LS][i]-1.0;
 
303
            fraction[LS][1][i]=
 
304
              REAL(getbits(codelength[LS][i]))*factor[LS][i]-1.0;
 
305
            fraction[LS][2][i]=
 
306
              REAL(getbits(codelength[LS][i]))*factor[LS][i]-1.0;
 
307
          }
 
308
        }
 
309
        else fraction[LS][0][i]=fraction[LS][1][i]=fraction[LS][2][i]=0.0;
 
310
 
 
311
 
 
312
        if(inputstereo && bitalloc[RS][i])
 
313
        {
 
314
          if(group[RS][i])
 
315
          {
 
316
            const REAL *s;
 
317
            int code=getbits(codelength[RS][i]);
 
318
 
 
319
            code+=code<<1;
 
320
#ifdef BUGFIX
 
321
            // bugfix for bad streams
 
322
            code=checkCodeRange(code,group[RS][i]);
 
323
            if (code == -1) return;
 
324
#endif
 
325
            s=group[RS][i]+code;
 
326
 
 
327
            fraction[RS][0][i]=s[0];
 
328
            fraction[RS][1][i]=s[1];
 
329
            fraction[RS][2][i]=s[2];
 
330
          }
 
331
          else
 
332
          {
 
333
            fraction[RS][0][i]=
 
334
              REAL(getbits(codelength[RS][i]))*factor[RS][i]-1.0;
 
335
            fraction[RS][1][i]=
 
336
              REAL(getbits(codelength[RS][i]))*factor[RS][i]-1.0;
 
337
            fraction[RS][2][i]=
 
338
              REAL(getbits(codelength[RS][i]))*factor[RS][i]-1.0;
 
339
          }
 
340
        }
 
341
        else fraction[RS][0][i]=fraction[RS][1][i]=fraction[RS][2][i]=0.0;
 
342
      }
 
343
 
 
344
 
 
345
     for(;i<n;i++)
 
346
      {
 
347
        if(bitalloc[LS][i])
 
348
        {
 
349
          if(group[LS][i])
 
350
          {
 
351
            register const REAL *s;
 
352
            int code=getbits(codelength[LS][i]);
 
353
 
 
354
            code+=code<<1;
 
355
#ifdef BUGFIX
 
356
            // bugfix for bad streams
 
357
            code=checkCodeRange(code,group[LS][i]);
 
358
            if (code == -1) return;
 
359
#endif
 
360
            s=group[LS][i]+code;
 
361
 
 
362
            fraction[LS][0][i]=fraction[RS][0][i]=s[0];
 
363
            fraction[LS][1][i]=fraction[RS][1][i]=s[1];
 
364
            fraction[LS][2][i]=fraction[RS][2][i]=s[2];
 
365
          }
 
366
          else
 
367
          {
 
368
            fraction[LS][0][i]=fraction[RS][0][i]=
 
369
              REAL(getbits(codelength[LS][i]))*factor[LS][i]-1.0;
 
370
            fraction[LS][1][i]=fraction[RS][1][i]=
 
371
              REAL(getbits(codelength[LS][i]))*factor[LS][i]-1.0;
 
372
            fraction[LS][2][i]=fraction[RS][2][i]=
 
373
              REAL(getbits(codelength[LS][i]))*factor[LS][i]-1.0;
 
374
          }
 
375
        }
 
376
        else fraction[LS][0][i]=fraction[LS][1][i]=fraction[LS][2][i]=
 
377
             fraction[RS][0][i]=fraction[RS][1][i]=fraction[RS][2][i]=0.0;
 
378
      }
 
379
 
 
380
 
 
381
 
 
382
 
 
383
      //Fraction
 
384
      if(lOutputStereo)
 
385
        for(i=0;i<n;i++)
 
386
        {
 
387
          if(bitalloc[LS][i])
 
388
          {
 
389
            if(!group[LS][i])
 
390
            {
 
391
              fraction[LS][0][i]=(fraction[LS][0][i]+d[LS][i])*c[LS][i];
 
392
              fraction[LS][1][i]=(fraction[LS][1][i]+d[LS][i])*c[LS][i];
 
393
              fraction[LS][2][i]=(fraction[LS][2][i]+d[LS][i])*c[LS][i];
 
394
            }
 
395
 
 
396
            register REAL t=scalefactor[LS][l>>2][i];
 
397
            fraction[LS][0][i]*=t;
 
398
            fraction[LS][1][i]*=t;
 
399
            fraction[LS][2][i]*=t;
 
400
          }
 
401
 
 
402
          if(bitalloc[RS][i])
 
403
          {
 
404
            if(!group[RS][i])
 
405
            {
 
406
              fraction[RS][0][i]=(fraction[RS][0][i]+d[RS][i])*c[LS][i];
 
407
              fraction[RS][1][i]=(fraction[RS][1][i]+d[RS][i])*c[LS][i];
 
408
              fraction[RS][2][i]=(fraction[RS][2][i]+d[RS][i])*c[LS][i];
 
409
            }
 
410
 
 
411
            register REAL t=scalefactor[RS][l>>2][i];
 
412
            fraction[RS][0][i]*=t;
 
413
            fraction[RS][1][i]*=t;
 
414
            fraction[RS][2][i]*=t;
 
415
          }
 
416
        }
 
417
      else
 
418
        for(i=0;i<n;i++)
 
419
          if(bitalloc[LS][i])
 
420
          {
 
421
            if(!group[LS][i])
 
422
            {
 
423
              fraction[LS][0][i]=(fraction[LS][0][i]+d[LS][i])*c[LS][i];
 
424
              fraction[LS][1][i]=(fraction[LS][1][i]+d[LS][i])*c[LS][i];
 
425
              fraction[LS][2][i]=(fraction[LS][2][i]+d[LS][i])*c[LS][i];
 
426
            }
 
427
 
 
428
            register REAL t=scalefactor[LS][l>>2][i];
 
429
            fraction[LS][0][i]*=t;
 
430
            fraction[LS][1][i]*=t;
 
431
            fraction[LS][2][i]*=t;
 
432
          }
 
433
 
 
434
 
 
435
      for(;i<MAXSUBBAND;i++)
 
436
        fraction[LS][0][i]=fraction[LS][1][i]=fraction[LS][2][i]=
 
437
        fraction[RS][0][i]=fraction[RS][1][i]=fraction[RS][2][i]=0.0;
 
438
 
 
439
      for(i=0;i<3;i++) {
 
440
        synthesis->doSynth(lDownSample,lOutputStereo,
 
441
                           fraction[LS][i],fraction[RS][i]);
 
442
      }
 
443
 
 
444
    }
 
445
  }
 
446
}