File: aiff.c

package info (click to toggle)
wavpack 5.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 4,424 kB
  • sloc: ansic: 27,424; asm: 9,843; sh: 5,051; makefile: 173
file content (366 lines) | stat: -rw-r--r-- 16,413 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
////////////////////////////////////////////////////////////////////////////
//                           **** WAVPACK ****                            //
//                  Hybrid Lossless Wavefile Compressor                   //
//                Copyright (c) 1998 - 2024 David Bryant.                 //
//                          All Rights Reserved.                          //
//      Distributed under the BSD Software License (see license.txt)      //
////////////////////////////////////////////////////////////////////////////

// aiff.c

// This module is a helper to the WavPack command-line programs to support AIFF files

#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#include <limits.h>

#include "wavpack.h"
#include "utils.h"
#include "md5.h"

#pragma pack(push,2)
typedef struct {
    uint16_t numChannels;
    uint32_t numSampleFrames;
    uint16_t sampleSize;
    uint16_t sampleRateExponent;
    uint64_t sampleRateMantissa;
    char compressionType [4];
    char compressionName [512];
} CommonChunk;
#pragma pack(pop)

#define CommonChunkFormat "SLSSD"

typedef struct {
    uint32_t offset;
    uint32_t blockSize;
} SoundChunk;

#define SoundChunkFormat "LL"

extern int debug_logging_mode;

static double get_extended (uint16_t exponent, uint64_t mantissa)
{
    double sign = (exponent & 0x8000) ? -1.0 : 1.0, value = (double) mantissa;
    double scaler = pow (2.0, (exponent & 0x7fff) - 16446);
    return value * scaler * sign;
}

