2 * layer1.c: Mpeg Layer-1 audio decoder
4 * Copyright (C) 1999-2010 The L.A.M.E. project
6 * Initially written by Michael Hipp, see also AUTHORS and README.
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.
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.
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.
24 /* $Id: layer1.c,v 1.30 2010/03/22 14:30:19 robert Exp $ */
32 #include "decode_i386.h"
40 static int gd_are_hip_tables_layer1_initialized = 0;
43 hip_init_tables_layer1(void)
45 if (gd_are_hip_tables_layer1_initialized) {
48 gd_are_hip_tables_layer1_initialized = 1;
51 typedef struct sideinfo_layer_I_struct
53 unsigned char allocation[SBLIMIT][2];
54 unsigned char scalefactor[SBLIMIT][2];
58 I_step_one(PMPSTR mp, sideinfo_layer_I* si)
60 struct frame *fr = &(mp->fr);
61 int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
63 memset(si, 0, sizeof(*si));
64 assert(fr->stereo == 1 || fr->stereo == 2);
66 if (fr->stereo == 2) {
67 for (i = 0; i < jsbound; i++) {
68 unsigned char b0 = get_leq_8_bits(mp, 4); /* values 0-15 */
69 unsigned char b1 = get_leq_8_bits(mp, 4); /* values 0-15 */
70 si->allocation[i][0] = b0;
71 si->allocation[i][1] = b1;
73 for (i = jsbound; i < SBLIMIT; i++) {
74 unsigned char b = get_leq_8_bits(mp, 4); /* values 0-15 */
75 si->allocation[i][0] = b;
76 si->allocation[i][1] = b;
78 for (i = 0; i < SBLIMIT; i++) {
79 unsigned char n0 = si->allocation[i][0];
80 unsigned char n1 = si->allocation[i][1];
81 unsigned char b0 = n0 ? get_leq_8_bits(mp, 6) : 0; /* values 0-63 */
82 unsigned char b1 = n1 ? get_leq_8_bits(mp, 6) : 0; /* values 0-63 */
83 si->scalefactor[i][0] = b0;
84 si->scalefactor[i][1] = b1;
88 for (i = 0; i < SBLIMIT; i++) {
89 unsigned char b0 = get_leq_8_bits(mp, 4); /* values 0-15 */
90 si->allocation[i][0] = b0;
92 for (i = 0; i < SBLIMIT; i++) {
93 unsigned char n0 = si->allocation[i][0];
94 unsigned char b0 = n0 ? get_leq_8_bits(mp, 6) : 0; /* values 0-63 */
95 si->scalefactor[i][0] = b0;
101 I_step_two(PMPSTR mp, sideinfo_layer_I *si, real fraction[2][SBLIMIT])
104 struct frame *fr = &(mp->fr);
105 int ds_limit = fr->down_sample_sblimit;
108 assert(fr->stereo == 1 || fr->stereo == 2);
109 if (fr->stereo == 2) {
110 int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
111 for (i = 0; i < jsbound; i++) {
112 unsigned char i0 = si->scalefactor[i][0];
113 unsigned char i1 = si->scalefactor[i][1];
114 unsigned char n0 = si->allocation[i][0];
115 unsigned char n1 = si->allocation[i][1];
121 unsigned short v = get_leq_16_bits(mp, n0 + 1); /* 0-65535 */
122 r0 = (((-1) << n0) + v + 1) * muls[n0 + 1][i0];
128 unsigned short v = get_leq_16_bits(mp, n1 + 1); /* 0-65535 */
129 r1 = (((-1) << n1) + v + 1) * muls[n1 + 1][i1];
134 fraction[0][i] = (real)r0;
135 fraction[1][i] = (real)r1;
137 for (i = jsbound; i < SBLIMIT; i++) {
138 unsigned char i0 = si->scalefactor[i][0];
139 unsigned char i1 = si->scalefactor[i][1];
140 unsigned char n = si->allocation[i][0];
145 unsigned short v = get_leq_16_bits(mp, n + 1); /* 0-65535 */
146 unsigned int w = (((-1) << n) + v + 1);
147 r0 = w * muls[n + 1][i0];
148 r1 = w * muls[n + 1][i1];
153 fraction[0][i] = (real)r0;
154 fraction[1][i] = (real)r1;
156 for (i = ds_limit; i < SBLIMIT; i++) {
157 fraction[0][i] = 0.0;
158 fraction[1][i] = 0.0;
162 for (i = 0; i < SBLIMIT; i++) {
163 unsigned char n = si->allocation[i][0];
164 unsigned char j = si->scalefactor[i][0];
168 unsigned short v = get_leq_16_bits(mp, n + 1);
169 r0 = (((-1) << n) + v + 1) * muls[n + 1][j];
174 fraction[0][i] = (real)r0;
176 for (i = ds_limit; i < SBLIMIT; i++) {
177 fraction[0][i] = 0.0;
183 decode_layer1_sideinfo(PMPSTR mp)
186 /* FIXME: extract side information and check values */
191 decode_layer1_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point)
193 real fraction[2][SBLIMIT]; /* FIXME: change real -> double ? */
195 struct frame *fr = &(mp->fr);
196 int single = fr->single;
201 if (fr->stereo == 1 || single == 3)
205 /* decoding one of possibly two channels */
206 for (i = 0; i < SCALE_BLOCK; i++) {
207 I_step_two(mp, &si, fraction);
208 clip += synth_1to1_mono(mp, (real *) fraction[single], pcm_sample, pcm_point);
212 for (i = 0; i < SCALE_BLOCK; i++) {
214 I_step_two(mp, &si, fraction);
215 clip += synth_1to1(mp, (real *) fraction[0], 0, pcm_sample, &p1);
216 clip += synth_1to1(mp, (real *) fraction[1], 1, pcm_sample, pcm_point);