Pull yesno_pop out of the radio code as it is a nice simple resuasble yesno api worth...
[kugel-rb.git] / apps / codecs / a52.c
blob4cd293e37fac2118a83bf2280b48a19a54e387f3
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 static unsigned long samplesdone;
35 static 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(enum codec_entry_call_reason reason)
121 if (reason == CODEC_LOAD) {
122 /* Generic codec initialisation */
123 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
124 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
126 else if (reason == CODEC_UNLOAD) {
127 if (state)
128 a52_free(state);
131 return CODEC_OK;
134 /* this is called for each file to process */
135 enum codec_status codec_run(void)
137 size_t n;
138 unsigned char *filebuf;
139 int sample_loc;
140 intptr_t param;
142 if (codec_init())
143 return CODEC_ERROR;
145 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
146 codec_set_replaygain(ci->id3);
148 /* Intialise the A52 decoder and check for success */
149 state = a52_init(0);
151 /* The main decoding loop */
152 if (ci->id3->offset) {
153 if (ci->seek_buffer(ci->id3->offset)) {
154 samplesdone = (ci->id3->offset / ci->id3->bytesperframe) *
155 A52_SAMPLESPERFRAME;
156 ci->set_elapsed(samplesdone/(ci->id3->frequency / 1000));
159 else {
160 ci->seek_buffer(ci->id3->first_frame_offset);
161 samplesdone = 0;
164 while (1) {
165 enum codec_command_action action = ci->get_command(&param);
167 if (action == CODEC_ACTION_HALT)
168 break;
170 if (action == CODEC_ACTION_SEEK_TIME) {
171 sample_loc = param/1000 * ci->id3->frequency;
173 if (ci->seek_buffer((sample_loc/A52_SAMPLESPERFRAME)*ci->id3->bytesperframe)) {
174 samplesdone = sample_loc;
175 ci->set_elapsed(samplesdone/(ci->id3->frequency/1000));
177 ci->seek_complete();
180 filebuf = ci->request_buffer(&n, BUFFER_SIZE);
182 if (n == 0) /* End of Stream */
183 break;
185 a52_decode_data(filebuf, filebuf + n);
186 ci->advance_buffer(n);
189 return CODEC_OK;