1 /* flacdiff - Displays where two FLAC streams differ
2 * Copyright (C) 2007,2008,2009 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "FLAC++/decoder.h"
26 #if defined _MSC_VER || defined __MINGW32__
27 #if _MSC_VER <= 1600 /* @@@ [2G limit] */
34 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
35 #pragma warning ( disable : 4800 )
42 inline AutoFILE(const char *path, const char *mode): f_(::fopen(path, mode)) { }
43 inline virtual ~AutoFILE() { if (f_) (void)::fclose(f_); }
45 inline operator bool() const { return 0 != f_; }
46 inline operator const ::FILE *() const { return f_; }
47 inline operator ::FILE *() { return f_; }
50 AutoFILE(const AutoFILE &);
51 void operator=(const AutoFILE &);
54 class Decoder: public FLAC::Decoder::Stream {
56 Decoder(AutoFILE &f, off_t tgt): tgtpos_((FLAC__uint64)tgt), curpos_(0), go_(true), err_(false), frame_(), f_(f) { memset(&frame_, 0, sizeof(::FLAC__Frame)); }
57 FLAC__uint64 tgtpos_, curpos_;
62 // from FLAC::Decoder::Stream
63 virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes)
65 *bytes = fread(buffer, 1, *bytes, f_);
67 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
68 else if(*bytes == 0 && feof((FILE*)f_))
69 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
71 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
74 virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset)
76 off_t off = ftello(f_);
78 return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
79 *absolute_byte_offset = off;
80 return ::FLAC__STREAM_DECODER_TELL_STATUS_OK;
83 virtual bool eof_callback()
85 return (bool)feof((FILE*)f_);
88 virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const /*buffer*/[])
91 if(!get_decode_position(&pos)) {
94 return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
102 return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
105 virtual void error_callback(::FLAC__StreamDecoderErrorStatus status)
107 fprintf(stderr, "got error %d:%s\n", status, ::FLAC__StreamDecoderErrorStatusString[status]);
113 static bool show_diff(AutoFILE &f1, AutoFILE &f2, off_t off)
115 Decoder d1(f1, off), d2(f2, off);
117 fprintf(stderr, "ERROR: setting up decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
121 fprintf(stderr, "ERROR: setting up decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
124 ::FLAC__StreamDecoderInitStatus is;
125 if((is = d1.init()) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
126 fprintf(stderr, "ERROR: initializing decoder1, status=%s state=%s\n", FLAC__StreamDecoderInitStatusString[is], d1.get_state().resolved_as_cstring(d1));
129 if((is = d2.init()) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
130 fprintf(stderr, "ERROR: initializing decoder2, status=%s state=%s\n", FLAC__StreamDecoderInitStatusString[is], d2.get_state().resolved_as_cstring(d2));
133 if(!d1.process_until_end_of_metadata()) {
134 fprintf(stderr, "ERROR: skipping metadata in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
137 if(!d2.process_until_end_of_metadata()) {
138 fprintf(stderr, "ERROR: skipping metadata in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
141 while(d1.go_ && d2.go_) {
142 if(!d1.process_single()) {
143 fprintf(stderr, "ERROR: decoding frame in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
146 if(!d2.process_single()) {
147 fprintf(stderr, "ERROR: decoding frame in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
152 fprintf(stderr, "ERROR: got err_ in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
156 fprintf(stderr, "ERROR: got err_ in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
159 if(d1.go_ != d2.go_) {
160 fprintf(stderr, "ERROR: d1.go_(%s) != d2.go_(%s)\n", d1.go_?"true":"false", d2.go_?"true":"false");
163 fprintf(stdout, "pos1 = %llu blocksize=%u sample#%llu frame#%llu\n", d1.curpos_, d1.frame_.header.blocksize, d1.frame_.header.number.sample_number, d1.frame_.header.number.sample_number / d1.frame_.header.blocksize);
164 fprintf(stdout, "pos2 = %llu blocksize=%u sample#%llu frame#%llu\n", d2.curpos_, d2.frame_.header.blocksize, d2.frame_.header.number.sample_number, d2.frame_.header.number.sample_number / d2.frame_.header.blocksize);
169 static off_t get_diff_offset(AutoFILE &f1, AutoFILE &f2)
173 if(feof((FILE*)f1) && feof((FILE*)f1)) {
174 fprintf(stderr, "ERROR: files are identical\n");
177 if(feof((FILE*)f1)) {
178 fprintf(stderr, "ERROR: file1 EOF\n");
181 if(feof((FILE*)f2)) {
182 fprintf(stderr, "ERROR: file2 EOF\n");
185 if(fgetc(f1) != fgetc(f2))
191 static bool run(const char *fn1, const char *fn2)
194 AutoFILE f1(fn1, "rb"), f2(fn2, "rb");
197 fprintf(stderr, "ERROR: opening %s for reading\n", fn1);
201 fprintf(stderr, "ERROR: opening %s for reading\n", fn2);
205 if((off = get_diff_offset(f1, f2)) < 0)
208 fprintf(stdout, "got diff offset = %u\n", (unsigned)off); //@@@ 4G limit (what is % modifier for off_t?)
210 return show_diff(f1, f2, off);
213 int main(int argc, char *argv[])
215 const char *usage = "usage: flacdiff flacfile1 flacfile2\n";
217 if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
222 fprintf(stderr, usage);
226 return run(argv[1], argv[2])? 0 : 1;