Touchscreen mapping: Fix setting context
[kugel-rb.git] / apps / codecs / atrac3_rm.c
blob41399fda3d27da13a2d4a03d6ce7bc56134bed94
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 <string.h>
24 #include "logf.h"
25 #include "codeclib.h"
26 #include "inttypes.h"
27 #include "libatrac/atrac3.h"
29 CODEC_HEADER
31 RMContext rmctx;
32 RMPacket pkt;
33 ATRAC3Context q IBSS_ATTR;
35 static void init_rm(RMContext *rmctx)
37 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
40 /* this is the codec entry point */
41 enum codec_status codec_main(void)
43 static size_t buff_size;
44 int datasize, res, consumed, i, time_offset;
45 uint8_t *bit_buffer;
46 uint16_t fs,sps,h;
47 uint32_t packet_count;
48 int scrambling_unit_size, num_units, elapsed = 0;
49 int playback_on = -1;
50 size_t resume_offset = ci->id3->offset;
52 next_track:
53 if (codec_init()) {
54 DEBUGF("codec init failed\n");
55 return CODEC_ERROR;
57 while (!*ci->taginfo_ready && !ci->stop_codec)
58 ci->sleep(1);
60 codec_set_replaygain(ci->id3);
61 ci->memset(&rmctx,0,sizeof(RMContext));
62 ci->memset(&pkt,0,sizeof(RMPacket));
63 ci->memset(&q,0,sizeof(ATRAC3Context));
65 init_rm(&rmctx);
67 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
68 ci->configure(DSP_SET_SAMPLE_DEPTH, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
69 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
70 STEREO_MONO : STEREO_NONINTERLEAVED);
72 packet_count = rmctx.nb_packets;
73 rmctx.audio_framesize = rmctx.block_align;
74 rmctx.block_align = rmctx.sub_packet_size;
75 fs = rmctx.audio_framesize;
76 sps= rmctx.block_align;
77 h = rmctx.sub_packet_h;
78 scrambling_unit_size = h*fs;
80 res =atrac3_decode_init(&q, &rmctx);
81 if(res < 0) {
82 DEBUGF("failed to initialize atrac decoder\n");
83 return CODEC_ERROR;
86 /* check for a mid-track resume and force a seek time accordingly */
87 if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
88 resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
89 num_units = (int)resume_offset / scrambling_unit_size;
90 /* put number of subpackets to skip in resume_offset */
91 resume_offset /= (sps + PACKET_HEADER_SIZE);
92 ci->seek_time = (int)resume_offset * ((sps * 8 * 1000)/rmctx.bit_rate);
95 ci->set_elapsed(0);
96 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
98 /* The main decoder loop */
99 seek_start :
100 while((unsigned)elapsed < rmctx.duration)
102 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
103 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
104 if(consumed < 0 && playback_on != 0) {
105 if(playback_on == -1) {
106 /* Error only if packet-parsing failed and playback hadn't started */
107 DEBUGF("rm_get_packet failed\n");
108 return CODEC_ERROR;
110 else
111 goto done;
114 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
116 ci->yield();
117 if (ci->stop_codec || ci->new_track)
118 goto done;
120 if (ci->seek_time) {
121 ci->set_elapsed(ci->seek_time);
123 /* Do not allow seeking beyond the file's length */
124 if ((unsigned) ci->seek_time > ci->id3->length) {
125 ci->seek_complete();
126 goto done;
129 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
130 packet_count = rmctx.nb_packets;
131 rmctx.audio_pkt_cnt = 0;
132 rmctx.frame_number = 0;
134 /* Seek to the start of the track */
135 if (ci->seek_time == 1) {
136 ci->set_elapsed(0);
137 ci->seek_complete();
138 goto seek_start;
140 num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
141 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
142 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
143 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
144 if(consumed < 0 && playback_on != 0) {
145 if(playback_on == -1) {
146 /* Error only if packet-parsing failed and playback hadn't started */
147 DEBUGF("rm_get_packet failed\n");
148 return CODEC_ERROR;
150 else
151 goto done;
154 packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
155 rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate));
156 while(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
157 rmctx.audio_pkt_cnt = 0;
158 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
159 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
160 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
161 packet_count += rmctx.audio_pkt_cnt;
162 num_units--;
164 time_offset = ci->seek_time - rmctx.audiotimestamp;
165 i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
166 elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
167 ci->set_elapsed(elapsed);
168 ci->seek_complete();
170 if(pkt.length)
171 res = atrac3_decode_frame(&rmctx, &q, &datasize, pkt.frames[i], rmctx.block_align);
172 else /* indicates that there are no remaining frames */
173 goto done;
175 if(res != rmctx.block_align) {
176 DEBUGF("codec error\n");
177 return CODEC_ERROR;
180 if(datasize)
181 ci->pcmbuf_insert(q.outSamples, q.outSamples + 1024, q.samples_per_frame / rmctx.nb_channels);
182 playback_on = 1;
183 elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
184 ci->set_elapsed(elapsed);
185 rmctx.frame_number++;
187 packet_count -= rmctx.audio_pkt_cnt;
188 rmctx.audio_pkt_cnt = 0;
189 ci->advance_buffer(consumed);
192 done :
193 if (ci->request_next_track())
194 goto next_track;
196 return CODEC_OK;