Const correctness for output_dyn_value and unify some identical constants
[kugel-rb.git] / apps / metadata / rm.c
blob740ec1b1ed75acf85ba27bc443a17a27f7462fc4
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 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]));
53 struct real_object_t
55 uint32_t fourcc;
56 uint32_t size;
57 uint16_t version;
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)
64 int n;
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;
70 return 1;
73 #if (defined(SIMULATOR) && defined(DEBUG_RM))
74 static char* fourcc2str(uint32_t f)
76 static char res[5];
78 res[0] = (f & 0xff000000) >> 24;
79 res[1] = (f & 0xff0000) >> 16;
80 res[2] = (f & 0xff00) >> 8;
81 res[3] = (f & 0xff);
82 res[4] = 0;
84 return res;
86 #endif
88 static inline int real_read_audio_stream_info(int fd, RMContext *rmctx)
90 int skipped = 0;
91 uint32_t version;
92 struct real_object_t obj;
93 #ifdef SIMULATOR
94 uint32_t header_size;
95 uint16_t flavor;
96 uint32_t coded_framesize;
97 uint8_t interleaver_id_length;
98 uint8_t fourcc_length;
99 #endif
100 uint32_t interleaver_id;
101 uint32_t fourcc = 0;
103 memset(&obj,0,sizeof(obj));
104 read_uint32be(fd, &version);
105 skipped += 4;
107 DEBUGF(" version=0x%04lx\n",((version >> 16) & 0xff));
108 if (((version >> 16) & 0xff) == 3) {
109 /* Very old version */
110 } else {
111 #ifdef SIMULATOR
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);
119 #else
120 lseek(fd, 20, SEEK_CUR);
121 #endif
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 */
127 skipped += 40;
128 if (((version >> 16) & 0xff) == 5)
130 lseek(fd, 6, SEEK_CUR); /* unknown */
131 skipped += 6;
133 read_uint16be(fd, &rmctx->sample_rate);
134 lseek(fd, 4, SEEK_CUR); /* unknown */
135 read_uint16be(fd, &rmctx->nb_channels);
136 skipped += 8;
137 if (((version >> 16) & 0xff) == 4)
139 #ifdef SIMULATOR
140 read_uint8(fd, &interleaver_id_length);
141 read_uint32be(fd, &interleaver_id);
142 read_uint8(fd, &fourcc_length);
143 #else
144 lseek(fd, 6, SEEK_CUR);
145 #endif
146 read_uint32be(fd, &fourcc);
147 skipped += 10;
149 if (((version >> 16) & 0xff) == 5)
151 read_uint32be(fd, &interleaver_id);
152 read_uint32be(fd, &fourcc);
153 skipped += 8;
155 lseek(fd, 3, SEEK_CUR); /* unknown */
156 skipped += 3;
157 if (((version >> 16) & 0xff) == 5)
159 lseek(fd, 1, SEEK_CUR); /* unknown */
160 skipped += 1;
163 switch(fourcc) {
164 case FOURCC('c','o','o','k'):
165 rmctx->codec_type = CODEC_COOK;
166 read_uint32be(fd, &rmctx->extradata_size);
167 skipped += 4;
168 read(fd, rmctx->codec_extradata, rmctx->extradata_size);
169 skipped += rmctx->extradata_size;
170 break;
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);
176 skipped += 4;
177 read(fd, rmctx->codec_extradata, rmctx->extradata_size);
178 skipped += rmctx->extradata_size;
179 break;
181 case FOURCC('d','n','e','t'):
182 rmctx->codec_type = CODEC_AC3;
183 break;
185 case FOURCC('a','t','r','c'):
186 rmctx->codec_type = CODEC_ATRAC;
187 read_uint32be(fd, &rmctx->extradata_size);
188 skipped += 4;
189 read(fd, rmctx->codec_extradata, rmctx->extradata_size);
190 skipped += rmctx->extradata_size;
191 break;
193 default: /* Not a supported codec */
194 return -1;
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);
214 return skipped;
217 static int rm_parse_header(int fd, RMContext *rmctx, struct mp3entry *id3)
219 struct real_object_t obj;
220 int res;
221 int skipped;
222 off_t curpos;
223 uint8_t len; /* Holds a string_length, which is then passed to read_string() */
225 #ifdef SIMULATOR
226 uint32_t avg_bitrate = 0;
227 uint32_t max_packet_size;
228 uint32_t avg_packet_size;
229 uint32_t packet_count;
230 uint32_t duration;
231 uint32_t preroll;
232 uint32_t index_offset;
233 uint16_t stream_id;
234 uint32_t start_time;
235 uint32_t codec_data_size;
236 #endif
237 uint32_t v;
238 uint32_t max_bitrate;
239 uint16_t num_streams;
240 uint32_t next_data_off;
241 uint8_t header_end;
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 */
250 return -1;
252 else if (obj.fourcc != FOURCC('.','R','M','F'))
254 return -1;
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);
262 header_end = 0;
263 while(res)
265 DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos);
266 skipped = 10;
267 if(obj.fourcc == FOURCC('I','N','D','X'))
268 break;
269 switch (obj.fourcc)
271 case FOURCC('P','R','O','P'): /* File properties */
272 read_uint32be(fd, &max_bitrate);
273 read_uint32be(fd, &rmctx->bit_rate); /*avg bitrate*/
274 #ifdef SIMULATOR
275 read_uint32be(fd, &max_packet_size);
276 read_uint32be(fd, &avg_packet_size);
277 read_uint32be(fd, &packet_count);
278 #else
279 lseek(fd, 3*sizeof(uint32_t), SEEK_CUR);
280 #endif
281 read_uint32be(fd, &rmctx->duration);
282 #ifdef SIMULATOR
283 read_uint32be(fd, &preroll);
284 read_uint32be(fd, &index_offset);
285 #else
286 lseek(fd, 2*sizeof(uint32_t), SEEK_CUR);
287 #endif
288 read_uint32be(fd, &rmctx->data_offset);
289 read_uint16be(fd, &num_streams);
290 read_uint16be(fd, &rmctx->flags);
291 skipped += 40;
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);
304 break;
306 case FOURCC('C','O','N','T'):
307 /* Four strings - Title, Author, Copyright, Comment */
308 read_uint8(fd,&len);
309 skipped += (int)read_string(fd, id3->id3v1buf[0], sizeof(id3->id3v1buf[0]), '\0', len);
310 read_uint8(fd,&len);
311 skipped += (int)read_string(fd, id3->id3v1buf[1], sizeof(id3->id3v1buf[1]), '\0', len);
312 read_uint8(fd,&len);
313 skipped += (int)read_string(fd, id3->id3v1buf[2], sizeof(id3->id3v1buf[2]), '\0', len);
314 read_uint8(fd,&len);
315 skipped += (int)read_string(fd, id3->id3v1buf[3], sizeof(id3->id3v1buf[3]), '\0', len);
316 skipped += 4;
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]);
322 break;
324 case FOURCC('M','D','P','R'): /* Media properties */
325 #ifdef SIMULATOR
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);
334 #else
335 lseek(fd, 30, SEEK_CUR);
336 #endif
337 skipped += 30;
338 read_uint8(fd,&len);
339 skipped += 1;
340 lseek(fd, len, SEEK_CUR); /* desc */
341 skipped += len;
342 read_uint8(fd,&len);
343 skipped += 1;
344 #ifdef SIMULATOR
345 lseek(fd, len, SEEK_CUR); /* mimetype */
346 read_uint32be(fd,&codec_data_size);
347 #else
348 lseek(fd, len + 4, SEEK_CUR);
349 #endif
350 skipped += len + 4;
351 //From ffmpeg: codec_pos = url_ftell(pb);
352 read_uint32be(fd,&v);
353 skipped += 4;
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);
369 if(skipped < 0)
370 return -1;
373 break;
375 case FOURCC('D','A','T','A'):
376 read_uint32be(fd,&rmctx->nb_packets);
377 skipped += 4;
378 read_uint32be(fd,&next_data_off);
379 skipped += 4;
381 /***
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.
387 ***/
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);
393 header_end = 1;
394 break;
396 if(header_end) break;
397 curpos = lseek(fd, obj.size - skipped, SEEK_CUR);
398 res = real_read_object_header(fd, &obj);
402 return 0;
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)
411 return false;
413 /* Copy tags */
414 id3->title = id3->id3v1buf[0];
415 id3->artist = id3->id3v1buf[1];
416 id3->comment = id3->id3v1buf[3];
418 switch(rmctx->codec_type)
420 case CODEC_COOK:
421 /* Already set, do nothing */
422 break;
423 case CODEC_AAC:
424 id3->codectype = AFMT_RM_AAC;
425 break;
427 case CODEC_AC3:
428 id3->codectype = AFMT_RM_AC3;
429 break;
431 case CODEC_ATRAC:
432 id3->codectype = AFMT_RM_ATRAC3;
433 break;
436 id3->bitrate = rmctx->bit_rate / 1000;
437 id3->frequency = rmctx->sample_rate;
438 id3->length = rmctx->duration;
439 id3->filesize = filesize(fd);
440 return true;