Coverage Report

Created: 2025-07-12 06:10

/src/libsndfile/src/alac.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** Copyright (C) 2011-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
3
**
4
** This program is free software; you can redistribute it and/or modify
5
** it under the terms of the GNU Lesser General Public License as published by
6
** the Free Software Foundation; either version 2.1 of the License, or
7
** (at your option) any later version.
8
**
9
** This program is distributed in the hope that it will be useful,
10
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
** GNU Lesser General Public License for more details.
13
**
14
** You should have received a copy of the GNU Lesser General Public License
15
** along with this program; if not, write to the Free Software
16
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
*/
18
19
#include  "sfconfig.h"
20
21
#include  <stdio.h>
22
#include  <stdlib.h>
23
#include  <string.h>
24
#include  <math.h>
25
#include  <errno.h>
26
27
#include  "sndfile.h"
28
#include  "sfendian.h"
29
#include  "common.h"
30
#include  "ALAC/alac_codec.h"
31
#include  "ALAC/ALACBitUtilities.h"
32
33
1.03k
#define   ALAC_MAX_FRAME_SIZE   8192
34
#define   ALAC_BYTE_BUFFER_SIZE 0x20000
35
#define   ALAC_MAX_CHANNEL_COUNT  8 // Same as kALACMaxChannels in /ALACAudioTypes.h
36
37
typedef struct
38
{ uint32_t  current, count, allocated ;
39
  uint32_t  packet_size [] ;
40
} PAKT_INFO ;
41
42
typedef struct
43
{ sf_count_t  input_data_pos ;
44
45
  PAKT_INFO * pakt_info ;
46
47
  int     channels, final_write_block ;
48
49
  uint32_t  frames_this_block, partial_block_frames, frames_per_block ;
50
  uint32_t  bits_per_sample, kuki_size ;
51
52
53
  /* Can't have a decoder and an encoder at the same time so stick
54
  ** them in a union.
55
  */
56
  union
57
  { ALAC_DECODER decoder ;
58
    ALAC_ENCODER encoder ;
59
  } u ;
60
61
  char enctmpname [512] ;
62
  FILE *enctmp ;
63
64
  uint8_t byte_buffer [ALAC_MAX_CHANNEL_COUNT * ALAC_BYTE_BUFFER_SIZE] ;
65
66
  int buffer  [] ;
67
68
} ALAC_PRIVATE ;
69
70
/*============================================================================================
71
*/
72
73
static int alac_reader_init (SF_PRIVATE *psf, const ALAC_DECODER_INFO * info) ;
74
static int alac_writer_init (SF_PRIVATE *psf) ;
75
76
static sf_count_t alac_reader_calc_frames (SF_PRIVATE *psf, ALAC_PRIVATE *plac) ;
77
78
static sf_count_t alac_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
79
static sf_count_t alac_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
80
static sf_count_t alac_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
81
static sf_count_t alac_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
82
83
static sf_count_t alac_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
84
static sf_count_t alac_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
85
static sf_count_t alac_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
86
static sf_count_t alac_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
87
88
static sf_count_t alac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
89
90
static int  alac_close    (SF_PRIVATE *psf) ;
91
static int  alac_byterate (SF_PRIVATE *psf) ;
92
93
static int alac_decode_block (SF_PRIVATE *psf, ALAC_PRIVATE *plac) ;
94
static int alac_encode_block (ALAC_PRIVATE *plac) ;
95
96
static uint32_t alac_kuki_read (SF_PRIVATE * psf, uint32_t kuki_offset, uint8_t * kuki, size_t kuki_maxlen) ;
97
98
static PAKT_INFO * alac_pakt_alloc (uint32_t initial_count) ;
99
static PAKT_INFO * alac_pakt_read_decode (SF_PRIVATE * psf, uint32_t pakt_offset) ;
100
static PAKT_INFO * alac_pakt_append (PAKT_INFO * info, uint32_t value) ;
101
static uint8_t * alac_pakt_encode (const SF_PRIVATE *psf, uint32_t * pakt_size) ;
102
static sf_count_t alac_pakt_block_offset (const PAKT_INFO *info, uint32_t block) ;
103
104
static const char * alac_error_string (int error) ;
105
106
/*============================================================================================
107
** ALAC Reader initialisation function.
108
*/
109
110
int
111
alac_init (SF_PRIVATE *psf, const ALAC_DECODER_INFO * info)
112
1.03k
{ int error ;
113
114
1.03k
  if ((psf->codec_data = calloc (1, sizeof (ALAC_PRIVATE) + psf->sf.channels * sizeof (int) * ALAC_MAX_FRAME_SIZE)) == NULL)
115
0
    return SFE_MALLOC_FAILED ;
116
117
1.03k
  psf->codec_close = alac_close ;
118
119
1.03k
  switch (psf->file.mode)
120
1.03k
  { case SFM_RDWR :
121
0
      return SFE_BAD_MODE_RW ;
122
123
1.03k
    case SFM_READ :
124
1.03k
      if ((error = alac_reader_init (psf, info)))
125
176
        return error ;
126
857
      break ;
127
128
857
    case SFM_WRITE :
129
0
      if ((error = alac_writer_init (psf)))
130
0
        return error ;
131
0
      break ;
132
133
0
    default :
134
0
      psf_log_printf (psf, "%s : Bad psf->file.mode.\n", __func__) ;
135
0
      return SFE_INTERNAL ;
136
1.03k
    } ;
137
138
857
  psf->byterate = alac_byterate ;
139
140
857
  return 0 ;
141
1.03k
} /* aiff_alac_init */
142
143
void
144
alac_get_desc_chunk_items (int subformat, uint32_t *fmt_flags, uint32_t *frames_per_packet)
145
0
{ switch (subformat)
146
0
  { case SF_FORMAT_ALAC_16 :
147
0
      *fmt_flags = 1 ;
148
0
      break ;
149
0
    case SF_FORMAT_ALAC_20 :
150
0
      *fmt_flags = 2 ;
151
0
      break ;
152
0
    case SF_FORMAT_ALAC_24 :
153
0
      *fmt_flags = 3 ;
154
0
      break ;
155
0
    case SF_FORMAT_ALAC_32 :
156
0
      *fmt_flags = 4 ;
157
0
      break ;
158
0
    default :
159
0
      break ;
160
0
    } ;
161
0
  *frames_per_packet = ALAC_FRAME_LENGTH ;
162
0
} /* alac_get_desc_chunk_items */
163
164
static int
165
alac_close  (SF_PRIVATE *psf)
166
1.03k
{ ALAC_PRIVATE *plac ;
167
1.03k
  BUF_UNION ubuf ;
168
169
1.03k
  plac = psf->codec_data ;
170
171
1.03k
  if (psf->file.mode == SFM_WRITE)
172
0
  { ALAC_ENCODER *penc = &plac->u.encoder ;
173
0
    SF_CHUNK_INFO chunk_info ;
174
0
    sf_count_t readcount ;
175
0
    uint8_t kuki_data [1024] ;
176
0
    uint32_t pakt_size = 0, saved_partial_block_frames ;
177
178
0
    plac->final_write_block = 1 ;
179
0
    saved_partial_block_frames = plac->partial_block_frames ;
180
181
    /*  If a block has been partially assembled, write it out as the final block. */
182
0
    if (plac->partial_block_frames && plac->partial_block_frames < plac->frames_per_block)
183
0
      alac_encode_block (plac) ;
184
185
0
    plac->partial_block_frames = saved_partial_block_frames ;
186
187
0
    alac_get_magic_cookie (penc, kuki_data, &plac->kuki_size) ;
188
189
0
    memset (&chunk_info, 0, sizeof (chunk_info)) ;
190
0
    chunk_info.id_size = snprintf (chunk_info.id, sizeof (chunk_info.id), "kuki") ;
191
0
    chunk_info.data = kuki_data ;
192
0
    chunk_info.datalen = plac->kuki_size ;
193
0
    psf_save_write_chunk (&psf->wchunks, &chunk_info) ;
194
195
0
    memset (&chunk_info, 0, sizeof (chunk_info)) ;
196
0
    chunk_info.id_size = snprintf (chunk_info.id, sizeof (chunk_info.id), "pakt") ;
197
0
    chunk_info.data = alac_pakt_encode (psf, &pakt_size) ;
198
0
    chunk_info.datalen = pakt_size ;
199
0
    psf_save_write_chunk (&psf->wchunks, &chunk_info) ;
200
201
0
    free (chunk_info.data) ;
202
0
    chunk_info.data = NULL ;
203
204
0
    psf->write_header (psf, 1) ;
205
206
0
    if (plac->enctmp != NULL)
207
0
    { fseek (plac->enctmp, 0, SEEK_SET) ;
208
209
0
      while ((readcount = fread (ubuf.ucbuf, 1, sizeof (ubuf.ucbuf), plac->enctmp)) > 0)
210
0
        psf_fwrite (ubuf.ucbuf, 1, readcount, psf) ;
211
0
      fclose (plac->enctmp) ;
212
0
      remove (plac->enctmpname) ;
213
0
      } ;
214
0
    } ;
215
216
1.03k
  if (plac->pakt_info)
217
956
    free (plac->pakt_info) ;
218
1.03k
  plac->pakt_info = NULL ;
219
220
1.03k
  return 0 ;
221
1.03k
} /* alac_close */
222
223
static int
224
alac_byterate (SF_PRIVATE *psf)
225
0
{
226
0
  if (psf->file.mode == SFM_READ)
227
0
    return (psf->datalength * psf->sf.samplerate) / psf->sf.frames ;
228
229
0
  return -1 ;
230
0
} /* alac_byterate */
231
232
/*============================================================================================
233
** ALAC initialisation Functions.
234
*/
235
236
static int
237
alac_reader_init (SF_PRIVATE *psf, const ALAC_DECODER_INFO * info)
238
1.03k
{ ALAC_PRIVATE  *plac ;
239
1.03k
  uint32_t    kuki_size ;
240
1.03k
  int       error ;
241
1.03k
  union     { uint8_t kuki [512] ; uint32_t alignment ; } u ;
242
243
1.03k
  if (info == NULL)
244
0
  { psf_log_printf (psf, "%s : ALAC_DECODER_INFO is NULL.\n", __func__) ;
245
0
    return SFE_INTERNAL ;
246
1.03k
    } ;
247
248
1.03k
  if (info->frames_per_packet > ALAC_FRAME_LENGTH)
249
10
  { psf_log_printf (psf, "*** Error : frames_per_packet (%u) is too big. ***\n", info->frames_per_packet) ;
250
10
    return SFE_INTERNAL ;
251
1.02k
    } ;
252
253
1.02k
  plac = psf->codec_data ;
254
255
1.02k
  plac->channels      = psf->sf.channels ;
256
1.02k
  plac->frames_per_block  = info->frames_per_packet ;
257
1.02k
  plac->bits_per_sample = info->bits_per_sample ;
258
259
1.02k
  if (plac->pakt_info != NULL)
260
0
    free (plac->pakt_info) ;
261
1.02k
  plac->pakt_info = alac_pakt_read_decode (psf, info->pakt_offset) ;
262
263
1.02k
  if (plac->pakt_info == NULL)
264
67
  { psf_log_printf (psf, "%s : alac_pkt_read() returns NULL.\n", __func__) ;
265
67
    return SFE_INTERNAL ;
266
956
    } ;
267
268
  /* Read in the ALAC cookie data and pass it to the init function. */
269
956
  kuki_size = alac_kuki_read (psf, info->kuki_offset, u.kuki, sizeof (u.kuki)) ;
270
271
956
  if ((error = alac_decoder_init (&plac->u.decoder, u.kuki, kuki_size)) != ALAC_noErr)
272
97
  { psf_log_printf (psf, "*** alac_decoder_init() returned %s. ***\n", alac_error_string (error)) ;
273
97
    return SFE_INTERNAL ;
274
859
    } ;
275
276
277
859
  if (plac->u.decoder.mNumChannels != (unsigned) psf->sf.channels)
278
2
  { psf_log_printf (psf, "*** Initialized decoder has %u channels, but it should be %d. ***\n", plac->u.decoder.mNumChannels, psf->sf.channels) ;
279
2
    return SFE_INTERNAL ;
280
857
    } ;
281
282
857
  switch (info->bits_per_sample)
283
857
  { case 16 :
284
642
    case 20 :
285
723
    case 24 :
286
857
    case 32 :
287
857
      psf->read_short   = alac_read_s ;
288
857
      psf->read_int   = alac_read_i ;
289
857
      psf->read_float   = alac_read_f ;
290
857
      psf->read_double  = alac_read_d ;
291
857
      break ;
292
293
0
    default :
294
0
      printf ("%s : info->bits_per_sample %u\n", __func__, info->bits_per_sample) ;
295
0
      return SFE_UNSUPPORTED_ENCODING ;
296
857
    } ;
297
298
857
  psf->codec_close  = alac_close ;
299
857
  psf->seek     = alac_seek ;
300
301
857
  psf->sf.frames    = alac_reader_calc_frames (psf, plac) ;
302
857
  alac_seek (psf, SFM_READ, 0) ;
303
304
857
  return 0 ;
305
857
} /* alac_reader_init */
306
307
static int
308
alac_writer_init (SF_PRIVATE *psf)
309
0
{ ALAC_PRIVATE  *plac ;
310
0
  uint32_t    alac_format_flags = 0 ;
311
312
0
  plac = psf->codec_data ;
313
314
0
  if (psf->file.mode != SFM_WRITE)
315
0
    return SFE_BAD_MODE_RW ;
316
317
0
  plac->channels  = psf->sf.channels ;
318
0
  plac->kuki_size = alac_get_magic_cookie_size (psf->sf.channels) ;
319
320
0
  psf->write_short  = alac_write_s ;
321
0
  psf->write_int    = alac_write_i ;
322
0
  psf->write_float  = alac_write_f ;
323
0
  psf->write_double = alac_write_d ;
324
325
0
  switch (SF_CODEC (psf->sf.format))
326
0
  { case SF_FORMAT_ALAC_16 :
327
0
      alac_format_flags = 1 ;
328
0
      plac->bits_per_sample = 16 ;
329
0
      break ;
330
331
0
    case SF_FORMAT_ALAC_20 :
332
0
      alac_format_flags = 2 ;
333
0
      plac->bits_per_sample = 20 ;
334
0
      break ;
335
336
0
    case SF_FORMAT_ALAC_24 :
337
0
      alac_format_flags = 3 ;
338
0
      plac->bits_per_sample = 24 ;
339
0
      break ;
340
341
0
    case SF_FORMAT_ALAC_32 :
342
0
      alac_format_flags = 4 ;
343
0
      plac->bits_per_sample = 32 ;
344
0
      break ;
345
346
0
    default :
347
0
      psf_log_printf (psf, "%s : Can't figure out bits per sample.\n", __func__) ;
348
0
      return SFE_UNIMPLEMENTED ;
349
0
    } ;
350
351
0
  plac->frames_per_block = ALAC_FRAME_LENGTH ;
352
353
0
  plac->pakt_info = alac_pakt_alloc (2000) ;
354
355
0
  if ((plac->enctmp = psf_open_tmpfile (plac->enctmpname, sizeof (plac->enctmpname))) == NULL)
356
0
  { psf_log_printf (psf, "Error : Failed to open temp file '%s' : \n", plac->enctmpname, strerror (errno)) ;
357
0
    return SFE_ALAC_FAIL_TMPFILE ;
358
0
    } ;
359
360
0
  alac_encoder_init (&plac->u.encoder, psf->sf.samplerate, psf->sf.channels, alac_format_flags, ALAC_FRAME_LENGTH) ;
361
362
0
  return 0 ;
363
0
} /* alac_writer_init */
364
365
/*============================================================================================
366
** ALAC block decoder and encoder.
367
*/
368
369
static inline uint32_t
370
alac_reader_next_packet_size (PAKT_INFO * info)
371
100k
{ if (info->current >= info->count)
372
306
    return 0 ;
373
100k
  return info->packet_size [info->current++] ;
374
100k
} /* alac_reader_next_packet_size */
375
376
static sf_count_t
377
alac_reader_calc_frames (SF_PRIVATE *psf, ALAC_PRIVATE *plac)
378
857
{ sf_count_t  frames = 0 ;
379
857
  uint32_t  current_pos = 1, blocks = 0 ;
380
381
857
  plac->pakt_info->current = 0 ;
382
383
53.5k
  while (current_pos < psf->filelength && current_pos > 0)
384
52.6k
  { current_pos = alac_reader_next_packet_size (plac->pakt_info) ;
385
52.6k
    blocks = current_pos > 0 ? blocks + 1 : blocks ;
386
52.6k
    } ;
387
388
857
  if (blocks == 0)
389
1
    return 0 ;
390
391
  /* Only count full blocks. */
392
856
  frames = plac->frames_per_block * (blocks - 1) ;
393
394
856
  alac_seek (psf, SFM_READ, frames) ;
395
856
  alac_decode_block (psf, plac) ;
396
856
  frames += plac->frames_this_block ;
397
398
856
  plac->pakt_info->current = 0 ;
399
400
856
  return frames ;
401
857
} /* alac_reader_calc_frames */
402
403
static int
404
alac_decode_block (SF_PRIVATE *psf, ALAC_PRIVATE *plac)
405
48.0k
{ ALAC_DECODER *pdec = &plac->u.decoder ;
406
48.0k
  uint32_t  packet_size ;
407
48.0k
  BitBuffer bit_buffer ;
408
409
48.0k
  packet_size = alac_reader_next_packet_size (plac->pakt_info) ;
410
48.0k
  if (packet_size == 0)
411
1.00k
  { if (plac->pakt_info->current < plac->pakt_info->count)
412
0
      psf_log_printf (psf, "packet_size is 0 (%d of %d)\n", plac->pakt_info->current, plac->pakt_info->count) ;
413
1.00k
    return 0 ;
414
46.9k
    } ;
415
416
46.9k
  psf_fseek (psf, plac->input_data_pos, SEEK_SET) ;
417
418
46.9k
  if (packet_size > sizeof (plac->byte_buffer))
419
311
  { psf_log_printf (psf, "%s : bad packet_size (%u)\n", __func__, packet_size) ;
420
311
    return 0 ;
421
46.6k
    } ;
422
423
46.6k
  if ((packet_size != psf_fread (plac->byte_buffer, 1, packet_size, psf)))
424
2.88k
    return 0 ;
425
426
43.8k
  BitBufferInit (&bit_buffer, plac->byte_buffer, packet_size) ;
427
428
43.8k
  plac->input_data_pos += packet_size ;
429
43.8k
  plac->frames_this_block = 0 ;
430
43.8k
  alac_decode (pdec, &bit_buffer, plac->buffer, plac->frames_per_block, &plac->frames_this_block) ;
431
432
43.8k
  plac->partial_block_frames = 0 ;
433
434
43.8k
  return 1 ;
435
46.6k
} /* alac_decode_block */
436
437
438
static int
439
alac_encode_block (ALAC_PRIVATE *plac)
440
0
{ ALAC_ENCODER *penc = &plac->u.encoder ;
441
0
  uint32_t num_bytes = 0 ;
442
443
0
  alac_encode (penc, plac->partial_block_frames, plac->buffer, plac->byte_buffer, &num_bytes) ;
444
445
0
  if (fwrite (plac->byte_buffer, 1, num_bytes, plac->enctmp) != num_bytes)
446
0
    return 0 ;
447
0
  if ((plac->pakt_info = alac_pakt_append (plac->pakt_info, num_bytes)) == NULL)
448
0
    return 0 ;
449
450
0
  plac->partial_block_frames = 0 ;
451
452
0
  return 1 ;
453
0
} /* alac_encode_block */
454
455
/*============================================================================================
456
** ALAC read functions.
457
*/
458
459
static sf_count_t
460
alac_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
461
14.8k
{ ALAC_PRIVATE *plac ;
462
14.8k
  int     *iptr ;
463
14.8k
  int     k, readcount ;
464
14.8k
  sf_count_t  total = 0 ;
465
466
14.8k
  if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL)
