File: format_aiff.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 (163 lines) | stat: -rw-r--r-- 4,677 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
/*  format_aiff.c - aiff 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 "format.h"

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

#define SOX "sox"

static char default_decoder_args[] = "-t aiff " FILENAME_PLACEHOLDER " -t wav -";
static char default_encoder_args[] = "-t wav - -t aiff " FILENAME_PLACEHOLDER;

static bool is_our_file(char *);
static bool input_header_kluge(unsigned char *,wave_info *);

format_module format_aiff = {
  "aiff",
  "Audio Interchange File Format",
  CVSIDSTR,
  TRUE,
  TRUE,
  TRUE,
  FALSE,
  TRUE,
  TRUE,
  "-",
  NULL,
  0,
  "aiff",
  SOX,
  default_decoder_args,
  SOX,
  default_encoder_args,
  is_our_file,
  NULL,
  NULL,
  NULL,
  NULL,
  input_header_kluge
};

static bool parse_aiff_header(char *filename,unsigned long *samples,unsigned short *channels,unsigned short *bits_per_sample)
/* generic function to parse an AIFF header and store certain values contained therein */
{
  FILE *f;
  unsigned long be_long = 0;
  unsigned short be_short = 0;
  unsigned char tag[4];
  bool is_compressed = FALSE;

  *samples = 0;
  *channels = 0;
  *bits_per_sample = 0;

  if (NULL == (f = open_input(filename)))
    return FALSE;

  /* look for FORM header */
  if (!read_tag(f,tag) || tagcmp(tag,(unsigned char *)AIFF_FORM))
    goto invalid_aiff_header;

  /* skip FORM chunk size, read in FORM type */
  if (!read_be_long(f,&be_long) || !read_tag(f,tag))
    goto invalid_aiff_header;

  /* if FORM type is not AIFF or AIFC, bail out */
  if (tagcmp(tag,(unsigned char *)AIFF_FORM_TYPE_AIFF) && tagcmp(tag,(unsigned char *)AIFF_FORM_TYPE_AIFC))
    goto invalid_aiff_header;

  if (!tagcmp(tag,(unsigned char *)AIFF_FORM_TYPE_AIFC))
    is_compressed = TRUE;

  /* now let's check AIFC compression type - it's in the COMM chunk */
  while (1) {
    /* read chunk id */
    if (!read_tag(f,tag))
      goto invalid_aiff_header;

    if (!tagcmp(tag,(unsigned char *)AIFF_COMM))
      break;

    /* not COMM, so read size of this chunk and skip it */
    if (!read_be_long(f,&be_long) || fseek(f,(long)be_long,SEEK_CUR))
      goto invalid_aiff_header;
  }

  /* now read channels, samples, and bits/sample from COMM chunk */
  if (!read_be_long(f,&be_long) || !read_be_short(f,channels) ||
      !read_be_long(f,samples) || !read_be_short(f,bits_per_sample) ||
      !read_be_long(f,&be_long) || !read_be_long(f,&be_long) ||
      !read_be_short(f,&be_short))
  {
    goto invalid_aiff_header;
  }

  if (is_compressed) {
    if (!read_tag(f,tag))
      goto invalid_aiff_header;

    if (tagcmp(tag,(unsigned char *)AIFF_COMPRESSION_NONE) && tagcmp(tag,(unsigned char *)AIFF_COMPRESSION_SOWT)) {
      st_debug1("found unsupported AIFF-C compression type [%c%c%c%c]",tag[0],tag[1],tag[2],tag[3]);
      goto invalid_aiff_header;
    }
  }

  fclose(f);
  return TRUE;

invalid_aiff_header:

  fclose(f);
  return FALSE;
}

static bool is_our_file(char *filename)
{
  unsigned long be_long=0;
  unsigned short be_short=0;

  return parse_aiff_header(filename,&be_long,&be_short,&be_short);
}

static bool input_header_kluge(unsigned char *header,wave_info *info)
/* kluge to set data_size and chunk_size, since sox can't fseek() on
 * stdout to backfill these values.  This relies on values in the
 * COMM chunk.
 */
{
  unsigned long samples = 0;
  unsigned short channels = 0,bits_per_sample = 0;

  if (!parse_aiff_header(info->filename,&samples,&channels,&bits_per_sample))
    return FALSE;

  /* set proper data size */
  info->data_size = channels * samples * (bits_per_sample/8);

  st_debug1("adjusting data size to: %lu",info->data_size);

  /* now set chunk size based on data size, and the canonical WAVE header size, which sox generates */
  info->chunk_size = info->data_size + CANONICAL_HEADER_SIZE - 8;
  if (PROB_ODD_SIZED_DATA(info))
    info->chunk_size++;

  make_canonical_header(header,info);

  return TRUE;
}