summaryrefslogtreecommitdiffstats
path: root/apps/codecs/libpcm/linear_pcm.c
blob: 4db27ca2b315caf16a8dcf080128da72811329d3 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Dave Chapman
 * 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"
#include "support_formats.h"

/*
 * Linear PCM
 */

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)
    {
        DEBUGF("CODEC_ERROR: pcm with more than 32 bitspersample "
               "is unsupported\n");
        return false;
    }

    if (fmt->totalsamples == 0)
    {
        fmt->bytespersample = (((fmt->bitspersample - 1)/8 + 1)*fmt->channels);
        fmt->totalsamples = fmt->numbytes/fmt->bytespersample;
    }

    /* chunksize is computed so that one chunk is about 1/50s.
     * this make 4096 for 44.1kHz 16bits stereo.
     * It also has to be a multiple of blockalign */
    fmt->chunksize = (1 + fmt->avgbytespersec / (50*fmt->blockalign))*fmt->blockalign;

    return true;
}

static uint32_t get_seek_pos(long seek_time)
{
    uint32_t newpos;

    /* use avgbytespersec to round to the closest blockalign multiple,
       add firstblockposn. 64-bit casts to avoid overflows. */
    newpos = (((uint64_t)fmt->avgbytespersec*(seek_time - 1))
             / (1000LL*fmt->blockalign))*fmt->blockalign;
    return newpos;
}

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

    if (fmt->bitspersample > 24)
    {
        for (i = 0; i < inbufsize; i += 4)
        {
            if (fmt->is_little_endian)
            {
                if (fmt->is_signed)
                    outbuf[i/4] = (inbuf[i]>>3)|(inbuf[i+1]<<5)|(inbuf[i+2]<<13)|(SE(inbuf[i+3])<<21);
                else
                    outbuf[i/4] = (inbuf[i]>>3)|(inbuf[i+1]<<5)|(inbuf[i+2]<<13)|(SFT(inbuf[i+3])<<21);
            }
            else
            {
                if (fmt->is_signed)
                    outbuf[i/4] = (inbuf[i+3]>>3)|(inbuf[i+2]<<5)|(inbuf[i+1]<<13)|(SE(inbuf[i])<<21);
                else
                    outbuf[i/4] = (inbuf[i+3]>>3)|(inbuf[i+2]<<5)|(inbuf[i+1]<<13)|(SFT(inbuf[i])<<21);
            }
        }
        *outbufsize = inbufsize >> 2;
    }
    else if (fmt->bitspersample > 16)
    {
        for (i = 0; i < inbufsize; i += 3)
        {
            if (fmt->is_little_endian)
            {
                if (fmt->is_signed)
                    outbuf[i/3] = (inbuf[i]<<5)|(inbuf[i+1]<<13)|(SE(inbuf[i+2])<<21);
                else
                    outbuf[i/3] = (inbuf[i]<<5)|(inbuf[i+1]<<13)|(SFT(inbuf[i+2])<<21);
            }
            else
            {
                if (fmt->is_signed)
                    outbuf[i/3] = (inbuf[i+2]<<5)|(inbuf[i+1]<<13)|(SE(inbuf[i])<<21);
                else
                    outbuf[i/3] = (inbuf[i+2]<<5)|(inbuf[i+1]<<13)|(SFT(inbuf[i])<<21);
            }
        }
        *outbufsize = inbufsize/3;
    }
    else if (fmt->bitspersample > 8)
    {
        for (i = 0; i < inbufsize; i += 2)
        {
            if (fmt->is_little_endian)
            {
                if (fmt->is_signed)
                    outbuf[i/2] = (inbuf[i]<<13)|(SE(inbuf[i+1])<<21);
                else
                    outbuf[i/2] = (inbuf[i]<<13)|(SFT(inbuf[i+1])<<21);
            }
            else
            {
                if (fmt->is_signed)
                    outbuf[i/2] = (inbuf[i+1]<<13)|(SE(inbuf[i])<<21);
                else
                    outbuf[i/2] = (inbuf[i+1]<<13)|(SFT(inbuf[i])<<21);
            }
        }
        *outbufsize = inbufsize >> 1;
    }
    else
    {
        for (i = 0; i < inbufsize; i++) {
            if (fmt->is_signed)
                outbuf[i] = SE(inbuf[i])<<21;
            else
                outbuf[i] = SFT(inbuf[i])<<21;
        }
        *outbufsize = inbufsize;
    }

    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_linear_pcm_codec(void)
{
    return &codec;
}