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

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/ac3enc.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 AC3 encoder
3
3
 * Copyright (c) 2000 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
/**
24
26
//#define DEBUG
25
27
//#define DEBUG_BITALLOC
26
28
#include "avcodec.h"
27
 
 
 
29
#include "bitstream.h"
 
30
#include "crc.h"
28
31
#include "ac3.h"
29
32
 
30
33
typedef struct AC3EncodeContext {
37
40
    unsigned int bsid;
38
41
    unsigned int frame_size_min; /* minimum frame size in case rounding is necessary */
39
42
    unsigned int frame_size; /* current frame size in words */
 
43
    unsigned int bits_written;
 
44
    unsigned int samples_written;
40
45
    int halfratecod;
41
46
    unsigned int frmsizecod;
42
47
    unsigned int fscod; /* frequency */
46
51
    short last_samples[AC3_MAX_CHANNELS][256];
47
52
    unsigned int chbwcod[AC3_MAX_CHANNELS];
48
53
    int nb_coefs[AC3_MAX_CHANNELS];
49
 
    
 
54
 
50
55
    /* bitrate allocation control */
51
 
    int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod; 
 
56
    int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod;
52
57
    AC3BitAllocParameters bit_alloc;
53
58
    int csnroffst;
54
59
    int fgaincod[AC3_MAX_CHANNELS];
66
71
#define EXP_DIFF_THRESHOLD 1000
67
72
 
68
73
static void fft_init(int ln);
69
 
static void ac3_crc_init(void);
70
74
 
71
75
static inline int16_t fix15(float a)
72
76
{
74
78
    v = (int)(a * (float)(1 << 15));
75
79
    if (v < -32767)
76
80
        v = -32767;
77
 
    else if (v > 32767) 
 
81
    else if (v > 32767)
78
82
        v = 32767;
79
83
    return v;
80
84
}
83
87
{
84
88
    if ((b0 + 256) == b1) {
85
89
        a = 384 ;
86
 
    } else if (b0 > b1) { 
 
90
    } else if (b0 > b1) {
87
91
        a = a - 64;
88
92
        if (a < 0) a=0;
89
93
    }
95
99
    if (bin < 7) {
96
100
        if ((b0 + 256) == b1) {
97
101
            a = 384 ;
98
 
        } else if (b0 > b1) { 
 
102
        } else if (b0 > b1) {
99
103
            a = a - 64;
100
104
            if (a < 0) a=0;
101
105
        }
118
122
void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
119
123
                                   int8_t *exp, int start, int end,
120
124
                                   int snroffset, int fgain, int is_lfe,
121
 
                                   int deltbae,int deltnseg, 
 
125
                                   int deltbae,int deltnseg,
122
126
                                   uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba)
123
127
{
124
128
    int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
164
168
    /* excitation function */
165
169
    bndstrt = masktab[start];
166
170
    bndend = masktab[end-1] + 1;
167
 
    
 
171
 
168
172
    if (bndstrt == 0) {
169
173
        lowcomp = 0;
170
174
        lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
185
189
                }
186
190
            }
187
191
        }
188
 
    
 
192
 
189
193
        end1=bndend;
190
194
        if (end1 > 22) end1=22;
191
 
    
 
195
 
192
196
        for (bin = begin; bin < end1; bin++) {
193
197
            if (!(is_lfe && bin == 6))
194
198
                lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
195
 
        
 
199
 
196
200
            fastleak -= s->fdecay ;
197
201
            v = bndpsd[bin] - fgain;
198
202
            if (fastleak < v) fastleak = v;
199
 
        
 
203
 
200
204
            slowleak -= s->sdecay ;
201
205
            v = bndpsd[bin] - s->sgain;
202
206
            if (slowleak < v) slowleak = v;
203
 
        
 
207
 
204
208
            v=fastleak - lowcomp;
205
209
            if (slowleak > v) v=slowleak;
206
 
        
 
210
 
207
211
            excite[bin] = v;
208
212
        }
209
213
        begin = 22;
210
214
    } else {
211
215
        /* coupling channel */
212
216
        begin = bndstrt;
213
 
        
 
217
 
214
218
        fastleak = (s->cplfleak << 8) + 768;
215
219
        slowleak = (s->cplsleak << 8) + 768;
216
220
    }
261
265
    }
262
266
 
263
267
    /* compute bit allocation */
264
 
    
 
