summaryrefslogtreecommitdiffstats
path: root/apps/codecs/libpcm/ieee_float.c
blob: a5c50e845e70dcf8c040b918e4c4fbf84f4f001b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 Yoshihisa Uchida
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "codeclib.h"
#include "pcm_common.h"

/*
 * IEEE float
 */

static struct pcm_format *fmt;

static bool set_format(struct pcm_format *format, const unsigned char *fmtpos)
{
    fmt = format;

    (void)fmtpos;

    if (fmt->bitspersample != 32 && fmt->bitspersample != 64)
    {
        DEBUGF("CODEC_ERROR: ieee float must be 32 or 64 bitspersample %d\n", fmt->bitspersample);
        return false;
    }

    fmt->bytespersample = fmt->bitspersample >> 3;
    fmt->blockalign = fmt->bytespersample;

    /* chunksize is computed so that one chunk is about 1/50s. */
    fmt->chunksize = (ci->id3->frequency * fmt->channels / 50) * fmt->blockalign;

    return true;
}

static uint32_t get_seek_pos(long seek_time)
{
    uint32_t newpos;

    newpos = ((uint64_t)(seek_time * ci->id3->frequency * fmt->channels / 1000LL))*fmt->blockalign;
    return newpos;
}

static int decode(const uint8_t *inbuf, size_t inbufsize,
                  int32_t *outbuf, int *outbufsize)
{
    uint32_t i;
    int32_t pcm;
    int16_t exp;
    int sgn;

    if (fmt->bitspersample == 32)
    {
        for (i = 0; i < inbufsize; i += 4)
        {
            if (fmt->is_little_endian)
            {
                pcm = (inbuf[0]<<5)|(inbuf[1]<<13)|((inbuf[2]|0x80)<<21);
                exp = ((inbuf[2]>>7)|((inbuf[3]&0x7f)<<1)) - 127;
                sgn = inbuf[3] & 0x80;
            }
            else
            {
                pcm = (inbuf[3]<<5)|(inbuf[2]<<13)|((inbuf[1]|0x80)<<21);
                exp = ((inbuf[1]>>7)|((inbuf[0]&0x7f)<<1)) - 127;
                sgn = inbuf[0] & 0x80;
            }
            if (exp > -29 && exp < 0)
            {
                pcm >>= -exp;
                if (sgn)
                    pcm = -pcm;
            }
            else if (exp < -28)
                pcm = 0;
            else
                pcm = (sgn)?-(1<<28):(1<<28)-1;

            outbuf[i/4] = pcm;
            inbuf += 4;
        }
        *outbufsize = inbufsize >> 2;
    }
    else
    {
        for (i = 0; i < inbufsize; i += 8)
        {
            if (fmt->is_little_endian)
            {
                pcm = inbuf[3]|(inbuf[4]<<8)|(inbuf[5]<<16)|(((inbuf[6]&0x0f)|0x10)<<24);
                exp = (((inbuf[6]&0xf0)>>4)|((inbuf[7]&0x7f)<<4)) - 1023;
                sgn = inbuf[7] & 0x80;
            }
            else
            {
                pcm = inbuf[4]|(inbuf[3]<<8)|(inbuf[2]<<16)|(((inbuf[1]&0x0f)|0x10)<<24);
                exp = (((inbuf[1]&0xf0)>>4)|((inbuf[0]&0x7f)<<4)) - 1023;
                sgn = inbuf[0] & 0x80;
            }
            if (exp > -29 && exp < 0)
            {
                pcm >>= -exp;
                if (sgn)
                    pcm = -pcm;
            }
            else if (exp < -28)
                pcm = 0;
            else
                pcm = (sgn)?-(1<<28):(1<<28)-1;

            outbuf[i/8] = pcm;
            inbuf += 8;
        }
        *outbufsize = inbufsize >> 3;
    }

    if (fmt->channels == 2)
        *outbufsize >>= 1;

    return CODEC_OK;
}

static const struct pcm_codec codec = {
                                          set_format,
                                          get_seek_pos,
                                          decode,
                                      };

const struct pcm_codec *get_ieee_float_codec(void)
{
    return &codec;
}