467
0
    return 0 ;
468
469
47.4k
  while (len > 0)
470
33.3k
  { if (plac->partial_block_frames >= plac->frames_this_block && alac_decode_block (psf, plac) == 0)
471
760
      break ;
472
473
32.5k
    readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ;
474
32.5k
    readcount = readcount > len ? (int) len : readcount ;
475
476
32.5k
    iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
477
478
7.64M
    for (k = 0 ; k < readcount ; k++)
479
7.60M
      ptr [total + k] = iptr [k] >> 16 ;
480
481
32.5k
    plac->partial_block_frames += readcount / plac->channels ;
482
32.5k
    total += readcount ;
483
32.5k
    len -= readcount ;
484
32.5k
    } ;
485
486
14.8k
  return total ;
487
14.8k
} /* alac_read_s */
488
489
static sf_count_t
490
alac_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
491
13.5k
{ ALAC_PRIVATE *plac ;
492
13.5k
  int     *iptr ;
493
13.5k
  int     k, readcount ;
494
13.5k
  sf_count_t  total = 0 ;
495
496
13.5k
  if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL)
497
0
    return 0 ;
498
499
37.0k
  while (len > 0)
500
25.0k
  { if (plac->partial_block_frames >= plac->frames_this_block && alac_decode_block (psf, plac) == 0)
501
1.51k
      break ;
502
503
23.5k
    readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ;
504
23.5k
    readcount = readcount > len ? (int) len : readcount ;
505
506
23.5k
    iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
507
508
21.5M
    for (k = 0 ; k < readcount ; k++)
509
21.5M
      ptr [total + k] = iptr [k] ;
510
511
23.5k
    plac->partial_block_frames += readcount / plac->channels ;
512
23.5k
    total += readcount ;
513
23.5k
    len -= readcount ;
514
23.5k
    } ;