268
 
265
269
    i = start ;
266
270
    j = masktab[start] ;
267
271
    do {
337
341
/* do a 2^n point complex fft on 2^ln points. */
338
342
static void fft(IComplex *z, int ln)
339
343
{
340
 
    int j, l, np, np2;
341
 
    int nblocks, nloops;
 
344
    int        j, l, np, np2;
 
345
    int        nblocks, nloops;
342
346
    register IComplex *p,*q;
343
347
    int tmp_re, tmp_im;
344
348
 
361
365
    p=&z[0];
362
366
    j=(np >> 1);
363
367
    do {
364
 
        BF(p[0].re, p[0].im, p[1].re, p[1].im, 
 
368
        BF(p[0].re, p[0].im, p[1].re, p[1].im,
365
369
           p[0].re, p[0].im, p[1].re, p[1].im);
366
370
        p+=2;
367
371
    } while (--j != 0);
371
375
    p=&z[0];
372
376
    j=np >> 2;
373
377
    do {
374
 
        BF(p[0].re, p[0].im, p[2].re, p[2].im, 
 
378
        BF(p[0].re, p[0].im, p[2].re, p[2].im,
375
379
           p[0].re, p[0].im, p[2].re, p[2].im);
376
 
        BF(p[1].re, p[1].im, p[3].re, p[3].im, 
 
380
        BF(p[1].re, p[1].im, p[3].re, p[3].im,
377
381
           p[1].re, p[1].im, p[3].im, -p[3].re);
378
382
        p+=4;
379
383
    } while (--j != 0);
390
394
 
391
395
            BF(p->re, p->im, q->re, q->im,
392
396
               p->re, p->im, q->re, q->im);
393
 
            
 
397
 
394
398
            p++;
395
399
            q++;
396
400
            for(l = nblocks; l < np2; l += nblocks) {
412
416
static void mdct512(int32_t *out, int16_t *in)
413
417
{
414
418
    int i, re, im, re1, im1;
415
 
    int16_t rot[N]; 
 
419
    int16_t rot[N];
416
420
    IComplex x[N/4];
417
421
 
418
422
    /* shift to simplify computations */
420
424
        rot[i] = -in[i + 3*N/4];
421
425
    for(i=N/4;i<N;i++)
422
426
        rot[i] = in[i - N/4];
423
 
        
 
427
 
424
428
    /* pre rotation */
425
429
    for(i=0;i<N/4;i++) {
426
430
        re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1;
429
433
    }
430
434
 
431
435
    fft(x, MDCT_NBITS - 2);
432
 
  
 
436
 
433
437
    /* post rotation */
434
438
    for(i=0;i<N/4;i++) {
435
439
        re = x[i].re;
457
461
{
458
462
    int i, j;
459
463
    int exp_diff;
460
 
    
 
464
 
461
465
    /* estimate if the exponent variation & decide if they should be
462
466
       reused in the next frame */
463
467
    exp_strategy[0][ch] = EXP_NEW;
464
468
    for(i=1;i<NB_BLOCKS;i++) {
465
469
        exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2);
466
 
#ifdef DEBUG            
467
 
        printf("exp_diff=%d\n", exp_diff);
 
470
#ifdef DEBUG
 
471
        av_log(NULL, AV_LOG_DEBUG, "exp_diff=%d\n", exp_diff);
468
472
#endif
469
473
        if (exp_diff > EXP_DIFF_THRESHOLD)
470
474
            exp_strategy[i][ch] = EXP_NEW;
472
476
            exp_strategy[i][ch] = EXP_REUSE;
473
477
    }
474
478
    if (is_lfe)
475
 
        return;
 
479
        return;
476
480
 
477
481
    /* now select the encoding strategy type : if exponents are often
478
482
       recoded, we use a coarse encoding */
493
497
            exp_strategy[i][ch] = EXP_D15;
494
498
            break;
495
499
        }
496
 
        i = j;
 
500
        i = j;
497
501
    }
498
502
}
499
503
 
507
511
            exp[i] = exp1[i];
508
512
    }
509
513
}
510
 
                                 
 
514
 
511
515
/* update the exponents so that they are the ones the decoder will
512
516
   decode. Return the number of bits used to code the exponents */
513
 
static int encode_exp(uint8_t encoded_exp[N/2], 
514
 
                      uint8_t exp[N/2], 
 
517
static int encode_exp(uint8_t encoded_exp[N/2],
 
518
                      uint8_t exp[N/2],
515
519
                      int nb_exps,
516
520
                      int exp_strategy)
517
521
{
518
 
    int group_size, nb_groups, i, j, k, recurse, exp_min, delta;
 
522
    int group_size, nb_groups, i, j, k, exp_min;
519
523
    uint8_t exp1[N/2];
520
524
 
521
525
    switch(exp_strategy) {
550
554
    if (exp1[0] > 15)
551
555
        exp1[0] = 15;
552
556
 
553
 
    /* Iterate until the delta constraints between each groups are
554
 
       satisfyed. I'm sure it is possible to find a better algorithm,
555
 
       but I am lazy */
556
 
    do {
557
 
        recurse = 0;
558
 
        for(i=1;i<=nb_groups;i++) {
559
 
            delta = exp1[i] - exp1[i-1];
560
 
            if (delta > 2) {
561
 
                /* if delta too big, we encode a smaller exponent */
562
 
                exp1[i] = exp1[i-1] + 2;
563
 
            } else if (delta < -2) {
564
 
                /* if delta is too small, we must decrease the previous
565
 
               exponent, which means we must recurse */
566
 
                recurse = 1;
567
 
                exp1[i-1] = exp1[i] + 2;
568
 
            }
569
 
        }
570
 
    } while (recurse);
571
 
    
 
557
    /* Decrease the delta between each groups to within 2
 
558
     * so that they can be differentially encoded */
 
559
    for (i=1;i<=nb_groups;i++)
 
560
        exp1[i] = FFMIN(exp1[i], exp1[i-1] + 2);
 
561
    for (i=nb_groups-1;i>=0;i--)
 
562
        exp1[i] = FFMIN(exp1[i], exp1[i+1] + 2);
 
563
 
572
564
    /* now we have the exponent values the decoder will see */
573
565
    encoded_exp[0] = exp1[0];
574
566
    k = 1;
578
570
        }
579
571
        k += group_size;
580
572
    }
581
 
    
 
573
 
582
574
#if defined(DEBUG)
583
 
    printf("exponents: strategy=%d\n", exp_strategy);
 
575
    av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy);
584
576
    for(i=0;i<=nb_groups * group_size;i++) {
585
 
        printf("%d ", encoded_exp[i]);
 
577
        av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]);
586
578
    }
587
 
    printf("\n");
 
579
    av_log(NULL, AV_LOG_DEBUG, "\n");
588
580
#endif
589
581
 
590
582
    return 4 + (nb_groups / 3) * 7;
604
596
            break;
605
597
        case 1:
606
598
            /* 3 mantissa in 5 bits */
607
 
            if (s->mant1_cnt == 0) 
 
599
            if (s->mant1_cnt == 0)
608
600
                bits += 5;
609
601
            if (++s->mant1_cnt == 3)
610
602
                s->mant1_cnt = 0;
611
603
            break;
612
604
        case 2:
613
605
            /* 3 mantissa in 7 bits */
614
 
            if (s->mant2_cnt == 0) 
 
606
            if (s->mant2_cnt == 0)
615
607
                bits += 7;
616
608
            if (++s->mant2_cnt == 3)
617
609
                s->mant2_cnt = 0;
623
615
            /* 2 mantissa in 7 bits */
624
616
            if (s->mant4_cnt == 0)
625
617
                bits += 7;
626
 
            if (++s->mant4_cnt == 2) 
 
618
            if (++s->mant4_cnt == 2)
627
619
                s->mant4_cnt = 0;
628
620
            break;
629
621
        case 14:
655
647
        s->mant2_cnt = 0;
656
648
        s->mant4_cnt = 0;
657
649
        for(ch=0;ch<s->nb_all_channels;ch++) {
658
 
            ac3_parametric_bit_allocation(&s->bit_alloc, 
659
 
                                          bap[i][ch], (int8_t *)encoded_exp[i][ch], 
660
 
                                          0, s->nb_coefs[ch], 
661
 
                                          (((csnroffst-15) << 4) + 
662
 
                                           fsnroffst) << 2, 
 
650
            ac3_parametric_bit_allocation(&s->bit_alloc,
 
651
                                          bap[i][ch], (int8_t *)encoded_exp[i][ch],
 
652
                                          0, s->nb_coefs[ch],
 
653
                                          (((csnroffst-15) << 4) +
 
654
                                           fsnroffst) << 2,
663
655
                                          fgaintab[s->fgaincod[ch]],
664
656
                                          ch == s->lfe_channel,
665
657
                                          2, 0, NULL, NULL, NULL);
666
 
            frame_bits += compute_mantissa_size(s, bap[i][ch], 
 
658
            frame_bits += compute_mantissa_size(s, bap[i][ch],
667
659
                                                 s->nb_coefs[ch]);
668
660
        }
669
661
    }
670
662
#if 0
671
 
    printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n", 
672
 
           csnroffst, fsnroffst, frame_bits, 
 
663
    printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
 
664
           csnroffst, fsnroffst, frame_bits,
673
665
           16 * s->frame_size - ((frame_bits + 7) & ~7));
674
666
#endif
675
667
    return 16 * s->frame_size - frame_bits;
694
686
    s->sgaincod = 1;
695
687
    s->dbkneecod = 2;
696
688
    s->floorcod = 4;
697
 
    for(ch=0;ch<s->nb_all_channels;ch++) 
 
689
    for(ch=0;ch<s->nb_all_channels;ch++)
698
690
        s->fgaincod[ch] = 4;
699
 
    
 
691
 
700
692
    /* compute real values */
701
693
    s->bit_alloc.fscod = s->fscod;
702
694
    s->bit_alloc.halfratecod = s->halfratecod;
705
697
    s->bit_alloc.sgain = sgaintab[s->sgaincod];
706
698
    s->bit_alloc.dbknee = dbkneetab[s->dbkneecod];
707
699
    s->bit_alloc.floor = floortab[s->floorcod];
708
 
    
 
700
 
709
701
    /* header size */
710
702
    frame_bits += 65;
711
703
    // if (s->acmod == 2)
715
707
    /* audio blocks */
716
708
    for(i=0;i<NB_BLOCKS;i++) {
717
709
        frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
718
 
        if (s->acmod == 2)
 
710
        if (s->acmod == 2) {
719
711
            frame_bits++; /* rematstr */
 
712
            if(i==0) frame_bits += 4;
 
713
        }
720
714
        frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */
721
 
        if (s->lfe)
722
 
            frame_bits++; /* lfeexpstr */
 
715
        if (s->lfe)
 
716
            frame_bits++; /* lfeexpstr */
723
717
        for(ch=0;ch<s->nb_channels;ch++) {
724
718
            if (exp_strategy[i][ch] != EXP_REUSE)
725
719
                frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
735
729
    /* (fsnoffset[4] + fgaincod[4]) * c */
736
730
    frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3);
737
731
 
 
732
    /* auxdatae, crcrsv */
 
733
    frame_bits += 2;
 
734
 
738
735
    /* CRC */
739
736
    frame_bits += 16;
740
737
 
742
739
       offset until we can pack everything in the requested frame size */
743
740
 
744
741
    csnroffst = s->csnroffst;
745
 
    while (csnroffst >= 0 && 
746
 
           bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
747
 
        csnroffst -= SNR_INC1;
 
742
    while (csnroffst >= 0 &&
 
743
           bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
 
744
        csnroffst -= SNR_INC1;
748
745
    if (csnroffst < 0) {
749
 
        fprintf(stderr, "Yack, Error !!!\n");
750
 
        return -1;
 
746
        av_log(NULL, AV_LOG_ERROR, "Bit allocation failed, try increasing the bitrate, -ab 384 for example!\n");
 
747
        return -1;
751
748
    }
752
 
    while ((csnroffst + SNR_INC1) <= 63 && 
753
 
           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 
 
749
    while ((csnroffst + SNR_INC1) <= 63 &&
 
750
           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
754
751
                     csnroffst + SNR_INC1, 0) >= 0) {
755
752
        csnroffst += SNR_INC1;
756
753
        memcpy(bap, bap1, sizeof(bap1));
757
754
    }
758
 
    while ((csnroffst + 1) <= 63 && 
 
755
    while ((csnroffst + 1) <= 63 &&
759
756
           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) {
760
757
        csnroffst++;
761
758
        memcpy(bap, bap1, sizeof(bap1));
762
759
    }
763
760
 
764
761
    fsnroffst = 0;
765
 
    while ((fsnroffst + SNR_INC1) <= 15 && 
766
 
           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 
 
762
    while ((fsnroffst + SNR_INC1) <= 15 &&
 
763
           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
767
764
                     csnroffst, fsnroffst + SNR_INC1) >= 0) {
768
765
        fsnroffst += SNR_INC1;
769
766
        memcpy(bap, bap1, sizeof(bap1));
770
767
    }
771
 
    while ((fsnroffst + 1) <= 15 && 
772
 
           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 
 
768
    while ((fsnroffst + 1) <= 15 &&
 
769
           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
773
770
                     csnroffst, fsnroffst + 1) >= 0) {
774
771
        fsnroffst++;
775
772
        memcpy(bap, bap1, sizeof(bap1));
776
773
    }
777
 
    
 
774
 
778
775
    s->csnroffst = csnroffst;
779
776
    for(ch=0;ch<s->nb_all_channels;ch++)
780
777
        s->fsnroffst[ch] = fsnroffst;
809
806
        for(j=0;j<v;j++) masktab[k++]=i;
810
807
        l += v;
811
808
    }
812
 
    bndtab[50] = 0;
 
809
    bndtab[50] = l;
813
810
}
814
811
 
815
812
 
822
819
    int i, j, ch;
823
820
    float alpha;
824
821
    static const uint8_t acmod_defs[6] = {
825
 
        0x01, /* C */
826
 
        0x02, /* L R */
827
 
        0x03, /* L C R */
828
 
        0x06, /* L R SL SR */
829
 
        0x07, /* L C R SL SR */
830
 
        0x07, /* L C R SL SR (+LFE) */
 
822
        0x01, /* C */
 
823
        0x02, /* L R */
 
824
        0x03, /* L C R */
 
825
        0x06, /* L R SL SR */
 
826
        0x07, /* L C R SL SR */
 
827
        0x07, /* L C R SL SR (+LFE) */
831
828
    };
832
829
 
833
830
    avctx->frame_size = AC3_FRAME_SIZE;
834
 
    
 
831
 
835
832
    /* number of channels */
836
833
    if (channels < 1 || channels > 6)
837
 
        return -1;
 
834
        return -1;
838
835
    s->acmod = acmod_defs[channels - 1];
839
836
    s->lfe = (channels == 6) ? 1 : 0;
840
837
    s->nb_all_channels = channels;
843
840
 
844
841
    /* frequency */
845
842
    for(i=0;i<3;i++) {
846
 
        for(j=0;j<3;j++) 
 
843
        for(j=0;j<3;j++)
847
844
            if ((ac3_freqs[j] >> i) == freq)
848
845
                goto found;
849
846
    }
850
847
    return -1;
851
 
 found:    
 
848
 found:
852
849
    s->sample_rate = freq;
853
850
    s->halfratecod = i;
854
851
    s->fscod = j;
866
863
    s->bit_rate = bitrate;
867
864
    s->frmsizecod = i << 1;
868
865
    s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16);
869
 
    /* for now we do not handle fractional sizes */
 
866
    s->bits_written = 0;
 
867
    s->samples_written = 0;
870
868
    s->frame_size = s->frame_size_min;
871
 
    
 
869
 
872
870
    /* bit allocation init */
873
871
    for(ch=0;ch<s->nb_channels;ch++) {
874
872
        /* bandwidth for each channel */
878
876
        s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37;
879
877
    }
880
878
    if (s->lfe) {
881
 
        s->nb_coefs[s->lfe_channel] = 7; /* fixed */
 
879
        s->nb_coefs[s->lfe_channel] = 7; /* fixed */
882
880
    }
883
881
    /* initial snr offset */
884
882
    s->csnroffst = 40;
893
891
        xsin1[i] = fix15(-sin(alpha));
894
892
    }
895
893
 
896
 
    ac3_crc_init();
897
 
    
898
894
    avctx->coded_frame= avcodec_alloc_frame();
899
895
    avctx->coded_frame->key_frame= 1;
900
896
 
904
900
/* output the AC3 frame header */
905
901
static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
906
902
{
907
 
    init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE, NULL, NULL);
 
903
    init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
908
904
 
909
905
    put_bits(&s->pb, 16, 0x0b77); /* frame header */
910
906
    put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
914
910
    put_bits(&s->pb, 3, s->bsmod);
915
911
    put_bits(&s->pb, 3, s->acmod);
916
912
    if ((s->acmod & 0x01) && s->acmod != 0x01)
917
 
        put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
 
913
        put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
918
914
    if (s->acmod & 0x04)
919
 
        put_bits(&s->pb, 2, 1); /* XXX -6 dB */
 
915
        put_bits(&s->pb, 2, 1); /* XXX -6 dB */
920
916
    if (s->acmod == 0x02)
921
917
        put_bits(&s->pb, 2, 0); /* surround not indicated */
922
918
    put_bits(&s->pb, 1, s->lfe); /* LFE */
986
982
    uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
987
983
    int delta0, delta1, delta2;
988
984
 
989
 
    for(ch=0;ch<s->nb_channels;ch++) 
 
985
    for(ch=0;ch<s->nb_channels;ch++)
990
986
        put_bits(&s->pb, 1, 0); /* 512 point MDCT */
991
 
    for(ch=0;ch<s->nb_channels;ch++) 
 
987
    for(ch=0;ch<s->nb_channels;ch++)
992
988
        put_bits(&s->pb, 1, 1); /* no dither */
993
989
    put_bits(&s->pb, 1, 0); /* no dynamic range */
994
990
    if (block_num == 0) {
1002
998
 
1003
999
    if (s->acmod == 2)
1004
1000
      {
1005
 
        if(block_num==0)
1006
 
          {
1007
 
            /* first block must define rematrixing (rematstr)  */
1008
 
            put_bits(&s->pb, 1, 1); 
1009
 
            
1010
 
            /* dummy rematrixing rematflg(1:4)=0 */
1011
 
            for (rbnd=0;rbnd<4;rbnd++)
1012
 
              put_bits(&s->pb, 1, 0); 
1013
 
          }
1014
 
        else 
1015
 
          {
1016
 
            /* no matrixing (but should be used in the future) */
1017
 
            put_bits(&s->pb, 1, 0);
1018
 
          } 
 
1001
        if(block_num==0)
 
1002
          {
 
1003
            /* first block must define rematrixing (rematstr)  */
 
1004
            put_bits(&s->pb, 1, 1);
 
1005
 
 
1006
            /* dummy rematrixing rematflg(1:4)=0 */
 
1007
            for (rbnd=0;rbnd<4;rbnd++)
 
1008
              put_bits(&s->pb, 1, 0);
 
1009
          }
 
1010
        else
 
1011
          {
 
1012
            /* no matrixing (but should be used in the future) */
 
1013
            put_bits(&s->pb, 1, 0);
 
1014
          }
1019
1015
      }
1020
1016
 
1021
 
#if defined(DEBUG) 
 
1017
#if defined(DEBUG)
1022
1018
    {
1023
1019
      static int count = 0;
1024
 
      printf("Block #%d (%d)\n", block_num, count++);
 
1020
      av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++);
1025
1021
    }
1026
1022
#endif
1027
1023
    /* exponent strategy */
1028
1024
    for(ch=0;ch<s->nb_channels;ch++) {
1029
1025
        put_bits(&s->pb, 2, exp_strategy[ch]);
1030
1026
    }
1031
 
    
 
1027
 
1032
1028
    if (s->lfe) {
1033
 
        put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
 
1029
        put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
1034
1030
    }
1035
1031
 
1036
1032
    for(ch=0;ch<s->nb_channels;ch++) {
1037
1033
        if (exp_strategy[ch] != EXP_REUSE)
1038
1034
            put_bits(&s->pb, 6, s->chbwcod[ch]);
1039
1035
    }
1040
 
    
 
1036
 
1041
1037
    /* exponents */
1042
1038
    for (ch = 0; ch < s->nb_all_channels; ch++) {
1043
1039
        switch(exp_strategy[ch]) {
1054
1050
            group_size = 4;
1055
1051
            break;
1056
1052
        }
1057
 
        nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
 
1053
        nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
1058
1054
        p = encoded_exp[ch];
1059
1055
 
1060
1056
        /* first exponent */
1082
1078
            put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
1083
1079
        }
1084
1080
 
1085
 
        if (ch != s->lfe_channel)
1086
 
            put_bits(&s->pb, 2, 0); /* no gain range info */
 
1081
        if (ch != s->lfe_channel)
 
1082
            put_bits(&s->pb, 2, 0); /* no gain range info */
1087
1083
    }
