sox-devel@lists.sourceforge.net unofficial mirror
 help / color / mirror / code / Atom feed
blob 900bb94e0f5a205d78816f196ec7a40f16b611a1 8963 bytes (raw)
name: src/amr.h 	 # note: path name is non-authoritative(*)

  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
 
/* File format: AMR   (c) 2007 robs@users.sourceforge.net
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library 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 Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <string.h>
#include <math.h>

#ifdef AMR_OPENCORE

LSX_DLENTRIES_TO_FUNCTIONS(AMR_OPENCORE_FUNC_ENTRIES)

typedef struct amr_opencore_funcs {
  LSX_DLENTRIES_TO_PTRS(AMR_OPENCORE_FUNC_ENTRIES, amr_dl);
} amr_opencore_funcs;

#endif /* AMR_OPENCORE */

#ifdef AMR_GP3

LSX_DLENTRIES_TO_FUNCTIONS(AMR_GP3_FUNC_ENTRIES)

typedef struct amr_gp3_funcs {
  LSX_DLENTRIES_TO_PTRS(AMR_GP3_FUNC_ENTRIES, amr_dl);
} amr_gp3_funcs;

#endif /* AMR_GP3 */

#if defined(AMR_OPENCORE) && defined (AMR_GP3)
  #define AMR_CALL(p, opencoreFunc, gp3Func, args) \
    ((p)->loaded_opencore ? ((p)->opencore.opencoreFunc args) : ((p)->gp3.gp3Func args))
  #if AMR_OPENCORE_ENABLE_ENCODE
    #define AMR_CALL_ENCODER AMR_CALL
  #else
    #define AMR_CALL_ENCODER(p, opencoreFunc, gp3Func, args) \
      ((p)->gp3.gp3Func args)
  #endif
#elif defined(AMR_OPENCORE)
  #define AMR_CALL(p, opencoreFunc, gp3Func, args) \
    ((p)->opencore.opencoreFunc args)
  #define AMR_CALL_ENCODER AMR_CALL
#elif defined(AMR_GP3)
  #define AMR_CALL(p, opencoreFunc, gp3Func, args) \
    ((p)->gp3.gp3Func args)
  #define AMR_CALL_ENCODER AMR_CALL
#endif

typedef struct amr_priv_t {
  void* state;
  unsigned mode;
  size_t pcm_index;
  int loaded_opencore;
#ifdef AMR_OPENCORE
  amr_opencore_funcs opencore;
#endif /* AMR_OPENCORE */
#ifdef AMR_GP3
  amr_gp3_funcs gp3;
#endif /* AMR_GP3 */
  short pcm[AMR_FRAME];
} priv_t;

static size_t decode_1_frame(sox_format_t * ft)
{
  priv_t * p = (priv_t *)ft->priv;
  size_t n_1;
  uint8_t coded[AMR_CODED_MAX];

  if (lsx_readbuf(ft, &coded[0], (size_t)1) != 1)
    return AMR_FRAME;
  n_1 = amr_block_size[(coded[0] >> 3) & 0x0F] - 1;
  if (lsx_readbuf(ft, &coded[1], n_1) != n_1)
    return AMR_FRAME;
  AMR_CALL(p, AmrOpencoreDecoderDecode, AmrGp3DecoderDecode, (p->state, coded, p->pcm, 0));
  return 0;
}

