commit FS#10424 and FS#10425
[kugel-rb.git] / apps / codecs / libpcm / itut_g711.c
blob9a380318826c263fad80a60783d61c557749b571
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
11 * Copyright (C) 2009 Yoshihisa Uchida
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "codeclib.h"
23 #include "pcm_common.h"
24 #include "support_formats.h"
27 * ITU-T G.711 A-law mu-law
30 static const int16_t alaw2linear16[256] ICONST_ATTR = {
31 -5504, -5248, -6016, -5760, -4480, -4224, -4992,
32 -4736, -7552, -7296, -8064, -7808, -6528, -6272,
33 -7040, -6784, -2752, -2624, -3008, -2880, -2240,
34 -2112, -2496, -2368, -3776, -3648, -4032, -3904,
35 -3264, -3136, -3520, -3392, -22016, -20992, -24064,
36 -23040, -17920, -16896, -19968, -18944, -30208, -29184,
37 -32256, -31232, -26112, -25088, -28160, -27136, -11008,
38 -10496, -12032, -11520, -8960, -8448, -9984, -9472,
39 -15104, -14592, -16128, -15616, -13056, -12544, -14080,
40 -13568, -344, -328, -376, -360, -280, -264,
41 -312, -296, -472, -456, -504, -488, -408,
42 -392, -440, -424, -88, -72, -120, -104,
43 -24, -8, -56, -40, -216, -200, -248,
44 -232, -152, -136, -184, -168, -1376, -1312,
45 -1504, -1440, -1120, -1056, -1248, -1184, -1888,
46 -1824, -2016, -1952, -1632, -1568, -1760, -1696,
47 -688, -656, -752, -720, -560, -528, -624,
48 -592, -944, -912, -1008, -976, -816, -784,
49 -880, -848, 5504, 5248, 6016, 5760, 4480,
50 4224, 4992, 4736, 7552, 7296, 8064, 7808,
51 6528, 6272, 7040, 6784, 2752, 2624, 3008,
52 2880, 2240, 2112, 2496, 2368, 3776, 3648,
53 4032, 3904, 3264, 3136, 3520, 3392, 22016,
54 20992, 24064, 23040, 17920, 16896, 19968, 18944,
55 30208, 29184, 32256, 31232, 26112, 25088, 28160,
56 27136, 11008, 10496, 12032, 11520, 8960, 8448,
57 9984, 9472, 15104, 14592, 16128, 15616, 13056,
58 12544, 14080, 13568, 344, 328, 376, 360,
59 280, 264, 312, 296, 472, 456, 504,
60 488, 408, 392, 440, 424, 88, 72,
61 120, 104, 24, 8, 56, 40, 216,
62 200, 248, 232, 152, 136, 184, 168,
63 1376, 1312, 1504, 1440, 1120, 1056, 1248,
64 1184, 1888, 1824, 2016, 1952, 1632, 1568,
65 1760, 1696, 688, 656, 752, 720, 560,
66 528, 624, 592, 944, 912, 1008, 976,
67 816, 784, 880, 848
70 static const int16_t ulaw2linear16[256] ICONST_ATTR = {
71 -32124, -31100, -30076, -29052, -28028, -27004, -25980,
72 -24956, -23932, -22908, -21884, -20860, -19836, -18812,
73 -17788, -16764, -15996, -15484, -14972, -14460, -13948,
74 -13436, -12924, -12412, -11900, -11388, -10876, -10364,
75 -9852, -9340, -8828, -8316, -7932, -7676, -7420,
76 -7164, -6908, -6652, -6396, -6140, -5884, -5628,
77 -5372, -5116, -4860, -4604, -4348, -4092, -3900,
78 -3772, -3644, -3516, -3388, -3260, -3132, -3004,
79 -2876, -2748, -2620, -2492, -2364, -2236, -2108,
80 -1980, -1884, -1820, -1756, -1692, -1628, -1564,
81 -1500, -1436, -1372, -1308, -1244, -1180, -1116,
82 -1052, -988, -924, -876, -844, -812, -780,
83 -748, -716, -684, -652, -620, -588, -556,
84 -524, -492, -460, -428, -396, -372, -356,
85 -340, -324, -308, -292, -276, -260, -244,
86 -228, -212, -196, -180, -164, -148, -132,
87 -120, -112, -104, -96, -88, -80, -72,
88 -64, -56, -48, -40, -32, -24, -16,
89 -8, 0, 32124, 31100, 30076, 29052, 28028,
90 27004, 25980, 24956, 23932, 22908, 21884, 20860,
91 19836, 18812, 17788, 16764, 15996, 15484, 14972,
92 14460, 13948, 13436, 12924, 12412, 11900, 11388,
93 10876, 10364, 9852, 9340, 8828, 8316, 7932,
94 7676, 7420, 7164, 6908, 6652, 6396, 6140,
95 5884, 5628, 5372, 5116, 4860, 4604, 4348,
96 4092, 3900, 3772, 3644, 3516, 3388, 3260,
97 3132, 3004, 2876, 2748, 2620, 2492, 2364,
98 2236, 2108, 1980, 1884, 1820, 1756, 1692,
99 1628, 1564, 1500, 1436, 1372, 1308, 1244,
100 1180, 1116, 1052, 988, 924, 876, 844,
101 812, 780, 748, 716, 684, 652, 620,
102 588, 556, 524, 492, 460, 428, 396,
103 372, 356, 340, 324, 308, 292, 276,
104 260, 244, 228, 212, 196, 180, 164,
105 148, 132, 120, 112, 104, 96, 88,
106 80, 72, 64, 56, 48, 40, 32,
107 24, 16, 8, 0
110 static struct pcm_format *fmt;
112 static bool set_format(struct pcm_format *format)
114 fmt = format;
116 if (fmt->bitspersample != 8)
118 DEBUGF("CODEC_ERROR: alaw and mulaw must have 8 bitspersample: %d\n",
119 fmt->bitspersample);
120 return false;
123 if (fmt->totalsamples == 0)
125 fmt->bytespersample = 1;
126 fmt->totalsamples = fmt->numbytes / (fmt->bytespersample * fmt->channels);
129 fmt->samplesperblock = fmt->blockalign / (fmt->bytespersample * fmt->channels);
131 /* chunksize = about 1/50[sec] data */
132 fmt->chunksize = (ci->id3->frequency / (50 * fmt->samplesperblock))
133 * fmt->blockalign;
135 return true;
138 static struct pcm_pos *get_seek_pos(long seek_time,
139 uint8_t *(*read_buffer)(size_t *realsize))
141 static struct pcm_pos newpos;
142 uint32_t newblock = ((uint64_t)seek_time * ci->id3->frequency)
143 / (1000LL * fmt->samplesperblock);
145 (void)read_buffer;
146 newpos.pos = newblock * fmt->blockalign;
147 newpos.samples = newblock * fmt->samplesperblock;
148 return &newpos;
151 static int decode_alaw(const uint8_t *inbuf, size_t inbufsize,
152 int32_t *outbuf, int *outbufsize)
154 uint32_t i;
156 for (i = 0; i < inbufsize; i++)
157 outbuf[i] = alaw2linear16[inbuf[i]] << 13;
159 *outbufsize = (fmt->channels == 2) ? (inbufsize >> 1) : inbufsize;
161 return CODEC_OK;
164 static int decode_mulaw(const uint8_t *inbuf, size_t inbufsize,
165 int32_t *outbuf, int *outbufsize)
167 uint32_t i;
169 for (i = 0; i < inbufsize; i++)
170 outbuf[i] = ulaw2linear16[inbuf[i]] << 13;
172 *outbufsize = (fmt->channels == 2) ? (inbufsize >> 1) : inbufsize;
174 return CODEC_OK;
177 static const struct pcm_codec alaw_codec = {
178 set_format,
179 get_seek_pos,
180 decode_alaw,
183 static const struct pcm_codec mulaw_codec = {
184 set_format,
185 get_seek_pos,
186 decode_mulaw,
189 const struct pcm_codec *get_itut_g711_alaw_codec(void)
191 return &alaw_codec;
194 const struct pcm_codec *get_itut_g711_mulaw_codec(void)
196 return &mulaw_codec;