1 /*
2 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
24 #include "config.h"
33
38 };
39
41
43 int len,
int channels);
44
46 int channels);
47
68 };
69
72 int ptr_align, int samples_align,
73 const char *descr,
void *
conv)
74 {
75 int found = 0;
76
85 if (ptr_align == 1 && samples_align == 1) {
88 } else {
90 }
91 found = 1;
92 }
93 break;
96 (!channels || ac->
channels == channels)) {
101 if (ptr_align == 1 && samples_align == 1) {
104 } else {
106 }
107 found = 1;
108 }
109 break;
112 (!channels || ac->
channels == channels)) {
117 if (ptr_align == 1 && samples_align == 1) {
120 } else {
122 }
123 found = 1;
124 }
125 break;
126 }
127 if (found) {
131 }
132 }
133
134 #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt
135
136 #define CONV_LOOP(otype, expr) \
137 do { \
138 *(otype *)po = expr; \
139 pi += is; \
140 po += os; \
141 } while (po < end); \
142
143 #define CONV_FUNC_FLAT(ofmt, otype, ifmt, itype, expr) \
144 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t *in, \
145 int len) \
146 { \
147 int is = sizeof(itype); \
148 int os = sizeof(otype); \
149 const uint8_t *pi = in; \
150 uint8_t *po = out; \
151 uint8_t *end = out + os * len; \
152 CONV_LOOP(otype, expr) \
153 }
154
155 #define CONV_FUNC_INTERLEAVE(ofmt, otype, ifmt, itype, expr) \
156 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t **in, \
157 int len, int channels) \
158 { \
159 int ch; \
160 int out_bps = sizeof(otype); \
161 int is = sizeof(itype); \
162 int os = channels * out_bps; \
163 for (ch = 0; ch < channels; ch++) { \
164 const uint8_t *pi = in[ch]; \
165 uint8_t *po = out + ch * out_bps; \
166 uint8_t *end = po + os * len; \
167 CONV_LOOP(otype, expr) \
168 } \
169 }
170
171 #define CONV_FUNC_DEINTERLEAVE(ofmt, otype, ifmt, itype, expr) \
172 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t **out, const uint8_t *in, \
173 int len, int channels) \
174 { \
175 int ch; \
176 int in_bps = sizeof(itype); \
177 int is = channels * in_bps; \
178 int os = sizeof(otype); \
179 for (ch = 0; ch < channels; ch++) { \
180 const uint8_t *pi = in + ch * in_bps; \
181 uint8_t *po = out[ch]; \
182 uint8_t *end = po + os * len; \
183 CONV_LOOP(otype, expr) \
184 } \
185 }
186
187 #define CONV_FUNC_GROUP(ofmt, otype, ifmt, itype, expr) \
188 CONV_FUNC_FLAT( ofmt, otype, ifmt, itype, expr) \
189 CONV_FUNC_INTERLEAVE( ofmt, otype, ifmt ## P, itype, expr) \
190 CONV_FUNC_DEINTERLEAVE(ofmt ## P, otype, ifmt, itype, expr)
191
197 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t, (*(const int16_t *)pi >> 8) + 0x80)
198 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi)
199 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi << 16)
200 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT,
float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0f / (1 << 15)))
201 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL,
double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0 / (1 << 15)))
202 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t, (*(const int32_t *)pi >> 24) + 0x80)
203 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi >> 16)
204 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi)
205 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT,
float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0f / (1
U << 31)))
206 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL,
double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0 / (1
U << 31)))
207 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT,
float, av_clip_uint8(
lrintf(*(const
float *)pi * (1 << 7)) + 0x80))
208 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT,
float, av_clip_int16(
lrintf(*(const
float *)pi * (1 << 15))))
209 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT,
float, av_clipl_int32(
llrintf(*(const
float *)pi * (1
U << 31))))
210 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT,
float, AV_SAMPLE_FMT_FLT,
float, *(const
float *)pi)
211 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL,
double, AV_SAMPLE_FMT_FLT,
float, *(const
float *)pi)
212 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL,
double, av_clip_uint8(
lrint(*(const
double *)pi * (1 << 7)) + 0x80))
213 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL,
double, av_clip_int16(
lrint(*(const
double *)pi * (1 << 15))))
214 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL,
double, av_clipl_int32(
llrint(*(const
double *)pi * (1
U << 31))))
215 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT,
float, AV_SAMPLE_FMT_DBL,
double, *(const
double *)pi)
216 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL,
double, AV_SAMPLE_FMT_DBL,
double, *(const
double *)pi)
217
218 #define SET_CONV_FUNC_GROUP(ofmt, ifmt) \
219 ff_audio_convert_set_func(ac, ofmt, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt)); \
220 ff_audio_convert_set_func(ac, ofmt ## P, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt ## P, ifmt)); \
221 ff_audio_convert_set_func(ac, ofmt, ifmt ## P, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt ## P));
222
224 {
250 }
251
253 {
254 if (!*ac)
255 return;
258 }
259
264 int apply_map)
265 {
267 int in_planar, out_planar;
268
270 if (!ac)
271 return NULL;
272
278
283 apply_map);
286 return NULL;
287 }
288 return ac;
289 }
290
293
294 if (in_planar == out_planar) {
297 } else if (in_planar)
299 else
301
302 set_generic_function(ac);
303
304 if (ARCH_ARM)
306 if (ARCH_X86)
308
309 return ac;
310 }
311
313 {
314 int use_generic = 1;
316 int p;
317
319 /* dithered conversion */
320 av_dlog(ac->
avr,
"%d samples - audio_convert: %s to %s (dithered)\n",
323
325 }
326
327 /* determine whether to use the optimized function based on pointer and
328 samples alignment in both the input and output */
333 if (!(ptr_align % ac->
ptr_align) && samples_align >= aligned_len) {
334 len = aligned_len;
335 use_generic = 0;
336 }
337 }
338 av_dlog(ac->
avr,
"%d samples - audio_convert: %s to %s (%s)\n", len,
342
345
349 }
350
355
356 for (p = 0; p < ac->
planes; p++)
359 } else {
364
367
369 }
370 }
372 for (p = 0; p < ac->
planes; p++) {
378 }
379 }
385 if (use_generic) {
386 for (p = 0; p < ac->
planes; p++)
388 } else {
389 for (p = 0; p < ac->
planes; p++)
391 }
392 break;
393 }
395 if (use_generic)
398 else
400 break;
402 if (use_generic)
405 else
408 break;
409 }
410 }
411
414 }