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