Some fixes for the standalone test program.
[kugel-rb.git] / apps / codecs / librm / rm.c
bloba126a5cad1431e3355f6461d7b7fb87e674853cd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 Dave Chapman
11 * Copyright (C) 2009 Mohamed Tarek
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include <stdio.h>
23 #include <string.h>
25 #include "rm.h"
26 #ifdef ROCKBOX
27 #include "codeclib.h"
28 #endif
30 void advance_buffer(uint8_t **buf, int val)
32 *buf += val;
35 static uint8_t get_uint8(uint8_t *buf)
37 return (uint8_t)buf[0];
40 #ifdef ROCKBOX_BIG_ENDIAN
41 static uint16_t get_uint16be(uint8_t *buf)
43 return (uint16_t)((buf[1] << 8)|buf[0]);
46 static uint32_t get_uint32be(uint8_t *buf)
48 return (uint32_t)((buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]);
51 #else
52 static uint16_t get_uint16be(uint8_t *buf)
54 return (uint16_t)((buf[0] << 8)|buf[1]);
57 static uint32_t get_uint32be(uint8_t *buf)
59 return (uint32_t)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
61 #endif /* ROCKBOX_BIG_ENDIAN */
63 #ifdef TEST
64 #include <fcntl.h>
65 #include <unistd.h>
66 #include <sys/types.h>
67 #include <sys/stat.h>
69 int filesize(int fd)
71 struct stat buf;
73 if (fstat(fd,&buf) == -1) {
74 return -1;
75 } else {
76 return (int)buf.st_size;
80 static int read_uint8(int fd, uint8_t* buf)
82 unsigned char tmp[1];
83 int res;
85 res=read(fd, tmp, 1);
86 *buf = tmp[0];
87 return res;
90 static int read_uint16be(int fd, uint16_t* buf)
92 unsigned char tmp[2];
93 int res;
95 res=read(fd, tmp, 2);
96 *buf = (tmp[0] << 8) | tmp[1];
97 return res;
100 static int read_uint32be(int fd, uint32_t* buf)
102 unsigned char tmp[4];
103 int res;
105 res=read(fd, tmp, 4);
106 *buf = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3];
107 return res;
112 static int read_cook_extradata(int fd, RMContext *rmctx) {
113 read_uint32be(fd, &rmctx->cook_version);
114 read_uint16be(fd, &rmctx->samples_pf_pc);
115 read_uint16be(fd, &rmctx->nb_subbands);
116 if(rmctx->extradata_size == 16) {
117 read_uint32be(fd, &rmctx->unused);
118 read_uint16be(fd, &rmctx->js_subband_start);
119 read_uint16be(fd, &rmctx->js_vlc_bits);
121 return rmctx->extradata_size; /* for 'skipped' */
124 static void print_cook_extradata(RMContext *rmctx) {
126 DEBUGF(" cook_version = 0x%08x\n", rmctx->cook_version);
127 DEBUGF(" samples_per_frame_per_channel = %d\n", rmctx->samples_pf_pc);
128 DEBUGF(" number_of_subbands_in_freq_domain = %d\n", rmctx->nb_subbands);
129 if(rmctx->extradata_size == 16) {
130 DEBUGF(" joint_stereo_subband_start = %d\n",rmctx->js_subband_start);
131 DEBUGF(" joint_stereo_vlc_bits = %d\n", rmctx->js_vlc_bits);
136 struct real_object_t
138 uint32_t fourcc;
139 uint32_t size;
140 uint16_t version;
143 #define FOURCC(a,b,c,d) (((a)<<24) | ((b) << 16) | ((c) << 8) | (d))
145 static int real_read_object_header(int fd, struct real_object_t* obj)
147 int n;
149 if ((n = read_uint32be(fd, &obj->fourcc)) <= 0) return n;
150 if ((n = read_uint32be(fd, &obj->size)) <= 0) return n;
151 if ((n = read_uint16be(fd, &obj->version)) <= 0) return n;
153 return 1;
156 static char* fourcc2str(uint32_t f)
158 static char res[5];
160 res[0] = (f & 0xff000000) >> 24;
161 res[1] = (f & 0xff0000) >> 16;
162 res[2] = (f & 0xff00) >> 8;
163 res[3] = (f & 0xff);
164 res[4] = 0;
166 return res;
169 static int read_str(int fd, char* buf)
171 uint8_t len;
172 int res;
174 res = read(fd, &len, 1);
175 res = read(fd, buf, len);
176 buf[len]=0;
178 return len+1;
181 static int real_read_audio_stream_info(int fd, RMContext *rmctx)
183 int skipped = 0;
184 uint32_t version;
185 struct real_object_t obj;
186 memset(&obj,0,sizeof(obj));
187 uint32_t header_size;
188 uint16_t flavor;
189 uint32_t coded_framesize;
190 uint32_t unknown1;
191 uint32_t unknown2;
192 uint32_t unknown3;
193 uint16_t unknown4;
194 uint16_t unknown5;
195 uint16_t unknown6;
196 uint16_t unknown7;
197 uint32_t unknown8;
198 uint8_t interleaver_id_length;
199 uint32_t interleaver_id;
200 uint8_t fourcc_length;
201 uint32_t fourcc = 0;
202 uint8_t unknown9;
203 uint16_t unknown10;
204 uint8_t unknown11;
206 read_uint32be(fd, &version);
207 skipped += 4;
209 DEBUGF(" version=0x%04x\n",((version >> 16) & 0xff));
210 if (((version >> 16) & 0xff) == 3) {
211 /* Very old version */
212 } else {
213 real_read_object_header(fd, &obj);
214 skipped += 10;
215 read_uint32be(fd, &header_size);
216 skipped += 4;
217 /* obj.size will be filled with an unknown value, replaced with header_size */
218 DEBUGF(" Object: %s, size: %d bytes, version: 0x%04x\n",fourcc2str(obj.fourcc),header_size,obj.version);
220 read_uint16be(fd, &flavor);
221 read_uint32be(fd, &coded_framesize);
222 read_uint32be(fd, &unknown1);
223 read_uint32be(fd, &unknown2);
224 read_uint32be(fd, &unknown3);
225 read_uint16be(fd, &rmctx->sub_packet_h);
226 read_uint16be(fd, &rmctx->block_align);
227 read_uint16be(fd, &rmctx->sub_packet_size);
228 read_uint16be(fd, &unknown4);
229 skipped += 26;
230 if (((version >> 16) & 0xff) == 5)
232 read_uint16be(fd, &unknown5);
233 read_uint16be(fd, &unknown6);
234 read_uint16be(fd, &unknown7);
235 skipped += 6;
237 read_uint16be(fd, &rmctx->sample_rate);
238 read_uint32be(fd, &unknown8);
239 read_uint16be(fd, &rmctx->nb_channels);
240 skipped += 8;
241 if (((version >> 16) & 0xff) == 4)
243 read_uint8(fd, &interleaver_id_length);
244 read_uint32be(fd, &interleaver_id);
245 read_uint8(fd, &fourcc_length);
246 read_uint32be(fd, &fourcc);
247 skipped += 10;
249 if (((version >> 16) & 0xff) == 5)
251 read_uint32be(fd, &interleaver_id);
252 read_uint32be(fd, &fourcc);
253 skipped += 8;
255 read_uint8(fd,&unknown9);
256 read_uint16be(fd,&unknown10);
257 skipped += 3;
258 if (((version >> 16) & 0xff) == 5)
260 read_uint8(fd, &unknown11);
261 skipped += 1;
264 read_uint32be(fd, &rmctx->extradata_size);
265 skipped += 4;
266 if(!strncmp(fourcc2str(fourcc),"cook",4)){
267 skipped += read_cook_extradata(fd, rmctx);
268 rmctx->codec_type = cook;
272 DEBUGF(" flavor = %d\n",flavor);
273 DEBUGF(" coded_frame_size = %d\n",coded_framesize);
274 DEBUGF(" sub_packet_h = %d\n",rmctx->sub_packet_h);
275 DEBUGF(" frame_size = %d\n",rmctx->block_align);
276 DEBUGF(" sub_packet_size = %d\n",rmctx->sub_packet_size);
277 DEBUGF(" sample_rate= %d\n",rmctx->sample_rate);
278 DEBUGF(" channels= %d\n",rmctx->nb_channels);
279 DEBUGF(" fourcc = %s\n",fourcc2str(fourcc));
280 DEBUGF(" codec_extra_data_length = %d\n",rmctx->extradata_size);
281 DEBUGF(" codec_extradata :\n");
282 print_cook_extradata(rmctx);
286 return skipped;
289 int real_parse_header(int fd, RMContext *rmctx)
291 struct real_object_t obj;
292 memset(&obj,0,sizeof(obj));
293 int res;
294 int skipped;
295 off_t curpos;
297 uint32_t unknown1;
298 uint32_t unknown2;
300 uint32_t max_bitrate;
301 uint32_t avg_bitrate = 0;
302 uint32_t max_packet_size;
303 uint32_t avg_packet_size;
304 uint32_t packet_count;
305 uint32_t duration;
306 uint32_t preroll;
307 uint32_t index_offset;
308 uint16_t num_streams;
309 uint16_t flags = 0;
311 uint16_t stream_id;
312 uint32_t start_time;
313 char desc[256];
314 char mimetype[256];
315 uint32_t codec_data_size;
316 uint32_t v;
318 char title[256];
319 char author[256];
320 char copyright[256];
321 char comment[256];
323 uint32_t next_data_off;
324 uint8_t header_end;
326 curpos = lseek(fd, 0, SEEK_SET);
327 res = real_read_object_header(fd, &obj);
329 if (obj.fourcc == FOURCC('.','r','a',0xfd))
331 /* Very old .ra format - not yet supported */
332 return -1;
334 else if (obj.fourcc != FOURCC('.','R','M','F'))
336 return -1;
339 read_uint32be(fd, &unknown1);
340 read_uint32be(fd, &unknown2);
342 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos);
343 DEBUGF(" unknown1=%d (0x%08x)\n",unknown1,unknown1);
344 DEBUGF(" unknown2=%d (0x%08x)\n",unknown2,unknown2);
346 res = real_read_object_header(fd, &obj);
347 header_end = 0;
348 while(res)
350 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos);
351 skipped = 10;
352 if(obj.fourcc == FOURCC('I','N','D','X'))
353 break;
354 switch (obj.fourcc)
356 case FOURCC('P','R','O','P'): /* File properties */
357 read_uint32be(fd, &max_bitrate);
358 read_uint32be(fd, &rmctx->bit_rate); /*avg bitrate*/
359 read_uint32be(fd, &max_packet_size);
360 read_uint32be(fd, &avg_packet_size);
361 read_uint32be(fd, &packet_count);
362 read_uint32be(fd, &rmctx->duration);
363 read_uint32be(fd, &preroll);
364 read_uint32be(fd, &index_offset);
365 read_uint32be(fd, &rmctx->data_offset);
366 read_uint16be(fd, &num_streams);
367 read_uint16be(fd, &rmctx->flags);
368 skipped += 40;
370 DEBUGF(" max_bitrate = %d\n",max_bitrate);
371 DEBUGF(" avg_bitrate = %d\n",avg_bitrate);
372 DEBUGF(" max_packet_size = %d\n",max_packet_size);
373 DEBUGF(" avg_packet_size = %d\n",avg_packet_size);
374 DEBUGF(" packet_count = %d\n",packet_count);
375 DEBUGF(" duration = %d\n",rmctx->duration);
376 DEBUGF(" preroll = %d\n",preroll);
377 DEBUGF(" index_offset = %d\n",index_offset);
378 DEBUGF(" data_offset = %d\n",rmctx->data_offset);
379 DEBUGF(" num_streams = %d\n",num_streams);
380 DEBUGF(" flags=0x%04x\n",flags);
381 break;
383 case FOURCC('C','O','N','T'):
384 /* Four strings - Title, Author, Copyright, Comment */
385 skipped += read_str(fd,title);
386 skipped += read_str(fd,author);
387 skipped += read_str(fd,copyright);
388 skipped += read_str(fd,comment);
390 DEBUGF(" title=\"%s\"\n",title);
391 DEBUGF(" author=\"%s\"\n",author);
392 DEBUGF(" copyright=\"%s\"\n",copyright);
393 DEBUGF(" comment=\"%s\"\n",comment);
394 break;
396 case FOURCC('M','D','P','R'): /* Media properties */
397 read_uint16be(fd,&stream_id);
398 skipped += 2;
399 read_uint32be(fd,&max_bitrate);
400 skipped += 4;
401 read_uint32be(fd,&avg_bitrate);
402 skipped += 4;
403 read_uint32be(fd,&max_packet_size);
404 skipped += 4;
405 read_uint32be(fd,&avg_packet_size);
406 skipped += 4;
407 read_uint32be(fd,&start_time);
408 skipped += 4;
409 read_uint32be(fd,&preroll);
410 skipped += 4;
411 read_uint32be(fd,&duration);
412 skipped += 4;
413 skipped += read_str(fd,desc);
414 skipped += read_str(fd,mimetype);
415 read_uint32be(fd,&codec_data_size);
416 skipped += 4;
417 //From ffmpeg: codec_pos = url_ftell(pb);
418 read_uint32be(fd,&v);
419 skipped += 4;
421 DEBUGF(" stream_id = 0x%04x\n",stream_id);
422 DEBUGF(" max_bitrate = %d\n",max_bitrate);
423 DEBUGF(" avg_bitrate = %d\n",avg_bitrate);
424 DEBUGF(" max_packet_size = %d\n",max_packet_size);
425 DEBUGF(" avg_packet_size = %d\n",avg_packet_size);
426 DEBUGF(" start_time = %d\n",start_time);
427 DEBUGF(" preroll = %d\n",preroll);
428 DEBUGF(" duration = %d\n",duration);
429 DEBUGF(" desc=\"%s\"\n",desc);
430 DEBUGF(" mimetype=\"%s\"\n",mimetype);
431 DEBUGF(" codec_data_size = %d\n",codec_data_size);
432 DEBUGF(" v=\"%s\"\n", fourcc2str(v));
434 if (v == FOURCC('.','r','a',0xfd))
436 skipped += real_read_audio_stream_info(fd, rmctx);
439 break;
441 case FOURCC('D','A','T','A'):
443 read_uint32be(fd,&rmctx->nb_packets);
444 skipped += 4;
445 read_uint32be(fd,&next_data_off);
446 skipped += 4;
447 if (!rmctx->nb_packets && (rmctx->flags & 4))
448 rmctx->nb_packets = 3600 * 25;
450 /***
451 * nb_packets correction :
452 * in some samples, number of packets may not exactly form
453 * an integer number of scrambling units. This is corrected
454 * by constructing a partially filled unit out of the few
455 * remaining samples at the end of decoding.
456 ***/
457 if(rmctx->nb_packets % rmctx->sub_packet_h)
458 rmctx->nb_packets += rmctx->sub_packet_h - (rmctx->nb_packets % rmctx->sub_packet_h);
460 DEBUGF(" data_nb_packets = %d\n",rmctx->nb_packets);
461 DEBUGF(" next DATA offset = %d\n",next_data_off);
462 header_end = 1;
463 break;
465 if(header_end) break;
466 curpos = lseek(fd, obj.size - skipped, SEEK_CUR);
467 res = real_read_object_header(fd, &obj);
471 return 0;
474 void rm_get_packet_fd(int fd,RMContext *rmctx, RMPacket *pkt)
476 uint8_t unknown,packet_group;
477 uint16_t x;
478 uint16_t sps = rmctx->sub_packet_size;
479 uint16_t h = rmctx->sub_packet_h;
480 uint16_t y = rmctx->sub_packet_cnt;
481 uint16_t w = rmctx->audio_framesize;
482 int res;
485 y = rmctx->sub_packet_cnt;
486 read_uint16be(fd,&pkt->version);
487 read_uint16be(fd,&pkt->length);
488 read_uint16be(fd,&pkt->stream_number);
489 read_uint32be(fd,&pkt->timestamp);
490 DEBUGF(" version = %d\n"
491 " length = %d\n"
492 " stream = %d\n"
493 " timestmp= %d\n",pkt->version,pkt->length,pkt->stream_number,pkt->timestamp);
495 //getchar();
496 if(pkt->version == 0)
498 read_uint8(fd,&packet_group);
499 read_uint8(fd,&pkt->flags);
501 if(pkt->version == 1)
502 read_uint8(fd,&unknown);
504 if (pkt->flags & 2) /* keyframe */
505 y = rmctx->sub_packet_cnt = 0;
506 if (!y) /* if keyframe update playback elapsed time */
507 rmctx->audiotimestamp = pkt->timestamp;
509 for(x = 0 ; x < w/sps; x++)
511 res = read(fd,pkt->data+(sps*(h*x+((h+1)/2)*(y&1)+(y>>1))), sps);
513 rmctx->audio_pkt_cnt++;
514 }while(++(rmctx->sub_packet_cnt) < h);
517 #endif /*TEST*/
519 int rm_get_packet(uint8_t **src,RMContext *rmctx, RMPacket *pkt)
521 int consumed = 0;
522 uint8_t unknown;
523 uint16_t x, place;
524 uint16_t sps = rmctx->sub_packet_size;
525 uint16_t h = rmctx->sub_packet_h;
526 uint16_t y = rmctx->sub_packet_cnt;
527 uint16_t w = rmctx->audio_framesize;
530 y = rmctx->sub_packet_cnt;
531 pkt->version = get_uint16be(*src);
533 /* Simple error checking */
534 if(pkt->version != 0 && pkt->version != 1)
536 DEBUGF("parsing packets failed\n");
537 return -1;
540 pkt->length = get_uint16be(*src+2);
541 pkt->stream_number = get_uint16be(*src+4);
542 pkt->timestamp = get_uint32be(*src+6);
543 /*DEBUGF(" version = %d\n"
544 " length = %d\n"
545 " stream = %d\n"
546 " timestamp= %d\n\n",pkt->version,pkt->length,pkt->stream_number,pkt->timestamp);*/
547 unknown = get_uint8(*src+10);
548 pkt->flags = get_uint8(*src+11);
550 if(pkt->version == 1)
551 unknown = get_uint8(*src+10);
553 if (pkt->flags & 2) /* keyframe */
554 y = rmctx->sub_packet_cnt = 0;
555 if (!y)
556 rmctx->audiotimestamp = pkt->timestamp;
558 advance_buffer(src,12);
559 consumed += 12;
560 for(x = 0 ; x < w/sps; x++)
562 place = sps*(h*x+((h+1)/2)*(y&1)+(y>>1));
563 pkt->frames[place/sps] = *src;
564 advance_buffer(src,sps);
565 consumed += sps;
567 rmctx->audio_pkt_cnt++;
568 }while(++(rmctx->sub_packet_cnt) < h);
570 return consumed;
573 #ifdef DEBUG
574 void dump_rm_context(RMContext *rmctx)
576 DEBUGF("block_align = %d\n", rmctx->block_align);
577 DEBUGF("nb_channels = %d\n", rmctx->nb_channels);
578 DEBUGF("sample_rate = %d\n", rmctx->sample_rate);
579 DEBUGF("bit_rate = %d\n", rmctx->bit_rate );
581 #endif