Update the discussion of themeing in the manual, and put a note in the wps tags appen...
[kugel-rb.git] / apps / codecs / a52_rm.c
blob822d717a81703e664a3b46b918e8d96217dbd333
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Mohamed Tarek
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 <codecs/librm/rm.h>
24 #include <inttypes.h> /* Needed by a52.h */
25 #include <codecs/liba52/config-a52.h>
26 #include <codecs/liba52/a52.h>
28 CODEC_HEADER
30 #define BUFFER_SIZE 4096
32 #define A52_SAMPLESPERFRAME (6*256)
34 static a52_state_t *state;
35 unsigned long samplesdone;
36 unsigned long frequency;
37 RMContext rmctx;
38 RMPacket pkt;
40 static void init_rm(RMContext *rmctx)
42 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
45 /* used outside liba52 */
46 static uint8_t buf[3840] IBSS_ATTR;
48 /* The following two functions, a52_decode_data and output_audio are taken from apps/codecs/a52.c */
49 static inline void output_audio(sample_t *samples)
51 ci->yield();
52 ci->pcmbuf_insert(&samples[0], &samples[256], 256);
55 static void a52_decode_data(uint8_t *start, uint8_t *end)
57 static uint8_t *bufptr = buf;
58 static uint8_t *bufpos = buf + 7;
60 * sample_rate and flags are static because this routine could
61 * exit between the a52_syncinfo() and the ao_setup(), and we want
62 * to have the same values when we get back !
64 static int sample_rate;
65 static int flags;
66 int bit_rate;
67 int len;
69 while (1) {
70 len = end - start;
71 if (!len)
72 break;
73 if (len > bufpos - bufptr)
74 len = bufpos - bufptr;
75 memcpy(bufptr, start, len);
76 bufptr += len;
77 start += len;
78 if (bufptr == bufpos) {
79 if (bufpos == buf + 7) {
80 int length;
82 length = a52_syncinfo(buf, &flags, &sample_rate, &bit_rate);
83 if (!length) {
84 //DEBUGF("skip\n");
85 for (bufptr = buf; bufptr < buf + 6; bufptr++)
86 bufptr[0] = bufptr[1];
87 continue;
89 bufpos = buf + length;
90 } else {
91 /* Unity gain is 1 << 26, and we want to end up on 28 bits
92 of precision instead of the default 30.
94 level_t level = 1 << 24;
95 sample_t bias = 0;
96 int i;
98 /* This is the configuration for the downmixing: */
99 flags = A52_STEREO | A52_ADJUST_LEVEL;
101 if (a52_frame(state, buf, &flags, &level, bias))
102 goto error;
103 a52_dynrng(state, NULL, NULL);
104 frequency = sample_rate;
106 /* An A52 frame consists of 6 blocks of 256 samples
107 So we decode and output them one block at a time */
108 for (i = 0; i < 6; i++) {
109 if (a52_block(state))
110 goto error;
111 output_audio(a52_samples(state));
112 samplesdone += 256;
114 ci->set_elapsed(samplesdone/(frequency/1000));
115 bufptr = buf;
116 bufpos = buf + 7;
117 continue;
118 error:
119 //logf("Error decoding A52 stream\n");
120 bufptr = buf;
121 bufpos = buf + 7;
128 /* this is the codec entry point */
129 enum codec_status codec_main(void)
131 size_t n;
132 uint8_t *filebuf;
133 int retval, consumed, packet_offset;
134 int playback_on = -1;
135 size_t resume_offset = ci->id3->offset;
137 /* Generic codec initialisation */
138 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
139 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
141 next_track:
142 if (codec_init()) {
143 retval = CODEC_ERROR;
144 goto exit;
147 while (!ci->taginfo_ready)
148 ci->yield();
150 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
151 codec_set_replaygain(ci->id3);
153 /* Intializations */
154 state = a52_init(0);
155 ci->memset(&rmctx,0,sizeof(RMContext));
156 ci->memset(&pkt,0,sizeof(RMPacket));
157 init_rm(&rmctx);
159 /* check for a mid-track resume and force a seek time accordingly */
160 if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
161 resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
162 /* put number of subpackets to skip in resume_offset */
163 resume_offset /= (rmctx.block_align + PACKET_HEADER_SIZE);
164 ci->seek_time = (int)resume_offset * ((rmctx.block_align * 8 * 1000)/rmctx.bit_rate);
167 /* Seek to the first packet */
168 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE );
170 /* The main decoding loop */
171 while((unsigned)rmctx.audio_pkt_cnt < rmctx.nb_packets) {
172 ci->yield();
173 if (ci->stop_codec || ci->new_track)
174 break;
176 if (ci->seek_time) {
177 packet_offset = ci->seek_time / ((rmctx.block_align*8*1000)/rmctx.bit_rate);
178 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + packet_offset*(rmctx.block_align + PACKET_HEADER_SIZE));
179 rmctx.audio_pkt_cnt = packet_offset;
180 samplesdone = (rmctx.sample_rate/1000 * ci->seek_time);
181 ci->seek_complete();
184 filebuf = ci->request_buffer(&n, rmctx.block_align + PACKET_HEADER_SIZE);
185 consumed = rm_get_packet(&filebuf, &rmctx, &pkt);
187 if(consumed < 0 && playback_on != 0) {
188 if(playback_on == -1) {
189 /* Error only if packet-parsing failed and playback hadn't started */
190 DEBUGF("rm_get_packet failed\n");
191 return CODEC_ERROR;
193 else {
194 retval = CODEC_OK;
195 goto exit;
199 playback_on = 1;
200 a52_decode_data(filebuf, filebuf + rmctx.block_align);
201 ci->advance_buffer(pkt.length);
204 retval = CODEC_OK;
206 if (ci->request_next_track())
207 goto next_track;
209 exit:
210 a52_free(state);
211 return retval;