fix some small glitches for backup:
[Rockbox.git] / apps / metadata / adx.c
blobf99d41fcd21415a1d36accce12254bec66414f8f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <inttypes.h>
25 #include "system.h"
26 #include "id3.h"
27 #include "metadata_common.h"
28 #include "metadata_parsers.h"
29 #include "debug.h"
31 bool get_adx_metadata(int fd, struct mp3entry* id3)
33 /* Use the trackname part of the id3 structure as a temporary buffer */
34 unsigned char * buf = (unsigned char *)id3->path;
35 int chanstart, channels, read_bytes;
36 int looping = 0, start_adr = 0, end_adr = 0;
38 /* try to get the basic header */
39 if ((lseek(fd, 0, SEEK_SET) < 0)
40 || ((read_bytes = read(fd, buf, 0x38)) < 0x38))
42 DEBUGF("lseek or read failed\n");
43 return false;
46 /* ADX starts with 0x80 */
47 if (buf[0] != 0x80) {
48 DEBUGF("get_adx_metadata: wrong first byte %c\n",buf[0]);
49 return false;
52 /* check for a reasonable offset */
53 chanstart = ((buf[2] << 8) | buf[3]) + 4;
54 if (chanstart > 4096) {
55 DEBUGF("get_adx_metadata: bad chanstart %i\n", chanstart);
56 return false;
59 /* check for a workable number of channels */
60 channels = buf[7];
61 if (channels != 1 && channels != 2) {
62 DEBUGF("get_adx_metadata: bad channel count %i\n",channels);
63 return false;
66 id3->frequency = get_long_be(&buf[8]);
67 /* 32 samples per 18 bytes */
68 id3->bitrate = id3->frequency * channels * 18 * 8 / 32 / 1000;
69 id3->length = get_long_be(&buf[12]) / id3->frequency * 1000;
70 id3->vbr = false;
71 id3->filesize = filesize(fd);
73 /* get loop info */
74 if (!memcmp(buf+0x10,"\x01\xF4\x03\x00",4)) {
75 /* Soul Calibur 2 style (type 03) */
76 DEBUGF("get_adx_metadata: type 03 found\n");
77 /* check if header is too small for loop data */
78 if (chanstart-6 < 0x2c) looping=0;
79 else {
80 looping = get_long_be(&buf[0x18]);
81 end_adr = get_long_be(&buf[0x28]);
82 start_adr = get_long_be(&buf[0x1c])/32*channels*18+chanstart;
84 } else if (!memcmp(buf+0x10,"\x01\xF4\x04\x00",4)) {
85 /* Standard (type 04) */
86 DEBUGF("get_adx_metadata: type 04 found\n");
87 /* check if header is too small for loop data */
88 if (chanstart-6 < 0x38) looping=0;
89 else {
90 looping = get_long_be(&buf[0x24]);
91 end_adr = get_long_be(&buf[0x34]);
92 start_adr = get_long_be(&buf[0x28])/32*channels*18+chanstart;
94 } else {
95 DEBUGF("get_adx_metadata: error, couldn't determine ADX type\n");
96 return false;
99 if (looping) {
100 /* 2 loops, 10 second fade */
101 id3->length = (start_adr-chanstart + 2*(end_adr-start_adr))
102 *8 / id3->bitrate + 10000;
105 /* try to get the channel header */
106 if ((lseek(fd, chanstart-6, SEEK_SET) < 0)
107 || ((read_bytes = read(fd, buf, 6)) < 6))
109 return false;
112 /* check channel header */
113 if (memcmp(buf, "(c)CRI", 6) != 0) return false;
115 return true;