1 /*******************************************************************************
2  audio.c
3 
4  libquicktime - A library for reading and writing quicktime/avi/mp4 files.
5  http://libquicktime.sourceforge.net
6 
7  Copyright (C) 2002 Heroine Virtual Ltd.
8  Copyright (C) 2002-2011 Members of the libquicktime project.
9 
10  This library is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser General Public License as published by the Free
12  Software Foundation; either version 2.1 of the License, or (at your option)
13  any later version.
14 
15  This library is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18  details.
19 
20  You should have received a copy of the GNU Lesser General Public License along
21  with this library; if not, write to the Free Software Foundation, Inc., 51
22  Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *******************************************************************************/
24 
25 #include "lqt_private.h"
26 
27 #define LOG_DOMAIN "audio"
28 
29 /***************************************************
30  * Audio conversion functions
31  ***************************************************/
32 
33 #define RECLIP(val, min, max) (val<min?min:((val>max)?max:val))
34 
35 /* Conversion Macros */
36 
37 /* int8 -> */
38 
39 #define INT8_TO_INT16(src, dst) dst = src * 0x0101
40 
41 #define INT8_TO_FLOAT(src, dst) dst = (float)src / 128.0
42 
43 /* uint8 -> */
44 
45 #define UINT8_TO_INT16(src, dst) dst = (src - 128) * 0x0101
46 
47 #define UINT8_TO_FLOAT(src, dst) dst = (float)src / 127.0 - 1.0
48 
49 /* int16 -> */
50 
51 #define INT16_TO_INT8(src, dst) dst = src >> 8
52 
53 #define INT16_TO_UINT8(src, dst) dst = (src ^ 0x8000) >> 8
54 
55 #define INT16_TO_INT16(src, dst) dst = src
56 
57 #define INT16_TO_INT32(src, dst) dst = src * 0x00010001
58 
59 #define INT16_TO_FLOAT(src, dst) dst = (float)src / 32767.0f
60 
61 #define INT16_TO_DOUBLE(src, dst) dst = (double)src / 32767.0
62 
63 /* int32 -> */
64 
65 #define INT32_TO_INT16(src, dst) dst = src >> 16
66 
67 #define INT32_TO_FLOAT(src, dst) dst = (float)src / 2147483647.0
68 
69 /* float -> */
70 
71 #define FLOAT_TO_INT8(src, dst) tmp = (int)(src * 127.0); dst = RECLIP(tmp, -128, 127)
72 
73 #define FLOAT_TO_UINT8(src, dst) tmp = (int)((src + 1.0) * 127.0); dst = RECLIP(tmp, 0, 255)
74 
75 #define FLOAT_TO_INT16(src, dst) tmp = (int)((src) * 32767.0); dst = RECLIP(tmp, -32768, 32767)
76 
77 #define FLOAT_TO_INT32(src, dst) tmp = (int64_t)((src) * 2147483647.0); dst = RECLIP(tmp, -2147483648LL, 2147483647LL)
78 
79 #define FLOAT_TO_FLOAT(src, dst) dst = src
80 
81 #define FLOAT_TO_DOUBLE(src, dst) dst = src
82 
83 
84 #define DOUBLE_TO_INT16(src, dst) tmp = (int)((src) * 32767.0); dst = RECLIP(tmp, -32768, 32767)
85 
86 #define DOUBLE_TO_FLOAT(src, dst) dst = src
87 
88 
89 /* Encoding */
90 
encode_int16_to_int8(int16_t ** in,void * _out,int num_channels,int num_samples)91 static void encode_int16_to_int8(int16_t ** in, void * _out, int num_channels, int num_samples)
92   {
93   int i, j;
94   int8_t * out;
95   for(i = 0; i < num_channels; i++)
96     {
97     out = ((int8_t*)_out) + i;
98     for(j = 0; j < num_samples; j++)
99       {
100       INT16_TO_INT8(in[i][j], (*out));
101       out+=num_channels;
102       }
103     }
104   }
105 
encode_int16_to_uint8(int16_t ** in,void * _out,int num_channels,int num_samples)106 static void encode_int16_to_uint8(int16_t ** in, void * _out, int num_channels, int num_samples)
107   {
108   int i, j;
109   uint8_t * out;
110   for(i = 0; i < num_channels; i++)
111     {
112     out = ((uint8_t*)_out) + i;
113     for(j = 0; j < num_samples; j++)
114       {
115       INT16_TO_UINT8(in[i][j], (*out));
116       out+=num_channels;
117       }
118     }
119   }
120 
encode_int16_to_int16(int16_t ** in,void * _out,int num_channels,int num_samples)121 static void encode_int16_to_int16(int16_t ** in, void * _out, int num_channels, int num_samples)
122   {
123   int i, j;
124   int16_t * out;
125   for(i = 0; i < num_channels; i++)
126     {
127     out = ((int16_t*)_out) + i;
128     for(j = 0; j < num_samples; j++)
129       {
130       INT16_TO_INT16(in[i][j], (*out));
131       out+=num_channels;
132       }
133     }
134   }
135 
encode_int16_to_int32(int16_t ** in,void * _out,int num_channels,int num_samples)136 static void encode_int16_to_int32(int16_t ** in, void * _out, int num_channels, int num_samples)
137   {
138   int i, j;
139   int32_t * out;
140   for(i = 0; i < num_channels; i++)
141     {
142     out = ((int32_t*)_out) + i;
143     for(j = 0; j < num_samples; j++)
144       {
145       INT16_TO_INT32(in[i][j], (*out));
146       out+=num_channels;
147       }
148     }
149   }
150 
encode_int16_to_float(int16_t ** in,void * _out,int num_channels,int num_samples)151 static void encode_int16_to_float(int16_t ** in, void * _out, int num_channels, int num_samples)
152   {
153   int i, j;
154   float * out;
155   for(i = 0; i < num_channels; i++)
156     {
157     out = ((float*)_out) + i;
158     for(j = 0; j < num_samples; j++)
159       {
160       INT16_TO_FLOAT(in[i][j], (*out));
161       out+=num_channels;
162       }
163     }
164   }
165 
encode_int16_to_double(int16_t ** in,void * _out,int num_channels,int num_samples)166 static void encode_int16_to_double(int16_t ** in, void * _out, int num_channels, int num_samples)
167   {
168   int i, j;
169   double * out;
170   for(i = 0; i < num_channels; i++)
171     {
172     out = ((double*)_out) + i;
173     for(j = 0; j < num_samples; j++)
174       {
175       INT16_TO_DOUBLE(in[i][j], (*out));
176       out+=num_channels;
177       }
178     }
179   }
180 
181 
encode_float_to_int8(float ** in,void * _out,int num_channels,int num_samples)182 static void encode_float_to_int8(float ** in, void * _out, int num_channels, int num_samples)
183   {
184   int i, j, tmp;
185   int8_t * out;
186   for(i = 0; i < num_channels; i++)
187     {
188     out = ((int8_t*)_out) + i;
189     for(j = 0; j < num_samples; j++)
190       {
191       FLOAT_TO_INT8(in[i][j], (*out));
192       out+=num_channels;
193       }
194     }
195   }
196 
encode_float_to_uint8(float ** in,void * _out,int num_channels,int num_samples)197 static void encode_float_to_uint8(float ** in, void * _out, int num_channels, int num_samples)
198   {
199   int i, j, tmp;
200   uint8_t * out;
201   for(i = 0; i < num_channels; i++)
202     {
203     out = ((uint8_t*)_out) + i;
204     for(j = 0; j < num_samples; j++)
205       {
206       FLOAT_TO_UINT8(in[i][j], (*out));
207       out+=num_channels;
208       }
209     }
210   }
211 
encode_float_to_int16(float ** in,void * _out,int num_channels,int num_samples)212 static void encode_float_to_int16(float ** in, void * _out, int num_channels, int num_samples)
213   {
214   int i, j, tmp;
215   int16_t * out;
216   for(i = 0; i < num_channels; i++)
217     {
218     out = ((int16_t*)_out) + i;
219     for(j = 0; j < num_samples; j++)
220       {
221       FLOAT_TO_INT16(in[i][j], (*out));
222       out+=num_channels;
223       }
224     }
225   }
226 
encode_float_to_int32(float ** in,void * _out,int num_channels,int num_samples)227 static void encode_float_to_int32(float ** in, void * _out, int num_channels, int num_samples)
228   {
229   int i, j;
230   int64_t tmp;
231   int32_t * out;
232   for(i = 0; i < num_channels; i++)
233     {
234     out = ((int32_t*)_out) + i;
235     for(j = 0; j < num_samples; j++)
236       {
237       FLOAT_TO_INT32(in[i][j], (*out));
238       out+=num_channels;
239       }
240     }
241   }
242 
encode_float_to_float(float ** in,void * _out,int num_channels,int num_samples)243 static void encode_float_to_float(float ** in, void * _out, int num_channels, int num_samples)
244   {
245   int i, j;
246   float * out;
247   for(i = 0; i < num_channels; i++)
248     {
249     out = ((float*)_out) + i;
250     for(j = 0; j < num_samples; j++)
251       {
252       FLOAT_TO_FLOAT(in[i][j], (*out));
253       out+=num_channels;
254       }
255     }
256   }
257 
encode_float_to_double(float ** in,void * _out,int num_channels,int num_samples)258 static void encode_float_to_double(float ** in, void * _out, int num_channels, int num_samples)
259   {
260   int i, j;
261   double * out;
262   for(i = 0; i < num_channels; i++)
263     {
264     out = ((double*)_out) + i;
265     for(j = 0; j < num_samples; j++)
266       {
267       FLOAT_TO_DOUBLE(in[i][j], (*out));
268       out+=num_channels;
269       }
270     }
271   }
272 
lqt_convert_audio_encode(quicktime_t * file,int16_t ** in_int,float ** in_float,void * out,int num_channels,int num_samples,lqt_sample_format_t stream_format)273 void lqt_convert_audio_encode(quicktime_t * file, int16_t ** in_int, float ** in_float, void * out,
274                               int num_channels, int num_samples,
275                               lqt_sample_format_t stream_format)
276   {
277   switch(stream_format)
278     {
279     case LQT_SAMPLE_INT8:
280       if(in_int)
281         encode_int16_to_int8(in_int, out, num_channels, num_samples);
282       else if(in_float)
283         encode_float_to_int8(in_float, out, num_channels, num_samples);
284       break;
285     case LQT_SAMPLE_UINT8:
286       if(in_int)
287         encode_int16_to_uint8(in_int, out, num_channels, num_samples);
288       else if(in_float)
289         encode_float_to_uint8(in_float, out, num_channels, num_samples);
290       break;
291     case LQT_SAMPLE_INT16:
292       if(in_int)
293         encode_int16_to_int16(in_int, out, num_channels, num_samples);
294       else if(in_float)
295         encode_float_to_int16(in_float, out, num_channels, num_samples);
296       break;
297     case LQT_SAMPLE_INT32:
298       if(in_int)
299         encode_int16_to_int32(in_int, out, num_channels, num_samples);
300       else if(in_float)
301         encode_float_to_int32(in_float, out, num_channels, num_samples);
302       break;
303     case LQT_SAMPLE_FLOAT:
304       if(in_int)
305         encode_int16_to_float(in_int, out, num_channels, num_samples);
306       else if(in_float)
307         encode_float_to_float(in_float, out, num_channels, num_samples);
308       break;
309     case LQT_SAMPLE_DOUBLE:
310       if(in_int)
311         encode_int16_to_double(in_int, out, num_channels, num_samples);
312       else if(in_float)
313         encode_float_to_double(in_float, out, num_channels, num_samples);
314       break;
315     case LQT_SAMPLE_UNDEFINED:
316       lqt_log(file, LQT_LOG_ERROR, LOG_DOMAIN, "Cannot encode samples: Stream format undefined");
317       break;
318     }
319   }
320 
321 /* Decoding */
322 
decode_int8_to_int16(void * _in,int16_t ** out,int num_channels,int num_samples)323 static void decode_int8_to_int16(void * _in, int16_t ** out, int num_channels, int num_samples)
324   {
325   int i, j;
326   int8_t * in;
327   for(i = 0; i < num_channels; i++)
328     {
329     if(out[i])
330       {
331       in = ((int8_t*)_in) + i;
332       for(j = 0; j < num_samples; j++)
333         {
334         INT8_TO_INT16((*in), out[i][j]);
335         in += num_channels;
336         }
337       }
338     }
339   }
340 
decode_uint8_to_int16(void * _in,int16_t ** out,int num_channels,int num_samples)341 static void decode_uint8_to_int16(void * _in, int16_t ** out, int num_channels, int num_samples)
342   {
343   int i, j;
344   uint8_t * in;
345   for(i = 0; i < num_channels; i++)
346     {
347     if(out[i])
348       {
349       in = ((uint8_t*)_in) + i;
350       for(j = 0; j < num_samples; j++)
351         {
352         UINT8_TO_INT16((*in), out[i][j]);
353         in += num_channels;
354         }
355       }
356     }
357   }
358 
decode_int16_to_int16(void * _in,int16_t ** out,int num_channels,int num_samples)359 static void decode_int16_to_int16(void * _in, int16_t ** out, int num_channels, int num_samples)
360   {
361   int i, j;
362   int16_t * in;
363   for(i = 0; i < num_channels; i++)
364     {
365     if(out[i])
366       {
367       in = ((int16_t*)_in) + i;
368       for(j = 0; j < num_samples; j++)
369         {
370         INT16_TO_INT16((*in), out[i][j]);
371         in += num_channels;
372         }
373       }
374     }
375   }
376 
decode_int32_to_int16(void * _in,int16_t ** out,int num_channels,int num_samples)377 static void decode_int32_to_int16(void * _in, int16_t ** out, int num_channels, int num_samples)
378   {
379   int i, j;
380   int32_t * in;
381   for(i = 0; i < num_channels; i++)
382     {
383     if(out[i])
384       {
385       in = ((int32_t*)_in) + i;
386       for(j = 0; j < num_samples; j++)
387         {
388         INT32_TO_INT16((*in), out[i][j]);
389         in += num_channels;
390         }
391       }
392     }
393   }
394 
decode_float_to_int16(void * _in,int16_t ** out,int num_channels,int num_samples)395 static void decode_float_to_int16(void * _in, int16_t ** out, int num_channels, int num_samples)
396   {
397   int i, j, tmp;
398   float * in;
399   for(i = 0; i < num_channels; i++)
400     {
401     if(out[i])
402       {
403       in = ((float*)_in) + i;
404       for(j = 0; j < num_samples; j++)
405         {
406         FLOAT_TO_INT16((*in), out[i][j]);
407         in += num_channels;
408         }
409       }
410     }
411   }
412 
decode_double_to_int16(void * _in,int16_t ** out,int num_channels,int num_samples)413 static void decode_double_to_int16(void * _in, int16_t ** out, int num_channels, int num_samples)
414   {
415   int i, j, tmp;
416   double * in;
417   for(i = 0; i < num_channels; i++)
418     {
419     if(out[i])
420       {
421       in = ((double*)_in) + i;
422       for(j = 0; j < num_samples; j++)
423         {
424         DOUBLE_TO_INT16((*in), out[i][j]);
425         in += num_channels;
426         }
427       }
428     }
429   }
430 
decode_int8_to_float(void * _in,float ** out,int num_channels,int num_samples)431 static void decode_int8_to_float(void * _in, float ** out, int num_channels, int num_samples)
432   {
433   int i, j;
434   int8_t * in;
435   for(i = 0; i < num_channels; i++)
436     {
437     if(out[i])
438       {
439       in = ((int8_t*)_in) + i;
440       for(j = 0; j < num_samples; j++)
441         {
442         INT8_TO_FLOAT((*in), out[i][j]);
443         in += num_channels;
444         }
445       }
446     }
447   }
448 
decode_uint8_to_float(void * _in,float ** out,int num_channels,int num_samples)449 static void decode_uint8_to_float(void * _in, float ** out, int num_channels, int num_samples)
450   {
451   int i, j;
452   uint8_t * in;
453   for(i = 0; i < num_channels; i++)
454     {
455     if(out[i])
456       {
457       in = ((uint8_t*)_in) + i;
458       for(j = 0; j < num_samples; j++)
459         {
460         UINT8_TO_FLOAT((*in), out[i][j]);
461         in += num_channels;
462         }
463       }
464     }
465   }
466 
decode_int16_to_float(void * _in,float ** out,int num_channels,int num_samples)467 static void decode_int16_to_float(void * _in, float ** out, int num_channels, int num_samples)
468   {
469   int i, j;
470   int16_t * in;
471   for(i = 0; i < num_channels; i++)
472     {
473     if(out[i])
474       {
475       in = ((int16_t*)_in) + i;
476       for(j = 0; j < num_samples; j++)
477         {
478         INT16_TO_FLOAT((*in), out[i][j]);
479         in += num_channels;
480         }
481       }
482     }
483   }
484 
decode_int32_to_float(void * _in,float ** out,int num_channels,int num_samples)485 static void decode_int32_to_float(void * _in, float ** out, int num_channels, int num_samples)
486   {
487   int i, j;
488   int32_t * in;
489   for(i = 0; i < num_channels; i++)
490     {
491     if(out[i])
492       {
493       in = ((int32_t*)_in) + i;
494       for(j = 0; j < num_samples; j++)
495         {
496         INT32_TO_FLOAT((*in), out[i][j]);
497         in += num_channels;
498         }
499       }
500     }
501   }
502 
decode_float_to_float(void * _in,float ** out,int num_channels,int num_samples)503 static void decode_float_to_float(void * _in, float ** out, int num_channels, int num_samples)
504   {
505   int i, j;
506   float * in;
507   for(i = 0; i < num_channels; i++)
508     {
509     if(out[i])
510       {
511       in = ((float*)_in) + i;
512       for(j = 0; j < num_samples; j++)
513         {
514         FLOAT_TO_FLOAT((*in), out[i][j]);
515         in += num_channels;
516         }
517       }
518     }
519   }
520 
decode_double_to_float(void * _in,float ** out,int num_channels,int num_samples)521 static void decode_double_to_float(void * _in, float ** out, int num_channels, int num_samples)
522   {
523   int i, j;
524   double * in;
525   for(i = 0; i < num_channels; i++)
526     {
527     if(out[i])
528       {
529       in = ((double*)_in) + i;
530       for(j = 0; j < num_samples; j++)
531         {
532         DOUBLE_TO_FLOAT((*in), out[i][j]);
533         in += num_channels;
534         }
535       }
536     }
537   }
538 
lqt_convert_audio_decode(quicktime_t * file,void * in,int16_t ** out_int,float ** out_float,int num_channels,int num_samples,lqt_sample_format_t stream_format)539 void lqt_convert_audio_decode(quicktime_t * file,
540                               void * in, int16_t ** out_int, float ** out_float,
541                               int num_channels, int num_samples,
542                               lqt_sample_format_t stream_format)
543   {
544   switch(stream_format)
545     {
546     case LQT_SAMPLE_INT8:
547       if(out_int)
548         decode_int8_to_int16(in, out_int, num_channels, num_samples);
549       if(out_float)
550         decode_int8_to_float(in, out_float, num_channels, num_samples);
551       break;
552     case LQT_SAMPLE_UINT8:
553       if(out_int)
554         decode_uint8_to_int16(in, out_int, num_channels, num_samples);
555       if(out_float)
556         decode_uint8_to_float(in, out_float, num_channels, num_samples);
557       break;
558     case LQT_SAMPLE_INT16:
559       if(out_int)
560         decode_int16_to_int16(in, out_int, num_channels, num_samples);
561       if(out_float)
562         decode_int16_to_float(in, out_float, num_channels, num_samples);
563       break;
564     case LQT_SAMPLE_INT32:
565       if(out_int)
566         decode_int32_to_int16(in, out_int, num_channels, num_samples);
567       if(out_float)
568         decode_int32_to_float(in, out_float, num_channels, num_samples);
569       break;
570     case LQT_SAMPLE_FLOAT: /* Float is ALWAYS machine native */
571       if(out_int)
572         decode_float_to_int16(in, out_int, num_channels, num_samples);
573       if(out_float)
574         decode_float_to_float(in, out_float, num_channels, num_samples);
575       break;
576     case LQT_SAMPLE_DOUBLE: /* Float is ALWAYS machine native */
577       if(out_int)
578         decode_double_to_int16(in, out_int, num_channels, num_samples);
579       if(out_float)
580         decode_double_to_float(in, out_float, num_channels, num_samples);
581       break;
582     case LQT_SAMPLE_UNDEFINED:
583       lqt_log(file, LQT_LOG_ERROR, LOG_DOMAIN, "Cannot decode samples: Stream format undefined");
584       break;
585     }
586   }
587