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 ////////////////////////////////////////////////////////////////////////////
11 // This module handles the metadata structure introduced in WavPack 4.0
17 int read_metadata_buff (WavpackContext
*wpc
, WavpackMetadata
*wpmd
)
19 uint32_t bytes_to_read
;
22 if (!wpc
->infile (&wpmd
->id
, 1) || !wpc
->infile (&tchar
, 1))
25 wpmd
->byte_length
= tchar
<< 1;
27 if (wpmd
->id
& ID_LARGE
) {
28 wpmd
->id
&= ~ID_LARGE
;
30 if (!wpc
->infile (&tchar
, 1))
33 wpmd
->byte_length
+= (int32_t) tchar
<< 9;
35 if (!wpc
->infile (&tchar
, 1))
38 wpmd
->byte_length
+= (int32_t) tchar
<< 17;
41 if (wpmd
->id
& ID_ODD_SIZE
) {
42 wpmd
->id
&= ~ID_ODD_SIZE
;
46 if (!wpmd
->byte_length
|| wpmd
->id
== ID_WV_BITSTREAM
) {
51 bytes_to_read
= wpmd
->byte_length
+ (wpmd
->byte_length
& 1);
53 if (bytes_to_read
> sizeof (wpc
->read_buffer
)) {
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
);
63 wpmd
->data
= wpc
->read_buffer
;
65 if (bytes_to_read
&& wpc
->infile (wpc
->read_buffer
, bytes_to_read
) != (int32_t) bytes_to_read
) {
73 int process_metadata (WavpackContext
*wpc
, WavpackMetadata
*wpmd
)
75 WavpackStream
*wps
= &wpc
->stream
;
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
);
91 return read_entropy_vars (wps
, wpmd
);
93 case ID_HYBRID_PROFILE
:
94 return read_hybrid_profile (wps
, wpmd
);
97 return read_float_info (wps
, wpmd
);
100 return read_int32_info (wps
, wpmd
);
102 case ID_CHANNEL_INFO
:
103 return read_channel_info (wpc
, wpmd
);
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
:
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
)
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);
155 memcpy (buffer_start
+ 2, wpmd
->data
, mdsize
- 2);
158 wphdr
->ckSize
+= mdsize
;
162 void free_metadata (WavpackMetadata
*wpmd
)