515
516
13.5k
  return total ;
517
13.5k
} /* alac_read_i */
518
519
static sf_count_t
520
alac_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
521
0
{ ALAC_PRIVATE *plac ;
522
0
  int     *iptr ;
523
0
  int     k, readcount ;
524
0
  sf_count_t  total = 0 ;
525
0
  float   normfact ;
526
527
0
  if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL)
528
0
    return 0 ;
529
530
0
  normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
531
532
0
  while (len > 0)
533
0
  { if (plac->partial_block_frames >= plac->frames_this_block && alac_decode_block (psf, plac) == 0)
534
0
      break ;
535
536
0
    readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ;
537
0
    readcount = readcount > len ? (int) len : readcount ;
538
539
0
    iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
540
541
0
    for (k = 0 ; k < readcount ; k++)
542
0
      ptr [total + k] = normfact * iptr [k] ;
543
544
0
    plac->partial_block_frames += readcount / plac->channels ;
545
0
    total += readcount ;
546
0
    len -= readcount ;
547
0
    } ;
548
549
0
  return total ;
550
0
} /* alac_read_f */
551
552
static sf_count_t
553
alac_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
554
17.0k
{ ALAC_PRIVATE *plac ;
555
17.0k
  int     *iptr ;
556
17.0k
  int     k, readcount ;
557
17.0k
  sf_count_t  total = 0 ;
558
17.0k
  double    normfact ;
559
560
17.0k
  if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL)