1088
1084
 
1089
1085
    /* bit allocation info */
1106
1102
            put_bits(&s->pb, 3, s->fgaincod[ch]);
1107
1103
        }
1108
1104
    }
1109
 
    
 
1105
 
1110
1106
    put_bits(&s->pb, 1, 0); /* no delta bit allocation */
1111
1107
    put_bits(&s->pb, 1, 0); /* no data to skip */
1112
1108
 
1207
1203
    /* second pass : output the values */
1208
1204
    for (ch = 0; ch < s->nb_all_channels; ch++) {
1209
1205
        int b, q;
1210
 
        
 
1206
 
1211
1207
        for(i=0;i<s->nb_coefs[ch];i++) {
1212
1208
            q = qmant[ch][i];
1213
1209
            b = bap[ch][i];
1215
1211
            case 0:
1216
1212
                break;
1217
1213
            case 1:
1218
 
                if (q != 128) 
 
1214
                if (q != 128)
1219
1215
                    put_bits(&s->pb, 5, q);
1220
1216
                break;
1221
1217
            case 2:
1222
 
                if (q != 128) 
 
1218
                if (q != 128)
1223
1219
                    put_bits(&s->pb, 7, q);
1224
1220
                break;
1225
1221
            case 3:
1243
1239
    }
1244
1240
}
1245
1241
 
