Browse code

Remaining parts of Nellymoser encoder

Originally committed as revision 15138 to svn://svn.ffmpeg.org/ffmpeg/trunk

Bartlomiej Wolowiec authored on 2008/09/01 20:19:26
Showing 5 changed files
... ...
@@ -135,6 +135,7 @@ version <next>
135 135
 - floating point PCM encoder/decoder
136 136
 - MXF muxer
137 137
 - E-AC-3 support added to AC-3 decoder
138
+- Nellymoser ASAO encoder
138 139
 
139 140
 version 0.4.9-pre1:
140 141
 
... ...
@@ -392,7 +392,7 @@ following image formats are supported:
392 392
 @item MS IMA ADPCM           @tab  X  @tab  X
393 393
 @item Musepack               @tab     @tab  X
394 394
     @tab SV7 and SV8 are supported.
395
-@item Nellymoser ASAO        @tab     @tab  X
395
+@item Nellymoser ASAO        @tab  X  @tab  X
396 396
 @item Qdesign QDM2           @tab     @tab  X
397 397
     @tab There are still some distortions.
398 398
 @item QT IMA ADPCM           @tab  X  @tab  X
... ...
@@ -139,6 +139,7 @@ OBJS-$(CONFIG_MSRLE_DECODER)           += msrle.o
139 139
 OBJS-$(CONFIG_MSVIDEO1_DECODER)        += msvideo1.o
140 140
 OBJS-$(CONFIG_MSZH_DECODER)            += lcldec.o
141 141
 OBJS-$(CONFIG_NELLYMOSER_DECODER)      += nellymoserdec.o nellymoser.o mdct.o fft.o
142
+OBJS-$(CONFIG_NELLYMOSER_ENCODER)      += nellymoserenc.o nellymoser.o mdct.o fft.o
142 143
 OBJS-$(CONFIG_NUV_DECODER)             += nuv.o rtjpeg.o
143 144
 OBJS-$(CONFIG_PAM_ENCODER)             += pnmenc.o pnm.o
144 145
 OBJS-$(CONFIG_PBM_ENCODER)             += pnmenc.o pnm.o
... ...
@@ -200,7 +200,7 @@ void avcodec_register_all(void)
200 200
     REGISTER_DECODER (MP3ON4, mp3on4);
201 201
     REGISTER_DECODER (MPC7, mpc7);
202 202
     REGISTER_DECODER (MPC8, mpc8);
203
-    REGISTER_DECODER (NELLYMOSER, nellymoser);
203
+    REGISTER_ENCDEC  (NELLYMOSER, nellymoser);
204 204
     REGISTER_DECODER (QDM2, qdm2);
205 205
     REGISTER_DECODER (RA_144, ra_144);
206 206
     REGISTER_DECODER (RA_288, ra_288);
... ...
@@ -48,8 +48,12 @@
48 48
 typedef struct NellyMoserEncodeContext {
49 49
     AVCodecContext  *avctx;
50 50
     int             last_frame;
51
+    int             bufsel;
52
+    int             have_saved;
51 53
     DSPContext      dsp;
52 54
     MDCTContext     mdct_ctx;
55
+    DECLARE_ALIGNED_16(float, mdct_out[NELLY_SAMPLES]);
56
+    DECLARE_ALIGNED_16(float, buf[2][3 * NELLY_BUF_LEN]);     ///< sample buffer
53 57
 } NellyMoserEncodeContext;
54 58
 
55 59
 static float pow_table[POW_TABLE_SIZE];     ///< -pow(2, -i / 2048.0 - 3.0);