561
0
    return 0 ;
562
563
17.0k
  normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
564
565
46.1k
  while (len > 0)
566
30.0k
  { if (plac->partial_block_frames >= plac->frames_this_block && alac_decode_block (psf, plac) == 0)
567
814
      break ;
568
569
29.1k
    readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ;
570
29.1k
    readcount = readcount > len ? (int) len : readcount ;
571
572
29.1k
    iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
573
574
16.7M
    for (k = 0 ; k < readcount ; k++)
575
16.7M
      ptr [total + k] = normfact * iptr [k] ;
576
577
29.1k
    plac->partial_block_frames += readcount / plac->channels ;
578
29.1k
    total += readcount ;
579
29.1k
    len -= readcount ;
580
29.1k
    } ;
581
582
17.0k
  return total ;
583
17.0k
} /* alac_read_d */
584
585
/*============================================================================================
586
*/
587
588
static sf_count_t
589
alac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
590
1.71k
{ ALAC_PRIVATE *plac ;
591
1.71k
  int     newblock, newsample ;
592
593
1.71k
  if (! psf->codec_data)
594
0
    return 0 ;
595
1.71k
  plac = (ALAC_PRIVATE*) psf->codec_data ;
596
597
1.71k
  if (psf->datalength < 0 || psf->dataoffset < 0)
598
8
  { psf->error = SFE_BAD_SEEK ;
599
8
    return PSF_SEEK_ERROR ;
600
1.70k
    } ;
601
602
1.70k
  if (offset == 0)
603
873
  { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
604
605
873
    plac->frames_this_block = 0 ;
606
873
    plac->input_data_pos = psf->dataoffset ;
607
873
    plac->pakt_info->current = 0 ;
608
873
    return 0 ;
609
873
    } ;
610
611
832
  if (offset < 0 || offset > plac->pakt_info->count * plac->frames_per_block)
612
0
  { psf->error = SFE_BAD_SEEK ;
613
0
    return  PSF_SEEK_ERROR ;
614
832
    } ;
615
616
832
  newblock  = offset / plac->frames_per_block ;
617
832
  newsample = offset % plac->frames_per_block ;
618
619
832
  if (mode == SFM_READ)
620
832
  { plac->input_data_pos = psf->dataoffset + alac_pakt_block_offset (plac->pakt_info, newblock) ;
621
622
832
    plac->pakt_info->current = newblock ;
623
832
    alac_decode_block (psf, plac) ;
624
832
    plac->partial_block_frames = newsample ;
625
832
    }
626
0
  else
627
0
  { /* What to do about write??? */
628
0
    psf->error = SFE_BAD_SEEK ;
629
0
    return  PSF_SEEK_ERROR ;
630
832
    } ;
631
632
832
  return newblock * plac->frames_per_block + newsample ;
633
832
} /* alac_seek */
634
635
/*==========================================================================================
636
** ALAC Write Functions.
637
*/
638
639
static sf_count_t
640
alac_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
641
0
{ ALAC_PRIVATE *plac ;
642
0
  int     *iptr ;
643
0
  int     k, writecount ;
644
0
  sf_count_t  total = 0 ;
645
646
0
  if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL)
