Updated italian translation.
[kugel-rb.git] / apps / metadata / mpc.c
blob298f8faa4031341f9dd31164af0d15c3d73eb111
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Thom Johansen
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 ****************************************************************************/
20 #include <string.h>
21 #include <inttypes.h>
22 #include "system.h"
23 #include "id3.h"
24 #include "metadata_common.h"
25 #include "logf.h"
26 #include "replaygain.h"
28 static int set_replaygain(struct mp3entry* id3, bool album, long value,
29 long used)
31 long gain = (int16_t) ((value >> 16) & 0xffff);
32 long peak = (uint16_t) (value & 0xffff);
34 /* We use a peak value of 0 to indicate a given gain type isn't used. */
35 if (peak != 0)
37 /* Use the Xing TOC field to store ReplayGain strings for use in the
38 * ID3 screen, since Musepack files shouldn't need to use it in any
39 * other way.
41 used += parse_replaygain_int(album, gain * 512 / 100, peak << 9,
42 id3, id3->toc + used, sizeof(id3->toc) - used);
45 return used;
48 bool get_musepack_metadata(int fd, struct mp3entry *id3)
50 const int32_t sfreqs_sv7[4] = { 44100, 48000, 37800, 32000 };
51 uint32_t header[8];
52 uint64_t samples = 0;
53 int i;
55 if (!skip_id3v2(fd, id3))
56 return false;
57 if (read(fd, header, 4*8) != 4*8) return false;
58 /* Musepack files are little endian, might need swapping */
59 for (i = 1; i < 8; i++)
60 header[i] = letoh32(header[i]);
61 if (!memcmp(header, "MP+", 3)) { /* Compare to sig "MP+" */
62 unsigned int streamversion;
64 header[0] = letoh32(header[0]);
65 streamversion = (header[0] >> 24) & 15;
66 if (streamversion >= 8) {
67 return false; /* SV8 or higher don't exist yet, so no support */
68 } else if (streamversion == 7) {
69 unsigned int gapless = (header[5] >> 31) & 0x0001;
70 unsigned int last_frame_samples = (header[5] >> 20) & 0x07ff;
71 unsigned int bufused = 0;
73 id3->frequency = sfreqs_sv7[(header[2] >> 16) & 0x0003];
74 samples = (uint64_t)header[1]*1152; /* 1152 is mpc frame size */
75 if (gapless)
76 samples -= 1152 - last_frame_samples;
77 else
78 samples -= 481; /* Musepack subband synth filter delay */
80 bufused = set_replaygain(id3, false, header[3], bufused);
81 bufused = set_replaygain(id3, true, header[4], bufused);
83 } else {
84 header[0] = letoh32(header[0]);
85 unsigned int streamversion = (header[0] >> 11) & 0x03FF;
86 if (streamversion != 4 && streamversion != 5 && streamversion != 6)
87 return false;
88 id3->frequency = 44100;
89 id3->track_gain = 0;
90 id3->track_peak = 0;
91 id3->album_gain = 0;
92 id3->album_peak = 0;
94 if (streamversion >= 5)
95 samples = (uint64_t)header[1]*1152; // 32 bit
96 else
97 samples = (uint64_t)(header[1] >> 16)*1152; // 16 bit
99 samples -= 576;
100 if (streamversion < 6)
101 samples -= 1152;
104 id3->vbr = true;
105 /* Estimate bitrate, we should probably subtract the various header sizes
106 here for super-accurate results */
107 id3->length = ((int64_t) samples * 1000) / id3->frequency;
109 if (id3->length <= 0)
111 logf("mpc length invalid!");
112 return false;
115 id3->filesize = filesize(fd);
116 id3->bitrate = id3->filesize * 8 / id3->length;
117 return true;