add SEEKTABLE support
[platform/upstream/flac.git] / src / metaflac / main.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001  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
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 /*
20  * WATCHOUT - this is meant to be very lightweight an not even dependent
21  * on libFLAC, so there are a couple places where FLAC__* variables are
22  * duplicated here.  Look for 'DUPLICATE:' in comments.
23  */
24
25 #include <assert.h>
26 #include <ctype.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "FLAC/all.h"
32
33 static const char *sync_string_ = "fLaC"; /* DUPLICATE:FLAC__STREAM_SYNC_STRING */
34 static const char *metadata_type_string_[] = { /* DUPLICATE:FLAC__MetaDataTypeString */
35         "STREAMINFO",
36         "PADDING",
37         "APPLICATION",
38         "SEEKTABLE"
39 };
40 static const unsigned SEEKPOINT_LEN_ = 18; /* DUPLICATE:FLAC__STREAM_METADATA_SEEKPOINT_LEN */
41
42 static int usage(const char *message, ...);
43 static bool list(FILE *f, bool verbose);
44 static uint32 unpack_uint32(byte *b, unsigned bytes);
45 static uint64 unpack_uint64(byte *b, unsigned bytes);
46 static void hexdump(const byte *buf, unsigned bytes);
47
48 int main(int argc, char *argv[])
49 {
50         int i;
51         bool verbose = false, list_mode = true;
52
53         if(argc <= 1)
54                 return usage(0);
55
56         /* get the options */
57         for(i = 1; i < argc; i++) {
58                 if(argv[i][0] != '-' || argv[i][1] == 0)
59                         break;
60                 if(0 == strcmp(argv[i], "-l"))
61                         list_mode = true;
62                 else if(0 == strcmp(argv[i], "-v"))
63                         verbose = true;
64                 else if(0 == strcmp(argv[i], "-v-"))
65                         verbose = false;
66                 else {
67                         return usage("ERROR: invalid option '%s'\n", argv[i]);
68                 }
69         }
70         if(i + (list_mode? 1:2) != argc)
71                 return usage("ERROR: invalid arguments (more/less than %d filename%s?)\n", (list_mode? 1:2), (list_mode? "":"s"));
72
73         if(list_mode) {
74                 FILE *f = fopen(argv[i], "r");
75
76                 if(0 == f) {
77                         fprintf(stderr, "ERROR opening %s\n", argv[i]);
78                         return 1;
79                 }
80
81                 if(!list(f, verbose))
82                         return 1;
83
84                 fclose(f);
85         }
86
87         return 0;
88 }
89
90 int usage(const char *message, ...)
91 {
92         va_list args;
93
94         if(message) {
95                 va_start(args, message);
96
97                 (void) vfprintf(stderr, message, args);
98
99                 va_end(args);
100
101         }
102         printf("==============================================================================\n");
103         printf("metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
104         printf("Copyright (C) 2001  Josh Coalson\n");
105         printf("\n");
106         printf("This program is free software; you can redistribute it and/or\n");
107         printf("modify it under the terms of the GNU General Public License\n");
108         printf("as published by the Free Software Foundation; either version 2\n");
109         printf("of the License, or (at your option) any later version.\n");
110         printf("\n");
111         printf("This program is distributed in the hope that it will be useful,\n");
112         printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
113         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
114         printf("GNU General Public License for more details.\n");
115         printf("\n");
116         printf("You should have received a copy of the GNU General Public License\n");
117         printf("along with this program; if not, write to the Free Software\n");
118         printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
119         printf("==============================================================================\n");
120         printf("Usage:\n");
121         printf("  metaflac [options] infile [outfile]\n");
122         printf("\n");
123         printf("options:\n");
124         printf("  -l : list metadata blocks\n");
125         printf("  -v : verbose\n");
126
127         return 1;
128 }
129
130 bool list(FILE *f, bool verbose)
131 {
132         byte buf[65536];
133         byte *b;
134         FLAC__StreamMetaData metadata;
135         unsigned blocknum = 0, byte_offset = 0, i;
136
137         /* read the stream sync code */
138         if(fread(buf, 1, 4, f) < 4 || memcmp(buf, sync_string_, 4)) {
139                 fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
140                 return false;
141         }
142         byte_offset += 4;
143
144         /* read the metadata blocks */
145         do {
146                 /* read the metadata block header */
147                 if(fread(buf, 1, 4, f) < 4) {
148                         fprintf(stderr, "ERROR: short count reading metadata block header\n");
149                         return false;
150                 }
151                 metadata.is_last = (buf[0] & 0x80)? true:false;
152                 metadata.type = (FLAC__MetaDataType)(buf[0] & 0x7f);
153                 metadata.length = unpack_uint32(buf+1, 3);
154
155                 /* print header */
156                 printf("METADATA block #%u\n", blocknum);
157                 printf("byte offset: %u\n", byte_offset);
158                 printf("type: %u (%s)\n", (unsigned)metadata.type, metadata.type<=FLAC__METADATA_TYPE_APPLICATION? metadata_type_string_[metadata.type] : "UNKNOWN");
159                 printf("is last: %s\n", metadata.is_last? "true":"false");
160                 printf("length: %u\n", metadata.length);
161
162                 if(metadata.length > sizeof(buf)) {
163                         printf("SKIPPING large block\n\n");
164                         continue;
165                 }
166
167                 /* read the metadata block data */
168                 if(fread(buf, 1, metadata.length, f) < metadata.length) {
169                         fprintf(stderr, "ERROR: short count reading metadata block data\n\n");
170                         return false;
171                 }
172                 switch(metadata.type) {
173                         case FLAC__METADATA_TYPE_STREAMINFO:
174                                 b = buf;
175                                 metadata.data.stream_info.min_blocksize = unpack_uint32(b, 2); b += 2;
176                                 metadata.data.stream_info.max_blocksize = unpack_uint32(b, 2); b += 2;
177                                 metadata.data.stream_info.min_framesize = unpack_uint32(b, 3); b += 3;
178                                 metadata.data.stream_info.max_framesize = unpack_uint32(b, 3); b += 3;
179                                 metadata.data.stream_info.sample_rate = (unpack_uint32(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
180                                 metadata.data.stream_info.channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
181                                 metadata.data.stream_info.bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 1) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
182                                 metadata.data.stream_info.total_samples = (((uint64)(b[3] & 0x0f)) << 32) | unpack_uint64(b+4, 4);
183                                 memcpy(metadata.data.stream_info.md5sum, b+8, 16);
184                                 break;
185                         case FLAC__METADATA_TYPE_PADDING:
186                                 if(verbose) {
187                                         /* dump contents */
188                                 }
189                                 break;
190                         case FLAC__METADATA_TYPE_APPLICATION:
191                                 memcpy(buf, metadata.data.application.id, 4);
192                                 metadata.data.application.data = buf+4;
193                                 break;
194                         case FLAC__METADATA_TYPE_SEEKTABLE:
195                                 metadata.data.seek_table.num_points = metadata.length / SEEKPOINT_LEN_;
196                                 b = buf+4; /* we leave the points in buf for printing later */
197                                 break;
198                         default:
199                                 printf("SKIPPING block of unknown type\n\n");
200                                 continue;
201                 }
202
203                 /* print data */
204                 switch(metadata.type) {
205                         case FLAC__METADATA_TYPE_STREAMINFO:
206                                 printf("minumum blocksize: %u samples\n", metadata.data.stream_info.min_blocksize);
207                                 printf("maxumum blocksize: %u samples\n", metadata.data.stream_info.max_blocksize);
208                                 printf("minimum framesize: %u bytes\n", metadata.data.stream_info.min_framesize);
209                                 printf("maximum framesize: %u bytes\n", metadata.data.stream_info.max_framesize);
210                                 printf("sample_rate: %u Hz\n", metadata.data.stream_info.sample_rate);
211                                 printf("channels: %u\n", metadata.data.stream_info.channels);
212                                 printf("bits-per-sample: %u\n", metadata.data.stream_info.bits_per_sample);
213                                 printf("total samples: %llu\n", metadata.data.stream_info.total_samples);
214                                 printf("MD5 signature: ");
215                                 for(i = 0; i < 16; i++)
216                                         printf("%02x", metadata.data.stream_info.md5sum[i]);
217                                 printf("\n");
218                                 break;
219                         case FLAC__METADATA_TYPE_PADDING:
220                                 if(verbose) {
221                                         printf("pad contents:\n");
222                                         hexdump(buf, metadata.length);
223                                 }
224                                 break;
225                         case FLAC__METADATA_TYPE_APPLICATION:
226                                 printf("Application ID: ");
227                                 for(i = 0; i < 4; i++)
228                                         printf("%02x", metadata.data.application.id[i]);
229                                 printf("\n");
230                                 if(verbose) {
231                                         printf("data contents:\n");
232                                         hexdump(metadata.data.application.data, metadata.length);
233                                 }
234                                 break;
235                         case FLAC__METADATA_TYPE_SEEKTABLE:
236                                 printf("seek points: %u\n", metadata.data.seek_table.num_points);
237                                 for(i = 0; i < metadata.data.seek_table.num_points; i++, b += SEEKPOINT_LEN_) {
238                                         printf("\tpoint %d: sample_number=%llu, stream_offset=%llu, block_offset=%u\n", i, unpack_uint64(b, 8), unpack_uint64(b+8, 8), unpack_uint32(b+16, 2));
239                                 }
240                                 break;
241                         default:
242                                 assert(0);
243                 }
244
245                 if(!metadata.is_last)
246                         printf("\n");
247
248                 blocknum++;
249                 byte_offset += (4 + metadata.length);
250         } while (!metadata.is_last);
251
252         return true;
253 }
254
255 uint32 unpack_uint32(byte *b, unsigned bytes)
256 {
257         uint32 ret = 0;
258         unsigned i;
259
260         for(i = 0; i < bytes; i++)
261                 ret = (ret << 8) | (uint32)(*b++);
262
263         return ret;
264 }
265
266 uint64 unpack_uint64(byte *b, unsigned bytes)
267 {
268         uint64 ret = 0;
269         unsigned i;
270
271         for(i = 0; i < bytes; i++)
272                 ret = (ret << 8) | (uint64)(*b++);
273
274         return ret;
275 }
276
277 void hexdump(const byte *buf, unsigned bytes)
278 {
279         unsigned i, left = bytes;
280         const byte *b = buf;
281
282         for(i = 0; i < bytes; i += 16) {
283                 printf("%08X: "
284                         "%02X %02X %02X %02X %02X %02X %02X %02X "
285                         "%02X %02X %02X %02X %02X %02X %02X %02X "
286                         "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
287                         i,
288                         left >  0? (unsigned char)b[ 0] : 0,
289                         left >  1? (unsigned char)b[ 1] : 0,
290                         left >  2? (unsigned char)b[ 2] : 0,
291                         left >  3? (unsigned char)b[ 3] : 0,
292                         left >  4? (unsigned char)b[ 4] : 0,
293                         left >  5? (unsigned char)b[ 5] : 0,
294                         left >  6? (unsigned char)b[ 6] : 0,
295                         left >  7? (unsigned char)b[ 7] : 0,
296                         left >  8? (unsigned char)b[ 8] : 0,
297                         left >  9? (unsigned char)b[ 9] : 0,
298                         left > 10? (unsigned char)b[10] : 0,
299                         left > 11? (unsigned char)b[11] : 0,
300                         left > 12? (unsigned char)b[12] : 0,
301                         left > 13? (unsigned char)b[13] : 0,
302                         left > 14? (unsigned char)b[14] : 0,
303                         left > 15? (unsigned char)b[15] : 0,
304                         (left >  0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
305                         (left >  1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
306                         (left >  2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
307                         (left >  3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
308                         (left >  4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
309                         (left >  5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
310                         (left >  6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
311                         (left >  7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
312                         (left >  8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
313                         (left >  9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
314                         (left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
315                         (left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
316                         (left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
317                         (left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
318                         (left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
319                         (left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '
320                 );
321                 left -= 16;
322                 b += 16;
323    }
324 }