Files
flac/src/metaflac/main.c

369 lines
12 KiB
C
Raw Normal View History

2001-02-23 21:38:26 +00:00
/* metaflac - Command-line FLAC metadata editor
2002-01-26 18:05:12 +00:00
* Copyright (C) 2001,2002 Josh Coalson
2001-02-23 21:38:26 +00:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
2001-03-21 22:59:07 +00:00
/*
2001-02-23 22:03:52 +00:00
* WATCHOUT - this is meant to be very lightweight an not even dependent
* on libFLAC, so there are a couple places where FLAC__* variables are
* duplicated here. Look for 'DUPLICATE:' in comments.
*/
2001-02-23 21:38:26 +00:00
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2002-05-04 17:46:37 +00:00
/* we only use some typedefs, #defines, and enums from here: */
2001-02-23 21:38:26 +00:00
#include "FLAC/all.h"
2001-02-23 22:03:52 +00:00
static const char *sync_string_ = "fLaC"; /* DUPLICATE:FLAC__STREAM_SYNC_STRING */
2001-02-23 23:09:32 +00:00
static const char *metadata_type_string_[] = { /* DUPLICATE:FLAC__MetaDataTypeString */
"STREAMINFO",
"PADDING",
2001-04-05 21:31:59 +00:00
"APPLICATION",
"SEEKTABLE"
2001-02-23 23:09:32 +00:00
};
2002-05-04 17:46:37 +00:00
static const unsigned SEEKPOINT_LEN_ = 18; /* DUPLICATE:FLAC__STREAM_METADATA_SEEKPOINT_LENGTH */
2001-02-23 22:03:52 +00:00
2001-02-23 21:38:26 +00:00
static int usage(const char *message, ...);
2001-06-23 03:03:24 +00:00
static FLAC__bool list(FILE *f, FLAC__bool verbose);
static FLAC__uint32 unpack_uint32(FLAC__byte *b, unsigned bytes);
static FLAC__uint64 unpack_uint64(FLAC__byte *b, unsigned bytes);
static void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent);
2001-02-23 21:38:26 +00:00
int main(int argc, char *argv[])
{
int i, first_file, retval = 0;
2001-06-23 03:03:24 +00:00
FLAC__bool verbose = false, list_mode = true;
2001-02-23 21:38:26 +00:00
if(argc <= 1)
return usage(0);
/* get the options */
for(i = 1; i < argc; i++) {
if(argv[i][0] != '-' || argv[i][1] == 0)
break;
if(0 == strcmp(argv[i], "-l"))
list_mode = true;
else if(0 == strcmp(argv[i], "-v"))
verbose = true;
else if(0 == strcmp(argv[i], "-v-"))
verbose = false;
else {
return usage("ERROR: invalid option '%s'\n", argv[i]);
}
}
if(i == argc)
return usage(0);
2001-02-23 21:38:26 +00:00
2001-02-23 22:03:52 +00:00
if(list_mode) {
for(first_file = i; i < argc; i++) {
FILE *f = fopen(argv[i], "rb");
2001-02-23 22:03:52 +00:00
if(0 == f) {
fprintf(stderr, "ERROR opening %s\n", argv[i]);
retval = 1;
}
else {
printf("%sfile: %s\n", i==first_file? "" : "\n", argv[i]);
2001-02-23 22:03:52 +00:00
if(!list(f, verbose))
retval = 1;
2001-02-23 22:03:52 +00:00
fclose(f);
}
}
2001-02-23 22:03:52 +00:00
}
return retval;
2001-02-23 21:38:26 +00:00
}
int usage(const char *message, ...)
{
va_list args;
if(message) {
va_start(args, message);
(void) vfprintf(stderr, message, args);
va_end(args);
}
printf("==============================================================================\n");
printf("metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
2002-01-26 18:05:12 +00:00
printf("Copyright (C) 2001,2002 Josh Coalson\n");
2001-02-23 21:38:26 +00:00
printf("\n");
printf("This program is free software; you can redistribute it and/or\n");
printf("modify it under the terms of the GNU General Public License\n");
printf("as published by the Free Software Foundation; either version 2\n");
printf("of the License, or (at your option) any later version.\n");
printf("\n");
printf("This program is distributed in the hope that it will be useful,\n");
printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
printf("GNU General Public License for more details.\n");
printf("\n");
printf("You should have received a copy of the GNU General Public License\n");
printf("along with this program; if not, write to the Free Software\n");
printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
printf("==============================================================================\n");
printf("Usage:\n");
printf(" metaflac [options] flacfile [...]\n");
2001-02-23 21:38:26 +00:00
printf("\n");
printf("options:\n");
printf(" -l : list metadata blocks\n");
printf(" -v : verbose\n");
return message? 1 : 0;
2001-02-23 21:38:26 +00:00
}
2001-02-23 22:03:52 +00:00
2001-06-23 03:03:24 +00:00
FLAC__bool list(FILE *f, FLAC__bool verbose)
2001-02-23 22:03:52 +00:00
{
2001-06-23 03:03:24 +00:00
FLAC__byte buf[65536];
FLAC__byte *b = buf;
2001-02-23 22:03:52 +00:00
FLAC__StreamMetaData metadata;
2001-02-23 23:32:22 +00:00
unsigned blocknum = 0, byte_offset = 0, i;
2001-02-23 22:03:52 +00:00
/* skip any id3v2 tag */
if(fread(buf, 1, 4, f) < 4) {
fprintf(stderr, "ERROR: not a FLAC file\n");
return false;
}
if(0 == memcmp(buf, "ID3", 3)) {
unsigned tag_length = 0;
/* skip to the tag length */
if(fseek(f, 2, SEEK_CUR) < 0) {
fprintf(stderr, "ERROR: bad ID3v2 tag\n");
return false;
}
/* read the length */
for(i = 0; i < 4; i++) {
if(fread(buf, 1, 1, f) < 1 || buf[0] & 0x80) {
fprintf(stderr, "ERROR: bad ID3v2 tag\n");
return false;
}
tag_length <<= 7;
tag_length |= (buf[0] & 0x7f);
}
/* skip the rest of the tag */
if(fseek(f, tag_length, SEEK_CUR) < 0) {
fprintf(stderr, "ERROR: bad ID3v2 tag\n");
return false;
}
/* read the stream sync code */
if(fread(buf, 1, 4, f) < 4) {
fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
return false;
}
}
/* check the stream sync code */
if(memcmp(buf, sync_string_, 4)) {
2001-02-23 22:03:52 +00:00
fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
return false;
}
2001-02-23 23:32:22 +00:00
byte_offset += 4;
2001-02-23 22:03:52 +00:00
/* read the metadata blocks */
do {
/* read the metadata block header */
if(fread(buf, 1, 4, f) < 4) {
fprintf(stderr, "ERROR: short count reading metadata block header\n");
return false;
}
metadata.is_last = (buf[0] & 0x80)? true:false;
metadata.type = (FLAC__MetaDataType)(buf[0] & 0x7f);
2001-02-23 23:09:32 +00:00
metadata.length = unpack_uint32(buf+1, 3);
/* print header */
2001-02-23 23:32:22 +00:00
printf("METADATA block #%u\n", blocknum);
printf(" byte offset: %u\n", byte_offset);
printf(" type: %u (%s)\n", (unsigned)metadata.type, metadata.type<=FLAC__METADATA_TYPE_SEEKTABLE? metadata_type_string_[metadata.type] : "UNKNOWN");
printf(" is last: %s\n", metadata.is_last? "true":"false");
printf(" length: %u\n", metadata.length);
2001-02-23 23:09:32 +00:00
if(metadata.length > sizeof(buf)) {
printf(" SKIPPING large block\n");
if(fseek(f, metadata.length, SEEK_CUR) < 0) {
fprintf(stderr, "ERROR: short count skipping metadata block data\n");
return false;
}
2001-02-23 23:09:32 +00:00
continue;
}
2001-02-23 22:03:52 +00:00
/* read the metadata block data */
2001-02-23 23:09:32 +00:00
if(fread(buf, 1, metadata.length, f) < metadata.length) {
fprintf(stderr, "ERROR: short count reading metadata block data\n");
2001-02-23 23:09:32 +00:00
return false;
}
switch(metadata.type) {
case FLAC__METADATA_TYPE_STREAMINFO:
b = buf;
metadata.data.stream_info.min_blocksize = unpack_uint32(b, 2); b += 2;
metadata.data.stream_info.max_blocksize = unpack_uint32(b, 2); b += 2;
metadata.data.stream_info.min_framesize = unpack_uint32(b, 3); b += 3;
metadata.data.stream_info.max_framesize = unpack_uint32(b, 3); b += 3;
metadata.data.stream_info.sample_rate = (unpack_uint32(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
metadata.data.stream_info.channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
metadata.data.stream_info.bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 1) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
2001-06-23 03:03:24 +00:00
metadata.data.stream_info.total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64(b+4, 4);
2001-02-23 23:09:32 +00:00
memcpy(metadata.data.stream_info.md5sum, b+8, 16);
break;
case FLAC__METADATA_TYPE_PADDING:
if(verbose) {
/* dump contents */
}
break;
case FLAC__METADATA_TYPE_APPLICATION:
2002-05-04 17:46:37 +00:00
memcpy(metadata.data.application.id, buf, 4);
2001-02-23 23:09:32 +00:00
metadata.data.application.data = buf+4;
break;
2001-04-05 21:31:59 +00:00
case FLAC__METADATA_TYPE_SEEKTABLE:
metadata.data.seek_table.num_points = metadata.length / SEEKPOINT_LEN_;
b = buf; /* we leave the points in buf for printing later */
2001-04-05 21:31:59 +00:00
break;
2001-02-23 23:09:32 +00:00
default:
printf("SKIPPING block of unknown type\n");
2001-02-23 23:09:32 +00:00
continue;
}
2001-02-23 22:03:52 +00:00
2001-02-23 23:09:32 +00:00
/* print data */
switch(metadata.type) {
case FLAC__METADATA_TYPE_STREAMINFO:
printf(" minumum blocksize: %u samples\n", metadata.data.stream_info.min_blocksize);
printf(" maximum blocksize: %u samples\n", metadata.data.stream_info.max_blocksize);
printf(" minimum framesize: %u bytes\n", metadata.data.stream_info.min_framesize);
printf(" maximum framesize: %u bytes\n", metadata.data.stream_info.max_framesize);
printf(" sample_rate: %u Hz\n", metadata.data.stream_info.sample_rate);
printf(" channels: %u\n", metadata.data.stream_info.channels);
printf(" bits-per-sample: %u\n", metadata.data.stream_info.bits_per_sample);
printf(" total samples: %llu\n", metadata.data.stream_info.total_samples);
printf(" MD5 signature: ");
2001-02-23 23:09:32 +00:00
for(i = 0; i < 16; i++)
printf("%02x", metadata.data.stream_info.md5sum[i]);
printf("\n");
break;
case FLAC__METADATA_TYPE_PADDING:
if(verbose) {
printf(" pad contents:\n");
hexdump(buf, metadata.length, " ");
2001-02-23 23:09:32 +00:00
}
break;
case FLAC__METADATA_TYPE_APPLICATION:
printf(" application ID: ");
2001-02-23 23:09:32 +00:00
for(i = 0; i < 4; i++)
printf("%02x", metadata.data.application.id[i]);
printf("\n");
if(verbose) {
printf(" data contents:\n");
hexdump(metadata.data.application.data, metadata.length, " ");
2001-02-23 23:09:32 +00:00
}
break;
2001-04-05 21:31:59 +00:00
case FLAC__METADATA_TYPE_SEEKTABLE:
printf(" seek points: %u\n", metadata.data.seek_table.num_points);
if(verbose) {
for(i = 0; i < metadata.data.seek_table.num_points; i++, b += SEEKPOINT_LEN_)
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));
2001-04-05 21:31:59 +00:00
}
break;
2001-02-23 23:09:32 +00:00
default:
FLAC__ASSERT(0);
2001-02-23 23:09:32 +00:00
}
blocknum++;
2001-02-23 23:32:22 +00:00
byte_offset += (4 + metadata.length);
2001-02-23 22:03:52 +00:00
} while (!metadata.is_last);
return true;
}
2001-02-23 23:09:32 +00:00
2001-06-23 03:03:24 +00:00
FLAC__uint32 unpack_uint32(FLAC__byte *b, unsigned bytes)
2001-02-23 23:09:32 +00:00
{
2001-06-23 03:03:24 +00:00
FLAC__uint32 ret = 0;
2001-02-23 23:09:32 +00:00
unsigned i;
for(i = 0; i < bytes; i++)
2001-06-23 03:03:24 +00:00
ret = (ret << 8) | (FLAC__uint32)(*b++);
2001-02-23 23:09:32 +00:00
return ret;
}
2001-06-23 03:03:24 +00:00
FLAC__uint64 unpack_uint64(FLAC__byte *b, unsigned bytes)
2001-02-23 23:09:32 +00:00
{
2001-06-23 03:03:24 +00:00
FLAC__uint64 ret = 0;
2001-02-23 23:09:32 +00:00
unsigned i;
for(i = 0; i < bytes; i++)
2001-06-23 03:03:24 +00:00
ret = (ret << 8) | (FLAC__uint64)(*b++);
2001-02-23 23:09:32 +00:00
return ret;
}
void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent)
2001-02-23 23:09:32 +00:00
{
unsigned i, left = bytes;
2001-06-23 03:03:24 +00:00
const FLAC__byte *b = buf;
2001-02-23 23:09:32 +00:00
for(i = 0; i < bytes; i += 16) {
printf("%s%08X: "
2001-02-23 23:09:32 +00:00
"%02X %02X %02X %02X %02X %02X %02X %02X "
"%02X %02X %02X %02X %02X %02X %02X %02X "
"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
indent, i,
2001-02-23 23:09:32 +00:00
left > 0? (unsigned char)b[ 0] : 0,
left > 1? (unsigned char)b[ 1] : 0,
left > 2? (unsigned char)b[ 2] : 0,
left > 3? (unsigned char)b[ 3] : 0,
left > 4? (unsigned char)b[ 4] : 0,
left > 5? (unsigned char)b[ 5] : 0,
left > 6? (unsigned char)b[ 6] : 0,
left > 7? (unsigned char)b[ 7] : 0,
left > 8? (unsigned char)b[ 8] : 0,
left > 9? (unsigned char)b[ 9] : 0,
left > 10? (unsigned char)b[10] : 0,
left > 11? (unsigned char)b[11] : 0,
left > 12? (unsigned char)b[12] : 0,
left > 13? (unsigned char)b[13] : 0,
left > 14? (unsigned char)b[14] : 0,
left > 15? (unsigned char)b[15] : 0,
(left > 0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
(left > 1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
(left > 2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
(left > 3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
(left > 4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
(left > 5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
(left > 6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
(left > 7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
(left > 8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
(left > 9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
(left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
(left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
(left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
(left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
(left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
(left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '
);
left -= 16;
b += 16;
}
}