int ParseAiffHeaderConfig (FILE *infile, char *infilename, char *fourcc, WavpackContext *wpc, WavpackConfig *config)
{
    int common_chunks = 0, version_chunks = 0;
    int64_t total_samples = 0, infilesize;
    RiffChunkHeader aiff_chunk_header;
    ChunkHeader chunk_header;
    CommonChunk common_chunk;
    SoundChunk sound_chunk;
    uint32_t bcount;

    CLEAR (common_chunk);
    CLEAR (sound_chunk);
    infilesize = DoGetFileSize (infile);

    if (infilesize >= 4294967296LL && !(config->qmode & QMODE_IGNORE_LENGTH)) {
        error_line ("can't handle .AIF files larger than 4 GB (non-standard)!");
        return WAVPACK_SOFT_ERROR;
    }

    memcpy (&aiff_chunk_header, fourcc, 4);

    if (!DoReadFile (infile, ((char *) &aiff_chunk_header) + 4, sizeof (RiffChunkHeader) - 4, &bcount)  ||
        bcount != sizeof (RiffChunkHeader) - 4                                                          ||
        (strncmp (aiff_chunk_header.formType, "AIFF", 4) && strncmp (aiff_chunk_header.formType, "AIFC", 4))) {
            error_line ("%s is not a valid .AIF file!", infilename);
            return WAVPACK_SOFT_ERROR;
    }
    else if (!(config->qmode & QMODE_NO_STORE_WRAPPER) &&
        !WavpackAddWrapper (wpc, &aiff_chunk_header, sizeof (RiffChunkHeader))) {
            error_line ("%s", WavpackGetErrorMessage (wpc));
            return WAVPACK_SOFT_ERROR;
    }

    if (debug_logging_mode) {
        WavpackBigEndianToNative (&aiff_chunk_header, ChunkHeaderFormat);
        error_line ("file size = %llu, chunk size in AIF%c header = %u", (unsigned long long) infilesize,
            aiff_chunk_header.formType [3], aiff_chunk_header.ckSize);
    }

    // loop through all elements of the AIFF header
    // (until the sound chunck) and copy them to the output file

    while (1) {
        int padded_chunk_size;

        if (!DoReadFile (infile, &chunk_header, sizeof (ChunkHeader), &bcount) ||
            bcount != sizeof (ChunkHeader)) {
                error_line ("%s is not a valid .AIF%c file!", infilename, aiff_chunk_header.formType [3]);
                return WAVPACK_SOFT_ERROR;
        }
        else if (!(config->qmode & QMODE_NO_STORE_WRAPPER) &&
            !WavpackAddWrapper (wpc, &chunk_header, sizeof (ChunkHeader))) {
                error_line ("%s", WavpackGetErrorMessage (wpc));
                return WAVPACK_SOFT_ERROR;
        }

        WavpackBigEndianToNative (&chunk_header, ChunkHeaderFormat);
        padded_chunk_size = (chunk_header.ckSize + 1) & ~1L;

        if (!strncmp (chunk_header.ckID, "FVER", 4)) {          // if it's the format version chunk, don't be too picky
            uint32_t timestamp;

            if (version_chunks++ || padded_chunk_size != sizeof (timestamp)  ||
                !DoReadFile (infile, &timestamp, padded_chunk_size, &bcount) ||
                bcount != sizeof (timestamp)) {
                    error_line ("%s is not a valid .AIF%c file!", infilename, aiff_chunk_header.formType [3]);
                    return WAVPACK_SOFT_ERROR;
            }
            else if (!(config->qmode & QMODE_NO_STORE_WRAPPER) &&
                !WavpackAddWrapper (wpc, &timestamp, padded_chunk_size)) {
                    error_line ("%s", WavpackGetErrorMessage (wpc));
                    return WAVPACK_SOFT_ERROR;
            }

            // WavpackBigEndianToNative (&timestamp, "L");
        }
        else if (!strncmp (chunk_header.ckID, "COMM", 4)) {     // if it's the common chunk, we want to get some info out of there and
            int supported = TRUE, floatData = FALSE;            // make sure it's a .aiff file we can handle
            double sampleRate;

            if (common_chunks++ || padded_chunk_size < 18 || padded_chunk_size > sizeof (common_chunk) ||
                (aiff_chunk_header.formType [3] == 'F' && padded_chunk_size != 18)                     ||
                !DoReadFile (infile, &common_chunk, padded_chunk_size, &bcount)                        ||
                bcount != padded_chunk_size) {
                    error_line ("%s is not a valid .AIF%c file!", infilename, aiff_chunk_header.formType [3]);
                    return WAVPACK_SOFT_ERROR;
            }
            else if (!(config->qmode & QMODE_NO_STORE_WRAPPER) &&
                !WavpackAddWrapper (wpc, &common_chunk, padded_chunk_size)) {
                    error_line ("%s", WavpackGetErrorMessage (wpc));
                    return WAVPACK_SOFT_ERROR;
            }

            WavpackBigEndianToNative (&common_chunk, CommonChunkFormat);
            sampleRate = get_extended (common_chunk.sampleRateExponent, common_chunk.sampleRateMantissa);

            if (debug_logging_mode) {
                error_line ("common tag size = %d", chunk_header.ckSize);
                error_line ("numChannels = %d, numSampleFrames = %u",
                    common_chunk.numChannels, common_chunk.numSampleFrames);
                error_line ("sampleSize = %d, sampleRate = %g", common_chunk.sampleSize, sampleRate);

                if (chunk_header.ckSize >= 22) {
                    error_line ("compressionType = %c%c%c%c",
                        common_chunk.compressionType [0],
                        common_chunk.compressionType [1],
                        common_chunk.compressionType [2],
                        common_chunk.compressionType [3]);

                    if (chunk_header.ckSize >= 24) {
                        int pstring_len = (unsigned char) common_chunk.compressionName [0];

                        if (pstring_len >= 1 && pstring_len <= (int)(chunk_header.ckSize - 23)) {
                            char compressionName [256];
                            int i, j;

                            for (j = i = 0; i < pstring_len; ++i)
                                if (common_chunk.compressionName [i + 1] >= ' ' && common_chunk.compressionName [i + 1] <= '~')
                                    compressionName [j++] = common_chunk.compressionName [i + 1];

                            compressionName [j] = 0;
                            error_line ("compressionName = \"%s\"", compressionName);
                        }
                    }
                }
            }

            if (chunk_header.ckSize < 22)
                config->qmode |= QMODE_BIG_ENDIAN;
            else if (!strncmp (common_chunk.compressionType, "NONE", 4) || !strncmp (common_chunk.compressionType, "none", 4))
                config->qmode |= QMODE_BIG_ENDIAN;
            else if (!strncmp (common_chunk.compressionType, "FL32", 4) || !strncmp (common_chunk.compressionType, "fl32", 4)) {
                config->qmode |= QMODE_BIG_ENDIAN;
                floatData = TRUE;
            }
            else if (strncmp (common_chunk.compressionType, "SOWT", 4) && strncmp (common_chunk.compressionType, "sowt", 4))
                supported = FALSE;

            if (sampleRate <= 0.0 || sampleRate > 16777215.0)
                supported = FALSE;

            if (floatData && common_chunk.sampleSize != 32)
                supported = FALSE;

            if (!common_chunk.numChannels || common_chunk.numChannels > WAVPACK_MAX_CLI_CHANS)
                supported = FALSE;

            if (common_chunk.sampleSize < 1 || common_chunk.sampleSize > 32)
                supported = FALSE;

            if (!supported) {
                error_line ("%s is an unsupported .AIF%c format!", infilename, aiff_chunk_header.formType [3]);
                return WAVPACK_SOFT_ERROR;
            }

            if (sampleRate != floor (sampleRate))
                error_line ("warning: the nonintegral sample rate of %s will be rounded", infilename);

            if (sampleRate < 1.0)
                config->sample_rate = 1;
            else
                config->sample_rate = (int) floor (sampleRate + 0.5);

            config->bytes_per_sample = (common_chunk.sampleSize + 7) / 8;
            config->bits_per_sample = common_chunk.sampleSize;
            config->num_channels = common_chunk.numChannels;

            if ((config->qmode & QMODE_EVEN_BYTE_DEPTH) && (config->bits_per_sample % 8))
                config->bits_per_sample += 8 - (config->bits_per_sample % 8);

            if (!config->channel_mask && !(config->qmode & QMODE_CHANS_UNASSIGNED)) {
                if (common_chunk.numChannels <= 2)
                    config->channel_mask = 0x5 - common_chunk.numChannels;
                else if (common_chunk.numChannels <= 18)
                    config->channel_mask = (1 << common_chunk.numChannels) - 1;
                else
                    config->channel_mask = 0x3ffff;
            }

            if (common_chunk.sampleSize <= 8)
                config->qmode |= QMODE_SIGNED_BYTES;

            if (floatData)
                config->float_norm_exp = 127;

            if (debug_logging_mode) {
                if (config->float_norm_exp == 127)
                    error_line ("data format: 32-bit big-endian floating point");
                else if (config->bytes_per_sample == 1)
                    error_line ("data format: %d-bit signed integers stored in %d byte",
                        config->bits_per_sample, config->bytes_per_sample);
                else
                    error_line ("data format: %d-bit %s-endian integers stored in %d byte(s)",
                        config->bits_per_sample, (config->qmode & QMODE_BIG_ENDIAN) ? "big" : "little", config->bytes_per_sample);
            }
        }
        else if (!strncmp (chunk_header.ckID, "SSND", 4)) {             // on the data chunk, get size and exit loop
            int64_t data_chunk_size;
            int bytes_per_frame;

            if (!common_chunks || chunk_header.ckSize < sizeof (sound_chunk)      ||
             /* (!version_chunks && aiff_chunk_header.formType [3] == 'C')        || */
                !DoReadFile (infile, &sound_chunk, sizeof (sound_chunk), &bcount) ||
                bcount != sizeof (sound_chunk)) {
                    error_line ("%s is not a valid .AIF%c file!", infilename, aiff_chunk_header.formType [3]);
                    return WAVPACK_SOFT_ERROR;
            }
            else if (!(config->qmode & QMODE_NO_STORE_WRAPPER) &&
                !WavpackAddWrapper (wpc, &sound_chunk, sizeof (sound_chunk))) {
                    error_line ("%s", WavpackGetErrorMessage (wpc));
                    return WAVPACK_SOFT_ERROR;
            }

            WavpackBigEndianToNative (&sound_chunk, SoundChunkFormat);

            // The "offset" and "blockSize" fields are normally zero, and we don't handle any kind of crazy
            // alignment rules. However, we CAN handle a simple small "offset" (by adding it to the sound
            // chunk) and a "blockSize" that matches the actual block size (can't complain about that).

            if (sound_chunk.offset > 65536 || (sound_chunk.blockSize != 0 &&
                sound_chunk.blockSize != config->bytes_per_sample * config->num_channels)) {
                    error_line ("%s is an unsupported .AIF%c format!", infilename, aiff_chunk_header.formType [3]);
                    return WAVPACK_SOFT_ERROR;
            }

            if (sound_chunk.offset) {
                unsigned char *offset_data = malloc (sound_chunk.offset);

                if (!DoReadFile (infile, offset_data, sound_chunk.offset, &bcount) || bcount != sound_chunk.offset ||
                    (!(config->qmode & QMODE_NO_STORE_WRAPPER) && !WavpackAddWrapper (wpc, offset_data, sound_chunk.offset))) {
                        error_line ("%s", WavpackGetErrorMessage (wpc));
                        free (offset_data);
                        return WAVPACK_SOFT_ERROR;
                }

                free (offset_data);
            }

            data_chunk_size = chunk_header.ckSize - sizeof (sound_chunk) - sound_chunk.offset;
            bytes_per_frame = config->bytes_per_sample * config->num_channels;

            if (infilesize && !(config->qmode & QMODE_IGNORE_LENGTH) && infilesize - data_chunk_size > 16777216) {
                error_line ("this .AIF file has over 16 MB of extra AIF data, probably is corrupt!");
                return WAVPACK_SOFT_ERROR;
            }

            if (config->qmode & QMODE_IGNORE_LENGTH) {
                if (infilesize && DoGetFilePosition (infile) != -1)
                    total_samples = (infilesize - DoGetFilePosition (infile)) / bytes_per_frame;
                else
                    total_samples = -1;
            }
            else {
                total_samples = data_chunk_size / bytes_per_frame;

                if (total_samples != common_chunk.numSampleFrames) {
                    total_samples = (data_chunk_size + sizeof (sound_chunk)) / bytes_per_frame;

                    if (total_samples != common_chunk.numSampleFrames) {
                        error_line ("%s is not a valid .AIF%c file!", infilename);
                        return WAVPACK_SOFT_ERROR;
                    }
                    else
                        error_line ("warning: %s has a malformed chunk size which will be ignored", infilename);
                }

                if (!total_samples) {
                    error_line ("%s has no audio samples, probably is corrupt!", infilename);
                    return WAVPACK_SOFT_ERROR;
                }

                if (total_samples > MAX_WAVPACK_SAMPLES) {
                    error_line ("%s has too many samples for WavPack!", infilename);
                    return WAVPACK_SOFT_ERROR;
                }
            }

            break;
        }
        else {          // just copy unknown chunks to output file
            char *buff;

            if (padded_chunk_size < 0 || padded_chunk_size > 4194304) {
                error_line ("%s is not a valid .AIF%c file!", infilename, aiff_chunk_header.formType [3]);
                return WAVPACK_SOFT_ERROR;
            }

            buff = malloc (padded_chunk_size);

            if (debug_logging_mode)
                error_line ("extra unknown chunk \"%c%c%c%c\" of %d bytes",
                    chunk_header.ckID [0], chunk_header.ckID [1], chunk_header.ckID [2],
                    chunk_header.ckID [3], chunk_header.ckSize);

            if (!DoReadFile (infile, buff, padded_chunk_size, &bcount) || bcount != padded_chunk_size ||
                (!(config->qmode & QMODE_NO_STORE_WRAPPER) && !WavpackAddWrapper (wpc, buff, padded_chunk_size))) {
                    error_line ("%s", WavpackGetErrorMessage (wpc));
                    free (buff);
                    return WAVPACK_SOFT_ERROR;
            }

            free (buff);
        }
    }

    if (!WavpackSetConfiguration64 (wpc, config, total_samples, NULL)) {
        error_line ("%s: %s", infilename, WavpackGetErrorMessage (wpc));
        return WAVPACK_SOFT_ERROR;
    }

    return WAVPACK_NO_ERROR;
}