2005-02-25 17:05:30 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// **** WAVPACK **** //
|
|
|
|
// Hybrid Lossless Wavefile Compressor //
|
|
|
|
// Copyright (c) 1998 - 2003 Conifer Software. //
|
|
|
|
// All Rights Reserved. //
|
|
|
|
// Distributed under the BSD Software License (see license.txt) //
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// metadata.c
|
|
|
|
|
|
|
|
// This module handles the metadata structure introduced in WavPack 4.0
|
|
|
|
|
|
|
|
#include "wavpack.h"
|
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2005-02-25 17:05:30 +00:00
|
|
|
int read_metadata_buff (WavpackContext *wpc, WavpackMetadata *wpmd)
|
|
|
|
{
|
2007-07-27 03:18:45 +00:00
|
|
|
uint32_t bytes_to_read;
|
2005-02-25 17:05:30 +00:00
|
|
|
uchar tchar;
|
|
|
|
|
|
|
|
if (!wpc->infile (&wpmd->id, 1) || !wpc->infile (&tchar, 1))
|
2005-07-04 06:38:00 +00:00
|
|
|
return FALSE;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
|
|
|
wpmd->byte_length = tchar << 1;
|
|
|
|
|
|
|
|
if (wpmd->id & ID_LARGE) {
|
2005-07-04 06:38:00 +00:00
|
|
|
wpmd->id &= ~ID_LARGE;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
if (!wpc->infile (&tchar, 1))
|
|
|
|
return FALSE;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2006-03-26 22:54:15 +00:00
|
|
|
wpmd->byte_length += (int32_t) tchar << 9;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
if (!wpc->infile (&tchar, 1))
|
|
|
|
return FALSE;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2006-03-26 22:54:15 +00:00
|
|
|
wpmd->byte_length += (int32_t) tchar << 17;
|
2005-02-25 17:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wpmd->id & ID_ODD_SIZE) {
|
2005-07-04 06:38:00 +00:00
|
|
|
wpmd->id &= ~ID_ODD_SIZE;
|
|
|
|
wpmd->byte_length--;
|
2005-02-25 17:05:30 +00:00
|
|
|
}
|
|
|
|
|
2007-07-27 03:18:45 +00:00
|
|
|
if (!wpmd->byte_length || wpmd->id == ID_WV_BITSTREAM) {
|
|
|
|
wpmd->data = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2007-07-27 03:18:45 +00:00
|
|
|
bytes_to_read = wpmd->byte_length + (wpmd->byte_length & 1);
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2007-07-27 03:18:45 +00:00
|
|
|
if (bytes_to_read > sizeof (wpc->read_buffer)) {
|
|
|
|
wpmd->data = NULL;
|
|
|
|
|
|
|
|
while (bytes_to_read > sizeof (wpc->read_buffer))
|
|
|
|
if (wpc->infile (wpc->read_buffer, sizeof (wpc->read_buffer)) == sizeof (wpc->read_buffer))
|
|
|
|
bytes_to_read -= sizeof (wpc->read_buffer);
|
|
|
|
else
|
|
|
|
return FALSE;
|
2005-02-25 17:05:30 +00:00
|
|
|
}
|
|
|
|
else
|
2007-07-27 03:18:45 +00:00
|
|
|
wpmd->data = wpc->read_buffer;
|
|
|
|
|
|
|
|
if (bytes_to_read && wpc->infile (wpc->read_buffer, bytes_to_read) != (int32_t) bytes_to_read) {
|
2005-07-04 06:38:00 +00:00
|
|
|
wpmd->data = NULL;
|
2007-07-27 03:18:45 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-02-25 17:05:30 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int process_metadata (WavpackContext *wpc, WavpackMetadata *wpmd)
|
|
|
|
{
|
|
|
|
WavpackStream *wps = &wpc->stream;
|
|
|
|
|
|
|
|
switch (wpmd->id) {
|
2005-07-04 06:38:00 +00:00
|
|
|
case ID_DUMMY:
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case ID_DECORR_TERMS:
|
|
|
|
return read_decorr_terms (wps, wpmd);
|
|
|
|
|
|
|
|
case ID_DECORR_WEIGHTS:
|
|
|
|
return read_decorr_weights (wps, wpmd);
|
|
|
|
|
|
|
|
case ID_DECORR_SAMPLES:
|
|
|
|
return read_decorr_samples (wps, wpmd);
|
|
|
|
|
|
|
|
case ID_ENTROPY_VARS:
|
|
|
|
return read_entropy_vars (wps, wpmd);
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
case ID_HYBRID_PROFILE:
|
|
|
|
return read_hybrid_profile (wps, wpmd);
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
case ID_FLOAT_INFO:
|
|
|
|
return read_float_info (wps, wpmd);
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
case ID_INT32_INFO:
|
|
|
|
return read_int32_info (wps, wpmd);
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
case ID_CHANNEL_INFO:
|
|
|
|
return read_channel_info (wpc, wpmd);
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
case ID_CONFIG_BLOCK:
|
|
|
|
return read_config_info (wpc, wpmd);
|
|
|
|
|
|
|
|
case ID_WV_BITSTREAM:
|
|
|
|
return init_wv_bitstream (wpc, wpmd);
|
|
|
|
|
|
|
|
case ID_SHAPING_WEIGHTS:
|
|
|
|
case ID_WVC_BITSTREAM:
|
|
|
|
case ID_WVX_BITSTREAM:
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (wpmd->id & ID_OPTIONAL_DATA) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
}
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
int copy_metadata (WavpackMetadata *wpmd, uchar *buffer_start, uchar *buffer_end)
|
|
|
|
{
|
2006-03-26 22:54:15 +00:00
|
|
|
uint32_t mdsize = wpmd->byte_length + (wpmd->byte_length & 1);
|
2005-07-04 06:38:00 +00:00
|
|
|
WavpackHeader *wphdr = (WavpackHeader *) buffer_start;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
if (wpmd->byte_length & 1)
|
|
|
|
((char *) wpmd->data) [wpmd->byte_length] = 0;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
mdsize += (wpmd->byte_length > 510) ? 4 : 2;
|
|
|
|
buffer_start += wphdr->ckSize + 8;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
if (buffer_start + mdsize >= buffer_end)
|
|
|
|
return FALSE;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
buffer_start [0] = wpmd->id | (wpmd->byte_length & 1 ? ID_ODD_SIZE : 0);
|
|
|
|
buffer_start [1] = (wpmd->byte_length + 1) >> 1;
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
if (wpmd->byte_length > 510) {
|
|
|
|
buffer_start [0] |= ID_LARGE;
|
|
|
|
buffer_start [2] = (wpmd->byte_length + 1) >> 9;
|
|
|
|
buffer_start [3] = (wpmd->byte_length + 1) >> 17;
|
|
|
|
}
|
2005-02-25 17:05:30 +00:00
|
|
|
|
2005-07-04 06:38:00 +00:00
|
|
|
if (wpmd->data && wpmd->byte_length) {
|
|
|
|
if (wpmd->byte_length > 510) {
|
|
|
|
buffer_start [0] |= ID_LARGE;
|
|
|
|
buffer_start [2] = (wpmd->byte_length + 1) >> 9;
|
|
|
|
buffer_start [3] = (wpmd->byte_length + 1) >> 17;
|
|
|
|
memcpy (buffer_start + 4, wpmd->data, mdsize - 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy (buffer_start + 2, wpmd->data, mdsize - 2);
|
2005-02-25 17:05:30 +00:00
|
|
|
}
|
2005-07-04 06:38:00 +00:00
|
|
|
|
|
|
|
wphdr->ckSize += mdsize;
|
|
|
|
return TRUE;
|
2005-02-25 17:05:30 +00:00
|
|
|
}
|
2005-07-04 06:38:00 +00:00
|
|
|
|
|
|
|
void free_metadata (WavpackMetadata *wpmd)
|
|
|
|
{
|
|
|
|
wpmd->data = NULL;
|
|
|
|
}
|
|
|
|
|