1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001 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.
32 static const char *sync_string_ = "fLaC"; /* DUPLICATE:FLAC__STREAM_SYNC_STRING */
33 static const char *metadata_type_string_[] = { /* DUPLICATE:FLAC__MetaDataTypeString */
39 static const unsigned SEEKPOINT_LEN_ = 18; /* DUPLICATE:FLAC__STREAM_METADATA_SEEKPOINT_LEN */
41 static int usage(const char *message, ...);
42 static FLAC__bool list(FILE *f, FLAC__bool verbose);
43 static FLAC__uint32 unpack_uint32(FLAC__byte *b, unsigned bytes);
44 static FLAC__uint64 unpack_uint64(FLAC__byte *b, unsigned bytes);
45 static void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent);
47 int main(int argc, char *argv[])
49 int i, first_file, retval = 0;
50 FLAC__bool verbose = false, list_mode = true;
56 for(i = 1; i < argc; i++) {
57 if(argv[i][0] != '-' || argv[i][1] == 0)
59 if(0 == strcmp(argv[i], "-l"))
61 else if(0 == strcmp(argv[i], "-v"))
63 else if(0 == strcmp(argv[i], "-v-"))
66 return usage("ERROR: invalid option '%s'\n", argv[i]);
73 for(first_file = i; i < argc; i++) {
74 FILE *f = fopen(argv[i], "r");
77 fprintf(stderr, "ERROR opening %s\n", argv[i]);
81 printf("%sfile: %s\n", i==first_file? "" : "\n", argv[i]);
94 int usage(const char *message, ...)
99 va_start(args, message);
101 (void) vfprintf(stderr, message, args);
106 printf("==============================================================================\n");
107 printf("metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
108 printf("Copyright (C) 2001 Josh Coalson\n");
110 printf("This program is free software; you can redistribute it and/or\n");
111 printf("modify it under the terms of the GNU General Public License\n");
112 printf("as published by the Free Software Foundation; either version 2\n");
113 printf("of the License, or (at your option) any later version.\n");
115 printf("This program is distributed in the hope that it will be useful,\n");
116 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
117 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
118 printf("GNU General Public License for more details.\n");
120 printf("You should have received a copy of the GNU General Public License\n");
121 printf("along with this program; if not, write to the Free Software\n");
122 printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
123 printf("==============================================================================\n");
125 printf(" metaflac [options] flacfile [...]\n");
127 printf("options:\n");
128 printf(" -l : list metadata blocks\n");
129 printf(" -v : verbose\n");
131 return message? 1 : 0;
134 FLAC__bool list(FILE *f, FLAC__bool verbose)
136 FLAC__byte buf[65536];
138 FLAC__StreamMetaData metadata;
139 unsigned blocknum = 0, byte_offset = 0, i;
141 /* read the stream sync code */
142 if(fread(buf, 1, 4, f) < 4 || memcmp(buf, sync_string_, 4)) {
143 fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
148 /* read the metadata blocks */
150 /* read the metadata block header */
151 if(fread(buf, 1, 4, f) < 4) {
152 fprintf(stderr, "ERROR: short count reading metadata block header\n");
155 metadata.is_last = (buf[0] & 0x80)? true:false;
156 metadata.type = (FLAC__MetaDataType)(buf[0] & 0x7f);
157 metadata.length = unpack_uint32(buf+1, 3);
160 printf("METADATA block #%u\n", blocknum);
161 printf(" byte offset: %u\n", byte_offset);
162 printf(" type: %u (%s)\n", (unsigned)metadata.type, metadata.type<=FLAC__METADATA_TYPE_SEEKTABLE? metadata_type_string_[metadata.type] : "UNKNOWN");
163 printf(" is last: %s\n", metadata.is_last? "true":"false");
164 printf(" length: %u\n", metadata.length);
166 if(metadata.length > sizeof(buf)) {
167 printf(" SKIPPING large block\n");
171 /* read the metadata block data */
172 if(fread(buf, 1, metadata.length, f) < metadata.length) {
173 fprintf(stderr, "ERROR: short count reading metadata block data\n");
176 switch(metadata.type) {
177 case FLAC__METADATA_TYPE_STREAMINFO:
179 metadata.data.stream_info.min_blocksize = unpack_uint32(b, 2); b += 2;
180 metadata.data.stream_info.max_blocksize = unpack_uint32(b, 2); b += 2;
181 metadata.data.stream_info.min_framesize = unpack_uint32(b, 3); b += 3;
182 metadata.data.stream_info.max_framesize = unpack_uint32(b, 3); b += 3;
183 metadata.data.stream_info.sample_rate = (unpack_uint32(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
184 metadata.data.stream_info.channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
185 metadata.data.stream_info.bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 1) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
186 metadata.data.stream_info.total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64(b+4, 4);
187 memcpy(metadata.data.stream_info.md5sum, b+8, 16);
189 case FLAC__METADATA_TYPE_PADDING:
194 case FLAC__METADATA_TYPE_APPLICATION:
195 memcpy(buf, metadata.data.application.id, 4);
196 metadata.data.application.data = buf+4;
198 case FLAC__METADATA_TYPE_SEEKTABLE:
199 metadata.data.seek_table.num_points = metadata.length / SEEKPOINT_LEN_;
200 b = buf; /* we leave the points in buf for printing later */
203 printf("SKIPPING block of unknown type\n");
208 switch(metadata.type) {
209 case FLAC__METADATA_TYPE_STREAMINFO:
210 printf(" minumum blocksize: %u samples\n", metadata.data.stream_info.min_blocksize);
211 printf(" maximum blocksize: %u samples\n", metadata.data.stream_info.max_blocksize);
212 printf(" minimum framesize: %u bytes\n", metadata.data.stream_info.min_framesize);
213 printf(" maximum framesize: %u bytes\n", metadata.data.stream_info.max_framesize);
214 printf(" sample_rate: %u Hz\n", metadata.data.stream_info.sample_rate);
215 printf(" channels: %u\n", metadata.data.stream_info.channels);
216 printf(" bits-per-sample: %u\n", metadata.data.stream_info.bits_per_sample);
217 printf(" total samples: %llu\n", metadata.data.stream_info.total_samples);
218 printf(" MD5 signature: ");
219 for(i = 0; i < 16; i++)
220 printf("%02x", metadata.data.stream_info.md5sum[i]);
223 case FLAC__METADATA_TYPE_PADDING:
225 printf(" pad contents:\n");
226 hexdump(buf, metadata.length, " ");
229 case FLAC__METADATA_TYPE_APPLICATION:
230 printf(" application ID: ");
231 for(i = 0; i < 4; i++)
232 printf("%02x", metadata.data.application.id[i]);
235 printf(" data contents:\n");
236 hexdump(metadata.data.application.data, metadata.length, " ");
239 case FLAC__METADATA_TYPE_SEEKTABLE:
240 printf(" seek points: %u\n", metadata.data.seek_table.num_points);
242 for(i = 0; i < metadata.data.seek_table.num_points; i++, b += SEEKPOINT_LEN_)
243 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));
251 byte_offset += (4 + metadata.length);
252 } while (!metadata.is_last);
257 FLAC__uint32 unpack_uint32(FLAC__byte *b, unsigned bytes)
259 FLAC__uint32 ret = 0;
262 for(i = 0; i < bytes; i++)
263 ret = (ret << 8) | (FLAC__uint32)(*b++);
268 FLAC__uint64 unpack_uint64(FLAC__byte *b, unsigned bytes)
270 FLAC__uint64 ret = 0;
273 for(i = 0; i < bytes; i++)
274 ret = (ret << 8) | (FLAC__uint64)(*b++);
279 void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent)
281 unsigned i, left = bytes;
282 const FLAC__byte *b = buf;
284 for(i = 0; i < bytes; i += 16) {
286 "%02X %02X %02X %02X %02X %02X %02X %02X "
287 "%02X %02X %02X %02X %02X %02X %02X %02X "
288 "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
290 left > 0? (unsigned char)b[ 0] : 0,
291 left > 1? (unsigned char)b[ 1] : 0,
292 left > 2? (unsigned char)b[ 2] : 0,
293 left > 3? (unsigned char)b[ 3] : 0,
294 left > 4? (unsigned char)b[ 4] : 0,
295 left > 5? (unsigned char)b[ 5] : 0,
296 left > 6? (unsigned char)b[ 6] : 0,
297 left > 7? (unsigned char)b[ 7] : 0,
298 left > 8? (unsigned char)b[ 8] : 0,
299 left > 9? (unsigned char)b[ 9] : 0,
300 left > 10? (unsigned char)b[10] : 0,
301 left > 11? (unsigned char)b[11] : 0,
302 left > 12? (unsigned char)b[12] : 0,
303 left > 13? (unsigned char)b[13] : 0,
304 left > 14? (unsigned char)b[14] : 0,
305 left > 15? (unsigned char)b[15] : 0,
306 (left > 0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
307 (left > 1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
308 (left > 2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
309 (left > 3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
310 (left > 4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
311 (left > 5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
312 (left > 6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
313 (left > 7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
314 (left > 8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
315 (left > 9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
316 (left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
317 (left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
318 (left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
319 (left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
320 (left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
321 (left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '