From b0b77b9ca572ed95bf24c222931f5f3da8982b89 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 8 Jul 2012 19:33:10 -0400 Subject: [PATCH 01/38] caf: support either old or new style ALAC magic kuki chunk --- libavformat/cafdec.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c index d5ee9be92a..4a04cb0b05 100644 --- a/libavformat/cafdec.c +++ b/libavformat/cafdec.c @@ -121,18 +121,40 @@ static int read_kuki_chunk(AVFormatContext *s, int64_t size) } else if (st->codec->codec_id == CODEC_ID_ALAC) { #define ALAC_PREAMBLE 12 #define ALAC_HEADER 36 - if (size < ALAC_PREAMBLE + ALAC_HEADER) { +#define ALAC_NEW_KUKI 24 + uint8_t preamble[12]; + if (size < ALAC_NEW_KUKI || size > ALAC_PREAMBLE + ALAC_HEADER) { av_log(s, AV_LOG_ERROR, "invalid ALAC magic cookie\n"); avio_skip(pb, size); return AVERROR_INVALIDDATA; } - avio_skip(pb, ALAC_PREAMBLE); + avio_read(pb, preamble, ALAC_PREAMBLE); + st->codec->extradata = av_mallocz(ALAC_HEADER + FF_INPUT_BUFFER_PADDING_SIZE); if (!st->codec->extradata) return AVERROR(ENOMEM); - avio_read(pb, st->codec->extradata, ALAC_HEADER); + + /* For the old style cookie, we skip 12 bytes, then read 36 bytes. + * The new style cookie only contains the last 24 bytes of what was + * 36 bytes in the old style cookie, so we fabricate the first 12 bytes + * in that case to maintain compatibility. */ + if (!memcmp(&preamble[4], "frmaalac", 8)) { + if (size < ALAC_PREAMBLE + ALAC_HEADER) { + av_log(s, AV_LOG_ERROR, "invalid ALAC magic cookie\n"); + av_freep(&st->codec->extradata); + return AVERROR_INVALIDDATA; + } + avio_read(pb, st->codec->extradata, ALAC_HEADER); + avio_skip(pb, size - ALAC_PREAMBLE - ALAC_HEADER); + } else { + AV_WB32(st->codec->extradata, 36); + memcpy(&st->codec->extradata[4], "alac", 4); + AV_WB32(&st->codec->extradata[8], 0); + memcpy(&st->codec->extradata[12], preamble, 12); + avio_read(pb, &st->codec->extradata[24], ALAC_NEW_KUKI - 12); + avio_skip(pb, size - ALAC_NEW_KUKI); + } st->codec->extradata_size = ALAC_HEADER; - avio_skip(pb, size - ALAC_PREAMBLE - ALAC_HEADER); } else { st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE); if (!st->codec->extradata) From f5a9c35f886508b851011b7dd4ec18cc67b57d37 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 14:10:52 -0400 Subject: [PATCH 02/38] caf: fix 'pakt' chunk parsing according to the CAF specification: "... the value for mChunkSize can be greater than the actual valid content of the packet table chunk" --- libavformat/cafdec.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c index 4a04cb0b05..90e97a10bd 100644 --- a/libavformat/cafdec.c +++ b/libavformat/cafdec.c @@ -192,10 +192,11 @@ static int read_pakt_chunk(AVFormatContext *s, int64_t size) st->duration += caf->frames_per_packet ? caf->frames_per_packet : ff_mp4_read_descr_len(pb); } - if (avio_tell(pb) - ccount != size) { + if (avio_tell(pb) - ccount > size) { av_log(s, AV_LOG_ERROR, "error reading packet table\n"); - return -1; + return AVERROR_INVALIDDATA; } + avio_skip(pb, ccount + size - avio_tell(pb)); caf->num_bytes = pos; return 0; From c0196a14b9f0f920da30bc30a8887fae9050a4b3 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 14:16:37 -0400 Subject: [PATCH 03/38] caf: use int64_t for num_packets It is used to store a value read by avio_rb64(). --- libavformat/cafdec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c index 90e97a10bd..15a1e2207c 100644 --- a/libavformat/cafdec.c +++ b/libavformat/cafdec.c @@ -172,8 +172,8 @@ static int read_pakt_chunk(AVFormatContext *s, int64_t size) AVIOContext *pb = s->pb; AVStream *st = s->streams[0]; CaffContext *caf = s->priv_data; - int64_t pos = 0, ccount; - int num_packets, i; + int64_t pos = 0, ccount, num_packets; + int i; ccount = avio_tell(pb); From 3bab7cd12802dc5abf2c5cc6dec49e9e249ce204 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sat, 7 Apr 2012 12:40:50 -0400 Subject: [PATCH 04/38] avformat: move 'chan' tag parsing to mov_chan.c to share with the CAF demuxer --- libavformat/cafdec.c | 6 ++++++ libavformat/mov.c | 39 +---------------------------------- libavformat/mov_chan.c | 46 +++++++++++++++++++++++++++++++++++++++++- libavformat/mov_chan.h | 19 +++++++++-------- 4 files changed, 63 insertions(+), 47 deletions(-) diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c index 15a1e2207c..6c40b1b229 100644 --- a/libavformat/cafdec.c +++ b/libavformat/cafdec.c @@ -29,6 +29,7 @@ #include "internal.h" #include "riff.h" #include "isom.h" +#include "mov_chan.h" #include "libavutil/intreadwrite.h" #include "libavutil/intfloat.h" #include "libavutil/dict.h" @@ -266,6 +267,11 @@ static int read_header(AVFormatContext *s) found_data = 1; break; + case MKBETAG('c','h','a','n'): + if ((ret = ff_mov_read_chan(s, st, size)) < 0) + return ret; + break; + /* magic cookie chunk */ case MKBETAG('k','u','k','i'): if (read_kuki_chunk(s, size)) diff --git a/libavformat/mov.c b/libavformat/mov.c index 647fd0eb9e..59d7b1a7e4 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -575,10 +575,6 @@ static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom) static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom) { AVStream *st; - uint8_t av_unused version; - uint32_t av_unused flags; - uint32_t layout_tag, bitmap, num_descr, label_mask; - int i; if (c->fc->nb_streams < 1) return 0; @@ -587,40 +583,7 @@ static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (atom.size < 16) return 0; - version = avio_r8(pb); - flags = avio_rb24(pb); - - layout_tag = avio_rb32(pb); - bitmap = avio_rb32(pb); - num_descr = avio_rb32(pb); - - if (atom.size < 16ULL + num_descr * 20ULL) - return 0; - - av_dlog(c->fc, "chan: size=%ld version=%u flags=%u layout=%u bitmap=%u num_descr=%u\n", - atom.size, version, flags, layout_tag, bitmap, num_descr); - - label_mask = 0; - for (i = 0; i < num_descr; i++) { - uint32_t label; - label = avio_rb32(pb); // mChannelLabel - avio_rb32(pb); // mChannelFlags - avio_rl32(pb); // mCoordinates[0] - avio_rl32(pb); // mCoordinates[1] - avio_rl32(pb); // mCoordinates[2] - if (layout_tag == 0) { - uint32_t mask_incr = ff_mov_get_channel_label(label); - if (mask_incr == 0) { - label_mask = 0; - break; - } - label_mask |= mask_incr; - } - } - if (layout_tag == 0) - st->codec->channel_layout = label_mask; - else - st->codec->channel_layout = ff_mov_get_channel_layout(layout_tag, bitmap); + ff_mov_read_chan(c->fc, st, atom.size - 4); return 0; } diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c index edd5f6c4da..c0b57119d1 100644 --- a/libavformat/mov_chan.c +++ b/libavformat/mov_chan.c @@ -477,7 +477,7 @@ uint64_t ff_mov_get_channel_layout(uint32_t tag, uint32_t bitmap) return layout_map[i].layout; } -uint32_t ff_mov_get_channel_label(uint32_t label) +static uint32_t mov_get_channel_label(uint32_t label) { if (label == 0) return 0; @@ -542,3 +542,47 @@ uint32_t ff_mov_get_channel_layout_tag(enum CodecID codec_id, return tag; } + +int ff_mov_read_chan(AVFormatContext *s, AVStream *st, int64_t size) +{ + AVIOContext *pb = s->pb; + uint32_t layout_tag, bitmap, num_descr, label_mask; + int i; + + if (size < 12) + return AVERROR_INVALIDDATA; + + layout_tag = avio_rb32(pb); + bitmap = avio_rb32(pb); + num_descr = avio_rb32(pb); + + av_dlog(s, "chan: layout=%u bitmap=%u num_descr=%u\n", + layout_tag, bitmap, num_descr); + + if (size < 12ULL + num_descr * 20ULL) + return 0; + + label_mask = 0; + for (i = 0; i < num_descr; i++) { + uint32_t label; + label = avio_rb32(pb); // mChannelLabel + avio_rb32(pb); // mChannelFlags + avio_rl32(pb); // mCoordinates[0] + avio_rl32(pb); // mCoordinates[1] + avio_rl32(pb); // mCoordinates[2] + if (layout_tag == 0) { + uint32_t mask_incr = mov_get_channel_label(label); + if (mask_incr == 0) { + label_mask = 0; + break; + } + label_mask |= mask_incr; + } + } + if (layout_tag == 0) + st->codec->channel_layout = label_mask; + else + st->codec->channel_layout = ff_mov_get_channel_layout(layout_tag, bitmap); + + return 0; +} diff --git a/libavformat/mov_chan.h b/libavformat/mov_chan.h index bd6adf6ad4..0897cd92dd 100644 --- a/libavformat/mov_chan.h +++ b/libavformat/mov_chan.h @@ -29,6 +29,7 @@ #include #include "libavcodec/avcodec.h" +#include "avformat.h" /** * Get the channel layout for the specified channel layout tag. @@ -39,14 +40,6 @@ */ uint64_t ff_mov_get_channel_layout(uint32_t tag, uint32_t bitmap); -/** - * Get the channel layout for the specified channel label. - * - * @param[in] label channel label - * @return channel layout mask fragment - */ -uint32_t ff_mov_get_channel_label(uint32_t label); - /** * Get the channel layout tag for the specified codec id and channel layout. * If the layout tag was not found, use a channel bitmap if possible. @@ -60,4 +53,14 @@ uint32_t ff_mov_get_channel_layout_tag(enum CodecID codec_id, uint64_t channel_layout, uint32_t *bitmap); +/** + * Read 'chan' tag from the input stream. + * + * @param s AVFormatContext + * @param st The stream to set codec values for + * @param size Remaining size in the 'chan' tag + * @return 0 if ok, or negative AVERROR code on failure + */ +int ff_mov_read_chan(AVFormatContext *s, AVStream *st, int64_t size); + #endif /* AVFORMAT_MOV_CHAN_H */ From 2ac17375830ed793eccabfaf54d3b0448baea2ea Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 10:10:02 -0400 Subject: [PATCH 05/38] alac: clean up and update comments leftover from reverse-engineering --- libavcodec/alac.c | 49 +++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index ab9fb81d87..699b86ad6f 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -70,19 +70,16 @@ typedef struct { int32_t *extra_bits_buffer[MAX_CHANNELS]; - /* stuff from setinfo */ - uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */ - uint8_t setinfo_sample_size; /* 0x10 */ - uint8_t setinfo_rice_historymult; /* 0x28 */ - uint8_t setinfo_rice_initialhistory; /* 0x0a */ - uint8_t setinfo_rice_kmodifier; /* 0x0e */ - /* end setinfo stuff */ + uint32_t setinfo_max_samples_per_frame; + uint8_t setinfo_sample_size; + uint8_t setinfo_rice_historymult; + uint8_t setinfo_rice_initialhistory; + uint8_t setinfo_rice_kmodifier; int extra_bits; /**< number of extra bits beyond 16-bit */ } ALACContext; static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){ - /* read x - number of 1s before 0 represent the rice */ int x = get_unary_0_9(gb); if (x > 8) { /* RICE THRESHOLD */ @@ -109,14 +106,13 @@ static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsam } static void bastardized_rice_decompress(ALACContext *alac, - int32_t *output_buffer, - int output_size, - int readsamplesize, /* arg_10 */ - int rice_initialhistory, /* arg424->b */ - int rice_kmodifier, /* arg424->d */ - int rice_historymult, /* arg424->c */ - int rice_kmodifier_mask /* arg424->e */ - ) + int32_t *output_buffer, + int output_size, + int readsamplesize, + int rice_initialhistory, + int rice_kmodifier, + int rice_historymult, + int rice_kmodifier_mask) { int output_count; unsigned int history = rice_initialhistory; @@ -203,10 +199,8 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, return; } - if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */ - /* second-best case scenario for fir decompression, - * error describes a small difference from the previous sample only - */ + if (predictor_coef_num == 31) { + /* simple 1st-order prediction */ if (output_size <= 1) return; for (i = 0; i < output_size - 1; i++) { @@ -231,9 +225,7 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, buffer_out[i+1] = val; } - /* 4 and 8 are very common cases (the only ones i've seen). these - * should be unrolled and optimized - */ + /* NOTE: 4 and 8 are very common cases that could be optimized. */ /* general case */ if (predictor_coef_num > 0) { @@ -371,14 +363,10 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, return AVERROR_INVALIDDATA; } - /* 2^result = something to do with output waiting. - * perhaps matters if we read > 1 frame in a pass? - */ - skip_bits(&alac->gb, 4); + skip_bits(&alac->gb, 4); /* element instance tag */ + skip_bits(&alac->gb, 12); /* unused header bits */ - skip_bits(&alac->gb, 12); /* unknown, skip 12 bits */ - - /* the output sample size is stored soon */ + /* the number of output samples is stored in the frame */ hassize = get_bits1(&alac->gb); alac->extra_bits = get_bits(&alac->gb, 2) << 3; @@ -578,7 +566,6 @@ static int alac_set_info(ALACContext *alac) bytestream2_skipu(&gb, 12); // size:4, alac:4, version:4 - /* buffer size / 2 ? */ alac->setinfo_max_samples_per_frame = bytestream2_get_be32u(&gb); if (alac->setinfo_max_samples_per_frame >= UINT_MAX/4){ av_log(alac->avctx, AV_LOG_ERROR, From 836e8b9ba0d80ce03b0664f97245f8f3ac43d145 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 10:23:47 -0400 Subject: [PATCH 06/38] alac: cosmetics: rename some ALACContext parameters --- libavcodec/alac.c | 127 +++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 64 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 699b86ad6f..4848ea218e 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -37,7 +37,7 @@ * 8bit sample size * 8bit history mult (40) * 8bit initial history (14) - * 8bit kmodifier (10) + * 8bit rice param limit (10) * 8bit channels * 16bit maxRun (255) * 32bit max coded frame size (0 means unknown) @@ -61,20 +61,18 @@ typedef struct { AVFrame frame; GetBitContext gb; - int numchannels; + int channels; /* buffers */ - int32_t *predicterror_buffer[MAX_CHANNELS]; - - int32_t *outputsamples_buffer[MAX_CHANNELS]; - + int32_t *predict_error_buffer[MAX_CHANNELS]; + int32_t *output_samples_buffer[MAX_CHANNELS]; int32_t *extra_bits_buffer[MAX_CHANNELS]; - uint32_t setinfo_max_samples_per_frame; - uint8_t setinfo_sample_size; - uint8_t setinfo_rice_historymult; - uint8_t setinfo_rice_initialhistory; - uint8_t setinfo_rice_kmodifier; + uint32_t max_samples_per_frame; + uint8_t sample_size; + uint8_t rice_history_mult; + uint8_t rice_initial_history; + uint8_t rice_limit; int extra_bits; /**< number of extra bits beyond 16-bit */ } ALACContext; @@ -109,13 +107,13 @@ 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_initial_history, + int rice_limit, + int rice_history_mult, int rice_kmodifier_mask) { int output_count; - unsigned int history = rice_initialhistory; + unsigned int history = rice_initial_history; int sign_modifier = 0; for (output_count = 0; output_count < output_size; output_count++) { @@ -128,7 +126,7 @@ static void bastardized_rice_decompress(ALACContext *alac, /* read k, that is bits as is */ k = av_log2((history >> 9) + 3); - x= decode_scalar(&alac->gb, k, rice_kmodifier, readsamplesize); + x= decode_scalar(&alac->gb, k, rice_limit, readsamplesize); x_modified = sign_modifier + x; final_val = (x_modified + 1) / 2; @@ -139,8 +137,8 @@ static void bastardized_rice_decompress(ALACContext *alac, sign_modifier = 0; /* now update the history */ - history += x_modified * rice_historymult - - ((history * rice_historymult) >> 9); + history += x_modified * rice_history_mult - + ((history * rice_history_mult) >> 9); if (x_modified > 0xffff) history = 0xffff; @@ -154,7 +152,7 @@ static void bastardized_rice_decompress(ALACContext *alac, k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */); - block_size= decode_scalar(&alac->gb, k, rice_kmodifier, 16); + block_size = decode_scalar(&alac->gb, k, rice_limit, 16); if (block_size > 0) { if(block_size >= output_size - output_count){ @@ -377,12 +375,13 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, if (hassize) { /* now read the number of samples as a 32bit integer */ outputsamples = get_bits_long(&alac->gb, 32); - if(outputsamples > alac->setinfo_max_samples_per_frame){ - av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", outputsamples, alac->setinfo_max_samples_per_frame); + if (outputsamples > alac->max_samples_per_frame) { + av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", + outputsamples, alac->max_samples_per_frame); return -1; } } else - outputsamples = alac->setinfo_max_samples_per_frame; + outputsamples = alac->max_samples_per_frame; /* get output buffer */ if (outputsamples > INT32_MAX) { @@ -395,7 +394,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, return ret; } - readsamplesize = alac->setinfo_sample_size - alac->extra_bits + channels - 1; + readsamplesize = alac->sample_size - alac->extra_bits + channels - 1; if (readsamplesize > MIN_CACHE_BITS) { av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize); return -1; @@ -432,13 +431,13 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, } for (ch = 0; ch < channels; ch++) { bastardized_rice_decompress(alac, - alac->predicterror_buffer[ch], + alac->predict_error_buffer[ch], outputsamples, readsamplesize, - alac->setinfo_rice_initialhistory, - alac->setinfo_rice_kmodifier, - ricemodifier[ch] * alac->setinfo_rice_historymult / 4, - (1 << alac->setinfo_rice_kmodifier) - 1); + alac->rice_initial_history, + alac->rice_limit, + ricemodifier[ch] * alac->rice_history_mult / 4, + (1 << alac->rice_limit) - 1); /* adaptive FIR filter */ if (prediction_type[ch] == 15) { @@ -449,16 +448,16 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, * However, this prediction type is not currently used by the * reference encoder. */ - predictor_decompress_fir_adapt(alac->predicterror_buffer[ch], - alac->predicterror_buffer[ch], + predictor_decompress_fir_adapt(alac->predict_error_buffer[ch], + alac->predict_error_buffer[ch], outputsamples, readsamplesize, NULL, 31, 0); } else if (prediction_type[ch] > 0) { av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n", prediction_type[ch]); } - predictor_decompress_fir_adapt(alac->predicterror_buffer[ch], - alac->outputsamples_buffer[ch], + predictor_decompress_fir_adapt(alac->predict_error_buffer[ch], + alac->output_samples_buffer[ch], outputsamples, readsamplesize, predictor_coef_table[ch], predictor_coef_num[ch], @@ -468,8 +467,8 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, /* not compressed, easy case */ for (i = 0; i < outputsamples; i++) { for (ch = 0; ch < channels; ch++) { - alac->outputsamples_buffer[ch][i] = get_sbits_long(&alac->gb, - alac->setinfo_sample_size); + alac->output_samples_buffer[ch][i] = get_sbits_long(&alac->gb, + alac->sample_size); } } alac->extra_bits = 0; @@ -480,35 +479,35 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n"); if (channels == 2 && interlacing_leftweight) { - decorrelate_stereo(alac->outputsamples_buffer, outputsamples, + decorrelate_stereo(alac->output_samples_buffer, outputsamples, interlacing_shift, interlacing_leftweight); } if (alac->extra_bits) { - append_extra_bits(alac->outputsamples_buffer, alac->extra_bits_buffer, - alac->extra_bits, alac->numchannels, outputsamples); + append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer, + alac->extra_bits, alac->channels, outputsamples); } - switch(alac->setinfo_sample_size) { + switch(alac->sample_size) { case 16: if (channels == 2) { - interleave_stereo_16(alac->outputsamples_buffer, + interleave_stereo_16(alac->output_samples_buffer, (int16_t *)alac->frame.data[0], outputsamples); } else { int16_t *outbuffer = (int16_t *)alac->frame.data[0]; for (i = 0; i < outputsamples; i++) { - outbuffer[i] = alac->outputsamples_buffer[0][i]; + outbuffer[i] = alac->output_samples_buffer[0][i]; } } break; case 24: if (channels == 2) { - interleave_stereo_24(alac->outputsamples_buffer, + interleave_stereo_24(alac->output_samples_buffer, (int32_t *)alac->frame.data[0], outputsamples); } else { int32_t *outbuffer = (int32_t *)alac->frame.data[0]; for (i = 0; i < outputsamples; i++) - outbuffer[i] = alac->outputsamples_buffer[0][i] << 8; + outbuffer[i] = alac->output_samples_buffer[0][i] << 8; } break; } @@ -527,9 +526,9 @@ static av_cold int alac_decode_close(AVCodecContext *avctx) ALACContext *alac = avctx->priv_data; int ch; - for (ch = 0; ch < alac->numchannels; ch++) { - av_freep(&alac->predicterror_buffer[ch]); - av_freep(&alac->outputsamples_buffer[ch]); + for (ch = 0; ch < alac->channels; ch++) { + av_freep(&alac->predict_error_buffer[ch]); + av_freep(&alac->output_samples_buffer[ch]); av_freep(&alac->extra_bits_buffer[ch]); } @@ -539,13 +538,13 @@ static av_cold int alac_decode_close(AVCodecContext *avctx) static int allocate_buffers(ALACContext *alac) { int ch; - for (ch = 0; ch < alac->numchannels; ch++) { - int buf_size = alac->setinfo_max_samples_per_frame * sizeof(int32_t); + for (ch = 0; ch < alac->channels; ch++) { + int buf_size = alac->max_samples_per_frame * sizeof(int32_t); - FF_ALLOC_OR_GOTO(alac->avctx, alac->predicterror_buffer[ch], + FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch], buf_size, buf_alloc_fail); - FF_ALLOC_OR_GOTO(alac->avctx, alac->outputsamples_buffer[ch], + FF_ALLOC_OR_GOTO(alac->avctx, alac->output_samples_buffer[ch], buf_size, buf_alloc_fail); FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch], @@ -566,18 +565,18 @@ static int alac_set_info(ALACContext *alac) bytestream2_skipu(&gb, 12); // size:4, alac:4, version:4 - alac->setinfo_max_samples_per_frame = bytestream2_get_be32u(&gb); - if (alac->setinfo_max_samples_per_frame >= UINT_MAX/4){ + alac->max_samples_per_frame = bytestream2_get_be32u(&gb); + if (alac->max_samples_per_frame >= UINT_MAX/4){ av_log(alac->avctx, AV_LOG_ERROR, - "setinfo_max_samples_per_frame too large\n"); + "max_samples_per_frame too large\n"); return AVERROR_INVALIDDATA; } bytestream2_skipu(&gb, 1); // compatible version - alac->setinfo_sample_size = bytestream2_get_byteu(&gb); - alac->setinfo_rice_historymult = bytestream2_get_byteu(&gb); - alac->setinfo_rice_initialhistory = bytestream2_get_byteu(&gb); - alac->setinfo_rice_kmodifier = bytestream2_get_byteu(&gb); - alac->numchannels = bytestream2_get_byteu(&gb); + alac->sample_size = bytestream2_get_byteu(&gb); + alac->rice_history_mult = bytestream2_get_byteu(&gb); + alac->rice_initial_history = bytestream2_get_byteu(&gb); + alac->rice_limit = bytestream2_get_byteu(&gb); + alac->channels = bytestream2_get_byteu(&gb); bytestream2_get_be16u(&gb); // maxRun bytestream2_get_be32u(&gb); // max coded frame size bytestream2_get_be32u(&gb); // average bitrate @@ -603,24 +602,24 @@ static av_cold int alac_decode_init(AVCodecContext * avctx) return -1; } - switch (alac->setinfo_sample_size) { + switch (alac->sample_size) { case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16; break; case 24: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break; default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n", - alac->setinfo_sample_size); + alac->sample_size); return AVERROR_PATCHWELCOME; } - if (alac->numchannels < 1) { + if (alac->channels < 1) { av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n"); - alac->numchannels = avctx->channels; + alac->channels = avctx->channels; } else { - if (alac->numchannels > MAX_CHANNELS) - alac->numchannels = avctx->channels; + if (alac->channels > MAX_CHANNELS) + alac->channels = avctx->channels; else - avctx->channels = alac->numchannels; + avctx->channels = alac->channels; } if (avctx->channels > MAX_CHANNELS) { av_log(avctx, AV_LOG_ERROR, "Unsupported channel count: %d\n", From 6e91f62256db72b056f6bc61106809d3994f8e26 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 10:30:40 -0400 Subject: [PATCH 07/38] alac: reduce the number of parameters to bastardized_rice_decompress() Use the ALACContext fields directly instead. --- libavcodec/alac.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 4848ea218e..8824ae8377 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -107,13 +107,10 @@ static void bastardized_rice_decompress(ALACContext *alac, int32_t *output_buffer, int output_size, int readsamplesize, - int rice_initial_history, - int rice_limit, - int rice_history_mult, - int rice_kmodifier_mask) + int rice_history_mult) { int output_count; - unsigned int history = rice_initial_history; + unsigned int history = alac->rice_initial_history; int sign_modifier = 0; for (output_count = 0; output_count < output_size; output_count++) { @@ -126,7 +123,7 @@ static void bastardized_rice_decompress(ALACContext *alac, /* read k, that is bits as is */ k = av_log2((history >> 9) + 3); - x= decode_scalar(&alac->gb, k, rice_limit, readsamplesize); + x = decode_scalar(&alac->gb, k, alac->rice_limit, readsamplesize); x_modified = sign_modifier + x; final_val = (x_modified + 1) / 2; @@ -152,7 +149,7 @@ static void bastardized_rice_decompress(ALACContext *alac, k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */); - block_size = decode_scalar(&alac->gb, k, rice_limit, 16); + block_size = decode_scalar(&alac->gb, k, alac->rice_limit, 16); if (block_size > 0) { if(block_size >= output_size - output_count){ @@ -434,10 +431,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, alac->predict_error_buffer[ch], outputsamples, readsamplesize, - alac->rice_initial_history, - alac->rice_limit, - ricemodifier[ch] * alac->rice_history_mult / 4, - (1 << alac->rice_limit) - 1); + ricemodifier[ch] * alac->rice_history_mult / 4); /* adaptive FIR filter */ if (prediction_type[ch] == 15) { From d9837434a91dbb3632df335414aad538e5b0a6e9 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 10:33:28 -0400 Subject: [PATCH 08/38] alac: limit the rice param before passing to decode_scalar() reduces the number of parameters to decode_scalar() and slightly simplifies the code --- libavcodec/alac.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 8824ae8377..76ef4998d6 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -77,17 +77,14 @@ typedef struct { int extra_bits; /**< number of extra bits beyond 16-bit */ } ALACContext; -static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){ +static inline int decode_scalar(GetBitContext *gb, int k, int readsamplesize) +{ int x = get_unary_0_9(gb); if (x > 8) { /* RICE THRESHOLD */ /* use alternative encoding */ x = get_bits(gb, readsamplesize); - } else { - if (k >= limit) - k = limit; - - if (k != 1) { + } else if (k != 1) { int extrabits = show_bits(gb, k); /* multiply x by 2^k - 1, as part of their strange algorithm */ @@ -98,7 +95,6 @@ static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsam skip_bits(gb, k); } else skip_bits(gb, k - 1); - } } return x; } @@ -123,7 +119,8 @@ static void bastardized_rice_decompress(ALACContext *alac, /* read k, that is bits as is */ k = av_log2((history >> 9) + 3); - x = decode_scalar(&alac->gb, k, alac->rice_limit, readsamplesize); + k = FFMIN(k, alac->rice_limit); + x = decode_scalar(&alac->gb, k, readsamplesize); x_modified = sign_modifier + x; final_val = (x_modified + 1) / 2; @@ -148,8 +145,9 @@ static void bastardized_rice_decompress(ALACContext *alac, sign_modifier = 1; k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */); + k = FFMIN(k, alac->rice_limit); - block_size = decode_scalar(&alac->gb, k, alac->rice_limit, 16); + block_size = decode_scalar(&alac->gb, k, 16); if (block_size > 0) { if(block_size >= output_size - output_count){ From a06fdadd97778fe50713c2762714cbd8b0eb81b6 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 10:34:11 -0400 Subject: [PATCH 09/38] alac: cosmetics: reindent after last commit --- libavcodec/alac.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 76ef4998d6..84f7e42373 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -85,16 +85,16 @@ static inline int decode_scalar(GetBitContext *gb, int k, int readsamplesize) /* use alternative encoding */ x = get_bits(gb, readsamplesize); } else if (k != 1) { - int extrabits = show_bits(gb, k); + int extrabits = show_bits(gb, k); - /* multiply x by 2^k - 1, as part of their strange algorithm */ - x = (x << k) - x; + /* multiply x by 2^k - 1, as part of their strange algorithm */ + x = (x << k) - x; - if (extrabits > 1) { - x += extrabits - 1; - skip_bits(gb, k); - } else - skip_bits(gb, k - 1); + if (extrabits > 1) { + x += extrabits - 1; + skip_bits(gb, k); + } else + skip_bits(gb, k - 1); } return x; } From 6fd8a28b596760b4b0a2f227c54d7315e04c396a Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 10:42:52 -0400 Subject: [PATCH 10/38] alac: adjust conditions for updating entropy decoder history avoids some unnecessary arithmetic in certain situations --- libavcodec/alac.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 84f7e42373..f27992d056 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -131,11 +131,11 @@ static void bastardized_rice_decompress(ALACContext *alac, sign_modifier = 0; /* now update the history */ - history += x_modified * rice_history_mult - - ((history * rice_history_mult) >> 9); - if (x_modified > 0xffff) history = 0xffff; + else + history += x_modified * rice_history_mult - + ((history * rice_history_mult) >> 9); /* special case: there may be compressed blocks of 0 */ if ((history < 128) && (output_count+1 < output_size)) { From 7e6593e9773773b5f6ad19fe4cdb2334d492493c Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 10:53:28 -0400 Subject: [PATCH 11/38] alac: eliminate 2 unneeded local variables in bastardized_rice_decompress() x_modified is just unnecessary, and final_val can be removed by simplifying the unsigned-to-signed conversion. --- libavcodec/alac.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index f27992d056..2aab84cd2e 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -110,32 +110,23 @@ static void bastardized_rice_decompress(ALACContext *alac, int sign_modifier = 0; for (output_count = 0; output_count < output_size; output_count++) { - int32_t x; - int32_t x_modified; - int32_t final_val; - - /* standard rice encoding */ - int k; /* size of extra bits */ + int x, k; /* read k, that is bits as is */ k = av_log2((history >> 9) + 3); k = FFMIN(k, alac->rice_limit); x = decode_scalar(&alac->gb, k, readsamplesize); - - x_modified = sign_modifier + x; - final_val = (x_modified + 1) / 2; - if (x_modified & 1) final_val *= -1; - - output_buffer[output_count] = final_val; - + x += sign_modifier; sign_modifier = 0; + output_buffer[output_count] = (x >> 1) ^ -(x & 1); + /* now update the history */ - if (x_modified > 0xffff) + if (x > 0xffff) history = 0xffff; else - history += x_modified * rice_history_mult - - ((history * rice_history_mult) >> 9); + history += x * rice_history_mult - + ((history * rice_history_mult) >> 9); /* special case: there may be compressed blocks of 0 */ if ((history < 128) && (output_count+1 < output_size)) { From 5177413d2043109f3e689e5fbfa79411c0c8e09d Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 10:57:42 -0400 Subject: [PATCH 12/38] alac: conditionally set sign_modifier to 1 It is already unconditionally set to 0 prior to this, so we can modify it only when needed. --- libavcodec/alac.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 2aab84cd2e..0aeb4109b7 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -133,8 +133,6 @@ static void bastardized_rice_decompress(ALACContext *alac, int k; unsigned int block_size; - sign_modifier = 1; - k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */); k = FFMIN(k, alac->rice_limit); @@ -149,8 +147,8 @@ static void bastardized_rice_decompress(ALACContext *alac, output_count += block_size; } - if (block_size > 0xffff) - sign_modifier = 0; + if (block_size <= 0xffff) + sign_modifier = 1; history = 0; } From 2fc24b3273f69a548531cdeaace8c68ac0f038ef Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 11:00:16 -0400 Subject: [PATCH 13/38] alac: remove a duplicate local variable --- libavcodec/alac.c | 1 - 1 file changed, 1 deletion(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 0aeb4109b7..74da253cab 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -130,7 +130,6 @@ static void bastardized_rice_decompress(ALACContext *alac, /* special case: there may be compressed blocks of 0 */ if ((history < 128) && (output_count+1 < output_size)) { - int k; unsigned int block_size; k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */); From 91620a04f197917d7272608620961e1145085d69 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 11:02:48 -0400 Subject: [PATCH 14/38] alac: make block_size signed It does not need to be unsigned. --- libavcodec/alac.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 74da253cab..91c94d7400 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -130,7 +130,7 @@ static void bastardized_rice_decompress(ALACContext *alac, /* special case: there may be compressed blocks of 0 */ if ((history < 128) && (output_count+1 < output_size)) { - unsigned int block_size; + int block_size; k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */); k = FFMIN(k, alac->rice_limit); From 4bcd637dcbae78ab419d61164e80438f5925030d Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 11:04:56 -0400 Subject: [PATCH 15/38] alac: use sizeof() instead of hardcoded data sizes --- libavcodec/alac.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 91c94d7400..483346c255 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -142,7 +142,8 @@ static void bastardized_rice_decompress(ALACContext *alac, av_log(alac->avctx, AV_LOG_ERROR, "invalid zero block size of %d %d %d\n", block_size, output_size, output_count); block_size= output_size - output_count - 1; } - memset(&output_buffer[output_count+1], 0, block_size * 4); + memset(&output_buffer[output_count + 1], 0, + block_size * sizeof(*output_buffer)); output_count += block_size; } @@ -176,7 +177,8 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, if (output_size <= 1) return; - memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4); + memcpy(&buffer_out[1], &error_buffer[1], + (output_size - 1) * sizeof(*buffer_out)); return; } From 79def4c523a1ebb4222c79f5dbf054d7bb4a5f01 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 11:07:57 -0400 Subject: [PATCH 16/38] alac: remove unneeded conditionals in predictor_decompress_fir_adapt() --- libavcodec/alac.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 483346c255..027e094c18 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -199,7 +199,6 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, } /* read warm-up samples */ - if (predictor_coef_num > 0) for (i = 0; i < predictor_coef_num; i++) { int32_t val; @@ -211,7 +210,6 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, /* NOTE: 4 and 8 are very common cases that could be optimized. */ /* general case */ - if (predictor_coef_num > 0) { for (i = predictor_coef_num + 1; i < output_size; i++) { int j; int sum = 0; @@ -266,7 +264,6 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, buffer_out++; } - } } static void decorrelate_stereo(int32_t *buffer[MAX_CHANNELS], From d0c0bf0d3e1bdb58188a2006bb24d83fae01236b Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 11:08:59 -0400 Subject: [PATCH 17/38] alac: cosmetics: reindent after last commit --- libavcodec/alac.c | 90 +++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 027e094c18..9a3ca51054 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -199,71 +199,71 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, } /* read warm-up samples */ - for (i = 0; i < predictor_coef_num; i++) { - int32_t val; + for (i = 0; i < predictor_coef_num; i++) { + int32_t val; - val = buffer_out[i] + error_buffer[i+1]; - val = sign_extend(val, readsamplesize); - buffer_out[i+1] = val; - } + val = buffer_out[i] + error_buffer[i+1]; + val = sign_extend(val, readsamplesize); + buffer_out[i+1] = val; + } /* NOTE: 4 and 8 are very common cases that could be optimized. */ /* general case */ - for (i = predictor_coef_num + 1; i < output_size; i++) { - int j; - int sum = 0; - int outval; - int error_val = error_buffer[i]; - - for (j = 0; j < predictor_coef_num; j++) { - sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * - predictor_coef_table[j]; - } + for (i = predictor_coef_num + 1; i < output_size; i++) { + int j; + int sum = 0; + int outval; + int error_val = error_buffer[i]; + + for (j = 0; j < predictor_coef_num; j++) { + sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * + predictor_coef_table[j]; + } - outval = (1 << (predictor_quantitization-1)) + sum; - outval = outval >> predictor_quantitization; - outval = outval + buffer_out[0] + error_val; - outval = sign_extend(outval, readsamplesize); + outval = (1 << (predictor_quantitization-1)) + sum; + outval = outval >> predictor_quantitization; + outval = outval + buffer_out[0] + error_val; + outval = sign_extend(outval, readsamplesize); - buffer_out[predictor_coef_num+1] = outval; + buffer_out[predictor_coef_num+1] = outval; - if (error_val > 0) { - int predictor_num = predictor_coef_num - 1; + if (error_val > 0) { + int predictor_num = predictor_coef_num - 1; - while (predictor_num >= 0 && error_val > 0) { - int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; - int sign = sign_only(val); + while (predictor_num >= 0 && error_val > 0) { + int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; + int sign = sign_only(val); - predictor_coef_table[predictor_num] -= sign; + predictor_coef_table[predictor_num] -= sign; - val *= sign; /* absolute value */ + val *= sign; /* absolute value */ - error_val -= ((val >> predictor_quantitization) * - (predictor_coef_num - predictor_num)); + error_val -= ((val >> predictor_quantitization) * + (predictor_coef_num - predictor_num)); - predictor_num--; - } - } else if (error_val < 0) { - int predictor_num = predictor_coef_num - 1; + predictor_num--; + } + } else if (error_val < 0) { + int predictor_num = predictor_coef_num - 1; - while (predictor_num >= 0 && error_val < 0) { - int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; - int sign = - sign_only(val); + while (predictor_num >= 0 && error_val < 0) { + int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; + int sign = - sign_only(val); - predictor_coef_table[predictor_num] -= sign; + predictor_coef_table[predictor_num] -= sign; - val *= sign; /* neg value */ + val *= sign; /* neg value */ - error_val -= ((val >> predictor_quantitization) * - (predictor_coef_num - predictor_num)); + error_val -= ((val >> predictor_quantitization) * + (predictor_coef_num - predictor_num)); - predictor_num--; - } + predictor_num--; } - - buffer_out++; } + + buffer_out++; + } } static void decorrelate_stereo(int32_t *buffer[MAX_CHANNELS], From 01880d287be6e2269b92eee8d9dc442532d277ea Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 11:12:25 -0400 Subject: [PATCH 18/38] alac: simplify 1st order prediction and reading of warm-up samples --- libavcodec/alac.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 9a3ca51054..b19973db50 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -186,25 +186,17 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, /* simple 1st-order prediction */ if (output_size <= 1) return; - for (i = 0; i < output_size - 1; i++) { - int32_t prev_value; - int32_t error_value; - - prev_value = buffer_out[i]; - error_value = error_buffer[i+1]; - buffer_out[i+1] = - sign_extend((prev_value + error_value), readsamplesize); + for (i = 1; i < output_size; i++) { + buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i], + readsamplesize); } return; } /* read warm-up samples */ for (i = 0; i < predictor_coef_num; i++) { - int32_t val; - - val = buffer_out[i] + error_buffer[i+1]; - val = sign_extend(val, readsamplesize); - buffer_out[i+1] = val; + buffer_out[i + 1] = sign_extend(buffer_out[i] + error_buffer[i + 1], + readsamplesize); } /* NOTE: 4 and 8 are very common cases that could be optimized. */ From abc4376b31b672f8f38c30851cbe1acd016b6e18 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 11:18:21 -0400 Subject: [PATCH 19/38] alac: reduce the number of local variables needed in lpc prediction --- libavcodec/alac.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index b19973db50..7306e57405 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -204,28 +204,27 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, /* general case */ for (i = predictor_coef_num + 1; i < output_size; i++) { int j; - int sum = 0; - int outval; + int val = 0; int error_val = error_buffer[i]; for (j = 0; j < predictor_coef_num; j++) { - sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * + val += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * predictor_coef_table[j]; } - outval = (1 << (predictor_quantitization-1)) + sum; - outval = outval >> predictor_quantitization; - outval = outval + buffer_out[0] + error_val; - outval = sign_extend(outval, readsamplesize); + val = (val + (1 << (predictor_quantitization - 1))) >> + predictor_quantitization; + val += buffer_out[0] + error_val; - buffer_out[predictor_coef_num+1] = outval; + buffer_out[predictor_coef_num + 1] = sign_extend(val, readsamplesize); if (error_val > 0) { int predictor_num = predictor_coef_num - 1; while (predictor_num >= 0 && error_val > 0) { - int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; - int sign = sign_only(val); + int sign; + val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; + sign = sign_only(val); predictor_coef_table[predictor_num] -= sign; @@ -240,8 +239,9 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, int predictor_num = predictor_coef_num - 1; while (predictor_num >= 0 && error_val < 0) { - int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; - int sign = - sign_only(val); + int sign; + val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; + sign = -sign_only(val); predictor_coef_table[predictor_num] -= sign; From f2515cd629d64484be5747639b485ddad9b6bf85 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 11:33:31 -0400 Subject: [PATCH 20/38] alac: simplify lpc coefficient adaptation --- libavcodec/alac.c | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 7306e57405..110d2cbcb1 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -206,6 +206,7 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, int j; int val = 0; int error_val = error_buffer[i]; + int error_sign; for (j = 0; j < predictor_coef_num; j++) { val += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * @@ -218,39 +219,17 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, buffer_out[predictor_coef_num + 1] = sign_extend(val, readsamplesize); - if (error_val > 0) { - int predictor_num = predictor_coef_num - 1; - - while (predictor_num >= 0 && error_val > 0) { + /* adapt LPC coefficients */ + error_sign = sign_only(error_val); + if (error_sign) { + for (j = predictor_coef_num - 1; j >= 0 && error_val * error_sign > 0; j--) { int sign; - val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; - sign = sign_only(val); - - predictor_coef_table[predictor_num] -= sign; - - val *= sign; /* absolute value */ - + val = buffer_out[0] - buffer_out[predictor_coef_num - j]; + sign = sign_only(val) * error_sign; + predictor_coef_table[j] -= sign; + val *= sign; error_val -= ((val >> predictor_quantitization) * - (predictor_coef_num - predictor_num)); - - predictor_num--; - } - } else if (error_val < 0) { - int predictor_num = predictor_coef_num - 1; - - while (predictor_num >= 0 && error_val < 0) { - int sign; - val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; - sign = -sign_only(val); - - predictor_coef_table[predictor_num] -= sign; - - val *= sign; /* neg value */ - - error_val -= ((val >> predictor_quantitization) * - (predictor_coef_num - predictor_num)); - - predictor_num--; + (predictor_coef_num - j)); } } From a4ecd4144265368db7cdb112e098a6ab5142557a Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 12:52:30 -0400 Subject: [PATCH 21/38] alac: use index into buffer_out instead of incrementing the pointer --- libavcodec/alac.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 110d2cbcb1..ba30bef7ea 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -202,29 +202,29 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, /* NOTE: 4 and 8 are very common cases that could be optimized. */ /* general case */ - for (i = predictor_coef_num + 1; i < output_size; i++) { + for (i = predictor_coef_num; i < output_size - 1; i++) { int j; int val = 0; - int error_val = error_buffer[i]; + int error_val = error_buffer[i + 1]; int error_sign; for (j = 0; j < predictor_coef_num; j++) { - val += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * + val += (buffer_out[i - j] - buffer_out[i - predictor_coef_num]) * predictor_coef_table[j]; } val = (val + (1 << (predictor_quantitization - 1))) >> predictor_quantitization; - val += buffer_out[0] + error_val; + val += buffer_out[i - predictor_coef_num] + error_val; - buffer_out[predictor_coef_num + 1] = sign_extend(val, readsamplesize); + buffer_out[i + 1] = sign_extend(val, readsamplesize); /* adapt LPC coefficients */ error_sign = sign_only(error_val); if (error_sign) { for (j = predictor_coef_num - 1; j >= 0 && error_val * error_sign > 0; j--) { int sign; - val = buffer_out[0] - buffer_out[predictor_coef_num - j]; + val = buffer_out[i - predictor_coef_num] - buffer_out[i - j]; sign = sign_only(val) * error_sign; predictor_coef_table[j] -= sign; val *= sign; @@ -232,8 +232,6 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, (predictor_coef_num - j)); } } - - buffer_out++; } } From ebd4c3add1ecad2c65ac80f0787ca5c1e78b600e Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 12:57:16 -0400 Subject: [PATCH 22/38] alac: factor out loading of next decoded sample in LPC prediction --- libavcodec/alac.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index ba30bef7ea..42dfdad595 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -207,15 +207,16 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, int val = 0; int error_val = error_buffer[i + 1]; int error_sign; + int d = buffer_out[i - predictor_coef_num]; for (j = 0; j < predictor_coef_num; j++) { - val += (buffer_out[i - j] - buffer_out[i - predictor_coef_num]) * + val += (buffer_out[i - j] - d) * predictor_coef_table[j]; } val = (val + (1 << (predictor_quantitization - 1))) >> predictor_quantitization; - val += buffer_out[i - predictor_coef_num] + error_val; + val += d + error_val; buffer_out[i + 1] = sign_extend(val, readsamplesize); @@ -224,7 +225,7 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, if (error_sign) { for (j = predictor_coef_num - 1; j >= 0 && error_val * error_sign > 0; j--) { int sign; - val = buffer_out[i - predictor_coef_num] - buffer_out[i - j]; + val = d - buffer_out[i - j]; sign = sign_only(val) * error_sign; predictor_coef_table[j] -= sign; val *= sign; From 9a6c528e08c42f43216fed9d6abd9e545db88d13 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 13:01:32 -0400 Subject: [PATCH 23/38] alac: factor out output_size check in predictor_decompress_fir_adapt() --- libavcodec/alac.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 42dfdad595..474531b83e 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -173,10 +173,10 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, /* first sample always copies */ *buffer_out = *error_buffer; - if (!predictor_coef_num) { - if (output_size <= 1) - return; + if (output_size <= 1) + return; + if (!predictor_coef_num) { memcpy(&buffer_out[1], &error_buffer[1], (output_size - 1) * sizeof(*buffer_out)); return; @@ -184,8 +184,6 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, if (predictor_coef_num == 31) { /* simple 1st-order prediction */ - if (output_size <= 1) - return; for (i = 1; i < output_size; i++) { buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i], readsamplesize); From 46043962eaf2a8f29290e9ffc19829fceb53f304 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 13:05:35 -0400 Subject: [PATCH 24/38] alac: avoid using a double-negative when checking if the frame is compressed --- libavcodec/alac.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 474531b83e..f803f9accc 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -298,7 +298,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, unsigned int outputsamples; int hassize; unsigned int readsamplesize; - int isnotcompressed; + int is_compressed; uint8_t interlacing_shift; uint8_t interlacing_leftweight; int i, ch, ret; @@ -320,7 +320,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, alac->extra_bits = get_bits(&alac->gb, 2) << 3; /* whether the frame is compressed */ - isnotcompressed = get_bits1(&alac->gb); + is_compressed = !get_bits1(&alac->gb); if (hassize) { /* now read the number of samples as a 32bit integer */ @@ -350,8 +350,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, return -1; } - if (!isnotcompressed) { - /* so it is compressed */ + if (is_compressed) { int16_t predictor_coef_table[MAX_CHANNELS][32]; int predictor_coef_num[MAX_CHANNELS]; int prediction_type[MAX_CHANNELS]; From 7a50ec6799b92f0e3681d734aa0769857a16ef87 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 13:15:35 -0400 Subject: [PATCH 25/38] alac: move the current samples per frame to the ALACContext This will simplify the multi-channel implementation. --- libavcodec/alac.c | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index f803f9accc..9e77f67457 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -75,6 +75,7 @@ typedef struct { uint8_t rice_limit; int extra_bits; /**< number of extra bits beyond 16-bit */ + int nb_samples; /**< number of samples in the current frame */ } ALACContext; static inline int decode_scalar(GetBitContext *gb, int k, int readsamplesize) @@ -295,7 +296,6 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, ALACContext *alac = avctx->priv_data; int channels; - unsigned int outputsamples; int hassize; unsigned int readsamplesize; int is_compressed; @@ -324,21 +324,18 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, if (hassize) { /* now read the number of samples as a 32bit integer */ - outputsamples = get_bits_long(&alac->gb, 32); - if (outputsamples > alac->max_samples_per_frame) { - av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", - outputsamples, alac->max_samples_per_frame); - return -1; + uint32_t output_samples = get_bits_long(&alac->gb, 32); + if (!output_samples || output_samples > alac->max_samples_per_frame) { + av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\n", + output_samples); + return AVERROR_INVALIDDATA; } + alac->nb_samples = output_samples; } else - outputsamples = alac->max_samples_per_frame; + alac->nb_samples = alac->max_samples_per_frame; /* get output buffer */ - if (outputsamples > INT32_MAX) { - av_log(avctx, AV_LOG_ERROR, "unsupported block size: %u\n", outputsamples); - return AVERROR_INVALIDDATA; - } - alac->frame.nb_samples = outputsamples; + alac->frame.nb_samples = alac->nb_samples; if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; @@ -373,7 +370,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, } if (alac->extra_bits) { - for (i = 0; i < outputsamples; i++) { + for (i = 0; i < alac->nb_samples; i++) { for (ch = 0; ch < channels; ch++) alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits); } @@ -381,7 +378,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, for (ch = 0; ch < channels; ch++) { bastardized_rice_decompress(alac, alac->predict_error_buffer[ch], - outputsamples, + alac->nb_samples, readsamplesize, ricemodifier[ch] * alac->rice_history_mult / 4); @@ -396,7 +393,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, */ predictor_decompress_fir_adapt(alac->predict_error_buffer[ch], alac->predict_error_buffer[ch], - outputsamples, readsamplesize, + alac->nb_samples, readsamplesize, NULL, 31, 0); } else if (prediction_type[ch] > 0) { av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n", @@ -404,14 +401,14 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, } predictor_decompress_fir_adapt(alac->predict_error_buffer[ch], alac->output_samples_buffer[ch], - outputsamples, readsamplesize, + alac->nb_samples, readsamplesize, predictor_coef_table[ch], predictor_coef_num[ch], prediction_quantitization[ch]); } } else { /* not compressed, easy case */ - for (i = 0; i < outputsamples; i++) { + for (i = 0; i < alac->nb_samples; i++) { for (ch = 0; ch < channels; ch++) { alac->output_samples_buffer[ch][i] = get_sbits_long(&alac->gb, alac->sample_size); @@ -425,23 +422,24 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n"); if (channels == 2 && interlacing_leftweight) { - decorrelate_stereo(alac->output_samples_buffer, outputsamples, + decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples, interlacing_shift, interlacing_leftweight); } if (alac->extra_bits) { append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer, - alac->extra_bits, alac->channels, outputsamples); + alac->extra_bits, alac->channels, alac->nb_samples); } switch(alac->sample_size) { case 16: if (channels == 2) { interleave_stereo_16(alac->output_samples_buffer, - (int16_t *)alac->frame.data[0], outputsamples); + (int16_t *)alac->frame.data[0], + alac->nb_samples); } else { int16_t *outbuffer = (int16_t *)alac->frame.data[0]; - for (i = 0; i < outputsamples; i++) { + for (i = 0; i < alac->nb_samples; i++) { outbuffer[i] = alac->output_samples_buffer[0][i]; } } @@ -449,10 +447,11 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, case 24: if (channels == 2) { interleave_stereo_24(alac->output_samples_buffer, - (int32_t *)alac->frame.data[0], outputsamples); + (int32_t *)alac->frame.data[0], + alac->nb_samples); } else { int32_t *outbuffer = (int32_t *)alac->frame.data[0]; - for (i = 0; i < outputsamples; i++) + for (i = 0; i < alac->nb_samples; i++) outbuffer[i] = alac->output_samples_buffer[0][i] << 8; } break; From 1193d3feddc1ce9ea233632679630b377d2a9894 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 13:19:10 -0400 Subject: [PATCH 26/38] alac: use get_sbits() to read LPC coefficients instead of casting --- libavcodec/alac.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 9e77f67457..db83796963 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -366,7 +366,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, /* read the predictor table */ for (i = 0; i < predictor_coef_num[ch]; i++) - predictor_coef_table[ch][i] = (int16_t)get_bits(&alac->gb, 16); + predictor_coef_table[ch][i] = get_sbits(&alac->gb, 16); } if (alac->extra_bits) { From 7a206eb32f624171a35235f714d44ee9dec9abcb Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 13:23:22 -0400 Subject: [PATCH 27/38] alac: fix check for valid max_samples_per_frame --- libavcodec/alac.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index db83796963..1fc4dc5cec 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -511,9 +511,9 @@ static int alac_set_info(ALACContext *alac) bytestream2_skipu(&gb, 12); // size:4, alac:4, version:4 alac->max_samples_per_frame = bytestream2_get_be32u(&gb); - if (alac->max_samples_per_frame >= UINT_MAX/4){ - av_log(alac->avctx, AV_LOG_ERROR, - "max_samples_per_frame too large\n"); + if (!alac->max_samples_per_frame || alac->max_samples_per_frame > INT_MAX) { + av_log(alac->avctx, AV_LOG_ERROR, "max samples per frame invalid: %u\n", + alac->max_samples_per_frame); return AVERROR_INVALIDDATA; } bytestream2_skipu(&gb, 1); // compatible version From 5138ff143f2ab3ea0dd7a34e03820f15ec64451d Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 13:30:03 -0400 Subject: [PATCH 28/38] alac: use AVPacket fields directly in alac_decode_frame() --- libavcodec/alac.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 1fc4dc5cec..ac51c2f3ee 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -291,8 +291,6 @@ static void interleave_stereo_24(int32_t *buffer[MAX_CHANNELS], static int alac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { - const uint8_t *inbuffer = avpkt->data; - int input_buffer_size = avpkt->size; ALACContext *alac = avctx->priv_data; int channels; @@ -303,7 +301,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, uint8_t interlacing_leftweight; int i, ch, ret; - init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8); + init_get_bits(&alac->gb, avpkt->data, avpkt->size * 8); channels = get_bits(&alac->gb, 3) + 1; if (channels != avctx->channels) { @@ -457,13 +455,14 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, break; } - if (input_buffer_size * 8 - get_bits_count(&alac->gb) > 8) - av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", input_buffer_size * 8 - get_bits_count(&alac->gb)); + if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) + av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", + avpkt->size * 8 - get_bits_count(&alac->gb)); *got_frame_ptr = 1; *(AVFrame *)data = alac->frame; - return input_buffer_size; + return avpkt->size; } static av_cold int alac_decode_close(AVCodecContext *avctx) From 6cda74c155a10bec7321d8c0c5be6fd3d809f41b Mon Sep 17 00:00:00 2001 From: Andrew D'Addesio Date: Mon, 9 Jul 2012 14:34:41 -0400 Subject: [PATCH 29/38] alac: simplify channel interleaving Signed-off-by: Justin Ruggles --- libavcodec/alac.c | 57 ++++++++++++----------------------------------- 1 file changed, 14 insertions(+), 43 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index ac51c2f3ee..df3c3148eb 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -266,28 +266,6 @@ static void append_extra_bits(int32_t *buffer[MAX_CHANNELS], buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i]; } -static void interleave_stereo_16(int32_t *buffer[MAX_CHANNELS], - int16_t *buffer_out, int numsamples) -{ - int i; - - for (i = 0; i < numsamples; i++) { - *buffer_out++ = buffer[0][i]; - *buffer_out++ = buffer[1][i]; - } -} - -static void interleave_stereo_24(int32_t *buffer[MAX_CHANNELS], - int32_t *buffer_out, int numsamples) -{ - int i; - - for (i = 0; i < numsamples; i++) { - *buffer_out++ = buffer[0][i] << 8; - *buffer_out++ = buffer[1][i] << 8; - } -} - static int alac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { @@ -430,28 +408,21 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, } switch(alac->sample_size) { - case 16: - if (channels == 2) { - interleave_stereo_16(alac->output_samples_buffer, - (int16_t *)alac->frame.data[0], - alac->nb_samples); - } else { - int16_t *outbuffer = (int16_t *)alac->frame.data[0]; - for (i = 0; i < alac->nb_samples; i++) { - outbuffer[i] = alac->output_samples_buffer[0][i]; - } - } + case 16: { + int16_t *outbuffer = (int16_t *)alac->frame.data[0]; + for (i = 0; i < alac->nb_samples; i++) { + *outbuffer++ = alac->output_samples_buffer[0][i]; + if (channels == 2) + *outbuffer++ = alac->output_samples_buffer[1][i]; + }} break; - case 24: - if (channels == 2) { - interleave_stereo_24(alac->output_samples_buffer, - (int32_t *)alac->frame.data[0], - alac->nb_samples); - } else { - int32_t *outbuffer = (int32_t *)alac->frame.data[0]; - for (i = 0; i < alac->nb_samples; i++) - outbuffer[i] = alac->output_samples_buffer[0][i] << 8; - } + case 24: { + int32_t *outbuffer = (int32_t *)alac->frame.data[0]; + for (i = 0; i < alac->nb_samples; i++) { + *outbuffer++ = alac->output_samples_buffer[0][i] << 8; + if (channels == 2) + *outbuffer++ = alac->output_samples_buffer[1][i] << 8; + }} break; } From 6482bd8831904ab7cd0a126687f871ff9c52b055 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 14:36:03 -0400 Subject: [PATCH 30/38] alac: add 32-bit decoding support --- libavcodec/alac.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index df3c3148eb..268d592325 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -424,6 +424,18 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, *outbuffer++ = alac->output_samples_buffer[1][i] << 8; }} break; + case 32: + if (channels == 2) { + int32_t *outbuffer = (int32_t *)alac->frame.data[0]; + for (i = 0; i < alac->nb_samples; i++) { + *outbuffer++ = alac->output_samples_buffer[0][i]; + *outbuffer++ = alac->output_samples_buffer[1][i]; + } + } else { + memcpy(alac->frame.data[0], alac->output_samples_buffer[0], + alac->nb_samples * sizeof(*alac->output_samples_buffer[0])); + } + break; } if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) @@ -520,7 +532,8 @@ static av_cold int alac_decode_init(AVCodecContext * avctx) switch (alac->sample_size) { case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16; break; - case 24: avctx->sample_fmt = AV_SAMPLE_FMT_S32; + case 24: + case 32: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break; default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n", alac->sample_size); From 73dc0db486b826e1442ae4e4177b4e3724b06a4e Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 15:18:51 -0400 Subject: [PATCH 31/38] alac: output in planar sample format Avoids unneeded interleaving and allows for reusing the AVFrame output buffer as the internal buffer for 24-bit and 32-bit sample size. --- libavcodec/alac.c | 46 +++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 268d592325..5f2c8a0492 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -316,6 +316,10 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } + if (alac->sample_size > 16) { + for (ch = 0; ch < alac->channels; ch++) + alac->output_samples_buffer[ch] = (int32_t *)alac->frame.data[ch]; + } readsamplesize = alac->sample_size - alac->extra_bits + channels - 1; if (readsamplesize > MIN_CACHE_BITS) { @@ -409,33 +413,18 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, switch(alac->sample_size) { case 16: { - int16_t *outbuffer = (int16_t *)alac->frame.data[0]; - for (i = 0; i < alac->nb_samples; i++) { - *outbuffer++ = alac->output_samples_buffer[0][i]; - if (channels == 2) - *outbuffer++ = alac->output_samples_buffer[1][i]; + for (ch = 0; ch < alac->channels; ch++) { + int16_t *outbuffer = (int16_t *)alac->frame.data[ch]; + for (i = 0; i < alac->nb_samples; i++) + *outbuffer++ = alac->output_samples_buffer[ch][i]; }} break; case 24: { - int32_t *outbuffer = (int32_t *)alac->frame.data[0]; - for (i = 0; i < alac->nb_samples; i++) { - *outbuffer++ = alac->output_samples_buffer[0][i] << 8; - if (channels == 2) - *outbuffer++ = alac->output_samples_buffer[1][i] << 8; + for (ch = 0; ch < alac->channels; ch++) { + for (i = 0; i < alac->nb_samples; i++) + alac->output_samples_buffer[ch][i] <<= 8; }} break; - case 32: - if (channels == 2) { - int32_t *outbuffer = (int32_t *)alac->frame.data[0]; - for (i = 0; i < alac->nb_samples; i++) { - *outbuffer++ = alac->output_samples_buffer[0][i]; - *outbuffer++ = alac->output_samples_buffer[1][i]; - } - } else { - memcpy(alac->frame.data[0], alac->output_samples_buffer[0], - alac->nb_samples * sizeof(*alac->output_samples_buffer[0])); - } - break; } if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) @@ -455,7 +444,8 @@ static av_cold int alac_decode_close(AVCodecContext *avctx) int ch; for (ch = 0; ch < alac->channels; ch++) { av_freep(&alac->predict_error_buffer[ch]); - av_freep(&alac->output_samples_buffer[ch]); + if (alac->sample_size == 16) + av_freep(&alac->output_samples_buffer[ch]); av_freep(&alac->extra_bits_buffer[ch]); } @@ -471,8 +461,10 @@ static int allocate_buffers(ALACContext *alac) FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch], buf_size, buf_alloc_fail); - FF_ALLOC_OR_GOTO(alac->avctx, alac->output_samples_buffer[ch], - buf_size, buf_alloc_fail); + if (alac->sample_size == 16) { + FF_ALLOC_OR_GOTO(alac->avctx, alac->output_samples_buffer[ch], + buf_size, buf_alloc_fail); + } FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch], buf_size, buf_alloc_fail); @@ -530,10 +522,10 @@ static av_cold int alac_decode_init(AVCodecContext * avctx) } switch (alac->sample_size) { - case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16; + case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16P; break; case 24: - case 32: avctx->sample_fmt = AV_SAMPLE_FMT_S32; + case 32: avctx->sample_fmt = AV_SAMPLE_FMT_S32P; break; default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n", alac->sample_size); From cd632619d954c653add4b4f1820e7d3488a5c9a7 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 15:38:58 -0400 Subject: [PATCH 32/38] alac: support a read sample size of up to 32 Use get_bits_long() in decode_scalar(). Use unsigned int for decoded value. --- libavcodec/alac.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 5f2c8a0492..b44b5c44ba 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -78,13 +78,14 @@ typedef struct { int nb_samples; /**< number of samples in the current frame */ } ALACContext; -static inline int decode_scalar(GetBitContext *gb, int k, int readsamplesize) +static inline unsigned int decode_scalar(GetBitContext *gb, int k, + int readsamplesize) { - int x = get_unary_0_9(gb); + unsigned int x = get_unary_0_9(gb); if (x > 8) { /* RICE THRESHOLD */ /* use alternative encoding */ - x = get_bits(gb, readsamplesize); + x = get_bits_long(gb, readsamplesize); } else if (k != 1) { int extrabits = show_bits(gb, k); @@ -111,7 +112,8 @@ static void bastardized_rice_decompress(ALACContext *alac, int sign_modifier = 0; for (output_count = 0; output_count < output_size; output_count++) { - int x, k; + int k; + unsigned int x; /* read k, that is bits as is */ k = av_log2((history >> 9) + 3); @@ -294,6 +296,11 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, hassize = get_bits1(&alac->gb); alac->extra_bits = get_bits(&alac->gb, 2) << 3; + readsamplesize = alac->sample_size - alac->extra_bits + channels - 1; + if (readsamplesize > 32) { + av_log(avctx, AV_LOG_ERROR, "bps is unsupported: %d\n", readsamplesize); + return AVERROR_PATCHWELCOME; + } /* whether the frame is compressed */ is_compressed = !get_bits1(&alac->gb); @@ -321,12 +328,6 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, alac->output_samples_buffer[ch] = (int32_t *)alac->frame.data[ch]; } - readsamplesize = alac->sample_size - alac->extra_bits + channels - 1; - if (readsamplesize > MIN_CACHE_BITS) { - av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize); - return -1; - } - if (is_compressed) { int16_t predictor_coef_table[MAX_CHANNELS][32]; int predictor_coef_num[MAX_CHANNELS]; From 81c9e2e6d074ccbf94e19c67d38b7b62e7c3f820 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 16:05:53 -0400 Subject: [PATCH 33/38] alac: split element parsing into a separate function This will make multi-channel implementation simpler. Based partially on a patch by Andrew D'Addesio . --- libavcodec/alac.c | 121 +++++++++++++++++++++++++++++++--------------- 1 file changed, 81 insertions(+), 40 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index b44b5c44ba..1e37290740 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -78,6 +78,18 @@ typedef struct { int nb_samples; /**< number of samples in the current frame */ } ALACContext; +enum RawDataBlockType { + /* At the moment, only SCE, CPE, LFE, and END are recognized. */ + TYPE_SCE, + TYPE_CPE, + TYPE_CCE, + TYPE_LFE, + TYPE_DSE, + TYPE_PCE, + TYPE_FIL, + TYPE_END +}; + static inline unsigned int decode_scalar(GetBitContext *gb, int k, int readsamplesize) { @@ -268,27 +280,18 @@ static void append_extra_bits(int32_t *buffer[MAX_CHANNELS], buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i]; } -static int alac_decode_frame(AVCodecContext *avctx, void *data, - int *got_frame_ptr, AVPacket *avpkt) +static int decode_element(AVCodecContext *avctx, void *data, int ch_index, + int channels) { ALACContext *alac = avctx->priv_data; - - int channels; int hassize; unsigned int readsamplesize; int is_compressed; uint8_t interlacing_shift; uint8_t interlacing_leftweight; + uint32_t output_samples; int i, ch, ret; - init_get_bits(&alac->gb, avpkt->data, avpkt->size * 8); - - channels = get_bits(&alac->gb, 3) + 1; - if (channels != avctx->channels) { - av_log(avctx, AV_LOG_ERROR, "frame header channel count mismatch\n"); - return AVERROR_INVALIDDATA; - } - skip_bits(&alac->gb, 4); /* element instance tag */ skip_bits(&alac->gb, 12); /* unused header bits */ @@ -305,28 +308,32 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, /* whether the frame is compressed */ is_compressed = !get_bits1(&alac->gb); - if (hassize) { - /* now read the number of samples as a 32bit integer */ - uint32_t output_samples = get_bits_long(&alac->gb, 32); - if (!output_samples || output_samples > alac->max_samples_per_frame) { - av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\n", - output_samples); - return AVERROR_INVALIDDATA; - } - alac->nb_samples = output_samples; - } else - alac->nb_samples = alac->max_samples_per_frame; - - /* get output buffer */ - alac->frame.nb_samples = alac->nb_samples; - if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); - return ret; + if (hassize) + output_samples = get_bits_long(&alac->gb, 32); + else + output_samples = alac->max_samples_per_frame; + if (!output_samples || output_samples > alac->max_samples_per_frame) { + av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\n", + output_samples); + return AVERROR_INVALIDDATA; } - if (alac->sample_size > 16) { - for (ch = 0; ch < alac->channels; ch++) - alac->output_samples_buffer[ch] = (int32_t *)alac->frame.data[ch]; + if (!alac->nb_samples) { + /* get output buffer */ + alac->frame.nb_samples = output_samples; + if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return ret; + } + if (alac->sample_size > 16) { + for (ch = 0; ch < channels; ch++) + alac->output_samples_buffer[ch] = (int32_t *)alac->frame.data[ch_index + ch]; + } + } else if (output_samples != alac->nb_samples) { + av_log(avctx, AV_LOG_ERROR, "sample count mismatch: %u != %d\n", + output_samples, alac->nb_samples); + return AVERROR_INVALIDDATA; } + alac->nb_samples = output_samples; if (is_compressed) { int16_t predictor_coef_table[MAX_CHANNELS][32]; @@ -391,16 +398,13 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, /* not compressed, easy case */ for (i = 0; i < alac->nb_samples; i++) { for (ch = 0; ch < channels; ch++) { - alac->output_samples_buffer[ch][i] = get_sbits_long(&alac->gb, - alac->sample_size); + alac->output_samples_buffer[ch][i] = get_sbits_long(&alac->gb, alac->sample_size); } } alac->extra_bits = 0; interlacing_shift = 0; interlacing_leftweight = 0; } - if (get_bits(&alac->gb, 3) != 7) - av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n"); if (channels == 2 && interlacing_leftweight) { decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples, @@ -409,25 +413,62 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, if (alac->extra_bits) { append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer, - alac->extra_bits, alac->channels, alac->nb_samples); + alac->extra_bits, channels, alac->nb_samples); } switch(alac->sample_size) { case 16: { - for (ch = 0; ch < alac->channels; ch++) { - int16_t *outbuffer = (int16_t *)alac->frame.data[ch]; + for (ch = 0; ch < channels; ch++) { + int16_t *outbuffer = (int16_t *)alac->frame.data[ch_index + ch]; for (i = 0; i < alac->nb_samples; i++) *outbuffer++ = alac->output_samples_buffer[ch][i]; }} break; case 24: { - for (ch = 0; ch < alac->channels; ch++) { + for (ch = 0; ch < channels; ch++) { for (i = 0; i < alac->nb_samples; i++) alac->output_samples_buffer[ch][i] <<= 8; }} break; } + return 0; +} + +static int alac_decode_frame(AVCodecContext *avctx, void *data, + int *got_frame_ptr, AVPacket *avpkt) +{ + ALACContext *alac = avctx->priv_data; + enum RawDataBlockType element; + int channels; + int ch, ret; + + init_get_bits(&alac->gb, avpkt->data, avpkt->size * 8); + + alac->nb_samples = 0; + ch = 0; + while (get_bits_left(&alac->gb)) { + element = get_bits(&alac->gb, 3); + if (element == TYPE_END) + break; + if (element > TYPE_CPE && element != TYPE_LFE) { + av_log(avctx, AV_LOG_ERROR, "syntax element unsupported: %d", element); + return AVERROR_PATCHWELCOME; + } + + channels = (element == TYPE_CPE) ? 2 : 1; + if (ch + channels > alac->channels) { + av_log(avctx, AV_LOG_ERROR, "invalid element channel count\n"); + return AVERROR_INVALIDDATA; + } + + ret = decode_element(avctx, data, ch, channels); + if (ret < 0) + return ret; + + ch += channels; + } + if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", avpkt->size * 8 - get_bits_count(&alac->gb)); From 1b3ef155d7033fcca4286d6e34a6c15214cb8bb0 Mon Sep 17 00:00:00 2001 From: Andrew D'Addesio Date: Mon, 9 Jul 2012 16:29:49 -0400 Subject: [PATCH 34/38] alac: multi-channel decoding support Signed-off-by: Justin Ruggles --- libavcodec/alac.c | 61 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 1e37290740..8c8e83076f 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -45,7 +45,7 @@ * 32bit samplerate */ - +#include "libavutil/audioconvert.h" #include "avcodec.h" #include "get_bits.h" #include "bytestream.h" @@ -53,7 +53,7 @@ #include "mathops.h" #define ALAC_EXTRADATA_SIZE 36 -#define MAX_CHANNELS 2 +#define MAX_CHANNELS 8 typedef struct { @@ -64,9 +64,9 @@ typedef struct { int channels; /* buffers */ - int32_t *predict_error_buffer[MAX_CHANNELS]; - int32_t *output_samples_buffer[MAX_CHANNELS]; - int32_t *extra_bits_buffer[MAX_CHANNELS]; + int32_t *predict_error_buffer[2]; + int32_t *output_samples_buffer[2]; + int32_t *extra_bits_buffer[2]; uint32_t max_samples_per_frame; uint8_t sample_size; @@ -90,6 +90,28 @@ enum RawDataBlockType { TYPE_END }; +static const uint8_t alac_channel_layout_offsets[8][8] = { + { 0 }, + { 0, 1 }, + { 2, 0, 1 }, + { 2, 0, 1, 3 }, + { 2, 0, 1, 3, 4 }, + { 2, 0, 1, 4, 5, 3 }, + { 2, 0, 1, 4, 5, 6, 3 }, + { 2, 6, 7, 0, 1, 4, 5, 3 } +}; + +static const uint16_t alac_channel_layouts[8] = { + AV_CH_LAYOUT_MONO, + AV_CH_LAYOUT_STEREO, + AV_CH_LAYOUT_SURROUND, + AV_CH_LAYOUT_4POINT0, + AV_CH_LAYOUT_5POINT0_BACK, + AV_CH_LAYOUT_5POINT1_BACK, + AV_CH_LAYOUT_6POINT1_BACK, + AV_CH_LAYOUT_7POINT1_WIDE_BACK +}; + static inline unsigned int decode_scalar(GetBitContext *gb, int k, int readsamplesize) { @@ -249,7 +271,7 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer, } } -static void decorrelate_stereo(int32_t *buffer[MAX_CHANNELS], +static void decorrelate_stereo(int32_t *buffer[2], int numsamples, uint8_t interlacing_shift, uint8_t interlacing_leftweight) { @@ -269,8 +291,8 @@ static void decorrelate_stereo(int32_t *buffer[MAX_CHANNELS], } } -static void append_extra_bits(int32_t *buffer[MAX_CHANNELS], - int32_t *extra_bits_buffer[MAX_CHANNELS], +static void append_extra_bits(int32_t *buffer[2], + int32_t *extra_bits_buffer[2], int extra_bits, int numchannels, int numsamples) { int i, ch; @@ -326,7 +348,7 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, } if (alac->sample_size > 16) { for (ch = 0; ch < channels; ch++) - alac->output_samples_buffer[ch] = (int32_t *)alac->frame.data[ch_index + ch]; + alac->output_samples_buffer[ch] = (int32_t *)alac->frame.extended_data[ch_index + ch]; } } else if (output_samples != alac->nb_samples) { av_log(avctx, AV_LOG_ERROR, "sample count mismatch: %u != %d\n", @@ -336,11 +358,11 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, alac->nb_samples = output_samples; if (is_compressed) { - int16_t predictor_coef_table[MAX_CHANNELS][32]; - int predictor_coef_num[MAX_CHANNELS]; - int prediction_type[MAX_CHANNELS]; - int prediction_quantitization[MAX_CHANNELS]; - int ricemodifier[MAX_CHANNELS]; + int16_t predictor_coef_table[2][32]; + int predictor_coef_num[2]; + int prediction_type[2]; + int prediction_quantitization[2]; + int ricemodifier[2]; interlacing_shift = get_bits(&alac->gb, 8); interlacing_leftweight = get_bits(&alac->gb, 8); @@ -419,7 +441,7 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, switch(alac->sample_size) { case 16: { for (ch = 0; ch < channels; ch++) { - int16_t *outbuffer = (int16_t *)alac->frame.data[ch_index + ch]; + int16_t *outbuffer = (int16_t *)alac->frame.extended_data[ch_index + ch]; for (i = 0; i < alac->nb_samples; i++) *outbuffer++ = alac->output_samples_buffer[ch][i]; }} @@ -462,7 +484,9 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, return AVERROR_INVALIDDATA; } - ret = decode_element(avctx, data, ch, channels); + ret = decode_element(avctx, data, + alac_channel_layout_offsets[alac->channels - 1][ch], + channels); if (ret < 0) return ret; @@ -484,7 +508,7 @@ static av_cold int alac_decode_close(AVCodecContext *avctx) ALACContext *alac = avctx->priv_data; int ch; - for (ch = 0; ch < alac->channels; ch++) { + for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) { av_freep(&alac->predict_error_buffer[ch]); if (alac->sample_size == 16) av_freep(&alac->output_samples_buffer[ch]); @@ -497,7 +521,7 @@ static av_cold int alac_decode_close(AVCodecContext *avctx) static int allocate_buffers(ALACContext *alac) { int ch; - for (ch = 0; ch < alac->channels; ch++) { + for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) { int buf_size = alac->max_samples_per_frame * sizeof(int32_t); FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch], @@ -588,6 +612,7 @@ static av_cold int alac_decode_init(AVCodecContext * avctx) avctx->channels); return AVERROR_PATCHWELCOME; } + avctx->channel_layout = alac_channel_layouts[alac->channels - 1]; if ((ret = allocate_buffers(alac)) < 0) { av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n"); From 2aebac691827caf78d700cec56b539f4d0d239f6 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 16:52:19 -0400 Subject: [PATCH 35/38] alac: cosmetics: rename some variables and function names --- libavcodec/alac.c | 170 +++++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 93 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 8c8e83076f..1c850986c8 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -112,14 +112,13 @@ static const uint16_t alac_channel_layouts[8] = { AV_CH_LAYOUT_7POINT1_WIDE_BACK }; -static inline unsigned int decode_scalar(GetBitContext *gb, int k, - int readsamplesize) +static inline unsigned int decode_scalar(GetBitContext *gb, int k, int bps) { unsigned int x = get_unary_0_9(gb); if (x > 8) { /* RICE THRESHOLD */ /* use alternative encoding */ - x = get_bits_long(gb, readsamplesize); + x = get_bits_long(gb, bps); } else if (k != 1) { int extrabits = show_bits(gb, k); @@ -135,28 +134,25 @@ static inline unsigned int decode_scalar(GetBitContext *gb, int k, return x; } -static void bastardized_rice_decompress(ALACContext *alac, - int32_t *output_buffer, - int output_size, - int readsamplesize, - int rice_history_mult) +static void rice_decompress(ALACContext *alac, int32_t *output_buffer, + int nb_samples, int bps, int rice_history_mult) { - int output_count; + int i; unsigned int history = alac->rice_initial_history; int sign_modifier = 0; - for (output_count = 0; output_count < output_size; output_count++) { + for (i = 0; i < nb_samples; i++) { int k; unsigned int x; /* read k, that is bits as is */ k = av_log2((history >> 9) + 3); k = FFMIN(k, alac->rice_limit); - x = decode_scalar(&alac->gb, k, readsamplesize); + x = decode_scalar(&alac->gb, k, bps); x += sign_modifier; sign_modifier = 0; - output_buffer[output_count] = (x >> 1) ^ -(x & 1); + output_buffer[i] = (x >> 1) ^ -(x & 1); /* now update the history */ if (x > 0xffff) @@ -166,7 +162,7 @@ static void bastardized_rice_decompress(ALACContext *alac, ((history * rice_history_mult) >> 9); /* special case: there may be compressed blocks of 0 */ - if ((history < 128) && (output_count+1 < output_size)) { + if ((history < 128) && (i + 1 < nb_samples)) { int block_size; k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */); @@ -175,13 +171,15 @@ static void bastardized_rice_decompress(ALACContext *alac, block_size = decode_scalar(&alac->gb, k, 16); if (block_size > 0) { - if(block_size >= output_size - output_count){ - av_log(alac->avctx, AV_LOG_ERROR, "invalid zero block size of %d %d %d\n", block_size, output_size, output_count); - block_size= output_size - output_count - 1; + if (block_size >= nb_samples - i) { + av_log(alac->avctx, AV_LOG_ERROR, + "invalid zero block size of %d %d %d\n", block_size, + nb_samples, i); + block_size = nb_samples - i - 1; } - memset(&output_buffer[output_count + 1], 0, + memset(&output_buffer[i + 1], 0, block_size * sizeof(*output_buffer)); - output_count += block_size; + i += block_size; } if (block_size <= 0xffff) @@ -197,93 +195,86 @@ static inline int sign_only(int v) return v ? FFSIGN(v) : 0; } -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) +static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out, + int nb_samples, int bps, int16_t *lpc_coefs, + int lpc_order, int lpc_quant) { int i; /* first sample always copies */ *buffer_out = *error_buffer; - if (output_size <= 1) + if (nb_samples <= 1) return; - if (!predictor_coef_num) { + if (!lpc_order) { memcpy(&buffer_out[1], &error_buffer[1], - (output_size - 1) * sizeof(*buffer_out)); + (nb_samples - 1) * sizeof(*buffer_out)); return; } - if (predictor_coef_num == 31) { + if (lpc_order == 31) { /* simple 1st-order prediction */ - for (i = 1; i < output_size; i++) { + for (i = 1; i < nb_samples; i++) { buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i], - readsamplesize); + bps); } return; } /* read warm-up samples */ - for (i = 0; i < predictor_coef_num; i++) { + for (i = 0; i < lpc_order; i++) { buffer_out[i + 1] = sign_extend(buffer_out[i] + error_buffer[i + 1], - readsamplesize); + bps); } /* NOTE: 4 and 8 are very common cases that could be optimized. */ /* general case */ - for (i = predictor_coef_num; i < output_size - 1; i++) { + for (i = lpc_order; i < nb_samples - 1; i++) { int j; int val = 0; int error_val = error_buffer[i + 1]; int error_sign; - int d = buffer_out[i - predictor_coef_num]; + int d = buffer_out[i - lpc_order]; - for (j = 0; j < predictor_coef_num; j++) { - val += (buffer_out[i - j] - d) * - predictor_coef_table[j]; + for (j = 0; j < lpc_order; j++) { + val += (buffer_out[i - j] - d) * lpc_coefs[j]; } - val = (val + (1 << (predictor_quantitization - 1))) >> - predictor_quantitization; + val = (val + (1 << (lpc_quant - 1))) >> lpc_quant; val += d + error_val; - buffer_out[i + 1] = sign_extend(val, readsamplesize); + buffer_out[i + 1] = sign_extend(val, bps); /* adapt LPC coefficients */ error_sign = sign_only(error_val); if (error_sign) { - for (j = predictor_coef_num - 1; j >= 0 && error_val * error_sign > 0; j--) { + for (j = lpc_order - 1; j >= 0 && error_val * error_sign > 0; j--) { int sign; val = d - buffer_out[i - j]; sign = sign_only(val) * error_sign; - predictor_coef_table[j] -= sign; + lpc_coefs[j] -= sign; val *= sign; - error_val -= ((val >> predictor_quantitization) * - (predictor_coef_num - j)); + error_val -= (val >> lpc_quant) * (lpc_order - j); } } } } static void decorrelate_stereo(int32_t *buffer[2], - int numsamples, uint8_t interlacing_shift, - uint8_t interlacing_leftweight) + int nb_samples, uint8_t decorr_shift, + uint8_t decorr_left_weight) { int i; - for (i = 0; i < numsamples; i++) { + for (i = 0; i < nb_samples; i++) { int32_t a, b; a = buffer[0][i]; b = buffer[1][i]; - a -= (b * interlacing_leftweight) >> interlacing_shift; + a -= (b * decorr_left_weight) >> decorr_shift; b += a; buffer[0][i] = b; @@ -293,12 +284,12 @@ static void decorrelate_stereo(int32_t *buffer[2], static void append_extra_bits(int32_t *buffer[2], int32_t *extra_bits_buffer[2], - int extra_bits, int numchannels, int numsamples) + int extra_bits, int channels, int nb_samples) { int i, ch; - for (ch = 0; ch < numchannels; ch++) - for (i = 0; i < numsamples; i++) + for (ch = 0; ch < channels; ch++) + for (i = 0; i < nb_samples; i++) buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i]; } @@ -306,11 +297,11 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, int channels) { ALACContext *alac = avctx->priv_data; - int hassize; - unsigned int readsamplesize; + int has_size; + unsigned int bps; int is_compressed; - uint8_t interlacing_shift; - uint8_t interlacing_leftweight; + uint8_t decorr_shift; + uint8_t decorr_left_weight; uint32_t output_samples; int i, ch, ret; @@ -318,19 +309,19 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, skip_bits(&alac->gb, 12); /* unused header bits */ /* the number of output samples is stored in the frame */ - hassize = get_bits1(&alac->gb); + has_size = get_bits1(&alac->gb); alac->extra_bits = get_bits(&alac->gb, 2) << 3; - readsamplesize = alac->sample_size - alac->extra_bits + channels - 1; - if (readsamplesize > 32) { - av_log(avctx, AV_LOG_ERROR, "bps is unsupported: %d\n", readsamplesize); + bps = alac->sample_size - alac->extra_bits + channels - 1; + if (bps > 32) { + av_log(avctx, AV_LOG_ERROR, "bps is unsupported: %d\n", bps); return AVERROR_PATCHWELCOME; } /* whether the frame is compressed */ is_compressed = !get_bits1(&alac->gb); - if (hassize) + if (has_size) output_samples = get_bits_long(&alac->gb, 32); else output_samples = alac->max_samples_per_frame; @@ -358,25 +349,24 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, alac->nb_samples = output_samples; if (is_compressed) { - int16_t predictor_coef_table[2][32]; - int predictor_coef_num[2]; + int16_t lpc_coefs[2][32]; + int lpc_order[2]; int prediction_type[2]; - int prediction_quantitization[2]; - int ricemodifier[2]; + int lpc_quant[2]; + int rice_history_mult[2]; - interlacing_shift = get_bits(&alac->gb, 8); - interlacing_leftweight = get_bits(&alac->gb, 8); + decorr_shift = get_bits(&alac->gb, 8); + decorr_left_weight = get_bits(&alac->gb, 8); for (ch = 0; ch < channels; ch++) { - prediction_type[ch] = get_bits(&alac->gb, 4); - prediction_quantitization[ch] = get_bits(&alac->gb, 4); - - ricemodifier[ch] = get_bits(&alac->gb, 3); - predictor_coef_num[ch] = get_bits(&alac->gb, 5); + prediction_type[ch] = get_bits(&alac->gb, 4); + lpc_quant[ch] = get_bits(&alac->gb, 4); + rice_history_mult[ch] = get_bits(&alac->gb, 3); + lpc_order[ch] = get_bits(&alac->gb, 5); /* read the predictor table */ - for (i = 0; i < predictor_coef_num[ch]; i++) - predictor_coef_table[ch][i] = get_sbits(&alac->gb, 16); + for (i = 0; i < lpc_order[ch]; i++) + lpc_coefs[ch][i] = get_sbits(&alac->gb, 16); } if (alac->extra_bits) { @@ -386,11 +376,9 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, } } for (ch = 0; ch < channels; ch++) { - bastardized_rice_decompress(alac, - alac->predict_error_buffer[ch], - alac->nb_samples, - readsamplesize, - ricemodifier[ch] * alac->rice_history_mult / 4); + rice_decompress(alac, alac->predict_error_buffer[ch], + alac->nb_samples, bps, + rice_history_mult[ch] * alac->rice_history_mult / 4); /* adaptive FIR filter */ if (prediction_type[ch] == 15) { @@ -401,20 +389,16 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, * However, this prediction type is not currently used by the * reference encoder. */ - predictor_decompress_fir_adapt(alac->predict_error_buffer[ch], - alac->predict_error_buffer[ch], - alac->nb_samples, readsamplesize, - NULL, 31, 0); + lpc_prediction(alac->predict_error_buffer[ch], + alac->predict_error_buffer[ch], + alac->nb_samples, bps, NULL, 31, 0); } else if (prediction_type[ch] > 0) { av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n", prediction_type[ch]); } - predictor_decompress_fir_adapt(alac->predict_error_buffer[ch], - alac->output_samples_buffer[ch], - alac->nb_samples, readsamplesize, - predictor_coef_table[ch], - predictor_coef_num[ch], - prediction_quantitization[ch]); + lpc_prediction(alac->predict_error_buffer[ch], + alac->output_samples_buffer[ch], alac->nb_samples, + bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]); } } else { /* not compressed, easy case */ @@ -424,13 +408,13 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, } } alac->extra_bits = 0; - interlacing_shift = 0; - interlacing_leftweight = 0; + decorr_shift = 0; + decorr_left_weight = 0; } - if (channels == 2 && interlacing_leftweight) { + if (channels == 2 && decorr_left_weight) { decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples, - interlacing_shift, interlacing_leftweight); + decorr_shift, decorr_left_weight); } if (alac->extra_bits) { From bae83f2c746f770f92ab4761f4c7bf38ed5045c5 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 16:56:34 -0400 Subject: [PATCH 36/38] alac: change some data types to plain int --- libavcodec/alac.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 1c850986c8..7732f17bcf 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -263,8 +263,8 @@ static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out, } static void decorrelate_stereo(int32_t *buffer[2], - int nb_samples, uint8_t decorr_shift, - uint8_t decorr_left_weight) + int nb_samples, int decorr_shift, + int decorr_left_weight) { int i; @@ -298,10 +298,10 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, { ALACContext *alac = avctx->priv_data; int has_size; - unsigned int bps; + int bps; int is_compressed; - uint8_t decorr_shift; - uint8_t decorr_left_weight; + int decorr_shift; + int decorr_left_weight; uint32_t output_samples; int i, ch, ret; From f3e5a7844bbf13620ca4b6a5e19aa087c9141b15 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 16:57:22 -0400 Subject: [PATCH 37/38] alac: calculate buffer size outside the loop in allocate_buffers() --- libavcodec/alac.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 7732f17bcf..7c76c62dfe 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -505,9 +505,9 @@ static av_cold int alac_decode_close(AVCodecContext *avctx) static int allocate_buffers(ALACContext *alac) { int ch; - for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) { - int buf_size = alac->max_samples_per_frame * sizeof(int32_t); + int buf_size = alac->max_samples_per_frame * sizeof(int32_t); + for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) { FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch], buf_size, buf_alloc_fail); From eeb55f5f2f48dba3cb4530e9c65999471affe26e Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 9 Jul 2012 17:02:42 -0400 Subject: [PATCH 38/38] alac: cosmetics: general pretty-printing and comment clean up --- libavcodec/alac.c | 49 +++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 7c76c62dfe..310a1f0d5b 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -56,14 +56,11 @@ #define MAX_CHANNELS 8 typedef struct { - AVCodecContext *avctx; AVFrame frame; GetBitContext gb; - int channels; - /* buffers */ int32_t *predict_error_buffer[2]; int32_t *output_samples_buffer[2]; int32_t *extra_bits_buffer[2]; @@ -74,8 +71,8 @@ typedef struct { uint8_t rice_initial_history; uint8_t rice_limit; - int extra_bits; /**< number of extra bits beyond 16-bit */ - int nb_samples; /**< number of samples in the current frame */ + int extra_bits; /**< number of extra bits beyond 16-bit */ + int nb_samples; /**< number of samples in the current frame */ } ALACContext; enum RawDataBlockType { @@ -145,16 +142,15 @@ static void rice_decompress(ALACContext *alac, int32_t *output_buffer, int k; unsigned int x; - /* read k, that is bits as is */ + /* calculate rice param and decode next value */ k = av_log2((history >> 9) + 3); k = FFMIN(k, alac->rice_limit); x = decode_scalar(&alac->gb, k, bps); x += sign_modifier; sign_modifier = 0; - output_buffer[i] = (x >> 1) ^ -(x & 1); - /* now update the history */ + /* update the history */ if (x > 0xffff) history = 0xffff; else @@ -165,9 +161,9 @@ static void rice_decompress(ALACContext *alac, int32_t *output_buffer, if ((history < 128) && (i + 1 < nb_samples)) { int block_size; - k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */); + /* calculate rice param and decode block size */ + k = 7 - av_log2(history) + ((history + 16) >> 6); k = FFMIN(k, alac->rice_limit); - block_size = decode_scalar(&alac->gb, k, 16); if (block_size > 0) { @@ -181,10 +177,8 @@ static void rice_decompress(ALACContext *alac, int32_t *output_buffer, block_size * sizeof(*output_buffer)); i += block_size; } - if (block_size <= 0xffff) sign_modifier = 1; - history = 0; } } @@ -230,7 +224,6 @@ static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out, /* NOTE: 4 and 8 are very common cases that could be optimized. */ - /* general case */ for (i = lpc_order; i < nb_samples - 1; i++) { int j; int val = 0; @@ -238,13 +231,11 @@ static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out, int error_sign; int d = buffer_out[i - lpc_order]; - for (j = 0; j < lpc_order; j++) { + /* LPC prediction */ + for (j = 0; j < lpc_order; j++) val += (buffer_out[i - j] - d) * lpc_coefs[j]; - } - val = (val + (1 << (lpc_quant - 1))) >> lpc_quant; val += d + error_val; - buffer_out[i + 1] = sign_extend(val, bps); /* adapt LPC coefficients */ @@ -262,9 +253,8 @@ static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out, } } -static void decorrelate_stereo(int32_t *buffer[2], - int nb_samples, int decorr_shift, - int decorr_left_weight) +static void decorrelate_stereo(int32_t *buffer[2], int nb_samples, + int decorr_shift, int decorr_left_weight) { int i; @@ -282,8 +272,7 @@ static void decorrelate_stereo(int32_t *buffer[2], } } -static void append_extra_bits(int32_t *buffer[2], - int32_t *extra_bits_buffer[2], +static void append_extra_bits(int32_t *buffer[2], int32_t *extra_bits_buffer[2], int extra_bits, int channels, int nb_samples) { int i, ch; @@ -297,13 +286,9 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, int channels) { ALACContext *alac = avctx->priv_data; - int has_size; - int bps; - int is_compressed; - int decorr_shift; - int decorr_left_weight; + int has_size, bps, is_compressed, decorr_shift, decorr_left_weight, ret; uint32_t output_samples; - int i, ch, ret; + int i, ch; skip_bits(&alac->gb, 4); /* element instance tag */ skip_bits(&alac->gb, 12); /* unused header bits */ @@ -404,10 +389,11 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, /* not compressed, easy case */ for (i = 0; i < alac->nb_samples; i++) { for (ch = 0; ch < channels; ch++) { - alac->output_samples_buffer[ch][i] = get_sbits_long(&alac->gb, alac->sample_size); + alac->output_samples_buffer[ch][i] = + get_sbits_long(&alac->gb, alac->sample_size); } } - alac->extra_bits = 0; + alac->extra_bits = 0; decorr_shift = 0; decorr_left_weight = 0; } @@ -477,9 +463,10 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, ch += channels; } - if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) + if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) { av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", avpkt->size * 8 - get_bits_count(&alac->gb)); + } *got_frame_ptr = 1; *(AVFrame *)data = alac->frame;