static int openlibrary(priv_t* p, int encoding)
{
  int open_library_result;

  (void)encoding;
#ifdef AMR_OPENCORE
  if (AMR_OPENCORE_ENABLE_ENCODE || !encoding)
  {
    LSX_DLLIBRARY_TRYOPEN(
      0,
      &p->opencore,
      amr_dl,
      AMR_OPENCORE_FUNC_ENTRIES,
      AMR_OPENCORE_DESC,
      amr_opencore_library_names,
      open_library_result);
    if (!open_library_result)
    {
      p->loaded_opencore = 1;
      return SOX_SUCCESS;
    }
  }
  else
  {
      lsx_report("Not attempting to load " AMR_OPENCORE_DESC " because it does not support encoding.");
  }
#endif /* AMR_OPENCORE */

#ifdef AMR_GP3
  LSX_DLLIBRARY_TRYOPEN(
      0,
      &p->gp3,
      amr_dl,
      AMR_GP3_FUNC_ENTRIES,
      AMR_GP3_DESC,
      amr_gp3_library_names,
      open_library_result);
  if (!open_library_result)
    return SOX_SUCCESS;
#endif /* AMR_GP3 */

  lsx_fail(
      "Unable to open "
#ifdef AMR_OPENCORE
      AMR_OPENCORE_DESC
#endif
#if defined(AMR_OPENCORE) && defined(AMR_GP3)
      " or "
#endif
#ifdef AMR_GP3
      AMR_GP3_DESC
#endif
      ".");
  return SOX_EOF;
}

static void closelibrary(priv_t* p)
{
#ifdef AMR_OPENCORE
  LSX_DLLIBRARY_CLOSE(&p->opencore, amr_dl);
#endif
#ifdef AMR_GP3
  LSX_DLLIBRARY_CLOSE(&p->gp3, amr_dl);
#endif
}

static size_t amr_duration_frames(sox_format_t * ft)
{
  off_t      frame_size, data_start_offset = lsx_tell(ft);
  size_t     frames;
  uint8_t    coded;

  for (frames = 0; lsx_readbuf(ft, &coded, (size_t)1) == 1; ++frames) {
    frame_size = amr_block_size[coded >> 3 & 15];
    if (lsx_seeki(ft, frame_size - 1, SEEK_CUR)) {
      lsx_fail("seek");
      break;
    }
  }
  lsx_debug("frames=%lu", (unsigned long)frames);
  lsx_seeki(ft, data_start_offset, SEEK_SET);
  return frames;
}

static int startread(sox_format_t * ft)
{
  priv_t * p = (priv_t *)ft->priv;
  char buffer[sizeof(amr_magic) - 1];
  int open_library_result;

  if (lsx_readchars(ft, buffer, sizeof(buffer)))
    return SOX_EOF;
  if (memcmp(buffer, amr_magic, sizeof(buffer))) {
    lsx_fail_errno(ft, SOX_EHDR, "invalid magic number");
    return SOX_EOF;
  }

  open_library_result = openlibrary(p, 0);
  if (open_library_result != SOX_SUCCESS)
    return open_library_result;

  p->pcm_index = AMR_FRAME;
  p->state = AMR_CALL(p, AmrOpencoreDecoderInit, AmrGp3DecoderInit, ());
  if (!p->state)
  {
      closelibrary(p);
      lsx_fail("AMR decoder failed to initialize.");
      return SOX_EOF;
  }

  ft->signal.rate = AMR_RATE;
  ft->encoding.encoding = AMR_ENCODING;
  ft->signal.channels = 1;
  ft->signal.length = ft->signal.length != SOX_IGNORE_LENGTH && ft->seekable?
    (size_t)(amr_duration_frames(ft) * .02 * ft->signal.rate +.5) : SOX_UNSPEC;
  return SOX_SUCCESS;
}

static size_t read_samples(sox_format_t * ft, sox_sample_t * buf, size_t len)
{
  priv_t * p = (priv_t *)ft->priv;
  size_t done;

  for (done = 0; done < len; done++) {
    if (p->pcm_index >= AMR_FRAME)
      p->pcm_index = decode_1_frame(ft);
    if (p->pcm_index >= AMR_FRAME)
      break;
    *buf++ = SOX_SIGNED_16BIT_TO_SAMPLE(p->pcm[p->pcm_index++], ft->clips);
  }
  return done;
}

static int stopread(sox_format_t * ft)
{
  priv_t * p = (priv_t *)ft->priv;
  AMR_CALL(p, AmrOpencoreDecoderExit, AmrGp3DecoderExit, (p->state));
  closelibrary(p);
  return SOX_SUCCESS;
}

