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"
34 /* Uncomment the following line for debugging */
41 #define ID3V1_OFFSET -128
42 #define METADATA_FOOTER_OFFSET -140
44 static inline void print_cook_extradata(RMContext
*rmctx
) {
46 DEBUGF(" cook_version = 0x%08lx\n", rm_get_uint32be(rmctx
->codec_extradata
));
47 DEBUGF(" samples_per_frame_per_channel = %d\n", rm_get_uint16be(&rmctx
->codec_extradata
[4]));
48 DEBUGF(" number_of_subbands_in_freq_domain = %d\n", rm_get_uint16be(&rmctx
->codec_extradata
[6]));
49 if(rmctx
->extradata_size
== 16) {
50 DEBUGF(" joint_stereo_subband_start = %d\n",rm_get_uint16be(&rmctx
->codec_extradata
[12]));
51 DEBUGF(" joint_stereo_vlc_bits = %d\n", rm_get_uint16be(&rmctx
->codec_extradata
[14]));
63 #define FOURCC(a,b,c,d) (((a)<<24) | ((b) << 16) | ((c) << 8) | (d))
65 static int real_read_object_header(int fd
, struct real_object_t
* obj
)
69 if ((n
= read_uint32be(fd
, &obj
->fourcc
)) <= 0) return n
;
70 if ((n
= read_uint32be(fd
, &obj
->size
)) <= 0) return n
;
71 if ((n
= read_uint16be(fd
, &obj
->version
)) <= 0) return n
;
76 #if (defined(SIMULATOR) && defined(DEBUG_RM))
77 static char* fourcc2str(uint32_t f
)
81 res
[0] = (f
& 0xff000000) >> 24;
82 res
[1] = (f
& 0xff0000) >> 16;
83 res
[2] = (f
& 0xff00) >> 8;
91 static inline int real_read_audio_stream_info(int fd
, RMContext
*rmctx
)
95 struct real_object_t obj
;
99 uint32_t coded_framesize
;
100 uint8_t interleaver_id_length
;
101 uint8_t fourcc_length
;
103 uint32_t interleaver_id
;
106 memset(&obj
,0,sizeof(obj
));
107 read_uint32be(fd
, &version
);
110 DEBUGF(" version=0x%04lx\n",((version
>> 16) & 0xff));
111 if (((version
>> 16) & 0xff) == 3) {
112 /* Very old version */
115 real_read_object_header(fd
, &obj
);
116 read_uint32be(fd
, &header_size
);
117 /* obj.size will be filled with an unknown value, replaced with header_size */
118 DEBUGF(" Object: %s, size: %ld bytes, version: 0x%04x\n",fourcc2str(obj
.fourcc
),header_size
,obj
.version
);
120 read_uint16be(fd
, &flavor
);
121 read_uint32be(fd
, &coded_framesize
);
123 lseek(fd
, 20, SEEK_CUR
);
125 lseek(fd
, 12, SEEK_CUR
); /* unknown */
126 read_uint16be(fd
, &rmctx
->sub_packet_h
);
127 read_uint16be(fd
, &rmctx
->block_align
);
128 read_uint16be(fd
, &rmctx
->sub_packet_size
);
129 lseek(fd
, 2, SEEK_CUR
); /* unknown */
131 if (((version
>> 16) & 0xff) == 5)
133 lseek(fd
, 6, SEEK_CUR
); /* unknown */
136 read_uint16be(fd
, &rmctx
->sample_rate
);
137 lseek(fd
, 4, SEEK_CUR
); /* unknown */
138 read_uint16be(fd
, &rmctx
->nb_channels
);
140 if (((version
>> 16) & 0xff) == 4)
143 read_uint8(fd
, &interleaver_id_length
);
144 read_uint32be(fd
, &interleaver_id
);
145 read_uint8(fd
, &fourcc_length
);
147 lseek(fd
, 6, SEEK_CUR
);
149 read_uint32be(fd
, &fourcc
);
152 if (((version
>> 16) & 0xff) == 5)
154 read_uint32be(fd
, &interleaver_id
);
155 read_uint32be(fd
, &fourcc
);
158 lseek(fd
, 3, SEEK_CUR
); /* unknown */
160 if (((version
>> 16) & 0xff) == 5)
162 lseek(fd
, 1, SEEK_CUR
); /* unknown */
167 case FOURCC('c','o','o','k'):
168 rmctx
->codec_type
= CODEC_COOK
;
169 read_uint32be(fd
, &rmctx
->extradata_size
);
171 read(fd
, rmctx
->codec_extradata
, rmctx
->extradata_size
);
172 skipped
+= rmctx
->extradata_size
;
175 case FOURCC('r','a','a','c'):
176 case FOURCC('r','a','c','p'):
177 rmctx
->codec_type
= CODEC_AAC
;
178 read_uint32be(fd
, &rmctx
->extradata_size
);
180 read(fd
, rmctx
->codec_extradata
, rmctx
->extradata_size
);
181 skipped
+= rmctx
->extradata_size
;
184 case FOURCC('d','n','e','t'):
185 rmctx
->codec_type
= CODEC_AC3
;
188 case FOURCC('a','t','r','c'):
189 rmctx
->codec_type
= CODEC_ATRAC
;
190 read_uint32be(fd
, &rmctx
->extradata_size
);
192 read(fd
, rmctx
->codec_extradata
, rmctx
->extradata_size
);
193 skipped
+= rmctx
->extradata_size
;
196 default: /* Not a supported codec */
200 DEBUGF(" flavor = %d\n",flavor
);
201 DEBUGF(" coded_frame_size = %ld\n",coded_framesize
);
202 DEBUGF(" sub_packet_h = %d\n",rmctx
->sub_packet_h
);
203 DEBUGF(" frame_size = %d\n",rmctx
->block_align
);
204 DEBUGF(" sub_packet_size = %d\n",rmctx
->sub_packet_size
);
205 DEBUGF(" sample_rate= %d\n",rmctx
->sample_rate
);
206 DEBUGF(" channels= %d\n",rmctx
->nb_channels
);
207 DEBUGF(" fourcc = %s\n",fourcc2str(fourcc
));
208 DEBUGF(" codec_extra_data_length = %ld\n",rmctx
->extradata_size
);
209 DEBUGF(" codec_extradata :\n");
210 if(rmctx
->codec_type
== CODEC_COOK
) {
211 DEBUGF(" cook_extradata :\n");
212 print_cook_extradata(rmctx
);
220 static int rm_parse_header(int fd
, RMContext
*rmctx
, struct mp3entry
*id3
)
222 struct real_object_t obj
;
226 uint8_t len
; /* Holds a string_length, which is then passed to read_string() */
229 uint32_t avg_bitrate
= 0;
230 uint32_t max_packet_size
;
231 uint32_t avg_packet_size
;
232 uint32_t packet_count
;
235 uint32_t index_offset
;
238 uint32_t codec_data_size
;
241 uint32_t max_bitrate
;
242 uint16_t num_streams
;
243 uint32_t next_data_off
;
246 memset(&obj
,0,sizeof(obj
));
247 curpos
= lseek(fd
, 0, SEEK_SET
);
248 res
= real_read_object_header(fd
, &obj
);
250 if (obj
.fourcc
== FOURCC('.','r','a',0xfd))
252 /* Very old .ra format - not yet supported */
255 else if (obj
.fourcc
!= FOURCC('.','R','M','F'))
260 lseek(fd
, 8, SEEK_CUR
); /* unknown */
262 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj
.fourcc
),(int)obj
.size
,obj
.version
,(int)curpos
);
264 res
= real_read_object_header(fd
, &obj
);
268 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj
.fourcc
),(int)obj
.size
,obj
.version
,(int)curpos
);
270 if(obj
.fourcc
== FOURCC('I','N','D','X'))
274 case FOURCC('P','R','O','P'): /* File properties */
275 read_uint32be(fd
, &max_bitrate
);
276 read_uint32be(fd
, &rmctx
->bit_rate
); /*avg bitrate*/
278 read_uint32be(fd
, &max_packet_size
);
279 read_uint32be(fd
, &avg_packet_size
);
280 read_uint32be(fd
, &packet_count
);
282 lseek(fd
, 3*sizeof(uint32_t), SEEK_CUR
);
284 read_uint32be(fd
, &rmctx
->duration
);
286 read_uint32be(fd
, &preroll
);
287 read_uint32be(fd
, &index_offset
);
289 lseek(fd
, 2*sizeof(uint32_t), SEEK_CUR
);
291 read_uint32be(fd
, &rmctx
->data_offset
);
292 read_uint16be(fd
, &num_streams
);
293 read_uint16be(fd
, &rmctx
->flags
);
296 DEBUGF(" max_bitrate = %ld\n",max_bitrate
);
297 DEBUGF(" avg_bitrate = %ld\n",rmctx
->bit_rate
);
298 DEBUGF(" max_packet_size = %ld\n",max_packet_size
);
299 DEBUGF(" avg_packet_size = %ld\n",avg_packet_size
);
300 DEBUGF(" packet_count = %ld\n",packet_count
);
301 DEBUGF(" duration = %ld\n",rmctx
->duration
);
302 DEBUGF(" preroll = %ld\n",preroll
);
303 DEBUGF(" index_offset = %ld\n",index_offset
);
304 DEBUGF(" data_offset = %ld\n",rmctx
->data_offset
);
305 DEBUGF(" num_streams = %d\n",num_streams
);
306 DEBUGF(" flags=0x%04x\n",rmctx
->flags
);
309 case FOURCC('C','O','N','T'):
310 /* Four strings - Title, Author, Copyright, Comment */
312 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[0], sizeof(id3
->id3v1buf
[0]), '\0', len
);
314 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[1], sizeof(id3
->id3v1buf
[1]), '\0', len
);
316 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[2], sizeof(id3
->id3v1buf
[2]), '\0', len
);
318 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[3], sizeof(id3
->id3v1buf
[3]), '\0', len
);
321 DEBUGF(" title=\"%s\"\n",id3
->id3v1buf
[0]);
322 DEBUGF(" author=\"%s\"\n",id3
->id3v1buf
[1]);
323 DEBUGF(" copyright=\"%s\"\n",id3
->id3v1buf
[2]);
324 DEBUGF(" comment=\"%s\"\n",id3
->id3v1buf
[3]);
327 case FOURCC('M','D','P','R'): /* Media properties */
329 read_uint16be(fd
,&stream_id
);
330 read_uint32be(fd
,&max_bitrate
);
331 read_uint32be(fd
,&avg_bitrate
);
332 read_uint32be(fd
,&max_packet_size
);
333 read_uint32be(fd
,&avg_packet_size
);
334 read_uint32be(fd
,&start_time
);
335 read_uint32be(fd
,&preroll
);
336 read_uint32be(fd
,&duration
);
338 lseek(fd
, 30, SEEK_CUR
);
343 lseek(fd
, len
, SEEK_CUR
); /* desc */
348 lseek(fd
, len
, SEEK_CUR
); /* mimetype */
349 read_uint32be(fd
,&codec_data_size
);
351 lseek(fd
, len
+ 4, SEEK_CUR
);
354 read_uint32be(fd
,&v
);
357 DEBUGF(" stream_id = 0x%04x\n",stream_id
);
358 DEBUGF(" max_bitrate = %ld\n",max_bitrate
);
359 DEBUGF(" avg_bitrate = %ld\n",avg_bitrate
);
360 DEBUGF(" max_packet_size = %ld\n",max_packet_size
);
361 DEBUGF(" avg_packet_size = %ld\n",avg_packet_size
);
362 DEBUGF(" start_time = %ld\n",start_time
);
363 DEBUGF(" preroll = %ld\n",preroll
);
364 DEBUGF(" duration = %ld\n",duration
);
365 DEBUGF(" codec_data_size = %ld\n",codec_data_size
);
366 DEBUGF(" v=\"%s\"\n", fourcc2str(v
));
368 if (v
== FOURCC('.','r','a',0xfd))
370 skipped
+= real_read_audio_stream_info(fd
, rmctx
);
377 case FOURCC('D','A','T','A'):
378 read_uint32be(fd
,&rmctx
->nb_packets
);
380 read_uint32be(fd
,&next_data_off
);
384 * nb_packets correction :
385 * in some samples, number of packets may not exactly form
386 * an integer number of scrambling units. This is corrected
387 * by constructing a partially filled unit out of the few
388 * remaining samples at the end of decoding.
390 if(rmctx
->nb_packets
% rmctx
->sub_packet_h
)
391 rmctx
->nb_packets
+= rmctx
->sub_packet_h
- (rmctx
->nb_packets
% rmctx
->sub_packet_h
);
393 DEBUGF(" data_nb_packets = %ld\n",rmctx
->nb_packets
);
394 DEBUGF(" next DATA offset = %ld\n",next_data_off
);
398 if(header_end
) break;
399 curpos
= lseek(fd
, obj
.size
- skipped
, SEEK_CUR
);
400 res
= real_read_object_header(fd
, &obj
);
408 bool get_rm_metadata(int fd
, struct mp3entry
* id3
)
410 RMContext
*rmctx
= (RMContext
*) (( (intptr_t)id3
->id3v2buf
+ 3 ) &~ 3);
411 memset(rmctx
,0,sizeof(RMContext
));
412 if(rm_parse_header(fd
, rmctx
, id3
) < 0)
415 if (!setid3v1title(fd
, id3
)) {
416 /* file has no id3v1 tags, use the tags from CONT chunk */
417 id3
->title
= id3
->id3v1buf
[0];
418 id3
->artist
= id3
->id3v1buf
[1];
419 id3
->comment
= id3
->id3v1buf
[3];
422 switch(rmctx
->codec_type
)
425 /* Already set, do nothing */
428 id3
->codectype
= AFMT_RM_AAC
;
432 id3
->codectype
= AFMT_RM_AC3
;
436 id3
->codectype
= AFMT_RM_ATRAC3
;
440 id3
->bitrate
= rmctx
->bit_rate
/ 1000;
441 id3
->frequency
= rmctx
->sample_rate
;
442 id3
->length
= rmctx
->duration
;
443 id3
->filesize
= filesize(fd
);