... ...
@@ -102,6 +106,22 @@ static const float quant_lut_mul[7] = { 0.0,  0.0,  2.0,  2.0,  5.0, 12.0,  36.6
102 102
 static const float quant_lut_add[7] = { 0.0,  0.0,  2.0,  7.0, 21.0, 56.0, 157.0 };
103 103
 static const uint8_t quant_lut_offset[8] = { 0, 0, 1, 4, 11, 32, 81, 230 };
104 104
 
105
+void apply_mdct(NellyMoserEncodeContext *s)
106
+{
107
+    DECLARE_ALIGNED_16(float, in_buff[NELLY_SAMPLES]);
108
+
109
+    memcpy(in_buff, s->buf[s->bufsel], NELLY_BUF_LEN * sizeof(float));
110
+    s->dsp.vector_fmul(in_buff, ff_sine_128, NELLY_BUF_LEN);
111
+    s->dsp.vector_fmul_reverse(in_buff + NELLY_BUF_LEN, s->buf[s->bufsel] + NELLY_BUF_LEN, ff_sine_128,
112
+                               NELLY_BUF_LEN);
113
+    ff_mdct_calc(&s->mdct_ctx, s->mdct_out, in_buff);
114
+
115
+    s->dsp.vector_fmul(s->buf[s->bufsel] + NELLY_BUF_LEN, ff_sine_128, NELLY_BUF_LEN);
116
+    s->dsp.vector_fmul_reverse(s->buf[s->bufsel] + 2 * NELLY_BUF_LEN, s->buf[1 - s->bufsel], ff_sine_128,
117
+                               NELLY_BUF_LEN);
118
+    ff_mdct_calc(&s->mdct_ctx, s->mdct_out + NELLY_BUF_LEN, s->buf[s->bufsel] + NELLY_BUF_LEN);
119
+}
120
+
105 121
 static av_cold int encode_init(AVCodecContext *avctx)
106 122
 {
107 123
     NellyMoserEncodeContext *s = avctx->priv_data;
... ...
@@ -146,6 +166,207 @@ static av_cold int encode_end(AVCodecContext *avctx)
146 146
     if (fabs(val - table[best_idx]) > fabs(val - table[best_idx + 1])) \
147 147
         best_idx++;
148 148
 
149
+static void get_exponent_greedy(NellyMoserEncodeContext *s, float *cand, int *idx_table)
150
+{
151
+    int band, best_idx, power_idx = 0;
152
+    float power_candidate;
153
+
154
+    //base exponent
155
+    find_best(cand[0], ff_nelly_init_table, sf_lut, -20, 96);
156
+    idx_table[0] = best_idx;
157
+    power_idx = ff_nelly_init_table[best_idx];
158
+
159
+    for (band = 1; band < NELLY_BANDS; band++) {
160
+        power_candidate = cand[band] - power_idx;
161
+        find_best(power_candidate, ff_nelly_delta_table, sf_delta_lut, 37, 78);
162
+        idx_table[band] = best_idx;
163
+        power_idx += ff_nelly_delta_table[best_idx];
164
+    }
165
+}
166
+
167
+#define OPT_SIZE ((1<<15) + 3000)
168
+
169
+static inline float distance(float x, float y, int band)
170
+{
171
+    //return pow(fabs(x-y), 2.0);
172
+    float tmp = x - y;
173
+    return tmp * tmp;
174
+}
175
+
176
+static void get_exponent_dynamic(NellyMoserEncodeContext *s, float *cand, int *idx_table)
177
+{
178
+    int i, j, band, best_idx;
179
+    float power_candidate, best_val;
180
+
181
+    float opt[NELLY_BANDS][OPT_SIZE];
182
+    int path[NELLY_BANDS][OPT_SIZE];
183
+
184
+    for (i = 0; i < NELLY_BANDS * OPT_SIZE; i++) {
185
+        opt[0][i] = INFINITY;
186
+    }
187
+
188
+    for (i = 0; i < 64; i++) {
189
+        opt[0][ff_nelly_init_table[i]] = distance(cand[0], ff_nelly_init_table[i], 0);
190
+        path[0][ff_nelly_init_table[i]] = i;
191
+    }
192
+
193
+    for (band = 1; band < NELLY_BANDS; band++) {
194
+        int q, c = 0;
195
+        float tmp;
196
+        int idx_min, idx_max, idx;
197
+        power_candidate = cand[band];
198
+        for (q = 1000; !c && q < OPT_SIZE; q <<= 2) {
199
+            idx_min = FFMAX(0, cand[band] - q);
200
+            idx_max = FFMIN(OPT_SIZE, cand[band - 1] + q);
201
+            for (i = FFMAX(0, cand[band - 1] - q); i < FFMIN(OPT_SIZE, cand[band - 1] + q); i++) {
202
+                if ( isinf(opt[band - 1][i]) )
203
+                    continue;
204
+                for (j = 0; j < 32; j++) {
205
+                    idx = i + ff_nelly_delta_table[j];
206
+                    if (idx > idx_max)
207
+                        break;
208
+                    if (idx >= idx_min) {
209
+                        tmp = opt[band - 1][i] + distance(idx, power_candidate, band);
210
+                        if (opt[band][idx] > tmp) {
211
+                            opt[band][idx] = tmp;
212
+                            path[band][idx] = j;
213
+                            c = 1;
214
+                        }
215
+                    }
216
+                }
217
+            }
218
+        }
219
+        assert(c); //FIXME
220
+    }
221
+
222
+    best_val = INFINITY;
223
+    best_idx = -1;
224
+    band = NELLY_BANDS - 1;
225
+    for (i = 0; i < OPT_SIZE; i++) {
226
+        if (best_val > opt[band][i]) {
227
+            best_val = opt[band][i];
228
+            best_idx = i;
229
+        }
230
+    }
231
+    for (band = NELLY_BANDS - 1; band >= 0; band--) {
232
+        idx_table[band] = path[band][best_idx];
233
+        if (band) {
234
+            best_idx -= ff_nelly_delta_table[path[band][best_idx]];
235
+        }
236
+    }
237
+}
238
+
239
+/**
240
+ * Encodes NELLY_SAMPLES samples. It assumes, that samples contains 3 * NELLY_BUF_LEN values
241
+ *  @param s               encoder context
242
+ *  @param output          output buffer
243
+ *  @param output_size     size of output buffer
244
+ */
245
+static void encode_block(NellyMoserEncodeContext *s, unsigned char *output, int output_size)
246
+{
247
+    PutBitContext pb;
248
+    int i, j, band, block, best_idx, power_idx = 0;
249
+    float power_val, coeff, coeff_sum;
250
+    float pows[NELLY_FILL_LEN];
251
+    int bits[NELLY_BUF_LEN], idx_table[NELLY_BANDS];
252
+    float cand[NELLY_BANDS];
253
+
254
+    apply_mdct(s);
255
+
256
+    init_put_bits(&pb, output, output_size * 8);
257
+
258
+    i = 0;
259
+    for (band = 0; band < NELLY_BANDS; band++) {
260
+        coeff_sum = 0;
261
+        for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
262
+            coeff_sum += s->mdct_out[i                ] * s->mdct_out[i                ]
263
+                       + s->mdct_out[i + NELLY_BUF_LEN] * s->mdct_out[i + NELLY_BUF_LEN];
264
+        }
265
+        cand[band] =
266
+            log(FFMAX(1.0, coeff_sum / (ff_nelly_band_sizes_table[band] << 7))) * 1024.0 / M_LN2;
267
+    }
268
+
269
+    if (s->avctx->trellis) {
270
+        get_exponent_dynamic(s, cand, idx_table);
271
+    } else {
272
+        get_exponent_greedy(s, cand, idx_table);
273
+    }
274
+
275
+    i = 0;
276
+    for (band = 0; band < NELLY_BANDS; band++) {
277
+        if (band) {
278
+            power_idx += ff_nelly_delta_table[idx_table[band]];
279
+            put_bits(&pb, 5, idx_table[band]);
280
+        } else {
281
+            power_idx = ff_nelly_init_table[idx_table[0]];
282
+            put_bits(&pb, 6, idx_table[0]);
283
+        }
284
+        power_val = pow_table[power_idx & 0x7FF] / (1 << ((power_idx >> 11) + POW_TABLE_OFFSET));
285
+        for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
286
+            s->mdct_out[i] *= power_val;
287
+            s->mdct_out[i + NELLY_BUF_LEN] *= power_val;
288
+            pows[i] = power_idx;
289
+        }
290
+    }
291
+
292
+    ff_nelly_get_sample_bits(pows, bits);
293
+
294
+    for (block = 0; block < 2; block++) {
295
+        for (i = 0; i < NELLY_FILL_LEN; i++) {
296
+            if (bits[i] > 0) {
297
+                const float *table = ff_nelly_dequantization_table + (1 << bits[i]) - 1;
298
+                coeff = s->mdct_out[block * NELLY_BUF_LEN + i];
299
+                best_idx =
300
+                    quant_lut[av_clip (
301
+                            coeff * quant_lut_mul[bits[i]] + quant_lut_add[bits[i]],
302
+                            quant_lut_offset[bits[i]],
303
+                            quant_lut_offset[bits[i]+1] - 1
304
+                            )];
305
+                if (fabs(coeff - table[best_idx]) > fabs(coeff - table[best_idx + 1]))
306
+                    best_idx++;
307
+
308
+                put_bits(&pb, bits[i], best_idx);
309
+            }
310
+        }
311
+        if (!block)
312
+            put_bits(&pb, NELLY_HEADER_BITS + NELLY_DETAIL_BITS - put_bits_count(&pb), 0);
313
+    }
314
+}
315
+
316
+static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size, void *data)
317
+{
318
+    NellyMoserEncodeContext *s = avctx->priv_data;
319
+    int16_t *samples = data;
320
+    int i;
321
+
322
+    if (s->last_frame)
323
+        return 0;
324
+
325
+    if (data) {
326
+        for (i = 0; i < avctx->frame_size; i++) {
327
+            s->buf[s->bufsel][i] = samples[i];
328
+        }
329
+        for (; i < NELLY_SAMPLES; i++) {
330
+            s->buf[s->bufsel][i] = 0;
331
+        }
332
+        s->bufsel = 1 - s->bufsel;
333
+        if (!s->have_saved) {
334
+            s->have_saved = 1;
335
+            return 0;
336
+        }
337
+    } else {
338
+        memset(s->buf[s->bufsel], 0, sizeof(s->buf[0][0]) * NELLY_BUF_LEN);
339
+        s->bufsel = 1 - s->bufsel;
340
+        s->last_frame = 1;
341
+    }
342
+
343
+    if (s->have_saved) {
344
+        encode_block(s, frame, buf_size);
345
+        return NELLY_BLOCK_LEN;
346
+    }
347
+    return 0;
348
+}
349
+
149 350
 AVCodec nellymoser_encoder = {
150 351
     .name = "nellymoser",
151 352
     .type = CODEC_TYPE_AUDIO,