File: mpeglayer1.cpp

package info (click to toggle)
smpeg 0.4.4-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,892 kB
  • ctags: 1,445
  • sloc: cpp: 14,948; sh: 8,962; ansic: 2,323; asm: 542; makefile: 162
file content (111 lines) | stat: -rw-r--r-- 3,169 bytes parent folder | download | duplicates (10)
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
/* MPEG/WAVE Sound library

   (C) 1997 by Jung woo-jae */

// Mpeglayer1.cc
// It's for MPEG Layer 1

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "MPEGaudio.h"
#if defined(_WIN32) && defined(_MSC_VER)
// disable warnings about double to float conversions
#pragma warning(disable: 4244 4305)
#endif

// Tables for layer 1
static const REAL factortable[15] = 
{
  0.0, 
  (1.0/2.0)    * (4.0/3.0),         (1.0/4.0)     * (8.0/7.0),
  (1.0/8.0)    * (16.0/15.0),       (1.0/16.0)    * (32.0/31.0),
  (1.0/32.0)   * (64.0/63.0),       (1.0/64.0)    * (128.0/127.0),
  (1.0/128.0)  * (256.0/255.0),     (1.0/256.0)   * (512.0/511.0),
  (1.0/512.0)  * (1024.0/1023.0),   (1.0/1024.0)  * (2048.0/2047.0),
  (1.0/2048.0) * (4096.0/4095.0),   (1.0/4096.0)  * (8192.0/8191.0),
  (1.0/8192.0) * (16384.0/16383.0), (1.0/16384.0) * (32768.0/32767.0)
};

static const REAL offsettable[15] = 
{
  0.0, 
  ((1.0/2.0)-1.0)    * (4.0/3.0),         ((1.0/4.0)-1.0)     * (8.0/7.0), 
  ((1.0/8.0)-1.0)    * (16.0/15.0),       ((1.0/16.0)-1.0)    * (32.0/31.0),
  ((1.0/32.0)-1.0)   * (64.0/63.0),       ((1.0/64.0)-1.0)    * (128.0/127.0),
  ((1.0/128.0)-1.0)  * (256.0/255.0),     ((1.0/256.0)-1.0)   * (512.0/511.0),
  ((1.0/512.0)-1.0)  * (1024.0/1023.0),   ((1.0/1024.0)-1.0)  * (2048.0/2047.0),
  ((1.0/2048.0)-1.0) * (4096.0/4095.0),   ((1.0/4096.0)-1.0)  * (8192.0/8191.0),
  ((1.0/8192.0)-1.0) * (16384.0/16383.0), ((1.0/16384.0)-1.0) * (32768.0/32767.0)
};

// Mpeg layer 1
void MPEGaudio::extractlayer1(void)
{
  REAL fraction[MAXCHANNEL][MAXSUBBAND];
  REAL scalefactor[MAXCHANNEL][MAXSUBBAND];

  int bitalloc[MAXCHANNEL][MAXSUBBAND],
      sample[MAXCHANNEL][MAXSUBBAND];

  register int i,j;
  int s=stereobound,l;


// Bitalloc
  for(i=0;i<s;i++)
  {
    bitalloc[LS][i]=getbits(4);
    bitalloc[RS][i]=getbits(4);
  }
  for(;i<MAXSUBBAND;i++)
    bitalloc[LS][i]=
    bitalloc[RS][i]=getbits(4);

// Scale index
  if(inputstereo)
    for(i=0;i<MAXSUBBAND;i++)
    {
      if(bitalloc[LS][i])scalefactor[LS][i]=scalefactorstable[getbits(6)];
      if(bitalloc[RS][i])scalefactor[RS][i]=scalefactorstable[getbits(6)];
    }
  else
    for(i=0;i<MAXSUBBAND;i++)
      if(bitalloc[LS][i])scalefactor[LS][i]=scalefactorstable[getbits(6)];

  for(l=0;l<SCALEBLOCK;l++)
  {
    // Sample
    for(i=0;i<s;i++)
    {
      if((j=bitalloc[LS][i]))sample[LS][i]=getbits(j+1);
      if((j=bitalloc[RS][i]))sample[RS][i]=getbits(j+1);
    }
    for(;i<MAXSUBBAND;i++)
      if((j=bitalloc[LS][i]))sample[LS][i]=sample[RS][i]=getbits(j+1);


    // Fraction
    if(outputstereo)
      for(i=0;i<MAXSUBBAND;i++)
      {
	if((j=bitalloc[LS][i]))
	  fraction[LS][i]=(REAL(sample[LS][i])*factortable[j]+offsettable[j])
			  *scalefactor[LS][i];
	else fraction[LS][i]=0.0;
	if((j=bitalloc[RS][i]))
	  fraction[RS][i]=(REAL(sample[RS][i])*factortable[j]+offsettable[j])
			  *scalefactor[RS][i];
	else fraction[RS][i]=0.0;
      }
    else
      for(i=0;i<MAXSUBBAND;i++)
	if((j=bitalloc[LS][i]))
	  fraction[LS][i]=(REAL(sample[LS][i])*factortable[j]+offsettable[j])
			  *scalefactor[LS][i];
	else fraction[LS][i]=0.0;

    subbandsynthesis(fraction[LS],fraction[RS]);
  }
}