1159b3361Sopenharmony_ci/*
2159b3361Sopenharmony_ci * decode_i396.c: Mpeg Layer-1,2,3 audio decoder
3159b3361Sopenharmony_ci *
4159b3361Sopenharmony_ci * Copyright (C) 1999-2010 The L.A.M.E. project
5159b3361Sopenharmony_ci *
6159b3361Sopenharmony_ci * Initially written by Michael Hipp, see also AUTHORS and README.
7159b3361Sopenharmony_ci *
8159b3361Sopenharmony_ci * This library is free software; you can redistribute it and/or
9159b3361Sopenharmony_ci * modify it under the terms of the GNU Library General Public
10159b3361Sopenharmony_ci * License as published by the Free Software Foundation; either
11159b3361Sopenharmony_ci * version 2 of the License, or (at your option) any later version.
12159b3361Sopenharmony_ci *
13159b3361Sopenharmony_ci * This library is distributed in the hope that it will be useful,
14159b3361Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
15159b3361Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16159b3361Sopenharmony_ci * Library General Public License for more details.
17159b3361Sopenharmony_ci *
18159b3361Sopenharmony_ci * You should have received a copy of the GNU Library General Public
19159b3361Sopenharmony_ci * License along with this library; if not, write to the
20159b3361Sopenharmony_ci * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21159b3361Sopenharmony_ci * Boston, MA 02111-1307, USA.
22159b3361Sopenharmony_ci *
23159b3361Sopenharmony_ci *
24159b3361Sopenharmony_ci * Slighlty optimized for machines without autoincrement/decrement.
25159b3361Sopenharmony_ci * The performance is highly compiler dependend. Maybe
26159b3361Sopenharmony_ci * the decode.c version for 'normal' processor may be faster
27159b3361Sopenharmony_ci * even for Intel processors.
28159b3361Sopenharmony_ci */
29159b3361Sopenharmony_ci
30159b3361Sopenharmony_ci/* $Id$ */
31159b3361Sopenharmony_ci
32159b3361Sopenharmony_ci#ifdef HAVE_CONFIG_H
33159b3361Sopenharmony_ci#include <config.h>
34159b3361Sopenharmony_ci#endif
35159b3361Sopenharmony_ci
36159b3361Sopenharmony_ci#ifdef STDC_HEADERS
37159b3361Sopenharmony_ci# include <stdlib.h>
38159b3361Sopenharmony_ci# include <string.h>
39159b3361Sopenharmony_ci#else
40159b3361Sopenharmony_ci# ifndef HAVE_STRCHR
41159b3361Sopenharmony_ci#  define strchr index
42159b3361Sopenharmony_ci#  define strrchr rindex
43159b3361Sopenharmony_ci# endif
44159b3361Sopenharmony_cichar   *strchr(), *strrchr();
45159b3361Sopenharmony_ci# ifndef HAVE_MEMCPY
46159b3361Sopenharmony_ci#  define memcpy(d, s, n) bcopy ((s), (d), (n))
47159b3361Sopenharmony_ci#  define memmove(d, s, n) bcopy ((s), (d), (n))
48159b3361Sopenharmony_ci# endif
49159b3361Sopenharmony_ci#endif
50159b3361Sopenharmony_ci
51159b3361Sopenharmony_ci#if defined(__riscos__) && defined(FPA10)
52159b3361Sopenharmony_ci#include "ymath.h"
53159b3361Sopenharmony_ci#else
54159b3361Sopenharmony_ci#include <math.h>
55159b3361Sopenharmony_ci#endif
56159b3361Sopenharmony_ci
57159b3361Sopenharmony_ci#include "decode_i386.h"
58159b3361Sopenharmony_ci#include "dct64_i386.h"
59159b3361Sopenharmony_ci#include "tabinit.h"
60159b3361Sopenharmony_ci
61159b3361Sopenharmony_ci#ifdef WITH_DMALLOC
62159b3361Sopenharmony_ci#include <dmalloc.h>
63159b3361Sopenharmony_ci#endif
64159b3361Sopenharmony_ci
65159b3361Sopenharmony_ci
66159b3361Sopenharmony_ci /* old WRITE_SAMPLE_CLIPPED */
67159b3361Sopenharmony_ci#define WRITE_SAMPLE_CLIPPED(TYPE,samples,sum,clip) \
68159b3361Sopenharmony_ci  if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
69159b3361Sopenharmony_ci  else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
70159b3361Sopenharmony_ci  else { *(samples) = (TYPE)((sum)>0 ? (sum)+0.5 : (sum)-0.5) ; }
71159b3361Sopenharmony_ci
72159b3361Sopenharmony_ci#define WRITE_SAMPLE_UNCLIPPED(TYPE,samples,sum,clip) \
73159b3361Sopenharmony_ci  *samples = (TYPE)sum;
74159b3361Sopenharmony_ci
75159b3361Sopenharmony_ci  /* *INDENT-OFF* */
76159b3361Sopenharmony_ci
77159b3361Sopenharmony_ci /* versions: clipped (when TYPE == short) and unclipped (when TYPE == real) of synth_1to1_mono* functions */
78159b3361Sopenharmony_ci#define SYNTH_1TO1_MONO_CLIPCHOICE(TYPE,SYNTH_1TO1)                    \
79159b3361Sopenharmony_ci  TYPE samples_tmp[64];                                                \
80159b3361Sopenharmony_ci  TYPE *tmp1 = samples_tmp;                                            \
81159b3361Sopenharmony_ci  int i,ret;                                                           \
82159b3361Sopenharmony_ci  int pnt1 = 0;                                                        \
83159b3361Sopenharmony_ci                                                                       \
84159b3361Sopenharmony_ci  ret = SYNTH_1TO1 (mp,bandPtr,0,(unsigned char *) samples_tmp,&pnt1); \
85159b3361Sopenharmony_ci  out += *pnt;                                                         \
86159b3361Sopenharmony_ci                                                                       \
87159b3361Sopenharmony_ci  for(i=0;i<32;i++) {                                                  \
88159b3361Sopenharmony_ci    *( (TYPE *) out) = *tmp1;                                          \
89159b3361Sopenharmony_ci    out += sizeof(TYPE);                                               \
90159b3361Sopenharmony_ci    tmp1 += 2;                                                         \
91159b3361Sopenharmony_ci  }                                                                    \
92159b3361Sopenharmony_ci  *pnt += 32*sizeof(TYPE);                                             \
93159b3361Sopenharmony_ci                                                                       \
94159b3361Sopenharmony_ci  return ret;
95159b3361Sopenharmony_ci
96159b3361Sopenharmony_ci  /* *INDENT-ON* */
97159b3361Sopenharmony_ci
98159b3361Sopenharmony_ci
99159b3361Sopenharmony_ciint
100159b3361Sopenharmony_cisynth_1to1_mono(PMPSTR mp, real * bandPtr, unsigned char *out, int *pnt)
101159b3361Sopenharmony_ci{
102159b3361Sopenharmony_ci    SYNTH_1TO1_MONO_CLIPCHOICE(short, synth_1to1)
103159b3361Sopenharmony_ci} int
104159b3361Sopenharmony_cisynth_1to1_mono_unclipped(PMPSTR mp, real * bandPtr, unsigned char *out, int *pnt)
105159b3361Sopenharmony_ci{
106159b3361Sopenharmony_ci    SYNTH_1TO1_MONO_CLIPCHOICE(real, synth_1to1_unclipped)
107159b3361Sopenharmony_ci}
108159b3361Sopenharmony_ci
109159b3361Sopenharmony_ci    /* *INDENT-OFF* */
110159b3361Sopenharmony_ci/* versions: clipped (when TYPE == short) and unclipped (when TYPE == real) of synth_1to1* functions */
111159b3361Sopenharmony_ci#define SYNTH_1TO1_CLIPCHOICE(TYPE,WRITE_SAMPLE)         \
112159b3361Sopenharmony_ci  static const int step = 2;                             \
113159b3361Sopenharmony_ci  int bo;                                                \
114159b3361Sopenharmony_ci  TYPE *samples = (TYPE *) (out + *pnt);                 \
115159b3361Sopenharmony_ci                                                         \
116159b3361Sopenharmony_ci  real *b0,(*buf)[0x110];                                \
117159b3361Sopenharmony_ci  int clip = 0;                                          \
118159b3361Sopenharmony_ci  int bo1;                                               \
119159b3361Sopenharmony_ci                                                         \
120159b3361Sopenharmony_ci  bo = mp->synth_bo;                                     \
121159b3361Sopenharmony_ci                                                         \
122159b3361Sopenharmony_ci  if(!channel) {                                         \
123159b3361Sopenharmony_ci    bo--;                                                \
124159b3361Sopenharmony_ci    bo &= 0xf;                                           \
125159b3361Sopenharmony_ci    buf = mp->synth_buffs[0];                            \
126159b3361Sopenharmony_ci  }                                                      \
127159b3361Sopenharmony_ci  else {                                                 \
128159b3361Sopenharmony_ci    samples++;                                           \
129159b3361Sopenharmony_ci    buf = mp->synth_buffs[1];                            \
130159b3361Sopenharmony_ci  }                                                      \
131159b3361Sopenharmony_ci                                                         \
132159b3361Sopenharmony_ci  if(bo & 0x1) {                                         \
133159b3361Sopenharmony_ci    b0 = buf[0];                                         \
134159b3361Sopenharmony_ci    bo1 = bo;                                            \
135159b3361Sopenharmony_ci    dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);        \
136159b3361Sopenharmony_ci  }                                                      \
137159b3361Sopenharmony_ci  else {                                                 \
138159b3361Sopenharmony_ci    b0 = buf[1];                                         \
139159b3361Sopenharmony_ci    bo1 = bo+1;                                          \
140159b3361Sopenharmony_ci    dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);                \
141159b3361Sopenharmony_ci  }                                                      \
142159b3361Sopenharmony_ci                                                         \
143159b3361Sopenharmony_ci  mp->synth_bo = bo;                                     \
144159b3361Sopenharmony_ci                                                         \
145159b3361Sopenharmony_ci  {                                                      \
146159b3361Sopenharmony_ci    int j;                                               \
147159b3361Sopenharmony_ci    real *window = decwin + 16 - bo1;                    \
148159b3361Sopenharmony_ci                                                         \
149159b3361Sopenharmony_ci    for (j=16;j;j--,b0+=0x10,window+=0x20,samples+=step) \
150159b3361Sopenharmony_ci    {                                                    \
151159b3361Sopenharmony_ci      real sum;                                          \
152159b3361Sopenharmony_ci      sum  = window[0x0] * b0[0x0];                      \
153159b3361Sopenharmony_ci      sum -= window[0x1] * b0[0x1];                      \
154159b3361Sopenharmony_ci      sum += window[0x2] * b0[0x2];                      \
155159b3361Sopenharmony_ci      sum -= window[0x3] * b0[0x3];                      \
156159b3361Sopenharmony_ci      sum += window[0x4] * b0[0x4];                      \
157159b3361Sopenharmony_ci      sum -= window[0x5] * b0[0x5];                      \
158159b3361Sopenharmony_ci      sum += window[0x6] * b0[0x6];                      \
159159b3361Sopenharmony_ci      sum -= window[0x7] * b0[0x7];                      \
160159b3361Sopenharmony_ci      sum += window[0x8] * b0[0x8];                      \
161159b3361Sopenharmony_ci      sum -= window[0x9] * b0[0x9];                      \
162159b3361Sopenharmony_ci      sum += window[0xA] * b0[0xA];                      \
163159b3361Sopenharmony_ci      sum -= window[0xB] * b0[0xB];                      \
164159b3361Sopenharmony_ci      sum += window[0xC] * b0[0xC];                      \
165159b3361Sopenharmony_ci      sum -= window[0xD] * b0[0xD];                      \
166159b3361Sopenharmony_ci      sum += window[0xE] * b0[0xE];                      \
167159b3361Sopenharmony_ci      sum -= window[0xF] * b0[0xF];                      \
168159b3361Sopenharmony_ci                                                         \
169159b3361Sopenharmony_ci      WRITE_SAMPLE (TYPE,samples,sum,clip);              \
170159b3361Sopenharmony_ci    }                                                    \
171159b3361Sopenharmony_ci                                                         \
172159b3361Sopenharmony_ci    {                                                    \
173159b3361Sopenharmony_ci      real sum;                                          \
174159b3361Sopenharmony_ci      sum  = window[0x0] * b0[0x0];                      \
175159b3361Sopenharmony_ci      sum += window[0x2] * b0[0x2];                      \
176159b3361Sopenharmony_ci      sum += window[0x4] * b0[0x4];                      \
177159b3361Sopenharmony_ci      sum += window[0x6] * b0[0x6];                      \
178159b3361Sopenharmony_ci      sum += window[0x8] * b0[0x8];                      \
179159b3361Sopenharmony_ci      sum += window[0xA] * b0[0xA];                      \
180159b3361Sopenharmony_ci      sum += window[0xC] * b0[0xC];                      \
181159b3361Sopenharmony_ci      sum += window[0xE] * b0[0xE];                      \
182159b3361Sopenharmony_ci      WRITE_SAMPLE (TYPE,samples,sum,clip);              \
183159b3361Sopenharmony_ci      b0-=0x10,window-=0x20,samples+=step;               \
184159b3361Sopenharmony_ci    }                                                    \
185159b3361Sopenharmony_ci    window += bo1<<1;                                    \
186159b3361Sopenharmony_ci                                                         \
187159b3361Sopenharmony_ci    for (j=15;j;j--,b0-=0x10,window-=0x20,samples+=step) \
188159b3361Sopenharmony_ci    {                                                    \
189159b3361Sopenharmony_ci      real sum;                                          \
190159b3361Sopenharmony_ci      sum = -window[-0x1] * b0[0x0];                     \
191159b3361Sopenharmony_ci      sum -= window[-0x2] * b0[0x1];                     \
192159b3361Sopenharmony_ci      sum -= window[-0x3] * b0[0x2];                     \
193159b3361Sopenharmony_ci      sum -= window[-0x4] * b0[0x3];                     \
194159b3361Sopenharmony_ci      sum -= window[-0x5] * b0[0x4];                     \
195159b3361Sopenharmony_ci      sum -= window[-0x6] * b0[0x5];                     \
196159b3361Sopenharmony_ci      sum -= window[-0x7] * b0[0x6];                     \
197159b3361Sopenharmony_ci      sum -= window[-0x8] * b0[0x7];                     \
198159b3361Sopenharmony_ci      sum -= window[-0x9] * b0[0x8];                     \
199159b3361Sopenharmony_ci      sum -= window[-0xA] * b0[0x9];                     \
200159b3361Sopenharmony_ci      sum -= window[-0xB] * b0[0xA];                     \
201159b3361Sopenharmony_ci      sum -= window[-0xC] * b0[0xB];                     \
202159b3361Sopenharmony_ci      sum -= window[-0xD] * b0[0xC];                     \
203159b3361Sopenharmony_ci      sum -= window[-0xE] * b0[0xD];                     \
204159b3361Sopenharmony_ci      sum -= window[-0xF] * b0[0xE];                     \
205159b3361Sopenharmony_ci      sum -= window[-0x0] * b0[0xF];                     \
206159b3361Sopenharmony_ci                                                         \
207159b3361Sopenharmony_ci      WRITE_SAMPLE (TYPE,samples,sum,clip);              \
208159b3361Sopenharmony_ci    }                                                    \
209159b3361Sopenharmony_ci  }                                                      \
210159b3361Sopenharmony_ci  *pnt += 64*sizeof(TYPE);                               \
211159b3361Sopenharmony_ci                                                         \
212159b3361Sopenharmony_ci  return clip;
213159b3361Sopenharmony_ci    /* *INDENT-ON* */
214159b3361Sopenharmony_ci
215159b3361Sopenharmony_ci
216159b3361Sopenharmony_ciint
217159b3361Sopenharmony_cisynth_1to1(PMPSTR mp, real * bandPtr, int channel, unsigned char *out, int *pnt)
218159b3361Sopenharmony_ci{
219159b3361Sopenharmony_ci    SYNTH_1TO1_CLIPCHOICE(short, WRITE_SAMPLE_CLIPPED)
220159b3361Sopenharmony_ci} int
221159b3361Sopenharmony_cisynth_1to1_unclipped(PMPSTR mp, real * bandPtr, int channel, unsigned char *out, int *pnt)
222159b3361Sopenharmony_ci{
223159b3361Sopenharmony_ci    SYNTH_1TO1_CLIPCHOICE(real, WRITE_SAMPLE_UNCLIPPED)
224159b3361Sopenharmony_ci}
225