1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002 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
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.
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.
30 /* we only use some typedefs, #defines, and enums from here: */
33 static const char *sync_string_ = "fLaC"; /* DUPLICATE:FLAC__STREAM_SYNC_STRING */
34 static const char *metadata_type_string_[] = { /* DUPLICATE:FLAC__MetaDataTypeString */
40 static const unsigned SEEKPOINT_LEN_ = 18; /* DUPLICATE:FLAC__STREAM_METADATA_SEEKPOINT_LENGTH */
42 static int usage(const char *message, ...);
43 static FLAC__bool list(FILE *f, FLAC__bool verbose);
44 static FLAC__uint32 unpack_uint32(FLAC__byte *b, unsigned bytes);
45 static FLAC__uint64 unpack_uint64(FLAC__byte *b, unsigned bytes);
46 static void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent);
48 int main(int argc, char *argv[])
50 int i, first_file, retval = 0;
51 FLAC__bool verbose = false, list_mode = true;
57 for(i = 1; i < argc; i++) {
58 if(argv[i][0] != '-' || argv[i][1] == 0)
60 if(0 == strcmp(argv[i], "-l"))
62 else if(0 == strcmp(argv[i], "-v"))
64 else if(0 == strcmp(argv[i], "-v-"))
67 return usage("ERROR: invalid option '%s'\n", argv[i]);
74 for(first_file = i; i < argc; i++) {
75 FILE *f = fopen(argv[i], "rb");
78 fprintf(stderr, "ERROR opening %s\n", argv[i]);
82 printf("%sfile: %s\n", i==first_file? "" : "\n", argv[i]);
95 int usage(const char *message, ...)
100 va_start(args, message);
102 (void) vfprintf(stderr, message, args);
107 printf("==============================================================================\n");
108 printf("metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
109 printf("Copyright (C) 2001,2002 Josh Coalson\n");
111 printf("This program is free software; you can redistribute it and/or\n");
112 printf("modify it under the terms of the GNU General Public License\n");
113 printf("as published by the Free Software Foundation; either version 2\n");
114 printf("of the License, or (at your option) any later version.\n");
116 printf("This program is distributed in the hope that it will be useful,\n");
117 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
118 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
119 printf("GNU General Public License for more details.\n");
121 printf("You should have received a copy of the GNU General Public License\n");
122 printf("along with this program; if not, write to the Free Software\n");
123 printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
124 printf("==============================================================================\n");
126 printf(" metaflac [options] flacfile [...]\n");
128 printf("options:\n");
129 printf(" -l : list metadata blocks\n");
130 printf(" -v : verbose\n");
132 return message? 1 : 0;
135 FLAC__bool list(FILE *f, FLAC__bool verbose)
137 FLAC__byte buf[65536];
139 FLAC__StreamMetaData metadata;
140 unsigned blocknum = 0, byte_offset = 0, i;
142 /* skip any id3v2 tag */
143 if(fread(buf, 1, 4, f) < 4) {
144 fprintf(stderr, "ERROR: not a FLAC file\n");
147 if(0 == memcmp(buf, "ID3", 3)) {
148 unsigned tag_length = 0;
150 /* skip to the tag length */
151 if(fseek(f, 2, SEEK_CUR) < 0) {
152 fprintf(stderr, "ERROR: bad ID3v2 tag\n");
156 /* read the length */
157 for(i = 0; i < 4; i++) {
158 if(fread(buf, 1, 1, f) < 1 || buf[0] & 0x80) {
159 fprintf(stderr, "ERROR: bad ID3v2 tag\n");
163 tag_length |= (buf[0] & 0x7f);
166 /* skip the rest of the tag */
167 if(fseek(f, tag_length, SEEK_CUR) < 0) {
168 fprintf(stderr, "ERROR: bad ID3v2 tag\n");
172 /* read the stream sync code */
173 if(fread(buf, 1, 4, f) < 4) {
174 fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
179 /* check the stream sync code */
180 if(memcmp(buf, sync_string_, 4)) {
181 fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
186 /* read the metadata blocks */
188 /* read the metadata block header */
189 if(fread(buf, 1, 4, f) < 4) {
190 fprintf(stderr, "ERROR: short count reading metadata block header\n");
193 metadata.is_last = (buf[0] & 0x80)? true:false;
194 metadata.type = (FLAC__MetaDataType)(buf[0] & 0x7f);
195 metadata.length = unpack_uint32(buf+1, 3);
198 printf("METADATA block #%u\n", blocknum);
199 printf(" byte offset: %u\n", byte_offset);
200 printf(" type: %u (%s)\n", (unsigned)metadata.type, metadata.type<=FLAC__METADATA_TYPE_SEEKTABLE? metadata_type_string_[metadata.type] : "UNKNOWN");
201 printf(" is last: %s\n", metadata.is_last? "true":"false");
202 printf(" length: %u\n", metadata.length);
204 if(metadata.length > sizeof(buf)) {
205 printf(" SKIPPING large block\n");
206 if(fseek(f, metadata.length, SEEK_CUR) < 0) {
207 fprintf(stderr, "ERROR: short count skipping metadata block data\n");
213 /* read the metadata block data */
214 if(fread(buf, 1, metadata.length, f) < metadata.length) {
215 fprintf(stderr, "ERROR: short count reading metadata block data\n");
218 switch(metadata.type) {
219 case FLAC__METADATA_TYPE_STREAMINFO:
221 metadata.data.stream_info.min_blocksize = unpack_uint32(b, 2); b += 2;
222 metadata.data.stream_info.max_blocksize = unpack_uint32(b, 2); b += 2;
223 metadata.data.stream_info.min_framesize = unpack_uint32(b, 3); b += 3;
224 metadata.data.stream_info.max_framesize = unpack_uint32(b, 3); b += 3;
225 metadata.data.stream_info.sample_rate = (unpack_uint32(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
226 metadata.data.stream_info.channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
227 metadata.data.stream_info.bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 1) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
228 metadata.data.stream_info.total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64(b+4, 4);
229 memcpy(metadata.data.stream_info.md5sum, b+8, 16);
231 case FLAC__METADATA_TYPE_PADDING:
236 case FLAC__METADATA_TYPE_APPLICATION:
237 memcpy(metadata.data.application.id, buf, 4);
238 metadata.data.application.data = buf+4;
240 case FLAC__METADATA_TYPE_SEEKTABLE:
241 metadata.data.seek_table.num_points = metadata.length / SEEKPOINT_LEN_;
242 b = buf; /* we leave the points in buf for printing later */
245 printf("SKIPPING block of unknown type\n");
250 switch(metadata.type) {
251 case FLAC__METADATA_TYPE_STREAMINFO:
252 printf(" minumum blocksize: %u samples\n", metadata.data.stream_info.min_blocksize);
253 printf(" maximum blocksize: %u samples\n", metadata.data.stream_info.max_blocksize);
254 printf(" minimum framesize: %u bytes\n", metadata.data.stream_info.min_framesize);
255 printf(" maximum framesize: %u bytes\n", metadata.data.stream_info.max_framesize);
256 printf(" sample_rate: %u Hz\n", metadata.data.stream_info.sample_rate);
257 printf(" channels: %u\n", metadata.data.stream_info.channels);
258 printf(" bits-per-sample: %u\n", metadata.data.stream_info.bits_per_sample);
259 printf(" total samples: %llu\n", metadata.data.stream_info.total_samples);
260 printf(" MD5 signature: ");
261 for(i = 0; i < 16; i++)
262 printf("%02x", metadata.data.stream_info.md5sum[i]);
265 case FLAC__METADATA_TYPE_PADDING:
267 printf(" pad contents:\n");
268 hexdump(buf, metadata.length, " ");
271 case FLAC__METADATA_TYPE_APPLICATION:
272 printf(" application ID: ");
273 for(i = 0; i < 4; i++)
274 printf("%02x", metadata.data.application.id[i]);
277 printf(" data contents:\n");
278 hexdump(metadata.data.application.data, metadata.length, " ");
281 case FLAC__METADATA_TYPE_SEEKTABLE:
282 printf(" seek points: %u\n", metadata.data.seek_table.num_points);
284 for(i = 0; i < metadata.data.seek_table.num_points; i++, b += SEEKPOINT_LEN_)
285 printf(" point %d: sample_number=%llu, stream_offset=%llu, frame_samples=%u\n", i, unpack_uint64(b, 8), unpack_uint64(b+8, 8), unpack_uint32(b+16, 2));
293 byte_offset += (4 + metadata.length);
294 } while (!metadata.is_last);
299 FLAC__uint32 unpack_uint32(FLAC__byte *b, unsigned bytes)
301 FLAC__uint32 ret = 0;
304 for(i = 0; i < bytes; i++)
305 ret = (ret << 8) | (FLAC__uint32)(*b++);
310 FLAC__uint64 unpack_uint64(FLAC__byte *b, unsigned bytes)
312 FLAC__uint64 ret = 0;
315 for(i = 0; i < bytes; i++)
316 ret = (ret << 8) | (FLAC__uint64)(*b++);
321 void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent)
323 unsigned i, left = bytes;
324 const FLAC__byte *b = buf;
326 for(i = 0; i < bytes; i += 16) {
328 "%02X %02X %02X %02X %02X %02X %02X %02X "
329 "%02X %02X %02X %02X %02X %02X %02X %02X "
330 "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
332 left > 0? (unsigned char)b[ 0] : 0,
333 left > 1? (unsigned char)b[ 1] : 0,
334 left > 2? (unsigned char)b[ 2] : 0,
335 left > 3? (unsigned char)b[ 3] : 0,
336 left > 4? (unsigned char)b[ 4] : 0,
337 left > 5? (unsigned char)b[ 5] : 0,
338 left > 6? (unsigned char)b[ 6] : 0,
339 left > 7? (unsigned char)b[ 7] : 0,
340 left > 8? (unsigned char)b[ 8] : 0,
341 left > 9? (unsigned char)b[ 9] : 0,
342 left > 10? (unsigned char)b[10] : 0,
343 left > 11? (unsigned char)b[11] : 0,
344 left > 12? (unsigned char)b[12] : 0,
345 left > 13? (unsigned char)b[13] : 0,
346 left > 14? (unsigned char)b[14] : 0,
347 left > 15? (unsigned char)b[15] : 0,
348 (left > 0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
349 (left > 1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
350 (left > 2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
351 (left > 3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
352 (left > 4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
353 (left > 5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
354 (left > 6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
355 (left > 7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
356 (left > 8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
357 (left > 9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
358 (left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
359 (left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
360 (left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
361 (left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
362 (left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
363 (left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '