d4c8f0666d2ce45eee2db487cd80e6ac2033fb4a
[platform/upstream/flac.git] / src / utils / flacdiff / main.cpp
1 /* flacdiff - Displays where two FLAC streams differ
2  * Copyright (C) 2007,2008,2009  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #include <string.h>
25 #include "FLAC++/decoder.h"
26 #include "share/compat.h"
27
28 #ifdef _MSC_VER
29 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
30 #pragma warning ( disable : 4800 )
31 #endif
32
33 class AutoFILE {
34 protected:
35         ::FILE *f_;
36 public:
37         inline AutoFILE(const char *path, const char *mode): f_(::fopen(path, mode)) { }
38         inline virtual ~AutoFILE() { if (f_) (void)::fclose(f_); }
39
40         inline operator bool() const { return 0 != f_; }
41         inline operator const ::FILE *() const { return f_; }
42         inline operator ::FILE *() { return f_; }
43 private:
44         AutoFILE();
45         AutoFILE(const AutoFILE &);
46         void operator=(const AutoFILE &);
47 };
48
49 class Decoder: public FLAC::Decoder::Stream {
50 public:
51         Decoder(AutoFILE &f, FLAC__off_t tgt): tgtpos_((FLAC__uint64)tgt), curpos_(0), go_(true), err_(false), frame_(), f_(f) { memset(&frame_, 0, sizeof(::FLAC__Frame)); }
52         FLAC__uint64 tgtpos_, curpos_;
53         bool go_, err_;
54         ::FLAC__Frame frame_;
55 protected:
56         AutoFILE &f_;
57         // from FLAC::Decoder::Stream
58         virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes)
59         {
60                 *bytes = fread(buffer, 1, *bytes, f_);
61                 if(ferror((FILE*)f_))
62                         return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
63                 else if(*bytes == 0 && feof((FILE*)f_))
64                         return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
65                 else
66                         return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
67         }
68
69         virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset)
70         {
71                 FLAC__off_t off = ftello(f_);
72                 if(off < 0)
73                         return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
74                 *absolute_byte_offset = off;
75                 return ::FLAC__STREAM_DECODER_TELL_STATUS_OK;
76         }
77
78         virtual bool eof_callback()
79         {
80                 return (bool)feof((FILE*)f_);
81         }
82
83         virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const /*buffer*/[])
84         {
85                 FLAC__uint64 pos;
86                 if(!get_decode_position(&pos)) {
87                         go_ = false;
88                         err_ = true;
89                         return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
90                 }
91                 if(pos > tgtpos_) {
92                         go_ = false;
93                         frame_ = *frame;
94                 }
95                 else
96                         curpos_ = pos;
97                 return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
98         }
99
100         virtual void error_callback(::FLAC__StreamDecoderErrorStatus status)
101         {
102                 fprintf(stderr, "got error %d:%s\n", status, ::FLAC__StreamDecoderErrorStatusString[status]);
103                 go_ = false;
104                 err_ = true;
105         }
106 };
107
108 static bool show_diff(AutoFILE &f1, AutoFILE &f2, FLAC__off_t off)
109 {
110         Decoder d1(f1, off), d2(f2, off);
111         if(!d1) {
112                 fprintf(stderr, "ERROR: setting up decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
113                 return false;
114         }
115         if(!d2) {
116                 fprintf(stderr, "ERROR: setting up decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
117                 return false;
118         }
119         ::FLAC__StreamDecoderInitStatus is;
120         if((is = d1.init()) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
121                 fprintf(stderr, "ERROR: initializing decoder1, status=%s state=%s\n", FLAC__StreamDecoderInitStatusString[is], d1.get_state().resolved_as_cstring(d1));
122                 return false;
123         }
124         if((is = d2.init()) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
125                 fprintf(stderr, "ERROR: initializing decoder2, status=%s state=%s\n", FLAC__StreamDecoderInitStatusString[is], d2.get_state().resolved_as_cstring(d2));
126                 return false;
127         }
128         if(!d1.process_until_end_of_metadata()) {
129                 fprintf(stderr, "ERROR: skipping metadata in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
130                 return false;
131         }
132         if(!d2.process_until_end_of_metadata()) {
133                 fprintf(stderr, "ERROR: skipping metadata in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
134                 return false;
135         }
136         while(d1.go_ && d2.go_) {
137                 if(!d1.process_single()) {
138                         fprintf(stderr, "ERROR: decoding frame in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
139                         return false;
140                 }
141                 if(!d2.process_single()) {
142                         fprintf(stderr, "ERROR: decoding frame in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
143                         return false;
144                 }
145         }
146         if(d1.err_) {
147                 fprintf(stderr, "ERROR: got err_ in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
148                 return false;
149         }
150         if(d2.err_) {
151                 fprintf(stderr, "ERROR: got err_ in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
152                 return false;
153         }
154         if(d1.go_ != d2.go_) {
155                 fprintf(stderr, "ERROR: d1.go_(%s) != d2.go_(%s)\n", d1.go_?"true":"false", d2.go_?"true":"false");
156                 return false;
157         }
158         fprintf(stdout, "pos1 = %" PRIu64 "  blocksize=%u sample#%" PRIu64 " frame#%" PRIu64 "\n", d1.curpos_, d1.frame_.header.blocksize, d1.frame_.header.number.sample_number, d1.frame_.header.number.sample_number / d1.frame_.header.blocksize);
159         fprintf(stdout, "pos2 = %" PRIu64 "  blocksize=%u sample#%" PRIu64 " frame#%" PRIu64 "\n", d2.curpos_, d2.frame_.header.blocksize, d2.frame_.header.number.sample_number, d2.frame_.header.number.sample_number / d2.frame_.header.blocksize);
160
161         return true;
162 }
163
164 static FLAC__off_t get_diff_offset(AutoFILE &f1, AutoFILE &f2)
165 {
166         FLAC__off_t off = 0;
167         while(1) {
168                 if(feof((FILE*)f1) && feof((FILE*)f1)) {
169                         fprintf(stderr, "ERROR: files are identical\n");
170                         return -1;
171                 }
172                 if(feof((FILE*)f1)) {
173                         fprintf(stderr, "ERROR: file1 EOF\n");
174                         return -1;
175                 }
176                 if(feof((FILE*)f2)) {
177                         fprintf(stderr, "ERROR: file2 EOF\n");
178                         return -1;
179                 }
180                 if(fgetc(f1) != fgetc(f2))
181                         return off;
182                 off++;
183         }
184 }
185
186 static bool run(const char *fn1, const char *fn2)
187 {
188         FLAC__off_t off;
189         AutoFILE f1(fn1, "rb"), f2(fn2, "rb");
190
191         if(!f1) {
192                 fprintf(stderr, "ERROR: opening %s for reading\n", fn1);
193                 return false;
194         }
195         if(!f2) {
196                 fprintf(stderr, "ERROR: opening %s for reading\n", fn2);
197                 return false;
198         }
199
200         if((off = get_diff_offset(f1, f2)) < 0)
201                 return false;
202
203         fprintf(stdout, "got diff offset = %" PRId64 "\n", off);
204
205         return show_diff(f1, f2, off);
206 }
207
208 int main(int argc, char *argv[])
209 {
210         const char *usage = "usage: flacdiff flacfile1 flacfile2\n";
211
212         if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
213                 printf(usage);
214                 return 0;
215         }
216         else if(argc != 3) {
217                 fprintf(stderr, usage);
218                 return 255;
219         }
220
221         return run(argv[1], argv[2])? 0 : 1;
222 }