2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
6 * This is the actual decoder.
8 * http://crazney.net/programs/itunes/alac.html
10 * Permission is hereby granted, free of charge, to any person
11 * obtaining a copy of this software and associated documentation
12 * files (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use,
14 * copy, modify, merge, publish, distribute, sublicense, and/or
15 * sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
41 int16_t predictor_coef_table
[32] IBSS_ATTR
;
42 int16_t predictor_coef_table_a
[32] IBSS_ATTR
;
43 int16_t predictor_coef_table_b
[32] IBSS_ATTR
;
46 /* Endian/aligment safe functions - only used in alac_set_info() */
47 static uint32_t get_uint32be(unsigned char* p
)
49 return((p
[0]<<24) | (p
[1]<<16) | (p
[2]<<8) | p
[3]);
52 static uint16_t get_uint16be(unsigned char* p
)
54 return((p
[0]<<8) | p
[1]);
57 void alac_set_info(alac_file
*alac
, char *inputbuffer
)
59 unsigned char* ptr
= (unsigned char*)inputbuffer
;
68 alac
->setinfo_max_samples_per_frame
= get_uint32be(ptr
); /* buffer size / 2 ? */
70 alac
->setinfo_7a
= *ptr
++;
71 alac
->setinfo_sample_size
= *ptr
++;
72 alac
->setinfo_rice_historymult
= *ptr
++;
73 alac
->setinfo_rice_initialhistory
= *ptr
++;
74 alac
->setinfo_rice_kmodifier
= *ptr
++;
75 alac
->setinfo_7f
= *ptr
++;
77 alac
->setinfo_80
= get_uint16be(ptr
);
79 alac
->setinfo_82
= get_uint32be(ptr
);
81 alac
->setinfo_86
= get_uint32be(ptr
);
83 alac
->setinfo_8a_rate
= get_uint32be(ptr
);
89 /* supports reading 1 to 16 bits, in big endian format */
90 static inline uint32_t readbits_16(alac_file
*alac
, int bits
)
95 result
= (alac
->input_buffer
[0] << 16) |
96 (alac
->input_buffer
[1] << 8) |
97 (alac
->input_buffer
[2]);
99 /* shift left by the number of bits we've already read,
100 * so that the top 'n' bits of the 24 bits we read will
101 * be the return bits */
102 result
= result
<< alac
->input_buffer_bitaccumulator
;
104 result
= result
& 0x00ffffff;
106 /* and then only want the top 'n' bits from that, where
108 result
= result
>> (24 - bits
);
110 new_accumulator
= (alac
->input_buffer_bitaccumulator
+ bits
);
112 /* increase the buffer pointer if we've read over n bytes. */
113 alac
->input_buffer
+= (new_accumulator
>> 3);
115 /* and the remainder goes back into the bit accumulator */
116 alac
->input_buffer_bitaccumulator
= (new_accumulator
& 7);
121 /* supports reading 1 to 32 bits, in big endian format */
122 static inline uint32_t readbits(alac_file
*alac
, int bits
)
129 result
= readbits_16(alac
, 16) << bits
;
132 result
|= readbits_16(alac
, bits
);
137 /* reads a single bit */
138 static inline int readbit(alac_file
*alac
)
143 result
= alac
->input_buffer
[0];
145 result
= result
<< alac
->input_buffer_bitaccumulator
;
147 result
= result
>> 7 & 1;
149 new_accumulator
= (alac
->input_buffer_bitaccumulator
+ 1);
151 alac
->input_buffer
+= (new_accumulator
/ 8);
153 alac
->input_buffer_bitaccumulator
= (new_accumulator
% 8);
158 static inline void unreadbits(alac_file
*alac
, int bits
)
160 int new_accumulator
= (alac
->input_buffer_bitaccumulator
- bits
);
162 alac
->input_buffer
+= (new_accumulator
>> 3);
164 alac
->input_buffer_bitaccumulator
= (new_accumulator
& 7);
165 if (alac
->input_buffer_bitaccumulator
< 0)
166 alac
->input_buffer_bitaccumulator
*= -1;
169 /* ARMv5+ has a clz instruction equivalent to our function.
171 #if (defined(CPU_ARM) && (ARM_ARCH > 4))
172 static inline int count_leading_zeros(uint32_t v
)
174 return __builtin_clz(v
);
178 static const unsigned char bittab
[16] ICONST_ATTR
= {
179 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
182 static inline int count_leading_zeros(int input
)
187 /* Experimentation has shown that the following test is always false,
188 so we don't bother to perform it. */
189 if (input
& 0xffff0000)
205 output
-= bittab
[input
];
210 void basterdised_rice_decompress(alac_file
*alac
,
211 int32_t *output_buffer
,
213 int readsamplesize
, /* arg_10 */
214 int rice_initialhistory
, /* arg424->b */
215 int rice_kmodifier
, /* arg424->d */
216 int rice_historymult
, /* arg424->c */
217 int rice_kmodifier_mask
/* arg424->e */
219 void basterdised_rice_decompress(alac_file
*alac
,
220 int32_t *output_buffer
,
222 int readsamplesize
, /* arg_10 */
223 int rice_initialhistory
, /* arg424->b */
224 int rice_kmodifier
, /* arg424->d */
225 int rice_historymult
, /* arg424->c */
226 int rice_kmodifier_mask
/* arg424->e */
230 unsigned int history
= rice_initialhistory
;
231 int sign_modifier
= 0;
233 for (output_count
= 0; output_count
< output_size
; output_count
++)
239 /* read x - number of 1s before 0 represent the rice */
240 while (x
<= 8 && readbit(alac
))
246 if (x
> 8) /* RICE THRESHOLD */
247 { /* use alternative encoding */
250 value
= readbits(alac
, readsamplesize
);
252 /* mask value to readsamplesize size */
253 if (readsamplesize
!= 32)
254 value
&= (0xffffffff >> (32 - readsamplesize
));
259 { /* standard rice encoding */
261 int k
; /* size of extra bits */
263 /* read k, that is bits as is */
264 k
= 31 - rice_kmodifier
- count_leading_zeros((history
>> 9) + 3);
266 if (k
< 0) k
+= rice_kmodifier
;
267 else k
= rice_kmodifier
;
271 extrabits
= readbits(alac
, k
);
273 /* multiply x by 2^k - 1, as part of their strange algorithm */
280 else unreadbits(alac
, 1);
284 x_modified
= sign_modifier
+ x
;
285 final_val
= (x_modified
+ 1) / 2;
286 if (x_modified
& 1) final_val
*= -1;
288 output_buffer
[output_count
] = final_val
;
292 /* now update the history */
293 history
+= (x_modified
* rice_historymult
)
294 - ((history
* rice_historymult
) >> 9);
296 if (x_modified
> 0xffff)
299 /* special case: there may be compressed blocks of 0 */
300 if ((history
< 128) && (output_count
+1 < output_size
))
307 while (x
<= 8 && readbit(alac
))
314 block_size
= readbits(alac
, 16);
315 block_size
&= 0xffff;
322 k
= count_leading_zeros(history
) + ((history
+ 16) >> 6 /* / 64 */) - 24;
324 extrabits
= readbits(alac
, k
);
326 block_size
= (((1 << k
) - 1) & rice_kmodifier_mask
) * x
339 memset(&output_buffer
[output_count
+1], 0, block_size
* 4);
340 output_count
+= block_size
;
344 if (block_size
> 0xffff)
352 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
354 #define SIGN_ONLY(v) \
359 static void predictor_decompress_fir_adapt(int32_t *error_buffer
,
363 int16_t *predictor_coef_table
,
364 int predictor_coef_num
,
365 int predictor_quantitization
) ICODE_ATTR_ALAC
;
366 static void predictor_decompress_fir_adapt(int32_t *error_buffer
,
370 int16_t *predictor_coef_table
,
371 int predictor_coef_num
,
372 int predictor_quantitization
)
376 /* first sample always copies */
377 *buffer_out
= *error_buffer
;
379 if (!predictor_coef_num
)
381 if (output_size
<= 1) return;
382 memcpy(buffer_out
+1, error_buffer
+1, (output_size
-1) * 4);
386 if (predictor_coef_num
== 0x1f) /* 11111 - max value of predictor_coef_num */
387 { /* second-best case scenario for fir decompression,
388 * error describes a small difference from the previous sample only
390 if (output_size
<= 1) return;
391 for (i
= 0; i
< output_size
- 1; i
++)
396 prev_value
= buffer_out
[i
];
397 error_value
= error_buffer
[i
+1];
398 buffer_out
[i
+1] = SIGN_EXTENDED32((prev_value
+ error_value
), readsamplesize
);
403 /* read warm-up samples */
404 if (predictor_coef_num
> 0)
407 for (i
= 0; i
< predictor_coef_num
; i
++)
411 val
= buffer_out
[i
] + error_buffer
[i
+1];
413 val
= SIGN_EXTENDED32(val
, readsamplesize
);
415 buffer_out
[i
+1] = val
;
419 /* 4 and 8 are very common cases (the only ones i've seen).
421 The following code is an initial attempt to unroll and optimise
422 these two cases by the Rockbox project. More work is needed.
425 /* optimised case: 4 */
426 if (predictor_coef_num
== 4)
428 for (i
= 4 + 1; i
< output_size
; i
++)
432 int error_val
= error_buffer
[i
];
434 sum
= (buffer_out
[4] - buffer_out
[0]) * predictor_coef_table
[0]
435 + (buffer_out
[3] - buffer_out
[0]) * predictor_coef_table
[1]
436 + (buffer_out
[2] - buffer_out
[0]) * predictor_coef_table
[2]
437 + (buffer_out
[1] - buffer_out
[0]) * predictor_coef_table
[3];
439 outval
= (1 << (predictor_quantitization
-1)) + sum
;
440 outval
= outval
>> predictor_quantitization
;
441 outval
= outval
+ buffer_out
[0] + error_val
;
442 outval
= SIGN_EXTENDED32(outval
, readsamplesize
);
444 buffer_out
[4+1] = outval
;
448 int predictor_num
= 4 - 1;
450 while (predictor_num
>= 0 && error_val
> 0)
452 int val
= buffer_out
[0] - buffer_out
[4 - predictor_num
];
456 predictor_coef_table
[predictor_num
]++;
459 predictor_coef_table
[predictor_num
]--;
461 error_val
-= ((val
>> predictor_quantitization
) * (4 - predictor_num
));
466 else if (error_val
< 0)
468 int predictor_num
= 4 - 1;
470 while (predictor_num
>= 0 && error_val
< 0)
472 int val
= buffer_out
[0] - buffer_out
[4 - predictor_num
];
476 predictor_coef_table
[predictor_num
]++;
477 val
=-val
; /* neg value */
479 predictor_coef_table
[predictor_num
]--;
481 error_val
-= ((val
>> predictor_quantitization
) * (4 - predictor_num
));
492 /* optimised case: 8 */
493 if (predictor_coef_num
== 8)
501 int error_val
= error_buffer
[i
];
503 sum
= (buffer_out
[8] - buffer_out
[0]) * predictor_coef_table
[0]
504 + (buffer_out
[7] - buffer_out
[0]) * predictor_coef_table
[1]
505 + (buffer_out
[6] - buffer_out
[0]) * predictor_coef_table
[2]
506 + (buffer_out
[5] - buffer_out
[0]) * predictor_coef_table
[3]
507 + (buffer_out
[4] - buffer_out
[0]) * predictor_coef_table
[4]
508 + (buffer_out
[3] - buffer_out
[0]) * predictor_coef_table
[5]
509 + (buffer_out
[2] - buffer_out
[0]) * predictor_coef_table
[6]
510 + (buffer_out
[1] - buffer_out
[0]) * predictor_coef_table
[7];
512 outval
= (1 << (predictor_quantitization
-1)) + sum
;
513 outval
= outval
>> predictor_quantitization
;
514 outval
= outval
+ buffer_out
[0] + error_val
;
515 outval
= SIGN_EXTENDED32(outval
, readsamplesize
);
517 buffer_out
[8+1] = outval
;
521 int predictor_num
= 8 - 1;
523 while (predictor_num
>= 0 && error_val
> 0)
525 int val
= buffer_out
[0] - buffer_out
[8 - predictor_num
];
529 predictor_coef_table
[predictor_num
]++;
532 predictor_coef_table
[predictor_num
]--;
534 error_val
-= ((val
>> predictor_quantitization
) * (8 - predictor_num
));
539 else if (error_val
< 0)
541 int predictor_num
= 8 - 1;
543 while (predictor_num
>= 0 && error_val
< 0)
545 int val
= buffer_out
[0] - buffer_out
[8 - predictor_num
];
548 predictor_coef_table
[predictor_num
]++;
549 val
=-val
; /* neg value */
551 predictor_coef_table
[predictor_num
]--;
553 error_val
-= ((val
>> predictor_quantitization
) * (8 - predictor_num
));
565 if (predictor_coef_num
> 0)
567 for (i
= predictor_coef_num
+ 1;
574 int error_val
= error_buffer
[i
];
576 for (j
= 0; j
< predictor_coef_num
; j
++)
578 sum
+= (buffer_out
[predictor_coef_num
-j
] - buffer_out
[0]) *
579 predictor_coef_table
[j
];
582 outval
= (1 << (predictor_quantitization
-1)) + sum
;
583 outval
= outval
>> predictor_quantitization
;
584 outval
= outval
+ buffer_out
[0] + error_val
;
585 outval
= SIGN_EXTENDED32(outval
, readsamplesize
);
587 buffer_out
[predictor_coef_num
+1] = outval
;
591 int predictor_num
= predictor_coef_num
- 1;
593 while (predictor_num
>= 0 && error_val
> 0)
595 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
596 int sign
= SIGN_ONLY(val
);
598 predictor_coef_table
[predictor_num
] -= sign
;
600 val
*= sign
; /* absolute value */
602 error_val
-= ((val
>> predictor_quantitization
) *
603 (predictor_coef_num
- predictor_num
));
608 else if (error_val
< 0)
610 int predictor_num
= predictor_coef_num
- 1;
612 while (predictor_num
>= 0 && error_val
< 0)
614 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
615 int sign
= - SIGN_ONLY(val
);
617 predictor_coef_table
[predictor_num
] -= sign
;
619 val
*= sign
; /* neg value */
621 error_val
-= ((val
>> predictor_quantitization
) *
622 (predictor_coef_num
- predictor_num
));
633 void deinterlace_16(int32_t* buffer0
,
636 uint8_t interlacing_shift
,
637 uint8_t interlacing_leftweight
) ICODE_ATTR_ALAC
;
638 void deinterlace_16(int32_t* buffer0
,
641 uint8_t interlacing_shift
,
642 uint8_t interlacing_leftweight
)
645 if (numsamples
<= 0) return;
647 /* weighted interlacing */
648 if (interlacing_leftweight
)
650 for (i
= 0; i
< numsamples
; i
++)
652 int32_t difference
, midright
;
654 midright
= buffer0
[i
];
655 difference
= buffer1
[i
];
657 buffer0
[i
] = ((midright
- ((difference
* interlacing_leftweight
)
658 >> interlacing_shift
)) + difference
) << SCALE16
;
659 buffer1
[i
] = (midright
- ((difference
* interlacing_leftweight
)
660 >> interlacing_shift
)) << SCALE16
;
666 /* otherwise basic interlacing took place */
667 for (i
= 0; i
< numsamples
; i
++)
669 buffer0
[i
] = buffer0
[i
] << SCALE16
;
670 buffer1
[i
] = buffer1
[i
] << SCALE16
;
675 static inline int decode_frame_mono(
677 int32_t outputbuffer
[ALAC_MAX_CHANNELS
][ALAC_BLOCKSIZE
],
683 int outputsamples
= alac
->setinfo_max_samples_per_frame
;
689 /* 2^result = something to do with output waiting.
690 * perhaps matters if we read > 1 frame in a pass?
694 readbits(alac
, 12); /* unknown, skip 12 bits */
696 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
698 wasted_bytes
= readbits(alac
, 2); /* unknown ? */
700 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
704 /* now read the number of samples,
705 * as a 32bit integer */
706 outputsamples
= readbits(alac
, 32);
709 readsamplesize
= alac
->setinfo_sample_size
- (wasted_bytes
* 8);
711 if (!isnotcompressed
)
712 { /* so it is compressed */
713 int predictor_coef_num
;
715 int prediction_quantitization
;
718 /* skip 16 bits, not sure what they are. seem to be used in
719 * two channel case */
723 prediction_type
= readbits(alac
, 4);
724 prediction_quantitization
= readbits(alac
, 4);
726 ricemodifier
= readbits(alac
, 3);
727 predictor_coef_num
= readbits(alac
, 5);
729 /* read the predictor table */
730 for (i
= 0; i
< predictor_coef_num
; i
++)
732 predictor_coef_table
[i
] = (int16_t)readbits(alac
, 16);
737 /* these bytes seem to have something to do with
740 //fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n");
745 basterdised_rice_decompress(alac
,
749 alac
->setinfo_rice_initialhistory
,
750 alac
->setinfo_rice_kmodifier
,
751 ricemodifier
* alac
->setinfo_rice_historymult
/ 4,
752 (1 << alac
->setinfo_rice_kmodifier
) - 1);
756 if (prediction_type
== 0)
758 predictor_decompress_fir_adapt(outputbuffer
[0],
762 predictor_coef_table
,
764 prediction_quantitization
);
768 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type);
769 /* i think the only other prediction type (or perhaps this is just a
770 * boolean?) runs adaptive fir twice.. like:
771 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
772 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
779 { /* not compressed, easy case */
780 if (readsamplesize
<= 16)
783 for (i
= 0; i
< outputsamples
; i
++)
785 int32_t audiobits
= readbits(alac
, readsamplesize
);
787 audiobits
= SIGN_EXTENDED32(audiobits
, readsamplesize
);
789 outputbuffer
[0][i
] = audiobits
;
795 for (i
= 0; i
< outputsamples
; i
++)
799 audiobits
= readbits(alac
, 16);
800 /* special case of sign extension..
801 * as we'll be ORing the low 16bits into this */
802 audiobits
= audiobits
<< 16;
803 audiobits
= audiobits
>> (32 - readsamplesize
);
805 audiobits
|= readbits(alac
, readsamplesize
- 16);
807 outputbuffer
[0][i
] = audiobits
;
810 /* wasted_bytes = 0; // unused */
815 switch(alac
->setinfo_sample_size
)
820 for (i
= 0; i
< outputsamples
; i
++)
822 /* Output mono data as stereo */
823 outputbuffer
[0][i
] = outputbuffer
[0][i
] << SCALE16
;
824 outputbuffer
[1][i
] = outputbuffer
[0][i
];
831 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
837 return outputsamples
;
840 static inline int decode_frame_stereo(
842 int32_t outputbuffer
[ALAC_MAX_CHANNELS
][ALAC_BLOCKSIZE
],
848 int outputsamples
= alac
->setinfo_max_samples_per_frame
;
851 uint8_t interlacing_shift
;
852 uint8_t interlacing_leftweight
;
854 /* 2^result = something to do with output waiting.
855 * perhaps matters if we read > 1 frame in a pass?
859 readbits(alac
, 12); /* unknown, skip 12 bits */
861 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
863 wasted_bytes
= readbits(alac
, 2); /* unknown ? */
865 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
869 /* now read the number of samples,
870 * as a 32bit integer */
871 outputsamples
= readbits(alac
, 32);
874 readsamplesize
= alac
->setinfo_sample_size
- (wasted_bytes
* 8) + 1;
877 if (!isnotcompressed
)
879 int predictor_coef_num_a
;
880 int prediction_type_a
;
881 int prediction_quantitization_a
;
884 int predictor_coef_num_b
;
885 int prediction_type_b
;
886 int prediction_quantitization_b
;
891 interlacing_shift
= readbits(alac
, 8);
892 interlacing_leftweight
= readbits(alac
, 8);
894 /******** channel 1 ***********/
895 prediction_type_a
= readbits(alac
, 4);
896 prediction_quantitization_a
= readbits(alac
, 4);
898 ricemodifier_a
= readbits(alac
, 3);
899 predictor_coef_num_a
= readbits(alac
, 5);
901 /* read the predictor table */
902 for (i
= 0; i
< predictor_coef_num_a
; i
++)
904 predictor_coef_table_a
[i
] = (int16_t)readbits(alac
, 16);
907 /******** channel 2 *********/
908 prediction_type_b
= readbits(alac
, 4);
909 prediction_quantitization_b
= readbits(alac
, 4);
911 ricemodifier_b
= readbits(alac
, 3);
912 predictor_coef_num_b
= readbits(alac
, 5);
914 /* read the predictor table */
915 for (i
= 0; i
< predictor_coef_num_b
; i
++)
917 predictor_coef_table_b
[i
] = (int16_t)readbits(alac
, 16);
920 /*********************/
922 { /* see mono case */
923 //fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n");
928 basterdised_rice_decompress(alac
,
932 alac
->setinfo_rice_initialhistory
,
933 alac
->setinfo_rice_kmodifier
,
934 ricemodifier_a
* alac
->setinfo_rice_historymult
/ 4,
935 (1 << alac
->setinfo_rice_kmodifier
) - 1);
938 if (prediction_type_a
== 0)
940 predictor_decompress_fir_adapt(outputbuffer
[0],
944 predictor_coef_table_a
,
945 predictor_coef_num_a
,
946 prediction_quantitization_a
);
949 { /* see mono case */
950 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_a);
956 basterdised_rice_decompress(alac
,
960 alac
->setinfo_rice_initialhistory
,
961 alac
->setinfo_rice_kmodifier
,
962 ricemodifier_b
* alac
->setinfo_rice_historymult
/ 4,
963 (1 << alac
->setinfo_rice_kmodifier
) - 1);
966 if (prediction_type_b
== 0)
968 predictor_decompress_fir_adapt(outputbuffer
[1],
972 predictor_coef_table_b
,
973 predictor_coef_num_b
,
974 prediction_quantitization_b
);
978 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_b);
982 { /* not compressed, easy case */
983 if (alac
->setinfo_sample_size
<= 16)
986 for (i
= 0; i
< outputsamples
; i
++)
988 int32_t audiobits_a
, audiobits_b
;
990 audiobits_a
= readbits(alac
, alac
->setinfo_sample_size
);
991 audiobits_b
= readbits(alac
, alac
->setinfo_sample_size
);
993 audiobits_a
= SIGN_EXTENDED32(audiobits_a
, alac
->setinfo_sample_size
);
994 audiobits_b
= SIGN_EXTENDED32(audiobits_b
, alac
->setinfo_sample_size
);
996 outputbuffer
[0][i
] = audiobits_a
;
997 outputbuffer
[1][i
] = audiobits_b
;
1003 for (i
= 0; i
< outputsamples
; i
++)
1005 int32_t audiobits_a
, audiobits_b
;
1007 audiobits_a
= readbits(alac
, 16);
1008 audiobits_a
= audiobits_a
<< 16;
1009 audiobits_a
= audiobits_a
>> (32 - alac
->setinfo_sample_size
);
1010 audiobits_a
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
1012 audiobits_b
= readbits(alac
, 16);
1013 audiobits_b
= audiobits_b
<< 16;
1014 audiobits_b
= audiobits_b
>> (32 - alac
->setinfo_sample_size
);
1015 audiobits_b
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
1017 outputbuffer
[0][i
] = audiobits_a
;
1018 outputbuffer
[1][i
] = audiobits_b
;
1021 /* wasted_bytes = 0; */
1022 interlacing_shift
= 0;
1023 interlacing_leftweight
= 0;
1028 switch(alac
->setinfo_sample_size
)
1032 deinterlace_16(outputbuffer
[0],
1036 interlacing_leftweight
);
1042 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
1047 return outputsamples
;
1050 int alac_decode_frame(alac_file
*alac
,
1051 unsigned char *inbuffer
,
1052 int32_t outputbuffer
[ALAC_MAX_CHANNELS
][ALAC_BLOCKSIZE
],
1053 void (*yield
)(void))
1058 /* setup the stream */
1059 alac
->input_buffer
= inbuffer
;
1060 alac
->input_buffer_bitaccumulator
= 0;
1062 channels
= readbits(alac
, 3);
1064 /* TODO: The mono and stereo functions should be combined. */
1067 case 0: /* 1 channel */
1068 outputsamples
=decode_frame_mono(alac
,outputbuffer
,yield
);
1070 case 1: /* 2 channels */
1071 outputsamples
=decode_frame_stereo(alac
,outputbuffer
,yield
);
1073 default: /* Unsupported */
1076 return outputsamples
;
1079 void create_alac(int samplesize
, int numchannels
, alac_file
* alac
)
1081 alac
->samplesize
= samplesize
;
1082 alac
->numchannels
= numchannels
;
1083 alac
->bytespersample
= (samplesize
/ 8) * numchannels
;