~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/mpegaudio.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * The simplest mpeg audio layer 2 encoder
3
3
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
6
8
 * modify it under the terms of the GNU Lesser General Public
7
9
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
 
10
 * version 2.1 of the License, or (at your option) any later version.
9
11
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
12
 * FFmpeg is distributed in the hope that it will be useful,
11
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
15
 * Lesser General Public License for more details.
14
16
 *
15
17
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 * License along with FFmpeg; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
20
 */
19
 
 
 
21
 
20
22
/**
21
23
 * @file mpegaudio.c
22
24
 * The simplest mpeg audio layer 2 encoder.
23
25
 */
24
 
 
 
26
 
25
27
#include "avcodec.h"
 
28
#include "bitstream.h"
26
29
#include "mpegaudio.h"
27
30
 
28
31
/* currently, cannot change these constants (need to modify
29
32
   quantization stage) */
30
 
#define FRAC_BITS 15
31
 
#define WFRAC_BITS  14
32
33
#define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
33
34
#define FIX(a)   ((int)((a) * (1 << FRAC_BITS)))
34
35
 
50
51
    int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
51
52
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
52
53
    /* code to group 3 scale factors */
53
 
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];       
 
54
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
54
55
    int sblimit; /* number of used subbands */
55
56
    const unsigned char *alloc_table;
56
57
} MpegAudioContext;
80
81
    /* encoding freq */
81
82
    s->lsf = 0;
82
83
    for(i=0;i<3;i++) {
83
 
        if (mpa_freq_tab[i] == freq) 
 
84
        if (mpa_freq_tab[i] == freq)
84
85
            break;
85
86
        if ((mpa_freq_tab[i] / 2) == freq) {
86
87
            s->lsf = 1;
87
88
            break;
88
89
        }
89
90
    }
90
 
    if (i == 3)
 
91
    if (i == 3){
 
92
        av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
91
93
        return -1;
 
94
    }
92
95
    s->freq_index = i;
93
96
 
94
97
    /* encoding bitrate & frequency */
95
98
    for(i=0;i<15;i++) {
96
 
        if (mpa_bitrate_tab[s->lsf][1][i] == bitrate) 
 
99
        if (mpa_bitrate_tab[s->lsf][1][i] == bitrate)
97
100
            break;
98
101
    }
99
 
    if (i == 15)
 
102
    if (i == 15){
 
103
        av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
100
104
        return -1;
 
105
    }
101
106
    s->bitrate_index = i;
102
107
 
103
108
    /* compute total header size & pad bit */
104
 
    
 
109
 
105
110
    a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
106
111
    s->frame_size = ((int)a) * 8;
107
112
 
108
113
    /* frame fractional size to compute padding */
109
114
    s->frame_frac = 0;
110
115
    s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
111
 
    
 
116
 
112
117
    /* select the right allocation table */
113
118
    table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
114
119
 
117
122
    s->alloc_table = alloc_tables[table];
118
123
 
119
124
#ifdef DEBUG
120
 
    printf("%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n", 
 
125
    av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
121
126
           bitrate, freq, s->frame_size, table, s->frame_frac_incr);
122
127
#endif
123
128
 
160
165
            v = 2;
161
166
        else if (v < 3)
162
167
            v = 3;
163
 
        else 
 
168
        else
164
169
            v = 4;
165
170
        scale_diff_table[i] = v;
166
171
    }
167
172
 
