Revision e77fd066

View differences:

libavcodec/ac3enc.c
33 33
#include "ac3.h"
34 34
#include "audioconvert.h"
35 35

  
36
#define MDCT_NBITS 9
37
#define MDCT_SAMPLES (1 << MDCT_NBITS)
38

  
36 39
#define SCALE_FLOAT(a, bits) lrintf((a) * (float)(1 << (bits)))
37 40

  
41
#define FIX15(a) av_clip_int16(SCALE_FLOAT(a, 15))
42

  
43
typedef struct IComplex {
44
    int16_t re,im;
45
} IComplex;
46

  
38 47
typedef struct AC3EncodeContext {
39 48
    PutBitContext pb;                       ///< bitstream writer context
40 49

  
......
82 91
static int16_t xcos1[128];
83 92
static int16_t xsin1[128];
84 93

  
85
#define MDCT_NBITS 9
86
#define MDCT_SAMPLES (1 << MDCT_NBITS)
87

  
88
#define FIX15(a) av_clip_int16(SCALE_FLOAT(a, 15))
89

  
90
typedef struct IComplex {
91
    int16_t re,im;
92
} IComplex;
93

  
94 94
static av_cold void fft_init(int ln)
95 95
{
96 96
    int i, n, n2;
......
241 241
    }
242 242
}
243 243

  
244
/* compute log2(max(abs(tab[]))) */
245
static int log2_tab(int16_t *tab, int n)
246
{
247
    int i, v;
248

  
249
    v = 0;
250
    for (i = 0; i < n; i++)
251
        v |= abs(tab[i]);
252

  
253
    return av_log2(v);
254
}
255

  
256
static void lshift_tab(int16_t *tab, int n, int lshift)
257
{
258
    int i;
259

  
260
    if (lshift > 0) {
261
        for(i = 0; i < n; i++)
262
            tab[i] <<= lshift;
263
    } else if (lshift < 0) {
264
        lshift = -lshift;
265
        for (i = 0; i < n; i++)
266
            tab[i] >>= lshift;
267
    }
268
}
269

  
244 270
static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n)
245 271
{
246 272
    int sum, i;
......
571 597
    return 0;
572 598
}
573 599

  
574
static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
575
                                    int64_t *channel_layout)
576
{
577
    int ch_layout;
578

  
579
    if (channels < 1 || channels > AC3_MAX_CHANNELS)
580
        return -1;
581
    if ((uint64_t)*channel_layout > 0x7FF)
582
        return -1;
583
    ch_layout = *channel_layout;
584
    if (!ch_layout)
585
        ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
586
    if (av_get_channel_layout_nb_channels(ch_layout) != channels)
587
        return -1;
588

  
589
    s->lfe_on       = !!(ch_layout & AV_CH_LOW_FREQUENCY);
590
    s->channels     = channels;
591
    s->fbw_channels = channels - s->lfe_on;
592
    s->lfe_channel  = s->lfe_on ? s->fbw_channels : -1;
593
    if (s->lfe_on)
594
        ch_layout -= AV_CH_LOW_FREQUENCY;
595

  
596
    switch (ch_layout) {
597
    case AV_CH_LAYOUT_MONO:           s->channel_mode = AC3_CHMODE_MONO;   break;
598
    case AV_CH_LAYOUT_STEREO:         s->channel_mode = AC3_CHMODE_STEREO; break;
599
    case AV_CH_LAYOUT_SURROUND:       s->channel_mode = AC3_CHMODE_3F;     break;
600
    case AV_CH_LAYOUT_2_1:            s->channel_mode = AC3_CHMODE_2F1R;   break;
601
    case AV_CH_LAYOUT_4POINT0:        s->channel_mode = AC3_CHMODE_3F1R;   break;
602
    case AV_CH_LAYOUT_QUAD:
603
    case AV_CH_LAYOUT_2_2:            s->channel_mode = AC3_CHMODE_2F2R;   break;
604
    case AV_CH_LAYOUT_5POINT0:
605
    case AV_CH_LAYOUT_5POINT0_BACK:   s->channel_mode = AC3_CHMODE_3F2R;   break;
606
    default:
607
        return -1;
608
    }
609

  
610
    s->channel_map  = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
611
    *channel_layout = ch_layout;
612
    if (s->lfe_on)
613
        *channel_layout |= AV_CH_LOW_FREQUENCY;
614

  
615
    return 0;
616
}
617

  
618
static av_cold int AC3_encode_init(AVCodecContext *avctx)
619
{
620
    int freq = avctx->sample_rate;
621
    int bitrate = avctx->bit_rate;
622
    AC3EncodeContext *s = avctx->priv_data;
623
    int i, j, ch;
624
    int bw_code;
625

  
626
    avctx->frame_size = AC3_FRAME_SIZE;
627

  
628
    ac3_common_init();
629

  
630
    if (!avctx->channel_layout) {
631
        av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
632
                                      "encoder will guess the layout, but it "
633
                                      "might be incorrect.\n");
634
    }
