56 #define ALAC_EXTRADATA_SIZE 36
57 #define MAX_CHANNELS 2
113 int32_t *output_buffer,
116 int rice_initialhistory,
118 int rice_historymult,
119 int rice_kmodifier_mask
123 unsigned int history = rice_initialhistory;
124 int sign_modifier = 0;
126 for (output_count = 0; output_count < output_size; output_count++) {
135 k = av_log2((history >> 9) + 3);
138 x_modified = sign_modifier + x;
139 final_val = (x_modified + 1) / 2;
140 if (x_modified & 1) final_val *= -1;
142 output_buffer[output_count] = final_val;
147 history += x_modified * rice_historymult
148 - ((history * rice_historymult) >> 9);
150 if (x_modified > 0xffff)
154 if ((history < 128) && (output_count+1 < output_size)) {
156 unsigned int block_size;
160 k = 7 - av_log2(history) + ((history + 16) >> 6 );
164 if (block_size > 0) {
165 if(block_size >= output_size - output_count){
166 av_log(alac->
avctx,
AV_LOG_ERROR,
"invalid zero block size of %d %d %d\n", block_size, output_size, output_count);
167 block_size= output_size - output_count - 1;
169 memset(&output_buffer[output_count+1], 0, block_size * 4);
170 output_count += block_size;
173 if (block_size > 0xffff)
190 int16_t *predictor_coef_table,
191 int predictor_coef_num,
192 int predictor_quantitization)
197 *buffer_out = *error_buffer;
199 if (!predictor_coef_num) {
200 if (output_size <= 1)
203 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
207 if (predictor_coef_num == 0x1f) {
211 if (output_size <= 1)
213 for (i = 0; i < output_size - 1; i++) {
217 prev_value = buffer_out[i];
218 error_value = error_buffer[i+1];
220 sign_extend((prev_value + error_value), readsamplesize);
226 if (predictor_coef_num > 0)
227 for (i = 0; i < predictor_coef_num; i++) {
230 val = buffer_out[i] + error_buffer[i+1];
232 buffer_out[i+1] = val;
240 if (predictor_coef_num > 0) {
241 for (i = predictor_coef_num + 1; i < output_size; i++) {
245 int error_val = error_buffer[i];
247 for (j = 0; j < predictor_coef_num; j++) {
248 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
249 predictor_coef_table[j];
252 outval = (1 << (predictor_quantitization-1)) + sum;
253 outval = outval >> predictor_quantitization;
254 outval = outval + buffer_out[0] + error_val;
257 buffer_out[predictor_coef_num+1] = outval;
260 int predictor_num = predictor_coef_num - 1;
262 while (predictor_num >= 0 && error_val > 0) {
263 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
266 predictor_coef_table[predictor_num] -= sign;
270 error_val -= ((val >> predictor_quantitization) *
271 (predictor_coef_num - predictor_num));
275 }
else if (error_val < 0) {
276 int predictor_num = predictor_coef_num - 1;
278 while (predictor_num >= 0 && error_val < 0) {
279 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
282 predictor_coef_table[predictor_num] -= sign;
286 error_val -= ((val >> predictor_quantitization) *
287 (predictor_coef_num - predictor_num));
299 int numsamples, uint8_t interlacing_shift,
300 uint8_t interlacing_leftweight)
304 for (i = 0; i < numsamples; i++) {
310 a -= (b * interlacing_leftweight) >> interlacing_shift;
319 int32_t *extra_bits_buffer[MAX_CHANNELS],
320 int extra_bits,
int numchannels,
int numsamples)
324 for (ch = 0; ch < numchannels; ch++)
325 for (i = 0; i < numsamples; i++)
326 buffer[ch][i] = (
buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i];
330 int16_t *buffer_out,
int numsamples)
334 for (i = 0; i < numsamples; i++) {
335 *buffer_out++ =
buffer[0][i];
336 *buffer_out++ =
buffer[1][i];
341 int32_t *buffer_out,
int numsamples)
345 for (i = 0; i < numsamples; i++) {
346 *buffer_out++ =
buffer[0][i] << 8;
347 *buffer_out++ =
buffer[1][i] << 8;
352 int *got_frame_ptr,
AVPacket *avpkt)
354 const uint8_t *inbuffer = avpkt->
data;
355 int input_buffer_size = avpkt->
size;
359 unsigned int outputsamples;
361 unsigned int readsamplesize;
363 uint8_t interlacing_shift;
364 uint8_t interlacing_leftweight;
417 if (!isnotcompressed) {
426 interlacing_leftweight =
get_bits(&alac->
gb, 8);
428 for (ch = 0; ch < channels; ch++) {
430 prediction_quantitization[ch] =
get_bits(&alac->
gb, 4);
433 predictor_coef_num[ch] =
get_bits(&alac->
gb, 5);
436 for (i = 0; i < predictor_coef_num[ch]; i++)
437 predictor_coef_table[ch][i] = (int16_t)
get_bits(&alac->
gb, 16);
441 for (i = 0; i < outputsamples; i++) {
442 for (ch = 0; ch < channels; ch++)
446 for (ch = 0; ch < channels; ch++) {
457 if (prediction_type[ch] == 15) {
467 outputsamples, readsamplesize,
469 }
else if (prediction_type[ch] > 0) {
471 prediction_type[ch]);
475 outputsamples, readsamplesize,
476 predictor_coef_table[ch],
477 predictor_coef_num[ch],
478 prediction_quantitization[ch]);
482 for (i = 0; i < outputsamples; i++) {
483 for (ch = 0; ch < channels; ch++) {
489 interlacing_shift = 0;
490 interlacing_leftweight = 0;
495 if (channels == 2 && interlacing_leftweight) {
497 interlacing_shift, interlacing_leftweight);
509 (int16_t *)alac->
frame.
data[0], outputsamples);
511 int16_t *outbuffer = (int16_t *)alac->
frame.
data[0];
512 for (i = 0; i < outputsamples; i++) {
520 (int32_t *)alac->
frame.
data[0], outputsamples);
522 int32_t *outbuffer = (int32_t *)alac->
frame.
data[0];
523 for (i = 0; i < outputsamples; i++)
535 return input_buffer_size;
559 buf_size, buf_alloc_fail);
562 buf_size, buf_alloc_fail);
565 buf_size, buf_alloc_fail);
581 if(
AV_RB32(ptr) >= UINT_MAX/4){
600 bytestream_get_be16(&ptr);
601 bytestream_get_be32(&ptr);
602 bytestream_get_be32(&ptr);
603 bytestream_get_be32(&ptr);
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define ALAC_EXTRADATA_SIZE
AV_WL32 AV_WL24 AV_WL16 AV_RB32
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
AVFrame * coded_frame
the picture in the bitstream
#define AV_LOG_WARNING
Something somehow does not look correct.
int32_t * extra_bits_buffer[MAX_CHANNELS]
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static int get_unary_0_9(GetBitContext *gb)
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
uint32_t setinfo_max_samples_per_frame
static void interleave_stereo_24(int32_t *buffer[MAX_CHANNELS], int32_t *buffer_out, int numsamples)
enum AVSampleFormat sample_fmt
audio sample format
static av_cold int alac_decode_close(AVCodecContext *avctx)
int32_t * predicterror_buffer[MAX_CHANNELS]
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static int get_bits_count(const GetBitContext *s)
static void predictor_decompress_fir_adapt(int32_t *error_buffer, int32_t *buffer_out, int output_size, int readsamplesize, int16_t *predictor_coef_table, int predictor_coef_num, int predictor_quantitization)
bitstream reader API header.
static int alac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
int extra_bits
number of extra bits beyond 16-bit
static int init(AVCodecParserContext *s)
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static void bastardized_rice_decompress(ALACContext *alac, int32_t *output_buffer, int output_size, int readsamplesize, int rice_initialhistory, int rice_kmodifier, int rice_historymult, int rice_kmodifier_mask)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int sign_only(int v)
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static void decorrelate_stereo(int32_t *buffer[MAX_CHANNELS], int numsamples, uint8_t interlacing_shift, uint8_t interlacing_leftweight)
static int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
static int alac_set_info(ALACContext *alac)
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
void av_log_ask_for_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message asking for a sample.
main external API structure.
static void close(AVCodecParserContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
uint8_t setinfo_rice_kmodifier
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
static void interleave_stereo_16(int32_t *buffer[MAX_CHANNELS], int16_t *buffer_out, int numsamples)
uint8_t setinfo_sample_size
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static av_const int sign_extend(int val, unsigned bits)
int32_t * outputsamples_buffer[MAX_CHANNELS]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
common internal api header.
static av_cold int alac_decode_init(AVCodecContext *avctx)
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
static void append_extra_bits(int32_t *buffer[MAX_CHANNELS], int32_t *extra_bits_buffer[MAX_CHANNELS], int extra_bits, int numchannels, int numsamples)
static int allocate_buffers(ALACContext *alac)
int channels
number of audio channels
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
uint8_t setinfo_rice_initialhistory
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t setinfo_rice_historymult