]> iEval git - fdkaac.git/blame_incremental - src/lpcm.c
smart padding for better gapless playback
[fdkaac.git] / src / lpcm.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
4 */
5#if HAVE_CONFIG_H
6# include "config.h"
7#endif
8#if HAVE_STDINT_H
9# include <stdint.h>
10#endif
11#include <stdlib.h>
12#include <math.h>
13#include "lpcm.h"
14#include "m4af_endian.h"
15
16static
17inline float pcm_i2f(int32_t n)
18{
19 union {
20 int32_t ivalue;
21 float fvalue;
22 } u;
23 u.ivalue = n;
24 return u.fvalue;
25}
26static
27inline double pcm_i2d(int64_t n)
28{
29 union {
30 int64_t ivalue;
31 double fvalue;
32 } u;
33 u.ivalue = n;
34 return u.fvalue;
35}
36static
37inline int16_t pcm_quantize_s32(int32_t n)
38{
39 n = ((n >> 15) + 1) >> 1;
40 return (n == 0x8000) ? 0x7fff : n;
41}
42static
43inline int16_t pcm_quantize_f64(double v)
44{
45 return (int16_t)lrint(pcm_clip(v * 32768.0, -32768.0, 32767.0));
46}
47static
48inline int16_t pcm_s8_to_s16(int8_t n)
49{
50 return n << 8;
51}
52static
53inline int16_t pcm_u8_to_s16(uint8_t n)
54{
55 return (n << 8) ^ 0x8000;
56}
57static
58inline int16_t pcm_s16le_to_s16(int16_t n)
59{
60 return m4af_ltoh16(n);
61}
62static
63inline int16_t pcm_s16be_to_s16(int16_t n)
64{
65 return m4af_btoh16(n);
66}
67static
68inline int16_t pcm_u16le_to_s16(uint16_t n)
69{
70 return m4af_ltoh16(n) ^ 0x8000;
71}
72static
73inline int16_t pcm_u16be_to_s16(uint16_t n)
74{
75 return m4af_btoh16(n) ^ 0x8000;
76}
77static
78inline int32_t pcm_s24le_to_s32(uint8_t *p)
79{
80 return p[0]<<8 | p[1]<<16 | p[2]<<24;
81}
82static
83inline int32_t pcm_s24be_to_s32(uint8_t *p)
84{
85 return p[0]<<24 | p[1]<<16 | p[2]<<8;
86}
87static
88inline int32_t pcm_u24le_to_s32(uint8_t *p)
89{
90 return pcm_s24le_to_s32(p) ^ 0x80000000;
91}
92static
93inline int32_t pcm_u24be_to_s32(uint8_t *p)
94{
95 return pcm_s24be_to_s32(p) ^ 0x80000000;
96}
97static
98inline int16_t pcm_s24le_to_s16(uint8_t *p)
99{
100 return pcm_quantize_s32(pcm_s24le_to_s32(p));
101}
102static
103inline int16_t pcm_s24be_to_s16(uint8_t *p)
104{
105 return pcm_quantize_s32(pcm_s24be_to_s32(p));
106}
107static
108inline int16_t pcm_u24le_to_s16(uint8_t *p)
109{
110 return pcm_quantize_s32(pcm_u24le_to_s32(p));
111}
112static
113inline int16_t pcm_u24be_to_s16(uint8_t *p)
114{
115 return pcm_quantize_s32(pcm_u24be_to_s32(p));
116}
117static
118inline int16_t pcm_s32le_to_s16(int32_t n)
119{
120 return pcm_quantize_s32(m4af_ltoh32(n));
121}
122static
123inline int16_t pcm_s32be_to_s16(int32_t n)
124{
125 return pcm_quantize_s32(m4af_btoh32(n));
126}
127static
128inline int16_t pcm_u32le_to_s16(int32_t n)
129{
130 return pcm_quantize_s32(m4af_ltoh32(n) ^ 0x80000000);
131}
132static
133inline int16_t pcm_u32be_to_s16(int32_t n)
134{
135 return pcm_quantize_s32(m4af_btoh32(n) ^ 0x80000000);
136}
137static
138inline int16_t pcm_f32le_to_s16(int32_t n)
139{
140 return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n)));
141}
142static
143inline int16_t pcm_f32be_to_s16(int32_t n)
144{
145 return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n)));
146}
147static
148inline int16_t pcm_f64le_to_s16(int64_t n)
149{
150 return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n)));
151}
152static
153inline int16_t pcm_f64be_to_s16(int64_t n)
154{
155 return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n)));
156}
157
158int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
159 const void *input, uint32_t nframes,
160 int16_t *result)
161{
162#define CONVERT(type, conv) \
163 do { \
164 unsigned i; \
165 type *ip = (type *)input; \
166 for (i = 0; i < count; ++i) { \
167 result[i] = conv(ip[i]); \
168 } \
169 } while(0)
170
171#define CONVERT_BYTES(conv) \
172 do { \
173 unsigned i, bytes_per_channel; \
174 uint8_t *ip = (uint8_t *)input; \
175 bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \
176 for (i = 0; i < count; ++i) { \
177 result[i] = conv(ip); \
178 ip += bytes_per_channel; \
179 } \
180 } while(0)
181
182 uint32_t count = nframes * format->channels_per_frame;
183 if (!count)
184 return 0;
185 switch (PCM_BYTES_PER_CHANNEL(format) | format->sample_type<<4) {
186 case 1 | PCM_TYPE_SINT<<4:
187 CONVERT(int8_t, pcm_s8_to_s16); break;
188 case 1 | PCM_TYPE_UINT<<4:
189 CONVERT(uint8_t, pcm_u8_to_s16); break;
190 case 2 | PCM_TYPE_SINT<<4:
191 CONVERT(int16_t, pcm_s16le_to_s16); break;
192 case 2 | PCM_TYPE_UINT<<4:
193 CONVERT(uint16_t, pcm_u16le_to_s16); break;
194 case 2 | PCM_TYPE_SINT_BE<<4:
195 CONVERT(int16_t, pcm_s16be_to_s16); break;
196 case 2 | PCM_TYPE_UINT_BE<<4:
197 CONVERT(int16_t, pcm_u16be_to_s16); break;
198 case 3 | PCM_TYPE_SINT<<4:
199 CONVERT_BYTES(pcm_s24le_to_s16); break;
200 case 3 | PCM_TYPE_UINT<<4:
201 CONVERT_BYTES(pcm_u24le_to_s16); break;
202 case 3 | PCM_TYPE_SINT_BE<<4:
203 CONVERT_BYTES(pcm_s24be_to_s16); break;
204 case 3 | PCM_TYPE_UINT_BE<<4:
205 CONVERT_BYTES(pcm_u24be_to_s16); break;
206 case 4 | PCM_TYPE_SINT<<4:
207 CONVERT(int32_t, pcm_s32le_to_s16); break;
208 case 4 | PCM_TYPE_UINT<<4:
209 CONVERT(uint32_t, pcm_u32le_to_s16); break;
210 case 4 | PCM_TYPE_FLOAT<<4:
211 CONVERT(int32_t, pcm_f32le_to_s16); break;
212 case 4 | PCM_TYPE_SINT_BE<<4:
213 CONVERT(int32_t, pcm_s32be_to_s16); break;
214 case 4 | PCM_TYPE_UINT_BE<<4:
215 CONVERT(uint32_t, pcm_u32be_to_s16); break;
216 case 4 | PCM_TYPE_FLOAT_BE<<4:
217 CONVERT(int32_t, pcm_f32be_to_s16); break;
218 case 8 | PCM_TYPE_FLOAT<<4:
219 CONVERT(int64_t, pcm_f64le_to_s16); break;
220 case 8 | PCM_TYPE_FLOAT_BE<<4:
221 CONVERT(int64_t, pcm_f64be_to_s16); break;
222 default:
223 return -1;
224 }
225 return 0;
226}
This page took 0.023853 seconds and 4 git commands to generate.