168
173
    for(i=0;i<17;i++) {
169
174
        v = quant_bits[i];
170
 
        if (v < 0) 
 
175
        if (v < 0)
171
176
            v = -v;
172
177
        else
173
178
            v = v * 3;
188
193
    const int *xp = costab32;
189
194
 
190
195
    for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
191
 
    
 
196
 
192
197
    t = tab + 30;
193
198
    t1 = tab + 2;
194
199
    do {
206
211
        t[3] += t[3-8];
207
212
        t -= 8;
208
213
    } while (t != t1);
209
 
    
 
214
 
210
215
    t = tab;
211
216
    t1 = tab + 32;
212
217
    do {
213
 
        t[ 3] = -t[ 3];    
214
 
        t[ 6] = -t[ 6];    
215
 
        
216
 
        t[11] = -t[11];    
217
 
        t[12] = -t[12];    
218
 
        t[13] = -t[13];    
219
 
        t[15] = -t[15]; 
 
218
        t[ 3] = -t[ 3];
 
219
        t[ 6] = -t[ 6];
 
220
 
 
221
        t[11] = -t[11];
 
222
        t[12] = -t[12];
 
223
        t[13] = -t[13];
 
224
        t[15] = -t[15];
220
225
        t += 16;
221
226
    } while (t != t1);
222
227
 
223
 
    
 
228
 
224
229
    t = tab;
225
230
    t1 = tab + 8;
226
231
    do {
227
232
        int x1, x2, x3, x4;
228
 
        
 
233
 
229
234
        x3 = MUL(t[16], FIX(SQRT2*0.5));
230
235
        x4 = t[0] - x3;
231
236
        x3 = t[0] + x3;
232
 
        
 
237
 
233
238
        x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
234
239
        x1 = MUL((t[8] - x2), xp[0]);
235
240
        x2 = MUL((t[8] + x2), xp[1]);
252
257
        xr = MUL(t[4],xp[1]);
253
258
        t[ 4] = (t[24] - xr);
254
259
        t[24] = (t[24] + xr);
255
 
        
 
260
 
256
261
        xr = MUL(t[20],xp[2]);
257
262
        t[20] = (t[8] - xr);
258
263
        t[ 8] = (t[8] + xr);
259
 
            
 
264
 
260
265
        xr = MUL(t[12],xp[3]);
261
266
        t[12] = (t[16] - xr);
262
267
        t[16] = (t[16] + xr);
268
273
        xr = MUL(tab[30-i*4],xp[0]);
269
274
        tab[30-i*4] = (tab[i*4] - xr);
270
275
        tab[   i*4] = (tab[i*4] + xr);
271
 
        
 
276
 
272
277
        xr = MUL(tab[ 2+i*4],xp[1]);
273
278
        tab[ 2+i*4] = (tab[28-i*4] - xr);
274
279
        tab[28-i*4] = (tab[28-i*4] + xr);
275
 
        
 
280
 
276
281
        xr = MUL(tab[31-i*4],xp[0]);
277
282
        tab[31-i*4] = (tab[1+i*4] - xr);
278
283
        tab[ 1+i*4] = (tab[1+i*4] + xr);
279
 
        
 
284
 
280
285
        xr = MUL(tab[ 3+i*4],xp[1]);
281
286
        tab[ 3+i*4] = (tab[29-i*4] - xr);
282
287
        tab[29-i*4] = (tab[29-i*4] + xr);
283
 
        
 
288
 
284
289
        xp += 2;
285
290
    }
286
291
 
349
354
        out += 32;
350
355
        /* handle the wrap around */
351
356
        if (offset < 0) {
352
 
            memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32), 
 
357
            memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
353
358
                    s->samples_buf[ch], (512 - 32) * 2);
354
359
            offset = SAMPLES_BUF_SIZE - 512;
355
360
        }
360
365
}
361
366
 
362
367
static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
363
 
                                  unsigned char scale_factors[SBLIMIT][3], 
 
368
                                  unsigned char scale_factors[SBLIMIT][3],
364
369
                                  int sb_samples[3][12][SBLIMIT],
365
370
                                  int sblimit)
366
371
{
367
372
    int *p, vmax, v, n, i, j, k, code;
368
373
    int index, d1, d2;
369
374
    unsigned char *sf = &scale_factors[0][0];
370
 
    
 
375
 
371
376
    for(j=0;j<sblimit;j++) {
372
377
        for(i=0;i<3;i++) {
373
378
            /* find the max absolute value */
382
387
            /* compute the scale factor index using log 2 computations */
383
388
            if (vmax > 0) {
384
389
                n = av_log2(vmax);
385
 
                /* n is the position of the MSB of vmax. now 
 
390
                /* n is the position of the MSB of vmax. now
386
391
                   use at most 2 compares to find the index */
387
392
                index = (21 - n) * 3 - 3;
388
393
                if (index >= 0) {
396
401
            }
397
402
 
398
403
#if 0
399
 
            printf("%2d:%d in=%x %x %d\n", 
 
404
            printf("%2d:%d in=%x %x %d\n",
400
405
                   j, i, vmax, scale_factor_table[index], index);
401
406
#endif
402
407
            /* store the scale factor */
408
413
           are close enough to each other */
409
414
        d1 = scale_diff_table[sf[0] - sf[1] + 64];
410
415
        d2 = scale_diff_table[sf[1] - sf[2] + 64];
411
 
        
 
416
 
412
417
        /* handle the 25 cases */
413
418
        switch(d1 * 5 + d2) {
414
419
        case 0*5+0:
462
467
            sf[1] = sf[2] = sf[0];
463
468
            break;
464
469
        default:
465
 
            av_abort();
 
470
            assert(0); //cant happen
 
471
            code = 0;           /* kill warning */
466
472
        }
467
 
        
 
473
 
468
474
#if 0
469
 
        printf("%d: %2d %2d %2d %d %d -> %d\n", j, 
 
475
        printf("%d: %2d %2d %2d %d %d -> %d\n", j,
470
476
               sf[0], sf[1], sf[2], d1, d2, code);
471
477
#endif
472
478
        scale_code[j] = code;
494
500
/* Try to maximize the smr while using a number of bits inferior to
495
501
   the frame size. I tried to make the code simpler, faster and
496
502
   smaller than other encoders :-) */
497
 
static void compute_bit_allocation(MpegAudioContext *s, 
 
503
static void compute_bit_allocation(MpegAudioContext *s,
498
504
                                   short smr1[MPA_MAX_CHANNELS][SBLIMIT],
499
505
                                   unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
500
506
                                   int *padding)
508
514
    memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
509
515
    memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
510
516
    memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
511
 
    
 
517
 
512
518
    /* compute frame size and padding */
513
519
    max_frame_size = s->frame_size;
514
520
    s->frame_frac += s->frame_frac_incr;
543
549
            }
544
550
        }
545
551
#if 0
546
 
        printf("current=%d max=%d max_sb=%d alloc=%d\n", 
 
552
        printf("current=%d max=%d max_sb=%d alloc=%d\n",
547
553
               current_frame_size, max_frame_size, max_sb,
548
554
               bit_alloc[max_sb]);
549
 
#endif        
 
555
#endif
550
556
        if (max_sb < 0)
551
557
            break;
552
 
        
 
558
 
553
559
        /* find alloc table entry (XXX: not optimal, should use
554
560
           pointer table) */
555
561
        alloc = s->alloc_table;
564
570
        } else {
565
571
            /* increments bit allocation */
566
572
            b = bit_alloc[max_ch][max_sb];
567
 
            incr = total_quant_bits[alloc[b + 1]] - 
 
573
            incr = total_quant_bits[alloc[b + 1]] -
568
574
                total_quant_bits[alloc[b]];
569
575
        }
570
576
 
633
639
        }
634
640
        j += 1 << bit_alloc_bits;
635
641
    }
636
 
    
 
642
 
637
643
    /* scale codes */
638
644
    for(i=0;i<s->sblimit;i++) {
639
645
        for(ch=0;ch<s->nb_channels;ch++) {
640
 
            if (bit_alloc[ch][i]) 
 
646
            if (bit_alloc[ch][i])
641
647
                put_bits(p, 2, s->scale_code[ch][i]);
642
648
        }
643
649
    }
665
671
            }
666
672
        }
667
673
    }
