5e7c49d93450a980fba9943b4188311fbee3215f
[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 #if defined _MSC_VER || defined __MINGW32__
27 #if _MSC_VER <= 1600 /* @@@ [2G limit] */
28 #define fseeko fseek
29 #define ftello ftell
30 #endif
31 #endif
32
33 #ifdef _MSC_VER
34 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
35 #pragma warning ( disable : 4800 )
36 #endif
37
38 class AutoFILE {
39 protected:
40         ::FILE *f_;
41 public:
42         inline AutoFILE(const char *path, const char *mode): f_(::fopen(path, mode)) { }
43         inline virtual ~AutoFILE() { if (f_) (void)::fclose(f_); }
44
45         inline operator bool() const { return 0 != f_; }
46         inline operator const ::FILE *() const { return f_; }
47         inline operator ::FILE *() { return f_; }
48 private:
49         AutoFILE();
50         AutoFILE(const AutoFILE &);
51         void operator=(const AutoFILE &);
52 };
53
54 class Decoder: public FLAC::Decoder::Stream {
55 public:
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_;
58         bool go_, err_;
59         ::FLAC__Frame frame_;
60 protected:
61         AutoFILE &f_;
62         // from FLAC::Decoder::Stream
63         virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes)
64         {
65                 *bytes = fread(buffer, 1, *bytes, f_);
66                 if(ferror((FILE*)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;
70                 else
71                         return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
72         }
73
74         virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset)
75         {
76                 off_t off = ftello(f_);
77                 if(off < 0)
78                         return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
79                 *absolute_byte_offset = off;
80                 return ::FLAC__STREAM_DECODER_TELL_STATUS_OK;
81         }
82
83         virtual bool eof_callback()
84         {
85                 return (bool)feof((FILE*)f_);
86         }
87
88         virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const /*buffer*/[])
89         {
90                 FLAC__uint64 pos;
91                 if(!get_decode_position(&pos)) {
92                         go_ = false;
93                         err_ = true;
94                         return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
95                 }
96                 if(pos > tgtpos_) {
97                         go_ = false;
98                         frame_ = *frame;
99                 }
100                 else
101                         curpos_ = pos;
102                 return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
103         }
104
105         virtual void error_callback(::FLAC__StreamDecoderErrorStatus status)
106         {
107                 fprintf(stderr, "got error %d:%s\n", status, ::FLAC__StreamDecoderErrorStatusString[status]);
108                 go_ = false;
109                 err_ = true;
110         }
111 };
112
113 static bool show_diff(AutoFILE &f1, AutoFILE &f2, off_t off)
114 {
115         Decoder d1(f1, off), d2(f2, off);
116         if(!d1) {
117                 fprintf(stderr, "ERROR: setting up decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
118                 return false;
119         }
120         if(!d2) {
121                 fprintf(stderr, "ERROR: setting up decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
122                 return false;
123         }
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));
127                 return false;
128         }
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));
131                 return false;
132         }
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));
135                 return false;
136         }
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));
139                 return false;
140         }
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));
144                         return false;
145                 }
146                 if(!d2.process_single()) {
147                         fprintf(stderr, "ERROR: decoding frame in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
148                         return false;
149                 }
150         }
151         if(d1.err_) {
152                 fprintf(stderr, "ERROR: got err_ in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
153                 return false;
154         }
155         if(d2.err_) {
156                 fprintf(stderr, "ERROR: got err_ in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
157                 return false;
158         }
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");
161                 return false;
162         }
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);
165
166         return true;
167 }
168
169 static off_t get_diff_offset(AutoFILE &f1, AutoFILE &f2)
170 {
171         off_t off = 0;
172         while(1) {
173                 if(feof((FILE*)f1) && feof((FILE*)f1)) {
174                         fprintf(stderr, "ERROR: files are identical\n");
175                         return -1;
176                 }
177                 if(feof((FILE*)f1)) {
178                         fprintf(stderr, "ERROR: file1 EOF\n");
179                         return -1;
180                 }
181                 if(feof((FILE*)f2)) {
182                         fprintf(stderr, "ERROR: file2 EOF\n");
183                         return -1;
184                 }
185                 if(fgetc(f1) != fgetc(f2))
186                         return off;
187                 off++;
188         }
189 }
190
191 static bool run(const char *fn1, const char *fn2)
192 {
193         off_t off;
194         AutoFILE f1(fn1, "rb"), f2(fn2, "rb");
195
196         if(!f1) {
197                 fprintf(stderr, "ERROR: opening %s for reading\n", fn1);
198                 return false;
199         }
200         if(!f2) {
201                 fprintf(stderr, "ERROR: opening %s for reading\n", fn2);
202                 return false;
203         }
204
205         if((off = get_diff_offset(f1, f2)) < 0)
206                 return false;
207
208         fprintf(stdout, "got diff offset = %u\n", (unsigned)off); //@@@ 4G limit (what is % modifier for off_t?)
209
210         return show_diff(f1, f2, off);
211 }
212
213 int main(int argc, char *argv[])
214 {
215         const char *usage = "usage: flacdiff flacfile1 flacfile2\n";
216
217         if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
218                 printf(usage);
219                 return 0;
220         }
221         else if(argc != 3) {
222                 fprintf(stderr, usage);
223                 return 255;
224         }
225
226         return run(argv[1], argv[2])? 0 : 1;
227 }