Add stdio.h include for SEEK_SET define to various files.
[maemo-rb.git] / apps / metadata / mpc.c
blob0dd76fcc4ba5459286ad53c3084bea30f7e08224
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Thom Johansen
11 * Copyright (C) 2010 Andree Buschmann
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include <string.h>
24 #include <stdio.h>
25 #include <inttypes.h>
26 #include "system.h"
27 #include "metadata.h"
28 #include "metadata_common.h"
29 #include "metadata_parsers.h"
30 #include "logf.h"
31 #include "replaygain.h"
32 #include "fixedpoint.h"
34 /* Needed for replay gain and clipping prevention of SV8 files. */
35 #define SV8_TO_SV7_CONVERT_GAIN (6482) /* 64.82 * 100, MPC_OLD_GAIN_REF */
36 #define SV8_TO_SV7_CONVERT_PEAK (23119) /* 256 * 20 * log10(32768) */
38 static int set_replaygain_sv7(struct mp3entry* id3,
39 bool album,
40 long value,
41 long used)
43 long gain = (int16_t) ((value >> 16) & 0xffff);
44 long peak = (uint16_t) (value & 0xffff);
46 /* We use a peak value of 0 to indicate a given gain type isn't used. */
47 if (peak != 0) {
48 /* Save the ReplayGain data to id3-structure for further processing. */
49 used += parse_replaygain_int(album, gain * 512 / 100, peak << 9,
50 id3, id3->toc + used, sizeof(id3->toc) - used);
53 return used;
56 static int set_replaygain_sv8(struct mp3entry* id3,
57 bool album,
58 long gain,
59 long peak,
60 long used)
62 gain = (long)(SV8_TO_SV7_CONVERT_GAIN - ((gain*100)/256));
64 /* Transform SV8's logarithmic peak representation to the desired linear
65 * representation: linear = pow(10, peak/256/20).
67 * FP_BITS = 24 bits = desired fp representation for dsp routines
68 * FRAC_BITS = 12 bits = resolution used for fp_bits
69 * fp_factor(peak*(1<<FRAC_BITS)/256, FRAC_BITS) << (FP_BITS-FRAC_BITS)
70 **/
71 peak = (fp_factor((peak-SV8_TO_SV7_CONVERT_PEAK)*16, 12) << 12);
73 /* We use a peak value of 0 to indicate a given gain type isn't used. */
74 if (peak != 0) {
75 /* Save the ReplayGain data to id3-structure for further processing. */
76 used += parse_replaygain_int(album, gain * 512 / 100, peak,
77 id3, id3->toc + used, sizeof(id3->toc) - used);
80 return used;
83 static int sv8_get_size(uint8_t *buffer, int index, uint64_t *p_size)
85 unsigned char tmp;
86 uint64_t size = 0;
88 do {
89 tmp = buffer[index++];
90 size = (size << 7) | (tmp & 0x7F);
91 } while((tmp & 0x80));
93 *p_size = size;
94 return index;
97 bool get_musepack_metadata(int fd, struct mp3entry *id3)
99 static const int32_t sfreqs[4] = { 44100, 48000, 37800, 32000 };
100 uint32_t header[8];
101 uint64_t samples = 0;
102 int i;
104 if (!skip_id3v2(fd, id3))
105 return false;
106 if (read(fd, header, 4*8) != 4*8) return false;
107 /* Musepack files are little endian, might need swapping */
108 for (i = 1; i < 8; i++)
109 header[i] = letoh32(header[i]);
110 if (!memcmp(header, "MP+", 3)) { /* Compare to sig "MP+" */
111 unsigned int streamversion;
112 header[0] = letoh32(header[0]);
113 streamversion = (header[0] >> 24) & 15;
114 if (streamversion == 7) {
115 unsigned int gapless = (header[5] >> 31) & 0x0001;
116 unsigned int last_frame_samples = (header[5] >> 20) & 0x07ff;
117 unsigned int bufused = 0;
119 id3->frequency = sfreqs[(header[2] >> 16) & 0x0003];
120 samples = (uint64_t)header[1]*1152; /* 1152 is mpc frame size */
121 if (gapless)
122 samples -= 1152 - last_frame_samples;
123 else
124 samples -= 481; /* Musepack subband synth filter delay */
126 bufused = set_replaygain_sv7(id3, false, header[3], bufused);
127 bufused = set_replaygain_sv7(id3, true , header[4], bufused);
128 } else {
129 return false; /* only SV7 is allowed within a "MP+" signature */
131 } else if (!memcmp(header, "MPCK", 4)) { /* Compare to sig "MPCK" */
132 uint8_t sv8_header[32];
133 /* 4 bytes 'MPCK' */
134 lseek(fd, 4, SEEK_SET);
135 if (read(fd, sv8_header, 2) != 2) return false; /* read frame ID */
136 if (!memcmp(sv8_header, "SH", 2)) { /* Stream Header ID */
137 int32_t k = 0;
138 uint32_t streamversion;
139 uint64_t size = 0; /* tag size */
140 uint64_t dummy = 0; /* used to dummy read data from header */
142 /* 4 bytes 'MPCK' + 2 'SH' */
143 lseek(fd, 6, SEEK_SET);
144 if (read(fd, sv8_header, 32) != 32) return false;
146 /* Read the size of 'SH'-tag */
147 k = sv8_get_size(sv8_header, k, &size);
149 /* Skip crc32 */
150 k += 4;
152 /* Read stream version */
153 streamversion = sv8_header[k++];
154 if (streamversion != 8) return false; /* Only SV8 is allowed. */
156 /* Number of samples */
157 k = sv8_get_size(sv8_header, k, &samples);
159 /* Number of leading zero-samples */
160 k = sv8_get_size(sv8_header, k, &dummy);
162 /* Sampling frequency */
163 id3->frequency = sfreqs[(sv8_header[k++] >> 5) & 0x0003];
165 /* Number of channels */
166 id3->channels = (sv8_header[k++] >> 4) + 1;
168 /* Skip to next tag: k = size -2 */
169 k = size - 2;
171 if (!memcmp(sv8_header+k, "RG", 2)) { /* Replay Gain ID */
172 long peak, gain;
173 int bufused = 0;
175 k += 2; /* 2 bytes 'RG' */
177 /* sv8_get_size must be called to skip the right amount of
178 * bits within the header data. */
179 k = sv8_get_size(sv8_header, k, &size);
181 /* Read and set replay gain */
182 if (sv8_header[k++] == 1) {
183 /* Title's peak and gain */
184 gain = (int16_t) ((sv8_header[k]<<8) + sv8_header[k+1]); k += 2;
185 peak = (uint16_t)((sv8_header[k]<<8) + sv8_header[k+1]); k += 2;
186 bufused += set_replaygain_sv8(id3, false, gain, peak, bufused);
188 /* Album's peak and gain */
189 gain = (int16_t) ((sv8_header[k]<<8) + sv8_header[k+1]); k += 2;
190 peak = (uint16_t)((sv8_header[k]<<8) + sv8_header[k+1]); k += 2;
191 bufused += set_replaygain_sv8(id3, true , gain, peak, bufused);
194 } else {
195 /* No sv8 stream header found */
196 return false;
198 } else {
199 return false; /* SV4-6 is not supported anymore */
202 id3->vbr = true;
203 /* Estimate bitrate, we should probably subtract the various header sizes
204 here for super-accurate results */
205 id3->length = ((int64_t) samples * 1000) / id3->frequency;
207 if (id3->length <= 0)
209 logf("mpc length invalid!");
210 return false;
213 id3->filesize = filesize(fd);
214 id3->bitrate = id3->filesize * 8 / id3->length;
216 read_ape_tags(fd, id3);
217 return true;