1 /*
2 * layer1.c: Mpeg Layer-1 audio decoder
3 *
4 * Copyright (C) 1999-2010 The L.A.M.E. project
5 *
6 * Initially written by Michael Hipp, see also AUTHORS and README.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
24 /* $Id$ */
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #include <assert.h>
31 #include "common.h"
32 #include "decode_i386.h"
33
34 #ifdef WITH_DMALLOC
35 #include <dmalloc.h>
36 #endif
37
38 #include "layer1.h"
39
40 static int gd_are_hip_tables_layer1_initialized = 0;
41
42 void
hip_init_tables_layer1(void)43 hip_init_tables_layer1(void)
44 {
45 if (gd_are_hip_tables_layer1_initialized) {
46 return;
47 }
48 gd_are_hip_tables_layer1_initialized = 1;
49 }
50
51 typedef struct sideinfo_layer_I_struct
52 {
53 unsigned char allocation[SBLIMIT][2];
54 unsigned char scalefactor[SBLIMIT][2];
55 } sideinfo_layer_I;
56
57 static int
I_step_one(PMPSTR mp,sideinfo_layer_I * si)58 I_step_one(PMPSTR mp, sideinfo_layer_I* si)
59 {
60 struct frame *fr = &(mp->fr);
61 int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
62 int i;
63 int illegal_value_detected = 0;
64 unsigned char const ba15 = 15; /* bit pattern not allowed, looks like sync(?) */
65 memset(si, 0, sizeof(*si));
66 assert(fr->stereo == 1 || fr->stereo == 2);
67
68 if (fr->stereo == 2) {
69 for (i = 0; i < jsbound; i++) {
70 unsigned char b0 = get_leq_8_bits(mp, 4); /* values 0-15 */
71 unsigned char b1 = get_leq_8_bits(mp, 4); /* values 0-15 */
72 si->allocation[i][0] = b0;
73 si->allocation[i][1] = b1;
74 if (b0 == ba15 || b1 == ba15)
75 illegal_value_detected = 1;
76 }
77 for (i = jsbound; i < SBLIMIT; i++) {
78 unsigned char b = get_leq_8_bits(mp, 4); /* values 0-15 */
79 si->allocation[i][0] = b;
80 si->allocation[i][1] = b;
81 if (b == ba15)
82 illegal_value_detected = 1;
83 }
84 for (i = 0; i < SBLIMIT; i++) {
85 unsigned char n0 = si->allocation[i][0];
86 unsigned char n1 = si->allocation[i][1];
87 unsigned char b0 = n0 ? get_leq_8_bits(mp, 6) : 0; /* values 0-63 */
88 unsigned char b1 = n1 ? get_leq_8_bits(mp, 6) : 0; /* values 0-63 */
89 si->scalefactor[i][0] = b0;
90 si->scalefactor[i][1] = b1;
91 }
92 }
93 else {
94 for (i = 0; i < SBLIMIT; i++) {
95 unsigned char b0 = get_leq_8_bits(mp, 4); /* values 0-15 */
96 si->allocation[i][0] = b0;
97 if (b0 == ba15)
98 illegal_value_detected = 1;
99 }
100 for (i = 0; i < SBLIMIT; i++) {
101 unsigned char n0 = si->allocation[i][0];
102 unsigned char b0 = n0 ? get_leq_8_bits(mp, 6) : 0; /* values 0-63 */
103 si->scalefactor[i][0] = b0;
104 }
105 }
106 return illegal_value_detected;
107 }
108
109 static void
I_step_two(PMPSTR mp,sideinfo_layer_I * si,real fraction[2][SBLIMIT])110 I_step_two(PMPSTR mp, sideinfo_layer_I *si, real fraction[2][SBLIMIT])
111 {
112 double r0, r1;
113 struct frame *fr = &(mp->fr);
114 int ds_limit = fr->down_sample_sblimit;
115 int i;
116
117 assert(fr->stereo == 1 || fr->stereo == 2);
118 if (fr->stereo == 2) {
119 int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
120 for (i = 0; i < jsbound; i++) {
121 unsigned char i0 = si->scalefactor[i][0];
122 unsigned char i1 = si->scalefactor[i][1];
123 unsigned char n0 = si->allocation[i][0];
124 unsigned char n1 = si->allocation[i][1];
125 assert( i0 < 64 );
126 assert( i1 < 64 );
127 assert( n0 < 16 );
128 assert( n1 < 16 );
129 if (n0 > 0) {
130 unsigned short v = get_leq_16_bits(mp, n0 + 1); /* 0-65535 */
131 r0 = (((-1) << n0) + v + 1) * muls[n0 + 1][i0];
132 }
133 else {
134 r0 = 0;
135 }
136 if (n1 > 0) {
137 unsigned short v = get_leq_16_bits(mp, n1 + 1); /* 0-65535 */
138 r1 = (((-1) << n1) + v + 1) * muls[n1 + 1][i1];
139 }
140 else {
141 r1 = 0;
142 }
143 fraction[0][i] = (real)r0;
144 fraction[1][i] = (real)r1;
145 }
146 for (i = jsbound; i < SBLIMIT; i++) {
147 unsigned char i0 = si->scalefactor[i][0];
148 unsigned char i1 = si->scalefactor[i][1];
149 unsigned char n = si->allocation[i][0];
150 assert( i0 < 64 );
151 assert( i1 < 64 );
152 assert( n < 16 );
153 if (n > 0) {
154 unsigned short v = get_leq_16_bits(mp, n + 1); /* 0-65535 */
155 unsigned int w = (((-1) << n) + v + 1);
156 r0 = w * muls[n + 1][i0];
157 r1 = w * muls[n + 1][i1];
158 }
159 else {
160 r0 = r1 = 0;
161 }
162 fraction[0][i] = (real)r0;
163 fraction[1][i] = (real)r1;
164 }
165 for (i = ds_limit; i < SBLIMIT; i++) {
166 fraction[0][i] = 0.0;
167 fraction[1][i] = 0.0;
168 }
169 }
170 else {
171 for (i = 0; i < SBLIMIT; i++) {
172 unsigned char n = si->allocation[i][0];
173 unsigned char j = si->scalefactor[i][0];
174 assert( j < 64 );
175 assert( n < 16 );
176 if (n > 0) {
177 unsigned short v = get_leq_16_bits(mp, n + 1);
178 r0 = (((-1) << n) + v + 1) * muls[n + 1][j];
179 }
180 else {
181 r0 = 0;
182 }
183 fraction[0][i] = (real)r0;
184 }
185 for (i = ds_limit; i < SBLIMIT; i++) {
186 fraction[0][i] = 0.0;
187 }
188 }
189 }
190
191 int
decode_layer1_sideinfo(PMPSTR mp)192 decode_layer1_sideinfo(PMPSTR mp)
193 {
194 (void) mp;
195 /* FIXME: extract side information and check values */
196 return 0;
197 }
198
199 int
decode_layer1_frame(PMPSTR mp,unsigned char * pcm_sample,int * pcm_point)200 decode_layer1_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point)
201 {
202 real fraction[2][SBLIMIT]; /* FIXME: change real -> double ? */
203 sideinfo_layer_I si;
204 struct frame *fr = &(mp->fr);
205 int single = fr->single;
206 int i, clip = 0;
207
208 if (I_step_one(mp, &si)) {
209 lame_report_fnc(mp->report_err, "hip: Aborting layer 1 decode, illegal bit allocation value\n");
210 return -1;
211 }
212 if (fr->stereo == 1 || single == 3)
213 single = 0;
214
215 if (single >= 0) {
216 /* decoding one of possibly two channels */
217 for (i = 0; i < SCALE_BLOCK; i++) {
218 I_step_two(mp, &si, fraction);
219 clip += synth_1to1_mono(mp, (real *) fraction[single], pcm_sample, pcm_point);
220 }
221 }
222 else {
223 for (i = 0; i < SCALE_BLOCK; i++) {
224 int p1 = *pcm_point;
225 I_step_two(mp, &si, fraction);
226 clip += synth_1to1(mp, (real *) fraction[0], 0, pcm_sample, &p1);
227 clip += synth_1to1(mp, (real *) fraction[1], 1, pcm_sample, pcm_point);
228 }
229 }
230
231 return clip;
232 }
233