2 * File: $RCSfile: SubBand1.pas,v $
3 * Revision: $Revision: 1.1.1.1 $
4 * Version : $Id: SubBand1.pas,v 1.1.1.1 2002/04/21 12:57:23 fobmagog Exp $
5 * Author: $Author: fobmagog $
6 * Homepage: http://delphimpeg.sourceforge.net/
7 * Kol translation by Thaddy de Koning
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 Windows
, kol
, kolShared
, kolBitStream
, kolHeader
, kolSynthFilter
, kolSubBand
, kolCRC
;
31 // class for layer I subbands in single channel mode:
32 PSubBandLayer1
= ^TSubbandlayer1
;
33 TSubBandLayer1
= object(TSubBand
)
35 FSubBandNumber
: cardinal;
36 FSampleNumber
: cardinal;
37 FAllocation
: cardinal;
39 FSampleLength
: cardinal;
41 FFactor
, FOffset
: single;
44 // constructor Create(SubBandNumber: Cardinal); virtual;
46 procedure ReadAllocation(Stream
: PBitStream
; Header
: PHeader
; CRC
: PCRC16
); virtual;
47 procedure ReadScaleFactor(Stream
: PBitStream
; Header
: PHeader
); virtual;
48 function ReadSampleData(Stream
: PBitStream
): boolean; virtual;
49 function PutNextSample(Channels
: TChannels
;
50 Filter1
, Filter2
: PSynthesisFilter
): boolean; virtual;
53 // class for layer I subbands in joint stereo mode:
54 PSubBandLayer1IntensityStereo
= ^TSubBandLayer1IntensityStereo
;
55 TSubBandLayer1IntensityStereo
= object(TSubBandLayer1
)
57 FChannel2ScaleFactor
: single;
60 procedure ReadScaleFactor(Stream
: PBitStream
; Header
: PHeader
); virtual;
61 function PutNextSample(Channels
: TChannels
;
62 Filter1
, Filter2
: PSynthesisFilter
): boolean; virtual;
65 // class for layer I subbands in stereo mode:
66 PSubbandlayer1Stereo
= ^TSubbandlayer1Stereo
;
67 TSubBandLayer1Stereo
= object(TSubBandLayer1
)
69 FChannel2Allocation
: cardinal;
70 FChannel2ScaleFactor
: single;
71 FChannel2SampleLength
: cardinal;
72 FChannel2Sample
: single;
73 FChannel2Factor
: single;
74 FChannel2Offset
: single;
77 procedure ReadAllocation(Stream
: PBitStream
; Header
: PHeader
; CRC
: PCRC16
); virtual;
78 procedure ReadScaleFactor(Stream
: PBitStream
; Header
: PHeader
); virtual;
79 function ReadSampleData(Stream
: PBitStream
): boolean; virtual;
80 function PutNextSample(Channels
: TChannels
;
81 Filter1
, Filter2
: PSynthesisFilter
): boolean; virtual;
83 function newSubBandLayer1(SubBandNumber
: cardinal): PSubBandLayer1
;
84 function newSubBandLayer1Stereo(SubBandNumber
: cardinal): PSubBandLayer1Stereo
;
85 function newSubBandLayer1Intensitystereo(SubBandNumber
: cardinal)
86 : PSubBandLayer1Intensitystereo
;
94 // factors and offsets for sample requantization:
95 TableFactor
: array[0..14] of single = (0.0, (1.0 / 2.0) * (4.0 / 3.0),
96 (1.0 / 4.0) * (8.0 / 7.0), (1.0 / 8.0) * (16.0 / 15.0),
97 (1.0 / 16.0) * (32.0 / 31.0), (1.0 / 32.0) * (64.0 / 63.0),
98 (1.0 / 64.0) * (128.0 / 127.0),
99 (1.0 / 128.0) * (256.0 / 255.0), (1.0 / 256.0) * (512.0 / 511.0),
100 (1.0 / 512.0) * (1024.0 / 1023.0), (1.0 / 1024.0) * (2048.0 / 2047.0),
101 (1.0 / 2048.0) * (4096.0 / 4095.0), (1.0 / 4096.0) * (8192.0 / 8191.0),
102 (1.0 / 8192.0) * (16384.0 / 16383.0), (1.0 / 16384.0) * (32768.0 / 32767.0));
104 TableOffset
: array[0..14] of single = (0.0, ((1.0 / 2.0) - 1.0) * (4.0 / 3.0),
105 ((1.0 / 4.0) - 1.0) * (8.0 / 7.0), ((1.0 / 8.0) - 1.0) * (16.0 / 15.0),
106 ((1.0 / 16.0) - 1.0) * (32.0 / 31.0), ((1.0 / 32.0) - 1.0) * (64.0 / 63.0),
107 ((1.0 / 64.0) - 1.0) * (128.0 / 127.0),
108 ((1.0 / 128.0) - 1.0) * (256.0 / 255.0), ((1.0 / 256.0) - 1.0) * (512.0 / 511.0),
109 ((1.0 / 512.0) - 1.0) * (1024.0 / 1023.0),
110 ((1.0 / 1024.0) - 1.0) * (2048.0 / 2047.0),
111 ((1.0 / 2048.0) - 1.0) * (4096.0 / 4095.0),
112 ((1.0 / 4096.0) - 1.0) * (8192.0 / 8191.0),
113 ((1.0 / 8192.0) - 1.0) * (16384.0 / 16383.0),
114 ((1.0 / 16384.0) - 1.0) * (32768.0 / 32767.0));
118 function newSubBandLayer1Intensitystereo(SubBandNumber
: cardinal)
119 : PSubBandLayer1Intensitystereo
;
122 Result
.FSubBandNumber
:= SubBandNumber
;
123 Result
.FSampleNumber
:= 0;
126 function newSubBandLayer1(SubBandNumber
: cardinal): PSubBandLayer1
;
129 Result
.FSubBandNumber
:= SubBandNumber
;
130 Result
.FSampleNumber
:= 0;
133 function newSubBandLayer1Stereo(SubBandNumber
: cardinal): PSubBandLayer1Stereo
;
136 Result
.FSubBandNumber
:= SubBandNumber
;
137 Result
.FSampleNumber
:= 0;
140 function TSubBandLayer1
.PutNextSample(Channels
: TChannels
; Filter1
,
141 Filter2
: PSynthesisFilter
): boolean;
143 ScaledSample
: single;
145 if (FAllocation
<> 0) and (Channels
<> Right
) then
147 ScaledSample
:= (FSample
* FFactor
+ FOffset
) * FScalefactor
;
148 Filter1
.InputSample(ScaledSample
, FSubBandNumber
);
154 procedure TSubBandLayer1
.ReadAllocation(Stream
: PBitStream
;
155 Header
: PHeader
; CRC
: PCRC16
);
157 FAllocation
:= Stream
.GetBits(4);
158 if (FAllocation
= 15) then;
159 // cerr << "WARNING: stream contains an illegal allocation!\n"; // MPEG-stream is corrupted!
162 CRC
.AddBits(FAllocation
, 4);
164 if (FAllocation
<> 0) then
166 FSampleLength
:= FAllocation
+ 1;
167 FFactor
:= TableFactor
[FAllocation
];
168 FOffset
:= TableOffset
[FAllocation
];
172 function TSubBandLayer1
.ReadSampleData(Stream
: PBitStream
): boolean;
174 if (FAllocation
<> 0) then
175 FSample
:= Stream
.GetBitsFloat(FSampleLength
);
178 if (FSampleNumber
= 12) then
187 procedure TSubBandLayer1
.ReadScaleFactor(Stream
: PBitStream
;
190 if (FAllocation
<> 0) then
191 FScalefactor
:= ScaleFactors
[Stream
.GetBits(6)];
194 { TSubBandLayer1IntensityStereo }
196 function TSubBandLayer1IntensityStereo
.PutNextSample(Channels
: TChannels
;
197 Filter1
, Filter2
: PSynthesisFilter
): boolean;
199 Sample1
, Sample2
: single;
201 if (FAllocation
<> 0) then
203 FSample
:= FSample
* FFactor
+ FOffset
; // requantization
204 if (Channels
= Both
) then
206 Sample1
:= FSample
* FScalefactor
;
207 Sample2
:= FSample
* FChannel2ScaleFactor
;
208 Filter1
.InputSample(Sample1
, FSubBandNumber
);
209 Filter2
.InputSample(Sample2
, FSubBandNumber
);
211 else if (Channels
= Left
) then
213 Sample1
:= FSample
* FScaleFactor
;
214 Filter1
.InputSample(Sample1
, FSubBandNumber
);
218 Sample2
:= FSample
* FChannel2ScaleFactor
;
219 Filter2
.InputSample(Sample2
, FSubBandNumber
);
226 procedure TSubBandLayer1IntensityStereo
.ReadScaleFactor(Stream
: PBitStream
;
229 if (FAllocation
<> 0) then
231 FScaleFactor
:= ScaleFactors
[Stream
.GetBits(6)];
232 FChannel2ScaleFactor
:= ScaleFactors
[Stream
.GetBits(6)];
236 { TSubBandLayer1Stereo }
238 function TSubBandLayer1Stereo
.PutNextSample(Channels
: TChannels
; Filter1
,
239 Filter2
: PSynthesisFilter
): boolean;
243 inherited PutNextSample(Channels
, Filter1
, Filter2
);
244 if (FChannel2Allocation
<> 0) and (Channels
<> Left
) then
246 Sample2
:= (FChannel2Sample
* FChannel2Factor
+ FChannel2Offset
) * FChannel2ScaleFactor
;
247 if (Channels
= Both
) then
248 Filter2
.InputSample(Sample2
, FSubBandNumber
)
250 Filter1
.InputSample(Sample2
, FSubBandNumber
);
256 procedure TSubBandLayer1Stereo
.ReadAllocation(Stream
: PBitStream
;
257 Header
: PHeader
; CRC
: PCRC16
);
259 FAllocation
:= Stream
.GetBits(4);
260 FChannel2Allocation
:= Stream
.GetBits(4);
263 CRC
.AddBits(FAllocation
, 4);
264 CRC
.AddBits(FChannel2Allocation
, 4);
267 if (FAllocation
<> 0) then
269 FSamplelength
:= FAllocation
+ 1;
270 FFactor
:= TableFactor
[FAllocation
];
271 FOffset
:= TableOffset
[FAllocation
];
274 if (FChannel2Allocation
<> 0) then
276 FChannel2SampleLength
:= FChannel2Allocation
+ 1;
277 FChannel2Factor
:= TableFactor
[FChannel2Allocation
];
278 FChannel2Offset
:= TableOffset
[FChannel2Allocation
];
282 function TSubBandLayer1Stereo
.ReadSampleData(Stream
: PBitStream
): boolean;
284 Result
:= inherited ReadSampleData(Stream
);
286 if (FChannel2Allocation
<> 0) then
287 FChannel2Sample
:= Stream
.GetBitsFloat(FChannel2SampleLength
);
290 procedure TSubBandLayer1Stereo
.ReadScaleFactor(Stream
: PBitStream
;
293 if (FAllocation
<> 0) then
294 FScaleFactor
:= ScaleFactors
[Stream
.GetBits(6)];
296 if (FChannel2Allocation
<> 0) then
297 FChannel2ScaleFactor
:= ScaleFactors
[Stream
.GetBits(6)];