668
 
    
 
674
 
669
675
    /* quantization & write sub band samples */
670
676
 
671
677
    for(k=0;k<3;k++) {
695
701
                                e = s->scale_factors[ch][i][k];
696
702
                                shift = scale_factor_shift[e];
697
703
                                mult = scale_factor_mult[e];
698
 
                                
 
704
 
699
705
                                /* normalize to P bits */
700
706
                                if (shift < 0)
701
707
                                    q1 = sample << (-shift);
712
718
                        bits = quant_bits[qindex];
713
719
                        if (bits < 0) {
714
720
                            /* group the 3 values to save bits */
715
 
                            put_bits(p, -bits, 
 
721
                            put_bits(p, -bits,
716
722
                                     q[0] + steps * (q[1] + steps * q[2]));
717
723
#if 0
718
 
                            printf("%d: gr1 %d\n", 
 
724
                            printf("%d: gr1 %d\n",
719
725
                                   i, q[0] + steps * (q[1] + steps * q[2]));
720
726
#endif
721
727
                        } else {
722
728
#if 0
723
 
                            printf("%d: gr3 %d %d %d\n", 
 
729
                            printf("%d: gr3 %d %d %d\n",
724
730
                                   i, q[0], q[1], q[2]);
725
 
#endif                               
 
731
#endif
726
732
                            put_bits(p, bits, q[0]);
727
733
                            put_bits(p, bits, q[1]);
728
734
                            put_bits(p, bits, q[2]);
730
736
                    }
731
737
                }
732
738
                /* next subband in alloc table */
733
 
                j += 1 << bit_alloc_bits; 
 
739
                j += 1 << bit_alloc_bits;
734
740
            }
735
741
        }
736
742
    }
744
750
}
745
751
 
746
752
static int MPA_encode_frame(AVCodecContext *avctx,
747
 
                            unsigned char *frame, int buf_size, void *data)
 
753
                            unsigned char *frame, int buf_size, void *data)
748
754
{
749
755
    MpegAudioContext *s = avctx->priv_data;
750
756
    short *samples = data;
757
763
    }
758
764
 
759
765
    for(i=0;i<s->nb_channels;i++) {
760
 
        compute_scale_factors(s->scale_code[i], s->scale_factors[i], 
 
766
        compute_scale_factors(s->scale_code[i], s->scale_factors[i],
761
767
                              s->sb_samples[i], s->sblimit);
762
768
    }
763
769
    for(i=0;i<s->nb_channels;i++) {
765
771
    }
766
772
    compute_bit_allocation(s, smr, bit_alloc, &padding);
767
773
 
768
 
    init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE, NULL, NULL);
 
774
    init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
769
775
 
770
776
    encode_frame(s, bit_alloc, padding);
771
 
    
 
777
 
772
778
    s->nb_samples += MPA_FRAME_SIZE;
773
779
    return pbBufPtr(&s->pb) - s->pb.buf;
774
780
}