Meizu: fix mask for GPIO function selection used for fmradio
[kugel-rb.git] / apps / codecs / cook.c
blobec30b3e6bc2b8b4dd55efaa442207a6ede598a46
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Copyright (C) 2009 Mohamed Tarek
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <string.h>
22 #include "logf.h"
23 #include "codeclib.h"
24 #include "inttypes.h"
25 #include "libcook/cook.h"
27 #define DATA_HEADER_SIZE 18 /* size of DATA chunk header in a rm file */
29 CODEC_HEADER
31 RMContext rmctx;
32 RMPacket pkt;
33 COOKContext 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 int16_t outbuf[2048] __attribute__((aligned(32)));
47 uint16_t fs,sps,h;
48 uint32_t packet_count;
49 int scrambling_unit_size, num_units;
51 next_track:
52 if (codec_init()) {
53 DEBUGF("codec init failed\n");
54 return CODEC_ERROR;
56 while (!*ci->taginfo_ready && !ci->stop_codec)
57 ci->sleep(1);
59 codec_set_replaygain(ci->id3);
60 ci->memset(&rmctx,0,sizeof(RMContext));
61 ci->memset(&pkt,0,sizeof(RMPacket));
62 ci->memset(&q,0,sizeof(COOKContext));
64 init_rm(&rmctx);
66 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
67 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
68 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
69 STEREO_MONO : STEREO_INTERLEAVED);
71 packet_count = rmctx.nb_packets;
72 rmctx.audio_framesize = rmctx.block_align;
73 rmctx.block_align = rmctx.sub_packet_size;
74 fs = rmctx.audio_framesize;
75 sps= rmctx.block_align;
76 h = rmctx.sub_packet_h;
77 scrambling_unit_size = h*fs;
79 res =cook_decode_init(&rmctx, &q);
80 if(res < 0) {
81 DEBUGF("failed to initialize cook decoder\n");
82 return CODEC_ERROR;
85 ci->set_elapsed(0);
86 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
88 /* The main decoder loop */
89 seek_start :
90 while(packet_count)
92 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
93 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
94 if(consumed < 0) {
95 DEBUGF("rm_get_packet failed\n");
96 return CODEC_ERROR;
99 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
101 ci->yield();
102 if (ci->stop_codec || ci->new_track)
103 goto done;
105 if (ci->seek_time) {
106 ci->set_elapsed(ci->seek_time);
108 /* Do not allow seeking beyond the file's length */
109 if ((unsigned) ci->seek_time > ci->id3->length) {
110 ci->seek_complete();
111 goto done;
114 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
115 packet_count = rmctx.nb_packets;
116 rmctx.audio_pkt_cnt = 0;
117 rmctx.frame_number = 0;
119 /* Seek to the start of the track */
120 if (ci->seek_time == 1) {
121 ci->set_elapsed(0);
122 ci->seek_complete();
123 goto seek_start;
125 num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
126 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
127 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
128 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
129 if(consumed < 0) {
130 DEBUGF("rm_get_packet failed\n");
131 return CODEC_ERROR;
133 packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
134 rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate));
135 while(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
136 rmctx.audio_pkt_cnt = 0;
137 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
138 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
139 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
140 packet_count += rmctx.audio_pkt_cnt;
141 num_units--;
143 time_offset = ci->seek_time - rmctx.audiotimestamp;
144 i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
145 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
146 ci->seek_complete();
148 res = cook_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i], rmctx.block_align);
149 rmctx.frame_number++;
151 /* skip the first two frames; no valid audio */
152 if(rmctx.frame_number < 3) continue;
154 if(res != rmctx.block_align) {
155 DEBUGF("codec error\n");
156 return CODEC_ERROR;
159 ci->pcmbuf_insert(outbuf, NULL, q.samples_per_frame / rmctx.nb_channels);
160 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
162 packet_count -= rmctx.audio_pkt_cnt;
163 rmctx.audio_pkt_cnt = 0;
164 ci->advance_buffer(consumed);
167 done :
168 if (ci->request_next_track())
169 goto next_track;
171 return CODEC_OK;