comparison nellymoserenc.c @ 8731:6a1ef00ca991 libavcodec

Allocate trellis tables on heap only when needed.
author banan
date Mon, 02 Feb 2009 18:51:02 +0000
parents e9d9d946f213
children bf274494b66e
comparison
equal deleted inserted replaced
8730:cfb8849d0452 8731:6a1ef00ca991
42 #define BITSTREAM_WRITER_LE 42 #define BITSTREAM_WRITER_LE
43 #include "bitstream.h" 43 #include "bitstream.h"
44 44
45 #define POW_TABLE_SIZE (1<<11) 45 #define POW_TABLE_SIZE (1<<11)
46 #define POW_TABLE_OFFSET 3 46 #define POW_TABLE_OFFSET 3
47 #define OPT_SIZE ((1<<15) + 3000)
47 48
48 typedef struct NellyMoserEncodeContext { 49 typedef struct NellyMoserEncodeContext {
49 AVCodecContext *avctx; 50 AVCodecContext *avctx;
50 int last_frame; 51 int last_frame;
51 int bufsel; 52 int bufsel;
52 int have_saved; 53 int have_saved;
53 DSPContext dsp; 54 DSPContext dsp;
54 MDCTContext mdct_ctx; 55 MDCTContext mdct_ctx;
55 DECLARE_ALIGNED_16(float, mdct_out[NELLY_SAMPLES]); 56 DECLARE_ALIGNED_16(float, mdct_out[NELLY_SAMPLES]);
56 DECLARE_ALIGNED_16(float, buf[2][3 * NELLY_BUF_LEN]); ///< sample buffer 57 DECLARE_ALIGNED_16(float, buf[2][3 * NELLY_BUF_LEN]); ///< sample buffer
58 float (*opt )[NELLY_BANDS];
59 uint8_t (*path)[NELLY_BANDS];
57 } NellyMoserEncodeContext; 60 } NellyMoserEncodeContext;
58 61
59 static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0); 62 static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0);
60 63
61 static const uint8_t sf_lut[96] = { 64 static const uint8_t sf_lut[96] = {
147 /* Generate overlap window */ 150 /* Generate overlap window */
148 ff_sine_window_init(ff_sine_128, 128); 151 ff_sine_window_init(ff_sine_128, 128);
149 for (i = 0; i < POW_TABLE_SIZE; i++) 152 for (i = 0; i < POW_TABLE_SIZE; i++)
150 pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET); 153 pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
151 154
155 if (s->avctx->trellis) {
156 s->opt = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(float ));
157 s->path = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(uint8_t));
158 }
159
152 return 0; 160 return 0;
153 } 161 }
154 162
155 static av_cold int encode_end(AVCodecContext *avctx) 163 static av_cold int encode_end(AVCodecContext *avctx)
156 { 164 {
157 NellyMoserEncodeContext *s = avctx->priv_data; 165 NellyMoserEncodeContext *s = avctx->priv_data;
158 166
159 ff_mdct_end(&s->mdct_ctx); 167 ff_mdct_end(&s->mdct_ctx);
168
169 if (s->avctx->trellis) {
170 av_free(s->opt);
171 av_free(s->path);
172 }
173
160 return 0; 174 return 0;
161 } 175 }
162 176
163 #define find_best(val, table, LUT, LUT_add, LUT_size) \ 177 #define find_best(val, table, LUT, LUT_add, LUT_size) \
164 best_idx = \ 178 best_idx = \
182 idx_table[band] = best_idx; 196 idx_table[band] = best_idx;
183 power_idx += ff_nelly_delta_table[best_idx]; 197 power_idx += ff_nelly_delta_table[best_idx];
184 } 198 }
185 } 199 }
186 200
187 #define OPT_SIZE ((1<<15) + 3000)
188
189 static inline float distance(float x, float y, int band) 201 static inline float distance(float x, float y, int band)
190 { 202 {
191 //return pow(fabs(x-y), 2.0); 203 //return pow(fabs(x-y), 2.0);
192 float tmp = x - y; 204 float tmp = x - y;
193 return tmp * tmp; 205 return tmp * tmp;
196 static void get_exponent_dynamic(NellyMoserEncodeContext *s, float *cand, int *idx_table) 208 static void get_exponent_dynamic(NellyMoserEncodeContext *s, float *cand, int *idx_table)
197 { 209 {
198 int i, j, band, best_idx; 210 int i, j, band, best_idx;
199 float power_candidate, best_val; 211 float power_candidate, best_val;
200 212
201 float opt[NELLY_BANDS][OPT_SIZE]; 213 float (*opt )[NELLY_BANDS] = s->opt ;
202 int path[NELLY_BANDS][OPT_SIZE]; 214 uint8_t(*path)[NELLY_BANDS] = s->path;
203 215
204 for (i = 0; i < NELLY_BANDS * OPT_SIZE; i++) { 216 for (i = 0; i < NELLY_BANDS * OPT_SIZE; i++) {
205 opt[0][i] = INFINITY; 217 opt[0][i] = INFINITY;
206 } 218 }
207 219