Restore commit that fixed the problem that config.h changes don't trigger a genlang...
[kugel-rb.git] / apps / codecs / libpcm / ieee_float.c
blob639390bcd5cacdd97081fbe88ad11bb60effddae
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Yoshihisa Uchida
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "codeclib.h"
22 #include "pcm_common.h"
23 #include "support_formats.h"
26 * IEEE float
29 static struct pcm_format *fmt;
31 static bool set_format(struct pcm_format *format)
33 fmt = format;
35 if (fmt->channels == 0)
37 DEBUGF("CODEC_ERROR: channels is 0\n");
38 return false;
41 if (fmt->bitspersample != 32 && fmt->bitspersample != 64)
43 DEBUGF("CODEC_ERROR: ieee float must be 32 or 64 bitspersample: %d\n",
44 fmt->bitspersample);
45 return false;
48 fmt->bytespersample = fmt->bitspersample >> 3;
50 if (fmt->blockalign == 0)
51 fmt->blockalign = fmt->bytespersample * fmt->channels;
53 fmt->samplesperblock = fmt->blockalign / (fmt->bytespersample * fmt->channels);
55 /* chunksize = about 1/50[sec] data */
56 fmt->chunksize = (ci->id3->frequency / (50 * fmt->samplesperblock))
57 * fmt->blockalign;
59 return true;
62 static struct pcm_pos *get_seek_pos(uint32_t seek_val, int seek_mode,
63 uint8_t *(*read_buffer)(size_t *realsize))
65 static struct pcm_pos newpos;
66 uint32_t newblock = (seek_mode == PCM_SEEK_TIME) ?
67 ((uint64_t)seek_val * ci->id3->frequency / 1000LL)
68 / fmt->samplesperblock :
69 seek_val / fmt->blockalign;
71 (void)read_buffer;
72 newpos.pos = newblock * fmt->blockalign;
73 newpos.samples = newblock * fmt->samplesperblock;
74 return &newpos;
77 static int decode(const uint8_t *inbuf, size_t inbufsize,
78 int32_t *outbuf, int *outbufsize)
80 uint32_t i;
81 int32_t pcm;
82 int16_t exp;
83 int sgn;
85 if (fmt->bitspersample == 32)
87 for (i = 0; i < inbufsize; i += 4)
89 if (fmt->is_little_endian)
91 pcm = (inbuf[0]<<5)|(inbuf[1]<<13)|((inbuf[2]|0x80)<<21);
92 exp = ((inbuf[2]>>7)|((inbuf[3]&0x7f)<<1)) - 127;
93 sgn = inbuf[3] & 0x80;
95 else
97 pcm = (inbuf[3]<<5)|(inbuf[2]<<13)|((inbuf[1]|0x80)<<21);
98 exp = ((inbuf[1]>>7)|((inbuf[0]&0x7f)<<1)) - 127;
99 sgn = inbuf[0] & 0x80;
101 if (exp > -29 && exp < 0)
103 pcm >>= -exp;
104 if (sgn)
105 pcm = -pcm;
107 else if (exp < -28)
108 pcm = 0;
109 else
110 pcm = (sgn)?-(1<<28):(1<<28)-1;
112 outbuf[i/4] = pcm;
113 inbuf += 4;
115 *outbufsize = inbufsize >> 2;
117 else
119 for (i = 0; i < inbufsize; i += 8)
121 if (fmt->is_little_endian)
123 pcm = inbuf[3]|(inbuf[4]<<8)|(inbuf[5]<<16)|(((inbuf[6]&0x0f)|0x10)<<24);
124 exp = (((inbuf[6]&0xf0)>>4)|((inbuf[7]&0x7f)<<4)) - 1023;
125 sgn = inbuf[7] & 0x80;
127 else
129 pcm = inbuf[4]|(inbuf[3]<<8)|(inbuf[2]<<16)|(((inbuf[1]&0x0f)|0x10)<<24);
130 exp = (((inbuf[1]&0xf0)>>4)|((inbuf[0]&0x7f)<<4)) - 1023;
131 sgn = inbuf[0] & 0x80;
133 if (exp > -29 && exp < 0)
135 pcm >>= -exp;
136 if (sgn)
137 pcm = -pcm;
139 else if (exp < -28)
140 pcm = 0;
141 else
142 pcm = (sgn)?-(1<<28):(1<<28)-1;
144 outbuf[i/8] = pcm;
145 inbuf += 8;
147 *outbufsize = inbufsize >> 3;
150 if (fmt->channels == 2)
151 *outbufsize >>= 1;
153 return CODEC_OK;
156 static const struct pcm_codec codec = {
157 set_format,
158 get_seek_pos,
159 decode,
162 const struct pcm_codec *get_ieee_float_codec(void)
164 return &codec;