reduce firmware and sun audio codec.
[kugel-rb.git] / apps / codecs / libpcm / ieee_float.c
blob7e3498edcb2fb0425c3cf54546bc4b10a024b0eb
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(long seek_time,
63 uint8_t *(*read_buffer)(size_t *realsize))
65 static struct pcm_pos newpos;
66 uint32_t newblock = ((uint64_t)seek_time * ci->id3->frequency)
67 / (1000LL * fmt->samplesperblock);
69 (void)read_buffer;
70 newpos.pos = newblock * fmt->blockalign;
71 newpos.samples = newblock * fmt->samplesperblock;
72 return &newpos;
75 static int decode(const uint8_t *inbuf, size_t inbufsize,
76 int32_t *outbuf, int *outbufsize)
78 uint32_t i;
79 int32_t pcm;
80 int16_t exp;
81 int sgn;
83 if (fmt->bitspersample == 32)
85 for (i = 0; i < inbufsize; i += 4)
87 if (fmt->is_little_endian)
89 pcm = (inbuf[0]<<5)|(inbuf[1]<<13)|((inbuf[2]|0x80)<<21);
90 exp = ((inbuf[2]>>7)|((inbuf[3]&0x7f)<<1)) - 127;
91 sgn = inbuf[3] & 0x80;
93 else
95 pcm = (inbuf[3]<<5)|(inbuf[2]<<13)|((inbuf[1]|0x80)<<21);
96 exp = ((inbuf[1]>>7)|((inbuf[0]&0x7f)<<1)) - 127;
97 sgn = inbuf[0] & 0x80;
99 if (exp > -29 && exp < 0)
101 pcm >>= -exp;
102 if (sgn)
103 pcm = -pcm;
105 else if (exp < -28)
106 pcm = 0;
107 else
108 pcm = (sgn)?-(1<<28):(1<<28)-1;
110 outbuf[i/4] = pcm;
111 inbuf += 4;
113 *outbufsize = inbufsize >> 2;
115 else
117 for (i = 0; i < inbufsize; i += 8)
119 if (fmt->is_little_endian)
121 pcm = inbuf[3]|(inbuf[4]<<8)|(inbuf[5]<<16)|(((inbuf[6]&0x0f)|0x10)<<24);
122 exp = (((inbuf[6]&0xf0)>>4)|((inbuf[7]&0x7f)<<4)) - 1023;
123 sgn = inbuf[7] & 0x80;
125 else
127 pcm = inbuf[4]|(inbuf[3]<<8)|(inbuf[2]<<16)|(((inbuf[1]&0x0f)|0x10)<<24);
128 exp = (((inbuf[1]&0xf0)>>4)|((inbuf[0]&0x7f)<<4)) - 1023;
129 sgn = inbuf[0] & 0x80;
131 if (exp > -29 && exp < 0)
133 pcm >>= -exp;
134 if (sgn)
135 pcm = -pcm;
137 else if (exp < -28)
138 pcm = 0;
139 else
140 pcm = (sgn)?-(1<<28):(1<<28)-1;
142 outbuf[i/8] = pcm;
143 inbuf += 8;
145 *outbufsize = inbufsize >> 3;
148 if (fmt->channels == 2)
149 *outbufsize >>= 1;
151 return CODEC_OK;
154 static const struct pcm_codec codec = {
155 set_format,
156 get_seek_pos,
157 decode,
160 const struct pcm_codec *get_ieee_float_codec(void)
162 return &codec;