Android: Partly revert r29569 and only call the new getJavaEnvironment() when needed.
[maemo-rb.git] / apps / mp3data.c
blobbb42c94fc665bb87bb4381dcc0c5686256201c4c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Daniel Stenberg
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 ****************************************************************************/
23 * Parts of this code has been stolen from the Ample project and was written
24 * by David Härdeman. It has since been extended and enhanced pretty much by
25 * all sorts of friendly Rockbox people.
27 * A nice reference for MPEG header info:
28 * http://rockbox.haxx.se/docs/mpeghdr.html
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdbool.h>
36 #include <limits.h>
37 #include "debug.h"
38 #include "logf.h"
39 #include "mp3data.h"
40 #include "file.h"
41 #include "buffer.h"
43 // #define DEBUG_VERBOSE
45 #ifdef DEBUG_VERBOSE
46 #define VDEBUGF DEBUGF
47 #else
48 #define VDEBUGF(...) do { } while(0)
49 #endif
51 #define SYNC_MASK (0x7ffL << 21)
52 #define VERSION_MASK (3L << 19)
53 #define LAYER_MASK (3L << 17)
54 #define PROTECTION_MASK (1L << 16)
55 #define BITRATE_MASK (0xfL << 12)
56 #define SAMPLERATE_MASK (3L << 10)
57 #define PADDING_MASK (1L << 9)
58 #define PRIVATE_MASK (1L << 8)
59 #define CHANNELMODE_MASK (3L << 6)
60 #define MODE_EXT_MASK (3L << 4)
61 #define COPYRIGHT_MASK (1L << 3)
62 #define ORIGINAL_MASK (1L << 2)
63 #define EMPHASIS_MASK 3L
65 /* MPEG Version table, sorted by version index */
66 static const signed char version_table[4] = {
67 MPEG_VERSION2_5, -1, MPEG_VERSION2, MPEG_VERSION1
70 /* Bitrate table for mpeg audio, indexed by row index and birate index */
71 static const short bitrates[5][16] = {
72 {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0}, /* V1 L1 */
73 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0}, /* V1 L2 */
74 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0}, /* V1 L3 */
75 {0,32,48,56, 64, 80, 96,112,128,144,160,176,192,224,256,0}, /* V2 L1 */
76 {0, 8,16,24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160,0} /* V2 L2+L3 */
79 /* Bitrate pointer table, indexed by version and layer */
80 static const short *bitrate_table[3][3] =
82 {bitrates[0], bitrates[1], bitrates[2]},
83 {bitrates[3], bitrates[4], bitrates[4]},
84 {bitrates[3], bitrates[4], bitrates[4]}
87 /* Sampling frequency table, indexed by version and frequency index */
88 static const unsigned short freq_table[3][3] =
90 {44100, 48000, 32000}, /* MPEG Version 1 */
91 {22050, 24000, 16000}, /* MPEG version 2 */
92 {11025, 12000, 8000}, /* MPEG version 2.5 */
95 unsigned long bytes2int(unsigned long b0, unsigned long b1,
96 unsigned long b2, unsigned long b3)
98 return (b0 & 0xFF) << (3*8) |
99 (b1 & 0xFF) << (2*8) |
100 (b2 & 0xFF) << (1*8) |
101 (b3 & 0xFF) << (0*8);
104 /* check if 'head' is a valid mp3 frame header */
105 static bool is_mp3frameheader(unsigned long head)
107 if ((head & SYNC_MASK) != (unsigned long)SYNC_MASK) /* bad sync? */
108 return false;
109 if ((head & VERSION_MASK) == (1L << 19)) /* bad version? */
110 return false;
111 if (!(head & LAYER_MASK)) /* no layer? */
112 return false;
113 #if CONFIG_CODEC != SWCODEC
114 /* The MAS can't decode layer 1, so treat layer 1 data as invalid */
115 if ((head & LAYER_MASK) == LAYER_MASK)
116 return false;
117 #endif
118 if ((head & BITRATE_MASK) == BITRATE_MASK) /* bad bitrate? */
119 return false;
120 if (!(head & BITRATE_MASK)) /* no bitrate? */
121 return false;
122 if ((head & SAMPLERATE_MASK) == SAMPLERATE_MASK) /* bad sample rate? */
123 return false;
125 return true;
128 static bool mp3headerinfo(struct mp3info *info, unsigned long header)
130 int bitindex, freqindex;
132 /* MPEG Audio Version */
133 if ((header & VERSION_MASK) >> 19 >= sizeof(version_table))
134 return false;
136 info->version = version_table[(header & VERSION_MASK) >> 19];
137 if (info->version < 0)
138 return false;
140 /* Layer */
141 info->layer = 3 - ((header & LAYER_MASK) >> 17);
142 if (info->layer == 3)
143 return false;
145 /* Rockbox: not used
146 info->protection = (header & PROTECTION_MASK) ? true : false;
149 /* Bitrate */
150 bitindex = (header & BITRATE_MASK) >> 12;
151 info->bitrate = bitrate_table[info->version][info->layer][bitindex];
152 if(info->bitrate == 0)
153 return false;
155 /* Sampling frequency */
156 freqindex = (header & SAMPLERATE_MASK) >> 10;
157 if (freqindex == 3)
158 return false;
159 info->frequency = freq_table[info->version][freqindex];
161 info->padding = (header & PADDING_MASK) ? 1 : 0;
163 /* Calculate number of bytes, calculation depends on layer */
164 if (info->layer == 0) {
165 info->frame_samples = 384;
166 info->frame_size = (12000 * info->bitrate / info->frequency
167 + info->padding) * 4;
169 else {
170 if ((info->version > MPEG_VERSION1) && (info->layer == 2))
171 info->frame_samples = 576;
172 else
173 info->frame_samples = 1152;
174 info->frame_size = (1000/8) * info->frame_samples * info->bitrate
175 / info->frequency + info->padding;
178 /* Frametime fraction denominator */
179 if (freqindex != 0) { /* 48/32/24/16/12/8 kHz */
180 info->ft_den = 1; /* integer number of milliseconds */
182 else { /* 44.1/22.05/11.025 kHz */
183 if (info->layer == 0) /* layer 1 */
184 info->ft_den = 147;
185 else /* layer 2+3 */
186 info->ft_den = 49;
188 /* Frametime fraction numerator */
189 info->ft_num = 1000 * info->ft_den * info->frame_samples / info->frequency;
191 info->channel_mode = (header & CHANNELMODE_MASK) >> 6;
192 /* Rockbox: not used
193 info->mode_extension = (header & MODE_EXT_MASK) >> 4;
194 info->emphasis = header & EMPHASIS_MASK;
196 VDEBUGF( "Header: %08lx, Ver %d, lay %d, bitr %d, freq %ld, "
197 "chmode %d, bytes: %d time: %d/%d\n",
198 header, info->version, info->layer+1, info->bitrate,
199 info->frequency, info->channel_mode,
200 info->frame_size, info->ft_num, info->ft_den);
201 return true;
204 static unsigned long __find_next_frame(int fd, long *offset, long max_offset,
205 unsigned long last_header,
206 int(*getfunc)(int fd, unsigned char *c))
208 unsigned long header=0;
209 unsigned char tmp;
210 int i;
212 long pos = 0;
214 /* We remember the last header we found, to use as a template to see if
215 the header we find has the same frequency, layer etc */
216 last_header &= 0xffff0c00;
218 /* Fill up header with first 24 bits */
219 for(i = 0; i < 3; i++) {
220 header <<= 8;
221 if(!getfunc(fd, &tmp))
222 return 0;
223 header |= tmp;
224 pos++;
227 do {
228 header <<= 8;
229 if(!getfunc(fd, &tmp))
230 return 0;
231 header |= tmp;
232 pos++;
233 if(max_offset > 0 && pos > max_offset)
234 return 0;
235 } while(!is_mp3frameheader(header) ||
236 (last_header?((header & 0xffff0c00) != last_header):false));
238 *offset = pos - 4;
240 if(*offset)
241 VDEBUGF("Warning: skipping %ld bytes of garbage\n", *offset);
243 return header;
246 static int fileread(int fd, unsigned char *c)
248 return read(fd, c, 1);
251 unsigned long find_next_frame(int fd, long *offset, long max_offset, unsigned long last_header)
253 return __find_next_frame(fd, offset, max_offset, last_header, fileread);
256 #ifndef __PCTOOL__
257 static int fnf_read_index;
258 static int fnf_buf_len;
260 static int buf_getbyte(int fd, unsigned char *c)
262 if(fnf_read_index < fnf_buf_len)
264 *c = audiobuf[fnf_read_index++];
265 return 1;
267 else
269 fnf_buf_len = read(fd, audiobuf, audiobufend - audiobuf);
270 if(fnf_buf_len < 0)
271 return -1;
273 fnf_read_index = 0;
275 if(fnf_buf_len > 0)
277 *c = audiobuf[fnf_read_index++];
278 return 1;
280 else
281 return 0;
283 return 0;
286 static int buf_seek(int fd, int len)
288 fnf_read_index += len;
289 if(fnf_read_index > fnf_buf_len)
291 len = fnf_read_index - fnf_buf_len;
293 fnf_buf_len = read(fd, audiobuf, audiobufend - audiobuf);
294 if(fnf_buf_len < 0)
295 return -1;
297 fnf_read_index = 0;
298 fnf_read_index += len;
301 if(fnf_read_index > fnf_buf_len)
303 return -1;
305 else
306 return 0;
309 static void buf_init(void)
311 fnf_buf_len = 0;
312 fnf_read_index = 0;
315 static unsigned long buf_find_next_frame(int fd, long *offset, long max_offset,
316 unsigned long last_header)
318 return __find_next_frame(fd, offset, max_offset, last_header, buf_getbyte);
321 static int audiobuflen;
322 static int mem_pos;
323 static int mem_cnt;
324 static int mem_maxlen;
326 static int mem_getbyte(int dummy, unsigned char *c)
328 dummy = dummy;
330 *c = audiobuf[mem_pos++];
331 if(mem_pos >= audiobuflen)
332 mem_pos = 0;
334 if(mem_cnt++ >= mem_maxlen)
335 return 0;
336 else
337 return 1;
340 unsigned long mem_find_next_frame(int startpos, long *offset, long max_offset,
341 unsigned long last_header)
343 audiobuflen = audiobufend - audiobuf;
344 mem_pos = startpos;
345 mem_cnt = 0;
346 mem_maxlen = max_offset;
348 return __find_next_frame(0, offset, max_offset, last_header, mem_getbyte);
350 #endif
352 /* Extract information from a 'Xing' or 'Info' header. */
353 static void get_xing_info(struct mp3info *info, unsigned char *buf)
355 int i = 8;
357 /* Is it a VBR file? */
358 info->is_vbr = !memcmp(buf, "Xing", 4);
360 if (buf[7] & VBR_FRAMES_FLAG) /* Is the frame count there? */
362 info->frame_count = bytes2int(buf[i], buf[i+1], buf[i+2], buf[i+3]);
363 if (info->frame_count <= ULONG_MAX / info->ft_num)
364 info->file_time = info->frame_count * info->ft_num / info->ft_den;
365 else
366 info->file_time = info->frame_count / info->ft_den * info->ft_num;
367 i += 4;
370 if (buf[7] & VBR_BYTES_FLAG) /* Is byte count there? */
372 info->byte_count = bytes2int(buf[i], buf[i+1], buf[i+2], buf[i+3]);
373 i += 4;
376 if (info->file_time && info->byte_count)
378 if (info->byte_count <= (ULONG_MAX/8))
379 info->bitrate = info->byte_count * 8 / info->file_time;
380 else
381 info->bitrate = info->byte_count / (info->file_time >> 3);
384 if (buf[7] & VBR_TOC_FLAG) /* Is table-of-contents there? */
386 info->has_toc = true;
387 memcpy( info->toc, buf+i, 100 );
388 i += 100;
390 if (buf[7] & VBR_QUALITY_FLAG)
392 /* We don't care about this, but need to skip it */
393 i += 4;
395 #if CONFIG_CODEC==SWCODEC
396 i += 21;
397 info->enc_delay = ((int)buf[i ] << 4) | (buf[i+1] >> 4);
398 info->enc_padding = ((int)buf[i+1] << 8) | buf[i+2];
399 /* TODO: This sanity checking is rather silly, seeing as how the LAME
400 header contains a CRC field that can be used to verify integrity. */
401 if (!(info->enc_delay >= 0 && info->enc_delay <= 2880 &&
402 info->enc_padding >= 0 && info->enc_padding <= 2*1152))
404 /* Invalid data */
405 info->enc_delay = -1;
406 info->enc_padding = -1;
408 #endif
411 /* Extract information from a 'VBRI' header. */
412 static void get_vbri_info(struct mp3info *info, unsigned char *buf)
414 int i, num_offsets, offset = 0;
416 info->is_vbr = true; /* Yes, it is a FhG VBR file */
417 info->has_toc = false; /* We don't parse the TOC (yet) */
419 info->byte_count = bytes2int(buf[10], buf[11], buf[12], buf[13]);
420 info->frame_count = bytes2int(buf[14], buf[15], buf[16], buf[17]);
421 if (info->frame_count <= ULONG_MAX / info->ft_num)
422 info->file_time = info->frame_count * info->ft_num / info->ft_den;
423 else
424 info->file_time = info->frame_count / info->ft_den * info->ft_num;
426 if (info->byte_count <= (ULONG_MAX/8))
427 info->bitrate = info->byte_count * 8 / info->file_time;
428 else
429 info->bitrate = info->byte_count / (info->file_time >> 3);
431 /* We don't parse the TOC, since we don't yet know how to (FIXME) */
432 num_offsets = bytes2int(0, 0, buf[18], buf[19]);
433 VDEBUGF("Frame size (%dkpbs): %d bytes (0x%x)\n",
434 info->bitrate, info->frame_size, info->frame_size);
435 VDEBUGF("Frame count: %lx\n", info->frame_count);
436 VDEBUGF("Byte count: %lx\n", info->byte_count);
437 VDEBUGF("Offsets: %d\n", num_offsets);
438 VDEBUGF("Frames/entry: %ld\n",
439 bytes2int(0, 0, buf[24], buf[25]));
441 for(i = 0; i < num_offsets; i++)
443 offset += bytes2int(0, 0, buf[26+i*2], buf[27+i*2]);;
444 VDEBUGF("%03d: %lx\n", i, offset - bytecount,);
448 /* Seek to next mpeg header and extract relevant information. */
449 static int get_next_header_info(int fd, long *bytecount, struct mp3info *info)
451 long tmp;
452 unsigned long header = find_next_frame(fd, &tmp, 0x20000, 0);
453 if(header == 0)
454 return -1;
456 if(!mp3headerinfo(info, header))
457 return -2;
459 /* Next header is tmp bytes away. */
460 *bytecount += tmp;
462 return 0;
465 int get_mp3file_info(int fd, struct mp3info *info)
467 unsigned char frame[1800], *vbrheader;
468 long bytecount = 0;
469 int result;
471 /* Initialize info and frame */
472 memset(info, 0, sizeof(struct mp3info));
473 memset(frame, 0, sizeof(frame));
475 #if CONFIG_CODEC==SWCODEC
476 /* These two are needed for proper LAME gapless MP3 playback */
477 info->enc_delay = -1;
478 info->enc_padding = -1;
479 #endif
481 /* Get the very first MPEG frame. */
482 result = get_next_header_info(fd, &bytecount, info);
483 if(result)
484 return result;
486 /* OK, we have found a frame. Let's see if it has a Xing header */
487 if (info->frame_size-4 >= (int)sizeof(frame))
489 DEBUGF("Error: Invalid id3 header, frame_size: %d\n", info->frame_size);
490 return -8;
493 if(read(fd, frame, info->frame_size-4) < 0)
494 return -3;
496 /* Calculate position of a possible VBR header */
497 if (info->version == MPEG_VERSION1) {
498 if (info->channel_mode == 3) /* mono */
499 vbrheader = frame + 17;
500 else
501 vbrheader = frame + 32;
502 } else {
503 if (info->channel_mode == 3) /* mono */
504 vbrheader = frame + 9;
505 else
506 vbrheader = frame + 17;
509 if (!memcmp(vbrheader, "Xing", 4) || !memcmp(vbrheader, "Info", 4))
511 VDEBUGF("-- XING header --\n");
513 /* We want to skip the Xing frame when playing the stream */
514 bytecount += info->frame_size;
516 /* Now get the next frame to read the real info about the mp3 stream */
517 result = get_next_header_info(fd, &bytecount, info);
518 if(result)
519 return result;
521 get_xing_info(info, vbrheader);
523 else if (!memcmp(vbrheader, "VBRI", 4))
525 VDEBUGF("-- VBRI header --\n");
527 /* We want to skip the VBRI frame when playing the stream */
528 bytecount += info->frame_size;
530 /* Now get the next frame to read the real info about the mp3 stream */
531 result = get_next_header_info(fd, &bytecount, info);
532 if(result)
533 return result;
535 get_vbri_info(info, vbrheader);
537 else
539 VDEBUGF("-- No VBR header --\n");
542 return bytecount;
545 #ifndef __PCTOOL__
546 static void long2bytes(unsigned char *buf, long val)
548 buf[0] = (val >> 24) & 0xff;
549 buf[1] = (val >> 16) & 0xff;
550 buf[2] = (val >> 8) & 0xff;
551 buf[3] = val & 0xff;
554 int count_mp3_frames(int fd, int startpos, int filesize,
555 void (*progressfunc)(int))
557 unsigned long header = 0;
558 struct mp3info info;
559 int num_frames;
560 long bytes;
561 int cnt;
562 long progress_chunk = filesize / 50; /* Max is 50%, in 1% increments */
563 int progress_cnt = 0;
564 bool is_vbr = false;
565 int last_bitrate = 0;
566 int header_template = 0;
568 if(lseek(fd, startpos, SEEK_SET) < 0)
569 return -1;
571 buf_init();
573 /* Find out the total number of frames */
574 num_frames = 0;
575 cnt = 0;
577 while((header = buf_find_next_frame(fd, &bytes, -1, header_template))) {
578 mp3headerinfo(&info, header);
580 if(!header_template)
581 header_template = header;
583 /* See if this really is a VBR file */
584 if(last_bitrate && info.bitrate != last_bitrate)
586 is_vbr = true;
588 last_bitrate = info.bitrate;
590 buf_seek(fd, info.frame_size-4);
591 num_frames++;
592 if(progressfunc)
594 cnt += bytes + info.frame_size;
595 if(cnt > progress_chunk)
597 progress_cnt++;
598 progressfunc(progress_cnt);
599 cnt = 0;
603 VDEBUGF("Total number of frames: %d\n", num_frames);
605 if(is_vbr)
606 return num_frames;
607 else
609 DEBUGF("Not a VBR file\n");
610 return 0;
614 static const char cooltext[] = "Rockbox - rocks your box";
616 /* buf needs to be the audio buffer with TOC generation enabled,
617 and at least MAX_XING_HEADER_SIZE bytes otherwise */
618 int create_xing_header(int fd, long startpos, long filesize,
619 unsigned char *buf, unsigned long num_frames,
620 unsigned long rec_time, unsigned long header_template,
621 void (*progressfunc)(int), bool generate_toc)
623 struct mp3info info;
624 unsigned char toc[100];
625 unsigned long header = 0;
626 unsigned long xing_header_template = header_template;
627 unsigned long filepos;
628 long pos, last_pos;
629 long j;
630 long bytes;
631 int i;
632 int index;
634 DEBUGF("create_xing_header()\n");
636 if(generate_toc)
638 lseek(fd, startpos, SEEK_SET);
639 buf_init();
641 /* Generate filepos table */
642 last_pos = 0;
643 filepos = 0;
644 header = 0;
645 for(i = 0;i < 100;i++) {
646 /* Calculate the absolute frame number for this seek point */
647 pos = i * num_frames / 100;
649 /* Advance from the last seek point to this one */
650 for(j = 0;j < pos - last_pos;j++)
652 header = buf_find_next_frame(fd, &bytes, -1, header_template);
653 filepos += bytes;
654 mp3headerinfo(&info, header);
655 buf_seek(fd, info.frame_size-4);
656 filepos += info.frame_size;
658 if(!header_template)
659 header_template = header;
662 /* Save a header for later use if header_template is empty.
663 We only save one header, and we want to save one in the
664 middle of the stream, just in case the first and the last
665 headers are corrupt. */
666 if(!xing_header_template && i == 1)
667 xing_header_template = header;
669 if(progressfunc)
671 progressfunc(50 + i/2);
674 /* Fill in the TOC entry */
675 /* each toc is a single byte indicating how many 256ths of the
676 * way through the file, is that percent of the way through the
677 * song. the easy method, filepos*256/filesize, chokes when
678 * the upper 8 bits of the file position are nonzero
679 * (i.e. files over 16mb in size).
681 if (filepos > (ULONG_MAX/256))
683 /* instead of multiplying filepos by 256, we divide
684 * filesize by 256.
686 toc[i] = filepos / (filesize >> 8);
688 else
690 toc[i] = filepos * 256 / filesize;
693 VDEBUGF("Pos %d: %ld relpos: %ld filepos: %lx tocentry: %x\n",
694 i, pos, pos-last_pos, filepos, toc[i]);
696 last_pos = pos;
700 /* Use the template header and create a new one.
701 We ignore the Protection bit even if the rest of the stream is
702 protected. */
703 header = xing_header_template & ~(BITRATE_MASK|PROTECTION_MASK|PADDING_MASK);
704 header |= 8 << 12; /* This gives us plenty of space, 192..576 bytes */
706 if (!mp3headerinfo(&info, header))
707 return 0; /* invalid header */
709 if (num_frames == 0 && rec_time) {
710 /* estimate the number of frames based on the recording time */
711 if (rec_time <= ULONG_MAX / info.ft_den)
712 num_frames = rec_time * info.ft_den / info.ft_num;
713 else
714 num_frames = rec_time / info.ft_num * info.ft_den;
717 /* Clear the frame */
718 memset(buf, 0, MAX_XING_HEADER_SIZE);
720 /* Write the header to the buffer */
721 long2bytes(buf, header);
723 /* Calculate position of VBR header */
724 if (info.version == MPEG_VERSION1) {
725 if (info.channel_mode == 3) /* mono */
726 index = 21;
727 else
728 index = 36;
730 else {
731 if (info.channel_mode == 3) /* mono */
732 index = 13;
733 else
734 index = 21;
737 /* Create the Xing data */
738 memcpy(&buf[index], "Xing", 4);
739 long2bytes(&buf[index+4], (num_frames ? VBR_FRAMES_FLAG : 0)
740 | (filesize ? VBR_BYTES_FLAG : 0)
741 | (generate_toc ? VBR_TOC_FLAG : 0));
742 index += 8;
743 if(num_frames)
745 long2bytes(&buf[index], num_frames);
746 index += 4;
749 if(filesize)
751 long2bytes(&buf[index], filesize - startpos);
752 index += 4;
755 /* Copy the TOC */
756 memcpy(buf + index, toc, 100);
758 /* And some extra cool info */
759 memcpy(buf + index + 100, cooltext, sizeof(cooltext));
761 #ifdef DEBUG
762 for(i = 0;i < info.frame_size;i++)
764 if(i && !(i % 16))
765 DEBUGF("\n");
767 DEBUGF("%02x ", buf[i]);
769 #endif
771 return info.frame_size;
774 #endif