summaryrefslogtreecommitdiff
path: root/apps/codecs/cook.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/cook.c')
-rw-r--r--apps/codecs/cook.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/apps/codecs/cook.c b/apps/codecs/cook.c
new file mode 100644
index 0000000000..7b4b8e7461
--- /dev/null
+++ b/apps/codecs/cook.c
@@ -0,0 +1,144 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Copyright (C) 2009 Mohamed Tarek
9 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include <string.h>
21
22#include "logf.h"
23#include "codeclib.h"
24#include "inttypes.h"
25#include "libcook/cook.h"
26
27#define DATA_HEADER_SIZE 18 /* size of DATA chunk header in a rm file */
28
29CODEC_HEADER
30
31RMContext rmctx;
32RMPacket pkt;
33COOKContext q;
34
35static void init_rm(RMContext *rmctx)
36{
37 memcpy(rmctx, ci->id3->id3v2buf, sizeof(RMContext));
38}
39
40/* this is the codec entry point */
41enum codec_status codec_main(void)
42{
43 static size_t buff_size;
44 int datasize, res, consumed,i;
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;
50
51next_track:
52 if (codec_init()) {
53 DEBUGF("codec init failed\n");
54 return CODEC_ERROR;
55 }
56 while (!*ci->taginfo_ready && !ci->stop_codec)
57 ci->sleep(1);
58
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));
63
64 init_rm(&rmctx);
65
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);
70
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;
78
79 res =cook_decode_init(&rmctx, &q);
80 if(res < 0) {
81 DEBUGF("failed to initialize cook decoder\n");
82 return CODEC_ERROR;
83 }
84
85 ci->set_elapsed(0);
86 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
87
88 /* The main decoder loop */
89 while (1)
90 {
91 /*if (ci->seek_time) {
92
93 ci->set_elapsed(ci->seek_time);
94 n = ci->seek_time/10;
95 memset(buf,0,BUF_SIZE);
96 ci->seek_complete();
97 }*/
98
99 while(packet_count)
100 {
101 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
102 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
103 if(consumed < 0) {
104 DEBUGF("rm_get_packet failed\n");
105 return CODEC_ERROR;
106 }
107 /*DEBUGF(" version = %d\n"
108 " length = %d\n"
109 " stream = %d\n"
110 " timestamp= %d\n",pkt.version,pkt.length,pkt.stream_number,pkt.timestamp);*/
111
112 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
113 {
114 ci->yield();
115 if (ci->stop_codec || ci->new_track)
116 goto done;
117
118 res = cook_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i], rmctx.block_align);
119 rmctx.frame_number++;
120
121 /* skip the first two frames; no valid audio */
122 if(rmctx.frame_number < 3) continue;
123
124 if(res != rmctx.block_align) {
125 DEBUGF("codec error\n");
126 return CODEC_ERROR;
127 }
128
129 ci->pcmbuf_insert(outbuf, NULL, rmctx.samples_pf_pc / rmctx.nb_channels);
130 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
131 }
132 packet_count -= rmctx.audio_pkt_cnt;
133 rmctx.audio_pkt_cnt = 0;
134 ci->advance_buffer(consumed);
135 }
136 goto done;
137
138 }
139 done :
140 if (ci->request_next_track())
141 goto next_track;
142
143 return CODEC_OK;
144}