Commit ee44de42 authored by Josh Coalson's avatar Josh Coalson
Browse files

initial import; split up on main.c

parent 9c2a011d
This diff is collapsed.
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002 Josh Coalson
*
* 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.
*/
#ifndef metaflac__operations_h
#define metaflac__operations_h
#include "options.h"
FLAC__bool do_operations(const CommandLineOptions *options);
#endif
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002 Josh Coalson
*
* 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.
*/
#include "utils.h"
#include "FLAC/assert.h"
#include "FLAC/file_decoder.h"
#include "FLAC/metadata.h"
#include "share/grabbag.h"
static FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write)
{
FLAC__bool ok = true, found_seektable_block = false;
FLAC__StreamMetadata *block = 0;
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
FLAC__uint64 total_samples = 0;
unsigned sample_rate = 0;
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
do {
block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
sample_rate = block->data.stream_info.sample_rate;
total_samples = block->data.stream_info.total_samples;
}
else if(block->type == FLAC__METADATA_TYPE_SEEKTABLE)
found_seektable_block = true;
} while(!found_seektable_block && FLAC__metadata_iterator_next(iterator));
if(total_samples == 0) {
fprintf(stderr, "%s: ERROR: cannot add seekpoints because STREAMINFO block does not specify total_samples\n", filename);
return false;
}
if(!found_seektable_block) {
/* create a new block */
block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
if(0 == block)
die("out of memory allocating SEEKTABLE block");
while(FLAC__metadata_iterator_prev(iterator))
;
if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
fprintf(stderr, "%s: ERROR: adding new SEEKTABLE block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
return false;
}
/* iterator is left pointing to new block */
FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
}
FLAC__metadata_iterator_delete(iterator);
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_SEEKTABLE);
if(!grabbag__seektable_convert_specification_to_template(specification, /*only_explicit_placeholders=*/false, total_samples, sample_rate, block, /*spec_has_real_points=*/0)) {
fprintf(stderr, "%s: ERROR (internal) preparing seektable with seekpoints\n", filename);
return false;
}
ok = populate_seekpoint_values(filename, block, needs_write);
if(ok) {
//@@@@ compact it
}
return ok;
}
/*
* local routines
*/
typedef struct {
FLAC__StreamMetadata_SeekTable *seektable_template;
FLAC__uint64 samples_written;
unsigned first_seekpoint_to_check;
FLAC__bool error_occurred;
FLAC__StreamDecoderErrorStatus error_status;
} ClientData;
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
ClientData *cd = (ClientData*)client_data;
FLAC__ASSERT(0 != cd);
if(!cd->error_occurred && cd->seektable_template->num_points > 0) {
const unsigned blocksize = frame->header.blocksize;
//@@@@
cd->samples_written += blocksize;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
else
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
#if 0
@@@@
{
const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
const FLAC__uint64 frame_first_sample = seekable_stream_encoder->private_->samples_written;
const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
FLAC__uint64 test_sample;
unsigned i;
for(i = seekable_stream_encoder->private_->first_seekpoint_to_check; i < seekable_stream_encoder->private_->seek_table->num_points; i++) {
test_sample = seekable_stream_encoder->private_->seek_table->points[i].sample_number;
if(test_sample > frame_last_sample) {
break;
}
else if(test_sample >= frame_first_sample) {
seekable_stream_encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
seekable_stream_encoder->private_->seek_table->points[i].stream_offset = seekable_stream_encoder->private_->bytes_written - seekable_stream_encoder->private_->stream_offset;
seekable_stream_encoder->private_->seek_table->points[i].frame_samples = blocksize;
seekable_stream_encoder->private_->first_seekpoint_to_check++;
/* DO NOT: "break;" and here's why:
* The seektable template may contain more than one target
* sample for any given frame; we will keep looping, generating
* duplicate seekpoints for them, and we'll clean it up later,
* just before writing the seektable back to the metadata.
*/
}
else {
seekable_stream_encoder->private_->first_seekpoint_to_check++;
}
}
}
#endif
static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
(void)decoder, (void)metadata, (void)client_data;
FLAC__ASSERT(0); /* we asked to skip all metadata */
}
static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
ClientData *cd = (ClientData*)client_data;
FLAC__ASSERT(0 != cd);
if(!cd->error_occurred) { /* don't let multiple errors overwrite the first one */
cd->error_occurred = true;
cd->error_status = status;
}
}
FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
{
FLAC__FileDecoder *decoder;
ClientData client_data;
FLAC__bool ok = true;
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_SEEKTABLE);
client_data.seektable_template = &block->data.seek_table;
client_data.samples_written = 0;
client_data.first_seekpoint_to_check = 0;
client_data.error_occurred = false;
decoder = FLAC__file_decoder_new();
if(0 == decoder) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) creating the decoder instance\n", filename);
return false;
}
FLAC__file_decoder_set_md5_checking(decoder, false);
FLAC__file_decoder_set_filename(decoder, filename);
FLAC__file_decoder_set_metadata_ignore_all(decoder);
FLAC__file_decoder_set_write_callback(decoder, write_callback_);
FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
FLAC__file_decoder_set_error_callback(decoder, error_callback_);
FLAC__file_decoder_set_client_data(decoder, &client_data);
if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) initializing the decoder instance (%s)\n", filename, FLAC__file_decoder_get_resolved_state_string(decoder));
ok = false;
}
if(ok && !FLAC__file_decoder_process_until_end_of_file(decoder)) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__file_decoder_get_resolved_state_string(decoder));
ok = false;
}
if(ok && client_data.error_occurred) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%u:%s)\n", filename, (unsigned)client_data.error_status, FLAC__StreamDecoderErrorStatusString[client_data.error_status]);
ok = false;
}
FLAC__file_decoder_delete(decoder);
return ok;
}
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002 Josh Coalson
*
* 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.
*/
#include "options.h"
#include "utils.h"
#include "FLAC/assert.h"
#include "FLAC/metadata.h"
#include <string.h>
FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
{
unsigned i;
FLAC__bool ok = true;
FLAC__StreamMetadata *block;
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
block = FLAC__metadata_iterator_get_block(iterator);
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_STREAMINFO);
if(0 != filename)
printf("%s:", filename);
switch(operation->type) {
case OP__SHOW_MD5SUM:
for(i = 0; i < 16; i++)
printf("%02x", block->data.stream_info.md5sum[i]);
printf("\n");
break;
case OP__SHOW_MIN_BLOCKSIZE:
printf("%u\n", block->data.stream_info.min_blocksize);
break;
case OP__SHOW_MAX_BLOCKSIZE:
printf("%u\n", block->data.stream_info.max_blocksize);
break;
case OP__SHOW_MIN_FRAMESIZE:
printf("%u\n", block->data.stream_info.min_framesize);
break;
case OP__SHOW_MAX_FRAMESIZE:
printf("%u\n", block->data.stream_info.max_framesize);
break;
case OP__SHOW_SAMPLE_RATE:
printf("%u\n", block->data.stream_info.sample_rate);
break;
case OP__SHOW_CHANNELS:
printf("%u\n", block->data.stream_info.channels);
break;
case OP__SHOW_BPS:
printf("%u\n", block->data.stream_info.bits_per_sample);
break;
case OP__SHOW_TOTAL_SAMPLES:
printf("%llu\n", block->data.stream_info.total_samples);
break;
case OP__SET_MD5SUM:
memcpy(block->data.stream_info.md5sum, operation->argument.streaminfo_md5.value, 16);
*needs_write = true;
break;
case OP__SET_MIN_BLOCKSIZE:
block->data.stream_info.min_blocksize = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_MAX_BLOCKSIZE:
block->data.stream_info.max_blocksize = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_MIN_FRAMESIZE:
block->data.stream_info.min_framesize = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_MAX_FRAMESIZE:
block->data.stream_info.max_framesize = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_SAMPLE_RATE:
block->data.stream_info.sample_rate = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_CHANNELS:
block->data.stream_info.channels = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_BPS:
block->data.stream_info.bits_per_sample = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_TOTAL_SAMPLES:
block->data.stream_info.total_samples = operation->argument.streaminfo_uint64.value;
*needs_write = true;
break;
default:
ok = false;
FLAC__ASSERT(0);
break;
};
FLAC__metadata_iterator_delete(iterator);
return ok;
}
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002 Josh Coalson
*
* 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.
*/
#include "options.h"
#include "utils.h"
#include "FLAC/assert.h"
#include "share/utf8.h"
#include <stdlib.h>
#include <string.h>
static FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
static FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
static FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
static FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw);
static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_VcFilename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw);
static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_VcFilename *vc_filename, FLAC__bool raw);
FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw)
{
FLAC__bool ok = true, found_vc_block = false;
FLAC__StreamMetadata *block = 0;
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
do {
block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
found_vc_block = true;
} while(!found_vc_block && FLAC__metadata_iterator_next(iterator));
if(!found_vc_block) {
/* create a new block if necessary */
if(operation->type == OP__SET_VC_FIELD || operation->type == OP__IMPORT_VC_FROM) {
block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
if(0 == block)
die("out of memory allocating VORBIS_COMMENT block");
while(FLAC__metadata_iterator_next(iterator))
;
if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
fprintf(stderr, "%s: ERROR: adding new VORBIS_COMMENT block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
return false;
}
/* iterator is left pointing to new block */
FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
}
else {
FLAC__metadata_iterator_delete(iterator);
return ok;
}
}
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
switch(operation->type) {
case OP__SHOW_VC_VENDOR:
write_vc_field(filename, &block->data.vorbis_comment.vendor_string, raw, stdout);
break;
case OP__SHOW_VC_FIELD:
write_vc_fields(filename, operation->argument.vc_field_name.value, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, stdout);
break;
case OP__REMOVE_VC_ALL:
ok = remove_vc_all(filename, block, needs_write);
break;
case OP__REMOVE_VC_FIELD:
ok = remove_vc_field(filename, block, operation->argument.vc_field_name.value, needs_write);
break;
case OP__REMOVE_VC_FIRSTFIELD:
ok = remove_vc_firstfield(filename, block, operation->argument.vc_field_name.value, needs_write);
break;
case OP__SET_VC_FIELD:
ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, raw);
break;
case OP__IMPORT_VC_FROM:
ok = import_vc_from(filename, block, &operation->argument.vc_filename, needs_write, raw);
break;
case OP__EXPORT_VC_TO:
ok = export_vc_to(filename, block, &operation->argument.vc_filename, raw);
break;
default:
ok = false;
FLAC__ASSERT(0);
break;
};
FLAC__metadata_iterator_delete(iterator);
return ok;
}
/*
* local routines
*/
FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
{
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
FLAC__ASSERT(0 != needs_write);
if(0 != block->data.vorbis_comment.comments) {
FLAC__ASSERT(block->data.vorbis_comment.num_comments > 0);
if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, 0)) {
fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
return false;
}
*needs_write = true;
}
else {
FLAC__ASSERT(block->data.vorbis_comment.num_comments == 0);
}
return true;
}
FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
{
int n;
FLAC__ASSERT(0 != needs_write);
n = FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, field_name);
if(n < 0) {
fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
return false;
}
else if(n > 0)
*needs_write = true;
return true;
}
FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
{
int n;
FLAC__ASSERT(0 != needs_write);
n = FLAC__metadata_object_vorbiscomment_remove_entry_matching(block, field_name);
if(n < 0) {
fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
return false;
}
else if(n > 0)
*needs_write = true;
return true;
}
FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw)
{
FLAC__StreamMetadata_VorbisComment_Entry entry;
char *converted;
FLAC__bool needs_free = false;
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
FLAC__ASSERT(0 != field);
FLAC__ASSERT(0 != needs_write);
if(raw) {
entry.entry = field->field;
}
else if(utf8_encode(field->field, &converted) >= 0) {
entry.entry = converted;
needs_free = true;
}
else {
fprintf(stderr, "%s: ERROR: couldn't convert comment to UTF-8\n", filename);
return false;
}
entry.length = strlen(entry.entry);
if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, block->data.vorbis_comment.num_comments, entry, /*copy=*/true)) {
if(needs_free)
free(converted);
fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
return false;
}
else {
*needs_write = true;
if(needs_free)
free(converted);
return true;
}
}
FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_VcFilename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw)
{
FILE *f;
char line[65536];
FLAC__bool ret;
if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
return false;
}
if(0 == strcmp(vc_filename->value, "-"))
f = stdin;
else
f = fopen(vc_filename->value, "r");
if(0 == f) {
fprintf(stderr, "%s: ERROR: can't open import file %s\n", filename, vc_filename->value);
return false;
}
ret = true;
while(ret && !feof(f)) {
fgets(line, sizeof(line), f);
if(!feof(f)) {
char *p = strchr(line, '\n');