FS#11968 by Peter Lecky - Slovak language update
[maemo-rb.git] / apps / codecs / libwavpack / metadata.c
blob4dce10100f627f272b543f4ad3867ddb505882a5
1 ////////////////////////////////////////////////////////////////////////////
2 // **** WAVPACK **** //
3 // Hybrid Lossless Wavefile Compressor //
4 // Copyright (c) 1998 - 2003 Conifer Software. //
5 // All Rights Reserved. //
6 // Distributed under the BSD Software License (see license.txt) //
7 ////////////////////////////////////////////////////////////////////////////
9 // metadata.c
11 // This module handles the metadata structure introduced in WavPack 4.0
13 #include "wavpack.h"
15 #include <string.h>
17 int read_metadata_buff (WavpackContext *wpc, WavpackMetadata *wpmd)
19 uint32_t bytes_to_read;
20 uchar tchar;
22 if (wpc->stream.block_bytes_left < 2 || !wpc->infile (&wpmd->id, 1) || !wpc->infile (&tchar, 1))
23 return FALSE;
25 wpmd->byte_length = tchar << 1;
26 wpc->stream.block_bytes_left -= 2;
28 if (wpmd->id & ID_LARGE) {
29 wpmd->id &= ~ID_LARGE;
31 if (wpc->stream.block_bytes_left < 2 || !wpc->infile (&tchar, 1))
32 return FALSE;
34 wpmd->byte_length += (int32_t) tchar << 9;
36 if (!wpc->infile (&tchar, 1))
37 return FALSE;
39 wpmd->byte_length += (int32_t) tchar << 17;
40 wpc->stream.block_bytes_left -= 2;
43 if ((wpc->stream.block_bytes_left -= wpmd->byte_length) < 0)
44 return FALSE;
46 if (wpmd->id & ID_ODD_SIZE) {
47 wpmd->id &= ~ID_ODD_SIZE;
48 wpmd->byte_length--;
51 if (!wpmd->byte_length || wpmd->id == ID_WV_BITSTREAM) {
52 wpmd->data = NULL;
53 return TRUE;
56 bytes_to_read = wpmd->byte_length + (wpmd->byte_length & 1);
58 if (bytes_to_read > sizeof (wpc->read_buffer)) {
59 wpmd->data = NULL;
61 while (bytes_to_read > sizeof (wpc->read_buffer))
62 if (wpc->infile (wpc->read_buffer, sizeof (wpc->read_buffer)) == sizeof (wpc->read_buffer))
63 bytes_to_read -= sizeof (wpc->read_buffer);
64 else
65 return FALSE;
67 else
68 wpmd->data = wpc->read_buffer;
70 if (bytes_to_read && wpc->infile (wpc->read_buffer, bytes_to_read) != (int32_t) bytes_to_read) {
71 wpmd->data = NULL;
72 return FALSE;
75 return TRUE;
78 int process_metadata (WavpackContext *wpc, WavpackMetadata *wpmd)
80 WavpackStream *wps = &wpc->stream;
82 switch (wpmd->id) {
83 case ID_DUMMY:
84 return TRUE;
86 case ID_DECORR_TERMS:
87 return read_decorr_terms (wps, wpmd);
89 case ID_DECORR_WEIGHTS:
90 return read_decorr_weights (wps, wpmd);
92 case ID_DECORR_SAMPLES:
93 return read_decorr_samples (wps, wpmd);
95 case ID_ENTROPY_VARS:
96 return read_entropy_vars (wps, wpmd);
98 case ID_HYBRID_PROFILE:
99 return read_hybrid_profile (wps, wpmd);
101 case ID_FLOAT_INFO:
102 return read_float_info (wps, wpmd);
104 case ID_INT32_INFO:
105 return read_int32_info (wps, wpmd);
107 case ID_CHANNEL_INFO:
108 return read_channel_info (wpc, wpmd);
110 case ID_SAMPLE_RATE:
111 return read_sample_rate (wpc, wpmd);
113 case ID_CONFIG_BLOCK:
114 return read_config_info (wpc, wpmd);
116 case ID_WV_BITSTREAM:
117 return init_wv_bitstream (wpc, wpmd);
119 case ID_SHAPING_WEIGHTS:
120 case ID_WVC_BITSTREAM:
121 case ID_WVX_BITSTREAM:
122 return TRUE;
124 default:
125 return (wpmd->id & ID_OPTIONAL_DATA) ? TRUE : FALSE;
129 int copy_metadata (WavpackMetadata *wpmd, uchar *buffer_start, uchar *buffer_end)
131 uint32_t mdsize = wpmd->byte_length + (wpmd->byte_length & 1);
132 WavpackHeader *wphdr = (WavpackHeader *) buffer_start;
134 if (wpmd->byte_length & 1)
135 ((char *) wpmd->data) [wpmd->byte_length] = 0;
137 mdsize += (wpmd->byte_length > 510) ? 4 : 2;
138 buffer_start += wphdr->ckSize + 8;
140 if (buffer_start + mdsize >= buffer_end)
141 return FALSE;
143 buffer_start [0] = wpmd->id | (wpmd->byte_length & 1 ? ID_ODD_SIZE : 0);
144 buffer_start [1] = (wpmd->byte_length + 1) >> 1;
146 if (wpmd->byte_length > 510) {
147 buffer_start [0] |= ID_LARGE;
148 buffer_start [2] = (wpmd->byte_length + 1) >> 9;
149 buffer_start [3] = (wpmd->byte_length + 1) >> 17;
152 if (wpmd->data && wpmd->byte_length) {
153 if (wpmd->byte_length > 510) {
154 buffer_start [0] |= ID_LARGE;
155 buffer_start [2] = (wpmd->byte_length + 1) >> 9;
156 buffer_start [3] = (wpmd->byte_length + 1) >> 17;
157 memcpy (buffer_start + 4, wpmd->data, mdsize - 4);
159 else
160 memcpy (buffer_start + 2, wpmd->data, mdsize - 2);
163 wphdr->ckSize += mdsize;
164 return TRUE;
167 void free_metadata (WavpackMetadata *wpmd)
169 wpmd->data = NULL;