[FFmpeg-devel] [PATCH] ALAC Encoder
Jai Menon
realityman
Tue Aug 19 17:18:09 CEST 2008
Hi,
On Tuesday 19 Aug 2008 2:08:32 am Michael Niedermayer wrote:
> On Tue, Aug 19, 2008 at 01:09:07AM +0530, Jai Menon wrote:
> > Hi,
> >
> > On Monday 18 Aug 2008 3:46:23 am Michael Niedermayer wrote:
>
> [...]
>
> > > > +
> > > > + sign_modifier = 0;
> > > > + if(x > 0xFFFF)
> > > > + history = 0xFFFF;
> > > > +
> > > > + if((history < 128) && (i < s->avctx->frame_size)) {
> > > > + unsigned int block_size = 0;
> > > > +
> > > >
> > > > + sign_modifier = 1;
> > >
> > > unused
> >
> > It is very much used actually. After we have coded a block of zeros, the
> > next scalar is non_zero and this sign modifier is applied to that.
>
> no, not this :)
>
> see:
> > + sign_modifier = 1;
>
> ^^^^^^^^^^^^^
>
> > + k = 7 - av_log2(history) + ((history + 16) >> 6);
> > +
> > + while((*samples == 0) && (i < s->avctx->frame_size)) {
> > + samples++;
> > + i++;
> > + block_size++;
> > + }
> > + encode_scalar(s, block_size, k, 16);
> > +
> > + sign_modifier = (block_size <= 0xFFFF);
>
> ^^^^^^^^^^^^^
>
Yeah, I see it now :-) duh!
Removed.
>
>
> [...]
>
> > Index: libavcodec/alacenc.c
> > ===================================================================
> > --- libavcodec/alacenc.c (revision 14824)
> > +++ libavcodec/alacenc.c (working copy)
> > @@ -38,6 +38,11 @@
> > #define ALAC_MAX_LPC_PRECISION 9
> > #define ALAC_MAX_LPC_SHIFT 9
> >
> > +#define ALAC_CHMODE_LEFT_RIGHT 0
> > +#define ALAC_CHMODE_LEFT_SIDE 1
> > +#define ALAC_CHMODE_RIGHT_SIDE 2
> > +#define ALAC_CHMODE_MID_SIDE 3
> > +
> > typedef struct RiceContext {
> > int history_mult;
> > int initial_history;
> > @@ -53,9 +58,12 @@
> >
> > typedef struct AlacEncodeContext {
> > int compression_level;
> > + int min_prediction_order;
> > + int max_prediction_order;
> > int max_coded_frame_size;
> > int write_sample_size;
> > int32_t sample_buf[MAX_CHANNELS][DEFAULT_FRAME_SIZE];
> > + int32_t predictor_buf[DEFAULT_FRAME_SIZE];
> > int interlacing_shift;
> > int interlacing_leftweight;
> > PutBitContext pbctx;
> > @@ -116,6 +124,20 @@
> > put_bits(&s->pbctx, 32, s->avctx->frame_size); // No. of
> > samples in the frame }
> >
> > +static void calc_predictor_params(AlacEncodeContext *s, int ch)
> > +{
> > + int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
> > + int shift[MAX_LPC_ORDER];
> > + int opt_order;
> > +
> > + opt_order = ff_lpc_calc_coefs(&s->dspctx, s->sample_buf[ch],
> > s->avctx->frame_size, DEFAULT_MIN_PRED_ORDER, DEFAULT_MAX_PRED_ORDER, +
> > ALAC_MAX_LPC_PRECISION, coefs, shift, 1,
> > ORDER_METHOD_EST, ALAC_MAX_LPC_SHIFT, 1); +
> > + s->lpc[ch].lpc_order = opt_order;
> > + s->lpc[ch].lpc_quant = shift[opt_order-1];
> > + memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1],
> > opt_order*sizeof(int)); +}
> > +
> > static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int
> > n) {
> > int i, best;
> > @@ -147,7 +169,163 @@
> > best = i;
> > }
> > }
> > + return best;
> > +}
> >
> > +static void alac_stereo_decorrelation(AlacEncodeContext *s)
> > +{
> > + int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
> > + int i, mode, n = s->avctx->frame_size;
> > + int32_t tmp;
> > +
> > + mode = estimate_stereo_mode(left, right, n);
> > +
> > + switch(mode)
> > + {
> > + case ALAC_CHMODE_LEFT_RIGHT:
> > + s->interlacing_leftweight = 0;
> > + s->interlacing_shift = 0;
> > + break;
> > +
> > + case ALAC_CHMODE_LEFT_SIDE:
> > + for(i=0; i<n; i++) {
> > + right[i] = left[i] - right[i];
> > + }
> > + s->interlacing_leftweight = 1;
> > + s->interlacing_shift = 0;
> > + break;
> > +
> > + case ALAC_CHMODE_RIGHT_SIDE:
> > + for(i=0; i<n; i++) {
> > + tmp = right[i];
> > + right[i] = left[i] - right[i];
> > + left[i] = tmp + (right[i] >> 31);
> > + }
> > + s->interlacing_leftweight = 1;
> > + s->interlacing_shift = 31;
> > + break;
> > +
> > + default:
> > + for(i=0; i<n; i++) {
> > + tmp = left[i];
> > + left[i] = (tmp + right[i]) >> 1;
> > + right[i] = tmp - right[i];
> > + }
> > + s->interlacing_leftweight = 1;
> > + s->interlacing_shift = 1;
> > + break;
> > + }
> > +}
>
> ok
>
> > +
> > +static void alac_linear_predictor(AlacEncodeContext *s, int ch)
> > +{
> > + int i;
> > + LPCContext lpc = s->lpc[ch];
> > +
> > + if(lpc.lpc_order == 31) {
> > + s->predictor_buf[0] = s->sample_buf[ch][0];
> > +
> > + for(i=1; i<s->avctx->frame_size; i++)
> > + s->predictor_buf[i] = s->sample_buf[ch][i] -
> > s->sample_buf[ch][i-1]; +
> > + return;
> > + }
> > +
> > + // generalised linear predictor
> > +
> > + if(lpc.lpc_order > 0) {
> > + int32_t *samples = s->sample_buf[ch];
> > + int32_t *residual = s->predictor_buf;
> > +
> > + // generate warm-up samples
> > + residual[0] = samples[0];
> > + for(i=1;i<=lpc.lpc_order;i++)
> > + residual[i] = samples[i] - samples[i-1];
> > +
> > + // perform lpc on remaining samples
> > + for(i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) {
> >
> > + int sum = 0, res_val, j;
> > +
> > + for (j = 0; j < lpc.lpc_order; j++) {
> > + sum += (samples[lpc.lpc_order-j] - samples[0]) *
> > + lpc.lpc_coeff[j];
> > + }
> >
> > + sum += (1 << (lpc.lpc_quant - 1));
>
> the added value can be use instead of 0 to initialize sum
Yeah, changed.
>
> > + sum >>= lpc.lpc_quant;
> > + sum += samples[0];
> > + residual[i] = samples[lpc.lpc_order+1] - sum;
> > + res_val = residual[i];
> > +
> >
> > + if(res_val) {
> > + int index = lpc.lpc_order - 1;
> > + int neg = (res_val < 0);
> > +
> > + while(index >= 0 && (neg ? (res_val < 0):(res_val > 0)))
> > { + int val = samples[0] - samples[lpc.lpc_order -
> > index]; + int sign = (val ? FFSIGN(val) : 0);
> > +
> > + if(neg)
> > + sign*=-1;
> > +
> > + lpc.lpc_coeff[index] -= sign;
> > + val *= sign;
> > + res_val -= ((val >> lpc.lpc_quant) *
> > + (lpc.lpc_order - index));
> > + index--;
> > + }
> > + }
>
> this part is also used in the decoder, thus could be factorized into
> a common function
> alac_entropy_coder() also has some common code with the decoder but it
> is maybe too much interleaved with non-common code to factorize.
>
I feel that the code in the encoder is cleaner. So when this is committed,
I'll see if I can split this code out from the encoder rather than the
decoder. But then we'll have to rename alac.c to alacdec.c and have a
separate file just for this one hunk ;-)
>
> [...]
>
> > @@ -226,6 +404,32 @@
> > AV_WB8(alac_extradata+20, s->rc.k_modifier);
> > }
> >
> > + if(avctx->min_prediction_order >= 0) {
> > + if(avctx->min_prediction_order < MIN_LPC_ORDER ||
> > + avctx->min_prediction_order > MAX_LPC_ORDER) {
> > + av_log(avctx, AV_LOG_ERROR, "invalid min prediction order:
> > %d\n", avctx->min_prediction_order); + return -1;
> > + }
> > +
> > + s->min_prediction_order = avctx->min_prediction_order;
> > + }
> > +
> > + if(avctx->max_prediction_order >= 0) {
> > + if(avctx->max_prediction_order < MIN_LPC_ORDER ||
> > + avctx->max_prediction_order > MAX_LPC_ORDER) {
> > + av_log(avctx, AV_LOG_ERROR, "invalid max prediction order:
> > %d\n", avctx->max_prediction_order); + return -1;
> > + }
> > +
> > + s->max_prediction_order = avctx->max_prediction_order;
> > + }
> > +
> > + if(s->max_prediction_order < s->min_prediction_order) {
> > + av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d
> > max=%d\n", + s->min_prediction_order,
> > s->max_prediction_order); + return -1;
> > + }
> > +
> > avctx->extradata = alac_extradata;
> > avctx->extradata_size = ALAC_EXTRADATA_SIZE;
> >
> > @@ -255,6 +459,9 @@
> > return -1;
> > }
> >
> > +verbatim:
> > + init_put_bits(pb, frame, buf_size);
> > +
> > if((s->compression_level == 0) || verbatim_flag) {
> > // Verbatim mode
> > int16_t *samples = data;
>
> ok
>
> btw, if you want a svn write account, send diego your wanted username
> and password (GPG encrypted!)
> Assuming that you agree to our svn policy and are willing to maintain your
> encoder after SOC.
>
I have replied to this separately.
Sadly, nobody was around to commit the previous stuff. The attached patch
addresses the issues.
Regards,
Jai Menon
-------------- next part --------------
A non-text attachment was scrubbed...
Name: alac_encoder.patch
Type: text/x-diff
Size: 8830 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20080819/4c243de4/attachment.patch>
More information about the ffmpeg-devel
mailing list