File: format_wv.c

package info (click to toggle)
shntool 3.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,220 kB
  • ctags: 1,118
  • sloc: ansic: 8,515; sh: 3,761; makefile: 36
file content (322 lines) | stat: -rw-r--r-- 7,773 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
/*  format_wv.c - wavpack format module
 *  Copyright (C) 2000-2009  Jason Jordan <shnutils@freeshell.org>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "format.h"

CVSID("$Id: format_wv.c,v 1.72 2009/03/11 17:18:01 jason Exp $")

#define WAVPACK  "wavpack"
#define WVUNPACK "wvunpack"

#define WAVPACK_MAGIC "wvpk"
#define WV_COMMON_HEADER_SIZE 10

#ifdef WIN32
static char default_decoder_args[] = "-q -y " FILENAME_PLACEHOLDER " -";
static char default_encoder_args[] = "-q -y - " FILENAME_PLACEHOLDER;
#else
static char default_decoder_args[] = "-q -y " FILENAME_PLACEHOLDER " -o -";
static char default_encoder_args[] = "-q -y - -o " FILENAME_PLACEHOLDER;
#endif

/* definitions for version 3 and older */
#define NEW_HIGH_FLAG        0x400  /* new high quality mode (lossless only) */
#define WavpackHeader3Format "4LSSSSLLL4L"

typedef struct _WavpackHeader3 {
  char ckID[4];
  int ckSize;
  short version;
  short bits;
  short flags, shift;
  int total_samples, crc, crc2;
  char extension[4], extra_bc, extras[3];
} WavpackHeader3;

/* definitions for version 4 */
#define HYBRID_FLAG          8
#define ID_WVC_BITSTREAM  0xb  /* these metadata identify .wvc */
#define ID_SHAPING_WEIGHTS  0x7
#define WavpackHeader4Format "4LS2LLLLL"

typedef struct _WavpackHeader4 {
  char ckID[4];
  int ckSize;
  short version;
  unsigned char track_no, index_no;
  int total_samples, block_index, block_samples, flags, crc;
} WavpackHeader4;

static bool is_our_file(char *);

format_module format_wv = {
  "wv",
  "WavPack Hybrid Lossless Audio Compression",
  CVSIDSTR,
  TRUE,
  TRUE,
  FALSE,
  TRUE,
  TRUE,
  FALSE,
  NULL,
  NULL,
  0,
  "wv",
  WVUNPACK,
  default_decoder_args,
  WAVPACK,
  default_encoder_args,
  is_our_file,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL
};

static char *filespec_ext(char *filespec)
{
  char *cp = filespec + strlen(filespec);

  while (--cp >= filespec) {
    if (*cp == '\\' || *cp == ':')
      return NULL;

    if (*cp == '.') {
      if (strlen (cp) > 1 && strlen (cp) <= 4)
        return cp;
      else
        return NULL;
    }
  }

  return NULL;
}

static void little_endian_to_native (void *data, char *format)
{
  unsigned char *cp = (unsigned char *) data;
  int temp;

  while (*format) {
    switch (*format) {
      case 'L':
        temp = cp [0] + (cp [1] << 8) + ((int) cp [2] << 16) + ((int) cp [3] << 24);
        * (int *) cp = temp;
        cp += 4;
        break;

      case 'S':
        temp = cp [0] + (cp [1] << 8);
        * (short *) cp = temp;
        cp += 2;
        break;

      default:
        if (isdigit (*format))
          cp += *format - '0';
        break;
    }

    format++;
  }
}

static bool file_exists_with_alternate_extension(char *filename,char *ext)
{
  char wvc_filename[FILENAME_SIZE];
  char *extp;
  FILE *f;

  strcpy(wvc_filename,filename);

  extp = filespec_ext(wvc_filename);

  if (extp)
    *extp = 0;

  strcat(wvc_filename,ext);

  if ((f = fopen(wvc_filename,"rb"))) {
    fclose(f);
    return TRUE;
  }

  return FALSE;
}

static long get_header_offset(FILE *f)
{
  unsigned char buf[4];
  long curpos;

  buf[0] = 'x';
  buf[1] = 'x';
  buf[2] = 'x';
  buf[3] = 'x';

  /* like WavPack, we check the first 1 meg of the file for a header. */
  /* unlike WavPack, we do it in the most inefficient way possible.   */

  for (curpos=0;curpos<1024*1024;curpos++) {
    buf[0] = buf[1];
    buf[1] = buf[2];
    buf[2] = buf[3];
    buf[3] = fgetc(f);

    if (feof(f)) {
      return -1;
    }

    if (!tagcmp(buf,(unsigned char *)WAVPACK_MAGIC)) {
      return curpos - 3;
    }
  }

  return -1;
}

