summaryrefslogtreecommitdiff
path: root/apps/codecs/librm/rm.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/librm/rm.c')
-rw-r--r--apps/codecs/librm/rm.c563
1 files changed, 563 insertions, 0 deletions
diff --git a/apps/codecs/librm/rm.c b/apps/codecs/librm/rm.c
new file mode 100644
index 0000000000..86c4378d56
--- /dev/null
+++ b/apps/codecs/librm/rm.c
@@ -0,0 +1,563 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2008 Dave Chapman
11 * Copyright (C) 2009 Mohamed Tarek
12 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22#include <stdio.h>
23#include <string.h>
24#include <stdint.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <unistd.h>
29
30#include "rm.h"
31
32
33#if 0
34#define DEBUG
35#define DEBUGF printf
36#else
37#define DEBUGF(...)
38#endif
39
40/* Some Rockbox-like functions (these should be implemented in metadata_common.[ch] */
41static uint8_t get_uint8(uint8_t *buf)
42{
43 return (uint8_t)buf[0];
44}
45
46static uint16_t get_uint16be(uint8_t *buf)
47{
48 return (uint16_t)((buf[0] << 8)|buf[1]);
49}
50
51static uint32_t get_uint32be(uint8_t *buf)
52{
53 return (uint32_t)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
54}
55
56static int read_uint8(int fd, uint8_t* buf)
57{
58 unsigned char tmp[1];
59 int res;
60
61 res=read(fd, tmp, 1);
62 *buf = tmp[0];
63 return res;
64}
65
66static int read_uint16be(int fd, uint16_t* buf)
67{
68 unsigned char tmp[2];
69 int res;
70
71 res=read(fd, tmp, 2);
72 *buf = (tmp[0] << 8) | tmp[1];
73 return res;
74}
75
76static int read_uint32be(int fd, uint32_t* buf)
77{
78 unsigned char tmp[4];
79 int res;
80
81 res=read(fd, tmp, 4);
82 *buf = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3];
83 return res;
84}
85
86off_t filesize(int fd)
87{
88 struct stat buf;
89
90 if (fstat(fd,&buf) == -1) {
91 return -1;
92 } else {
93 return buf.st_size;
94 }
95}
96
97void advance_buffer(uint8_t **buf, int val)
98{
99 *buf += val;
100}
101
102int read_cook_extradata(int fd, RMContext *rmctx) {
103 read_uint32be(fd, &rmctx->cook_version);
104 read_uint16be(fd, &rmctx->samples_pf_pc);
105 read_uint16be(fd, &rmctx->nb_subbands);
106 if(rmctx->extradata_size == 16) {
107 read_uint32be(fd, &rmctx->unused);
108 read_uint16be(fd, &rmctx->js_subband_start);
109 read_uint16be(fd, &rmctx->js_vlc_bits);
110 }
111 return rmctx->extradata_size; /* for 'skipped' */
112}
113
114void print_cook_extradata(RMContext *rmctx) {
115
116 printf(" cook_version = 0x%08x\n", rmctx->cook_version);
117 printf(" samples_per_frame_per_channel = %d\n", rmctx->samples_pf_pc);
118 printf(" number_of_subbands_in_freq_domain = %d\n", rmctx->nb_subbands);
119 if(rmctx->extradata_size == 16) {
120 printf(" joint_stereo_subband_start = %d\n",rmctx->js_subband_start);
121 printf(" joint_stereo_vlc_bits = %d\n", rmctx->js_vlc_bits);
122 }
123}
124
125
126struct real_object_t
127{
128 uint32_t fourcc;
129 uint32_t size;
130 uint16_t version;
131};
132
133#define FOURCC(a,b,c,d) (((a)<<24) | ((b) << 16) | ((c) << 8) | (d))
134
135static int real_read_object_header(int fd, struct real_object_t* obj)
136{
137 int n;
138
139 if ((n = read_uint32be(fd, &obj->fourcc)) <= 0) return n;
140 if ((n = read_uint32be(fd, &obj->size)) <= 0) return n;
141 if ((n = read_uint16be(fd, &obj->version)) <= 0) return n;
142
143 return 1;
144}
145
146static char* fourcc2str(uint32_t f)
147{
148 static char res[5];
149
150 res[0] = (f & 0xff000000) >> 24;
151 res[1] = (f & 0xff0000) >> 16;
152 res[2] = (f & 0xff00) >> 8;
153 res[3] = (f & 0xff);
154 res[4] = 0;
155
156 return res;
157}
158
159static int read_str(int fd, char* buf)
160{
161 uint8_t len;
162 int res;
163
164 res = read(fd, &len, 1);
165 res = read(fd, buf, len);
166 buf[len]=0;
167
168 return len+1;
169}
170
171static int real_read_audio_stream_info(int fd, RMContext *rmctx)
172{
173 int skipped = 0;
174 uint32_t version;
175 struct real_object_t obj;
176 memset(&obj,0,sizeof(obj));
177 uint32_t header_size;
178 uint16_t flavor;
179 uint32_t coded_framesize;
180 uint32_t unknown1;
181 uint32_t unknown2;
182 uint32_t unknown3;
183 uint16_t unknown4;
184 uint16_t unknown5;
185 uint16_t unknown6;
186 uint16_t unknown7;
187 uint32_t unknown8;
188 uint8_t interleaver_id_length;
189 uint32_t interleaver_id;
190 uint8_t fourcc_length;
191 uint32_t fourcc = 0;
192 uint8_t unknown9;
193 uint16_t unknown10;
194 uint8_t unknown11;
195
196 read_uint32be(fd, &version);
197 skipped += 4;
198
199 printf(" version=0x%04x\n",((version >> 16) & 0xff));
200 if (((version >> 16) & 0xff) == 3) {
201 /* Very old version */
202 } else {
203 real_read_object_header(fd, &obj);
204 skipped += 10;
205 read_uint32be(fd, &header_size);
206 skipped += 4;
207 /* obj.size will be filled with an unknown value, replaced with header_size */
208 printf(" Object: %s, size: %d bytes, version: 0x%04x\n",fourcc2str(obj.fourcc),header_size,obj.version);
209
210 read_uint16be(fd, &flavor);
211 read_uint32be(fd, &coded_framesize);
212 read_uint32be(fd, &unknown1);
213 read_uint32be(fd, &unknown2);
214 read_uint32be(fd, &unknown3);
215 read_uint16be(fd, &rmctx->sub_packet_h);
216 read_uint16be(fd, &rmctx->block_align);
217 read_uint16be(fd, &rmctx->sub_packet_size);
218 read_uint16be(fd, &unknown4);
219 skipped += 26;
220 if (((version >> 16) & 0xff) == 5)
221 {
222 read_uint16be(fd, &unknown5);
223 read_uint16be(fd, &unknown6);
224 read_uint16be(fd, &unknown7);
225 skipped += 6;
226 }
227 read_uint16be(fd, &rmctx->sample_rate);
228 read_uint32be(fd, &unknown8);
229 read_uint16be(fd, &rmctx->nb_channels);
230 skipped += 8;
231 if (((version >> 16) & 0xff) == 4)
232 {
233 read_uint8(fd, &interleaver_id_length);
234 read_uint32be(fd, &interleaver_id);
235 read_uint8(fd, &fourcc_length);
236 read_uint32be(fd, &fourcc);
237 skipped += 10;
238 }
239 if (((version >> 16) & 0xff) == 5)
240 {
241 read_uint32be(fd, &interleaver_id);
242 read_uint32be(fd, &fourcc);
243 skipped += 8;
244 }
245 read_uint8(fd,&unknown9);
246 read_uint16be(fd,&unknown10);
247 skipped += 3;
248 if (((version >> 16) & 0xff) == 5)
249 {
250 read_uint8(fd, &unknown11);
251 skipped += 1;
252 }
253
254 read_uint32be(fd, &rmctx->extradata_size);
255 skipped += 4;
256 if(!strncmp(fourcc2str(fourcc),"cook",4))
257 skipped += read_cook_extradata(fd, rmctx);
258
259
260 printf(" flavor = %d\n",flavor);
261 printf(" coded_frame_size = %d\n",coded_framesize);
262 printf(" sub_packet_h = %d\n",rmctx->sub_packet_h);
263 printf(" frame_size = %d\n",rmctx->block_align);
264 printf(" sub_packet_size = %d\n",rmctx->sub_packet_size);
265 printf(" sample_rate= %d\n",rmctx->sample_rate);
266 printf(" channels= %d\n",rmctx->nb_channels);
267 printf(" fourcc = %s\n",fourcc2str(fourcc));
268 printf(" codec_extra_data_length = %d\n",rmctx->extradata_size);
269 printf(" codec_extradata :\n");
270 print_cook_extradata(rmctx);
271
272 }
273
274 return skipped;
275}
276
277int real_parse_header(int fd, RMContext *rmctx)
278{
279 struct real_object_t obj;
280 memset(&obj,0,sizeof(obj));
281 int res;
282 int skipped;
283 off_t curpos;
284
285 uint32_t unknown1;
286 uint32_t unknown2;
287
288 uint32_t max_bitrate;
289 uint32_t avg_bitrate = 0;
290 uint32_t max_packet_size;
291 uint32_t avg_packet_size;
292 uint32_t packet_count;
293 uint32_t duration;
294 uint32_t preroll;
295 uint32_t index_offset;
296 uint16_t num_streams;
297 uint16_t flags = 0;
298
299 uint16_t stream_id;
300 uint32_t start_time;
301 char desc[256];
302 char mimetype[256];
303 uint32_t codec_data_size;
304 uint32_t v;
305
306 char title[256];
307 char author[256];
308 char copyright[256];
309 char comment[256];
310
311 uint32_t next_data_off;
312 uint8_t header_end;
313
314 curpos = lseek(fd, 0, SEEK_SET);
315 res = real_read_object_header(fd, &obj);
316
317 if (obj.fourcc == FOURCC('.','r','a',0xfd))
318 {
319 /* Very old .ra format - not yet supported */
320 return -1;
321 }
322 else if (obj.fourcc != FOURCC('.','R','M','F'))
323 {
324 return -1;
325 }
326
327 read_uint32be(fd, &unknown1);
328 read_uint32be(fd, &unknown2);
329
330 printf("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos);
331 printf(" unknown1=%d (0x%08x)\n",unknown1,unknown1);
332 printf(" unknown2=%d (0x%08x)\n",unknown2,unknown2);
333
334 res = real_read_object_header(fd, &obj);
335 header_end = 0;
336 while(res)
337 {
338 printf("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos);
339 skipped = 10;
340 if(obj.fourcc == FOURCC('I','N','D','X'))
341 break;
342 switch (obj.fourcc)
343 {
344 case FOURCC('P','R','O','P'): /* File properties */
345 read_uint32be(fd, &max_bitrate);
346 read_uint32be(fd, &rmctx->bit_rate); /*avg bitrate*/
347 read_uint32be(fd, &max_packet_size);
348 read_uint32be(fd, &avg_packet_size);
349 read_uint32be(fd, &packet_count);
350 read_uint32be(fd, &duration);
351 read_uint32be(fd, &preroll);
352 read_uint32be(fd, &index_offset);
353 read_uint32be(fd, &rmctx->data_offset);
354 read_uint16be(fd, &num_streams);
355 read_uint16be(fd, &rmctx->flags);
356 skipped += 40;
357
358 printf(" max_bitrate = %d\n",max_bitrate);
359 printf(" avg_bitrate = %d\n",avg_bitrate);
360 printf(" max_packet_size = %d\n",max_packet_size);
361 printf(" avg_packet_size = %d\n",avg_packet_size);
362 printf(" packet_count = %d\n",packet_count);
363 printf(" duration = %d\n",duration);
364 printf(" preroll = %d\n",preroll);
365 printf(" index_offset = %d\n",index_offset);
366 printf(" data_offset = %d\n",rmctx->data_offset);
367 printf(" num_streams = %d\n",num_streams);
368 printf(" flags=0x%04x\n",flags);
369 break;
370
371 case FOURCC('C','O','N','T'):
372 /* Four strings - Title, Author, Copyright, Comment */
373 skipped += read_str(fd,title);
374 skipped += read_str(fd,author);
375 skipped += read_str(fd,copyright);
376 skipped += read_str(fd,comment);
377
378 printf(" title=\"%s\"\n",title);
379 printf(" author=\"%s\"\n",author);
380 printf(" copyright=\"%s\"\n",copyright);
381 printf(" comment=\"%s\"\n",comment);
382 break;
383
384 case FOURCC('M','D','P','R'): /* Media properties */
385 read_uint16be(fd,&stream_id);
386 skipped += 2;
387 read_uint32be(fd,&max_bitrate);
388 skipped += 4;
389 read_uint32be(fd,&avg_bitrate);
390 skipped += 4;
391 read_uint32be(fd,&max_packet_size);
392 skipped += 4;
393 read_uint32be(fd,&avg_packet_size);
394 skipped += 4;
395 read_uint32be(fd,&start_time);
396 skipped += 4;
397 read_uint32be(fd,&preroll);
398 skipped += 4;
399 read_uint32be(fd,&duration);
400 skipped += 4;
401 skipped += read_str(fd,desc);
402 skipped += read_str(fd,mimetype);
403 read_uint32be(fd,&codec_data_size);
404 skipped += 4;
405 //From ffmpeg: codec_pos = url_ftell(pb);
406 read_uint32be(fd,&v);
407 skipped += 4;
408
409 printf(" stream_id = 0x%04x\n",stream_id);
410 printf(" max_bitrate = %d\n",max_bitrate);
411 printf(" avg_bitrate = %d\n",avg_bitrate);
412 printf(" max_packet_size = %d\n",max_packet_size);
413 printf(" avg_packet_size = %d\n",avg_packet_size);
414 printf(" start_time = %d\n",start_time);
415 printf(" preroll = %d\n",preroll);
416 printf(" duration = %d\n",duration);
417 printf(" desc=\"%s\"\n",desc);
418 printf(" mimetype=\"%s\"\n",mimetype);
419 printf(" codec_data_size = %d\n",codec_data_size);
420 printf(" v=\"%s\"\n", fourcc2str(v));
421
422 if (v == FOURCC('.','r','a',0xfd))
423 {
424 skipped += real_read_audio_stream_info(fd, rmctx);
425 }
426
427 break;
428
429 case FOURCC('D','A','T','A'):
430
431 read_uint32be(fd,&rmctx->nb_packets);
432 skipped += 4;
433 read_uint32be(fd,&next_data_off);
434 skipped += 4;
435 if (!rmctx->nb_packets && (rmctx->flags & 4))
436 rmctx->nb_packets = 3600 * 25;
437
438 /***
439 * nb_packets correction :
440 * in some samples, number of packets may not exactly form
441 * an integer number of scrambling units. This is corrected
442 * by constructing a partially filled unit out of the few
443 * remaining samples at the end of decoding.
444 ***/
445 if(rmctx->nb_packets % rmctx->sub_packet_h)
446 rmctx->nb_packets += rmctx->sub_packet_h - (rmctx->nb_packets % rmctx->sub_packet_h);
447
448 printf(" data_nb_packets = %d\n",rmctx->nb_packets);
449 printf(" next DATA offset = %d\n",next_data_off);
450 header_end = 1;
451 break;
452 }
453 if(header_end) break;
454 curpos = lseek(fd, obj.size - skipped, SEEK_CUR);
455 res = real_read_object_header(fd, &obj);
456 }
457
458
459 return 0;
460}
461
462void rm_get_packet(int fd,RMContext *rmctx, RMPacket *pkt)
463{
464 uint8_t unknown,packet_group;
465 uint16_t x, place;
466 uint16_t sps = rmctx->sub_packet_size;
467 uint16_t h = rmctx->sub_packet_h;
468 uint16_t y = rmctx->sub_packet_cnt;
469 uint16_t w = rmctx->audio_framesize;
470 do
471 {
472 y = rmctx->sub_packet_cnt;
473 read_uint16be(fd,&pkt->version);
474 read_uint16be(fd,&pkt->length);
475 read_uint16be(fd,&pkt->stream_number);
476 read_uint32be(fd,&pkt->timestamp);
477 DEBUGF(" version = %d\n"
478 " length = %d\n"
479 " stream = %d\n"
480 " timestmp= %d\n",pkt->version,pkt->length,pkt->stream_number,pkt->timestamp);
481
482 //getchar();
483 if(pkt->version == 0)
484 {
485 read_uint8(fd,&packet_group);
486 read_uint8(fd,&pkt->flags);
487 }
488 if(pkt->version == 1)
489 read_uint8(fd,&unknown);
490
491 if (pkt->flags & 2) /* keyframe */
492 y = rmctx->sub_packet_cnt = 0;
493 if (!y) /* if keyframe update playback elapsed time */
494 rmctx->audiotimestamp = pkt->timestamp;
495
496 for(x = 0 ; x < w/sps; x++)
497 {
498 place = sps*(h*x+((h+1)/2)*(y&1)+(y>>1));
499 read(fd,pkt->data+place, sps);
500 //DEBUGF("place = %d data[place] = %d\n",place,pkt->data[place]);
501 }
502 rmctx->audio_pkt_cnt++;
503 }while(++(rmctx->sub_packet_cnt) < h);
504
505 //return pkt->data;
506}
507
508/**
509 * Another version of rm_get_packet which reads from a memory buffer
510 * instead of readind from a file descriptor.
511 **/
512void rm_get_packet_membuf(uint8_t **filebuf,RMContext *rmctx, RMPacket *pkt)
513{
514 uint8_t unknown;
515 uint16_t x, place;
516 uint16_t sps = rmctx->sub_packet_size;
517 uint16_t h = rmctx->sub_packet_h;
518 uint16_t y = rmctx->sub_packet_cnt;
519 uint16_t w = rmctx->audio_framesize;
520 do
521 {
522 y = rmctx->sub_packet_cnt;
523 pkt->version = get_uint16be(*filebuf);
524 pkt->length = get_uint16be(*filebuf+2);
525 pkt->stream_number = get_uint16be(*filebuf+4);
526 pkt->timestamp = get_uint32be(*filebuf+6);
527 DEBUGF(" version = %d\n"
528 " length = %d\n"
529 " stream = %d\n"
530 " timestamp= %d\n",pkt->version,pkt->length,pkt->stream_number,pkt->timestamp);
531
532 unknown = get_uint8(*filebuf+10);
533 pkt->flags = get_uint8(*filebuf+11);
534
535 if(pkt->version == 1)
536 unknown = get_uint8(*filebuf+10);
537
538 if (pkt->flags & 2) /* keyframe */
539 y = rmctx->sub_packet_cnt = 0;
540 if (!y) /* if keyframe update playback elapsed time */
541 rmctx->audiotimestamp = pkt->timestamp;
542
543 advance_buffer(filebuf,12);
544
545 for(x = 0 ; x < w/sps; x++)
546 {
547 place = sps*(h*x+((h+1)/2)*(y&1)+(y>>1));
548 pkt->frames[place/sps] = *filebuf;
549 advance_buffer(filebuf,sps);
550 }
551 rmctx->audio_pkt_cnt++;
552 }while(++(rmctx->sub_packet_cnt) < h);
553}
554
555#ifdef DEBUG
556void dump_rm_context(RMContext *rmctx)
557{
558 DEBUGF("block_align = %d\n", rmctx->block_align);
559 DEBUGF("nb_channels = %d\n", rmctx->nb_channels);
560 DEBUGF("sample_rate = %d\n", rmctx->sample_rate);
561 DEBUGF("bit_rate = %d\n", rmctx->bit_rate );
562}
563#endif