-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.cpp
267 lines (230 loc) · 7.6 KB
/
encode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* Copyright (c) 2011, Markus Peloquin <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <FLAC/format.h>
#if FLACPP_API_VERSION_CURRENT <= 8
# include <cerrno>
#endif
#include <cstdint>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <FLAC++/encoder.h>
#include "encode.hpp"
#include "replaygain_writer.hpp"
namespace {
class Flac_encoder :
public FLAC::Encoder::File,
public flacsplit::Basic_encoder {
public:
struct Flac_encode_error : flacsplit::Encode_error {
Flac_encode_error(const std::string &msg) : _msg(msg) {}
const char *what() const noexcept override {
return _msg.c_str();
}
std::string _msg;
};
Flac_encoder(
FILE *fp,
const flacsplit::Music_info &track,
int64_t total_samples,
int32_t sample_rate);
virtual ~Flac_encoder() {
if (_init)
finish();
}
//! \throw Flac_encode_error
void add_frame(const struct flacsplit::Frame &) override;
bool finish() override {
bool result = FLAC::Encoder::File::finish();
_init = false;
return result;
}
protected:
FLAC__StreamEncoderWriteStatus write_callback(
const FLAC__byte *, size_t, uint32_t, uint32_t) override;
FLAC__StreamEncoderSeekStatus seek_callback(FLAC__uint64) override;
FLAC__StreamEncoderTellStatus tell_callback(FLAC__uint64 *) override;
private:
virtual void set_meta(const flacsplit::Music_info &track) {
set_meta(track, true);
}
void set_meta(const flacsplit::Music_info &, bool);
FLAC__StreamMetadata *cast_metadata(FLAC::Metadata::Prototype &meta) {
return const_cast<FLAC__StreamMetadata *>(
static_cast<const FLAC__StreamMetadata *>(meta));
}
std::unique_ptr<FLAC::Metadata::Padding> _padding;
std::unique_ptr<FLAC::Metadata::SeekTable> _seek_table;
FLAC::Metadata::VorbisComment _tag;
//std::vector<std::shared_ptr<FLAC::Metadata::VorbisComment::Entry>>
// _entries;
FILE *_fp;
bool _init;
};
Flac_encoder::Flac_encoder(FILE *fp, const flacsplit::Music_info &track,
int64_t total_samples, int32_t sample_rate) :
FLAC::Encoder::File(),
Basic_encoder(),
_padding(),
_seek_table(),
_tag(),
_fp(fp),
_init(false)
{
set_compression_level(8);
set_do_exhaustive_model_search(true);
if (total_samples) {
_seek_table.reset(new FLAC::Metadata::SeekTable);
#if FLACPP_API_VERSION_CURRENT <= 8
if (!FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(
cast_metadata(*_seek_table), sample_rate * 10,
total_samples)) {
throw_traced(flacsplit::Unix_error(ENOMEM));
}
#else
_seek_table->template_append_spaced_points_by_samples(
sample_rate * 10, total_samples);
#endif
}
set_meta(track);
}
void
Flac_encoder::add_frame(const struct flacsplit::Frame &frame) {
if (!_init) {
FLAC__StreamEncoderInitStatus status;
set_bits_per_sample(frame.bits_per_sample);
set_channels(frame.channels);
set_sample_rate(frame.rate);
status = init();
if (status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
throw_traced(Flac_encode_error(
FLAC__StreamEncoderInitStatusString[status]
));
}
_init = true;
}
// TODO check that channels/rate/bits_per_sample are unchanged
if (!process(frame.data, frame.samples))
throw_traced(Flac_encode_error(get_state().as_cstring()));
}
void
Flac_encoder::set_meta(const flacsplit::Music_info &track,
bool add_replaygain_padding) {
using FLAC::Metadata::VorbisComment;
const std::string &album = track.album();
const std::string &album_artist = track.album_artist();
const std::string &artist = track.artist();
const std::string &date = track.date();
const std::string &genre = track.genre();
const std::string &title = track.title();
uint8_t tracknum = track.track();
if (!album.empty())
_tag.append_comment(VorbisComment::Entry(
"ALBUM", album.c_str()));
if (!album_artist.empty())
_tag.append_comment(VorbisComment::Entry(
"ALBUM ARTIST", album_artist.c_str()));
if (!artist.empty())
_tag.append_comment(VorbisComment::Entry(
"ARTIST", artist.c_str()));
if (!date.empty())
_tag.append_comment(VorbisComment::Entry(
"DATE", date.c_str()));
if (!genre.empty())
_tag.append_comment(VorbisComment::Entry(
"GENRE", genre.c_str()));
if (!title.empty())
_tag.append_comment(VorbisComment::Entry(
"TITLE", title.c_str()));
if (tracknum) {
std::ostringstream out;
out << static_cast<int>(tracknum);
_tag.append_comment(VorbisComment::Entry(
"TRACKNUMBER", out.str().c_str()));
}
if (add_replaygain_padding) {
// use -10 for gain since this gives the field's maximum
// length
flacsplit::Replaygain_stats basic_gain_stats;
basic_gain_stats.album_gain(-10.0);
basic_gain_stats.album_peak(0.0);
basic_gain_stats.track_gain(-10.0);
basic_gain_stats.track_peak(0.0);
flacsplit::append_replaygain_tags(_tag, basic_gain_stats);
unsigned pad_length = _tag.get_length();
flacsplit::delete_replaygain_tags(_tag);
pad_length -= _tag.get_length();
// we then must subtract 4 from the pad_length to account for
// the padding header
// it is possible that the written gain values are as much as
// four bytes short, requiring anywhere from 0--4 bytes
// padding; the minimum size of padding is 4 bytes (the
// METADATA_BLOCK_HEADER length), so we then add 4 bytes
//pad_length -= 4; pad_length += 4;
if (!_padding)
_padding.reset(new FLAC::Metadata::Padding);
_padding->set_length(pad_length);
}
if (_tag.get_num_comments()) {
FLAC__StreamMetadata *meta[3];
size_t metalen = 0;
if (_seek_table)
meta[metalen++] = cast_metadata(*_seek_table);
meta[metalen++] = cast_metadata(_tag);
if (add_replaygain_padding)
meta[metalen++] = cast_metadata(*_padding);
// using the C-style function to avoid stupid libFLAC++ bug
set_metadata(meta, metalen);
}
}
FLAC__StreamEncoderWriteStatus
Flac_encoder::write_callback(const FLAC__byte *buffer, size_t bytes,
uint32_t /*samples*/, uint32_t /*current_frame*/) {
if (fwrite(buffer, bytes, 1, _fp))
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
}
FLAC__StreamEncoderSeekStatus
Flac_encoder::seek_callback(FLAC__uint64 absolute_byte_offset) {
long off = absolute_byte_offset;
if (static_cast<FLAC__uint64>(off) !=
absolute_byte_offset) {
flacsplit::throw_traced(std::runtime_error("bad offset"));
}
if (fseek(_fp, off, SEEK_SET))
return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
}
FLAC__StreamEncoderTellStatus
Flac_encoder::tell_callback(FLAC__uint64 *absolute_byte_offset) {
long off = ftell(_fp);
if (off < 0)
return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
*absolute_byte_offset = off;
return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
}
} // end anon
flacsplit::Encoder::Encoder(
FILE *fp,
const Music_info &track,
int64_t total_samples,
int32_t sample_rate,
file_format file_format
) {
if (file_format != file_format::FLAC)
throw_traced(Bad_format());
_encoder.reset(new Flac_encoder(
fp, track, total_samples, sample_rate
));
}