annotate mpegaudio.c @ 0:986e461dc072 libavcodec

Initial revision
author glantau
date Sun, 22 Jul 2001 14:18:56 +0000
parents
children 5aa6292a1660
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
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2000 Gerard Lantau.
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
19 #include <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 #include <string.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include <math.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 #include "mpegaudio.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
25
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 #define NDEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 #include <assert.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
28
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 /* define it to use floats in quantization (I don't like floats !) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
30 //#define USE_FLOATS
986e461dc072 Initial revision
glantau
parents:
diff changeset
31
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 #define MPA_STEREO 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 #define MPA_JSTEREO 1
986e461dc072 Initial revision
glantau
parents:
diff changeset
34 #define MPA_DUAL 2
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 #define MPA_MONO 3
986e461dc072 Initial revision
glantau
parents:
diff changeset
36
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 #include "mpegaudiotab.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
38
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 int MPA_encode_init(AVCodecContext *avctx)
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 MpegAudioContext *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 int freq = avctx->sample_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 int bitrate = avctx->bit_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 int channels = avctx->channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 int i, v, table, ch_bitrate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 float a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
47
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 if (channels > 2)
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 bitrate = bitrate / 1000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 s->nb_channels = channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 s->freq = freq;
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 s->bit_rate = bitrate * 1000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 avctx->frame_size = MPA_FRAME_SIZE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 avctx->key_frame = 1; /* always key frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
56
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 /* encoding freq */
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 s->lsf = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 if (freq_tab[i] == freq)
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
62 if ((freq_tab[i] / 2) == freq) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 s->lsf = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 if (i == 3)
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 s->freq_index = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
70
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 /* encoding bitrate & frequency */
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 for(i=0;i<15;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 if (bitrate_tab[1-s->lsf][i] == bitrate)
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 if (i == 15)
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 s->bitrate_index = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
79
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 /* compute total header size & pad bit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
81
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 s->frame_size = ((int)a) * 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
84
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 /* frame fractional size to compute padding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 s->frame_frac = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
88
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 /* select the right allocation table */
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 ch_bitrate = bitrate / s->nb_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 if (!s->lsf) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 if ((freq == 48000 && ch_bitrate >= 56) ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 (ch_bitrate >= 56 && ch_bitrate <= 80))
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 table = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 else if (freq != 48000 && ch_bitrate >= 96)
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 table = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 else if (freq != 32000 && ch_bitrate <= 48)
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 table = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 table = 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 table = 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 /* number of used subbands */
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 s->sblimit = sblimit_table[table];
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 s->alloc_table = alloc_tables[table];
986e461dc072 Initial revision
glantau
parents:
diff changeset
107
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 printf("%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 bitrate, freq, s->frame_size, table, s->frame_frac_incr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
112
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 for(i=0;i<s->nb_channels;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 s->samples_offset[i] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
115
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 for(i=0;i<512;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 float a = enwindow[i] * 32768.0 * 16.0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 filter_bank[i] = (int)(a);
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 for(i=0;i<64;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 if (v <= 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 v = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 scale_factor_table[i] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 #ifdef USE_FLOATS
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 #else
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 #define P 15
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 scale_factor_shift[i] = 21 - P - (i / 3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 for(i=0;i<128;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 v = i - 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 if (v <= -3)
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 v = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 else if (v < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 v = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 else if (v == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 v = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 else if (v < 3)
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 v = 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 v = 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 scale_diff_table[i] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
147
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 for(i=0;i<17;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 v = quant_bits[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 if (v < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 v = -v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 v = v * 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 total_quant_bits[i] = 12 * v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
156
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
159
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 /* 32 point floating point IDCT */
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 static void idct32(int *out, int *tab, int sblimit, int left_shift)
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 int i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 int *t, *t1, xr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 const int *xp = costab32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
166
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
168
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 t = tab + 30;
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 t1 = tab + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 t[0] += t[-4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 t[1] += t[1 - 4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 t -= 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 } while (t != t1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
176
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 t = tab + 28;
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 t1 = tab + 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 t[0] += t[-8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 t[1] += t[1-8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 t[2] += t[2-8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 t[3] += t[3-8];
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 t -= 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 } while (t != t1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
186
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 t = tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 t1 = tab + 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 t[ 3] = -t[ 3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
191 t[ 6] = -t[ 6];
986e461dc072 Initial revision
glantau
parents:
diff changeset
192
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 t[11] = -t[11];
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 t[12] = -t[12];
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 t[13] = -t[13];
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 t[15] = -t[15];
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 t += 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 } while (t != t1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
199
986e461dc072 Initial revision
glantau
parents:
diff changeset
200
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 t = tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 t1 = tab + 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 int x1, x2, x3, x4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
205
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 x3 = MUL(t[16], FIX(SQRT2*0.5));
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 x4 = t[0] - x3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 x3 = t[0] + x3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
209
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 x1 = MUL((t[8] - x2), xp[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 x2 = MUL((t[8] + x2), xp[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
213
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 t[ 0] = x3 + x1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 t[ 8] = x4 - x2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 t[16] = x4 + x2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 t[24] = x3 - x1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 t++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 } while (t != t1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
220
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 xp += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 t = tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 t1 = tab + 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 xr = MUL(t[28],xp[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 t[28] = (t[0] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 t[0] = (t[0] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
228
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 xr = MUL(t[4],xp[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 t[ 4] = (t[24] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 t[24] = (t[24] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
232
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 xr = MUL(t[20],xp[2]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 t[20] = (t[8] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 t[ 8] = (t[8] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
236
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 xr = MUL(t[12],xp[3]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 t[12] = (t[16] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 t[16] = (t[16] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 t++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 } while (t != t1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 xp += 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
243
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 for (i = 0; i < 4; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 xr = MUL(tab[30-i*4],xp[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 tab[30-i*4] = (tab[i*4] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 tab[ i*4] = (tab[i*4] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
248
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 xr = MUL(tab[ 2+i*4],xp[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 tab[ 2+i*4] = (tab[28-i*4] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 tab[28-i*4] = (tab[28-i*4] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
252
986e461dc072 Initial revision
glantau
parents:
diff changeset
253 xr = MUL(tab[31-i*4],xp[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 tab[31-i*4] = (tab[1+i*4] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 tab[ 1+i*4] = (tab[1+i*4] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
256
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 xr = MUL(tab[ 3+i*4],xp[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 tab[ 3+i*4] = (tab[29-i*4] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 tab[29-i*4] = (tab[29-i*4] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
260
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 xp += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
263
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 t = tab + 30;
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 t1 = tab + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 xr = MUL(t1[0], *xp);
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 t1[0] = (t[0] - xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 t[0] = (t[0] + xr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 t -= 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 t1 += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 xp++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 } while (t >= tab);
986e461dc072 Initial revision
glantau
parents:
diff changeset
274
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 for(i=0;i<32;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 out[i] = tab[bitinv32[i]] << left_shift;
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
279
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 short *p, *q;
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 int sum, offset, i, j, norm, n;
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 short tmp[64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 int tmp1[32];
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 int *out;
986e461dc072 Initial revision
glantau
parents:
diff changeset
287
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 // print_pow1(samples, 1152);
986e461dc072 Initial revision
glantau
parents:
diff changeset
289
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 offset = s->samples_offset[ch];
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 out = &s->sb_samples[ch][0][0][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 for(j=0;j<36;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 /* 32 samples at once */
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 for(i=0;i<32;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 s->samples_buf[ch][offset + (31 - i)] = samples[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 samples += incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
298
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 /* filter */
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 p = s->samples_buf[ch] + offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 q = filter_bank;
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 /* maxsum = 23169 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 for(i=0;i<64;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 sum = p[0*64] * q[0*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 sum += p[1*64] * q[1*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 sum += p[2*64] * q[2*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 sum += p[3*64] * q[3*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 sum += p[4*64] * q[4*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 sum += p[5*64] * q[5*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 sum += p[6*64] * q[6*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 sum += p[7*64] * q[7*64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 tmp[i] = sum >> 14;
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 p++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 q++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 tmp1[0] = tmp[16];
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 for( i=1; i<=16; i++ ) tmp1[i] = tmp[i+16]+tmp[16-i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 for( i=17; i<=31; i++ ) tmp1[i] = tmp[i+16]-tmp[80-i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
319
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 /* integer IDCT 32 with normalization. XXX: There may be some
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 overflow left */
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 norm = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 for(i=0;i<32;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 norm |= abs(tmp1[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 n = log2(norm) - 12;
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 if (n > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 for(i=0;i<32;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 tmp1[i] >>= n;
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 n = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
333
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 idct32(out, tmp1, s->sblimit, n);
986e461dc072 Initial revision
glantau
parents:
diff changeset
335
986e461dc072 Initial revision
glantau
parents:
diff changeset
336 /* advance of 32 samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 offset -= 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 out += 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
339 /* handle the wrap around */
986e461dc072 Initial revision
glantau
parents:
diff changeset
340 if (offset < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 s->samples_buf[ch], (512 - 32) * 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 offset = SAMPLES_BUF_SIZE - 512;
986e461dc072 Initial revision
glantau
parents:
diff changeset
344 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 s->samples_offset[ch] = offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
347
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 // print_pow(s->sb_samples, 1152);
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
350
986e461dc072 Initial revision
glantau
parents:
diff changeset
351 static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 unsigned char scale_factors[SBLIMIT][3],
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 int sb_samples[3][12][SBLIMIT],
986e461dc072 Initial revision
glantau
parents:
diff changeset
354 int sblimit)
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 int *p, vmax, v, n, i, j, k, code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 int index, d1, d2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 unsigned char *sf = &scale_factors[0][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
359
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 for(j=0;j<sblimit;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
361 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 /* find the max absolute value */
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 p = &sb_samples[i][0][j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
364 vmax = abs(*p);
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 for(k=1;k<12;k++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
366 p += SBLIMIT;
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 v = abs(*p);
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 if (v > vmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
369 vmax = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
371 /* compute the scale factor index using log 2 computations */
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 if (vmax > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 n = log2(vmax);
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 /* n is the position of the MSB of vmax. now
986e461dc072 Initial revision
glantau
parents:
diff changeset
375 use at most 2 compares to find the index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 index = (21 - n) * 3 - 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
377 if (index >= 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
378 while (vmax <= scale_factor_table[index+1])
986e461dc072 Initial revision
glantau
parents:
diff changeset
379 index++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
380 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
381 index = 0; /* very unlikely case of overflow */
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
383 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
384 index = 63;
986e461dc072 Initial revision
glantau
parents:
diff changeset
385 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
386
986e461dc072 Initial revision
glantau
parents:
diff changeset
387 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
388 printf("%2d:%d in=%x %x %d\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
389 j, i, vmax, scale_factor_table[index], index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
390 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
391 /* store the scale factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
392 assert(index >=0 && index <= 63);
986e461dc072 Initial revision
glantau
parents:
diff changeset
393 sf[i] = index;
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
395
986e461dc072 Initial revision
glantau
parents:
diff changeset
396 /* compute the transmission factor : look if the scale factors
986e461dc072 Initial revision
glantau
parents:
diff changeset
397 are close enough to each other */
986e461dc072 Initial revision
glantau
parents:
diff changeset
398 d1 = scale_diff_table[sf[0] - sf[1] + 64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
399 d2 = scale_diff_table[sf[1] - sf[2] + 64];
986e461dc072 Initial revision
glantau
parents:
diff changeset
400
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 /* handle the 25 cases */
986e461dc072 Initial revision
glantau
parents:
diff changeset
402 switch(d1 * 5 + d2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
403 case 0*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
404 case 0*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
405 case 3*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 case 4*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 case 4*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 code = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
409 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 case 0*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 case 0*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
412 case 4*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
413 case 4*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
414 code = 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
415 sf[2] = sf[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
416 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
417 case 0*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
418 case 4*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
419 code = 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
420 sf[1] = sf[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
421 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
422 case 1*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
423 case 1*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
424 case 2*5+4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
425 code = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
426 sf[1] = sf[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
427 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
428 case 1*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
429 case 1*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
430 case 2*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
431 case 2*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
432 case 2*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
433 code = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
434 sf[1] = sf[2] = sf[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
435 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
436 case 2*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
437 case 3*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
438 code = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
439 sf[0] = sf[1] = sf[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
440 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
441 case 3*5+0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
442 case 3*5+1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
443 case 3*5+2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
444 code = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
445 sf[0] = sf[2] = sf[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
446 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
447 case 1*5+3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
448 code = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
449 if (sf[0] > sf[2])
986e461dc072 Initial revision
glantau
parents:
diff changeset
450 sf[0] = sf[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
451 sf[1] = sf[2] = sf[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
452 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
453 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
454 abort();
986e461dc072 Initial revision
glantau
parents:
diff changeset
455 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
456
986e461dc072 Initial revision
glantau
parents:
diff changeset
457 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
458 printf("%d: %2d %2d %2d %d %d -> %d\n", j,
986e461dc072 Initial revision
glantau
parents:
diff changeset
459 sf[0], sf[1], sf[2], d1, d2, code);
986e461dc072 Initial revision
glantau
parents:
diff changeset
460 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
461 scale_code[j] = code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
462 sf += 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
463 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
464 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
465
986e461dc072 Initial revision
glantau
parents:
diff changeset
466 /* The most important function : psycho acoustic module. In this
986e461dc072 Initial revision
glantau
parents:
diff changeset
467 encoder there is basically none, so this is the worst you can do,
986e461dc072 Initial revision
glantau
parents:
diff changeset
468 but also this is the simpler. */
986e461dc072 Initial revision
glantau
parents:
diff changeset
469 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
986e461dc072 Initial revision
glantau
parents:
diff changeset
470 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
471 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
472
986e461dc072 Initial revision
glantau
parents:
diff changeset
473 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
474 smr[i] = (int)(fixed_smr[i] * 10);
986e461dc072 Initial revision
glantau
parents:
diff changeset
475 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
476 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
477
986e461dc072 Initial revision
glantau
parents:
diff changeset
478
986e461dc072 Initial revision
glantau
parents:
diff changeset
479 #define SB_NOTALLOCATED 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
480 #define SB_ALLOCATED 1
986e461dc072 Initial revision
glantau
parents:
diff changeset
481 #define SB_NOMORE 2
986e461dc072 Initial revision
glantau
parents:
diff changeset
482
986e461dc072 Initial revision
glantau
parents:
diff changeset
483 /* Try to maximize the smr while using a number of bits inferior to
986e461dc072 Initial revision
glantau
parents:
diff changeset
484 the frame size. I tried to make the code simpler, faster and
986e461dc072 Initial revision
glantau
parents:
diff changeset
485 smaller than other encoders :-) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
486 static void compute_bit_allocation(MpegAudioContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
487 short smr1[MPA_MAX_CHANNELS][SBLIMIT],
986e461dc072 Initial revision
glantau
parents:
diff changeset
488 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
986e461dc072 Initial revision
glantau
parents:
diff changeset
489 int *padding)
986e461dc072 Initial revision
glantau
parents:
diff changeset
490 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
491 int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
492 int incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
493 short smr[MPA_MAX_CHANNELS][SBLIMIT];
986e461dc072 Initial revision
glantau
parents:
diff changeset
494 unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
986e461dc072 Initial revision
glantau
parents:
diff changeset
495 const unsigned char *alloc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
496
986e461dc072 Initial revision
glantau
parents:
diff changeset
497 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
986e461dc072 Initial revision
glantau
parents:
diff changeset
498 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
986e461dc072 Initial revision
glantau
parents:
diff changeset
499 memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
986e461dc072 Initial revision
glantau
parents:
diff changeset
500
986e461dc072 Initial revision
glantau
parents:
diff changeset
501 /* compute frame size and padding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
502 max_frame_size = s->frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
503 s->frame_frac += s->frame_frac_incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
504 if (s->frame_frac >= 65536) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
505 s->frame_frac -= 65536;
986e461dc072 Initial revision
glantau
parents:
diff changeset
506 s->do_padding = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
507 max_frame_size += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
508 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
509 s->do_padding = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
510 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
511
986e461dc072 Initial revision
glantau
parents:
diff changeset
512 /* compute the header + bit alloc size */
986e461dc072 Initial revision
glantau
parents:
diff changeset
513 current_frame_size = 32;
986e461dc072 Initial revision
glantau
parents:
diff changeset
514 alloc = s->alloc_table;
986e461dc072 Initial revision
glantau
parents:
diff changeset
515 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
516 incr = alloc[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
517 current_frame_size += incr * s->nb_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
518 alloc += 1 << incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
519 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
520 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
521 /* look for the subband with the largest signal to mask ratio */
986e461dc072 Initial revision
glantau
parents:
diff changeset
522 max_sb = -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
523 max_ch = -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
524 max_smr = 0x80000000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
525 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
526 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
527 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
528 max_smr = smr[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
529 max_sb = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
530 max_ch = ch;
986e461dc072 Initial revision
glantau
parents:
diff changeset
531 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
532 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
533 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
534 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
535 printf("current=%d max=%d max_sb=%d alloc=%d\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
536 current_frame_size, max_frame_size, max_sb,
986e461dc072 Initial revision
glantau
parents:
diff changeset
537 bit_alloc[max_sb]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
538 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
539 if (max_sb < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
540 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
541
986e461dc072 Initial revision
glantau
parents:
diff changeset
542 /* find alloc table entry (XXX: not optimal, should use
986e461dc072 Initial revision
glantau
parents:
diff changeset
543 pointer table) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
544 alloc = s->alloc_table;
986e461dc072 Initial revision
glantau
parents:
diff changeset
545 for(i=0;i<max_sb;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
546 alloc += 1 << alloc[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
547 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
548
986e461dc072 Initial revision
glantau
parents:
diff changeset
549 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 /* nothing was coded for this band: add the necessary bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
551 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
986e461dc072 Initial revision
glantau
parents:
diff changeset
552 incr += total_quant_bits[alloc[1]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
553 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
554 /* increments bit allocation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
555 b = bit_alloc[max_ch][max_sb];
986e461dc072 Initial revision
glantau
parents:
diff changeset
556 incr = total_quant_bits[alloc[b + 1]] -
986e461dc072 Initial revision
glantau
parents:
diff changeset
557 total_quant_bits[alloc[b]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
558 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
559
986e461dc072 Initial revision
glantau
parents:
diff changeset
560 if (current_frame_size + incr <= max_frame_size) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
561 /* can increase size */
986e461dc072 Initial revision
glantau
parents:
diff changeset
562 b = ++bit_alloc[max_ch][max_sb];
986e461dc072 Initial revision
glantau
parents:
diff changeset
563 current_frame_size += incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
564 /* decrease smr by the resolution we added */
986e461dc072 Initial revision
glantau
parents:
diff changeset
565 smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
566 /* max allocation size reached ? */
986e461dc072 Initial revision
glantau
parents:
diff changeset
567 if (b == ((1 << alloc[0]) - 1))
986e461dc072 Initial revision
glantau
parents:
diff changeset
568 subband_status[max_ch][max_sb] = SB_NOMORE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
569 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
570 subband_status[max_ch][max_sb] = SB_ALLOCATED;
986e461dc072 Initial revision
glantau
parents:
diff changeset
571 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
572 /* cannot increase the size of this subband */
986e461dc072 Initial revision
glantau
parents:
diff changeset
573 subband_status[max_ch][max_sb] = SB_NOMORE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
574 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
575 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
576 *padding = max_frame_size - current_frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
577 assert(*padding >= 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
578
986e461dc072 Initial revision
glantau
parents:
diff changeset
579 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
580 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
581 printf("%d ", bit_alloc[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
582 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
583 printf("\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
584 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
585 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
586
986e461dc072 Initial revision
glantau
parents:
diff changeset
587 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
588 * Output the mpeg audio layer 2 frame. Note how the code is small
986e461dc072 Initial revision
glantau
parents:
diff changeset
589 * compared to other encoders :-)
986e461dc072 Initial revision
glantau
parents:
diff changeset
590 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
591 static void encode_frame(MpegAudioContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
592 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
986e461dc072 Initial revision
glantau
parents:
diff changeset
593 int padding)
986e461dc072 Initial revision
glantau
parents:
diff changeset
594 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
595 int i, j, k, l, bit_alloc_bits, b, ch;
986e461dc072 Initial revision
glantau
parents:
diff changeset
596 unsigned char *sf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
597 int q[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
598 PutBitContext *p = &s->pb;
986e461dc072 Initial revision
glantau
parents:
diff changeset
599
986e461dc072 Initial revision
glantau
parents:
diff changeset
600 /* header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
601
986e461dc072 Initial revision
glantau
parents:
diff changeset
602 put_bits(p, 12, 0xfff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
603 put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
986e461dc072 Initial revision
glantau
parents:
diff changeset
604 put_bits(p, 2, 4-2); /* layer 2 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
605 put_bits(p, 1, 1); /* no error protection */
986e461dc072 Initial revision
glantau
parents:
diff changeset
606 put_bits(p, 4, s->bitrate_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 put_bits(p, 2, s->freq_index);
986e461dc072 Initial revision
glantau
parents:
diff changeset
608 put_bits(p, 1, s->do_padding); /* use padding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 put_bits(p, 1, 0); /* private_bit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
610 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
986e461dc072 Initial revision
glantau
parents:
diff changeset
611 put_bits(p, 2, 0); /* mode_ext */
986e461dc072 Initial revision
glantau
parents:
diff changeset
612 put_bits(p, 1, 0); /* no copyright */
986e461dc072 Initial revision
glantau
parents:
diff changeset
613 put_bits(p, 1, 1); /* original */
986e461dc072 Initial revision
glantau
parents:
diff changeset
614 put_bits(p, 2, 0); /* no emphasis */
986e461dc072 Initial revision
glantau
parents:
diff changeset
615
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 /* bit allocation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
617 j = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
619 bit_alloc_bits = s->alloc_table[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
620 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
621 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
622 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
623 j += 1 << bit_alloc_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
624 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
625
986e461dc072 Initial revision
glantau
parents:
diff changeset
626 /* scale codes */
986e461dc072 Initial revision
glantau
parents:
diff changeset
627 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
628 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
629 if (bit_alloc[ch][i])
986e461dc072 Initial revision
glantau
parents:
diff changeset
630 put_bits(p, 2, s->scale_code[ch][i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
631 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
632 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
633
986e461dc072 Initial revision
glantau
parents:
diff changeset
634 /* scale factors */
986e461dc072 Initial revision
glantau
parents:
diff changeset
635 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
636 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 if (bit_alloc[ch][i]) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
638 sf = &s->scale_factors[ch][i][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
639 switch(s->scale_code[ch][i]) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
640 case 0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
641 put_bits(p, 6, sf[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
642 put_bits(p, 6, sf[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 put_bits(p, 6, sf[2]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
645 case 3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
646 case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 put_bits(p, 6, sf[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 put_bits(p, 6, sf[2]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
649 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
650 case 2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 put_bits(p, 6, sf[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
652 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
653 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
656 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
657
986e461dc072 Initial revision
glantau
parents:
diff changeset
658 /* quantization & write sub band samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
659
986e461dc072 Initial revision
glantau
parents:
diff changeset
660 for(k=0;k<3;k++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
661 for(l=0;l<12;l+=3) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
662 j = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
663 for(i=0;i<s->sblimit;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
664 bit_alloc_bits = s->alloc_table[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
665 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
666 b = bit_alloc[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
667 if (b) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
668 int qindex, steps, m, sample, bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
669 /* we encode 3 sub band samples of the same sub band at a time */
986e461dc072 Initial revision
glantau
parents:
diff changeset
670 qindex = s->alloc_table[j+b];
986e461dc072 Initial revision
glantau
parents:
diff changeset
671 steps = quant_steps[qindex];
986e461dc072 Initial revision
glantau
parents:
diff changeset
672 for(m=0;m<3;m++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
673 sample = s->sb_samples[ch][k][l + m][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
674 /* divide by scale factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
675 #ifdef USE_FLOATS
986e461dc072 Initial revision
glantau
parents:
diff changeset
676 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
677 float a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
678 a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
679 q[m] = (int)((a + 1.0) * steps * 0.5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
681 #else
986e461dc072 Initial revision
glantau
parents:
diff changeset
682 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
683 int q1, e, shift, mult;
986e461dc072 Initial revision
glantau
parents:
diff changeset
684 e = s->scale_factors[ch][i][k];
986e461dc072 Initial revision
glantau
parents:
diff changeset
685 shift = scale_factor_shift[e];
986e461dc072 Initial revision
glantau
parents:
diff changeset
686 mult = scale_factor_mult[e];
986e461dc072 Initial revision
glantau
parents:
diff changeset
687
986e461dc072 Initial revision
glantau
parents:
diff changeset
688 /* normalize to P bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
689 if (shift < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
690 q1 = sample << (-shift);
986e461dc072 Initial revision
glantau
parents:
diff changeset
691 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
692 q1 = sample >> shift;
986e461dc072 Initial revision
glantau
parents:
diff changeset
693 q1 = (q1 * mult) >> P;
986e461dc072 Initial revision
glantau
parents:
diff changeset
694 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
695 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
696 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
697 if (q[m] >= steps)
986e461dc072 Initial revision
glantau
parents:
diff changeset
698 q[m] = steps - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
699 assert(q[m] >= 0 && q[m] < steps);
986e461dc072 Initial revision
glantau
parents:
diff changeset
700 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
701 bits = quant_bits[qindex];
986e461dc072 Initial revision
glantau
parents:
diff changeset
702 if (bits < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
703 /* group the 3 values to save bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
704 put_bits(p, -bits,
986e461dc072 Initial revision
glantau
parents:
diff changeset
705 q[0] + steps * (q[1] + steps * q[2]));
986e461dc072 Initial revision
glantau
parents:
diff changeset
706 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
707 printf("%d: gr1 %d\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
708 i, q[0] + steps * (q[1] + steps * q[2]));
986e461dc072 Initial revision
glantau
parents:
diff changeset
709 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
710 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
711 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
712 printf("%d: gr3 %d %d %d\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
713 i, q[0], q[1], q[2]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
714 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
715 put_bits(p, bits, q[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
716 put_bits(p, bits, q[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
717 put_bits(p, bits, q[2]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
718 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
719 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
720 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
721 /* next subband in alloc table */
986e461dc072 Initial revision
glantau
parents:
diff changeset
722 j += 1 << bit_alloc_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
723 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
724 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
725 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
726
986e461dc072 Initial revision
glantau
parents:
diff changeset
727 /* padding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
728 for(i=0;i<padding;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
729 put_bits(p, 1, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
730
986e461dc072 Initial revision
glantau
parents:
diff changeset
731 /* flush */
986e461dc072 Initial revision
glantau
parents:
diff changeset
732 flush_put_bits(p);
986e461dc072 Initial revision
glantau
parents:
diff changeset
733 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
734
986e461dc072 Initial revision
glantau
parents:
diff changeset
735 int MPA_encode_frame(AVCodecContext *avctx,
986e461dc072 Initial revision
glantau
parents:
diff changeset
736 unsigned char *frame, int buf_size, void *data)
986e461dc072 Initial revision
glantau
parents:
diff changeset
737 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
738 MpegAudioContext *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
739 short *samples = data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
740 short smr[MPA_MAX_CHANNELS][SBLIMIT];
986e461dc072 Initial revision
glantau
parents:
diff changeset
741 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
986e461dc072 Initial revision
glantau
parents:
diff changeset
742 int padding, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
743
986e461dc072 Initial revision
glantau
parents:
diff changeset
744 for(i=0;i<s->nb_channels;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
745 filter(s, i, samples + i, s->nb_channels);
986e461dc072 Initial revision
glantau
parents:
diff changeset
746 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
747
986e461dc072 Initial revision
glantau
parents:
diff changeset
748 for(i=0;i<s->nb_channels;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
749 compute_scale_factors(s->scale_code[i], s->scale_factors[i],
986e461dc072 Initial revision
glantau
parents:
diff changeset
750 s->sb_samples[i], s->sblimit);
986e461dc072 Initial revision
glantau
parents:
diff changeset
751 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
752 for(i=0;i<s->nb_channels;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
753 psycho_acoustic_model(s, smr[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
754 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
755 compute_bit_allocation(s, smr, bit_alloc, &padding);
986e461dc072 Initial revision
glantau
parents:
diff changeset
756
986e461dc072 Initial revision
glantau
parents:
diff changeset
757 init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE, NULL, NULL);
986e461dc072 Initial revision
glantau
parents:
diff changeset
758
986e461dc072 Initial revision
glantau
parents:
diff changeset
759 encode_frame(s, bit_alloc, padding);
986e461dc072 Initial revision
glantau
parents:
diff changeset
760
986e461dc072 Initial revision
glantau
parents:
diff changeset
761 s->nb_samples += MPA_FRAME_SIZE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
762 return s->pb.buf_ptr - s->pb.buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
763 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
764
986e461dc072 Initial revision
glantau
parents:
diff changeset
765
986e461dc072 Initial revision
glantau
parents:
diff changeset
766 AVCodec mp2_encoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
767 "mp2",
986e461dc072 Initial revision
glantau
parents:
diff changeset
768 CODEC_TYPE_AUDIO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
769 CODEC_ID_MP2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
770 sizeof(MpegAudioContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
771 MPA_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
772 MPA_encode_frame,
986e461dc072 Initial revision
glantau
parents:
diff changeset
773 NULL,
986e461dc072 Initial revision
glantau
parents:
diff changeset
774 };