rockpaint: steal the big buffer from audiobuffer
[kugel-rb.git] / apps / codecs / atrac3_rm.c
bloba0b0fd8b4b10aadc774c1c9ac222f86d80e66928
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 /* initialize the RMContext */
38 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
40 /* and atrac3 expects extadata in id3v2buf, so we shall give it that */
41 memcpy(ci->id3->id3v2buf, (char*)rmctx->codec_extradata, rmctx->extradata_size*sizeof(char));
44 /* this is the codec entry point */
45 enum codec_status codec_main(void)
47 static size_t buff_size;
48 int datasize, res, consumed, i, time_offset;
49 uint8_t *bit_buffer;
50 uint16_t fs,sps,h;
51 uint32_t packet_count;
52 int scrambling_unit_size, num_units, elapsed = 0;
53 int playback_on = -1;
54 size_t resume_offset = ci->id3->offset;
56 next_track:
57 if (codec_init()) {
58 DEBUGF("codec init failed\n");
59 return CODEC_ERROR;
61 while (!*ci->taginfo_ready && !ci->stop_codec)
62 ci->sleep(1);
64 codec_set_replaygain(ci->id3);
65 ci->memset(&rmctx,0,sizeof(RMContext));
66 ci->memset(&pkt,0,sizeof(RMPacket));
67 ci->memset(&q,0,sizeof(ATRAC3Context));
69 init_rm(&rmctx);
71 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
72 ci->configure(DSP_SET_SAMPLE_DEPTH, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
73 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
74 STEREO_MONO : STEREO_NONINTERLEAVED);
76 packet_count = rmctx.nb_packets;
77 rmctx.audio_framesize = rmctx.block_align;
78 rmctx.block_align = rmctx.sub_packet_size;
79 fs = rmctx.audio_framesize;
80 sps= rmctx.block_align;
81 h = rmctx.sub_packet_h;
82 scrambling_unit_size = h*fs;
84 res =atrac3_decode_init(&q, ci->id3);
85 if(res < 0) {
86 DEBUGF("failed to initialize RM atrac decoder\n");
87 return CODEC_ERROR;
90 /* check for a mid-track resume and force a seek time accordingly */
91 if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
92 resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
93 num_units = (int)resume_offset / scrambling_unit_size;
94 /* put number of subpackets to skip in resume_offset */
95 resume_offset /= (sps + PACKET_HEADER_SIZE);
96 ci->seek_time = (int)resume_offset * ((sps * 8 * 1000)/rmctx.bit_rate);
99 ci->set_elapsed(0);
100 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
102 /* The main decoder loop */
103 seek_start :
104 while((unsigned)elapsed < rmctx.duration)
106 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
107 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
108 if(consumed < 0 && playback_on != 0) {
109 if(playback_on == -1) {
110 /* Error only if packet-parsing failed and playback hadn't started */
111 DEBUGF("rm_get_packet failed\n");
112 return CODEC_ERROR;
114 else
115 goto done;
118 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
120 ci->yield();
121 if (ci->stop_codec || ci->new_track)
122 goto done;
124 if (ci->seek_time) {
125 ci->set_elapsed(ci->seek_time);
127 /* Do not allow seeking beyond the file's length */
128 if ((unsigned) ci->seek_time > ci->id3->length) {
129 ci->seek_complete();
130 goto done;
133 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
134 packet_count = rmctx.nb_packets;
135 rmctx.audio_pkt_cnt = 0;
136 rmctx.frame_number = 0;
138 /* Seek to the start of the track */
139 if (ci->seek_time == 1) {
140 ci->set_elapsed(0);
141 ci->seek_complete();
142 goto seek_start;
144 num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
145 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
146 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
147 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
148 if(consumed < 0 && playback_on != 0) {
149 if(playback_on == -1) {
150 /* Error only if packet-parsing failed and playback hadn't started */
151 DEBUGF("rm_get_packet failed\n");
152 return CODEC_ERROR;
154 else
155 goto done;
158 packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
159 rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate));
160 while(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
161 rmctx.audio_pkt_cnt = 0;
162 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
163 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
164 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
165 packet_count += rmctx.audio_pkt_cnt;
166 num_units--;
168 time_offset = ci->seek_time - rmctx.audiotimestamp;
169 i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
170 elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
171 ci->set_elapsed(elapsed);
172 ci->seek_complete();
174 if(pkt.length)
175 res = atrac3_decode_frame(rmctx.block_align, &q, &datasize, pkt.frames[i], rmctx.block_align);
176 else /* indicates that there are no remaining frames */
177 goto done;
179 if(res != rmctx.block_align) {
180 DEBUGF("codec error\n");
181 return CODEC_ERROR;
184 if(datasize)
185 ci->pcmbuf_insert(q.outSamples, q.outSamples + 1024, q.samples_per_frame / rmctx.nb_channels);
186 playback_on = 1;
187 elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
188 ci->set_elapsed(elapsed);
189 rmctx.frame_number++;
191 packet_count -= rmctx.audio_pkt_cnt;
192 rmctx.audio_pkt_cnt = 0;
193 ci->advance_buffer(consumed);
196 done :
197 if (ci->request_next_track())
198 goto next_track;
200 return CODEC_OK;