static bool is_our_file(char *filename)
{
  wave_info *info;
  unsigned char wph[64];
  WavpackHeader3 *wph3;
  WavpackHeader4 *wph4;
  char first_id;
  int remaining_bytes;
  long header_offset;

  if (NULL == (info = new_wave_info(NULL)))
    st_error("could not allocate memory for WAVE info in wv check");

  info->filename = filename;

  if (NULL == (info->input = open_input(filename))) {
    st_free(info);
    return FALSE;
  }

  if (-1 == (header_offset = get_header_offset(info->input))) {
    fclose(info->input);
    st_free(info);
    return FALSE;
  }

  fseek(info->input,header_offset,SEEK_SET);

  /* read up to size of largest header, making sure we read enough to fill the smallest header */
  memset((void *)wph,0,64);

  if (fread(&wph,1,WV_COMMON_HEADER_SIZE,info->input) != WV_COMMON_HEADER_SIZE) {
    fclose(info->input);
    st_free(info);
    return FALSE;
  }

  if (wph[9] >= 4) {
    /* we're dealing with a version 4+ file */

    remaining_bytes = sizeof(WavpackHeader4) - WV_COMMON_HEADER_SIZE;
    if (fread(wph+WV_COMMON_HEADER_SIZE,1,remaining_bytes,info->input) != remaining_bytes) {
      fclose(info->input);
      st_free(info);
      return FALSE;
    }

    first_id = getc(info->input) & 0x1f;

    fclose(info->input);
    st_free(info);

    wph4 = (WavpackHeader4 *)wph;

    little_endian_to_native(wph4,WavpackHeader4Format);

    if (tagcmp((unsigned char *)wph4->ckID,(unsigned char *)WAVPACK_MAGIC) || wph4->version < 4 || wph4->version > 0x40f) {
      return FALSE;
    }

    st_debug1("examining version >=4 (%d) Wavpack file: [%s]",wph4->version,filename);

    if (wph4->block_samples) {
      if ((ID_WVC_BITSTREAM == first_id) || (ID_SHAPING_WEIGHTS == first_id)) {
        /* these ids belong to the correction file */
        st_debug1("this appears to be a hybrid correction file -- skipping.");
        return FALSE;
      }

      if (wph4->flags & HYBRID_FLAG) {
        /* hybrid */
        if (file_exists_with_alternate_extension(filename,".wvc") || file_exists_with_alternate_extension(filename,".WVC"))
          return TRUE;

        /* lossy */
        st_warning("encountered lossy WavPack file: [%s]",filename);
        return TRUE;
      }
    }

    /* lossless */
    return TRUE;
  }

  /* we're dealing with an older file */

  remaining_bytes = sizeof(WavpackHeader3) - WV_COMMON_HEADER_SIZE;
  if (fread(wph+WV_COMMON_HEADER_SIZE,1,remaining_bytes,info->input) != remaining_bytes) {
    fclose(info->input);
    st_free(info);
    return FALSE;
  }

  fclose(info->input);
  st_free(info);

  wph3 = (WavpackHeader3 *)wph;

  little_endian_to_native(wph3,WavpackHeader3Format);

  if (tagcmp((unsigned char *)wph3->ckID,(unsigned char *)WAVPACK_MAGIC) || wph3->version < 1 || wph3->version > 3) {
    return FALSE;
  }

  st_debug1("examining version <=3 (%d) Wavpack file: [%s]",wph3->version,filename);

  /* lossy */
  if (wph3->version == 3 && wph3->bits && (wph3->flags & NEW_HIGH_FLAG) && wph3->crc != wph3->crc2) {
    st_warning("encountered lossy WavPack file: [%s]",filename);
    return TRUE;
  }

  if (wph3->bits) {
    /* hybrid */
    if (file_exists_with_alternate_extension(filename,".wvc") || file_exists_with_alternate_extension(filename,".WVC"))
      return TRUE;

    /* lossy */
    st_warning("encountered lossy WavPack file: [%s]",filename);
    return TRUE;
  }

  /* lossless */
  return TRUE;
}