647
0
    return 0 ;
648
649
0
  while (len > 0)
650
0
  { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ;
651
0
    writecount = (writecount == 0 || writecount > len) ? (int) len : writecount ;
652
653
0
    iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
654
655
0
    for (k = 0 ; k < writecount ; k++)
656
0
      iptr [k] = arith_shift_left (ptr [k], 16) ;
657
658
0
    plac->partial_block_frames += writecount / plac->channels ;
659
0
    total += writecount ;
660
0
    len -= writecount ;
661
0
    ptr += writecount ;
662
663
0
    if (plac->partial_block_frames >= plac->frames_per_block)
664
0
      alac_encode_block (plac) ;
665
0
    } ;
666
667
0
  return total ;
668
0
} /* alac_write_s */
669
670
static sf_count_t
671
alac_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
672
0
{ ALAC_PRIVATE *plac ;
673
0
  int     *iptr ;
674
0
  int     k, writecount ;
675
0
  sf_count_t  total = 0 ;
676
677
0
  if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL)
678
0
    return 0 ;
679
680
0
  while (len > 0)
681
0
  { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ;
682
0
    writecount = (writecount == 0 || writecount > len) ? (int) len : writecount ;
683
684
0
    iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
685
686
0
    for (k = 0 ; k < writecount ; k++)
687
0
      iptr [k] = ptr [k] ;
688
689
0
    plac->partial_block_frames += writecount / plac->channels ;
690
0
    total += writecount ;
691
0
    len -= writecount ;
692
0
    ptr += writecount ;
693
694
0
    if (plac->partial_block_frames >= plac->frames_per_block)
695
0
      alac_encode_block (plac) ;
696
0
    } ;
