summaryrefslogtreecommitdiff
path: root/apps/codecs
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2009-05-09 23:24:02 +0000
committerDave Chapman <dave@dchapman.com>2009-05-09 23:24:02 +0000
commit7ba6ef42de037837a91c8d58a318ceaf7e5edab0 (patch)
treeeb672d272556a65210b50f33d7fd28f0635b5fa7 /apps/codecs
parent868652abaa163de646a3d530b9ecfa4a137b7d9b (diff)
downloadrockbox-7ba6ef42de037837a91c8d58a318ceaf7e5edab0.tar.gz
rockbox-7ba6ef42de037837a91c8d58a318ceaf7e5edab0.zip
Patch by Mohamed Tarek from FS#10182: 1) Move the main() test program from cook.c to a new main.c; 2) Move some common definitions from cook.c to cook.h. No functional changes.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20898 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs')
-rw-r--r--apps/codecs/libcook/Makefile.test2
-rw-r--r--apps/codecs/libcook/cook.c185
-rw-r--r--apps/codecs/libcook/cook.h122
-rw-r--r--apps/codecs/libcook/main.c114
4 files changed, 241 insertions, 182 deletions
diff --git a/apps/codecs/libcook/Makefile.test b/apps/codecs/libcook/Makefile.test
index 98ee7ffcb4..5b60ca3ef5 100644
--- a/apps/codecs/libcook/Makefile.test
+++ b/apps/codecs/libcook/Makefile.test
@@ -1,5 +1,5 @@
1CFLAGS = -Wall -O3 1CFLAGS = -Wall -O3
2OBJS = bitstream.o cook.o fft.o libavutil/log.o mdct.o libavutil/mem.o libavutil/lfg.o libavutil/md5.o rm2wav.o 2OBJS = main.o bitstream.o cook.o fft.o libavutil/log.o mdct.o libavutil/mem.o libavutil/lfg.o libavutil/md5.o rm2wav.o
3cooktest: $(OBJS) 3cooktest: $(OBJS)
4 gcc -o cooktest $(OBJS) -lm 4 gcc -o cooktest $(OBJS) -lm
5 5
diff --git a/apps/codecs/libcook/cook.c b/apps/codecs/libcook/cook.c
index 856004f31b..e3a919ddec 100644
--- a/apps/codecs/libcook/cook.c
+++ b/apps/codecs/libcook/cook.c
@@ -45,18 +45,9 @@
45#include <math.h> 45#include <math.h>
46#include <stddef.h> 46#include <stddef.h>
47#include <stdio.h> 47#include <stdio.h>
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <fcntl.h>
51#include <unistd.h>
52
53#include "libavutil/lfg.h"
54#include "bitstream.h"
55#include "dsputil.h"
56#include "bytestream.h"
57 48
49#include "cook.h"
58#include "cookdata.h" 50#include "cookdata.h"
59#include "rm2wav.h"
60 51
61/* The following table is taken from libavutil/mathematics.c */ 52/* The following table is taken from libavutil/mathematics.c */
62const uint8_t ff_log2_tab[256]={ 53const uint8_t ff_log2_tab[256]={
@@ -82,90 +73,6 @@ const uint8_t ff_log2_tab[256]={
82//#define DUMP_RAW_FRAMES 73//#define DUMP_RAW_FRAMES
83#define DEBUGF(message,args ...) av_log(NULL,AV_LOG_ERROR,message,## args) 74#define DEBUGF(message,args ...) av_log(NULL,AV_LOG_ERROR,message,## args)
84 75
85typedef struct {
86 int *now;
87 int *previous;
88} cook_gains;
89
90typedef struct cook {
91 /*
92 * The following 5 functions provide the lowlevel arithmetic on
93 * the internal audio buffers.
94 */
95 void (* scalar_dequant)(struct cook *q, int index, int quant_index,
96 int* subband_coef_index, int* subband_coef_sign,
97 float* mlt_p);
98
99 void (* decouple) (struct cook *q,
100 int subband,
101 float f1, float f2,
102 float *decode_buffer,
103 float *mlt_buffer1, float *mlt_buffer2);
104
105 void (* imlt_window) (struct cook *q, float *buffer1,
106 cook_gains *gains_ptr, float *previous_buffer);
107
108 void (* interpolate) (struct cook *q, float* buffer,
109 int gain_index, int gain_index_next);
110
111 void (* saturate_output) (struct cook *q, int chan, int16_t *out);
112
113 GetBitContext gb;
114 int frame_number;
115 int block_align;
116 int extradata_size;
117 /* stream data */
118 int nb_channels;
119 int joint_stereo;
120 int bit_rate;
121 int sample_rate;
122 int samples_per_channel;
123 int samples_per_frame;
124 int subbands;
125 int log2_numvector_size;
126 int numvector_size; //1 << log2_numvector_size;
127 int js_subband_start;
128 int total_subbands;
129 int num_vectors;
130 int bits_per_subpacket;
131 int cookversion;
132 /* states */
133 AVLFG random_state;
134 /* transform data */
135 MDCTContext mdct_ctx;
136 float* mlt_window;
137
138 /* gain buffers */
139 cook_gains gains1;
140 cook_gains gains2;
141 int gain_1[9];
142 int gain_2[9];
143 int gain_3[9];
144 int gain_4[9];
145
146 /* VLC data */
147 int js_vlc_bits;
148 VLC envelope_quant_index[13];
149 VLC sqvh[7]; //scalar quantization
150 VLC ccpl; //channel coupling
151
152 /* generatable tables and related variables */
153 int gain_size_factor;
154 float gain_table[23];
155
156 /* data buffers */
157
158 uint8_t* decoded_bytes_buffer;
159 float mono_mdct_output[2048] __attribute__ ((aligned(16))); //DECLARE_ALIGNED_16(float,mono_mdct_output[2048]);
160 float mono_previous_buffer1[1024];
161 float mono_previous_buffer2[1024];
162 float decode_buffer_1[1024];
163 float decode_buffer_2[1024];
164 float decode_buffer_0[1060]; /* static allocation for joint decode */
165
166 const float *cplscales[5];
167} COOKContext;
168
169static float pow2tab[127]; 76static float pow2tab[127];
170static float rootpow2tab[127]; 77static float rootpow2tab[127];
171 78
@@ -335,7 +242,7 @@ static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes)
335 * Cook uninit 242 * Cook uninit
336 */ 243 */
337 244
338static av_cold int cook_decode_close(COOKContext *q) 245av_cold int cook_decode_close(COOKContext *q)
339{ 246{
340 int i; 247 int i;
341 //COOKContext *q = avctx->priv_data; 248 //COOKContext *q = avctx->priv_data;
@@ -997,7 +904,7 @@ static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer,
997 * @param rmctx pointer to the RMContext 904 * @param rmctx pointer to the RMContext
998 */ 905 */
999 906
1000static int cook_decode_frame(RMContext *rmctx,COOKContext *q, 907int cook_decode_frame(RMContext *rmctx,COOKContext *q,
1001 int16_t *outbuffer, int *data_size, 908 int16_t *outbuffer, int *data_size,
1002 const uint8_t *inbuffer, int buf_size) { 909 const uint8_t *inbuffer, int buf_size) {
1003 //COOKContext *q = avctx->priv_data; 910 //COOKContext *q = avctx->priv_data;
@@ -1053,7 +960,7 @@ static av_cold int cook_count_channels(unsigned int mask){
1053 * Cook initialization 960 * Cook initialization
1054 */ 961 */
1055 962
1056static av_cold int cook_decode_init(RMContext *rmctx, COOKContext *q) 963av_cold int cook_decode_init(RMContext *rmctx, COOKContext *q)
1057{ 964{
1058 /* cook extradata */ 965 /* cook extradata */
1059 q->cookversion = rmctx->cook_version; 966 q->cookversion = rmctx->cook_version;
@@ -1203,87 +1110,3 @@ static av_cold int cook_decode_init(RMContext *rmctx, COOKContext *q)
1203 return 0; 1110 return 0;
1204} 1111}
1205 1112
1206
1207int main(int argc, char *argv[])
1208{
1209 int fd, fd_dec;
1210 int res, datasize,x,i;
1211 int nb_frames = 0;
1212#ifdef DUMP_RAW_FRAMES
1213 char filename[15];
1214 int fd_out;
1215#endif
1216 int16_t outbuf[2048];
1217 uint8_t inbuf[1024];
1218 uint16_t fs,sps,h;
1219 uint32_t packet_count;
1220 COOKContext q;
1221 RMContext rmctx;
1222 RMPacket pkt;
1223
1224 memset(&q,0,sizeof(COOKContext));
1225 memset(&rmctx,0,sizeof(RMContext));
1226 memset(&pkt,0,sizeof(RMPacket));
1227
1228 if (argc != 2) {
1229 av_log(NULL,AV_LOG_ERROR,"Incorrect number of arguments\n");
1230 return -1;
1231 }
1232
1233 fd = open(argv[1],O_RDONLY);
1234 if (fd < 0) {
1235 av_log(NULL,AV_LOG_ERROR,"Error opening file %s\n", argv[1]);
1236 return -1;
1237 }
1238
1239 fd_dec = open_wav("output.wav");
1240 if (fd_dec < 0) {
1241 av_log(NULL,AV_LOG_ERROR,"Error creating output file\n");
1242 return -1;
1243 }
1244 res = real_parse_header(fd, &rmctx);
1245 packet_count = rmctx.nb_packets;
1246 rmctx.audio_framesize = rmctx.block_align;
1247 rmctx.block_align = rmctx.sub_packet_size;
1248 fs = rmctx.audio_framesize;
1249 sps= rmctx.block_align;
1250 h = rmctx.sub_packet_h;
1251 cook_decode_init(&rmctx,&q);
1252 av_log(NULL,AV_LOG_ERROR,"nb_frames = %d\n",nb_frames);
1253 x = 0;
1254 if(packet_count % h)
1255 {
1256 packet_count += h - (packet_count % h);
1257 rmctx.nb_packets = packet_count;
1258 }
1259 while(packet_count)
1260 {
1261
1262 memset(pkt.data,0,sizeof(pkt.data));
1263 rm_get_packet(fd, &rmctx, &pkt);
1264 DEBUGF("total frames = %d packet count = %d output counter = %d \n",rmctx.audio_pkt_cnt*(fs/sps), packet_count,rmctx.audio_pkt_cnt);
1265 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
1266 {
1267 /* output raw audio frames that are sent to the decoder into separate files */
1268 #ifdef DUMP_RAW_FRAMES
1269 snprintf(filename,sizeof(filename),"dump%d.raw",++x);
1270 fd_out = open(filename,O_WRONLY|O_CREAT|O_APPEND);
1271 write(fd_out,pkt.data+i*sps,sps);
1272 close(fd_out);
1273 #endif
1274
1275 memcpy(inbuf,pkt.data+i*sps,sps);
1276 nb_frames = cook_decode_frame(&rmctx,&q, outbuf, &datasize, inbuf , rmctx.block_align);
1277 rmctx.frame_number++;
1278 write(fd_dec,outbuf,datasize);
1279 }
1280 packet_count -= rmctx.audio_pkt_cnt;
1281 rmctx.audio_pkt_cnt = 0;
1282 }
1283 cook_decode_close(&q);
1284 close_wav(fd_dec,&rmctx);
1285 close(fd);
1286
1287
1288 return 0;
1289}
diff --git a/apps/codecs/libcook/cook.h b/apps/codecs/libcook/cook.h
new file mode 100644
index 0000000000..8d4c078f1a
--- /dev/null
+++ b/apps/codecs/libcook/cook.h
@@ -0,0 +1,122 @@
1/*
2 * COOK compatible decoder
3 * Copyright (c) 2003 Sascha Sommer
4 * Copyright (c) 2005 Benjamin Larsson
5 *
6 * This file is taken from FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef _COOK_H
24#define _COOK_H
25
26#include <stdint.h>
27#include "libavutil/lfg.h"
28#include "bitstream.h"
29#include "dsputil.h"
30#include "bytestream.h"
31#include "rm2wav.h"
32
33typedef struct {
34 int *now;
35 int *previous;
36} cook_gains;
37
38typedef struct cook {
39 /*
40 * The following 5 functions provide the lowlevel arithmetic on
41 * the internal audio buffers.
42 */
43 void (* scalar_dequant)(struct cook *q, int index, int quant_index,
44 int* subband_coef_index, int* subband_coef_sign,
45 float* mlt_p);
46
47 void (* decouple) (struct cook *q,
48 int subband,
49 float f1, float f2,
50 float *decode_buffer,
51 float *mlt_buffer1, float *mlt_buffer2);
52
53 void (* imlt_window) (struct cook *q, float *buffer1,
54 cook_gains *gains_ptr, float *previous_buffer);
55
56 void (* interpolate) (struct cook *q, float* buffer,
57 int gain_index, int gain_index_next);
58
59 void (* saturate_output) (struct cook *q, int chan, int16_t *out);
60
61 GetBitContext gb;
62 int frame_number;
63 int block_align;
64 int extradata_size;
65 /* stream data */
66 int nb_channels;
67 int joint_stereo;
68 int bit_rate;
69 int sample_rate;
70 int samples_per_channel;
71 int samples_per_frame;
72 int subbands;
73 int log2_numvector_size;
74 int numvector_size; //1 << log2_numvector_size;
75 int js_subband_start;
76 int total_subbands;
77 int num_vectors;
78 int bits_per_subpacket;
79 int cookversion;
80 /* states */
81 AVLFG random_state;
82 /* transform data */
83 MDCTContext mdct_ctx;
84 float* mlt_window;
85
86 /* gain buffers */
87 cook_gains gains1;
88 cook_gains gains2;
89 int gain_1[9];
90 int gain_2[9];
91 int gain_3[9];
92 int gain_4[9];
93
94 /* VLC data */
95 int js_vlc_bits;
96 VLC envelope_quant_index[13];
97 VLC sqvh[7]; //scalar quantization
98 VLC ccpl; //channel coupling
99
100 /* generatable tables and related variables */
101 int gain_size_factor;
102 float gain_table[23];
103
104 /* data buffers */
105
106 uint8_t* decoded_bytes_buffer;
107 float mono_mdct_output[2048] __attribute__ ((aligned(16))); //DECLARE_ALIGNED_16(float,mono_mdct_output[2048]);
108 float mono_previous_buffer1[1024];
109 float mono_previous_buffer2[1024];
110 float decode_buffer_1[1024];
111 float decode_buffer_2[1024];
112 float decode_buffer_0[1060]; /* static allocation for joint decode */
113
114 const float *cplscales[5];
115} COOKContext;
116
117av_cold int cook_decode_init(RMContext *rmctx, COOKContext *q);
118int cook_decode_frame(RMContext *rmctx,COOKContext *q,
119 int16_t *outbuffer, int *data_size,
120 const uint8_t *inbuffer, int buf_size);
121av_cold int cook_decode_close(COOKContext *q);
122#endif
diff --git a/apps/codecs/libcook/main.c b/apps/codecs/libcook/main.c
new file mode 100644
index 0000000000..2f85295fb5
--- /dev/null
+++ b/apps/codecs/libcook/main.c
@@ -0,0 +1,114 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2009 Mohamed Tarek
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include <stdint.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <unistd.h>
26
27#include "rm2wav.h"
28#include "cook.h"
29
30//#define DUMP_RAW_FRAMES
31#define DEBUGF(message,args ...) av_log(NULL,AV_LOG_ERROR,message,## args)
32int main(int argc, char *argv[])
33{
34 int fd, fd_dec;
35 int res, datasize,x,i;
36 int nb_frames = 0;
37#ifdef DUMP_RAW_FRAMES
38 char filename[15];
39 int fd_out;
40#endif
41 int16_t outbuf[2048];
42 uint8_t inbuf[1024];
43 uint16_t fs,sps,h;
44 uint32_t packet_count;
45 COOKContext q;
46 RMContext rmctx;
47 RMPacket pkt;
48
49 memset(&q,0,sizeof(COOKContext));
50 memset(&rmctx,0,sizeof(RMContext));
51 memset(&pkt,0,sizeof(RMPacket));
52
53 if (argc != 2) {
54 DEBUGF("Incorrect number of arguments\n");
55 return -1;
56 }
57
58 fd = open(argv[1],O_RDONLY);
59 if (fd < 0) {
60 DEBUGF("Error opening file %s\n", argv[1]);
61 return -1;
62 }
63
64 fd_dec = open_wav("output.wav");
65 if (fd_dec < 0) {
66 DEBUGF("Error creating output file\n");
67 return -1;
68 }
69 res = real_parse_header(fd, &rmctx);
70 packet_count = rmctx.nb_packets;
71 rmctx.audio_framesize = rmctx.block_align;
72 rmctx.block_align = rmctx.sub_packet_size;
73 fs = rmctx.audio_framesize;
74 sps= rmctx.block_align;
75 h = rmctx.sub_packet_h;
76 cook_decode_init(&rmctx,&q);
77 av_log(NULL,AV_LOG_ERROR,"nb_frames = %d\n",nb_frames);
78 x = 0;
79 if(packet_count % h)
80 {
81 packet_count += h - (packet_count % h);
82 rmctx.nb_packets = packet_count;
83 }
84 while(packet_count)
85 {
86
87 memset(pkt.data,0,sizeof(pkt.data));
88 rm_get_packet(fd, &rmctx, &pkt);
89 DEBUGF("total frames = %d packet count = %d output counter = %d \n",rmctx.audio_pkt_cnt*(fs/sps), packet_count,rmctx.audio_pkt_cnt);
90 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
91 {
92 /* output raw audio frames that are sent to the decoder into separate files */
93 #ifdef DUMP_RAW_FRAMES
94 snprintf(filename,sizeof(filename),"dump%d.raw",++x);
95 fd_out = open(filename,O_WRONLY|O_CREAT|O_APPEND);
96 write(fd_out,pkt.data+i*sps,sps);
97 close(fd_out);
98 #endif
99
100 memcpy(inbuf,pkt.data+i*sps,sps);
101 nb_frames = cook_decode_frame(&rmctx,&q, outbuf, &datasize, inbuf , rmctx.block_align);
102 rmctx.frame_number++;
103 write(fd_dec,outbuf,datasize);
104 }
105 packet_count -= rmctx.audio_pkt_cnt;
106 rmctx.audio_pkt_cnt = 0;
107 }
108 cook_decode_close(&q);
109 close_wav(fd_dec,&rmctx);
110 close(fd);
111
112
113 return 0;
114}