commit FS#10424 and FS#10425
[kugel-rb.git] / apps / codecs / libpcm / ieee_float.c
blob0530993f31064e24dfb0a1531d75563ef0fa5521
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->bitspersample != 32 && fmt->bitspersample != 64)
37 DEBUGF("CODEC_ERROR: ieee float must be 32 or 64 bitspersample: %d\n",
38 fmt->bitspersample);
39 return false;
42 fmt->bytespersample = fmt->bitspersample >> 3;
43 fmt->samplesperblock = fmt->blockalign / (fmt->bytespersample * fmt->channels);
45 /* chunksize = about 1/50[sec] data */
46 fmt->chunksize = (ci->id3->frequency / (50 * fmt->samplesperblock))
47 * fmt->blockalign;
49 return true;
52 static struct pcm_pos *get_seek_pos(long seek_time,
53 uint8_t *(*read_buffer)(size_t *realsize))
55 static struct pcm_pos newpos;
56 uint32_t newblock = ((uint64_t)seek_time * ci->id3->frequency)
57 / (1000LL * fmt->samplesperblock);
59 (void)read_buffer;
60 newpos.pos = newblock * fmt->blockalign;
61 newpos.samples = newblock * fmt->samplesperblock;
62 return &newpos;
65 static int decode(const uint8_t *inbuf, size_t inbufsize,
66 int32_t *outbuf, int *outbufsize)
68 uint32_t i;
69 int32_t pcm;
70 int16_t exp;
71 int sgn;
73 if (fmt->bitspersample == 32)
75 for (i = 0; i < inbufsize; i += 4)
77 if (fmt->is_little_endian)
79 pcm = (inbuf[0]<<5)|(inbuf[1]<<13)|((inbuf[2]|0x80)<<21);
80 exp = ((inbuf[2]>>7)|((inbuf[3]&0x7f)<<1)) - 127;
81 sgn = inbuf[3] & 0x80;
83 else
85 pcm = (inbuf[3]<<5)|(inbuf[2]<<13)|((inbuf[1]|0x80)<<21);
86 exp = ((inbuf[1]>>7)|((inbuf[0]&0x7f)<<1)) - 127;
87 sgn = inbuf[0] & 0x80;
89 if (exp > -29 && exp < 0)
91 pcm >>= -exp;
92 if (sgn)
93 pcm = -pcm;
95 else if (exp < -28)
96 pcm = 0;
97 else
98 pcm = (sgn)?-(1<<28):(1<<28)-1;
100 outbuf[i/4] = pcm;
101 inbuf += 4;
103 *outbufsize = inbufsize >> 2;
105 else
107 for (i = 0; i < inbufsize; i += 8)
109 if (fmt->is_little_endian)
111 pcm = inbuf[3]|(inbuf[4]<<8)|(inbuf[5]<<16)|(((inbuf[6]&0x0f)|0x10)<<24);
112 exp = (((inbuf[6]&0xf0)>>4)|((inbuf[7]&0x7f)<<4)) - 1023;
113 sgn = inbuf[7] & 0x80;
115 else
117 pcm = inbuf[4]|(inbuf[3]<<8)|(inbuf[2]<<16)|(((inbuf[1]&0x0f)|0x10)<<24);
118 exp = (((inbuf[1]&0xf0)>>4)|((inbuf[0]&0x7f)<<4)) - 1023;
119 sgn = inbuf[0] & 0x80;
121 if (exp > -29 && exp < 0)
123 pcm >>= -exp;
124 if (sgn)
125 pcm = -pcm;
127 else if (exp < -28)
128 pcm = 0;
129 else
130 pcm = (sgn)?-(1<<28):(1<<28)-1;
132 outbuf[i/8] = pcm;
133 inbuf += 8;
135 *outbufsize = inbufsize >> 3;
138 if (fmt->channels == 2)
139 *outbufsize >>= 1;
141 return CODEC_OK;
144 static const struct pcm_codec codec = {
145 set_format,
146 get_seek_pos,
147 decode,
150 const struct pcm_codec *get_ieee_float_codec(void)
152 return &codec;