697
698
0
  return total ;
699
0
} /* alac_write_i */
700
701
static sf_count_t
702
alac_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
703
0
{ ALAC_PRIVATE *plac ;
704
0
  void    (*convert) (const float *, int *t, int, int) ;
705
0
  int     *iptr ;
706
0
  int     writecount ;
707
0
  sf_count_t  total = 0 ;
708
709
0
  if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL)
710
0
    return 0 ;
711
712
0
  convert = (psf->add_clipping) ? psf_f2i_clip_array : psf_f2i_array ;
713
714
0
  while (len > 0)
715
0
  { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ;
716
0
    writecount = (writecount == 0 || writecount > len) ? (int) len : writecount ;
717
718
0
    iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
719
720
0
    convert (ptr, iptr, writecount, psf->norm_float) ;
721
722
0
    plac->partial_block_frames += writecount / plac->channels ;
723
0
    total += writecount ;
724
0
    len -= writecount ;
725
0
    ptr += writecount ;
726
727
0
    if (plac->partial_block_frames >= plac->frames_per_block)
728
0
      alac_encode_block (plac) ;
729
0
    } ;
730
731
0
  return total ;
732
0
} /* alac_write_f */
733
734
static sf_count_t
735
alac_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
736
0
{ ALAC_PRIVATE *plac ;
737
0
  void    (*convert) (const double *, int *t, int, int) ;
738
0
  int     *iptr ;
739
0
  int     writecount ;
740
0
  sf_count_t  total = 0 ;
741
742
0
  if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL)
743
0
    return 0 ;
744
745
0
  convert = (psf->add_clipping) ? psf_d2i_clip_array : psf_d2i_array ;
746
747
0
  while (len > 0)
748
0
  { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ;
749
0
    writecount = (writecount == 0 || writecount > len) ? (int) len : writecount ;
750
751
0
    iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
752
753
0
    convert (ptr, iptr, writecount, psf->norm_float) ;
754
755
0
    plac->partial_block_frames += writecount / plac->channels ;
756
0
    total += writecount ;
757
0
    len -= writecount ;
758
0
    ptr += writecount ;
759
760
0
    if (plac->partial_block_frames >= plac->frames_per_block)
761
0
      alac_encode_block (plac) ;
762
0
    } ;
763
764
0
  return total ;
