mirror of
https://github.com/claunia/flac.git
synced 2025-12-16 18:54:26 +00:00
move in some common metadata setting stuff here
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
#include <stdlib.h> /* for malloc() */
|
||||
#include <string.h> /* for memcmp() */
|
||||
|
||||
FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block, const FLAC__StreamMetadata_StreamInfo *blockcopy)
|
||||
FLAC__bool mutils__compare_block_data_streaminfo(const FLAC__StreamMetadata_StreamInfo *block, const FLAC__StreamMetadata_StreamInfo *blockcopy)
|
||||
{
|
||||
if(blockcopy->min_blocksize != block->min_blocksize) {
|
||||
printf("FAILED, min_blocksize mismatch, expected %u, got %u\n", block->min_blocksize, blockcopy->min_blocksize);
|
||||
@@ -100,14 +100,14 @@ FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo
|
||||
return true;
|
||||
}
|
||||
|
||||
FLAC__bool compare_block_data_padding_(const FLAC__StreamMetadata_Padding *block, const FLAC__StreamMetadata_Padding *blockcopy, unsigned block_length)
|
||||
FLAC__bool mutils__compare_block_data_padding(const FLAC__StreamMetadata_Padding *block, const FLAC__StreamMetadata_Padding *blockcopy, unsigned block_length)
|
||||
{
|
||||
/* we don't compare the padding guts */
|
||||
(void)block, (void)blockcopy, (void)block_length;
|
||||
return true;
|
||||
}
|
||||
|
||||
FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block, const FLAC__StreamMetadata_Application *blockcopy, unsigned block_length)
|
||||
FLAC__bool mutils__compare_block_data_application(const FLAC__StreamMetadata_Application *block, const FLAC__StreamMetadata_Application *blockcopy, unsigned block_length)
|
||||
{
|
||||
if(block_length < sizeof(block->id)) {
|
||||
printf("FAILED, bad block length = %u\n", block_length);
|
||||
@@ -149,7 +149,7 @@ FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Applicatio
|
||||
return true;
|
||||
}
|
||||
|
||||
FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block, const FLAC__StreamMetadata_SeekTable *blockcopy)
|
||||
FLAC__bool mutils__compare_block_data_seektable(const FLAC__StreamMetadata_SeekTable *block, const FLAC__StreamMetadata_SeekTable *blockcopy)
|
||||
{
|
||||
unsigned i;
|
||||
if(blockcopy->num_points != block->num_points) {
|
||||
@@ -173,7 +173,7 @@ FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *b
|
||||
return true;
|
||||
}
|
||||
|
||||
FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block, const FLAC__StreamMetadata_VorbisComment *blockcopy)
|
||||
FLAC__bool mutils__compare_block_data_vorbiscomment(const FLAC__StreamMetadata_VorbisComment *block, const FLAC__StreamMetadata_VorbisComment *blockcopy)
|
||||
{
|
||||
unsigned i;
|
||||
if(blockcopy->vendor_string.length != block->vendor_string.length) {
|
||||
@@ -215,7 +215,7 @@ FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisCo
|
||||
return true;
|
||||
}
|
||||
|
||||
FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block, const FLAC__StreamMetadata_CueSheet *blockcopy)
|
||||
FLAC__bool mutils__compare_block_data_cuesheet(const FLAC__StreamMetadata_CueSheet *block, const FLAC__StreamMetadata_CueSheet *blockcopy)
|
||||
{
|
||||
unsigned i, j;
|
||||
|
||||
@@ -285,7 +285,32 @@ FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *blo
|
||||
return true;
|
||||
}
|
||||
|
||||
FLAC__bool compare_block_(const FLAC__StreamMetadata *block, const FLAC__StreamMetadata *blockcopy)
|
||||
FLAC__bool mutils__compare_block_data_unknown(const FLAC__StreamMetadata_Unknown *block, const FLAC__StreamMetadata_Unknown *blockcopy, unsigned block_length)
|
||||
{
|
||||
if(0 == block->data || 0 == blockcopy->data) {
|
||||
if(block->data != blockcopy->data) {
|
||||
printf("FAILED, data mismatch (%s's data pointer is null)\n", 0==block->data?"original":"copy");
|
||||
return false;
|
||||
}
|
||||
else if(block_length > 0) {
|
||||
printf("FAILED, data pointer is null but block length is not 0\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(block_length == 0) {
|
||||
printf("FAILED, data pointer is not null but block length is 0\n");
|
||||
return false;
|
||||
}
|
||||
else if(0 != memcmp(blockcopy->data, block->data, block_length)) {
|
||||
printf("FAILED, data mismatch\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
FLAC__bool mutils__compare_block(const FLAC__StreamMetadata *block, const FLAC__StreamMetadata *blockcopy)
|
||||
{
|
||||
if(blockcopy->type != block->type) {
|
||||
printf("FAILED, type mismatch, expected %s, got %s\n", FLAC__MetadataTypeString[block->type], FLAC__MetadataTypeString[blockcopy->type]);
|
||||
@@ -301,19 +326,196 @@ FLAC__bool compare_block_(const FLAC__StreamMetadata *block, const FLAC__StreamM
|
||||
}
|
||||
switch(block->type) {
|
||||
case FLAC__METADATA_TYPE_STREAMINFO:
|
||||
return compare_block_data_streaminfo_(&block->data.stream_info, &blockcopy->data.stream_info);
|
||||
return mutils__compare_block_data_streaminfo(&block->data.stream_info, &blockcopy->data.stream_info);
|
||||
case FLAC__METADATA_TYPE_PADDING:
|
||||
return compare_block_data_padding_(&block->data.padding, &blockcopy->data.padding, block->length);
|
||||
return mutils__compare_block_data_padding(&block->data.padding, &blockcopy->data.padding, block->length);
|
||||
case FLAC__METADATA_TYPE_APPLICATION:
|
||||
return compare_block_data_application_(&block->data.application, &blockcopy->data.application, block->length);
|
||||
return mutils__compare_block_data_application(&block->data.application, &blockcopy->data.application, block->length);
|
||||
case FLAC__METADATA_TYPE_SEEKTABLE:
|
||||
return compare_block_data_seektable_(&block->data.seek_table, &blockcopy->data.seek_table);
|
||||
return mutils__compare_block_data_seektable(&block->data.seek_table, &blockcopy->data.seek_table);
|
||||
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
||||
return compare_block_data_vorbiscomment_(&block->data.vorbis_comment, &blockcopy->data.vorbis_comment);
|
||||
return mutils__compare_block_data_vorbiscomment(&block->data.vorbis_comment, &blockcopy->data.vorbis_comment);
|
||||
case FLAC__METADATA_TYPE_CUESHEET:
|
||||
return compare_block_data_cuesheet_(&block->data.cue_sheet, &blockcopy->data.cue_sheet);
|
||||
return mutils__compare_block_data_cuesheet(&block->data.cue_sheet, &blockcopy->data.cue_sheet);
|
||||
default:
|
||||
printf("FAILED, invalid block type %u\n", (unsigned)block->type);
|
||||
return false;
|
||||
return mutils__compare_block_data_unknown(&block->data.unknown, &blockcopy->data.unknown, block->length);
|
||||
}
|
||||
}
|
||||
static void *malloc_or_die_(size_t size)
|
||||
{
|
||||
void *x = malloc(size);
|
||||
if(0 == x) {
|
||||
fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
|
||||
exit(1);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
static void *calloc_or_die_(size_t n, size_t size)
|
||||
{
|
||||
void *x = calloc(n, size);
|
||||
if(0 == x) {
|
||||
fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)n * (unsigned)size);
|
||||
exit(1);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
void mutils__init_metadata_blocks(
|
||||
FLAC__StreamMetadata *streaminfo,
|
||||
FLAC__StreamMetadata *padding,
|
||||
FLAC__StreamMetadata *seektable,
|
||||
FLAC__StreamMetadata *application1,
|
||||
FLAC__StreamMetadata *application2,
|
||||
FLAC__StreamMetadata *vorbiscomment,
|
||||
FLAC__StreamMetadata *cuesheet
|
||||
)
|
||||
{
|
||||
/*
|
||||
most of the actual numbers and data in the blocks don't matter,
|
||||
we just want to make sure the decoder parses them correctly
|
||||
|
||||
remember, the metadata interface gets tested after the decoders,
|
||||
so we do all the metadata manipulation here without it.
|
||||
*/
|
||||
|
||||
/* min/max_framesize and md5sum don't get written at first, so we have to leave them 0 */
|
||||
streaminfo->is_last = false;
|
||||
streaminfo->type = FLAC__METADATA_TYPE_STREAMINFO;
|
||||
streaminfo->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
|
||||
streaminfo->data.stream_info.min_blocksize = 576;
|
||||
streaminfo->data.stream_info.max_blocksize = 576;
|
||||
streaminfo->data.stream_info.min_framesize = 0;
|
||||
streaminfo->data.stream_info.max_framesize = 0;
|
||||
streaminfo->data.stream_info.sample_rate = 44100;
|
||||
streaminfo->data.stream_info.channels = 1;
|
||||
streaminfo->data.stream_info.bits_per_sample = 8;
|
||||
streaminfo->data.stream_info.total_samples = 0;
|
||||
memset(streaminfo->data.stream_info.md5sum, 0, 16);
|
||||
|
||||
padding->is_last = false;
|
||||
padding->type = FLAC__METADATA_TYPE_PADDING;
|
||||
padding->length = 1234;
|
||||
|
||||
seektable->is_last = false;
|
||||
seektable->type = FLAC__METADATA_TYPE_SEEKTABLE;
|
||||
seektable->data.seek_table.num_points = 2;
|
||||
seektable->length = seektable->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
|
||||
seektable->data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)malloc_or_die_(seektable->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
|
||||
seektable->data.seek_table.points[0].sample_number = 0;
|
||||
seektable->data.seek_table.points[0].stream_offset = 0;
|
||||
seektable->data.seek_table.points[0].frame_samples = streaminfo->data.stream_info.min_blocksize;
|
||||
seektable->data.seek_table.points[1].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
|
||||
seektable->data.seek_table.points[1].stream_offset = 1000;
|
||||
seektable->data.seek_table.points[1].frame_samples = streaminfo->data.stream_info.min_blocksize;
|
||||
|
||||
application1->is_last = false;
|
||||
application1->type = FLAC__METADATA_TYPE_APPLICATION;
|
||||
application1->length = 8;
|
||||
memcpy(application1->data.application.id, "\xfe\xdc\xba\x98", 4);
|
||||
application1->data.application.data = (FLAC__byte*)malloc_or_die_(4);
|
||||
memcpy(application1->data.application.data, "\xf0\xe1\xd2\xc3", 4);
|
||||
|
||||
application2->is_last = false;
|
||||
application2->type = FLAC__METADATA_TYPE_APPLICATION;
|
||||
application2->length = 4;
|
||||
memcpy(application2->data.application.id, "\x76\x54\x32\x10", 4);
|
||||
application2->data.application.data = 0;
|
||||
|
||||
{
|
||||
const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);
|
||||
vorbiscomment->is_last = false;
|
||||
vorbiscomment->type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
|
||||
vorbiscomment->length = (4 + vendor_string_length) + 4 + (4 + 5) + (4 + 0);
|
||||
vorbiscomment->data.vorbis_comment.vendor_string.length = vendor_string_length;
|
||||
vorbiscomment->data.vorbis_comment.vendor_string.entry = (FLAC__byte*)malloc_or_die_(vendor_string_length);
|
||||
memcpy(vorbiscomment->data.vorbis_comment.vendor_string.entry, FLAC__VENDOR_STRING, vendor_string_length);
|
||||
vorbiscomment->data.vorbis_comment.num_comments = 2;
|
||||
vorbiscomment->data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc_or_die_(vorbiscomment->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
|
||||
vorbiscomment->data.vorbis_comment.comments[0].length = 5;
|
||||
vorbiscomment->data.vorbis_comment.comments[0].entry = (FLAC__byte*)malloc_or_die_(5);
|
||||
memcpy(vorbiscomment->data.vorbis_comment.comments[0].entry, "ab=cd", 5);
|
||||
vorbiscomment->data.vorbis_comment.comments[1].length = 0;
|
||||
vorbiscomment->data.vorbis_comment.comments[1].entry = 0;
|
||||
}
|
||||
|
||||
cuesheet->is_last = true;
|
||||
cuesheet->type = FLAC__METADATA_TYPE_CUESHEET;
|
||||
cuesheet->length =
|
||||
/* cuesheet guts */
|
||||
(
|
||||
FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
|
||||
) / 8 +
|
||||
/* 2 tracks */
|
||||
3 * (
|
||||
FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
|
||||
) / 8 +
|
||||
/* 3 index points */
|
||||
3 * (
|
||||
FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
|
||||
FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
|
||||
) / 8
|
||||
;
|
||||
memset(cuesheet->data.cue_sheet.media_catalog_number, 0, sizeof(cuesheet->data.cue_sheet.media_catalog_number));
|
||||
cuesheet->data.cue_sheet.media_catalog_number[0] = 'j';
|
||||
cuesheet->data.cue_sheet.media_catalog_number[1] = 'C';
|
||||
cuesheet->data.cue_sheet.lead_in = 2 * 44100;
|
||||
cuesheet->data.cue_sheet.is_cd = true;
|
||||
cuesheet->data.cue_sheet.num_tracks = 3;
|
||||
cuesheet->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc_or_die_(cuesheet->data.cue_sheet.num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
|
||||
cuesheet->data.cue_sheet.tracks[0].offset = 0;
|
||||
cuesheet->data.cue_sheet.tracks[0].number = 1;
|
||||
memcpy(cuesheet->data.cue_sheet.tracks[0].isrc, "ACBDE1234567", sizeof(cuesheet->data.cue_sheet.tracks[0].isrc));
|
||||
cuesheet->data.cue_sheet.tracks[0].type = 0;
|
||||
cuesheet->data.cue_sheet.tracks[0].pre_emphasis = 1;
|
||||
cuesheet->data.cue_sheet.tracks[0].num_indices = 2;
|
||||
cuesheet->data.cue_sheet.tracks[0].indices = (FLAC__StreamMetadata_CueSheet_Index*)malloc_or_die_(cuesheet->data.cue_sheet.tracks[0].num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
|
||||
cuesheet->data.cue_sheet.tracks[0].indices[0].offset = 0;
|
||||
cuesheet->data.cue_sheet.tracks[0].indices[0].number = 0;
|
||||
cuesheet->data.cue_sheet.tracks[0].indices[1].offset = 123 * 588;
|
||||
cuesheet->data.cue_sheet.tracks[0].indices[1].number = 1;
|
||||
cuesheet->data.cue_sheet.tracks[1].offset = 1234 * 588;
|
||||
cuesheet->data.cue_sheet.tracks[1].number = 2;
|
||||
memcpy(cuesheet->data.cue_sheet.tracks[1].isrc, "ACBDE7654321", sizeof(cuesheet->data.cue_sheet.tracks[1].isrc));
|
||||
cuesheet->data.cue_sheet.tracks[1].type = 1;
|
||||
cuesheet->data.cue_sheet.tracks[1].pre_emphasis = 0;
|
||||
cuesheet->data.cue_sheet.tracks[1].num_indices = 1;
|
||||
cuesheet->data.cue_sheet.tracks[1].indices = (FLAC__StreamMetadata_CueSheet_Index*)malloc_or_die_(cuesheet->data.cue_sheet.tracks[1].num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
|
||||
cuesheet->data.cue_sheet.tracks[1].indices[0].offset = 0;
|
||||
cuesheet->data.cue_sheet.tracks[1].indices[0].number = 1;
|
||||
cuesheet->data.cue_sheet.tracks[2].offset = 12345 * 588;
|
||||
cuesheet->data.cue_sheet.tracks[2].number = 170;
|
||||
cuesheet->data.cue_sheet.tracks[2].num_indices = 0;
|
||||
}
|
||||
|
||||
void mutils__free_metadata_blocks(
|
||||
FLAC__StreamMetadata *streaminfo,
|
||||
FLAC__StreamMetadata *padding,
|
||||
FLAC__StreamMetadata *seektable,
|
||||
FLAC__StreamMetadata *application1,
|
||||
FLAC__StreamMetadata *application2,
|
||||
FLAC__StreamMetadata *vorbiscomment,
|
||||
FLAC__StreamMetadata *cuesheet
|
||||
)
|
||||
{
|
||||
(void)streaminfo, (void)padding, (void)application2;
|
||||
free(seektable->data.seek_table.points);
|
||||
free(application1->data.application.data);
|
||||
free(vorbiscomment->data.vorbis_comment.vendor_string.entry);
|
||||
free(vorbiscomment->data.vorbis_comment.comments[0].entry);
|
||||
free(vorbiscomment->data.vorbis_comment.comments);
|
||||
free(cuesheet->data.cue_sheet.tracks[0].indices);
|
||||
free(cuesheet->data.cue_sheet.tracks[1].indices);
|
||||
free(cuesheet->data.cue_sheet.tracks);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user