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 bool list(FILE *f, bool verbose);
43 static uint32 unpack_uint32(byte *b, unsigned bytes);
44 static uint64 unpack_uint64(byte *b, unsigned bytes);
45 static void hexdump(const byte *buf, unsigned bytes);
47 int main(int argc, char *argv[])
50 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]);
69 if(i + (list_mode? 1:2) != argc)
70 return usage("ERROR: invalid arguments (more/less than %d filename%s?)\n", (list_mode? 1:2), (list_mode? "":"s"));
73 FILE *f = fopen(argv[i], "r");
76 fprintf(stderr, "ERROR opening %s\n", argv[i]);
89 int usage(const char *message, ...)
94 va_start(args, message);
96 (void) vfprintf(stderr, message, args);
101 printf("==============================================================================\n");
102 printf("metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
103 printf("Copyright (C) 2001 Josh Coalson\n");
105 printf("This program is free software; you can redistribute it and/or\n");
106 printf("modify it under the terms of the GNU General Public License\n");
107 printf("as published by the Free Software Foundation; either version 2\n");
108 printf("of the License, or (at your option) any later version.\n");
110 printf("This program is distributed in the hope that it will be useful,\n");
111 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
112 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
113 printf("GNU General Public License for more details.\n");
115 printf("You should have received a copy of the GNU General Public License\n");
116 printf("along with this program; if not, write to the Free Software\n");
117 printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
118 printf("==============================================================================\n");
120 printf(" metaflac [options] infile [outfile]\n");
122 printf("options:\n");
123 printf(" -l : list metadata blocks\n");
124 printf(" -v : verbose\n");
129 bool list(FILE *f, bool verbose)
133 FLAC__StreamMetaData metadata;
134 unsigned blocknum = 0, byte_offset = 0, i;
136 /* read the stream sync code */
137 if(fread(buf, 1, 4, f) < 4 || memcmp(buf, sync_string_, 4)) {
138 fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
143 /* read the metadata blocks */
145 /* read the metadata block header */
146 if(fread(buf, 1, 4, f) < 4) {
147 fprintf(stderr, "ERROR: short count reading metadata block header\n");
150 metadata.is_last = (buf[0] & 0x80)? true:false;
151 metadata.type = (FLAC__MetaDataType)(buf[0] & 0x7f);
152 metadata.length = unpack_uint32(buf+1, 3);
155 printf("METADATA block #%u\n", blocknum);
156 printf("byte offset: %u\n", byte_offset);
157 printf("type: %u (%s)\n", (unsigned)metadata.type, metadata.type<=FLAC__METADATA_TYPE_SEEKTABLE? metadata_type_string_[metadata.type] : "UNKNOWN");
158 printf("is last: %s\n", metadata.is_last? "true":"false");
159 printf("length: %u\n", metadata.length);
161 if(metadata.length > sizeof(buf)) {
162 printf("SKIPPING large block\n\n");
166 /* read the metadata block data */
167 if(fread(buf, 1, metadata.length, f) < metadata.length) {
168 fprintf(stderr, "ERROR: short count reading metadata block data\n\n");
171 switch(metadata.type) {
172 case FLAC__METADATA_TYPE_STREAMINFO:
174 metadata.data.stream_info.min_blocksize = unpack_uint32(b, 2); b += 2;
175 metadata.data.stream_info.max_blocksize = unpack_uint32(b, 2); b += 2;
176 metadata.data.stream_info.min_framesize = unpack_uint32(b, 3); b += 3;
177 metadata.data.stream_info.max_framesize = unpack_uint32(b, 3); b += 3;
178 metadata.data.stream_info.sample_rate = (unpack_uint32(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
179 metadata.data.stream_info.channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
180 metadata.data.stream_info.bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 1) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
181 metadata.data.stream_info.total_samples = (((uint64)(b[3] & 0x0f)) << 32) | unpack_uint64(b+4, 4);
182 memcpy(metadata.data.stream_info.md5sum, b+8, 16);
184 case FLAC__METADATA_TYPE_PADDING:
189 case FLAC__METADATA_TYPE_APPLICATION:
190 memcpy(buf, metadata.data.application.id, 4);
191 metadata.data.application.data = buf+4;
193 case FLAC__METADATA_TYPE_SEEKTABLE:
194 metadata.data.seek_table.num_points = metadata.length / SEEKPOINT_LEN_;
195 b = buf; /* we leave the points in buf for printing later */
198 printf("SKIPPING block of unknown type\n\n");
203 switch(metadata.type) {
204 case FLAC__METADATA_TYPE_STREAMINFO:
205 printf("minumum blocksize: %u samples\n", metadata.data.stream_info.min_blocksize);
206 printf("maximum blocksize: %u samples\n", metadata.data.stream_info.max_blocksize);
207 printf("minimum framesize: %u bytes\n", metadata.data.stream_info.min_framesize);
208 printf("maximum framesize: %u bytes\n", metadata.data.stream_info.max_framesize);
209 printf("sample_rate: %u Hz\n", metadata.data.stream_info.sample_rate);
210 printf("channels: %u\n", metadata.data.stream_info.channels);
211 printf("bits-per-sample: %u\n", metadata.data.stream_info.bits_per_sample);
212 printf("total samples: %llu\n", metadata.data.stream_info.total_samples);
213 printf("MD5 signature: ");
214 for(i = 0; i < 16; i++)
215 printf("%02x", metadata.data.stream_info.md5sum[i]);
218 case FLAC__METADATA_TYPE_PADDING:
220 printf("pad contents:\n");
221 hexdump(buf, metadata.length);
224 case FLAC__METADATA_TYPE_APPLICATION:
225 printf("Application ID: ");
226 for(i = 0; i < 4; i++)
227 printf("%02x", metadata.data.application.id[i]);
230 printf("data contents:\n");
231 hexdump(metadata.data.application.data, metadata.length);
234 case FLAC__METADATA_TYPE_SEEKTABLE:
235 printf("seek points: %u\n", metadata.data.seek_table.num_points);
236 for(i = 0; i < metadata.data.seek_table.num_points; i++, b += SEEKPOINT_LEN_) {
237 printf("\tpoint %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));
244 if(!metadata.is_last)
248 byte_offset += (4 + metadata.length);
249 } while (!metadata.is_last);
254 uint32 unpack_uint32(byte *b, unsigned bytes)
259 for(i = 0; i < bytes; i++)
260 ret = (ret << 8) | (uint32)(*b++);
265 uint64 unpack_uint64(byte *b, unsigned bytes)
270 for(i = 0; i < bytes; i++)
271 ret = (ret << 8) | (uint64)(*b++);
276 void hexdump(const byte *buf, unsigned bytes)
278 unsigned i, left = bytes;
281 for(i = 0; i < bytes; i += 16) {
283 "%02X %02X %02X %02X %02X %02X %02X %02X "
284 "%02X %02X %02X %02X %02X %02X %02X %02X "
285 "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
287 left > 0? (unsigned char)b[ 0] : 0,
288 left > 1? (unsigned char)b[ 1] : 0,
289 left > 2? (unsigned char)b[ 2] : 0,
290 left > 3? (unsigned char)b[ 3] : 0,
291 left > 4? (unsigned char)b[ 4] : 0,
292 left > 5? (unsigned char)b[ 5] : 0,
293 left > 6? (unsigned char)b[ 6] : 0,
294 left > 7? (unsigned char)b[ 7] : 0,
295 left > 8? (unsigned char)b[ 8] : 0,
296 left > 9? (unsigned char)b[ 9] : 0,
297 left > 10? (unsigned char)b[10] : 0,
298 left > 11? (unsigned char)b[11] : 0,
299 left > 12? (unsigned char)b[12] : 0,
300 left > 13? (unsigned char)b[13] : 0,
301 left > 14? (unsigned char)b[14] : 0,
302 left > 15? (unsigned char)b[15] : 0,
303 (left > 0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
304 (left > 1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
305 (left > 2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
306 (left > 3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
307 (left > 4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
308 (left > 5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
309 (left > 6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
310 (left > 7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
311 (left > 8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
312 (left > 9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
313 (left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
314 (left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
315 (left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
316 (left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
317 (left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
318 (left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '