60 #define ALAC_EXTRADATA_SIZE 36
106 int nb_samples,
int bps,
int rice_history_mult)
110 int sign_modifier = 0;
112 for (i = 0; i < nb_samples; i++) {
120 k =
av_log2((history >> 9) + 3);
125 output_buffer[i] = (x >> 1) ^ -(x & 1);
131 history += x * rice_history_mult -
132 ((history * rice_history_mult) >> 9);
135 if ((history < 128) && (i + 1 < nb_samples)) {
139 k = 7 -
av_log2(history) + ((history + 16) >> 6);
143 if (block_size > 0) {
144 if (block_size >= nb_samples - i) {
146 "invalid zero block size of %d %d %d\n", block_size,
148 block_size = nb_samples - i - 1;
150 memset(&output_buffer[i + 1], 0,
151 block_size *
sizeof(*output_buffer));
154 if (block_size <= 0xffff)
168 int nb_samples,
int bps, int16_t *lpc_coefs,
169 int lpc_order,
int lpc_quant)
175 *buffer_out = *error_buffer;
181 memcpy(&buffer_out[1], &error_buffer[1],
182 (nb_samples - 1) *
sizeof(*buffer_out));
186 if (lpc_order == 31) {
188 for (i = 1; i < nb_samples; i++) {
189 buffer_out[i] =
sign_extend(buffer_out[i - 1] + error_buffer[i],
196 for (i = 1; i <= lpc_order && i < nb_samples; i++)
197 buffer_out[i] =
sign_extend(buffer_out[i - 1] + error_buffer[i], bps);
201 for (; i < nb_samples; i++) {
204 int error_val = error_buffer[i];
209 for (j = 0; j < lpc_order; j++)
210 val += (pred[j] - d) * lpc_coefs[j];
211 val = (val + (1 << (lpc_quant - 1))) >> lpc_quant;
212 val += d + error_val;
218 for (j = 0; j < lpc_order && error_val * error_sign > 0; j++) {
222 lpc_coefs[j] -= sign;
224 error_val -= (val >> lpc_quant) * (j + 1);
231 int decorr_shift,
int decorr_left_weight)
235 for (i = 0; i < nb_samples; i++) {
241 a -= (b * decorr_left_weight) >> decorr_shift;
254 for (ch = 0; ch < channels; ch++)
255 for (i = 0; i < nb_samples; i++)
256 buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i];
263 int has_size,
bps, is_compressed, decorr_shift, decorr_left_weight,
ret;
264 uint32_t output_samples;
298 }
else if (output_samples != alac->
nb_samples) {
305 for (ch = 0; ch < channels; ch++)
310 int16_t lpc_coefs[2][32];
312 int prediction_type[2];
314 int rice_history_mult[2];
319 for (ch = 0; ch < channels; ch++) {
322 rice_history_mult[ch] =
get_bits(&alac->
gb, 3);
329 for (i = lpc_order[ch] - 1; i >= 0; i--)
337 for (ch = 0; ch < channels; ch++)
341 for (ch = 0; ch < channels; ch++) {
349 if (prediction_type[ch] == 15) {
360 }
else if (prediction_type[ch] > 0) {
362 prediction_type[ch]);
366 bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]);
373 for (ch = 0; ch < channels; ch++) {
380 decorr_left_weight = 0;
383 if (channels == 2 && decorr_left_weight) {
385 decorr_shift, decorr_left_weight);
396 for (ch = 0; ch < channels; ch++) {
397 int16_t *outbuffer = (int16_t *)frame->
extended_data[ch_index + ch];
403 for (ch = 0; ch < channels; ch++) {
412 int16_t *outbuffer = ((int16_t *)frame->
extended_data[0]) + ch_index;
414 for (ch = 0; ch < channels; ch++)
416 outbuffer += alac->
channels - channels;
423 for (ch = 0; ch < channels; ch++)
425 outbuffer += alac->
channels - channels;
432 for (ch = 0; ch < channels; ch++)
434 outbuffer += alac->
channels - channels;
445 int *got_frame_ptr,
AVPacket *avpkt)
451 int ch,
ret, got_end;
470 channels = (element ==
TYPE_CPE) ? 2 : 1;
471 if (ch + channels > alac->
channels ||
525 buf_size, buf_alloc_fail);
530 buf_size, buf_alloc_fail);
534 buf_size, buf_alloc_fail);
555 "max samples per frame invalid: %"PRIu32
"\n",
563 alac->
rice_limit = bytestream2_get_byteu(&gb);
564 alac->
channels = bytestream2_get_byteu(&gb);
565 bytestream2_get_be16u(&gb);
566 bytestream2_get_be32u(&gb);
567 bytestream2_get_be32u(&gb);
568 bytestream2_get_be32u(&gb);