635
    if (set_channel_info(s, avctx->channels, &avctx->channel_layout)) {
636
        av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
637
        return -1;
638
    }
639

  
640
    /* frequency */
641
    for (i = 0; i < 3; i++) {
642
        for (j = 0; j < 3; j++)
643
            if ((ff_ac3_sample_rate_tab[j] >> i) == freq)
644
                goto found;
645
    }
646
    return -1;
647
 found:
648
    s->sample_rate        = freq;
649
    s->bit_alloc.sr_shift = i;
650
    s->bit_alloc.sr_code  = j;
651
    s->bitstream_id       = 8 + s->bit_alloc.sr_shift;
652
    s->bitstream_mode     = 0; /* complete main audio service */
653

  
654
    /* bitrate & frame size */
655
    for (i = 0; i < 19; i++) {
656
        if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == bitrate)
657
            break;
658
    }
659
    if (i == 19)
660
        return -1;
661
    s->bit_rate        = bitrate;
662
    s->frame_size_code = i << 1;
663
    s->frame_size_min  = ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
664
    s->bits_written    = 0;
665
    s->samples_written = 0;
666
    s->frame_size      = s->frame_size_min;
667

  
668
    /* set bandwidth */
669
    if(avctx->cutoff) {
670
        /* calculate bandwidth based on user-specified cutoff frequency */
671
        int cutoff     = av_clip(avctx->cutoff, 1, s->sample_rate >> 1);
672
        int fbw_coeffs = cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
673
        bw_code        = av_clip((fbw_coeffs - 73) / 3, 0, 60);
674
    } else {
675
        /* use default bandwidth setting */
676
        /* XXX: should compute the bandwidth according to the frame
677
           size, so that we avoid annoying high frequency artifacts */
678
        bw_code = 50;
679
    }
680
    for(ch=0;ch<s->fbw_channels;ch++) {
681
        /* bandwidth for each channel */
682
        s->bandwidth_code[ch] = bw_code;
683
        s->nb_coefs[ch]       = bw_code * 3 + 73;
684
    }
685
    if (s->lfe_on)
686
        s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
687

  
688
    /* initial snr offset */
689
    s->coarse_snr_offset = 40;
690

  
691
    mdct_init(9);
692

  
693
    avctx->coded_frame= avcodec_alloc_frame();
694
    avctx->coded_frame->key_frame= 1;
695

  
696
    return 0;
697
}
698

  
699 600
/* output the AC-3 frame header */
700 601
static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
701 602
{
......
1027 928
    return r;
1028 929
}
1029 930

  
1030

  
1031
/* compute log2(max(abs(tab[]))) */
1032
static int log2_tab(int16_t *tab, int n)
1033
{
1034
    int i, v;
1035

  
1036
    v = 0;
1037
    for (i = 0; i < n; i++)
1038
        v |= abs(tab[i]);
1039

  
1040
    return av_log2(v);
1041
}
1042

  
1043
static void lshift_tab(int16_t *tab, int n, int lshift)
1044
{
1045
    int i;
1046

  
1047
    if (lshift > 0) {
1048
        for(i = 0; i < n; i++)
1049
            tab[i] <<= lshift;
1050
    } else if (lshift < 0) {
1051
        lshift = -lshift;
1052
        for (i = 0; i < n; i++)
1053
            tab[i] >>= lshift;
1054
    }
1055
}
1056

  
1057 931
/* fill the end of the frame and compute the two crcs */
1058 932
static int output_frame_end(AC3EncodeContext *s)
1059 933
{
......
1209 1083
    return 0;
1210 1084
}
1211 1085

  
1086
static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
1087
                                    int64_t *channel_layout)