static int startwrite(sox_format_t * ft)
{
#if !defined(AMR_GP3) && !AMR_OPENCORE_ENABLE_ENCODE
  lsx_fail_errno(ft, SOX_EOF, "SoX was compiled without AMR-WB encoding support.");
  return SOX_EOF;
#else
  priv_t * p = (priv_t *)ft->priv;
  int open_library_result;

  if (ft->encoding.compression != HUGE_VAL) {
    p->mode = (unsigned)ft->encoding.compression;
    if (p->mode != ft->encoding.compression || p->mode > AMR_MODE_MAX) {
      lsx_fail_errno(ft, SOX_EINVAL, "compression level must be a whole number from 0 to %i", AMR_MODE_MAX);
      return SOX_EOF;
    }
  }
  else p->mode = 0;

  open_library_result = openlibrary(p, 1);
  if (open_library_result != SOX_SUCCESS)
    return open_library_result;

#define IGNORE_WARNING \
  p->state = AMR_CALL_ENCODER(p, AmrOpencoreEncoderInit, AmrGp3EncoderInit, ());
#include "ignore-warning.h"
  if (!p->state)
  {
      closelibrary(p);
      lsx_fail("AMR encoder failed to initialize.");
      return SOX_EOF;
  }

  lsx_writes(ft, amr_magic);
  p->pcm_index = 0;
  return SOX_SUCCESS;
#endif
}

#if defined(AMR_GP3) || AMR_OPENCORE_ENABLE_ENCODE

static sox_bool encode_1_frame(sox_format_t * ft)
{
  priv_t * p = (priv_t *)ft->priv;
  uint8_t coded[AMR_CODED_MAX];
#define IGNORE_WARNING \
  int n = AMR_CALL_ENCODER(p, AmrOpencoreEncoderEncode, AmrGp3EncoderEncode, (p->state, p->mode, p->pcm, coded, 1));
#include "ignore-warning.h"
  sox_bool result = lsx_writebuf(ft, coded, (size_t) (size_t) (unsigned)n) == (unsigned)n;
  if (!result)
    lsx_fail_errno(ft, errno, "write error");
  return result;
}

static size_t write_samples(sox_format_t * ft, const sox_sample_t * buf, size_t len)
{
  priv_t * p = (priv_t *)ft->priv;
  size_t done;

  for (done = 0; done < len; ++done) {
    SOX_SAMPLE_LOCALS;
    p->pcm[p->pcm_index++] = SOX_SAMPLE_TO_SIGNED_16BIT(*buf++, ft->clips);
    if (p->pcm_index == AMR_FRAME) {
      p->pcm_index = 0;
      if (!encode_1_frame(ft))
        return 0;
    }
  }
  return done;
}

static int stopwrite(sox_format_t * ft)
{
  priv_t * p = (priv_t *)ft->priv;
  int result = SOX_SUCCESS;

  if (p->pcm_index) {
    do {
      p->pcm[p->pcm_index++] = 0;
    } while (p->pcm_index < AMR_FRAME);
    if (!encode_1_frame(ft))
      result = SOX_EOF;
  }
  AMR_CALL_ENCODER(p, AmrOpencoreEncoderExit, AmrGp3EncoderExit, (p->state));
  return result;
}

#else

#define write_samples NULL
#define stopwrite NULL

#endif /* defined(AMR_GP3) || AMR_OPENCORE_ENABLE_ENCODE */

sox_format_handler_t const * AMR_FORMAT_FN(void);
sox_format_handler_t const * AMR_FORMAT_FN(void)
{
  static char const * const names[] = {AMR_NAMES, NULL};
  static sox_rate_t   const write_rates[] = {AMR_RATE, 0};
  static unsigned const write_encodings[] = {AMR_ENCODING, 0, 0};
  static sox_format_handler_t handler = {
    SOX_LIB_VERSION_CODE,
    AMR_DESC,
    names, SOX_FILE_MONO,
    startread, read_samples, stopread,
    startwrite, write_samples, stopwrite,
    NULL, write_encodings, write_rates, sizeof(priv_t)
  };
  return &handler;
}

debug log:

solving 900bb94e0f5a ...
found 900bb94e0f5a in https://80x24.org/mirrors/sox.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/sox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).