FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / libwavpack / metadata.c
blobc944093b1987bbf066dfeac4c5e607c4507116e3
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->infile (&wpmd->id, 1) || !wpc->infile (&tchar, 1))
23 return FALSE;
25 wpmd->byte_length = tchar << 1;
27 if (wpmd->id & ID_LARGE) {
28 wpmd->id &= ~ID_LARGE;
30 if (!wpc->infile (&tchar, 1))
31 return FALSE;
33 wpmd->byte_length += (int32_t) tchar << 9;
35 if (!wpc->infile (&tchar, 1))
36 return FALSE;
38 wpmd->byte_length += (int32_t) tchar << 17;
41 if (wpmd->id & ID_ODD_SIZE) {
42 wpmd->id &= ~ID_ODD_SIZE;
43 wpmd->byte_length--;
46 if (!wpmd->byte_length || wpmd->id == ID_WV_BITSTREAM) {
47 wpmd->data = NULL;
48 return TRUE;
51 bytes_to_read = wpmd->byte_length + (wpmd->byte_length & 1);
53 if (bytes_to_read > sizeof (wpc->read_buffer)) {
54 wpmd->data = NULL;
56 while (bytes_to_read > sizeof (wpc->read_buffer))
57 if (wpc->infile (wpc->read_buffer, sizeof (wpc->read_buffer)) == sizeof (wpc->read_buffer))
58 bytes_to_read -= sizeof (wpc->read_buffer);
59 else
60 return FALSE;
62 else
63 wpmd->data = wpc->read_buffer;
65 if (bytes_to_read && wpc->infile (wpc->read_buffer, bytes_to_read) != (int32_t) bytes_to_read) {
66 wpmd->data = NULL;
67 return FALSE;
70 return TRUE;
73 int process_metadata (WavpackContext *wpc, WavpackMetadata *wpmd)
75 WavpackStream *wps = &wpc->stream;
77 switch (wpmd->id) {
78 case ID_DUMMY:
79 return TRUE;
81 case ID_DECORR_TERMS:
82 return read_decorr_terms (wps, wpmd);
84 case ID_DECORR_WEIGHTS:
85 return read_decorr_weights (wps, wpmd);
87 case ID_DECORR_SAMPLES:
88 return read_decorr_samples (wps, wpmd);
90 case ID_ENTROPY_VARS:
91 return read_entropy_vars (wps, wpmd);
93 case ID_HYBRID_PROFILE:
94 return read_hybrid_profile (wps, wpmd);
96 case ID_FLOAT_INFO:
97 return read_float_info (wps, wpmd);
99 case ID_INT32_INFO:
100 return read_int32_info (wps, wpmd);
102 case ID_CHANNEL_INFO:
103 return read_channel_info (wpc, wpmd);
105 case ID_SAMPLE_RATE:
106 return read_sample_rate (wpc, wpmd);
108 case ID_CONFIG_BLOCK:
109 return read_config_info (wpc, wpmd);
111 case ID_WV_BITSTREAM:
112 return init_wv_bitstream (wpc, wpmd);
114 case ID_SHAPING_WEIGHTS:
115 case ID_WVC_BITSTREAM:
116 case ID_WVX_BITSTREAM:
117 return TRUE;
119 default:
120 return (wpmd->id & ID_OPTIONAL_DATA) ? TRUE : FALSE;
124 int copy_metadata (WavpackMetadata *wpmd, uchar *buffer_start, uchar *buffer_end)
126 uint32_t mdsize = wpmd->byte_length + (wpmd->byte_length & 1);
127 WavpackHeader *wphdr = (WavpackHeader *) buffer_start;
129 if (wpmd->byte_length & 1)
130 ((char *) wpmd->data) [wpmd->byte_length] = 0;
132 mdsize += (wpmd->byte_length > 510) ? 4 : 2;
133 buffer_start += wphdr->ckSize + 8;
135 if (buffer_start + mdsize >= buffer_end)
136 return FALSE;
138 buffer_start [0] = wpmd->id | (wpmd->byte_length & 1 ? ID_ODD_SIZE : 0);
139 buffer_start [1] = (wpmd->byte_length + 1) >> 1;
141 if (wpmd->byte_length > 510) {
142 buffer_start [0] |= ID_LARGE;
143 buffer_start [2] = (wpmd->byte_length + 1) >> 9;
144 buffer_start [3] = (wpmd->byte_length + 1) >> 17;
147 if (wpmd->data && wpmd->byte_length) {
148 if (wpmd->byte_length > 510) {
149 buffer_start [0] |= ID_LARGE;
150 buffer_start [2] = (wpmd->byte_length + 1) >> 9;
151 buffer_start [3] = (wpmd->byte_length + 1) >> 17;
152 memcpy (buffer_start + 4, wpmd->data, mdsize - 4);
154 else
155 memcpy (buffer_start + 2, wpmd->data, mdsize - 2);
158 wphdr->ckSize += mdsize;
159 return TRUE;
162 void free_metadata (WavpackMetadata *wpmd)
164 wpmd->data = NULL;