au metadata parser: optimize just a little.
[kugel-rb.git] / apps / metadata / au.c
blob0639bd11e6a883d8b52cde2c6990aeaf019b132c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Yoshihisa Uchida
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 "system.h"
28 #include "metadata.h"
29 #include "metadata_common.h"
30 #include "metadata_parsers.h"
31 #include "logf.h"
33 /* table of bits per sample / 8 */
34 static const unsigned char bitspersamples[28] = {
36 1, /* G.711 MULAW */
37 1, /* 8bit */
38 2, /* 16bit */
39 3, /* 24bit */
40 4, /* 32bit */
41 4, /* 32bit */
42 8, /* 64bit */
43 0, /* Fragmented sample data */
44 0, /* DSP program */
45 0, /* 8bit fixed point */
46 0, /* 16bit fixed point */
47 0, /* 24bit fixed point */
48 0, /* 32bit fixed point */
53 0, /* 16bit linear with emphasis */
54 0, /* 16bit linear compressed */
55 0, /* 16bit linear with emphasis and compression */
56 0, /* Music kit DSP commands */
58 0, /* G.721 MULAW */
59 0, /* G.722 */
60 0, /* G.723 3bit */
61 0, /* G.723 5bit */
62 1, /* G.711 ALAW */
65 static inline unsigned char get_au_bitspersample(unsigned int encoding)
67 if (encoding > 27)
68 return 0;
69 return bitspersamples[encoding];
72 bool get_au_metadata(int fd, struct mp3entry* id3)
74 /* Use the trackname part of the id3 structure as a temporary buffer */
75 unsigned char* buf = (unsigned char *)id3->path;
76 unsigned long numbytes = 0;
77 int read_bytes;
78 int offset;
79 unsigned char bits_ch; /* bitspersample * channels */
81 id3->vbr = false; /* All Sun audio files are CBR */
82 id3->filesize = filesize(fd);
83 id3->length = 0;
85 if ((lseek(fd, 0, SEEK_SET) < 0) || ((read_bytes = read(fd, buf, 24)) < 0))
86 return false;
88 if (read_bytes < 24 || (memcmp(buf, ".snd", 4) != 0))
91 * no header
93 * frequency: 8000 Hz
94 * bits per sample: 8 bit
95 * channel: mono
97 numbytes = id3->filesize;
98 id3->frequency = 8000;
99 bits_ch = 1;
101 else
103 /* data offset */
104 offset = get_long_be(buf + 4);
105 if (offset < 24)
107 DEBUGF("CODEC_ERROR: sun audio offset size is small: %d\n", offset);
108 return false;
110 /* data size */
111 numbytes = get_long_be(buf + 8);
112 if (numbytes == (uint32_t)0xffffffff)
113 numbytes = id3->filesize - offset;
115 bits_ch = get_au_bitspersample(get_long_be(buf + 12)) * get_long_be(buf + 20);
116 id3->frequency = get_long_be(buf + 16);
119 /* Calculate track length [ms] */
120 if (bits_ch)
121 id3->length = ((int64_t)numbytes * 1000LL) / (bits_ch * id3->frequency);
123 return true;