smart padding for better gapless playback
[fdkaac.git] / src / lpcm.c
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
16 static
17 inline 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 }
26 static
27 inline 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 }
36 static
37 inline int16_t pcm_quantize_s32(int32_t n)
38 {
39 n = ((n >> 15) + 1) >> 1;
40 return (n == 0x8000) ? 0x7fff : n;
41 }
42 static
43 inline int16_t pcm_quantize_f64(double v)
44 {
45 return (int16_t)lrint(pcm_clip(v * 32768.0, -32768.0, 32767.0));
46 }
47 static
48 inline int16_t pcm_s8_to_s16(int8_t n)
49 {
50 return n << 8;
51 }
52 static
53 inline int16_t pcm_u8_to_s16(uint8_t n)
54 {
55 return (n << 8) ^ 0x8000;
56 }
57 static
58 inline int16_t pcm_s16le_to_s16(int16_t n)
59 {
60 return m4af_ltoh16(n);
61 }
62 static
63 inline int16_t pcm_s16be_to_s16(int16_t n)
64 {
65 return m4af_btoh16(n);
66 }
67 static
68 inline int16_t pcm_u16le_to_s16(uint16_t n)
69 {
70 return m4af_ltoh16(n) ^ 0x8000;
71 }
72 static
73 inline int16_t pcm_u16be_to_s16(uint16_t n)
74 {
75 return m4af_btoh16(n) ^ 0x8000;
76 }
77 static
78 inline int32_t pcm_s24le_to_s32(uint8_t *p)
79 {
80 return p[0]<<8 | p[1]<<16 | p[2]<<24;
81 }
82 static
83 inline int32_t pcm_s24be_to_s32(uint8_t *p)
84 {
85 return p[0]<<24 | p[1]<<16 | p[2]<<8;
86 }
87 static
88 inline int32_t pcm_u24le_to_s32(uint8_t *p)
89 {
90 return pcm_s24le_to_s32(p) ^ 0x80000000;
91 }
92 static
93 inline int32_t pcm_u24be_to_s32(uint8_t *p)
94 {
95 return pcm_s24be_to_s32(p) ^ 0x80000000;
96 }
97 static
98 inline int16_t pcm_s24le_to_s16(uint8_t *p)
99 {
100 return pcm_quantize_s32(pcm_s24le_to_s32(p));
101 }
102 static
103 inline int16_t pcm_s24be_to_s16(uint8_t *p)
104 {
105 return pcm_quantize_s32(pcm_s24be_to_s32(p));
106 }
107 static
108 inline int16_t pcm_u24le_to_s16(uint8_t *p)
109 {
110 return pcm_quantize_s32(pcm_u24le_to_s32(p));
111 }
112 static
113 inline int16_t pcm_u24be_to_s16(uint8_t *p)
114 {
115 return pcm_quantize_s32(pcm_u24be_to_s32(p));
116 }
117 static
118 inline int16_t pcm_s32le_to_s16(int32_t n)
119 {
120 return pcm_quantize_s32(m4af_ltoh32(n));
121 }
122 static
123 inline int16_t pcm_s32be_to_s16(int32_t n)
124 {
125 return pcm_quantize_s32(m4af_btoh32(n));
126 }
127 static
128 inline int16_t pcm_u32le_to_s16(int32_t n)
129 {
130 return pcm_quantize_s32(m4af_ltoh32(n) ^ 0x80000000);
131 }
132 static
133 inline int16_t pcm_u32be_to_s16(int32_t n)
134 {
135 return pcm_quantize_s32(m4af_btoh32(n) ^ 0x80000000);
136 }
137 static
138 inline int16_t pcm_f32le_to_s16(int32_t n)
139 {
140 return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n)));
141 }
142 static
143 inline int16_t pcm_f32be_to_s16(int32_t n)
144 {
145 return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n)));
146 }
147 static
148 inline int16_t pcm_f64le_to_s16(int64_t n)
149 {
150 return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n)));
151 }
152 static
153 inline int16_t pcm_f64be_to_s16(int64_t n)
154 {
155 return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n)));
156 }
157
158 int 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.027843 seconds and 4 git commands to generate.