1246
 
/* compute the ac3 crc */
1247
 
 
1248
1242
#define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1249
1243
 
1250
 
static void ac3_crc_init(void)
1251
 
{
1252
 
    unsigned int c, n, k;
1253
 
 
1254
 
    for(n=0;n<256;n++) {
1255
 
        c = n << 8;
1256
 
        for (k = 0; k < 8; k++) {
1257
 
            if (c & (1 << 15)) 
1258
 
                c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff);
1259
 
            else
1260
 
                c = c << 1;
1261
 
        }
1262
 
        crc_table[n] = c;
1263
 
    }
1264
 
}
1265
 
 
1266
 
static unsigned int ac3_crc(uint8_t *data, int n, unsigned int crc)
1267
 
{
1268
 
    int i;
1269
 
    for(i=0;i<n;i++) {
1270
 
        crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff;
1271
 
    }
1272
 
    return crc;
1273
 
}
1274
 
 
1275
1244
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
1276
1245
{
1277
1246
    unsigned int c;
1345
1314
    assert(n >= 0);
1346
1315
    if(n>0)
1347
1316
      memset(pbBufPtr(&s->pb), 0, n);
1348
 
    
 
1317
 
1349
1318
    /* Now we must compute both crcs : this is not so easy for crc1
1350
1319
       because it is at the beginning of the data... */
1351
1320
    frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
1352
 
    crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0);
 
