lcd-m6sp.c: remove \r
[kugel-rb.git] / apps / codecs / a52.c
blob360a5862d7b9548d955e621f8ed45222b463fc1b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "codeclib.h"
23 #include <inttypes.h> /* Needed by a52.h */
24 #include <codecs/liba52/config-a52.h>
25 #include <codecs/liba52/a52.h>
27 CODEC_HEADER
29 #define BUFFER_SIZE 4096
31 #define A52_SAMPLESPERFRAME (6*256)
33 static a52_state_t *state;
34 unsigned long samplesdone;
35 unsigned long frequency;
37 /* used outside liba52 */
38 static uint8_t buf[3840] IBSS_ATTR;
40 static inline void output_audio(sample_t *samples)
42 ci->yield();
43 ci->pcmbuf_insert(&samples[0], &samples[256], 256);
46 static void a52_decode_data(uint8_t *start, uint8_t *end)
48 static uint8_t *bufptr = buf;
49 static uint8_t *bufpos = buf + 7;
51 * sample_rate and flags are static because this routine could
52 * exit between the a52_syncinfo() and the ao_setup(), and we want
53 * to have the same values when we get back !
55 static int sample_rate;
56 static int flags;
57 int bit_rate;
58 int len;
60 while (1) {
61 len = end - start;
62 if (!len)
63 break;
64 if (len > bufpos - bufptr)
65 len = bufpos - bufptr;
66 memcpy(bufptr, start, len);
67 bufptr += len;
68 start += len;
69 if (bufptr == bufpos) {
70 if (bufpos == buf + 7) {
71 int length;
73 length = a52_syncinfo(buf, &flags, &sample_rate, &bit_rate);
74 if (!length) {
75 //DEBUGF("skip\n");
76 for (bufptr = buf; bufptr < buf + 6; bufptr++)
77 bufptr[0] = bufptr[1];
78 continue;
80 bufpos = buf + length;
81 } else {
82 /* Unity gain is 1 << 26, and we want to end up on 28 bits
83 of precision instead of the default 30.
85 level_t level = 1 << 24;
86 sample_t bias = 0;
87 int i;
89 /* This is the configuration for the downmixing: */
90 flags = A52_STEREO | A52_ADJUST_LEVEL;
92 if (a52_frame(state, buf, &flags, &level, bias))
93 goto error;
94 a52_dynrng(state, NULL, NULL);
95 frequency = sample_rate;
97 /* An A52 frame consists of 6 blocks of 256 samples
98 So we decode and output them one block at a time */
99 for (i = 0; i < 6; i++) {
100 if (a52_block(state))
101 goto error;
102 output_audio(a52_samples(state));
103 samplesdone += 256;
105 ci->set_elapsed(samplesdone/(frequency/1000));
106 bufptr = buf;
107 bufpos = buf + 7;
108 continue;
109 error:
110 //logf("Error decoding A52 stream\n");
111 bufptr = buf;
112 bufpos = buf + 7;
118 /* this is the codec entry point */
119 enum codec_status codec_main(void)
121 size_t n;
122 unsigned char *filebuf;
123 int sample_loc;
124 int retval;
126 /* Generic codec initialisation */
127 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
128 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
130 next_track:
131 if (codec_init()) {
132 retval = CODEC_ERROR;
133 goto exit;
136 while (!ci->taginfo_ready)
137 ci->yield();
139 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
140 codec_set_replaygain(ci->id3);
142 /* Intialise the A52 decoder and check for success */
143 state = a52_init(0);
145 /* The main decoding loop */
146 if (ci->id3->offset) {
147 if (ci->seek_buffer(ci->id3->offset)) {
148 samplesdone = (ci->id3->offset / ci->id3->bytesperframe) *
149 A52_SAMPLESPERFRAME;
150 ci->set_elapsed(samplesdone/(ci->id3->frequency / 1000));
153 else {
154 samplesdone = 0;
157 while (1) {
158 if (ci->stop_codec || ci->new_track)
159 break;
161 if (ci->seek_time) {
162 sample_loc = (ci->seek_time - 1)/1000 * ci->id3->frequency;
164 if (ci->seek_buffer((sample_loc/A52_SAMPLESPERFRAME)*ci->id3->bytesperframe)) {
165 samplesdone = sample_loc;
166 ci->set_elapsed(samplesdone/(ci->id3->frequency/1000));
168 ci->seek_complete();
171 filebuf = ci->request_buffer(&n, BUFFER_SIZE);
173 if (n == 0) /* End of Stream */
174 break;
176 a52_decode_data(filebuf, filebuf + n);
177 ci->advance_buffer(n);
179 retval = CODEC_OK;
181 if (ci->request_next_track())
182 goto next_track;
184 exit:
185 a52_free(state);
186 return retval;