1088
{
1089
    int ch_layout;
1090

  
1091
    if (channels < 1 || channels > AC3_MAX_CHANNELS)
1092
        return -1;
1093
    if ((uint64_t)*channel_layout > 0x7FF)
1094
        return -1;
1095
    ch_layout = *channel_layout;
1096
    if (!ch_layout)
1097
        ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
1098
    if (av_get_channel_layout_nb_channels(ch_layout) != channels)
1099
        return -1;
1100

  
1101
    s->lfe_on       = !!(ch_layout & AV_CH_LOW_FREQUENCY);
1102
    s->channels     = channels;
1103
    s->fbw_channels = channels - s->lfe_on;
1104
    s->lfe_channel  = s->lfe_on ? s->fbw_channels : -1;
1105
    if (s->lfe_on)
1106
        ch_layout -= AV_CH_LOW_FREQUENCY;
1107

  
1108
    switch (ch_layout) {
1109
    case AV_CH_LAYOUT_MONO:           s->channel_mode = AC3_CHMODE_MONO;   break;
1110
    case AV_CH_LAYOUT_STEREO:         s->channel_mode = AC3_CHMODE_STEREO; break;
1111
    case AV_CH_LAYOUT_SURROUND:       s->channel_mode = AC3_CHMODE_3F;     break;
1112
    case AV_CH_LAYOUT_2_1:            s->channel_mode = AC3_CHMODE_2F1R;   break;
1113
    case AV_CH_LAYOUT_4POINT0:        s->channel_mode = AC3_CHMODE_3F1R;   break;
1114
    case AV_CH_LAYOUT_QUAD:
1115
    case AV_CH_LAYOUT_2_2:            s->channel_mode = AC3_CHMODE_2F2R;   break;
1116
    case AV_CH_LAYOUT_5POINT0:
1117
    case AV_CH_LAYOUT_5POINT0_BACK:   s->channel_mode = AC3_CHMODE_3F2R;   break;
1118
    default:
1119
        return -1;
1120
    }
1121

  
1122
    s->channel_map  = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
1123
    *channel_layout = ch_layout;
1124
    if (s->lfe_on)
1125
        *channel_layout |= AV_CH_LOW_FREQUENCY;
1126

  
1127
    return 0;
1128
}
1129

  
1130
static av_cold int AC3_encode_init(AVCodecContext *avctx)
1131
{
1132
    int freq = avctx->sample_rate;
1133
    int bitrate = avctx->bit_rate;
1134
    AC3EncodeContext *s = avctx->priv_data;
1135
    int i, j, ch;
1136
    int bw_code;
1137

  
1138
    avctx->frame_size = AC3_FRAME_SIZE;
1139

  
1140
    ac3_common_init();
1141

  
1142
    if (!avctx->channel_layout) {
1143
        av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
1144
                                      "encoder will guess the layout, but it "
1145
                                      "might be incorrect.\n");
1146
    }
1147
    if (set_channel_info(s, avctx->channels, &avctx->channel_layout)) {
1148
        av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
1149
        return -1;
1150
    }
1151

  
1152
    /* frequency */
1153
    for (i = 0; i < 3; i++) {
1154
        for (j = 0; j < 3; j++)
1155
            if ((ff_ac3_sample_rate_tab[j] >> i) == freq)
1156
                goto found;
1157
    }
1158
    return -1;
1159
 found:
1160
    s->sample_rate        = freq;
1161
    s->bit_alloc.sr_shift = i;
1162
    s->bit_alloc.sr_code  = j;
1163
    s->bitstream_id       = 8 + s->bit_alloc.sr_shift;
1164
    s->bitstream_mode     = 0; /* complete main audio service */
1165

  
1166
    /* bitrate & frame size */
1167
    for (i = 0; i < 19; i++) {
1168
        if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == bitrate)
1169
            break;
1170
    }
1171
    if (i == 19)
1172
        return -1;
1173
    s->bit_rate        = bitrate;
1174
    s->frame_size_code = i << 1;
1175
    s->frame_size_min  = ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
1176
    s->bits_written    = 0;
1177
    s->samples_written = 0;
1178
    s->frame_size      = s->frame_size_min;
1179

  
1180
    /* set bandwidth */
1181
    if(avctx->cutoff) {
1182
        /* calculate bandwidth based on user-specified cutoff frequency */
1183
        int cutoff     = av_clip(avctx->cutoff, 1, s->sample_rate >> 1);
1184
        int fbw_coeffs = cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
1185
        bw_code        = av_clip((fbw_coeffs - 73) / 3, 0, 60);
1186
    } else {
1187
        /* use default bandwidth setting */
1188
        /* XXX: should compute the bandwidth according to the frame
1189
           size, so that we avoid annoying high frequency artifacts */
1190
        bw_code = 50;
1191
    }
1192
    for(ch=0;ch<s->fbw_channels;ch++) {
1193
        /* bandwidth for each channel */
1194
        s->bandwidth_code[ch] = bw_code;
1195
        s->nb_coefs[ch]       = bw_code * 3 + 73;
1196
    }
1197
    if (s->lfe_on)
1198
        s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
1199

  
1200
    /* initial snr offset */
1201
    s->coarse_snr_offset = 40;
1202

  
1203
    mdct_init(9);
1204

  
1205
    avctx->coded_frame= avcodec_alloc_frame();
1206
    avctx->coded_frame->key_frame= 1;
1207

  
1208
    return 0;
1209
}
1210

  
1212 1211
#ifdef TEST
1213 1212
/*************************************************************************/
1214 1213
/* TEST */

Also available in: Unified diff