765
0
} /* alac_write_d */
766
767
/*==============================================================================
768
** PAKT_INFO handling.
769
*/
770
771
static PAKT_INFO *
772
alac_pakt_alloc (uint32_t initial_count)
773
956
{ PAKT_INFO * info ;
774
775
956
  if ((info = calloc (1, sizeof (PAKT_INFO) + initial_count * sizeof (info->packet_size [0]))) == NULL)
776
0
    return NULL ;
777
778
956
  info->allocated = initial_count ;
779
956
  info->current = 0 ;
780
956
  info->count = 0 ;
781
782
956
  return info ;
783
956
} /* alac_pakt_alloc */
784
785
static PAKT_INFO *
786
alac_pakt_append (PAKT_INFO * info, uint32_t value)
787
102k
{
788
102k
  if (info->count >= info->allocated)
789
690
  { PAKT_INFO * temp ;
790
690
    uint32_t newcount = info->allocated + info->allocated / 2 ;
791
792
690
    if ((temp = realloc (info, sizeof (PAKT_INFO) + newcount * sizeof (info->packet_size [0]))) == NULL)
793
0
      return NULL ;
794
795
690
    info = temp ;
796
690
    info->allocated = newcount ;
797
102k
    } ;
798
799
102k
  info->packet_size [info->count++] = value ;
800
102k
  return info ;
801
102k
} /* alac_pakt_append */
802
803
static PAKT_INFO *
804
alac_pakt_read_decode (SF_PRIVATE * psf, uint32_t UNUSED (pakt_offset))
805
1.02k
{ SF_CHUNK_INFO chunk_info ;
806
1.02k
  PAKT_INFO * info = NULL ;
807
1.02k
  uint8_t *pakt_data = NULL ;
808
1.02k
  uint32_t bcount, value = 1, pakt_size ;
809
1.02k
  SF_CHUNK_ITERATOR * chunk_iterator ;
810
811
812
1.02k
  memset (&chunk_info, 0, sizeof (chunk_info)) ;
813
1.02k
  snprintf (chunk_info.id, sizeof (chunk_info.id), "pakt") ;
814
1.02k
  chunk_info.id_size = 4 ;
815
816
1.02k
  if ((chunk_iterator = psf_get_chunk_iterator (psf, chunk_info.id)) == NULL)
817
67
  { psf_log_printf (psf, "%s : no chunk iterator found\n", __func__) ;
818
67
    free (chunk_info.data) ;
819
67
    chunk_info.data = NULL ;
820
67
    return NULL ;
821
956
    } ;
822
823
956
  psf->get_chunk_size (psf, chunk_iterator, &chunk_info) ;
824
825
956
  pakt_size = chunk_info.datalen ;
826
956
  chunk_info.data = pakt_data = malloc (pakt_size + 5) ;
827
956
  if (!chunk_info.data)
828
0
    return NULL ;
829
830
956
  if ((bcount = psf->get_chunk_data (psf, chunk_iterator, &chunk_info)) != SF_ERR_NO_ERROR)
831
0
  { while (chunk_iterator)
832
0
      chunk_iterator = psf->next_chunk_iterator (psf, chunk_iterator) ;
833
0
    free (chunk_info.data) ;
834
0
    chunk_info.data = NULL ;
835
0
    return NULL ;
836
956
    } ;
837
838
2.02k
  while (chunk_iterator)
839
1.06k
    chunk_iterator = psf->next_chunk_iterator (psf, chunk_iterator) ;
840
841
956
  info = alac_pakt_alloc (pakt_size / 4) ;
842
843
  /* Start at 24 bytes in, skipping over the 'pakt' chunks header. */
844
103k
  for (bcount = 24 ; bcount < pakt_size && value != 0 ; )
845
102k
  { uint8_t byte ;
846
102k
    int32_t count = 0 ;
847
848
102k
    value = 0 ;
849
102k
    do
850
108k
    { byte = pakt_data [bcount + count] ;
851
108k
      value = (value << 7) + (byte & 0x7F) ;
852
853
108k
      count ++ ;
854
108k
      if (count > 5 || bcount + count > pakt_size)
855
416
      { printf ("%s %d : Ooops! count %" PRIi32 "    bcount %" PRIu32 "\n", __func__, __LINE__, count, bcount) ;
856
416
        value = 0 ;
857
416
        break ;
858
108k
        } ;
859
108k
      }
860
108k
      while (byte & 0x80) ;
861
862
0
    bcount += count ;
863
864
102k
    if ((info = alac_pakt_append (info, value)) == NULL)
865
0
      goto FreeExit ;
866
102k
    } ;
867
868
956
  free (pakt_data) ;
869
870
956
  return info ;
871
872
0
FreeExit :
873
0
  free (pakt_data) ;
874
0
  free (info) ;
875
0
  return NULL ;
876
956
} /* alac_pakt_read_decode */
877
878
static uint8_t *
879
alac_pakt_encode (const SF_PRIVATE *psf, uint32_t * pakt_size_out)
880
0
{ const ALAC_PRIVATE *plac ;
881
0
  const PAKT_INFO *info ;
882
0
  uint8_t *data ;
883
0
  uint32_t k, allocated, pakt_size ;
884
885
0
  plac = psf->codec_data ;
886
0
  info = plac->pakt_info ;
887
888
0
  allocated = 100 + 4 * info->count ;
889
0
  if ((data = calloc (1, allocated)) == NULL)
890
0
    return NULL ;
891
892
0
  psf_put_be64 (data, 0, info->count) ;
893
0
  psf_put_be64 (data, 8, psf->sf.frames) ;
894
0
  psf_put_be32 (data, 20, kALACDefaultFramesPerPacket - plac->partial_block_frames) ;
895
896
  /* Real 'pakt' data starts after 24 byte header. */
897
0
  pakt_size = 24 ;
898
899
0
  for (k = 0 ; k < info->count ; k++)
900
0
  { int32_t value = info->packet_size [k] ;
901
902
0
    if ((value & 0x7f) == value)
903
0
    { data [pakt_size++] = value ;
904
0
      continue ;
905
0
      } ;
906
907
0
    if ((value & 0x3fff) == value)
908
0
    { data [pakt_size++] = (value >> 7) | 0x80 ;
909
0
      data [pakt_size++] = value & 0x7f ;
910
0
      continue ;
911
0
      } ;
912
913
0
    if ((value & 0x1fffff) == value)
914
0
    { data [pakt_size++] = (value >> 14) | 0x80 ;
915
0
      data [pakt_size++] = ((value >> 7) & 0x7f) | 0x80 ;
916
0
      data [pakt_size++] = value & 0x7f ;
917
0
      continue ;
918
0
    } ;
919
920
0
    if ((value & 0x0fffffff) == value)
921
0
    { data [pakt_size++] = (value >> 21) | 0x80 ;
922
0
      data [pakt_size++] = ((value >> 14) & 0x7f) | 0x80 ;
923
0
      data [pakt_size++] = ((value >> 7) & 0x7f) | 0x80 ;
924
0
      data [pakt_size++] = value & 0x7f ;
925
0
      continue ;
926
0
      } ;
927
928
0
    *pakt_size_out = 0 ;
929
0
    free (data) ;
930
0
    return NULL ;
931
0
    } ;
932
933
0
  *pakt_size_out = pakt_size ;
934
0
  return data ;
935
0
} /* alac_pakt_encode */
936
937
static sf_count_t
938
alac_pakt_block_offset (const PAKT_INFO *info, uint32_t block)
939
832
{ sf_count_t offset = 0 ;
940
832
  uint32_t k ;
941
942
52.0k
  for (k = 0 ; k < block ; k++)
943
51.2k
    offset += info->packet_size [k] ;
944
945
832
  return offset ;
946
832
} /* alac_pakt_block_offset */
947
948
static uint32_t
949
alac_kuki_read (SF_PRIVATE * psf, uint32_t kuki_offset, uint8_t * kuki, size_t kuki_maxlen)
950
956
{ uint32_t marker ;
951
956
  uint64_t kuki_size ;
952
953
956
  if (psf_fseek (psf, kuki_offset, SEEK_SET) != kuki_offset)
954
0
    return 0 ;
955
956
956
  psf_fread (&marker, 1, sizeof (marker), psf) ;
957
956
  if (marker != MAKE_MARKER ('k', 'u', 'k', 'i'))
958
70
    return 0 ;
959
960
886
  psf_fread (&kuki_size, 1, sizeof (kuki_size), psf) ;
961
886
  kuki_size = BE2H_64 (kuki_size) ;
962
963
886
  if (kuki_size == 0 || kuki_size > kuki_maxlen)
964
7
  { psf_log_printf (psf, "%s : Bad size (%D) of 'kuki' chunk.\n", __func__, kuki_size) ;
965
7
    return 0 ;
966
879
    } ;
967
968
879
  psf_fread (kuki, 1, kuki_size, psf) ;
969
970
879
  return kuki_size ;
971
886
} /* alac_kuki_read */
972
973
97
#define CASE_NAME(x)  case x : return #x ; break ;
974
975
static const char *
976
alac_error_string (int error)
977
97
{ static char errstr [128] ;
978
97
  switch (error)
979
97
  { CASE_NAME (kALAC_UnimplementedError) ;
980
0
    CASE_NAME (kALAC_FileNotFoundError) ;
981
0
    CASE_NAME (kALAC_ParamError) ;
982
0
    CASE_NAME (kALAC_MemFullError) ;
983
10
    CASE_NAME (fALAC_FrameLengthError) ;
984
985
    /* Added for libsndfile */
986
5
    CASE_NAME (kALAC_BadBitWidth) ;
987
1
    CASE_NAME (kALAC_IncompatibleVersion) ;
988
81
    CASE_NAME (kALAC_BadSpecificConfigSize) ;
989
0
    CASE_NAME (kALAC_ZeroChannelCount) ;
990
0
    CASE_NAME (kALAC_NumSamplesTooBig) ;
991
0
    CASE_NAME (kALAC_UnsupportedElement) ;
992
0
    default :
993
0
      break ;
994
97
    } ;
995
996
0
  snprintf (errstr, sizeof (errstr), "Unknown error %d", error) ;
997
0
  return errstr ;
998
97
} /* alac_error_string */
999