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 static inline void print_cook_extradata(RMContext
*rmctx
) {
43 DEBUGF(" cook_version = 0x%08lx\n", rm_get_uint32be(rmctx
->codec_extradata
));
44 DEBUGF(" samples_per_frame_per_channel = %d\n", rm_get_uint16be(&rmctx
->codec_extradata
[4]));
45 DEBUGF(" number_of_subbands_in_freq_domain = %d\n", rm_get_uint16be(&rmctx
->codec_extradata
[6]));
46 if(rmctx
->extradata_size
== 16) {
47 DEBUGF(" joint_stereo_subband_start = %d\n",rm_get_uint16be(&rmctx
->codec_extradata
[12]));
48 DEBUGF(" joint_stereo_vlc_bits = %d\n", rm_get_uint16be(&rmctx
->codec_extradata
[14]));
60 #define FOURCC(a,b,c,d) (((a)<<24) | ((b) << 16) | ((c) << 8) | (d))
62 static int real_read_object_header(int fd
, struct real_object_t
* obj
)
66 if ((n
= read_uint32be(fd
, &obj
->fourcc
)) <= 0) return n
;
67 if ((n
= read_uint32be(fd
, &obj
->size
)) <= 0) return n
;
68 if ((n
= read_uint16be(fd
, &obj
->version
)) <= 0) return n
;
73 #if (defined(SIMULATOR) && defined(DEBUG_RM))
74 static char* fourcc2str(uint32_t f
)
78 res
[0] = (f
& 0xff000000) >> 24;
79 res
[1] = (f
& 0xff0000) >> 16;
80 res
[2] = (f
& 0xff00) >> 8;
88 static inline int real_read_audio_stream_info(int fd
, RMContext
*rmctx
)
92 struct real_object_t obj
;
96 uint32_t coded_framesize
;
97 uint8_t interleaver_id_length
;
98 uint8_t fourcc_length
;
100 uint32_t interleaver_id
;
103 memset(&obj
,0,sizeof(obj
));
104 read_uint32be(fd
, &version
);
107 DEBUGF(" version=0x%04lx\n",((version
>> 16) & 0xff));
108 if (((version
>> 16) & 0xff) == 3) {
109 /* Very old version */
112 real_read_object_header(fd
, &obj
);
113 read_uint32be(fd
, &header_size
);
114 /* obj.size will be filled with an unknown value, replaced with header_size */
115 DEBUGF(" Object: %s, size: %ld bytes, version: 0x%04x\n",fourcc2str(obj
.fourcc
),header_size
,obj
.version
);
117 read_uint16be(fd
, &flavor
);
118 read_uint32be(fd
, &coded_framesize
);
120 lseek(fd
, 20, SEEK_CUR
);
122 lseek(fd
, 12, SEEK_CUR
); /* unknown */
123 read_uint16be(fd
, &rmctx
->sub_packet_h
);
124 read_uint16be(fd
, &rmctx
->block_align
);
125 read_uint16be(fd
, &rmctx
->sub_packet_size
);
126 lseek(fd
, 2, SEEK_CUR
); /* unknown */
128 if (((version
>> 16) & 0xff) == 5)
130 lseek(fd
, 6, SEEK_CUR
); /* unknown */
133 read_uint16be(fd
, &rmctx
->sample_rate
);
134 lseek(fd
, 4, SEEK_CUR
); /* unknown */
135 read_uint16be(fd
, &rmctx
->nb_channels
);
137 if (((version
>> 16) & 0xff) == 4)
140 read_uint8(fd
, &interleaver_id_length
);
141 read_uint32be(fd
, &interleaver_id
);
142 read_uint8(fd
, &fourcc_length
);
144 lseek(fd
, 6, SEEK_CUR
);
146 read_uint32be(fd
, &fourcc
);
149 if (((version
>> 16) & 0xff) == 5)
151 read_uint32be(fd
, &interleaver_id
);
152 read_uint32be(fd
, &fourcc
);
155 lseek(fd
, 3, SEEK_CUR
); /* unknown */
157 if (((version
>> 16) & 0xff) == 5)
159 lseek(fd
, 1, SEEK_CUR
); /* unknown */
164 case FOURCC('c','o','o','k'):
165 rmctx
->codec_type
= CODEC_COOK
;
166 read_uint32be(fd
, &rmctx
->extradata_size
);
168 read(fd
, rmctx
->codec_extradata
, rmctx
->extradata_size
);
169 skipped
+= rmctx
->extradata_size
;
172 case FOURCC('r','a','a','c'):
173 case FOURCC('r','a','c','p'):
174 rmctx
->codec_type
= CODEC_AAC
;
175 read_uint32be(fd
, &rmctx
->extradata_size
);
177 read(fd
, rmctx
->codec_extradata
, rmctx
->extradata_size
);
178 skipped
+= rmctx
->extradata_size
;
181 case FOURCC('d','n','e','t'):
182 rmctx
->codec_type
= CODEC_AC3
;
185 case FOURCC('a','t','r','c'):
186 rmctx
->codec_type
= CODEC_ATRAC
;
187 read_uint32be(fd
, &rmctx
->extradata_size
);
189 read(fd
, rmctx
->codec_extradata
, rmctx
->extradata_size
);
190 skipped
+= rmctx
->extradata_size
;
193 default: /* Not a supported codec */
197 DEBUGF(" flavor = %d\n",flavor
);
198 DEBUGF(" coded_frame_size = %ld\n",coded_framesize
);
199 DEBUGF(" sub_packet_h = %d\n",rmctx
->sub_packet_h
);
200 DEBUGF(" frame_size = %d\n",rmctx
->block_align
);
201 DEBUGF(" sub_packet_size = %d\n",rmctx
->sub_packet_size
);
202 DEBUGF(" sample_rate= %d\n",rmctx
->sample_rate
);
203 DEBUGF(" channels= %d\n",rmctx
->nb_channels
);
204 DEBUGF(" fourcc = %s\n",fourcc2str(fourcc
));
205 DEBUGF(" codec_extra_data_length = %ld\n",rmctx
->extradata_size
);
206 DEBUGF(" codec_extradata :\n");
207 if(rmctx
->codec_type
== CODEC_COOK
) {
208 DEBUGF(" cook_extradata :\n");
209 print_cook_extradata(rmctx
);
217 static int rm_parse_header(int fd
, RMContext
*rmctx
, struct mp3entry
*id3
)
219 struct real_object_t obj
;
223 uint8_t len
; /* Holds a string_length, which is then passed to read_string() */
226 uint32_t avg_bitrate
= 0;
227 uint32_t max_packet_size
;
228 uint32_t avg_packet_size
;
229 uint32_t packet_count
;
232 uint32_t index_offset
;
235 uint32_t codec_data_size
;
238 uint32_t max_bitrate
;
239 uint16_t num_streams
;
240 uint32_t next_data_off
;
243 memset(&obj
,0,sizeof(obj
));
244 curpos
= lseek(fd
, 0, SEEK_SET
);
245 res
= real_read_object_header(fd
, &obj
);
247 if (obj
.fourcc
== FOURCC('.','r','a',0xfd))
249 /* Very old .ra format - not yet supported */
252 else if (obj
.fourcc
!= FOURCC('.','R','M','F'))
257 lseek(fd
, 8, SEEK_CUR
); /* unknown */
259 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj
.fourcc
),(int)obj
.size
,obj
.version
,(int)curpos
);
261 res
= real_read_object_header(fd
, &obj
);
265 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj
.fourcc
),(int)obj
.size
,obj
.version
,(int)curpos
);
267 if(obj
.fourcc
== FOURCC('I','N','D','X'))
271 case FOURCC('P','R','O','P'): /* File properties */
272 read_uint32be(fd
, &max_bitrate
);
273 read_uint32be(fd
, &rmctx
->bit_rate
); /*avg bitrate*/
275 read_uint32be(fd
, &max_packet_size
);
276 read_uint32be(fd
, &avg_packet_size
);
277 read_uint32be(fd
, &packet_count
);
279 lseek(fd
, 3*sizeof(uint32_t), SEEK_CUR
);
281 read_uint32be(fd
, &rmctx
->duration
);
283 read_uint32be(fd
, &preroll
);
284 read_uint32be(fd
, &index_offset
);
286 lseek(fd
, 2*sizeof(uint32_t), SEEK_CUR
);
288 read_uint32be(fd
, &rmctx
->data_offset
);
289 read_uint16be(fd
, &num_streams
);
290 read_uint16be(fd
, &rmctx
->flags
);
293 DEBUGF(" max_bitrate = %ld\n",max_bitrate
);
294 DEBUGF(" avg_bitrate = %ld\n",rmctx
->bit_rate
);
295 DEBUGF(" max_packet_size = %ld\n",max_packet_size
);
296 DEBUGF(" avg_packet_size = %ld\n",avg_packet_size
);
297 DEBUGF(" packet_count = %ld\n",packet_count
);
298 DEBUGF(" duration = %ld\n",rmctx
->duration
);
299 DEBUGF(" preroll = %ld\n",preroll
);
300 DEBUGF(" index_offset = %ld\n",index_offset
);
301 DEBUGF(" data_offset = %ld\n",rmctx
->data_offset
);
302 DEBUGF(" num_streams = %d\n",num_streams
);
303 DEBUGF(" flags=0x%04x\n",rmctx
->flags
);
306 case FOURCC('C','O','N','T'):
307 /* Four strings - Title, Author, Copyright, Comment */
309 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[0], sizeof(id3
->id3v1buf
[0]), '\0', len
);
311 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[1], sizeof(id3
->id3v1buf
[1]), '\0', len
);
313 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[2], sizeof(id3
->id3v1buf
[2]), '\0', len
);
315 skipped
+= (int)read_string(fd
, id3
->id3v1buf
[3], sizeof(id3
->id3v1buf
[3]), '\0', len
);
318 DEBUGF(" title=\"%s\"\n",id3
->id3v1buf
[0]);
319 DEBUGF(" author=\"%s\"\n",id3
->id3v1buf
[1]);
320 DEBUGF(" copyright=\"%s\"\n",id3
->id3v1buf
[2]);
321 DEBUGF(" comment=\"%s\"\n",id3
->id3v1buf
[3]);
324 case FOURCC('M','D','P','R'): /* Media properties */
326 read_uint16be(fd
,&stream_id
);
327 read_uint32be(fd
,&max_bitrate
);
328 read_uint32be(fd
,&avg_bitrate
);
329 read_uint32be(fd
,&max_packet_size
);
330 read_uint32be(fd
,&avg_packet_size
);
331 read_uint32be(fd
,&start_time
);
332 read_uint32be(fd
,&preroll
);
333 read_uint32be(fd
,&duration
);
335 lseek(fd
, 30, SEEK_CUR
);
340 lseek(fd
, len
, SEEK_CUR
); /* desc */
345 lseek(fd
, len
, SEEK_CUR
); /* mimetype */
346 read_uint32be(fd
,&codec_data_size
);
348 lseek(fd
, len
+ 4, SEEK_CUR
);
351 //From ffmpeg: codec_pos = url_ftell(pb);
352 read_uint32be(fd
,&v
);
355 DEBUGF(" stream_id = 0x%04x\n",stream_id
);
356 DEBUGF(" max_bitrate = %ld\n",max_bitrate
);
357 DEBUGF(" avg_bitrate = %ld\n",avg_bitrate
);
358 DEBUGF(" max_packet_size = %ld\n",max_packet_size
);
359 DEBUGF(" avg_packet_size = %ld\n",avg_packet_size
);
360 DEBUGF(" start_time = %ld\n",start_time
);
361 DEBUGF(" preroll = %ld\n",preroll
);
362 DEBUGF(" duration = %ld\n",duration
);
363 DEBUGF(" codec_data_size = %ld\n",codec_data_size
);
364 DEBUGF(" v=\"%s\"\n", fourcc2str(v
));
366 if (v
== FOURCC('.','r','a',0xfd))
368 skipped
+= real_read_audio_stream_info(fd
, rmctx
);
375 case FOURCC('D','A','T','A'):
376 read_uint32be(fd
,&rmctx
->nb_packets
);
378 read_uint32be(fd
,&next_data_off
);
382 * nb_packets correction :
383 * in some samples, number of packets may not exactly form
384 * an integer number of scrambling units. This is corrected
385 * by constructing a partially filled unit out of the few
386 * remaining samples at the end of decoding.
388 if(rmctx
->nb_packets
% rmctx
->sub_packet_h
)
389 rmctx
->nb_packets
+= rmctx
->sub_packet_h
- (rmctx
->nb_packets
% rmctx
->sub_packet_h
);
391 DEBUGF(" data_nb_packets = %ld\n",rmctx
->nb_packets
);
392 DEBUGF(" next DATA offset = %ld\n",next_data_off
);
396 if(header_end
) break;
397 curpos
= lseek(fd
, obj
.size
- skipped
, SEEK_CUR
);
398 res
= real_read_object_header(fd
, &obj
);
406 bool get_rm_metadata(int fd
, struct mp3entry
* id3
)
408 RMContext
*rmctx
= (RMContext
*) (( (intptr_t)id3
->id3v2buf
+ 3 ) &~ 3);
409 memset(rmctx
,0,sizeof(RMContext
));
410 if(rm_parse_header(fd
, rmctx
, id3
) < 0)
414 id3
->title
= id3
->id3v1buf
[0];
415 id3
->artist
= id3
->id3v1buf
[1];
416 id3
->comment
= id3
->id3v1buf
[3];
418 switch(rmctx
->codec_type
)
421 /* Already set, do nothing */
424 id3
->codectype
= AFMT_RM_AAC
;
428 id3
->codectype
= AFMT_RM_AC3
;
432 id3
->codectype
= AFMT_RM_ATRAC3
;
436 id3
->bitrate
= rmctx
->bit_rate
/ 1000;
437 id3
->frequency
= rmctx
->sample_rate
;
438 id3
->length
= rmctx
->duration
;
439 id3
->filesize
= filesize(fd
);