1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 #include <codecs/librm/rm.h>
30 #include "metadata_common.h"
31 #include "metadata_parsers.h"
40 static inline int read_cook_extradata(int fd
, RMContext
*rmctx
) {
41 read_uint32be(fd
, &rmctx
->cook_version
);
42 read_uint16be(fd
, &rmctx
->samples_pf_pc
);
43 read_uint16be(fd
, &rmctx
->nb_subbands
);
44 if(rmctx
->extradata_size
== 16) {
45 lseek(fd
, sizeof(uint32_t), SEEK_CUR
); /* reserved */
46 read_uint16be(fd
, &rmctx
->js_subband_start
);
47 read_uint16be(fd
, &rmctx
->js_vlc_bits
);
49 return rmctx
->extradata_size
; /* for 'skipped' */
52 static inline void print_cook_extradata(RMContext
*rmctx
) {
54 DEBUGF(" cook_version = 0x%08lx\n", rmctx
->cook_version
);
55 DEBUGF(" samples_per_frame_per_channel = %d\n", rmctx
->samples_pf_pc
);
56 DEBUGF(" number_of_subbands_in_freq_domain = %d\n", rmctx
->nb_subbands
);
57 if(rmctx
->extradata_size
== 16) {
58 DEBUGF(" joint_stereo_subband_start = %d\n",rmctx
->js_subband_start
);
59 DEBUGF(" joint_stereo_vlc_bits = %d\n", rmctx
->js_vlc_bits
);
71 #define FOURCC(a,b,c,d) (((a)<<24) | ((b) << 16) | ((c) << 8) | (d))
73 static int real_read_object_header(int fd
, struct real_object_t
* obj
)
77 if ((n
= read_uint32be(fd
, &obj
->fourcc
)) <= 0) return n
;
78 if ((n
= read_uint32be(fd
, &obj
->size
)) <= 0) return n
;
79 if ((n
= read_uint16be(fd
, &obj
->version
)) <= 0) return n
;
84 #if (defined(SIMULATOR) && defined(DEBUG_RM))
85 static char* fourcc2str(uint32_t f
)
89 res
[0] = (f
& 0xff000000) >> 24;
90 res
[1] = (f
& 0xff0000) >> 16;
91 res
[2] = (f
& 0xff00) >> 8;
99 static inline int real_read_audio_stream_info(int fd
, RMContext
*rmctx
)
103 struct real_object_t obj
;
105 uint32_t header_size
;
107 uint32_t coded_framesize
;
108 uint8_t interleaver_id_length
;
109 uint8_t fourcc_length
;
111 uint32_t interleaver_id
;
114 memset(&obj
,0,sizeof(obj
));
115 read_uint32be(fd
, &version
);
118 DEBUGF(" version=0x%04lx\n",((version
>> 16) & 0xff));
119 if (((version
>> 16) & 0xff) == 3) {
120 /* Very old version */
123 real_read_object_header(fd
, &obj
);
124 read_uint32be(fd
, &header_size
);
125 /* obj.size will be filled with an unknown value, replaced with header_size */
126 DEBUGF(" Object: %s, size: %ld bytes, version: 0x%04x\n",fourcc2str(obj
.fourcc
),header_size
,obj
.version
);
128 read_uint16be(fd
, &flavor
);
129 read_uint32be(fd
, &coded_framesize
);
131 lseek(fd
, 20, SEEK_CUR
);
133 lseek(fd
, 12, SEEK_CUR
); /* unknown */
134 read_uint16be(fd
, &rmctx
->sub_packet_h
);
135 read_uint16be(fd
, &rmctx
->block_align
);
136 read_uint16be(fd
, &rmctx
->sub_packet_size
);
137 lseek(fd
, 2, SEEK_CUR
); /* unknown */
139 if (((version
>> 16) & 0xff) == 5)
141 lseek(fd
, 6, SEEK_CUR
); /* unknown */
144 read_uint16be(fd
, &rmctx
->sample_rate
);
145 lseek(fd
, 4, SEEK_CUR
); /* unknown */
146 read_uint16be(fd
, &rmctx
->nb_channels
);
148 if (((version
>> 16) & 0xff) == 4)
151 read_uint8(fd
, &interleaver_id_length
);
152 read_uint32be(fd
, &interleaver_id
);
153 read_uint8(fd
, &fourcc_length
);
155 lseek(fd
, 6, SEEK_CUR
);
157 read_uint32be(fd
, &fourcc
);
160 if (((version
>> 16) & 0xff) == 5)
162 read_uint32be(fd
, &interleaver_id
);
163 read_uint32be(fd
, &fourcc
);
166 lseek(fd
, 3, SEEK_CUR
); /* unknown */
168 if (((version
>> 16) & 0xff) == 5)
170 lseek(fd
, 1, SEEK_CUR
); /* unknown */
174 read_uint32be(fd
, &rmctx
->extradata_size
);
176 /*if(!strncmp(fourcc2str(fourcc),"cook",4)){
177 skipped += read_cook_extradata(fd, rmctx);
178 rmctx->codec_type = cook;
181 case FOURCC('c','o','o','k'):
182 skipped
+= read_cook_extradata(fd
, rmctx
);
183 rmctx
->codec_type
= cook
;
186 default: /* Not a supported codec */
190 DEBUGF(" flavor = %d\n",flavor
);
191 DEBUGF(" coded_frame_size = %ld\n",coded_framesize
);
192 DEBUGF(" sub_packet_h = %d\n",rmctx
->sub_packet_h
);
193 DEBUGF(" frame_size = %d\n",rmctx
->block_align
);
194 DEBUGF(" sub_packet_size = %d\n",rmctx
->sub_packet_size
);
195 DEBUGF(" sample_rate= %d\n",rmctx
->sample_rate
);
196 DEBUGF(" channels= %d\n",rmctx
->nb_channels
);
197 DEBUGF(" fourcc = %s\n",fourcc2str(fourcc
));
198 DEBUGF(" codec_extra_data_length = %ld\n",rmctx
->extradata_size
);
199 DEBUGF(" codec_extradata :\n");
200 print_cook_extradata(rmctx
);
207 static int rm_parse_header(int fd
, RMContext
*rmctx
, struct mp3entry
*id3
)
209 struct real_object_t obj
;
213 uint8_t len
; /* Holds a string_length, which is then passed to read_string() */
216 uint32_t avg_bitrate
= 0;
217 uint32_t max_packet_size
;
218 uint32_t avg_packet_size
;
219 uint32_t packet_count
;
222 uint32_t index_offset
;
225 uint32_t codec_data_size
;
228 uint32_t max_bitrate
;
229 uint16_t num_streams
;
230 uint32_t next_data_off
;
233 memset(&obj
,0,sizeof(obj
));
234 curpos
= lseek(fd
, 0, SEEK_SET
);
235 res
= real_read_object_header(fd
, &obj
);
237 if (obj
.fourcc
== FOURCC('.','r','a',0xfd))
239 /* Very old .ra format - not yet supported */
242 else if (obj
.fourcc
!= FOURCC('.','R','M','F'))
247 lseek(fd
, 8, SEEK_CUR
); /* unknown */
249 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj
.fourcc
),(int)obj
.size
,obj
.version
,(int)curpos
);
251 res
= real_read_object_header(fd
, &obj
);
255 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj
.fourcc
),(int)obj
.size
,obj
.version
,(int)curpos
);
257 if(obj
.fourcc
== FOURCC('I','N','D','X'))
261 case FOURCC('P','R','O','P'): /* File properties */
262 read_uint32be(fd
, &max_bitrate
);
263 read_uint32be(fd
, &rmctx
->bit_rate
); /*avg bitrate*/
265 read_uint32be(fd
, &max_packet_size
);
266 read_uint32be(fd
, &avg_packet_size
);
267 read_uint32be(fd
, &packet_count
);
269 lseek(fd
, 3*sizeof(uint32_t), SEEK_CUR
);
271 read_uint32be(fd
, &rmctx
->duration
);
273 read_uint32be(fd
, &preroll
);
274 read_uint32be(fd
, &index_offset
);
276 lseek(fd
, 2*sizeof(uint32_t), SEEK_CUR
);
278 read_uint32be(fd
, &rmctx
->data_offset
);
279 read_uint16be(fd
, &num_streams
);
280 read_uint16be(fd
, &rmctx
->flags
);
283 DEBUGF(" max_bitrate = %ld\n",max_bitrate
);
284 DEBUGF(" avg_bitrate = %ld\n",rmctx
->bit_Rate
);
285 DEBUGF(" max_packet_size = %ld\n",max_packet_size
);
286 DEBUGF(" avg_packet_size = %ld\n",avg_packet_size
);
287 DEBUGF(" packet_count = %ld\n",packet_count
);
288 DEBUGF(" duration = %ld\n",rmctx
->duration
);
289 DEBUGF(" preroll = %ld\n",preroll
);
290 DEBUGF(" index_offset = %ld\n",index_offset
);
291 DEBUGF(" data_offset = %ld\n",rmctx
->data_offset
);
292 DEBUGF(" num_streams = %d\n",num_streams
);
293 DEBUGF(" flags=0x%04x\n",rmctx
->flags
);
296 case FOURCC('C','O','N','T'):
297 /* Four strings - Title, Author, Copyright, Comment */
299 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[0], sizeof(id3
->id3v1buf
[0]), '\0', len
);
301 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[1], sizeof(id3
->id3v1buf
[1]), '\0', len
);
303 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[2], sizeof(id3
->id3v1buf
[2]), '\0', len
);
305 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[3], sizeof(id3
->id3v1buf
[3]), '\0', len
);
308 DEBUGF(" title=\"%s\"\n",id3
->id3v1buf
[0]);
309 DEBUGF(" author=\"%s\"\n",id3
->id3v1buf
[1]);
310 DEBUGF(" copyright=\"%s\"\n",id3
->id3v1buf
[2]);
311 DEBUGF(" comment=\"%s\"\n",id3
->id3v1buf
[3]);
314 case FOURCC('M','D','P','R'): /* Media properties */
316 read_uint16be(fd
,&stream_id
);
317 read_uint32be(fd
,&max_bitrate
);
318 read_uint32be(fd
,&avg_bitrate
);
319 read_uint32be(fd
,&max_packet_size
);
320 read_uint32be(fd
,&avg_packet_size
);
321 read_uint32be(fd
,&start_time
);
322 read_uint32be(fd
,&preroll
);
323 read_uint32be(fd
,&duration
);
325 lseek(fd
, 30, SEEK_CUR
);
330 lseek(fd
, len
, SEEK_CUR
); /* desc */
335 lseek(fd
, len
, SEEK_CUR
); /* mimetype */
336 read_uint32be(fd
,&codec_data_size
);
338 lseek(fd
, len
+ 4, SEEK_CUR
);
341 //From ffmpeg: codec_pos = url_ftell(pb);
342 read_uint32be(fd
,&v
);
345 DEBUGF(" stream_id = 0x%04x\n",stream_id
);
346 DEBUGF(" max_bitrate = %ld\n",max_bitrate
);
347 DEBUGF(" avg_bitrate = %ld\n",avg_bitrate
);
348 DEBUGF(" max_packet_size = %ld\n",max_packet_size
);
349 DEBUGF(" avg_packet_size = %ld\n",avg_packet_size
);
350 DEBUGF(" start_time = %ld\n",start_time
);
351 DEBUGF(" preroll = %ld\n",preroll
);
352 DEBUGF(" duration = %ld\n",duration
);
353 DEBUGF(" codec_data_size = %ld\n",codec_data_size
);
354 DEBUGF(" v=\"%s\"\n", fourcc2str(v
));
356 if (v
== FOURCC('.','r','a',0xfd))
358 skipped
+= real_read_audio_stream_info(fd
, rmctx
);
365 case FOURCC('D','A','T','A'):
366 read_uint32be(fd
,&rmctx
->nb_packets
);
368 read_uint32be(fd
,&next_data_off
);
372 * nb_packets correction :
373 * in some samples, number of packets may not exactly form
374 * an integer number of scrambling units. This is corrected
375 * by constructing a partially filled unit out of the few
376 * remaining samples at the end of decoding.
378 if(rmctx
->nb_packets
% rmctx
->sub_packet_h
)
379 rmctx
->nb_packets
+= rmctx
->sub_packet_h
- (rmctx
->nb_packets
% rmctx
->sub_packet_h
);
381 DEBUGF(" data_nb_packets = %ld\n",rmctx
->nb_packets
);
382 DEBUGF(" next DATA offset = %ld\n",next_data_off
);
386 if(header_end
) break;
387 curpos
= lseek(fd
, obj
.size
- skipped
, SEEK_CUR
);
388 res
= real_read_object_header(fd
, &obj
);
396 bool get_rm_metadata(int fd
, struct mp3entry
* id3
)
398 RMContext
*rmctx
= (RMContext
*) (( (int)id3
->id3v2buf
+ 3 ) &~ 3);
399 memset(rmctx
,0,sizeof(RMContext
));
400 if(rm_parse_header(fd
, rmctx
, id3
) < 0)
404 id3
->title
= id3
->id3v1buf
[0];
405 id3
->artist
= id3
->id3v1buf
[1];
406 id3
->comment
= id3
->id3v1buf
[3];
408 /*switch(rmctx->codec_type)
411 id3->codectype = AFMT_COOK;
415 id3
->bitrate
= rmctx
->bit_rate
/ 1000;
416 id3
->frequency
= rmctx
->sample_rate
;
417 id3
->length
= rmctx
->duration
;
418 id3
->filesize
= filesize(fd
);