-Remove all dynamic allocations, hence remove cook_decode_close() which was basically
[kugel-rb.git] / apps / codecs / libcook / main.c
blob3c671e0c3f37a3a14fc8b8105c25fef12ef267f0
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 ****************************************************************************/
21 #include <stdint.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <string.h>
28 #include "rm2wav.h"
29 #include "cook.h"
31 //#define DUMP_RAW_FRAMES
32 #ifndef DEBUGF
33 # if 0
34 # define DEBUGF(message,args ...) printf
35 # else
36 # define DEBUGF(...)
37 # endif
38 #endif
40 int main(int argc, char *argv[])
42 int fd, fd_dec;
43 int res, datasize,x,i;
44 int nb_frames = 0;
45 #ifdef DUMP_RAW_FRAMES
46 char filename[15];
47 int fd_out;
48 #endif
49 int16_t outbuf[2048];
50 uint8_t inbuf[1024];
51 uint16_t fs,sps,h;
52 uint32_t packet_count;
53 COOKContext q;
54 RMContext rmctx;
55 RMPacket pkt;
57 memset(&q,0,sizeof(COOKContext));
58 memset(&rmctx,0,sizeof(RMContext));
59 memset(&pkt,0,sizeof(RMPacket));
61 if (argc != 2) {
62 DEBUGF("Incorrect number of arguments\n");
63 return -1;
66 fd = open(argv[1],O_RDONLY);
67 if (fd < 0) {
68 DEBUGF("Error opening file %s\n", argv[1]);
69 return -1;
72 fd_dec = open_wav("output.wav");
73 if (fd_dec < 0) {
74 DEBUGF("Error creating output file\n");
75 return -1;
77 res = real_parse_header(fd, &rmctx);
78 packet_count = rmctx.nb_packets;
79 rmctx.audio_framesize = rmctx.block_align;
80 rmctx.block_align = rmctx.sub_packet_size;
81 fs = rmctx.audio_framesize;
82 sps= rmctx.block_align;
83 h = rmctx.sub_packet_h;
84 cook_decode_init(&rmctx,&q);
85 DEBUGF("nb_frames = %d\n",nb_frames);
86 x = 0;
87 if(packet_count % h)
89 packet_count += h - (packet_count % h);
90 rmctx.nb_packets = packet_count;
92 while(packet_count)
95 memset(pkt.data,0,sizeof(pkt.data));
96 rm_get_packet(fd, &rmctx, &pkt);
97 DEBUGF("total frames = %d packet count = %d output counter = %d \n",rmctx.audio_pkt_cnt*(fs/sps), packet_count,rmctx.audio_pkt_cnt);
98 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
100 /* output raw audio frames that are sent to the decoder into separate files */
101 #ifdef DUMP_RAW_FRAMES
102 snprintf(filename,sizeof(filename),"dump%d.raw",++x);
103 fd_out = open(filename,O_WRONLY|O_CREAT|O_APPEND);
104 write(fd_out,pkt.data+i*sps,sps);
105 close(fd_out);
106 #endif
108 memcpy(inbuf,pkt.data+i*sps,sps);
109 nb_frames = cook_decode_frame(&rmctx,&q, outbuf, &datasize, inbuf , rmctx.block_align);
110 rmctx.frame_number++;
111 write(fd_dec,outbuf,datasize);
113 packet_count -= rmctx.audio_pkt_cnt;
114 rmctx.audio_pkt_cnt = 0;
116 close_wav(fd_dec,&rmctx);
117 close(fd);
120 return 0;