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 static const unsigned char bittab
[16] ICONST_ATTR
= {
170 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
173 static inline int count_leading_zeros(int input
)
178 /* Experimentation has shown that the following test is always false,
179 so we don't bother to perform it. */
180 if (input
& 0xffff0000)
196 output
-= bittab
[input
];
203 void basterdised_rice_decompress(alac_file
*alac
,
204 int32_t *output_buffer
,
206 int readsamplesize
, /* arg_10 */
207 int rice_initialhistory
, /* arg424->b */
208 int rice_kmodifier
, /* arg424->d */
209 int rice_historymult
, /* arg424->c */
210 int rice_kmodifier_mask
/* arg424->e */
212 void basterdised_rice_decompress(alac_file
*alac
,
213 int32_t *output_buffer
,
215 int readsamplesize
, /* arg_10 */
216 int rice_initialhistory
, /* arg424->b */
217 int rice_kmodifier
, /* arg424->d */
218 int rice_historymult
, /* arg424->c */
219 int rice_kmodifier_mask
/* arg424->e */
223 unsigned int history
= rice_initialhistory
;
224 int sign_modifier
= 0;
226 for (output_count
= 0; output_count
< output_size
; output_count
++)
232 /* read x - number of 1s before 0 represent the rice */
233 while (x
<= 8 && readbit(alac
))
239 if (x
> 8) /* RICE THRESHOLD */
240 { /* use alternative encoding */
243 value
= readbits(alac
, readsamplesize
);
245 /* mask value to readsamplesize size */
246 if (readsamplesize
!= 32)
247 value
&= (0xffffffff >> (32 - readsamplesize
));
252 { /* standard rice encoding */
254 int k
; /* size of extra bits */
256 /* read k, that is bits as is */
257 k
= 31 - rice_kmodifier
- count_leading_zeros((history
>> 9) + 3);
259 if (k
< 0) k
+= rice_kmodifier
;
260 else k
= rice_kmodifier
;
264 extrabits
= readbits(alac
, k
);
266 /* multiply x by 2^k - 1, as part of their strange algorithm */
273 else unreadbits(alac
, 1);
277 x_modified
= sign_modifier
+ x
;
278 final_val
= (x_modified
+ 1) / 2;
279 if (x_modified
& 1) final_val
*= -1;
281 output_buffer
[output_count
] = final_val
;
285 /* now update the history */
286 history
+= (x_modified
* rice_historymult
)
287 - ((history
* rice_historymult
) >> 9);
289 if (x_modified
> 0xffff)
292 /* special case: there may be compressed blocks of 0 */
293 if ((history
< 128) && (output_count
+1 < output_size
))
300 while (x
<= 8 && readbit(alac
))
307 block_size
= readbits(alac
, 16);
308 block_size
&= 0xffff;
315 k
= count_leading_zeros(history
) + ((history
+ 16) >> 6 /* / 64 */) - 24;
317 extrabits
= readbits(alac
, k
);
319 block_size
= (((1 << k
) - 1) & rice_kmodifier_mask
) * x
332 memset(&output_buffer
[output_count
+1], 0, block_size
* 4);
333 output_count
+= block_size
;
337 if (block_size
> 0xffff)
345 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
347 #define SIGN_ONLY(v) \
352 static void predictor_decompress_fir_adapt(int32_t *error_buffer
,
356 int16_t *predictor_coef_table
,
357 int predictor_coef_num
,
358 int predictor_quantitization
) ICODE_ATTR_ALAC
;
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
)
369 /* first sample always copies */
370 *buffer_out
= *error_buffer
;
372 if (!predictor_coef_num
)
374 if (output_size
<= 1) return;
375 memcpy(buffer_out
+1, error_buffer
+1, (output_size
-1) * 4);
379 if (predictor_coef_num
== 0x1f) /* 11111 - max value of predictor_coef_num */
380 { /* second-best case scenario for fir decompression,
381 * error describes a small difference from the previous sample only
383 if (output_size
<= 1) return;
384 for (i
= 0; i
< output_size
- 1; i
++)
389 prev_value
= buffer_out
[i
];
390 error_value
= error_buffer
[i
+1];
391 buffer_out
[i
+1] = SIGN_EXTENDED32((prev_value
+ error_value
), readsamplesize
);
396 /* read warm-up samples */
397 if (predictor_coef_num
> 0)
400 for (i
= 0; i
< predictor_coef_num
; i
++)
404 val
= buffer_out
[i
] + error_buffer
[i
+1];
406 val
= SIGN_EXTENDED32(val
, readsamplesize
);
408 buffer_out
[i
+1] = val
;
412 /* 4 and 8 are very common cases (the only ones i've seen).
414 The following code is an initial attempt to unroll and optimise
415 these two cases by the Rockbox project. More work is needed.
418 /* optimised case: 4 */
419 if (predictor_coef_num
== 4)
421 for (i
= 4 + 1; i
< output_size
; i
++)
425 int error_val
= error_buffer
[i
];
427 sum
= (buffer_out
[4] - buffer_out
[0]) * predictor_coef_table
[0]
428 + (buffer_out
[3] - buffer_out
[0]) * predictor_coef_table
[1]
429 + (buffer_out
[2] - buffer_out
[0]) * predictor_coef_table
[2]
430 + (buffer_out
[1] - buffer_out
[0]) * predictor_coef_table
[3];
432 outval
= (1 << (predictor_quantitization
-1)) + sum
;
433 outval
= outval
>> predictor_quantitization
;
434 outval
= outval
+ buffer_out
[0] + error_val
;
435 outval
= SIGN_EXTENDED32(outval
, readsamplesize
);
437 buffer_out
[4+1] = outval
;
441 int predictor_num
= 4 - 1;
443 while (predictor_num
>= 0 && error_val
> 0)
445 int val
= buffer_out
[0] - buffer_out
[4 - predictor_num
];
449 predictor_coef_table
[predictor_num
]++;
452 predictor_coef_table
[predictor_num
]--;
454 error_val
-= ((val
>> predictor_quantitization
) * (4 - predictor_num
));
459 else if (error_val
< 0)
461 int predictor_num
= 4 - 1;
463 while (predictor_num
>= 0 && error_val
< 0)
465 int val
= buffer_out
[0] - buffer_out
[4 - predictor_num
];
469 predictor_coef_table
[predictor_num
]++;
470 val
=-val
; /* neg value */
472 predictor_coef_table
[predictor_num
]--;
474 error_val
-= ((val
>> predictor_quantitization
) * (4 - predictor_num
));
485 /* optimised case: 8 */
486 if (predictor_coef_num
== 8)
494 int error_val
= error_buffer
[i
];
496 sum
= (buffer_out
[8] - buffer_out
[0]) * predictor_coef_table
[0]
497 + (buffer_out
[7] - buffer_out
[0]) * predictor_coef_table
[1]
498 + (buffer_out
[6] - buffer_out
[0]) * predictor_coef_table
[2]
499 + (buffer_out
[5] - buffer_out
[0]) * predictor_coef_table
[3]
500 + (buffer_out
[4] - buffer_out
[0]) * predictor_coef_table
[4]
501 + (buffer_out
[3] - buffer_out
[0]) * predictor_coef_table
[5]
502 + (buffer_out
[2] - buffer_out
[0]) * predictor_coef_table
[6]
503 + (buffer_out
[1] - buffer_out
[0]) * predictor_coef_table
[7];
505 outval
= (1 << (predictor_quantitization
-1)) + sum
;
506 outval
= outval
>> predictor_quantitization
;
507 outval
= outval
+ buffer_out
[0] + error_val
;
508 outval
= SIGN_EXTENDED32(outval
, readsamplesize
);
510 buffer_out
[8+1] = outval
;
514 int predictor_num
= 8 - 1;
516 while (predictor_num
>= 0 && error_val
> 0)
518 int val
= buffer_out
[0] - buffer_out
[8 - predictor_num
];
522 predictor_coef_table
[predictor_num
]++;
525 predictor_coef_table
[predictor_num
]--;
527 error_val
-= ((val
>> predictor_quantitization
) * (8 - predictor_num
));
532 else if (error_val
< 0)
534 int predictor_num
= 8 - 1;
536 while (predictor_num
>= 0 && error_val
< 0)
538 int val
= buffer_out
[0] - buffer_out
[8 - predictor_num
];
541 predictor_coef_table
[predictor_num
]++;
542 val
=-val
; /* neg value */
544 predictor_coef_table
[predictor_num
]--;
546 error_val
-= ((val
>> predictor_quantitization
) * (8 - predictor_num
));
558 if (predictor_coef_num
> 0)
560 for (i
= predictor_coef_num
+ 1;
567 int error_val
= error_buffer
[i
];
569 for (j
= 0; j
< predictor_coef_num
; j
++)
571 sum
+= (buffer_out
[predictor_coef_num
-j
] - buffer_out
[0]) *
572 predictor_coef_table
[j
];
575 outval
= (1 << (predictor_quantitization
-1)) + sum
;
576 outval
= outval
>> predictor_quantitization
;
577 outval
= outval
+ buffer_out
[0] + error_val
;
578 outval
= SIGN_EXTENDED32(outval
, readsamplesize
);
580 buffer_out
[predictor_coef_num
+1] = outval
;
584 int predictor_num
= predictor_coef_num
- 1;
586 while (predictor_num
>= 0 && error_val
> 0)
588 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
589 int sign
= SIGN_ONLY(val
);
591 predictor_coef_table
[predictor_num
] -= sign
;
593 val
*= sign
; /* absolute value */
595 error_val
-= ((val
>> predictor_quantitization
) *
596 (predictor_coef_num
- predictor_num
));
601 else if (error_val
< 0)
603 int predictor_num
= predictor_coef_num
- 1;
605 while (predictor_num
>= 0 && error_val
< 0)
607 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
608 int sign
= - SIGN_ONLY(val
);
610 predictor_coef_table
[predictor_num
] -= sign
;
612 val
*= sign
; /* neg value */
614 error_val
-= ((val
>> predictor_quantitization
) *
615 (predictor_coef_num
- predictor_num
));
626 void deinterlace_16(int32_t* buffer0
,
629 uint8_t interlacing_shift
,
630 uint8_t interlacing_leftweight
) ICODE_ATTR_ALAC
;
631 void deinterlace_16(int32_t* buffer0
,
634 uint8_t interlacing_shift
,
635 uint8_t interlacing_leftweight
)
638 if (numsamples
<= 0) return;
640 /* weighted interlacing */
641 if (interlacing_leftweight
)
643 for (i
= 0; i
< numsamples
; i
++)
645 int32_t difference
, midright
;
647 midright
= buffer0
[i
];
648 difference
= buffer1
[i
];
650 buffer0
[i
] = ((midright
- ((difference
* interlacing_leftweight
)
651 >> interlacing_shift
)) + difference
) << SCALE16
;
652 buffer1
[i
] = (midright
- ((difference
* interlacing_leftweight
)
653 >> interlacing_shift
)) << SCALE16
;
659 /* otherwise basic interlacing took place */
660 for (i
= 0; i
< numsamples
; i
++)
662 buffer0
[i
] = buffer0
[i
] << SCALE16
;
663 buffer1
[i
] = buffer1
[i
] << SCALE16
;
668 static inline int decode_frame_mono(
670 int32_t outputbuffer
[ALAC_MAX_CHANNELS
][ALAC_BLOCKSIZE
],
676 int outputsamples
= alac
->setinfo_max_samples_per_frame
;
682 /* 2^result = something to do with output waiting.
683 * perhaps matters if we read > 1 frame in a pass?
687 readbits(alac
, 12); /* unknown, skip 12 bits */
689 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
691 wasted_bytes
= readbits(alac
, 2); /* unknown ? */
693 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
697 /* now read the number of samples,
698 * as a 32bit integer */
699 outputsamples
= readbits(alac
, 32);
702 readsamplesize
= alac
->setinfo_sample_size
- (wasted_bytes
* 8);
704 if (!isnotcompressed
)
705 { /* so it is compressed */
706 int predictor_coef_num
;
708 int prediction_quantitization
;
711 /* skip 16 bits, not sure what they are. seem to be used in
712 * two channel case */
716 prediction_type
= readbits(alac
, 4);
717 prediction_quantitization
= readbits(alac
, 4);
719 ricemodifier
= readbits(alac
, 3);
720 predictor_coef_num
= readbits(alac
, 5);
722 /* read the predictor table */
723 for (i
= 0; i
< predictor_coef_num
; i
++)
725 predictor_coef_table
[i
] = (int16_t)readbits(alac
, 16);
730 /* these bytes seem to have something to do with
733 //fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n");
738 basterdised_rice_decompress(alac
,
742 alac
->setinfo_rice_initialhistory
,
743 alac
->setinfo_rice_kmodifier
,
744 ricemodifier
* alac
->setinfo_rice_historymult
/ 4,
745 (1 << alac
->setinfo_rice_kmodifier
) - 1);
749 if (prediction_type
== 0)
751 predictor_decompress_fir_adapt(outputbuffer
[0],
755 predictor_coef_table
,
757 prediction_quantitization
);
761 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type);
762 /* i think the only other prediction type (or perhaps this is just a
763 * boolean?) runs adaptive fir twice.. like:
764 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
765 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
772 { /* not compressed, easy case */
773 if (readsamplesize
<= 16)
776 for (i
= 0; i
< outputsamples
; i
++)
778 int32_t audiobits
= readbits(alac
, readsamplesize
);
780 audiobits
= SIGN_EXTENDED32(audiobits
, readsamplesize
);
782 outputbuffer
[0][i
] = audiobits
;
788 for (i
= 0; i
< outputsamples
; i
++)
792 audiobits
= readbits(alac
, 16);
793 /* special case of sign extension..
794 * as we'll be ORing the low 16bits into this */
795 audiobits
= audiobits
<< 16;
796 audiobits
= audiobits
>> (32 - readsamplesize
);
798 audiobits
|= readbits(alac
, readsamplesize
- 16);
800 outputbuffer
[0][i
] = audiobits
;
803 /* wasted_bytes = 0; // unused */
808 switch(alac
->setinfo_sample_size
)
813 for (i
= 0; i
< outputsamples
; i
++)
815 /* Output mono data as stereo */
816 outputbuffer
[0][i
] = outputbuffer
[0][i
] << SCALE16
;
817 outputbuffer
[1][i
] = outputbuffer
[0][i
];
824 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
830 return outputsamples
;
833 static inline int decode_frame_stereo(
835 int32_t outputbuffer
[ALAC_MAX_CHANNELS
][ALAC_BLOCKSIZE
],
841 int outputsamples
= alac
->setinfo_max_samples_per_frame
;
844 uint8_t interlacing_shift
;
845 uint8_t interlacing_leftweight
;
847 /* 2^result = something to do with output waiting.
848 * perhaps matters if we read > 1 frame in a pass?
852 readbits(alac
, 12); /* unknown, skip 12 bits */
854 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
856 wasted_bytes
= readbits(alac
, 2); /* unknown ? */
858 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
862 /* now read the number of samples,
863 * as a 32bit integer */
864 outputsamples
= readbits(alac
, 32);
867 readsamplesize
= alac
->setinfo_sample_size
- (wasted_bytes
* 8) + 1;
870 if (!isnotcompressed
)
872 int predictor_coef_num_a
;
873 int prediction_type_a
;
874 int prediction_quantitization_a
;
877 int predictor_coef_num_b
;
878 int prediction_type_b
;
879 int prediction_quantitization_b
;
884 interlacing_shift
= readbits(alac
, 8);
885 interlacing_leftweight
= readbits(alac
, 8);
887 /******** channel 1 ***********/
888 prediction_type_a
= readbits(alac
, 4);
889 prediction_quantitization_a
= readbits(alac
, 4);
891 ricemodifier_a
= readbits(alac
, 3);
892 predictor_coef_num_a
= readbits(alac
, 5);
894 /* read the predictor table */
895 for (i
= 0; i
< predictor_coef_num_a
; i
++)
897 predictor_coef_table_a
[i
] = (int16_t)readbits(alac
, 16);
900 /******** channel 2 *********/
901 prediction_type_b
= readbits(alac
, 4);
902 prediction_quantitization_b
= readbits(alac
, 4);
904 ricemodifier_b
= readbits(alac
, 3);
905 predictor_coef_num_b
= readbits(alac
, 5);
907 /* read the predictor table */
908 for (i
= 0; i
< predictor_coef_num_b
; i
++)
910 predictor_coef_table_b
[i
] = (int16_t)readbits(alac
, 16);
913 /*********************/
915 { /* see mono case */
916 //fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n");
921 basterdised_rice_decompress(alac
,
925 alac
->setinfo_rice_initialhistory
,
926 alac
->setinfo_rice_kmodifier
,
927 ricemodifier_a
* alac
->setinfo_rice_historymult
/ 4,
928 (1 << alac
->setinfo_rice_kmodifier
) - 1);
931 if (prediction_type_a
== 0)
933 predictor_decompress_fir_adapt(outputbuffer
[0],
937 predictor_coef_table_a
,
938 predictor_coef_num_a
,
939 prediction_quantitization_a
);
942 { /* see mono case */
943 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_a);
949 basterdised_rice_decompress(alac
,
953 alac
->setinfo_rice_initialhistory
,
954 alac
->setinfo_rice_kmodifier
,
955 ricemodifier_b
* alac
->setinfo_rice_historymult
/ 4,
956 (1 << alac
->setinfo_rice_kmodifier
) - 1);
959 if (prediction_type_b
== 0)
961 predictor_decompress_fir_adapt(outputbuffer
[1],
965 predictor_coef_table_b
,
966 predictor_coef_num_b
,
967 prediction_quantitization_b
);
971 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_b);
975 { /* not compressed, easy case */
976 if (alac
->setinfo_sample_size
<= 16)
979 for (i
= 0; i
< outputsamples
; i
++)
981 int32_t audiobits_a
, audiobits_b
;
983 audiobits_a
= readbits(alac
, alac
->setinfo_sample_size
);
984 audiobits_b
= readbits(alac
, alac
->setinfo_sample_size
);
986 audiobits_a
= SIGN_EXTENDED32(audiobits_a
, alac
->setinfo_sample_size
);
987 audiobits_b
= SIGN_EXTENDED32(audiobits_b
, alac
->setinfo_sample_size
);
989 outputbuffer
[0][i
] = audiobits_a
;
990 outputbuffer
[1][i
] = audiobits_b
;
996 for (i
= 0; i
< outputsamples
; i
++)
998 int32_t audiobits_a
, audiobits_b
;
1000 audiobits_a
= readbits(alac
, 16);
1001 audiobits_a
= audiobits_a
<< 16;
1002 audiobits_a
= audiobits_a
>> (32 - alac
->setinfo_sample_size
);
1003 audiobits_a
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
1005 audiobits_b
= readbits(alac
, 16);
1006 audiobits_b
= audiobits_b
<< 16;
1007 audiobits_b
= audiobits_b
>> (32 - alac
->setinfo_sample_size
);
1008 audiobits_b
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
1010 outputbuffer
[0][i
] = audiobits_a
;
1011 outputbuffer
[1][i
] = audiobits_b
;
1014 /* wasted_bytes = 0; */
1015 interlacing_shift
= 0;
1016 interlacing_leftweight
= 0;
1021 switch(alac
->setinfo_sample_size
)
1025 deinterlace_16(outputbuffer
[0],
1029 interlacing_leftweight
);
1035 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
1040 return outputsamples
;
1043 int alac_decode_frame(alac_file
*alac
,
1044 unsigned char *inbuffer
,
1045 int32_t outputbuffer
[ALAC_MAX_CHANNELS
][ALAC_BLOCKSIZE
],
1046 void (*yield
)(void))
1051 /* setup the stream */
1052 alac
->input_buffer
= inbuffer
;
1053 alac
->input_buffer_bitaccumulator
= 0;
1055 channels
= readbits(alac
, 3);
1057 /* TODO: The mono and stereo functions should be combined. */
1060 case 0: /* 1 channel */
1061 outputsamples
=decode_frame_mono(alac
,outputbuffer
,yield
);
1063 case 1: /* 2 channels */
1064 outputsamples
=decode_frame_stereo(alac
,outputbuffer
,yield
);
1066 default: /* Unsupported */
1069 return outputsamples
;
1072 void create_alac(int samplesize
, int numchannels
, alac_file
* alac
)
1074 alac
->samplesize
= samplesize
;
1075 alac
->numchannels
= numchannels
;
1076 alac
->bytespersample
= (samplesize
/ 8) * numchannels
;