Diffstat (limited to 'core/multimedia/opieplayer/libmpeg3/audio/ac3.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/libmpeg3/audio/ac3.c | 691 |
1 files changed, 691 insertions, 0 deletions
diff --git a/core/multimedia/opieplayer/libmpeg3/audio/ac3.c b/core/multimedia/opieplayer/libmpeg3/audio/ac3.c new file mode 100644 index 0000000..7a3b664 --- a/dev/null +++ b/core/multimedia/opieplayer/libmpeg3/audio/ac3.c | |||
@@ -0,0 +1,691 @@ | |||
1 | /* | ||
2 | * | ||
3 | *ac3.c Copyright (C) Aaron Holtzman - May 1999 | ||
4 | * | ||
5 | * | ||
6 | * This file is part of libmpeg3 | ||
7 | * | ||
8 | * libmpeg3 is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2, or (at your option) | ||
11 | * any later version. | ||
12 | * | ||
13 | * libmpeg3 is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with GNU Make; see the file COPYING. If not, write to | ||
20 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include "mpeg3audio.h" | ||
25 | #include "../libmpeg3.h" | ||
26 | #include "../mpeg3protos.h" | ||
27 | |||
28 | #include <math.h> | ||
29 | #include <stdlib.h> | ||
30 | #include <stdio.h> | ||
31 | #include <string.h> | ||
32 | |||
33 | #define MPEG3AC3_MAGIC_NUMBER 0xdeadbeef | ||
34 | |||
35 | |||
36 | int mpeg3_ac3_samplerates[] = { 48000, 44100, 32000 }; | ||
37 | |||
38 | struct mpeg3_framesize_s | ||
39 | { | ||
40 | unsigned short bit_rate; | ||
41 | unsigned short frm_size[3]; | ||
42 | }; | ||
43 | |||
44 | struct mpeg3_framesize_s framesize_codes[] = | ||
45 | { | ||
46 | { 32 ,{64 ,69 ,96 } }, | ||
47 | { 32 ,{64 ,70 ,96 } }, | ||
48 | { 40 ,{80 ,87 ,120 } }, | ||
49 | { 40 ,{80 ,88 ,120 } }, | ||
50 | { 48 ,{96 ,104 ,144 } }, | ||
51 | { 48 ,{96 ,105 ,144 } }, | ||
52 | { 56 ,{112 ,121 ,168 } }, | ||
53 | { 56 ,{112 ,122 ,168 } }, | ||
54 | { 64 ,{128 ,139 ,192 } }, | ||
55 | { 64 ,{128 ,140 ,192 } }, | ||
56 | { 80 ,{160 ,174 ,240 } }, | ||
57 | { 80 ,{160 ,175 ,240 } }, | ||
58 | { 96 ,{192 ,208 ,288 } }, | ||
59 | { 96 ,{192 ,209 ,288 } }, | ||
60 | { 112 ,{224 ,243 ,336 } }, | ||
61 | { 112 ,{224 ,244 ,336 } }, | ||
62 | { 128 ,{256 ,278 ,384 } }, | ||
63 | { 128 ,{256 ,279 ,384 } }, | ||
64 | { 160 ,{320 ,348 ,480 } }, | ||
65 | { 160 ,{320 ,349 ,480 } }, | ||
66 | { 192 ,{384 ,417 ,576 } }, | ||
67 | { 192 ,{384 ,418 ,576 } }, | ||
68 | { 224 ,{448 ,487 ,672 } }, | ||
69 | { 224 ,{448 ,488 ,672 } }, | ||
70 | { 256 ,{512 ,557 ,768 } }, | ||
71 | { 256 ,{512 ,558 ,768 } }, | ||
72 | { 320 ,{640 ,696 ,960 } }, | ||
73 | { 320 ,{640 ,697 ,960 } }, | ||
74 | { 384 ,{768 ,835 ,1152 } }, | ||
75 | { 384 ,{768 ,836 ,1152 } }, | ||
76 | { 448 ,{896 ,975 ,1344 } }, | ||
77 | { 448 ,{896 ,976 ,1344 } }, | ||
78 | { 512 ,{1024 ,1114 ,1536 } }, | ||
79 | { 512 ,{1024 ,1115 ,1536 } }, | ||
80 | { 576 ,{1152 ,1253 ,1728 } }, | ||
81 | { 576 ,{1152 ,1254 ,1728 } }, | ||
82 | { 640 ,{1280 ,1393 ,1920 } }, | ||
83 | { 640 ,{1280 ,1394 ,1920 } } | ||
84 | }; | ||
85 | |||
86 | /* Audio channel modes */ | ||
87 | short mpeg3_ac3_acmodes[] = {2, 1, 2, 3, 3, 4, 4, 5}; | ||
88 | |||
89 | /* Rematrix tables */ | ||
90 | struct rematrix_band_s | ||
91 | { | ||
92 | int start; | ||
93 | int end; | ||
94 | }; | ||
95 | |||
96 | struct rematrix_band_s mpeg3_rematrix_band[] = | ||
97 | { | ||
98 | {13, 24}, | ||
99 | {25, 36}, | ||
100 | {37, 60}, | ||
101 | {61, 252} | ||
102 | }; | ||
103 | |||
104 | int mpeg3_min(int x, int y) | ||
105 | { | ||
106 | return (x < y) ? x : y; | ||
107 | } | ||
108 | |||
109 | int mpeg3_max(int x, int y) | ||
110 | { | ||
111 | return (x > y) ? x : y; | ||
112 | } | ||
113 | |||
114 | int mpeg3audio_read_ac3_header(mpeg3audio_t *audio) | ||
115 | { | ||
116 | unsigned int code, crc; | ||
117 | unsigned int i; | ||
118 | mpeg3_ac3bsi_t *bsi = &(audio->ac3_bsi); | ||
119 | |||
120 | /* Get the sync code */ | ||
121 | code = mpeg3bits_getbits(audio->astream, 16); | ||
122 | while(!mpeg3bits_eof(audio->astream) && code != MPEG3_AC3_START_CODE) | ||
123 | { | ||
124 | code <<= 8; | ||
125 | code &= 0xffff; | ||
126 | code |= mpeg3bits_getbits(audio->astream, 8); | ||
127 | } | ||
128 | |||
129 | if(mpeg3bits_eof(audio->astream)) return 1; | ||
130 | |||
131 | /* Get crc1 - we don't actually use this data though */ | ||
132 | /* The crc can be the same as the sync code or come after a sync code repeated twice */ | ||
133 | crc = mpeg3bits_getbits(audio->astream, 16); | ||
134 | |||
135 | /* Got the sync code. Read the entire frame into a buffer if possible. */ | ||
136 | if(audio->avg_framesize > 0) | ||
137 | { | ||
138 | if(mpeg3bits_read_buffer(audio->astream, audio->ac3_buffer, audio->framesize - 4)) | ||
139 | return 1; | ||
140 | mpeg3bits_use_ptr(audio->astream, audio->ac3_buffer); | ||
141 | } | ||
142 | |||
143 | /* Get the sampling rate code */ | ||
144 | audio->sampling_frequency_code = mpeg3bits_getbits(audio->astream, 2); | ||
145 | |||
146 | /* Get the frame size code */ | ||
147 | audio->ac3_framesize_code = mpeg3bits_getbits(audio->astream, 6); | ||
148 | |||
149 | audio->bitrate = framesize_codes[audio->ac3_framesize_code].bit_rate; | ||
150 | audio->avg_framesize = audio->framesize = 2 * framesize_codes[audio->ac3_framesize_code].frm_size[audio->sampling_frequency_code]; | ||
151 | |||
152 | /* Check the AC-3 version number */ | ||
153 | bsi->bsid = mpeg3bits_getbits(audio->astream, 5); | ||
154 | |||
155 | /* Get the audio service provided by the steram */ | ||
156 | bsi->bsmod = mpeg3bits_getbits(audio->astream, 3); | ||
157 | |||
158 | /* Get the audio coding mode (ie how many channels)*/ | ||
159 | bsi->acmod = mpeg3bits_getbits(audio->astream, 3); | ||
160 | |||
161 | /* Predecode the number of full bandwidth channels as we use this | ||
162 | * number a lot */ | ||
163 | bsi->nfchans = mpeg3_ac3_acmodes[bsi->acmod]; | ||
164 | audio->channels = bsi->nfchans; | ||
165 | |||
166 | /* If it is in use, get the centre channel mix level */ | ||
167 | if((bsi->acmod & 0x1) && (bsi->acmod != 0x1)) | ||
168 | bsi->cmixlev = mpeg3bits_getbits(audio->astream, 2); | ||
169 | |||
170 | /* If it is in use, get the surround channel mix level */ | ||
171 | if(bsi->acmod & 0x4) | ||
172 | bsi->surmixlev = mpeg3bits_getbits(audio->astream, 2); | ||
173 | |||
174 | /* Get the dolby surround mode if in 2/0 mode */ | ||
175 | if(bsi->acmod == 0x2) | ||
176 | bsi->dsurmod= mpeg3bits_getbits(audio->astream, 2); | ||
177 | |||
178 | /* Is the low frequency effects channel on? */ | ||
179 | bsi->lfeon = mpeg3bits_getbits(audio->astream, 1); | ||
180 | |||
181 | /* Get the dialogue normalization level */ | ||
182 | bsi->dialnorm = mpeg3bits_getbits(audio->astream, 5); | ||
183 | |||
184 | /* Does compression gain exist? */ | ||
185 | bsi->compre = mpeg3bits_getbits(audio->astream, 1); | ||
186 | if (bsi->compre) | ||
187 | { | ||
188 | /* Get compression gain */ | ||
189 | bsi->compr = mpeg3bits_getbits(audio->astream, 8); | ||
190 | } | ||
191 | |||
192 | /* Does language code exist? */ | ||
193 | bsi->langcode = mpeg3bits_getbits(audio->astream, 1); | ||
194 | if (bsi->langcode) | ||
195 | { | ||
196 | /* Get langauge code */ | ||
197 | bsi->langcod = mpeg3bits_getbits(audio->astream, 8); | ||
198 | } | ||
199 | |||
200 | /* Does audio production info exist? */ | ||
201 | bsi->audprodie = mpeg3bits_getbits(audio->astream, 1); | ||
202 | if (bsi->audprodie) | ||
203 | { | ||
204 | /* Get mix level */ | ||
205 | bsi->mixlevel = mpeg3bits_getbits(audio->astream, 5); | ||
206 | |||
207 | /* Get room type */ | ||
208 | bsi->roomtyp = mpeg3bits_getbits(audio->astream, 2); | ||
209 | } | ||
210 | |||
211 | /* If we're in dual mono mode then get some extra info */ | ||
212 | if (bsi->acmod == 0) | ||
213 | { | ||
214 | /* Get the dialogue normalization level two */ | ||
215 | bsi->dialnorm2 = mpeg3bits_getbits(audio->astream, 5); | ||
216 | |||
217 | /* Does compression gain two exist? */ | ||
218 | bsi->compr2e = mpeg3bits_getbits(audio->astream, 1); | ||
219 | if (bsi->compr2e) | ||
220 | { | ||
221 | /* Get compression gain two */ | ||
222 | bsi->compr2 = mpeg3bits_getbits(audio->astream, 8); | ||
223 | } | ||
224 | |||
225 | /* Does language code two exist? */ | ||
226 | bsi->langcod2e = mpeg3bits_getbits(audio->astream, 1); | ||
227 | if (bsi->langcod2e) | ||
228 | { | ||
229 | /* Get langauge code two */ | ||
230 | bsi->langcod2 = mpeg3bits_getbits(audio->astream, 8); | ||
231 | } | ||
232 | |||
233 | /* Does audio production info two exist? */ | ||
234 | bsi->audprodi2e = mpeg3bits_getbits(audio->astream, 1); | ||
235 | if (bsi->audprodi2e) | ||
236 | { | ||
237 | /* Get mix level two */ | ||
238 | bsi->mixlevel2 = mpeg3bits_getbits(audio->astream, 5); | ||
239 | |||
240 | /* Get room type two */ | ||
241 | bsi->roomtyp2 = mpeg3bits_getbits(audio->astream, 2); | ||
242 | } | ||
243 | } | ||
244 | |||
245 | /* Get the copyright bit */ | ||
246 | bsi->copyrightb = mpeg3bits_getbits(audio->astream, 1); | ||
247 | |||
248 | /* Get the original bit */ | ||
249 | bsi->origbs = mpeg3bits_getbits(audio->astream, 1); | ||
250 | |||
251 | /* Does timecode one exist? */ | ||
252 | bsi->timecod1e = mpeg3bits_getbits(audio->astream, 1); | ||
253 | |||
254 | if(bsi->timecod1e) | ||
255 | bsi->timecod1 = mpeg3bits_getbits(audio->astream, 14); | ||
256 | |||
257 | /* Does timecode two exist? */ | ||
258 | bsi->timecod2e = mpeg3bits_getbits(audio->astream, 1); | ||
259 | |||
260 | if(bsi->timecod2e) | ||
261 | bsi->timecod2 = mpeg3bits_getbits(audio->astream, 14); | ||
262 | |||
263 | /* Does addition info exist? */ | ||
264 | bsi->addbsie = mpeg3bits_getbits(audio->astream, 1); | ||
265 | |||
266 | if(bsi->addbsie) | ||
267 | { | ||
268 | /* Get how much info is there */ | ||
269 | bsi->addbsil = mpeg3bits_getbits(audio->astream, 6); | ||
270 | |||
271 | /* Get the additional info */ | ||
272 | for(i = 0; i < (bsi->addbsil + 1); i++) | ||
273 | bsi->addbsi[i] = mpeg3bits_getbits(audio->astream, 8); | ||
274 | } | ||
275 | |||
276 | if(mpeg3bits_eof(audio->astream)) | ||
277 | { | ||
278 | mpeg3bits_use_demuxer(audio->astream); | ||
279 | return 1; | ||
280 | } | ||
281 | //return mpeg3bits_error(audio->astream); | ||
282 | } | ||
283 | |||
284 | int mpeg3audio_read_ac3_audblk(mpeg3audio_t *audio) | ||
285 | { | ||
286 | int i, j; | ||
287 | mpeg3_ac3bsi_t *bsi = &(audio->ac3_bsi); | ||
288 | mpeg3_ac3audblk_t *audblk = &(audio->ac3_audblk); | ||
289 | |||
290 | for(i = 0; i < bsi->nfchans; i++) | ||
291 | { | ||
292 | /* Is this channel an interleaved 256 + 256 block ? */ | ||
293 | audblk->blksw[i] = mpeg3bits_getbits(audio->astream, 1); | ||
294 | } | ||
295 | |||
296 | for(i = 0; i < bsi->nfchans; i++) | ||
297 | { | ||
298 | /* Should we dither this channel? */ | ||
299 | audblk->dithflag[i] = mpeg3bits_getbits(audio->astream, 1); | ||
300 | } | ||
301 | |||
302 | /* Does dynamic range control exist? */ | ||
303 | audblk->dynrnge = mpeg3bits_getbits(audio->astream, 1); | ||
304 | if(audblk->dynrnge) | ||
305 | { | ||
306 | /* Get dynamic range info */ | ||
307 | audblk->dynrng = mpeg3bits_getbits(audio->astream, 8); | ||
308 | } | ||
309 | |||
310 | /* If we're in dual mono mode then get the second channel DR info */ | ||
311 | if(bsi->acmod == 0) | ||
312 | { | ||
313 | /* Does dynamic range control two exist? */ | ||
314 | audblk->dynrng2e = mpeg3bits_getbits(audio->astream, 1); | ||
315 | if (audblk->dynrng2e) | ||
316 | { | ||
317 | /* Get dynamic range info */ | ||
318 | audblk->dynrng2 = mpeg3bits_getbits(audio->astream, 8); | ||
319 | } | ||
320 | } | ||
321 | |||
322 | /* Does coupling strategy exist? */ | ||
323 | audblk->cplstre = mpeg3bits_getbits(audio->astream, 1); | ||
324 | if(audblk->cplstre) | ||
325 | { | ||
326 | /* Is coupling turned on? */ | ||
327 | audblk->cplinu = mpeg3bits_getbits(audio->astream, 1); | ||
328 | if(audblk->cplinu) | ||
329 | { | ||
330 | for(i = 0; i < bsi->nfchans; i++) | ||
331 | audblk->chincpl[i] = mpeg3bits_getbits(audio->astream, 1); | ||
332 | |||
333 | if(bsi->acmod == 0x2) | ||
334 | audblk->phsflginu = mpeg3bits_getbits(audio->astream, 1); | ||
335 | |||
336 | audblk->cplbegf = mpeg3bits_getbits(audio->astream, 4); | ||
337 | audblk->cplendf = mpeg3bits_getbits(audio->astream, 4); | ||
338 | audblk->ncplsubnd = (audblk->cplendf + 2) - audblk->cplbegf + 1; | ||
339 | |||
340 | /* Calculate the start and end bins of the coupling channel */ | ||
341 | audblk->cplstrtmant = (audblk->cplbegf * 12) + 37 ; | ||
342 | audblk->cplendmant = ((audblk->cplendf + 3) * 12) + 37; | ||
343 | |||
344 | /* The number of combined subbands is ncplsubnd minus each combined band */ | ||
345 | audblk->ncplbnd = audblk->ncplsubnd; | ||
346 | |||
347 | for(i = 1; i < audblk->ncplsubnd; i++) | ||
348 | { | ||
349 | audblk->cplbndstrc[i] = mpeg3bits_getbits(audio->astream, 1); | ||
350 | audblk->ncplbnd -= audblk->cplbndstrc[i]; | ||
351 | } | ||
352 | } | ||
353 | } | ||
354 | |||
355 | if(audblk->cplinu) | ||
356 | { | ||
357 | /* Loop through all the channels and get their coupling co-ords */ | ||
358 | for(i = 0; i < bsi->nfchans; i++) | ||
359 | { | ||
360 | if(!audblk->chincpl[i]) | ||
361 | continue; | ||
362 | |||
363 | /* Is there new coupling co-ordinate info? */ | ||
364 | audblk->cplcoe[i] = mpeg3bits_getbits(audio->astream, 1); | ||
365 | |||
366 | if(audblk->cplcoe[i]) | ||
367 | { | ||
368 | audblk->mstrcplco[i] = mpeg3bits_getbits(audio->astream, 2); | ||
369 | for(j = 0; j < audblk->ncplbnd; j++) | ||
370 | { | ||
371 | audblk->cplcoexp[i][j] = mpeg3bits_getbits(audio->astream, 4); | ||
372 | audblk->cplcomant[i][j] = mpeg3bits_getbits(audio->astream, 4); | ||
373 | } | ||
374 | } | ||
375 | } | ||
376 | |||
377 | /* If we're in dual mono mode, there's going to be some phase info */ | ||
378 | if((bsi->acmod == 0x2) && audblk->phsflginu && | ||
379 | (audblk->cplcoe[0] || audblk->cplcoe[1])) | ||
380 | { | ||
381 | for(j = 0; j < audblk->ncplbnd; j++) | ||
382 | { | ||
383 | audblk->phsflg[j] = mpeg3bits_getbits(audio->astream, 1); | ||
384 | } | ||
385 | } | ||
386 | } | ||
387 | |||
388 | /* If we're in dual mono mode, there may be a rematrix strategy */ | ||
389 | if(bsi->acmod == 0x2) | ||
390 | { | ||
391 | audblk->rematstr = mpeg3bits_getbits(audio->astream, 1); | ||
392 | if(audblk->rematstr) | ||
393 | { | ||
394 | if (audblk->cplinu == 0) | ||
395 | { | ||
396 | for(i = 0; i < 4; i++) | ||
397 | audblk->rematflg[i] = mpeg3bits_getbits(audio->astream, 1); | ||
398 | } | ||
399 | if((audblk->cplbegf > 2) && audblk->cplinu) | ||
400 | { | ||
401 | for(i = 0; i < 4; i++) | ||
402 | audblk->rematflg[i] = mpeg3bits_getbits(audio->astream, 1); | ||
403 | } | ||
404 | if((audblk->cplbegf <= 2) && audblk->cplinu) | ||
405 | { | ||
406 | for(i = 0; i < 3; i++) | ||
407 | audblk->rematflg[i] = mpeg3bits_getbits(audio->astream, 1); | ||
408 | } | ||
409 | if((audblk->cplbegf == 0) && audblk->cplinu) | ||
410 | for(i = 0; i < 2; i++) | ||
411 | audblk->rematflg[i] = mpeg3bits_getbits(audio->astream, 1); | ||
412 | |||
413 | } | ||
414 | } | ||
415 | |||
416 | if (audblk->cplinu) | ||
417 | { | ||
418 | /* Get the coupling channel exponent strategy */ | ||
419 | audblk->cplexpstr = mpeg3bits_getbits(audio->astream, 2); | ||
420 | |||
421 | if(audblk->cplexpstr == 0) | ||
422 | audblk->ncplgrps = 0; | ||
423 | else | ||
424 | audblk->ncplgrps = (audblk->cplendmant - audblk->cplstrtmant) / | ||
425 | (3 << (audblk->cplexpstr - 1)); | ||
426 | |||
427 | } | ||
428 | |||
429 | for(i = 0; i < bsi->nfchans; i++) | ||
430 | { | ||
431 | audblk->chexpstr[i] = mpeg3bits_getbits(audio->astream, 2); | ||
432 | } | ||
433 | |||
434 | /* Get the exponent strategy for lfe channel */ | ||
435 | if(bsi->lfeon) | ||
436 | audblk->lfeexpstr = mpeg3bits_getbits(audio->astream, 1); | ||
437 | |||
438 | /* Determine the bandwidths of all the fbw channels */ | ||
439 | for(i = 0; i < bsi->nfchans; i++) | ||
440 | { | ||
441 | unsigned short grp_size; | ||
442 | |||
443 | if(audblk->chexpstr[i] != MPEG3_EXP_REUSE) | ||
444 | { | ||
445 | if (audblk->cplinu && audblk->chincpl[i]) | ||
446 | { | ||
447 | audblk->endmant[i] = audblk->cplstrtmant; | ||
448 | } | ||
449 | else | ||
450 | { | ||
451 | audblk->chbwcod[i] = mpeg3bits_getbits(audio->astream, 6); | ||
452 | audblk->endmant[i] = ((audblk->chbwcod[i] + 12) * 3) + 37; | ||
453 | } | ||
454 | |||
455 | /* Calculate the number of exponent groups to fetch */ | ||
456 | grp_size = 3 * (1 << (audblk->chexpstr[i] - 1)); | ||
457 | audblk->nchgrps[i] = (audblk->endmant[i] - 1 + (grp_size - 3)) / grp_size; | ||
458 | } | ||
459 | } | ||
460 | |||
461 | /* Get the coupling exponents if they exist */ | ||
462 | if(audblk->cplinu && (audblk->cplexpstr != MPEG3_EXP_REUSE)) | ||
463 | { | ||
464 | audblk->cplabsexp = mpeg3bits_getbits(audio->astream, 4); | ||
465 | for(i = 0; i< audblk->ncplgrps; i++) | ||
466 | audblk->cplexps[i] = mpeg3bits_getbits(audio->astream, 7); | ||
467 | } | ||
468 | |||
469 | /* Get the fwb channel exponents */ | ||
470 | for(i = 0; i < bsi->nfchans; i++) | ||
471 | { | ||
472 | if(audblk->chexpstr[i] != MPEG3_EXP_REUSE) | ||
473 | { | ||
474 | audblk->exps[i][0] = mpeg3bits_getbits(audio->astream, 4); | ||
475 | for(j = 1; j <= audblk->nchgrps[i]; j++) | ||
476 | audblk->exps[i][j] = mpeg3bits_getbits(audio->astream, 7); | ||
477 | audblk->gainrng[i] = mpeg3bits_getbits(audio->astream, 2); | ||
478 | } | ||
479 | } | ||
480 | |||
481 | /* Get the lfe channel exponents */ | ||
482 | if(bsi->lfeon && (audblk->lfeexpstr != MPEG3_EXP_REUSE)) | ||
483 | { | ||
484 | audblk->lfeexps[0] = mpeg3bits_getbits(audio->astream, 4); | ||
485 | audblk->lfeexps[1] = mpeg3bits_getbits(audio->astream, 7); | ||
486 | audblk->lfeexps[2] = mpeg3bits_getbits(audio->astream, 7); | ||
487 | } | ||
488 | |||
489 | /* Get the parametric bit allocation parameters */ | ||
490 | audblk->baie = mpeg3bits_getbits(audio->astream, 1); | ||
491 | |||
492 | if(audblk->baie) | ||
493 | { | ||
494 | audblk->sdcycod = mpeg3bits_getbits(audio->astream, 2); | ||
495 | audblk->fdcycod = mpeg3bits_getbits(audio->astream, 2); | ||
496 | audblk->sgaincod = mpeg3bits_getbits(audio->astream, 2); | ||
497 | audblk->dbpbcod = mpeg3bits_getbits(audio->astream, 2); | ||
498 | audblk->floorcod = mpeg3bits_getbits(audio->astream, 3); | ||
499 | } | ||
500 | |||
501 | |||
502 | /* Get the SNR off set info if it exists */ | ||
503 | audblk->snroffste = mpeg3bits_getbits(audio->astream, 1); | ||
504 | |||
505 | if(audblk->snroffste) | ||
506 | { | ||
507 | audblk->csnroffst = mpeg3bits_getbits(audio->astream, 6); | ||
508 | |||
509 | if(audblk->cplinu) | ||
510 | { | ||
511 | audblk->cplfsnroffst = mpeg3bits_getbits(audio->astream, 4); | ||
512 | audblk->cplfgaincod = mpeg3bits_getbits(audio->astream, 3); | ||
513 | } | ||
514 | |||
515 | for(i = 0; i < bsi->nfchans; i++) | ||
516 | { | ||
517 | audblk->fsnroffst[i] = mpeg3bits_getbits(audio->astream, 4); | ||
518 | audblk->fgaincod[i] = mpeg3bits_getbits(audio->astream, 3); | ||
519 | } | ||
520 | if(bsi->lfeon) | ||
521 | { | ||
522 | |||
523 | audblk->lfefsnroffst = mpeg3bits_getbits(audio->astream, 4); | ||
524 | audblk->lfefgaincod = mpeg3bits_getbits(audio->astream, 3); | ||
525 | } | ||
526 | } | ||
527 | |||
528 | /* Get coupling leakage info if it exists */ | ||
529 | if(audblk->cplinu) | ||
530 | { | ||
531 | audblk->cplleake = mpeg3bits_getbits(audio->astream, 1); | ||
532 | |||
533 | if(audblk->cplleake) | ||
534 | { | ||
535 | audblk->cplfleak = mpeg3bits_getbits(audio->astream, 3); | ||
536 | audblk->cplsleak = mpeg3bits_getbits(audio->astream, 3); | ||
537 | } | ||
538 | } | ||
539 | |||
540 | /* Get the delta bit alloaction info */ | ||
541 | audblk->deltbaie = mpeg3bits_getbits(audio->astream, 1); | ||
542 | |||
543 | if(audblk->deltbaie) | ||
544 | { | ||
545 | if(audblk->cplinu) | ||
546 | audblk->cpldeltbae = mpeg3bits_getbits(audio->astream, 2); | ||
547 | |||
548 | for(i = 0; i < bsi->nfchans; i++) | ||
549 | audblk->deltbae[i] = mpeg3bits_getbits(audio->astream, 2); | ||
550 | |||
551 | if (audblk->cplinu && (audblk->cpldeltbae == DELTA_BIT_NEW)) | ||
552 | { | ||
553 | audblk->cpldeltnseg = mpeg3bits_getbits(audio->astream, 3); | ||
554 | for(i = 0; i < audblk->cpldeltnseg + 1; i++) | ||
555 | { | ||
556 | audblk->cpldeltoffst[i] = mpeg3bits_getbits(audio->astream, 5); | ||
557 | audblk->cpldeltlen[i] = mpeg3bits_getbits(audio->astream, 4); | ||
558 | audblk->cpldeltba[i] = mpeg3bits_getbits(audio->astream, 3); | ||
559 | } | ||
560 | } | ||
561 | |||
562 | for(i = 0; i < bsi->nfchans; i++) | ||
563 | { | ||
564 | if (audblk->deltbae[i] == DELTA_BIT_NEW) | ||
565 | { | ||
566 | audblk->deltnseg[i] = mpeg3bits_getbits(audio->astream, 3); | ||
567 | for(j = 0; j < audblk->deltnseg[i] + 1; j++) | ||
568 | { | ||
569 | audblk->deltoffst[i][j] = mpeg3bits_getbits(audio->astream, 5); | ||
570 | audblk->deltlen[i][j] = mpeg3bits_getbits(audio->astream, 4); | ||
571 | audblk->deltba[i][j] = mpeg3bits_getbits(audio->astream, 3); | ||
572 | } | ||
573 | } | ||
574 | } | ||
575 | } | ||
576 | |||
577 | /* Check to see if there's any dummy info to get */ | ||
578 | if((audblk->skiple = mpeg3bits_getbits(audio->astream, 1))) | ||
579 | { | ||
580 | unsigned int skip_data; | ||
581 | audblk->skipl = mpeg3bits_getbits(audio->astream, 9); | ||
582 | for(i = 0; i < audblk->skipl ; i++) | ||
583 | { | ||
584 | skip_data = mpeg3bits_getbits(audio->astream, 8); | ||
585 | } | ||
586 | } | ||
587 | |||
588 | return mpeg3bits_error(audio->astream); | ||
589 | } | ||
590 | |||
591 | |||
592 | /* This routine simply does stereo rematrixing for the 2 channel | ||
593 | * stereo mode */ | ||
594 | int mpeg3audio_ac3_rematrix(mpeg3_ac3audblk_t *audblk, | ||
595 | mpeg3ac3_stream_samples_t samples) | ||
596 | { | ||
597 | int num_bands; | ||
598 | int start; | ||
599 | int end; | ||
600 | int i, j; | ||
601 | mpeg3_real_t left, right; | ||
602 | |||
603 | if(audblk->cplinu || audblk->cplbegf > 2) | ||
604 | num_bands = 4; | ||
605 | else if (audblk->cplbegf > 0) | ||
606 | num_bands = 3; | ||
607 | else | ||
608 | num_bands = 2; | ||
609 | |||
610 | for(i = 0; i < num_bands; i++) | ||
611 | { | ||
612 | if(!audblk->rematflg[i]) | ||
613 | continue; | ||
614 | |||
615 | start = mpeg3_rematrix_band[i].start; | ||
616 | end = mpeg3_min(mpeg3_rematrix_band[i].end, 12 * audblk->cplbegf + 36); | ||
617 | |||
618 | for(j = start; j < end; j++) | ||
619 | { | ||
620 | left = samples[0][j] + samples[1][j]; | ||
621 | right = samples[0][j] - samples[1][j]; | ||
622 | samples[0][j] = left; | ||
623 | samples[1][j] = right; | ||
624 | } | ||
625 | } | ||
626 | return 0; | ||
627 | } | ||
628 | |||
629 | |||
630 | int mpeg3audio_ac3_reset_frame(mpeg3audio_t *audio) | ||
631 | { | ||
632 | memset(&audio->ac3_bit_allocation, 0, sizeof(mpeg3_ac3_bitallocation_t)); | ||
633 | memset(&audio->ac3_mantissa, 0, sizeof(mpeg3_ac3_mantissa_t)); | ||
634 | memset(&audio->ac3_audblk, 0, sizeof(mpeg3_ac3audblk_t)); | ||
635 | |||
636 | return 0; | ||
637 | } | ||
638 | |||
639 | int mpeg3audio_do_ac3(mpeg3audio_t *audio) | ||
640 | { | ||
641 | int result = 0, i; | ||
642 | |||
643 | /* Reset the coefficients and exponents */ | ||
644 | mpeg3audio_ac3_reset_frame(audio); | ||
645 | |||
646 | for(i = 0; i < 6 && !result; i++) | ||
647 | { | ||
648 | memset(audio->ac3_samples, 0, sizeof(mpeg3_real_t) * 256 * (audio->ac3_bsi.nfchans + audio->ac3_bsi.lfeon)); | ||
649 | /* Extract most of the audblk info from the bitstream | ||
650 | * (minus the mantissas */ | ||
651 | result |= mpeg3audio_read_ac3_audblk(audio); | ||
652 | |||
653 | /* Take the differential exponent data and turn it into | ||
654 | * absolute exponents */ | ||
655 | if(!result) result |= mpeg3audio_ac3_exponent_unpack(audio, | ||
656 | &(audio->ac3_bsi), | ||
657 | &(audio->ac3_audblk)); | ||
658 | |||
659 | /* Figure out how many bits per mantissa */ | ||
660 | if(!result) result |= mpeg3audio_ac3_bit_allocate(audio, | ||
661 | audio->sampling_frequency_code, | ||
662 | &(audio->ac3_bsi), | ||
663 | &(audio->ac3_audblk)); | ||
664 | |||
665 | /* Extract the mantissas from the data stream */ | ||
666 | if(!result) result |= mpeg3audio_ac3_coeff_unpack(audio, | ||
667 | &(audio->ac3_bsi), | ||
668 | &(audio->ac3_audblk), | ||
669 | audio->ac3_samples); | ||
670 | |||
671 | if(audio->ac3_bsi.acmod == 0x2) | ||
672 | if(!result) result |= mpeg3audio_ac3_rematrix(&(audio->ac3_audblk), | ||
673 | audio->ac3_samples); | ||
674 | |||
675 | /* Convert the frequency data into time samples */ | ||
676 | if(!result) result |= mpeg3audio_ac3_imdct(audio, | ||
677 | &(audio->ac3_bsi), | ||
678 | &(audio->ac3_audblk), | ||
679 | audio->ac3_samples); | ||
680 | |||
681 | if(audio->pcm_point / audio->channels >= audio->pcm_allocated - MPEG3AUDIO_PADDING * audio->channels) | ||
682 | { | ||
683 | /* Need more room */ | ||
684 | mpeg3audio_replace_buffer(audio, audio->pcm_allocated + MPEG3AUDIO_PADDING * audio->channels); | ||
685 | } | ||
686 | } | ||
687 | |||
688 | mpeg3bits_use_demuxer(audio->astream); | ||
689 | |||
690 | return result; | ||
691 | } | ||