1321
    crc1 = bswap_16(av_crc(av_crc8005, 0, frame + 4, 2 * frame_size_58 - 4));
1353
1322
    /* XXX: could precompute crc_inv */
1354
1323
    crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
1355
1324
    crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
1356
1325
    frame[2] = crc1 >> 8;
1357
1326
    frame[3] = crc1;
1358
 
    
1359
 
    crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0);
 
1327
 
 
1328
    crc2 = bswap_16(av_crc(av_crc8005, 0, frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2));
1360
1329
    frame[2*frame_size - 2] = crc2 >> 8;
1361
1330
    frame[2*frame_size - 1] = crc2;
1362
1331
 
1368
1337
                            unsigned char *frame, int buf_size, void *data)
1369
1338
{
1370
1339
    AC3EncodeContext *s = avctx->priv_data;
1371
 
    short *samples = data;
 
1340
    int16_t *samples = data;
1372
1341
    int i, j, k, v, ch;
1373
1342
    int16_t input_samples[N];
1374
1343
    int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
1393
1362
            for(j=0;j<N/2;j++) {
1394
1363
                v = *sptr;
1395
1364
                input_samples[j + N/2] = v;
1396
 
                s->last_samples[ch][j] = v; 
 
1365
                s->last_samples[ch][j] = v;
1397
1366
                sptr += sinc;
1398
1367
            }
1399
1368
 
1400
1369
            /* apply the MDCT window */
1401
1370
            for(j=0;j<N/2;j++) {
1402
 
                input_samples[j] = MUL16(input_samples[j], 
 
1371
                input_samples[j] = MUL16(input_samples[j],
1403
1372
                                         ac3_window[j]) >> 15;
1404
 
                input_samples[N-j-1] = MUL16(input_samples[N-j-1], 
 
1373
                input_samples[N-j-1] = MUL16(input_samples[N-j-1],
1405
1374
                                             ac3_window[j]) >> 15;
1406
1375
            }
1407
 
        
 
1376
 
1408
1377
            /* Normalize the samples to use the maximum available
1409
1378
               precision */
1410
1379
            v = 14 - log2_tab(input_samples, N);
1411
1380
            if (v < 0)
1412
1381
                v = 0;
1413
 
            exp_samples[i][ch] = v - 8;
 
1382
            exp_samples[i][ch] = v - 10;
1414
1383
            lshift_tab(input_samples, N, v);
1415
1384
 
1416
1385
            /* do the MDCT */
1417
1386
            mdct512(mdct_coef[i][ch], input_samples);
1418
 
            
 
1387
 
1419
1388
            /* compute "exponents". We take into account the
1420
1389
               normalization there */
1421
1390
            for(j=0;j<N/2;j++) {
1433
1402
                exp[i][ch][j] = e;
1434
1403
            }
1435
1404
        }
1436
 
        
 
1405
 
1437
1406
        compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel);
1438
1407
 
1439
1408
        /* compute the exponents as the decoder will see them. The
1447
1416
                j++;
1448
1417
            }
1449
1418
            frame_bits += encode_exp(encoded_exp[i][ch],
1450
 
                                     exp[i][ch], s->nb_coefs[ch], 
 
1419
                                     exp[i][ch], s->nb_coefs[ch],
1451
1420
                                     exp_strategy[i][ch]);
1452
1421
            /* copy encoded exponents for reuse case */
1453
1422
            for(k=i+1;k<j;k++) {
1454
 
                memcpy(encoded_exp[k][ch], encoded_exp[i][ch], 
 
1423
                memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
1455
1424
                       s->nb_coefs[ch] * sizeof(uint8_t));
1456
1425
            }
1457
1426
            i = j;
1458
1427
        }
