* Copyright (c) 2005 David Hammerton
* All rights reserved.
*
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* bytes 0-3 atom size (0x24), big-endian
* bytes 4-7 atom type ('alac', not the 'alac' tag from start of stsd)
* bytes 8-35 data bytes needed by decoder
+ *
+ * Extradata:
+ * 32bit size
+ * 32bit tag (=alac)
+ * 32bit zero?
+ * 32bit max sample per frame
+ * 8bit ?? (zero?)
+ * 8bit sample size
+ * 8bit history mult
+ * 8bit initial history
+ * 8bit kmodifier
+ * 8bit channels?
+ * 16bit ??
+ * 32bit max coded frame size
+ * 32bit bitrate?
+ * 32bit samplerate
*/
#include "avcodec.h"
+#include "bitstream.h"
#define ALAC_EXTRADATA_SIZE 36
-struct alac_file {
- unsigned char *input_buffer;
- int input_buffer_index;
- int input_buffer_size;
- int input_buffer_bitaccumulator; /* used so we can do arbitary
- bit reads */
+typedef struct {
+
+ AVCodecContext *avctx;
+ GetBitContext gb;
+ /* init to 0; first frame decode should initialize from extradata and
+ * set this to 1 */
+ int context_initialized;
int samplesize;
int numchannels;
int bytespersample;
-
/* buffers */
int32_t *predicterror_buffer_a;
int32_t *predicterror_buffer_b;
int32_t *outputsamples_buffer_a;
int32_t *outputsamples_buffer_b;
-
/* stuff from setinfo */
uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */
uint8_t setinfo_7a; /* 0x00 */
uint32_t setinfo_86; /* 0x00069fe4 */
uint32_t setinfo_8a_rate; /* 0x0000ac44 */
/* end setinfo stuff */
-};
-
-typedef struct alac_file alac_file;
-typedef struct {
-
- AVCodecContext *avctx;
- /* init to 0; first frame decode should initialize from extradata and
- * set this to 1 */
- int context_initialized;
-
- alac_file *alac;
} ALACContext;
-static void allocate_buffers(alac_file *alac)
+static void allocate_buffers(ALACContext *alac)
{
alac->predicterror_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
alac->predicterror_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
alac->outputsamples_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
}
-void alac_set_info(alac_file *alac, char *inputbuffer)
+static int alac_set_info(ALACContext *alac)
{
- unsigned char *ptr = inputbuffer;
+ unsigned char *ptr = alac->avctx->extradata;
ptr += 4; /* size */
ptr += 4; /* alac */
ptr += 4; /* 0 ? */
- alac->setinfo_max_samples_per_frame = BE_32(ptr); /* buffer size / 2 ? */
+ if(AV_RB32(ptr) >= UINT_MAX/4){
+ av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
+ return -1;
+ }
+ alac->setinfo_max_samples_per_frame = AV_RB32(ptr); /* buffer size / 2 ? */
ptr += 4;
alac->setinfo_7a = *ptr++;
alac->setinfo_sample_size = *ptr++;
alac->setinfo_rice_historymult = *ptr++;
alac->setinfo_rice_initialhistory = *ptr++;
alac->setinfo_rice_kmodifier = *ptr++;
- alac->setinfo_7f = *ptr++;
- alac->setinfo_80 = BE_16(ptr);
+ alac->setinfo_7f = *ptr++; // channels?
+ alac->setinfo_80 = AV_RB16(ptr);
ptr += 2;
- alac->setinfo_82 = BE_32(ptr);
+ alac->setinfo_82 = AV_RB32(ptr); // max coded frame size
ptr += 4;
- alac->setinfo_86 = BE_32(ptr);
+ alac->setinfo_86 = AV_RB32(ptr); // bitrate ?
ptr += 4;
- alac->setinfo_8a_rate = BE_32(ptr);
+ alac->setinfo_8a_rate = AV_RB32(ptr); // samplerate
ptr += 4;
allocate_buffers(alac);
-}
-
-/* stream reading */
-
-/* supports reading 1 to 16 bits, in big endian format */
-static uint32_t readbits_16(alac_file *alac, int bits)
-{
- uint32_t result;
- int new_accumulator;
-
- if (alac->input_buffer_index + 2 >= alac->input_buffer_size) {
- av_log(NULL, AV_LOG_ERROR, "alac: input buffer went out of bounds (%d >= %d)\n",
- alac->input_buffer_index + 2, alac->input_buffer_size);
- }
- result = (alac->input_buffer[alac->input_buffer_index + 0] << 16) |
- (alac->input_buffer[alac->input_buffer_index + 1] << 8) |
- (alac->input_buffer[alac->input_buffer_index + 2]);
-
- /* shift left by the number of bits we've already read,
- * so that the top 'n' bits of the 24 bits we read will
- * be the return bits */
- result = result << alac->input_buffer_bitaccumulator;
-
- result = result & 0x00ffffff;
-
- /* and then only want the top 'n' bits from that, where
- * n is 'bits' */
- result = result >> (24 - bits);
-
- new_accumulator = (alac->input_buffer_bitaccumulator + bits);
-
- /* increase the buffer pointer if we've read over n bytes. */
- alac->input_buffer_index += (new_accumulator >> 3);
- /* and the remainder goes back into the bit accumulator */
- alac->input_buffer_bitaccumulator = (new_accumulator & 7);
-
- return result;
-}
-
-/* supports reading 1 to 32 bits, in big endian format */
-static uint32_t readbits(alac_file *alac, int bits)
-{
- int32_t result = 0;
-
- if (bits > 16) {
- bits -= 16;
- result = readbits_16(alac, 16) << bits;
- }
-
- result |= readbits_16(alac, bits);
-
- return result;
-}
-
-/* reads a single bit */
-static int readbit(alac_file *alac)
-{
- int result;
- int new_accumulator;
-
- if (alac->input_buffer_index >= alac->input_buffer_size) {
- av_log(NULL, AV_LOG_ERROR, "alac: input buffer went out of bounds (%d >= %d)\n",
- alac->input_buffer_index + 2, alac->input_buffer_size);
- }
-
- result = alac->input_buffer[alac->input_buffer_index];
-
- result = result << alac->input_buffer_bitaccumulator;
-
- result = result >> 7 & 1;
-
- new_accumulator = (alac->input_buffer_bitaccumulator + 1);
-
- alac->input_buffer_index += (new_accumulator / 8);
-
- alac->input_buffer_bitaccumulator = (new_accumulator % 8);
-
- return result;
-}
-
-static void unreadbits(alac_file *alac, int bits)
-{
- int new_accumulator = (alac->input_buffer_bitaccumulator - bits);
-
- alac->input_buffer_index += (new_accumulator >> 3);
-
- alac->input_buffer_bitaccumulator = (new_accumulator & 7);
- if (alac->input_buffer_bitaccumulator < 0)
- alac->input_buffer_bitaccumulator *= -1;
+ return 0;
}
/* hideously inefficient. could use a bitmask search,
return i;
}
-void bastardized_rice_decompress(alac_file *alac,
+static void bastardized_rice_decompress(ALACContext *alac,
int32_t *output_buffer,
int output_size,
int readsamplesize, /* arg_10 */
int32_t final_val;
/* read x - number of 1s before 0 represent the rice */
- while (x <= 8 && readbit(alac)) {
+ while (x <= 8 && get_bits1(&alac->gb)) {
x++;
}
/* use alternative encoding */
int32_t value;
- value = readbits(alac, readsamplesize);
+ value = get_bits(&alac->gb, readsamplesize);
/* mask value to readsamplesize size */
if (readsamplesize != 32)
/* read k, that is bits as is */
k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
- if (k < 0)
+ if (k < 0)
k += rice_kmodifier;
- else
+ else
k = rice_kmodifier;
if (k != 1) {
- extrabits = readbits(alac, k);
+ extrabits = show_bits(&alac->gb, k);
/* multiply x by 2^k - 1, as part of their strange algorithm */
x = (x << k) - x;
if (extrabits > 1) {
x += extrabits - 1;
- } else
- unreadbits(alac, 1);
+ get_bits(&alac->gb, k);
+ } else {
+ get_bits(&alac->gb, k - 1);
+ }
}
}
sign_modifier = 1;
x = 0;
- while (x <= 8 && readbit(alac)) {
+ while (x <= 8 && get_bits1(&alac->gb)) {
x++;
}
if (x > 8) {
- block_size = readbits(alac, 16);
+ block_size = get_bits(&alac->gb, 16);
block_size &= 0xffff;
} else {
int k;
k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
- extrabits = readbits(alac, k);
+ extrabits = show_bits(&alac->gb, k);
block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
+ extrabits - 1;
if (extrabits < 2) {
x = 1 - extrabits;
block_size += x;
- unreadbits(alac, 1);
+ get_bits(&alac->gb, k - 1);
+ } else {
+ get_bits(&alac->gb, k);
}
}
}
}
-void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
+static void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
int16_t *buffer_out,
int numchannels, int numsamples,
uint8_t interlacing_shift,
void *outbuffer, int *outputsize,
uint8_t *inbuffer, int input_buffer_size)
{
- ALACContext *s = avctx->priv_data;
- alac_file *alac = s->alac;
+ ALACContext *alac = avctx->priv_data;
int channels;
int32_t outputsamples;
return input_buffer_size;
/* initialize from the extradata */
- if (!s->context_initialized) {
- if (s->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
- av_log(NULL, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
+ if (!alac->context_initialized) {
+ if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
+ av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
ALAC_EXTRADATA_SIZE);
return input_buffer_size;
}
- alac_set_info(s->alac, s->avctx->extradata);
- s->context_initialized = 1;
+ if (alac_set_info(alac)) {
+ av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
+ return input_buffer_size;
+ }
+ alac->context_initialized = 1;
}
outputsamples = alac->setinfo_max_samples_per_frame;
- /* setup the stream */
- alac->input_buffer = inbuffer;
- alac->input_buffer_index = 0;
- alac->input_buffer_size = input_buffer_size;
- alac->input_buffer_bitaccumulator = 0;
+ init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
- channels = readbits(alac, 3);
+ channels = get_bits(&alac->gb, 3);
*outputsize = outputsamples * alac->bytespersample;
/* 2^result = something to do with output waiting.
* perhaps matters if we read > 1 frame in a pass?
*/
- readbits(alac, 4);
+ get_bits(&alac->gb, 4);
- readbits(alac, 12); /* unknown, skip 12 bits */
+ get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
- hassize = readbits(alac, 1); /* the output sample size is stored soon */
+ hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
- wasted_bytes = readbits(alac, 2); /* unknown ? */
+ wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
- isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
+ isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
if (hassize) {
/* now read the number of samples,
* as a 32bit integer */
- outputsamples = readbits(alac, 32);
+ outputsamples = get_bits(&alac->gb, 32);
*outputsize = outputsamples * alac->bytespersample;
}
int prediction_quantitization;
int i;
- /* skip 16 bits, not sure what they are. seem to be used in
+ /* FIXME: skip 16 bits, not sure what they are. seem to be used in
* two channel case */
- readbits(alac, 8);
- readbits(alac, 8);
+ get_bits(&alac->gb, 8);
+ get_bits(&alac->gb, 8);
- prediction_type = readbits(alac, 4);
- prediction_quantitization = readbits(alac, 4);
+ prediction_type = get_bits(&alac->gb, 4);
+ prediction_quantitization = get_bits(&alac->gb, 4);
- ricemodifier = readbits(alac, 3);
- predictor_coef_num = readbits(alac, 5);
+ ricemodifier = get_bits(&alac->gb, 3);
+ predictor_coef_num = get_bits(&alac->gb, 5);
/* read the predictor table */
for (i = 0; i < predictor_coef_num; i++) {
- predictor_coef_table[i] = (int16_t)readbits(alac, 16);
+ predictor_coef_table[i] = (int16_t)get_bits(&alac->gb, 16);
}
if (wasted_bytes) {
/* these bytes seem to have something to do with
* > 2 channel files.
*/
- av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
+ av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
}
bastardized_rice_decompress(alac,
predictor_coef_num,
prediction_quantitization);
} else {
- av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
+ av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
/* i think the only other prediction type (or perhaps this is just a
* boolean?) runs adaptive fir twice.. like:
* predictor_decompress_fir_adapt(predictor_error, tempout, ...)
if (readsamplesize <= 16) {
int i;
for (i = 0; i < outputsamples; i++) {
- int32_t audiobits = readbits(alac, readsamplesize);
+ int32_t audiobits = get_bits(&alac->gb, readsamplesize);
audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
for (i = 0; i < outputsamples; i++) {
int32_t audiobits;
- audiobits = readbits(alac, 16);
+ audiobits = get_bits(&alac->gb, 16);
/* special case of sign extension..
* as we'll be ORing the low 16bits into this */
audiobits = audiobits << 16;
audiobits = audiobits >> (32 - readsamplesize);
- audiobits |= readbits(alac, readsamplesize - 16);
+ audiobits |= get_bits(&alac->gb, readsamplesize - 16);
alac->outputsamples_buffer_a[i] = audiobits;
}
int i;
for (i = 0; i < outputsamples; i++) {
int16_t sample = alac->outputsamples_buffer_a[i];
- be2me_16(sample);
((int16_t*)outbuffer)[i * alac->numchannels] = sample;
}
break;
case 20:
case 24:
case 32:
- av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
+ av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
break;
default:
break;
/* 2^result = something to do with output waiting.
* perhaps matters if we read > 1 frame in a pass?
*/
- readbits(alac, 4);
+ get_bits(&alac->gb, 4);
- readbits(alac, 12); /* unknown, skip 12 bits */
+ get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
- hassize = readbits(alac, 1); /* the output sample size is stored soon */
+ hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
- wasted_bytes = readbits(alac, 2); /* unknown ? */
+ wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
- isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
+ isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
if (hassize) {
/* now read the number of samples,
* as a 32bit integer */
- outputsamples = readbits(alac, 32);
+ outputsamples = get_bits(&alac->gb, 32);
*outputsize = outputsamples * alac->bytespersample;
}
int i;
- interlacing_shift = readbits(alac, 8);
- interlacing_leftweight = readbits(alac, 8);
+ interlacing_shift = get_bits(&alac->gb, 8);
+ interlacing_leftweight = get_bits(&alac->gb, 8);
/******** channel 1 ***********/
- prediction_type_a = readbits(alac, 4);
- prediction_quantitization_a = readbits(alac, 4);
+ prediction_type_a = get_bits(&alac->gb, 4);
+ prediction_quantitization_a = get_bits(&alac->gb, 4);
- ricemodifier_a = readbits(alac, 3);
- predictor_coef_num_a = readbits(alac, 5);
+ ricemodifier_a = get_bits(&alac->gb, 3);
+ predictor_coef_num_a = get_bits(&alac->gb, 5);
/* read the predictor table */
for (i = 0; i < predictor_coef_num_a; i++) {
- predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
+ predictor_coef_table_a[i] = (int16_t)get_bits(&alac->gb, 16);
}
/******** channel 2 *********/
- prediction_type_b = readbits(alac, 4);
- prediction_quantitization_b = readbits(alac, 4);
+ prediction_type_b = get_bits(&alac->gb, 4);
+ prediction_quantitization_b = get_bits(&alac->gb, 4);
- ricemodifier_b = readbits(alac, 3);
- predictor_coef_num_b = readbits(alac, 5);
+ ricemodifier_b = get_bits(&alac->gb, 3);
+ predictor_coef_num_b = get_bits(&alac->gb, 5);
/* read the predictor table */
for (i = 0; i < predictor_coef_num_b; i++) {
- predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
+ predictor_coef_table_b[i] = (int16_t)get_bits(&alac->gb, 16);
}
/*********************/
if (wasted_bytes) {
/* see mono case */
- av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
+ av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
}
/* channel 1 */
prediction_quantitization_a);
} else {
/* see mono case */
- av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
+ av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
}
/* channel 2 */
predictor_coef_num_b,
prediction_quantitization_b);
} else {
- av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
+ av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
}
- } else {
+ } else {
/* not compressed, easy case */
if (alac->setinfo_sample_size <= 16) {
int i;
for (i = 0; i < outputsamples; i++) {
int32_t audiobits_a, audiobits_b;
- audiobits_a = readbits(alac, alac->setinfo_sample_size);
- audiobits_b = readbits(alac, alac->setinfo_sample_size);
+ audiobits_a = get_bits(&alac->gb, alac->setinfo_sample_size);
+ audiobits_b = get_bits(&alac->gb, alac->setinfo_sample_size);
audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
for (i = 0; i < outputsamples; i++) {
int32_t audiobits_a, audiobits_b;
- audiobits_a = readbits(alac, 16);
+ audiobits_a = get_bits(&alac->gb, 16);
audiobits_a = audiobits_a << 16;
audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
- audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16);
+ audiobits_a |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
- audiobits_b = readbits(alac, 16);
+ audiobits_b = get_bits(&alac->gb, 16);
audiobits_b = audiobits_b << 16;
audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
- audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16);
+ audiobits_b |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
alac->outputsamples_buffer_a[i] = audiobits_a;
alac->outputsamples_buffer_b[i] = audiobits_b;
case 20:
case 24:
case 32:
- av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
+ av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
break;
default:
break;
static int alac_decode_init(AVCodecContext * avctx)
{
- ALACContext *s = avctx->priv_data;
- s->avctx = avctx;
- s->context_initialized = 0;
-
- s->alac = av_malloc(sizeof(alac_file));
+ ALACContext *alac = avctx->priv_data;
+ alac->avctx = avctx;
+ alac->context_initialized = 0;
- s->alac->samplesize = s->avctx->bits_per_sample;
- s->alac->numchannels = s->avctx->channels;
- s->alac->bytespersample = (s->alac->samplesize / 8) * s->alac->numchannels;
+ alac->samplesize = alac->avctx->bits_per_sample;
+ alac->numchannels = alac->avctx->channels;
+ alac->bytespersample = (alac->samplesize / 8) * alac->numchannels;
return 0;
}
static int alac_decode_close(AVCodecContext *avctx)
{
- ALACContext *s = avctx->priv_data;
+ ALACContext *alac = avctx->priv_data;
- av_free(s->alac->predicterror_buffer_a);
- av_free(s->alac->predicterror_buffer_b);
+ av_free(alac->predicterror_buffer_a);
+ av_free(alac->predicterror_buffer_b);
- av_free(s->alac->outputsamples_buffer_a);
- av_free(s->alac->outputsamples_buffer_b);
+ av_free(alac->outputsamples_buffer_a);
+ av_free(alac->outputsamples_buffer_b);
return 0;
}