annotate mpegaudioenc.c @ 11559:444f4b594fdb libavcodec

Add spectral extension to the E-AC-3 decoder. Original patch by Justin, updated and resubmitted by Christophe Gisquet, christophe D gisquet A gmail
author cehoyos
date Tue, 30 Mar 2010 22:09:14 +0000
parents d4c97368f3e4
children 8a4984c5cacc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * The simplest mpeg audio layer 2 encoder
8629
04423b2f6e0b cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 8595
diff changeset
3 * Copyright (c) 2000, 2001 Fabrice Bellard
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
15 * Lesser General Public License for more details.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2979
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
21
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
22 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 8629
diff changeset
23 * @file libavcodec/mpegaudio.c
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
24 * The simplest mpeg audio layer 2 encoder.
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
25 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
26
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
27 #include "avcodec.h"
9411
4cb7c65fc775 Split bitstream.h, put the bitstream writer stuff in the new file
stefano
parents: 8718
diff changeset
28 #include "put_bits.h"
8595
2f476018b4ac Fix mpeg audio regression test failure
mru
parents: 7451
diff changeset
29
2f476018b4ac Fix mpeg audio regression test failure
mru
parents: 7451
diff changeset
30 #undef CONFIG_MPEGAUDIO_HP
2f476018b4ac Fix mpeg audio regression test failure
mru
parents: 7451
diff changeset
31 #define CONFIG_MPEGAUDIO_HP 0
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 #include "mpegaudio.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
33
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
34 /* currently, cannot change these constants (need to modify
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
35 quantization stage) */
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1057
diff changeset
36 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
84
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
37
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
38 #define SAMPLES_BUF_SIZE 4096
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
39
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
40 typedef struct MpegAudioContext {
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
41 PutBitContext pb;
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
42 int nb_channels;
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
43 int freq, bit_rate;
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
44 int lsf; /* 1 if mpeg2 low bitrate selected */
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
45 int bitrate_index; /* bit rate */
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
46 int freq_index;
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
47 int frame_size; /* frame size, in bits, without padding */
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1057
diff changeset
48 int64_t nb_samples; /* total number of samples encoded */
84
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
49 /* padding computation */
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
50 int frame_frac, frame_frac_incr, do_padding;
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
51 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
52 int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
53 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
54 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
55 /* code to group 3 scale factors */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
56 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
84
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
57 int sblimit; /* number of used subbands */
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
58 const unsigned char *alloc_table;
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
59 } MpegAudioContext;
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
60
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 /* define it to use floats in quantization (I don't like floats !) */
10328
d4c97368f3e4 Use floating point mathematics when encoding mpeg audio.
cehoyos
parents: 10145
diff changeset
62 #define USE_FLOATS
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
63
5031
70f194a2ee53 move some common mpeg audio tables from mpegaudiodectab.h to mpegaudiodata.c
aurel
parents: 4885
diff changeset
64 #include "mpegaudiodata.h"
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 #include "mpegaudiotab.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
66
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 5161
diff changeset
67 static av_cold int MPA_encode_init(AVCodecContext *avctx)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 MpegAudioContext *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 int freq = avctx->sample_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 int bitrate = avctx->bit_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 int channels = avctx->channels;
84
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
73 int i, v, table;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 float a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
75
4885
4f351b1e02bc check for channels<=0 and print a reasonable error message
alex
parents: 4472
diff changeset
76 if (channels <= 0 || channels > 2){
4f351b1e02bc check for channels<=0 and print a reasonable error message
alex
parents: 4472
diff changeset
77 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 return -1;
4885
4f351b1e02bc check for channels<=0 and print a reasonable error message
alex
parents: 4472
diff changeset
79 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 bitrate = bitrate / 1000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 s->nb_channels = channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 s->freq = freq;
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 s->bit_rate = bitrate * 1000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 avctx->frame_size = MPA_FRAME_SIZE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
85
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 /* encoding freq */
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 s->lsf = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 for(i=0;i<3;i++) {
5032
d47ee2d1d7da add proper prefix to extern mpeg audio data tables
aurel
parents: 5031
diff changeset
89 if (ff_mpa_freq_tab[i] == freq)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 break;
5032
d47ee2d1d7da add proper prefix to extern mpeg audio data tables
aurel
parents: 5031
diff changeset
91 if ((ff_mpa_freq_tab[i] / 2) == freq) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 s->lsf = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 }
2124
6f44ab5ad03c more info about why init failed
michael
parents: 1602
diff changeset
96 if (i == 3){
6f44ab5ad03c more info about why init failed
michael
parents: 1602
diff changeset
97 av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 return -1;
2124
6f44ab5ad03c more info about why init failed
michael
parents: 1602
diff changeset
99 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 s->freq_index = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
101
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 /* encoding bitrate & frequency */
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 for(i=0;i<15;i++) {
5032
d47ee2d1d7da add proper prefix to extern mpeg audio data tables
aurel
parents: 5031
diff changeset
104 if (ff_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 }
2124
6f44ab5ad03c more info about why init failed
michael
parents: 1602
diff changeset
107 if (i == 15){
6f44ab5ad03c more info about why init failed
michael
parents: 1602
diff changeset
108 av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 return -1;
2124
6f44ab5ad03c more info about why init failed
michael
parents: 1602
diff changeset
110 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 s->bitrate_index = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
112
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 /* compute total header size & pad bit */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
114
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 s->frame_size = ((int)a) * 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
117
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 /* frame fractional size to compute padding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 s->frame_frac = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
121
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 /* select the right allocation table */
5052
d981eb275c8f remove dependency of mpeg audio encoder over mpeg audio decoder
aurel
parents: 5032
diff changeset
123 table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
84
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
124
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 /* number of used subbands */
5032
d47ee2d1d7da add proper prefix to extern mpeg audio data tables
aurel
parents: 5031
diff changeset
126 s->sblimit = ff_mpa_sblimit_table[table];
d47ee2d1d7da add proper prefix to extern mpeg audio data tables
aurel
parents: 5031
diff changeset
127 s->alloc_table = ff_mpa_alloc_tables[table];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
128
9999
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9431
diff changeset
129 dprintf(avctx, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9431
diff changeset
130 bitrate, freq, s->frame_size, table, s->frame_frac_incr);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
131
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 for(i=0;i<s->nb_channels;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 s->samples_offset[i] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
134
84
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
135 for(i=0;i<257;i++) {
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
136 int v;
5032
d47ee2d1d7da add proper prefix to extern mpeg audio data tables
aurel
parents: 5031
diff changeset
137 v = ff_mpa_enwindow[i];
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
138 #if WFRAC_BITS != 16
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
139 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
140 #endif
84
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
141 filter_bank[i] = v;
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
142 if ((i & 63) != 0)
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
143 v = -v;
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
144 if (i != 0)
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
145 filter_bank[512 - i] = v;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 }
84
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
147
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 for(i=0;i<64;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 if (v <= 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 v = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 scale_factor_table[i] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 #ifdef USE_FLOATS
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 #else
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 #define P 15
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 scale_factor_shift[i] = 21 - P - (i / 3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 for(i=0;i<128;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 v = i - 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 if (v <= -3)
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 v = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 else if (v < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 v = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 else if (v == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 v = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 else if (v < 3)
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 v = 3;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
171 else
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 v = 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 scale_diff_table[i] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
175
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 for(i=0;i<17;i++) {
5032
d47ee2d1d7da add proper prefix to extern mpeg audio data tables
aurel
parents: 5031
diff changeset
177 v = ff_mpa_quant_bits[i];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
178 if (v < 0)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 v = -v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 v = v * 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 total_quant_bits[i] = 12 * v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
184
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 653
diff changeset
185 avctx->coded_frame= avcodec_alloc_frame();
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 653
diff changeset
186 avctx->coded_frame->key_frame= 1;
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 653
diff changeset
187
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
190
84
608c7f964bca merged code and tables between encoder and decoder
glantau
parents: 70
diff changeset
191 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
192 static void idct32(int *out, int *tab)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 int i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 int *t, *t1, xr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 const int *xp = costab32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
197
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
199
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 t = tab + 30;
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 t1 = tab + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 t[0] += t[-4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 t[1] += t[1 - 4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 t -= 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 } while (t != t1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
207
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 t = tab + 28;
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 t1 = tab + 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 t[0] += t[-8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 t[1] += t[1-8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 t[2] += t[2-8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 t[3] += t[3-8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 t -= 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 } while (t != t1);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
217
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 t = tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 t1 = tab + 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 do {
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
221 t[ 3] = -t[ 3];
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
222 t[ 6] = -t[ 6];
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
223
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
224 t[11] = -t[11];
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
225 t[12] = -t[12];
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
226 t[13] = -t[13];
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
227 t[15] = -t[15];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 t += 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 } while (t != t1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
230
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
231
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 t = tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 t1 = tab + 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 int x1, x2, x3, x4;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
236
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 x3 = MUL(t[16], FIX(SQRT2*0.5));
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 x4 = t[0] - x3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 x3 = t[0] + x3;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
240
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 x1 = MUL((t[8] - x2), xp[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 x2 = MUL((t[8] + x2), xp[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
244
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 t[ 0] = x3 + x1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 t[ 8] = x4 - x2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 t[16] = x4 + x2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 t[24] = x3 - x1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 t++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 } while (t != t1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
251
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 xp += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
253 t = tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 t1 = tab + 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 xr = MUL(t[28],xp[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 t[28] = (t[0] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 t[0] = (t[0] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
259
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 xr = MUL(t[4],xp[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 t[ 4] = (t[24] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 t[24] = (t[24] + xr);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
263
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 xr = MUL(t[20],xp[2]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 t[20] = (t[8] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 t[ 8] = (t[8] + xr);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
267
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 xr = MUL(t[12],xp[3]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 t[12] = (t[16] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 t[16] = (t[16] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 t++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 } while (t != t1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 xp += 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
274
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 for (i = 0; i < 4; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 xr = MUL(tab[30-i*4],xp[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 tab[30-i*4] = (tab[i*4] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 tab[ i*4] = (tab[i*4] + xr);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
279
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 xr = MUL(tab[ 2+i*4],xp[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 tab[ 2+i*4] = (tab[28-i*4] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 tab[28-i*4] = (tab[28-i*4] + xr);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
283
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 xr = MUL(tab[31-i*4],xp[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 tab[31-i*4] = (tab[1+i*4] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 tab[ 1+i*4] = (tab[1+i*4] + xr);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
287
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 xr = MUL(tab[ 3+i*4],xp[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 tab[ 3+i*4] = (tab[29-i*4] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 tab[29-i*4] = (tab[29-i*4] + xr);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
291
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 xp += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
294
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 t = tab + 30;
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 t1 = tab + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 xr = MUL(t1[0], *xp);
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 t1[0] = (t[0] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 t[0] = (t[0] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 t -= 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 t1 += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 xp++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 } while (t >= tab);
986e461dc072 Initial revision
glantau
parents:
diff changeset
305
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 for(i=0;i<32;i++) {
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
307 out[i] = tab[bitinv32[i]];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
310
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
311 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
312
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 short *p, *q;
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
316 int sum, offset, i, j;
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
317 int tmp[64];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 int tmp1[32];
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 int *out;
986e461dc072 Initial revision
glantau
parents:
diff changeset
320
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 // print_pow1(samples, 1152);
986e461dc072 Initial revision
glantau
parents:
diff changeset
322
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 offset = s->samples_offset[ch];
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 out = &s->sb_samples[ch][0][0][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 for(j=0;j<36;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 /* 32 samples at once */
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 for(i=0;i<32;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 s->samples_buf[ch][offset + (31 - i)] = samples[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 samples += incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
331
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 /* filter */
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 p = s->samples_buf[ch] + offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 q = filter_bank;
986e461dc072 Initial revision
glantau
parents:
diff changeset
335 /* maxsum = 23169 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
336 for(i=0;i<64;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 sum = p[0*64] * q[0*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 sum += p[1*64] * q[1*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
339 sum += p[2*64] * q[2*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
340 sum += p[3*64] * q[3*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 sum += p[4*64] * q[4*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 sum += p[5*64] * q[5*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 sum += p[6*64] * q[6*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
344 sum += p[7*64] * q[7*64];
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
345 tmp[i] = sum;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 p++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 q++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 }
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
349 tmp1[0] = tmp[16] >> WSHIFT;
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
350 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
351 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
352
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
353 idct32(out, tmp1);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
354
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 /* advance of 32 samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 offset -= 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 out += 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 /* handle the wrap around */
986e461dc072 Initial revision
glantau
parents:
diff changeset
359 if (offset < 0) {
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
360 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
361 s->samples_buf[ch], (512 - 32) * 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 offset = SAMPLES_BUF_SIZE - 512;
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
364 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 s->samples_offset[ch] = offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
366
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 // print_pow(s->sb_samples, 1152);
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
369
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
371 unsigned char scale_factors[SBLIMIT][3],
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 int sb_samples[3][12][SBLIMIT],
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 int sblimit)
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
375 int *p, vmax, v, n, i, j, k, code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 int index, d1, d2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
377 unsigned char *sf = &scale_factors[0][0];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
378
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
379 for(j=0;j<sblimit;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
380 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
381 /* find the max absolute value */
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 p = &sb_samples[i][0][j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
383 vmax = abs(*p);
986e461dc072 Initial revision
glantau
parents:
diff changeset
384 for(k=1;k<12;k++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
385 p += SBLIMIT;
986e461dc072 Initial revision
glantau
parents:
diff changeset
386 v = abs(*p);
986e461dc072 Initial revision
glantau
parents:
diff changeset
387 if (v > vmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
388 vmax = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
389 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
390 /* compute the scale factor index using log 2 computations */
6961
1cf8c883365f Prevent scalefactors from overflowing.
michael
parents: 6929
diff changeset
391 if (vmax > 1) {
70
c7767df463f4 log2 to av_log2
glantau
parents: 64
diff changeset
392 n = av_log2(vmax);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
393 /* n is the position of the MSB of vmax. now
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 use at most 2 compares to find the index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
395 index = (21 - n) * 3 - 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
396 if (index >= 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
397 while (vmax <= scale_factor_table[index+1])
986e461dc072 Initial revision
glantau
parents:
diff changeset
398 index++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
399 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
400 index = 0; /* very unlikely case of overflow */
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
402 } else {
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
403 index = 62; /* value 63 is not allowed */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
404 }
89
2e88e3afecd0 corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents: 84
diff changeset
405
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 #if 0
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
407 printf("%2d:%d in=%x %x %d\n",
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 j, i, vmax, scale_factor_table[index], index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
409 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 /* store the scale factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 assert(index >=0 && index <= 63);
986e461dc072 Initial revision
glantau
parents:
diff changeset
412 sf[i] = index;
986e461dc072 Initial revision
glantau
parents:
diff changeset
413 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
414
986e461dc072 Initial revision
glantau
parents:
diff changeset
415 /* compute the transmission factor : look if the scale factors
986e461dc072 Initial revision
glantau
parents:
diff changeset
416 are close enough to each other */
986e461dc072 Initial revision
glantau
parents:
diff changeset
417 d1 = scale_diff_table[sf[0] - sf[1] + 64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
418 d2 = scale_diff_table[sf[1] - sf[2] + 64];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
419
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
420 /* handle the 25 cases */
986e461dc072 Initial revision
glantau
parents:
diff changeset
421 switch(d1 * 5 + d2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
422 case 0*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
423 case 0*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
424 case 3*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
425 case 4*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
426 case 4*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
427 code = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
428 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
429 case 0*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
430 case 0*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
431 case 4*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
432 case 4*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
433 code = 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
434 sf[2] = sf[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
435 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
436 case 0*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
437 case 4*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
438 code = 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
439 sf[1] = sf[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
440 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
441 case 1*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
442 case 1*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
443 case 2*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
444 code = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
445 sf[1] = sf[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
446 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
447 case 1*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
448 case 1*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
449 case 2*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
450 case 2*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
451 case 2*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
452 code = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
453 sf[1] = sf[2] = sf[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
454 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
455 case 2*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
456 case 3*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
457 code = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
458 sf[0] = sf[1] = sf[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
459 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
460 case 3*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
461 case 3*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
462 case 3*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
463 code = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
464 sf[0] = sf[2] = sf[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
465 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
466 case 1*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
467 code = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
468 if (sf[0] > sf[2])
986e461dc072 Initial revision
glantau
parents:
diff changeset
469 sf[0] = sf[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
470 sf[1] = sf[2] = sf[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
471 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
472 default:
5127
4dbe6578f811 misc spelling fixes
diego
parents: 5052
diff changeset
473 assert(0); //cannot happen
2522
e25782262d7d kill warnings patch by (M«©ns Rullg«©rd <mru inprovide com>)
michael
parents: 2398
diff changeset
474 code = 0; /* kill warning */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
475 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
476
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
477 #if 0
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
478 printf("%d: %2d %2d %2d %d %d -> %d\n", j,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
479 sf[0], sf[1], sf[2], d1, d2, code);
986e461dc072 Initial revision
glantau
parents:
diff changeset
480 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
481 scale_code[j] = code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
482 sf += 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
483 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
484 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
485
986e461dc072 Initial revision
glantau
parents:
diff changeset
486 /* The most important function : psycho acoustic module. In this
986e461dc072 Initial revision
glantau
parents:
diff changeset
487 encoder there is basically none, so this is the worst you can do,
986e461dc072 Initial revision
glantau
parents:
diff changeset
488 but also this is the simpler. */
986e461dc072 Initial revision
glantau
parents:
diff changeset
489 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
986e461dc072 Initial revision
glantau
parents:
diff changeset
490 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
491 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
492
986e461dc072 Initial revision
glantau
parents:
diff changeset
493 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
494 smr[i] = (int)(fixed_smr[i] * 10);
986e461dc072 Initial revision
glantau
parents:
diff changeset
495 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
496 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
497
986e461dc072 Initial revision
glantau
parents:
diff changeset
498
986e461dc072 Initial revision
glantau
parents:
diff changeset
499 #define SB_NOTALLOCATED 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
500 #define SB_ALLOCATED 1
986e461dc072 Initial revision
glantau
parents:
diff changeset
501 #define SB_NOMORE 2
986e461dc072 Initial revision
glantau
parents:
diff changeset
502
986e461dc072 Initial revision
glantau
parents:
diff changeset
503 /* Try to maximize the smr while using a number of bits inferior to
986e461dc072 Initial revision
glantau
parents:
diff changeset
504 the frame size. I tried to make the code simpler, faster and
986e461dc072 Initial revision
glantau
parents:
diff changeset
505 smaller than other encoders :-) */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
506 static void compute_bit_allocation(MpegAudioContext *s,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
507 short smr1[MPA_MAX_CHANNELS][SBLIMIT],
986e461dc072 Initial revision
glantau
parents:
diff changeset
508 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
986e461dc072 Initial revision
glantau
parents:
diff changeset
509 int *padding)
986e461dc072 Initial revision
glantau
parents:
diff changeset
510 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
511 int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
512 int incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
513 short smr[MPA_MAX_CHANNELS][SBLIMIT];
986e461dc072 Initial revision
glantau
parents:
diff changeset
514 unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
986e461dc072 Initial revision
glantau
parents:
diff changeset
515 const unsigned char *alloc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
516
986e461dc072 Initial revision
glantau
parents:
diff changeset
517 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
986e461dc072 Initial revision
glantau
parents:
diff changeset
518 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
986e461dc072 Initial revision
glantau
parents:
diff changeset
519 memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
520
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
521 /* compute frame size and padding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
522 max_frame_size = s->frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
523 s->frame_frac += s->frame_frac_incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
524 if (s->frame_frac >= 65536) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
525 s->frame_frac -= 65536;
986e461dc072 Initial revision
glantau
parents:
diff changeset
526 s->do_padding = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
527 max_frame_size += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
528 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
529 s->do_padding = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
530 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
531
986e461dc072 Initial revision
glantau
parents:
diff changeset
532 /* compute the header + bit alloc size */
986e461dc072 Initial revision
glantau
parents:
diff changeset
533 current_frame_size = 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
534 alloc = s->alloc_table;
986e461dc072 Initial revision
glantau
parents:
diff changeset
535 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
536 incr = alloc[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
537 current_frame_size += incr * s->nb_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
538 alloc += 1 << incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
539 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
540 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
541 /* look for the subband with the largest signal to mask ratio */
986e461dc072 Initial revision
glantau
parents:
diff changeset
542 max_sb = -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
543 max_ch = -1;
6929
f2fc71a9c89f ILP64 fix
michael
parents: 6712
diff changeset
544 max_smr = INT_MIN;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
545 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
546 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
547 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
548 max_smr = smr[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
549 max_sb = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 max_ch = ch;
986e461dc072 Initial revision
glantau
parents:
diff changeset
551 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
552 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
553 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
554 #if 0
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
555 printf("current=%d max=%d max_sb=%d alloc=%d\n",
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
556 current_frame_size, max_frame_size, max_sb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
557 bit_alloc[max_sb]);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
558 #endif
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
559 if (max_sb < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
560 break;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
561
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
562 /* find alloc table entry (XXX: not optimal, should use
986e461dc072 Initial revision
glantau
parents:
diff changeset
563 pointer table) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
564 alloc = s->alloc_table;
986e461dc072 Initial revision
glantau
parents:
diff changeset
565 for(i=0;i<max_sb;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
566 alloc += 1 << alloc[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
567 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
568
986e461dc072 Initial revision
glantau
parents:
diff changeset
569 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
570 /* nothing was coded for this band: add the necessary bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
571 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
986e461dc072 Initial revision
glantau
parents:
diff changeset
572 incr += total_quant_bits[alloc[1]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
573 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
574 /* increments bit allocation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
575 b = bit_alloc[max_ch][max_sb];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
576 incr = total_quant_bits[alloc[b + 1]] -
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
577 total_quant_bits[alloc[b]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
578 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
579
986e461dc072 Initial revision
glantau
parents:
diff changeset
580 if (current_frame_size + incr <= max_frame_size) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
581 /* can increase size */
986e461dc072 Initial revision
glantau
parents:
diff changeset
582 b = ++bit_alloc[max_ch][max_sb];
986e461dc072 Initial revision
glantau
parents:
diff changeset
583 current_frame_size += incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
584 /* decrease smr by the resolution we added */
986e461dc072 Initial revision
glantau
parents:
diff changeset
585 smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
586 /* max allocation size reached ? */
986e461dc072 Initial revision
glantau
parents:
diff changeset
587 if (b == ((1 << alloc[0]) - 1))
986e461dc072 Initial revision
glantau
parents:
diff changeset
588 subband_status[max_ch][max_sb] = SB_NOMORE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
589 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
590 subband_status[max_ch][max_sb] = SB_ALLOCATED;
986e461dc072 Initial revision
glantau
parents:
diff changeset
591 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
592 /* cannot increase the size of this subband */
986e461dc072 Initial revision
glantau
parents:
diff changeset
593 subband_status[max_ch][max_sb] = SB_NOMORE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
594 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
595 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
596 *padding = max_frame_size - current_frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
597 assert(*padding >= 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
598
986e461dc072 Initial revision
glantau
parents:
diff changeset
599 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
600 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
601 printf("%d ", bit_alloc[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
602 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
603 printf("\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
604 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
605 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
606
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
608 * Output the mpeg audio layer 2 frame. Note how the code is small
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 * compared to other encoders :-)
986e461dc072 Initial revision
glantau
parents:
diff changeset
610 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
611 static void encode_frame(MpegAudioContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
612 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
986e461dc072 Initial revision
glantau
parents:
diff changeset
613 int padding)
986e461dc072 Initial revision
glantau
parents:
diff changeset
614 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
615 int i, j, k, l, bit_alloc_bits, b, ch;
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 unsigned char *sf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
617 int q[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 PutBitContext *p = &s->pb;
986e461dc072 Initial revision
glantau
parents:
diff changeset
619
986e461dc072 Initial revision
glantau
parents:
diff changeset
620 /* header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
621
986e461dc072 Initial revision
glantau
parents:
diff changeset
622 put_bits(p, 12, 0xfff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
623 put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
986e461dc072 Initial revision
glantau
parents:
diff changeset
624 put_bits(p, 2, 4-2); /* layer 2 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
625 put_bits(p, 1, 1); /* no error protection */
986e461dc072 Initial revision
glantau
parents:
diff changeset
626 put_bits(p, 4, s->bitrate_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
627 put_bits(p, 2, s->freq_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
628 put_bits(p, 1, s->do_padding); /* use padding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
629 put_bits(p, 1, 0); /* private_bit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
630 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
986e461dc072 Initial revision
glantau
parents:
diff changeset
631 put_bits(p, 2, 0); /* mode_ext */
986e461dc072 Initial revision
glantau
parents:
diff changeset
632 put_bits(p, 1, 0); /* no copyright */
986e461dc072 Initial revision
glantau
parents:
diff changeset
633 put_bits(p, 1, 1); /* original */
986e461dc072 Initial revision
glantau
parents:
diff changeset
634 put_bits(p, 2, 0); /* no emphasis */
986e461dc072 Initial revision
glantau
parents:
diff changeset
635
986e461dc072 Initial revision
glantau
parents:
diff changeset
636 /* bit allocation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 j = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
638 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
639 bit_alloc_bits = s->alloc_table[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
640 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
641 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
642 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 j += 1 << bit_alloc_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
645
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
646 /* scale codes */
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 for(ch=0;ch<s->nb_channels;ch++) {
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
649 if (bit_alloc[ch][i])
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
650 put_bits(p, 2, s->scale_code[ch][i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
652 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
653
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 /* scale factors */
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
656 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
657 if (bit_alloc[ch][i]) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
658 sf = &s->scale_factors[ch][i][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
659 switch(s->scale_code[ch][i]) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
660 case 0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
661 put_bits(p, 6, sf[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
662 put_bits(p, 6, sf[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
663 put_bits(p, 6, sf[2]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
664 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
665 case 3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
666 case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
667 put_bits(p, 6, sf[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
668 put_bits(p, 6, sf[2]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
669 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
670 case 2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
671 put_bits(p, 6, sf[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
672 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
673 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
674 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
675 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
676 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
677
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
678 /* quantization & write sub band samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
679
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 for(k=0;k<3;k++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
681 for(l=0;l<12;l+=3) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
682 j = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
683 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
684 bit_alloc_bits = s->alloc_table[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
685 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
686 b = bit_alloc[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
687 if (b) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
688 int qindex, steps, m, sample, bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
689 /* we encode 3 sub band samples of the same sub band at a time */
986e461dc072 Initial revision
glantau
parents:
diff changeset
690 qindex = s->alloc_table[j+b];
5032
d47ee2d1d7da add proper prefix to extern mpeg audio data tables
aurel
parents: 5031
diff changeset
691 steps = ff_mpa_quant_steps[qindex];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
692 for(m=0;m<3;m++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
693 sample = s->sb_samples[ch][k][l + m][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
694 /* divide by scale factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
695 #ifdef USE_FLOATS
986e461dc072 Initial revision
glantau
parents:
diff changeset
696 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
697 float a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
698 a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
699 q[m] = (int)((a + 1.0) * steps * 0.5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
700 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
701 #else
986e461dc072 Initial revision
glantau
parents:
diff changeset
702 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
703 int q1, e, shift, mult;
986e461dc072 Initial revision
glantau
parents:
diff changeset
704 e = s->scale_factors[ch][i][k];
986e461dc072 Initial revision
glantau
parents:
diff changeset
705 shift = scale_factor_shift[e];
986e461dc072 Initial revision
glantau
parents:
diff changeset
706 mult = scale_factor_mult[e];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
707
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
708 /* normalize to P bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
709 if (shift < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
710 q1 = sample << (-shift);
986e461dc072 Initial revision
glantau
parents:
diff changeset
711 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
712 q1 = sample >> shift;
986e461dc072 Initial revision
glantau
parents:
diff changeset
713 q1 = (q1 * mult) >> P;
986e461dc072 Initial revision
glantau
parents:
diff changeset
714 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
715 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
716 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
717 if (q[m] >= steps)
986e461dc072 Initial revision
glantau
parents:
diff changeset
718 q[m] = steps - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
719 assert(q[m] >= 0 && q[m] < steps);
986e461dc072 Initial revision
glantau
parents:
diff changeset
720 }
5032
d47ee2d1d7da add proper prefix to extern mpeg audio data tables
aurel
parents: 5031
diff changeset
721 bits = ff_mpa_quant_bits[qindex];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
722 if (bits < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
723 /* group the 3 values to save bits */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
724 put_bits(p, -bits,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
725 q[0] + steps * (q[1] + steps * q[2]));
986e461dc072 Initial revision
glantau
parents:
diff changeset
726 #if 0
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
727 printf("%d: gr1 %d\n",
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
728 i, q[0] + steps * (q[1] + steps * q[2]));
986e461dc072 Initial revision
glantau
parents:
diff changeset
729 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
730 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
731 #if 0
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
732 printf("%d: gr3 %d %d %d\n",
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
733 i, q[0], q[1], q[2]);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
734 #endif
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
735 put_bits(p, bits, q[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
736 put_bits(p, bits, q[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
737 put_bits(p, bits, q[2]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
738 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
739 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
740 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
741 /* next subband in alloc table */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
742 j += 1 << bit_alloc_bits;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
743 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
744 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
745 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
746
986e461dc072 Initial revision
glantau
parents:
diff changeset
747 /* padding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
748 for(i=0;i<padding;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
749 put_bits(p, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
750
986e461dc072 Initial revision
glantau
parents:
diff changeset
751 /* flush */
986e461dc072 Initial revision
glantau
parents:
diff changeset
752 flush_put_bits(p);
986e461dc072 Initial revision
glantau
parents:
diff changeset
753 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
754
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 1031
diff changeset
755 static int MPA_encode_frame(AVCodecContext *avctx,
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
756 unsigned char *frame, int buf_size, void *data)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
757 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
758 MpegAudioContext *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
759 short *samples = data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
760 short smr[MPA_MAX_CHANNELS][SBLIMIT];
986e461dc072 Initial revision
glantau
parents:
diff changeset
761 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
986e461dc072 Initial revision
glantau
parents:
diff changeset
762 int padding, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
763
986e461dc072 Initial revision
glantau
parents:
diff changeset
764 for(i=0;i<s->nb_channels;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
765 filter(s, i, samples + i, s->nb_channels);
986e461dc072 Initial revision
glantau
parents:
diff changeset
766 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
767
986e461dc072 Initial revision
glantau
parents:
diff changeset
768 for(i=0;i<s->nb_channels;i++) {
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
769 compute_scale_factors(s->scale_code[i], s->scale_factors[i],
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
770 s->sb_samples[i], s->sblimit);
986e461dc072 Initial revision
glantau
parents:
diff changeset
771 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
772 for(i=0;i<s->nb_channels;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
773 psycho_acoustic_model(s, smr[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
774 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
775 compute_bit_allocation(s, smr, bit_alloc, &padding);
986e461dc072 Initial revision
glantau
parents:
diff changeset
776
1522
79dddc5cd990 removed the obsolete and unused parameters of init_put_bits
alex
parents: 1106
diff changeset
777 init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
778
986e461dc072 Initial revision
glantau
parents:
diff changeset
779 encode_frame(s, bit_alloc, padding);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2913
diff changeset
780
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
781 s->nb_samples += MPA_FRAME_SIZE;
9431
932543edc1d2 Rename pbBufPtr() to put_bits_ptr().
stefano
parents: 9411
diff changeset
782 return put_bits_ptr(&s->pb) - s->pb.buf;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
783 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
784
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 5161
diff changeset
785 static av_cold int MPA_encode_close(AVCodecContext *avctx)
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 653
diff changeset
786 {
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 653
diff changeset
787 av_freep(&avctx->coded_frame);
1031
19de1445beb2 use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents: 925
diff changeset
788 return 0;
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 653
diff changeset
789 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
790
986e461dc072 Initial revision
glantau
parents:
diff changeset
791 AVCodec mp2_encoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
792 "mp2",
986e461dc072 Initial revision
glantau
parents:
diff changeset
793 CODEC_TYPE_AUDIO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
794 CODEC_ID_MP2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
795 sizeof(MpegAudioContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
796 MPA_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
797 MPA_encode_frame,
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 653
diff changeset
798 MPA_encode_close,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
799 NULL,
10145
7955db355703 Make sample_fmts and channel_layouts compound literals const to reduce size of
reimar
parents: 9999
diff changeset
800 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6961
diff changeset
801 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
802 };
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
803
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
804 #undef FIX