Make svn properties consistent and set some missing ones
[kugel-rb.git] / apps / metadata / rm.c
blobe4b5005f2df1cc6bfcb444ae18182c169ac6d375
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <inttypes.h>
27 #include <codecs/librm/rm.h>
28 #include "system.h"
29 #include "metadata.h"
30 #include "metadata_common.h"
31 #include "metadata_parsers.h"
32 #include "logf.h"
34 /* Uncomment the following line for debugging */
35 //#define DEBUG_RM
36 #ifndef DEBUG_RM
37 #undef DEBUGF
38 #define DEBUGF(...)
39 #endif
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]));
56 struct real_object_t
58 uint32_t fourcc;
59 uint32_t size;
60 uint16_t version;
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)
67 int n;
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;
73 return 1;
76 #if (defined(SIMULATOR) && defined(DEBUG_RM))
77 static char* fourcc2str(uint32_t f)
79 static char res[5];
81 res[0] = (f & 0xff000000) >> 24;
82 res[1] = (f & 0xff0000) >> 16;
83 res[2] = (f & 0xff00) >> 8;
84 res[3] = (f & 0xff);
85 res[4] = 0;
87 return res;
89 #endif
91 static inline int real_read_audio_stream_info(int fd, RMContext *rmctx)
93 int skipped = 0;
94 uint32_t version;
95 struct real_object_t obj;
96 #ifdef SIMULATOR
97 uint32_t header_size;
98 uint16_t flavor;
99 uint32_t coded_framesize;
100 uint8_t interleaver_id_length;
101 uint8_t fourcc_length;
102 #endif
103 uint32_t interleaver_id;
104 uint32_t fourcc = 0;
106 memset(&obj,0,sizeof(obj));
107 read_uint32be(fd, &version);
108 skipped += 4;
110 DEBUGF(" version=0x%04lx\n",((version >> 16) & 0xff));
111 if (((version >> 16) & 0xff) == 3) {
112 /* Very old version */
113 } else {
114 #ifdef SIMULATOR
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);
122 #else
123 lseek(fd, 20, SEEK_CUR);
124 #endif
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 */
130 skipped += 40;
131 if (((version >> 16) & 0xff) == 5)
133 lseek(fd, 6, SEEK_CUR); /* unknown */
134 skipped += 6;
136 read_uint16be(fd, &rmctx->sample_rate);
137 lseek(fd, 4, SEEK_CUR); /* unknown */
138 read_uint16be(fd, &rmctx->nb_channels);
139 skipped += 8;
140 if (((version >> 16) & 0xff) == 4)
142 #ifdef SIMULATOR
143 read_uint8(fd, &interleaver_id_length);
144 read_uint32be(fd, &interleaver_id);
145 read_uint8(fd, &fourcc_length);
146 #else
147 lseek(fd, 6, SEEK_CUR);
148 #endif
149 read_uint32be(fd, &fourcc);
150 skipped += 10;
152 if (((version >> 16) & 0xff) == 5)
154 read_uint32be(fd, &interleaver_id);
155 read_uint32be(fd, &fourcc);
156 skipped += 8;
158 lseek(fd, 3, SEEK_CUR); /* unknown */
159 skipped += 3;
160 if (((version >> 16) & 0xff) == 5)
162 lseek(fd, 1, SEEK_CUR); /* unknown */
163 skipped += 1;
166 switch(fourcc) {
167 case FOURCC('c','o','o','k'):
168 rmctx->codec_type = CODEC_COOK;
169 read_uint32be(fd, &rmctx->extradata_size);
170 skipped += 4;
171 read(fd, rmctx->codec_extradata, rmctx->extradata_size);
172 skipped += rmctx->extradata_size;
173 break;
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);
179 skipped += 4;
180 read(fd, rmctx->codec_extradata, rmctx->extradata_size);
181 skipped += rmctx->extradata_size;
182 break;
184 case FOURCC('d','n','e','t'):
185 rmctx->codec_type = CODEC_AC3;
186 break;
188 case FOURCC('a','t','r','c'):
189 rmctx->codec_type = CODEC_ATRAC;
190 read_uint32be(fd, &rmctx->extradata_size);
191 skipped += 4;
192 read(fd, rmctx->codec_extradata, rmctx->extradata_size);
193 skipped += rmctx->extradata_size;
194 break;
196 default: /* Not a supported codec */
197 return -1;
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);
217 return skipped;
220 static int rm_parse_header(int fd, RMContext *rmctx, struct mp3entry *id3)
222 struct real_object_t obj;
223 int res;
224 int skipped;
225 off_t curpos;
226 uint8_t len; /* Holds a string_length, which is then passed to read_string() */
228 #ifdef SIMULATOR
229 uint32_t avg_bitrate = 0;
230 uint32_t max_packet_size;
231 uint32_t avg_packet_size;
232 uint32_t packet_count;
233 uint32_t duration;
234 uint32_t preroll;
235 uint32_t index_offset;
236 uint16_t stream_id;
237 uint32_t start_time;
238 uint32_t codec_data_size;
239 #endif
240 uint32_t v;
241 uint32_t max_bitrate;
242 uint16_t num_streams;
243 uint32_t next_data_off;
244 uint8_t header_end;
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 */
253 return -1;
255 else if (obj.fourcc != FOURCC('.','R','M','F'))
257 return -1;
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);
265 header_end = 0;
266 while(res)
268 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos);
269 skipped = 10;
270 if(obj.fourcc == FOURCC('I','N','D','X'))
271 break;
272 switch (obj.fourcc)
274 case FOURCC('P','R','O','P'): /* File properties */
275 read_uint32be(fd, &max_bitrate);
276 read_uint32be(fd, &rmctx->bit_rate); /*avg bitrate*/
277 #ifdef SIMULATOR
278 read_uint32be(fd, &max_packet_size);
279 read_uint32be(fd, &avg_packet_size);
280 read_uint32be(fd, &packet_count);
281 #else
282 lseek(fd, 3*sizeof(uint32_t), SEEK_CUR);
283 #endif
284 read_uint32be(fd, &rmctx->duration);
285 #ifdef SIMULATOR
286 read_uint32be(fd, &preroll);
287 read_uint32be(fd, &index_offset);
288 #else
289 lseek(fd, 2*sizeof(uint32_t), SEEK_CUR);
290 #endif
291 read_uint32be(fd, &rmctx->data_offset);
292 read_uint16be(fd, &num_streams);
293 read_uint16be(fd, &rmctx->flags);
294 skipped += 40;
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);
307 break;
309 case FOURCC('C','O','N','T'):
310 /* Four strings - Title, Author, Copyright, Comment */
311 read_uint8(fd,&len);
312 skipped += (int)read_string(fd, id3->id3v1buf[0], sizeof(id3->id3v1buf[0]), '\0', len);
313 read_uint8(fd,&len);
314 skipped += (int)read_string(fd, id3->id3v1buf[1], sizeof(id3->id3v1buf[1]), '\0', len);
315 read_uint8(fd,&len);
316 skipped += (int)read_string(fd, id3->id3v1buf[2], sizeof(id3->id3v1buf[2]), '\0', len);
317 read_uint8(fd,&len);
318 skipped += (int)read_string(fd, id3->id3v1buf[3], sizeof(id3->id3v1buf[3]), '\0', len);
319 skipped += 4;
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]);
325 break;
327 case FOURCC('M','D','P','R'): /* Media properties */
328 #ifdef SIMULATOR
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);
337 #else
338 lseek(fd, 30, SEEK_CUR);
339 #endif
340 skipped += 30;
341 read_uint8(fd,&len);
342 skipped += 1;
343 lseek(fd, len, SEEK_CUR); /* desc */
344 skipped += len;
345 read_uint8(fd,&len);
346 skipped += 1;
347 #ifdef SIMULATOR
348 lseek(fd, len, SEEK_CUR); /* mimetype */
349 read_uint32be(fd,&codec_data_size);
350 #else
351 lseek(fd, len + 4, SEEK_CUR);
352 #endif
353 skipped += len + 4;
354 read_uint32be(fd,&v);
355 skipped += 4;
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);
371 if(skipped < 0)
372 return -1;
375 break;
377 case FOURCC('D','A','T','A'):
378 read_uint32be(fd,&rmctx->nb_packets);
379 skipped += 4;
380 read_uint32be(fd,&next_data_off);
381 skipped += 4;
383 /***
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.
389 ***/
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);
395 header_end = 1;
396 break;
398 if(header_end) break;
399 curpos = lseek(fd, obj.size - skipped, SEEK_CUR);
400 res = real_read_object_header(fd, &obj);
404 return 0;
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)
413 return false;
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)
424 case CODEC_COOK:
425 /* Already set, do nothing */
426 break;
427 case CODEC_AAC:
428 id3->codectype = AFMT_RM_AAC;
429 break;
431 case CODEC_AC3:
432 id3->codectype = AFMT_RM_AC3;
433 break;
435 case CODEC_ATRAC:
436 id3->codectype = AFMT_RM_ATRAC3;
437 break;
440 id3->bitrate = rmctx->bit_rate / 1000;
441 id3->frequency = rmctx->sample_rate;
442 id3->length = rmctx->duration;
443 id3->filesize = filesize(fd);
444 return true;