Rva2.cxx 3.77 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
21
#include "Rva2.hxx"
22
#include "ReplayGainInfo.hxx"
23

24 25
#include <id3tag.h>

26
#include <stdint.h>
27
#include <string.h>
28

29 30 31 32 33 34 35 36 37 38
enum class Rva2Channel : uint8_t {
	OTHER = 0x00,
	MASTER_VOLUME = 0x01,
	FRONT_RIGHT = 0x02,
	FRONT_LEFT = 0x03,
	BACK_RIGHT = 0x04,
	BACK_LEFT = 0x05,
	FRONT_CENTRE = 0x06,
	BACK_CENTRE = 0x07,
	SUBWOOFER = 0x08
39 40
};

41
struct Rva2Data {
42
	Rva2Channel type;
43 44 45 46 47
	uint8_t volume_adjustment[2];
	uint8_t peak_bits;
};

static inline id3_length_t
48
rva2_peak_bytes(const Rva2Data &data)
49
{
50
	return (data.peak_bits + 7) / 8;
51 52 53
}

static inline int
54
rva2_fixed_volume_adjustment(const Rva2Data &data)
55 56
{
	signed int voladj_fixed;
57 58
	voladj_fixed = (data.volume_adjustment[0] << 8) |
		data.volume_adjustment[1];
59 60 61 62 63
	voladj_fixed |= -(voladj_fixed & 0x8000);
	return voladj_fixed;
}

static inline float
64
rva2_float_volume_adjustment(const Rva2Data &data)
65 66 67 68 69 70 71 72 73 74
{
	/*
	 * "The volume adjustment is encoded as a fixed point decibel
	 * value, 16 bit signed integer representing (adjustment*512),
	 * giving +/- 64 dB with a precision of 0.001953125 dB."
	 */

	return (float)rva2_fixed_volume_adjustment(data) / (float)512;
}

75
static inline bool
76
rva2_apply_data(ReplayGainInfo &rgi,
77
		const Rva2Data &data, const id3_latin1_t *id)
78
{
79
	if (data.type != Rva2Channel::MASTER_VOLUME)
80 81 82 83
		return false;

	float volume_adjustment = rva2_float_volume_adjustment(data);

84
	if (strcmp((const char *)id, "album") == 0)  {
85
		rgi.album.gain = volume_adjustment;
86
	} else if (strcmp((const char *)id, "track") == 0) {
87
		rgi.track.gain = volume_adjustment;
88
	} else {
89 90
		rgi.album.gain = volume_adjustment;
		rgi.track.gain = volume_adjustment;
91
	}
92 93 94 95

	return true;
}

96
static bool
97
rva2_apply_frame(ReplayGainInfo &replay_gain_info,
98
		 const struct id3_frame *frame)
99
{
100
	const id3_latin1_t *id = id3_field_getlatin1(id3_frame_field(frame, 0));
101
	id3_length_t length;
102 103
	const id3_byte_t *data =
		id3_field_getbinarydata(id3_frame_field(frame, 1), &length);
104

105
	if (id == nullptr || data == nullptr)
106 107 108 109 110 111 112 113 114 115 116 117 118 119
		return false;

	/*
	 * "The 'identification' string is used to identify the
	 * situation and/or device where this adjustment should apply.
	 * The following is then repeated for every channel
	 *
	 *   Type of channel         $xx
	 *   Volume adjustment       $xx xx
	 *   Bits representing peak  $xx
	 *   Peak volume             $xx (xx ...)"
	 */

	while (length >= 4) {
120
		const Rva2Data &d = *(const Rva2Data *)data;
121
		unsigned int peak_bytes = rva2_peak_bytes(d);
122 123 124
		if (4 + peak_bytes > length)
			break;

125
		if (rva2_apply_data(replay_gain_info, d, id))
126 127 128 129 130 131 132 133
			return true;

		data   += 4 + peak_bytes;
		length -= 4 + peak_bytes;
	}

	return false;
}
134 135

bool
136
tag_rva2_parse(struct id3_tag *tag, ReplayGainInfo &replay_gain_info)
137
{
138 139 140 141 142 143
	bool found = false;

	/* Loop through all RVA2 frames as some programs (e.g. mp3gain) store
	   track and album gain in separate tags */
	const struct id3_frame *frame;
	for (unsigned i = 0;
144
	     (frame = id3_tag_findframe(tag, "RVA2", i)) != nullptr;
145 146 147 148 149
	     ++i)
		if (rva2_apply_frame(replay_gain_info, frame))
			found = true;

	return found;
150
}