Merge branch 'master' into android-test-plugins
[kugel-rb.git] / apps / codecs / cook.c
blob82739694d8ea3832d8e6f8d0824e46462a75c955
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 "libcook/cook.h"
29 CODEC_HEADER
31 static RMContext rmctx;
32 static RMPacket pkt;
33 static COOKContext q IBSS_ATTR;
34 static int32_t rm_outbuf[2048];
36 static void init_rm(RMContext *rmctx)
38 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
41 /* this is the codec entry point */
42 enum codec_status codec_main(enum codec_entry_call_reason reason)
44 /* Nothing to do */
45 return CODEC_OK;
46 (void)reason;
49 /* this is called for each file to process */
50 enum codec_status codec_run(void)
52 static size_t buff_size;
53 int datasize, res, consumed, i, time_offset;
54 uint8_t *bit_buffer;
55 uint16_t fs,sps,h;
56 uint32_t packet_count;
57 int scrambling_unit_size, num_units;
58 size_t resume_offset;
59 intptr_t param = 0;
60 enum codec_command_action action = CODEC_ACTION_NULL;
62 if (codec_init()) {
63 DEBUGF("codec init failed\n");
64 return CODEC_ERROR;
67 resume_offset = ci->id3->offset;
69 codec_set_replaygain(ci->id3);
70 ci->memset(&rmctx,0,sizeof(RMContext));
71 ci->memset(&pkt,0,sizeof(RMPacket));
72 ci->memset(&q,0,sizeof(COOKContext));
74 ci->seek_buffer(0);
76 init_rm(&rmctx);
78 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
79 /* cook's sample representation is 21.11
80 * DSP_SET_SAMPLE_DEPTH = 11 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 26 */
81 ci->configure(DSP_SET_SAMPLE_DEPTH, 26);
82 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
83 STEREO_MONO : STEREO_NONINTERLEAVED);
85 packet_count = rmctx.nb_packets;
86 rmctx.audio_framesize = rmctx.block_align;
87 rmctx.block_align = rmctx.sub_packet_size;
88 fs = rmctx.audio_framesize;
89 sps= rmctx.block_align;
90 h = rmctx.sub_packet_h;
91 scrambling_unit_size = h*fs;
93 res =cook_decode_init(&rmctx, &q);
94 if(res < 0) {
95 DEBUGF("failed to initialize cook decoder\n");
96 return CODEC_ERROR;
99 /* check for a mid-track resume and force a seek time accordingly */
100 if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
101 resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
102 num_units = (int)resume_offset / scrambling_unit_size;
103 /* put number of subpackets to skip in resume_offset */
104 resume_offset /= (sps + PACKET_HEADER_SIZE);
105 param = (int)resume_offset * ((sps * 8 * 1000)/rmctx.bit_rate);
106 action = CODEC_ACTION_SEEK_TIME;
109 ci->set_elapsed(0);
110 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
112 /* The main decoder loop */
113 seek_start :
114 while(packet_count)
116 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
117 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
118 if(consumed < 0) {
119 DEBUGF("rm_get_packet failed\n");
120 return CODEC_ERROR;
123 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
125 if (action == CODEC_ACTION_NULL)
126 action = ci->get_command(&param);
128 if (action == CODEC_ACTION_HALT)
129 return CODEC_OK;
131 if (action == CODEC_ACTION_SEEK_TIME) {
132 /* Do not allow seeking beyond the file's length */
133 if ((unsigned) param > ci->id3->length) {
134 ci->set_elapsed(ci->id3->length);
135 ci->seek_complete();
136 return CODEC_OK;
139 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
140 packet_count = rmctx.nb_packets;
141 rmctx.audio_pkt_cnt = 0;
142 rmctx.frame_number = 0;
144 /* Seek to the start of the track */
145 if (param == 0) {
146 ci->set_elapsed(0);
147 ci->seek_complete();
148 action = CODEC_ACTION_NULL;
149 goto seek_start;
151 num_units = (param/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
152 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
153 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
154 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
155 if(consumed < 0) {
156 DEBUGF("rm_get_packet failed\n");
157 ci->seek_complete();
158 return CODEC_ERROR;
160 packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
161 rmctx.frame_number = (param/(sps*1000*8/rmctx.bit_rate));
162 while(rmctx.audiotimestamp > (unsigned) param) {
163 rmctx.audio_pkt_cnt = 0;
164 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
165 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
166 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
167 packet_count += rmctx.audio_pkt_cnt;
168 num_units--;
170 time_offset = param - rmctx.audiotimestamp;
171 i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
172 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
173 ci->seek_complete();
176 action = CODEC_ACTION_NULL;
178 res = cook_decode_frame(&rmctx,&q, rm_outbuf, &datasize, pkt.frames[i], rmctx.block_align);
179 rmctx.frame_number++;
181 /* skip the first two frames; no valid audio */
182 if(rmctx.frame_number < 3) continue;
184 if(res != rmctx.block_align) {
185 DEBUGF("codec error\n");
186 return CODEC_ERROR;
189 ci->pcmbuf_insert(rm_outbuf,
190 rm_outbuf+q.samples_per_channel,
191 q.samples_per_channel);
192 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
194 packet_count -= rmctx.audio_pkt_cnt;
195 rmctx.audio_pkt_cnt = 0;
196 ci->advance_buffer(consumed);
199 return CODEC_OK;