1459
1428
    }
1460
1429
 
 
1430
    /* adjust for fractional frame sizes */
 
1431
    while(s->bits_written >= s->bit_rate*1000 && s->samples_written >= s->sample_rate) {
 
1432
        s->bits_written -= s->bit_rate*1000;
 
1433
        s->samples_written -= s->sample_rate;
 
1434
    }
 
1435
    s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate*1000);
 
1436
    s->bits_written += s->frame_size * 16;
 
1437
    s->samples_written += AC3_FRAME_SIZE;
 
1438
 
1461
1439
    compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
1462
1440
    /* everything is known... let's output the frame */
1463
1441
    output_frame_header(s, frame);
1464
 
        
 
1442
 
1465
1443
    for(i=0;i<NB_BLOCKS;i++) {
1466
 
        output_audio_block(s, exp_strategy[i], encoded_exp[i], 
 
1444
        output_audio_block(s, exp_strategy[i], encoded_exp[i],
1467
1445
                           bap[i], mdct_coef[i], exp_samples[i], i);
1468
1446
    }
1469
1447
    return output_frame_end(s);
1505
1483
            sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
1506
1484
            sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
1507
1485
        }
1508
 
        printf("%3d: %6d,%6d %6.0f,%6.0f\n", 
1509
 
               k, in[k].re, in[k].im, sum_re / FN, sum_im / FN); 
 
1486
        printf("%3d: %6d,%6d %6.0f,%6.0f\n",
 
1487
               k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
1510
1488
    }
1511
1489
}
1512
1490
 
1525
1503
    }
1526
1504
 
1527
1505
    mdct512(output, input);
1528
 
    
 
1506
 
1529
1507
    /* do it by hand */
1530
1508
    for(k=0;k<N/2;k++) {
1531
1509
        s = 0;
1535
1513
        }
1536
1514
        output1[k] = -2 * s / N;
1537
1515
    }
1538
 
    
 
1516
 
1539
1517
    err = 0;
1540
1518
    emax = 0;
1541
1519
    for(i=0;i<N/2;i++) {
1554
1532
    unsigned char frame[AC3_MAX_CODED_FRAME_SIZE];
1555
1533
    short samples[AC3_FRAME_SIZE];
1556
1534
    int ret, i;
1557
 
    
 
1535
 
1558
1536
    AC3_encode_init(&ctx, 44100, 64000, 1);
1559
1537
 
1560
1538
    fft_test();