[FFmpeg-soc] [soc]: r3452 - alacenc/alacenc.c

jai_menon subversion at mplayerhq.hu
Mon Aug 18 22:18:32 CEST 2008


Author: jai_menon
Date: Mon Aug 18 22:18:32 2008
New Revision: 3452

Log:
sync alacenc soc repo with code currently under review

Modified:
   alacenc/alacenc.c

Modified: alacenc/alacenc.c
==============================================================================
--- alacenc/alacenc.c	(original)
+++ alacenc/alacenc.c	Mon Aug 18 22:18:32 2008
@@ -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;
@@ -52,13 +57,13 @@ typedef struct LPCContext {
 } LPCContext;
 
 typedef struct AlacEncodeContext {
-    int channels;
-    int samplerate;
     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];
-    int32_t *predictor_buf;
+    int32_t sample_buf[MAX_CHANNELS][DEFAULT_FRAME_SIZE];
+    int32_t predictor_buf[DEFAULT_FRAME_SIZE];
     int interlacing_shift;
     int interlacing_leftweight;
     PutBitContext pbctx;
@@ -69,37 +74,15 @@ typedef struct AlacEncodeContext {
 } AlacEncodeContext;
 
 
-static void allocate_sample_buffers(AlacEncodeContext *s)
-{
-    int i = s->channels;
-
-    while(i) {
-        s->sample_buf[i-1] = av_mallocz(s->avctx->frame_size*sizeof(int32_t));
-        i--;
-    }
-    s->predictor_buf = av_mallocz(s->avctx->frame_size*sizeof(int32_t));
-}
-
-static void free_sample_buffers(AlacEncodeContext *s)
-{
-    int i = s->channels;
-
-    while(i) {
-        av_freep(&s->sample_buf[i-1]);
-        i--;
-    }
-    av_freep(&s->predictor_buf);
-}
-
 static void init_sample_buffers(AlacEncodeContext *s, int16_t *input_samples)
 {
     int ch, i;
 
-    for(ch=0;ch<s->channels;ch++) {
+    for(ch=0;ch<s->avctx->channels;ch++) {
         int16_t *sptr = input_samples + ch;
         for(i=0;i<s->avctx->frame_size;i++) {
             s->sample_buf[ch][i] = *sptr;
-            sptr += s->channels;
+            sptr += s->avctx->channels;
         }
     }
 }
@@ -133,7 +116,7 @@ static void encode_scalar(AlacEncodeCont
 
 static void write_frame_header(AlacEncodeContext *s, int is_verbatim)
 {
-    put_bits(&s->pbctx, 3,  s->channels-1);                 // No. of channels -1
+    put_bits(&s->pbctx, 3,  s->avctx->channels-1);          // No. of channels -1
     put_bits(&s->pbctx, 16, 0);                             // Seems to be zero
     put_bits(&s->pbctx, 1,  1);                             // Sample count is in the header
     put_bits(&s->pbctx, 2,  0);                             // FIXME: Wasted bytes field
@@ -147,7 +130,7 @@ static void calc_predictor_params(AlacEn
     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,
+    opt_order = ff_lpc_calc_coefs(&s->dspctx, s->sample_buf[ch], s->avctx->frame_size, s->min_prediction_order, s->max_prediction_order,
                                    ALAC_MAX_LPC_PRECISION, coefs, shift, 1, ORDER_METHOD_EST, ALAC_MAX_LPC_SHIFT, 1);
 
     s->lpc[ch].lpc_order = opt_order;
@@ -155,19 +138,83 @@ static void calc_predictor_params(AlacEn
     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;
+    int32_t lt, rt;
+    uint64_t sum[4];
+    uint64_t score[4];
+
+    /* calculate sum of 2nd order residual for each channel */
+    sum[0] = sum[1] = sum[2] = sum[3] = 0;
+    for(i=2; i<n; i++) {
+        lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
+        rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
+        sum[2] += FFABS((lt + rt) >> 1);
+        sum[3] += FFABS(lt - rt);
+        sum[0] += FFABS(lt);
+        sum[1] += FFABS(rt);
+    }
+
+    /* calculate score for each mode */
+    score[0] = sum[0] + sum[1];
+    score[1] = sum[0] + sum[3];
+    score[2] = sum[1] + sum[3];
+    score[3] = sum[2] + sum[3];
+
+    /* return mode with lowest score */
+    best = 0;
+    for(i=1; i<4; i++) {
+        if(score[i] < score[best]) {
+            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;
-    int i;
 
-    for(i=0; i<s->avctx->frame_size; i++) {
-        tmp = left[i];
-        left[i] = (tmp + right[i]) >> 1;
-        right[i] = tmp - right[i];
+    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;
     }
-    s->interlacing_leftweight = 1;
-    s->interlacing_shift = 1;
 }
 
 static void alac_linear_predictor(AlacEncodeContext *s, int ch)
@@ -177,11 +224,10 @@ static void alac_linear_predictor(AlacEn
 
     if(lpc.lpc_order == 31) {
         s->predictor_buf[0] = s->sample_buf[ch][0];
-        i = s->avctx->frame_size - 1;
-        while(i > 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];
-            i--;
-        }
+
         return;
     }
 
@@ -192,12 +238,10 @@ static void alac_linear_predictor(AlacEn
         int32_t *residual = s->predictor_buf;
 
         // generate warm-up samples
-        i = lpc.lpc_order;
         residual[0] = samples[0];
-        while(i > 0) {
+        for(i=1;i<=lpc.lpc_order;i++)
             residual[i] = samples[i] - samples[i-1];
-            i--;
-        }
+
         // perform lpc on remaining samples
         for(i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) {
             int sum = 0, res_val, j;
@@ -238,10 +282,10 @@ static void alac_linear_predictor(AlacEn
 static void alac_entropy_coder(AlacEncodeContext *s)
 {
     unsigned int history = s->rc.initial_history;
-    int sign_modifier = 0, i = 0, k;
+    int sign_modifier = 0, i, k;
     int32_t *samples = s->predictor_buf;
 
-    while(i < s->avctx->frame_size) {
+    for(i=0;i < s->avctx->frame_size;) {
         int x;
 
         k = av_log2((history >> 9) + 3);
@@ -291,7 +335,7 @@ static void write_compressed_frame(AlacE
     put_bits(&s->pbctx, 8, s->interlacing_shift);
     put_bits(&s->pbctx, 8, s->interlacing_leftweight);
 
-    for(i=0;i<s->channels;i++) {
+    for(i=0;i<s->avctx->channels;i++) {
 
         calc_predictor_params(s, i);
 
@@ -308,7 +352,7 @@ static void write_compressed_frame(AlacE
 
     // apply lpc and entropy coding to audio samples
 
-    for(i=0;i<s->channels;i++) {
+    for(i=0;i<s->avctx->channels;i++) {
         alac_linear_predictor(s, i);
         alac_entropy_coder(s);
     }
@@ -321,8 +365,6 @@ static av_cold int alac_encode_init(AVCo
 
     avctx->frame_size      = DEFAULT_FRAME_SIZE;
     avctx->bits_per_sample = DEFAULT_SAMPLE_SIZE;
-    s->channels            = avctx->channels;
-    s->samplerate          = avctx->sample_rate;
 
     if(avctx->sample_fmt != SAMPLE_FMT_S16) {
         av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
@@ -342,18 +384,18 @@ static av_cold int alac_encode_init(AVCo
     s->rc.rice_modifier   = 4;
 
     s->max_coded_frame_size = (ALAC_FRAME_HEADER_SIZE + ALAC_FRAME_FOOTER_SIZE +
-                               avctx->frame_size*s->channels*avctx->bits_per_sample)>>3;
+                               avctx->frame_size*avctx->channels*avctx->bits_per_sample)>>3;
 
-    s->write_sample_size  = avctx->bits_per_sample + s->channels - 1; // FIXME: consider wasted_bytes
+    s->write_sample_size  = avctx->bits_per_sample + avctx->channels - 1; // FIXME: consider wasted_bytes
 
     AV_WB32(alac_extradata,    ALAC_EXTRADATA_SIZE);
     AV_WB32(alac_extradata+4,  MKBETAG('a','l','a','c'));
     AV_WB32(alac_extradata+12, avctx->frame_size);
     AV_WB8 (alac_extradata+17, avctx->bits_per_sample);
-    AV_WB8 (alac_extradata+21, s->channels);
+    AV_WB8 (alac_extradata+21, avctx->channels);
     AV_WB32(alac_extradata+24, s->max_coded_frame_size);
-    AV_WB32(alac_extradata+28, s->samplerate*s->channels*avctx->bits_per_sample); // average bitrate
-    AV_WB32(alac_extradata+32, s->samplerate);
+    AV_WB32(alac_extradata+28, avctx->sample_rate*avctx->channels*avctx->bits_per_sample); // average bitrate
+    AV_WB32(alac_extradata+32, avctx->sample_rate);
 
     // Set relevant extradata fields
     if(s->compression_level > 0) {
@@ -362,6 +404,36 @@ static av_cold int alac_encode_init(AVCo
         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;
+    } else {
+        s->min_prediction_order = DEFAULT_MIN_PRED_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;
+    } else {
+        s->max_prediction_order = DEFAULT_MAX_PRED_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;
 
@@ -371,8 +443,6 @@ static av_cold int alac_encode_init(AVCo
     s->avctx = avctx;
     dsputil_init(&s->dspctx, avctx);
 
-    allocate_sample_buffers(s);
-
     return 0;
 }
 
@@ -381,25 +451,26 @@ static int alac_encode_frame(AVCodecCont
 {
     AlacEncodeContext *s = avctx->priv_data;
     PutBitContext *pb = &s->pbctx;
-    int i, out_bytes;
+    int i, out_bytes, verbatim_flag = 0;
 
     if(avctx->frame_size > DEFAULT_FRAME_SIZE) {
         av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\n");
         return -1;
     }
 
-    if(buf_size < s->max_coded_frame_size) {
+    if(buf_size < 2*s->max_coded_frame_size) {
         av_log(avctx, AV_LOG_ERROR, "buffer size is too small\n");
         return -1;
     }
 
+verbatim:
     init_put_bits(pb, frame, buf_size);
 
-    if(s->compression_level == 0) {
+    if((s->compression_level == 0) || verbatim_flag) {
         // Verbatim mode
         int16_t *samples = data;
         write_frame_header(s, 1);
-        for(i=0; i<avctx->frame_size*s->channels; i++) {
+        for(i=0; i<avctx->frame_size*avctx->channels; i++) {
             put_sbits(pb, 16, *samples++);
         }
     } else {
@@ -414,22 +485,13 @@ static int alac_encode_frame(AVCodecCont
 
     if(out_bytes > s->max_coded_frame_size) {
         /* frame too large. use verbatim mode */
-        int16_t *samples = data;
-        init_put_bits(pb, frame, buf_size);
-        write_frame_header(s, 0);
-
-        for(i=0; i<avctx->frame_size*s->channels; i++) {
-            put_sbits(pb, 16, *samples++);
-        }
-        put_bits(pb, 3, 7);
-        flush_put_bits(pb);
-        out_bytes = put_bits_count(pb) >> 3;
-
-        if(out_bytes > s->max_coded_frame_size || out_bytes >= buf_size) {
+        if(verbatim_flag || (s->compression_level == 0)) {
             /* still too large. must be an error. */
             av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
             return -1;
         }
+        verbatim_flag = 1;
+        goto verbatim;
     }
 
     return out_bytes;
@@ -437,12 +499,9 @@ static int alac_encode_frame(AVCodecCont
 
 static av_cold int alac_encode_close(AVCodecContext *avctx)
 {
-    AlacEncodeContext *s = avctx->priv_data;
-
     av_freep(&avctx->extradata);
     avctx->extradata_size = 0;
     av_freep(&avctx->coded_frame);
-    free_sample_buffers(s);
     return 0;
 }
 
@@ -455,5 +514,5 @@ AVCodec alac_encoder = {
     alac_encode_frame,
     alac_encode_close,
     .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
-    .long_name = "ALAC (Apple Lossless Audio Codec)",
+    .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
 };



More information about the FFmpeg-soc mailing list