[FFmpeg-soc] [soc]: r3693 - mlp/mlpenc.c

ramiro subversion at mplayerhq.hu
Sun Aug 31 22:56:06 CEST 2008


Author: ramiro
Date: Sun Aug 31 22:56:06 2008
New Revision: 3693

Log:
Dynamically allocate just enough memory for channel_params.

Modified:
   mlp/mlpenc.c

Modified: mlp/mlpenc.c
==============================================================================
--- mlp/mlpenc.c	(original)
+++ mlp/mlpenc.c	Sun Aug 31 22:56:06 2008
@@ -134,8 +134,10 @@ typedef struct {
                                      *   These values are correct for mono and stereo. */
 
     unsigned int    seq_size  [MAJOR_HEADER_INTERVAL];
+    unsigned int    seq_offset[MAJOR_HEADER_INTERVAL];
+    unsigned int    sequence_size;
 
-    ChannelParams   channel_params[MAJOR_HEADER_INTERVAL][MAJOR_HEADER_INTERVAL][MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS];
+    ChannelParams  *channel_params;
 
     BestOffset      best_offset[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS][NUM_CODEBOOKS];
 
@@ -160,6 +162,8 @@ typedef struct {
     ChannelParams  *prev_channel_params;
     DecodingParams *prev_decoding_params;
 
+    ChannelParams  *seq_channel_params;
+
     unsigned int    max_codebook_search;
 
     DSPContext      dsp;
@@ -331,6 +335,7 @@ static void copy_matrix_params(MatrixPar
 static void copy_restart_frame_params(MLPEncodeContext *ctx,
                                       unsigned int substr)
 {
+    ChannelParams (*seq_cp)[ctx->avctx->channels] = (ChannelParams (*)[ctx->avctx->channels]) ctx->seq_channel_params;
     unsigned int index;
 
     for (index = 0; index < ctx->number_of_frames + 1; index++) {
@@ -340,7 +345,7 @@ static void copy_restart_frame_params(ML
         copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
 
         for (channel = 0; channel < ctx->avctx->channels; channel++) {
-            ChannelParams *cp = &ctx->channel_params[ctx->seq_index][ctx->frame_index][index][channel];
+            ChannelParams *cp = &seq_cp[index][channel];
             unsigned int filter;
 
             dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
@@ -454,8 +459,10 @@ static av_cold int mlp_encode_init(AVCod
     unsigned int major_scratch_buffer_size;
     unsigned int lossless_check_data_size;
     unsigned int lpc_sample_buffer_size;
+    unsigned int channel_params_size;
     unsigned int frame_size_size;
     unsigned int substr, index;
+    unsigned int sum = 0;
 
     if (avctx->strict_std_compliance > -1 /* inofficial */) {
         av_log(avctx, AV_LOG_ERROR, "The bitstream generated by this encoder "
@@ -568,7 +575,18 @@ static av_cold int mlp_encode_init(AVCod
         return -1;
 
     for (index = 0; index < ctx->restart_intervals; index++) {
+        ctx->seq_offset[index] = sum;
         ctx->seq_size  [index] = ((index + 1) * ctx->min_restart_interval) + 1;
+        sum += ctx->seq_size[index];
+    }
+    ctx->sequence_size = sum;
+    channel_params_size = ctx->restart_intervals * ctx->sequence_size
+                        * ctx->avctx->channels * sizeof(ChannelParams);
+    ctx->channel_params = av_malloc(channel_params_size);
+    if (!ctx->channel_params) {
+        av_log(avctx, AV_LOG_ERROR,
+               "Not enough memory for analysis context.\n");
+        return -1;
     }
 
     for (substr = 0; substr < ctx->num_substreams; substr++) {
@@ -1933,6 +1951,7 @@ static int best_codebook_path_cost(MLPEn
 
 static void set_best_codebook(MLPEncodeContext *ctx)
 {
+    ChannelParams (*seq_cp)[ctx->avctx->channels] = (ChannelParams (*)[ctx->avctx->channels]) ctx->seq_channel_params;
     DecodingParams *dp = ctx->cur_decoding_params;
     RestartHeader *rh = ctx->cur_restart_header;
     unsigned int channel;
@@ -1998,7 +2017,7 @@ static void set_best_codebook(MLPEncodeC
 
         /* Update context. */
         for (index = 0; index < ctx->number_of_frames + 1; index++) {
-            ChannelParams *cp = &ctx->channel_params[ctx->seq_index][ctx->frame_index][index][channel];
+            ChannelParams *cp = &seq_cp[index][channel];
 
             best_codebook = *best_path++ - ZERO_PATH;
             cur_bo = &ctx->best_offset[index][channel][best_codebook];
@@ -2016,10 +2035,21 @@ static void set_best_codebook(MLPEncodeC
  */
 static void set_major_params(MLPEncodeContext *ctx)
 {
+    ChannelParams (*channel_params)[ctx->sequence_size][ctx->avctx->channels] =
+                 (ChannelParams (*)[ctx->sequence_size][ctx->avctx->channels]) ctx->channel_params;
     unsigned int index;
     unsigned int substr;
 
-    memcpy(ctx->major_channel_params, ctx->channel_params[ctx->restart_intervals-1][MAJOR_HEADER_INTERVAL-1], sizeof(ctx->major_channel_params));
+    {
+        ChannelParams (*seq_cp)[ctx->avctx->channels] =
+             (ChannelParams (*)[ctx->avctx->channels]) &channel_params[ctx->restart_intervals - 1][ctx->seq_offset[ctx->restart_intervals - 1]];
+        unsigned int channel;
+        for (index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
+            for (channel = 0; channel < ctx->avctx->channels; channel++) {
+                memcpy(&ctx->major_channel_params[index][channel], &seq_cp[index][channel], sizeof(ChannelParams));
+            }
+        }
+    }
     memcpy(ctx->major_decoding_params, ctx->decoding_params[ctx->restart_intervals-1][MAJOR_HEADER_INTERVAL-1], sizeof(ctx->major_decoding_params));
 
     for (substr = 0; substr < ctx->num_substreams; substr++) {
@@ -2043,6 +2073,7 @@ static void set_major_params(MLPEncodeCo
 
 static void analyze_sample_buffer(MLPEncodeContext *ctx)
 {
+    ChannelParams (*seq_cp)[ctx->avctx->channels] = (ChannelParams (*)[ctx->avctx->channels]) ctx->seq_channel_params;
     unsigned int index;
     unsigned int substr;
 
@@ -2058,7 +2089,7 @@ static void analyze_sample_buffer(MLPEnc
         ctx->decoding_params[ctx->seq_index][ctx->frame_index][1][substr].blocksize -= 8;
 
         ctx->cur_decoding_params = &ctx->decoding_params[ctx->seq_index][ctx->frame_index][1][substr];
-        ctx->cur_channel_params = ctx->channel_params[ctx->seq_index][ctx->frame_index][1];
+        ctx->cur_channel_params = seq_cp[1];
 
         generate_2_noise_channels(ctx);
         lossless_matrix_coeffs   (ctx);
@@ -2071,7 +2102,7 @@ static void analyze_sample_buffer(MLPEnc
 
         for (index = 0; index < ctx->number_of_frames + 1; index++) {
                 ctx->cur_decoding_params = &ctx->decoding_params[ctx->seq_index][ctx->frame_index][index][substr];
-                ctx->cur_channel_params = ctx->channel_params[ctx->seq_index][ctx->frame_index][index];
+                ctx->cur_channel_params = seq_cp[index];
                 ctx->cur_best_offset = ctx->best_offset[index];
                 determine_bits(ctx);
                 ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
@@ -2182,11 +2213,14 @@ input_and_return:
     restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
 
     if (!restart_frame) {
+    ChannelParams (*channel_params)[ctx->sequence_size][ctx->avctx->channels] =
+                 (ChannelParams (*)[ctx->sequence_size][ctx->avctx->channels]) ctx->channel_params;
     int seq_index;
 
     for (seq_index = 0;
          seq_index < ctx->restart_intervals && (seq_index * ctx->min_restart_interval) <= ctx->avctx->frame_number;
          seq_index++) {
+        ChannelParams (*seq_cp)[ctx->avctx->channels] = &channel_params[(ctx->frame_index / ctx->min_restart_interval)][ctx->seq_offset[seq_index]];
         unsigned int number_of_samples = 0;
         unsigned int index;
 
@@ -2197,6 +2231,7 @@ input_and_return:
         ctx->starting_frame_index = (ctx->avctx->frame_number - (ctx->avctx->frame_number % ctx->min_restart_interval)
                                   - (seq_index * ctx->min_restart_interval)) % ctx->max_restart_interval;
         ctx->number_of_frames = ctx->seq_size[seq_index] - 1;
+        ctx->seq_channel_params = (ChannelParams *) seq_cp;
 
         for (index = 0; index < ctx->number_of_frames; index++) {
             number_of_samples += ctx->frame_size[(ctx->starting_frame_index + index) % ctx->max_restart_interval];
@@ -2204,7 +2239,7 @@ input_and_return:
         ctx->number_of_samples = number_of_samples;
 
         for (index = 0; index < ctx->seq_size[seq_index]; index++) {
-            clear_channel_params(ctx, ctx->channel_params[ctx->seq_index][ctx->frame_index][index]);
+            clear_channel_params(ctx, seq_cp[index]);
             default_decoding_params(ctx, ctx->decoding_params[ctx->seq_index][ctx->frame_index][index]);
         }
 
@@ -2235,6 +2270,7 @@ static av_cold int mlp_encode_close(AVCo
     av_freep(&ctx->major_scratch_buffer);
     av_freep(&ctx->major_inout_buffer);
     av_freep(&ctx->lpc_sample_buffer);
+    av_freep(&ctx->channel_params);
     av_freep(&avctx->coded_frame);
     av_freep(&ctx->frame_size);
 



More information about the FFmpeg-soc mailing list