1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
30 void advance_buffer(uint8_t **buf
, int 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]);
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 */
66 #include <sys/types.h>
73 if (fstat(fd
,&buf
) == -1) {
76 return (int)buf
.st_size
;
80 static int read_uint8(int fd
, uint8_t* buf
)
90 static int read_uint16be(int fd
, uint16_t* buf
)
96 *buf
= (tmp
[0] << 8) | tmp
[1];
100 static int read_uint32be(int fd
, uint32_t* buf
)
102 unsigned char tmp
[4];
105 res
=read(fd
, tmp
, 4);
106 *buf
= (tmp
[0] << 24) | (tmp
[1] << 16) | (tmp
[2] << 8) | tmp
[3];
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
);
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
)
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
;
156 static char* fourcc2str(uint32_t f
)
160 res
[0] = (f
& 0xff000000) >> 24;
161 res
[1] = (f
& 0xff0000) >> 16;
162 res
[2] = (f
& 0xff00) >> 8;
169 static int read_str(int fd
, char* buf
)
174 res
= read(fd
, &len
, 1);
175 res
= read(fd
, buf
, len
);
181 static int real_read_audio_stream_info(int fd
, RMContext
*rmctx
)
185 struct real_object_t obj
;
186 memset(&obj
,0,sizeof(obj
));
187 uint32_t header_size
;
189 uint32_t coded_framesize
;
198 uint8_t interleaver_id_length
;
199 uint32_t interleaver_id
;
200 uint8_t fourcc_length
;
206 read_uint32be(fd
, &version
);
209 DEBUGF(" version=0x%04x\n",((version
>> 16) & 0xff));
210 if (((version
>> 16) & 0xff) == 3) {
211 /* Very old version */
213 real_read_object_header(fd
, &obj
);
215 read_uint32be(fd
, &header_size
);
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
);
230 if (((version
>> 16) & 0xff) == 5)
232 read_uint16be(fd
, &unknown5
);
233 read_uint16be(fd
, &unknown6
);
234 read_uint16be(fd
, &unknown7
);
237 read_uint16be(fd
, &rmctx
->sample_rate
);
238 read_uint32be(fd
, &unknown8
);
239 read_uint16be(fd
, &rmctx
->nb_channels
);
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
);
249 if (((version
>> 16) & 0xff) == 5)
251 read_uint32be(fd
, &interleaver_id
);
252 read_uint32be(fd
, &fourcc
);
255 read_uint8(fd
,&unknown9
);
256 read_uint16be(fd
,&unknown10
);
258 if (((version
>> 16) & 0xff) == 5)
260 read_uint8(fd
, &unknown11
);
264 read_uint32be(fd
, &rmctx
->extradata_size
);
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
);
289 int real_parse_header(int fd
, RMContext
*rmctx
)
291 struct real_object_t obj
;
292 memset(&obj
,0,sizeof(obj
));
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
;
307 uint32_t index_offset
;
308 uint16_t num_streams
;
315 uint32_t codec_data_size
;
323 uint32_t next_data_off
;
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 */
334 else if (obj
.fourcc
!= FOURCC('.','R','M','F'))
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
);
350 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj
.fourcc
),(int)obj
.size
,obj
.version
,(int)curpos
);
352 if(obj
.fourcc
== FOURCC('I','N','D','X'))
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
);
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
);
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
);
396 case FOURCC('M','D','P','R'): /* Media properties */
397 read_uint16be(fd
,&stream_id
);
399 read_uint32be(fd
,&max_bitrate
);
401 read_uint32be(fd
,&avg_bitrate
);
403 read_uint32be(fd
,&max_packet_size
);
405 read_uint32be(fd
,&avg_packet_size
);
407 read_uint32be(fd
,&start_time
);
409 read_uint32be(fd
,&preroll
);
411 read_uint32be(fd
,&duration
);
413 skipped
+= read_str(fd
,desc
);
414 skipped
+= read_str(fd
,mimetype
);
415 read_uint32be(fd
,&codec_data_size
);
417 //From ffmpeg: codec_pos = url_ftell(pb);
418 read_uint32be(fd
,&v
);
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
);
441 case FOURCC('D','A','T','A'):
443 read_uint32be(fd
,&rmctx
->nb_packets
);
445 read_uint32be(fd
,&next_data_off
);
447 if (!rmctx
->nb_packets
&& (rmctx
->flags
& 4))
448 rmctx
->nb_packets
= 3600 * 25;
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.
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
);
465 if(header_end
) break;
466 curpos
= lseek(fd
, obj
.size
- skipped
, SEEK_CUR
);
467 res
= real_read_object_header(fd
, &obj
);
474 void rm_get_packet_fd(int fd
,RMContext
*rmctx
, RMPacket
*pkt
)
476 uint8_t unknown
,packet_group
;
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
;
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"
493 " timestmp= %d\n",pkt
->version
,pkt
->length
,pkt
->stream_number
,pkt
->timestamp
);
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
);
519 int rm_get_packet(uint8_t **src
,RMContext
*rmctx
, RMPacket
*pkt
)
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");
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"
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;
556 rmctx
->audiotimestamp
= pkt
->timestamp
;
558 advance_buffer(src
,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
);
567 rmctx
->audio_pkt_cnt
++;
568 }while(++(rmctx
->sub_packet_cnt
) < h
);
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
);