Lines Matching defs:psf
93 static int caf_close (SF_PRIVATE *psf) ;
94 static int caf_read_header (SF_PRIVATE *psf) ;
95 static int caf_write_header (SF_PRIVATE *psf, int calc_length) ;
96 static int caf_write_tailer (SF_PRIVATE *psf) ;
97 static int caf_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
98 static int caf_read_chanmap (SF_PRIVATE * psf, sf_count_t chunk_size) ;
99 static int caf_read_strings (SF_PRIVATE * psf, sf_count_t chunk_size) ;
100 static void caf_write_strings (SF_PRIVATE * psf, int location) ;
103 static int caf_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info) ;
104 static SF_CHUNK_ITERATOR * caf_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator) ;
105 static int caf_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
106 static int caf_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
113 caf_open (SF_PRIVATE *psf)
117 if ((psf->container_data = calloc (1, sizeof (CAF_PRIVATE))) == NULL)
120 pcaf = psf->container_data ;
122 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
123 { if ((error = caf_read_header (psf)))
126 psf->next_chunk_iterator = caf_next_chunk_iterator ;
127 psf->get_chunk_size = caf_get_chunk_size ;
128 psf->get_chunk_data = caf_get_chunk_data ;
131 subformat = SF_CODEC (psf->sf.format) ;
133 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
134 { if (psf->is_pipe)
137 format = SF_CONTAINER (psf->sf.format) ;
141 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
143 if (psf->file.mode != SFM_RDWR || psf->filelength < 44)
144 { psf->filelength = 0 ;
145 psf->datalength = 0 ;
146 psf->dataoffset = 0 ;
147 psf->sf.frames = 0 ;
150 psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
156 if (psf->file.mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
157 { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
159 psf->peak_info->peak_loc = SF_PEAK_START ;
162 if ((error = caf_write_header (psf, SF_FALSE)) != 0)
165 psf->write_header = caf_write_header ;
166 psf->set_chunk = caf_set_chunk ;
169 psf->container_close = caf_close ;
170 psf->command = caf_command ;
177 error = pcm_init (psf) ;
181 error = ulaw_init (psf) ;
185 error = alaw_init (psf) ;
190 error = float32_init (psf) ;
194 error = double64_init (psf) ;
201 if (psf->file.mode == SFM_READ)
203 error = alac_init (psf, &pcaf->alac) ;
205 error = alac_init (psf, NULL) ;
218 caf_close (SF_PRIVATE *psf)
220 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
221 { caf_write_tailer (psf) ;
222 caf_write_header (psf, SF_TRUE) ;
229 caf_command (SF_PRIVATE * psf, int command, void * UNUSED (data), int UNUSED (datasize))
232 if ((pcaf = psf->container_data) == NULL)
237 pcaf->chanmap_tag = aiff_caf_find_channel_layout_tag (psf->channel_map, psf->sf.channels) ;
251 decode_desc_chunk (SF_PRIVATE *psf, const DESC_CHUNK *desc)
254 psf->sf.channels = desc->channels_per_frame ;
259 if ((pcaf = psf->container_data) != NULL)
278 psf_log_printf (psf, "Bad ALAC format flag value of %d\n", desc->fmt_flags) ;
287 format |= psf->endian == SF_ENDIAN_LITTLE ? SF_ENDIAN_LITTLE : 0 ;
292 { psf->bytewidth = 4 ;
296 { psf->bytewidth = 8 ;
304 { psf->bytewidth = 4 ;
308 { psf->bytewidth = 3 ;
312 { psf->bytewidth = 2 ;
316 { psf->bytewidth = 1 ;
322 { psf->bytewidth = 1 ;
327 { psf->bytewidth = 1 ;
331 psf_log_printf (psf, "**** Unknown format identifier.\n") ;
337 caf_read_header (SF_PRIVATE *psf)
346 if ((pcaf = psf->container_data) == NULL)
352 psf_binheader_readf (psf, "pmE2E2", 0, &marker, &version, &flags) ;
353 psf_log_printf (psf, "%M\n Version : %d\n Flags : %x\n", marker, version, flags) ;
357 psf_binheader_readf (psf, "mE8b", &marker, &chunk_size, ubuf.ucbuf, 8) ;
360 psf_log_printf (psf, "%M : %D\n Sample rate : %s\n", marker, chunk_size, ubuf.cbuf) ;
365 { psf_log_printf (psf, "**** Chunk size too small. Should be > 32 bytes.\n") ;
369 psf->sf.samplerate = psf_lrint (srate) ;
371 psf_binheader_readf (psf, "mE44444", &desc.fmt_id, &desc.fmt_flags, &desc.pkt_bytes, &desc.frames_per_packet,
373 psf_log_printf (psf, " Format id : %M\n Format flags : %x\n Bytes / packet : %u\n"
378 { psf_log_printf (psf, "**** Bad channels per frame value %u.\n", desc.channels_per_frame) ;
383 psf_binheader_readf (psf, "j", (int) (chunk_size - sizeof (DESC_CHUNK))) ;
385 psf->sf.channels = desc.channels_per_frame ;
391 psf_binheader_readf (psf, "mE8", &marker, &chunk_size) ;
393 { sf_count_t pos = psf_ftell (psf) ;
394 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
398 { psf_log_printf (psf, "%M : %D *** Should be >= 0 ***\n", marker, chunk_size) ;
401 if (chunk_size > psf->filelength)
404 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
408 psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ;
409 if (chunk_size != CAF_PEAK_CHUNK_SIZE (psf->sf.channels))
410 { psf_binheader_readf (psf, "j", (size_t) chunk_size) ;
411 psf_log_printf (psf, "*** File PEAK chunk %D should be %d.\n", chunk_size, CAF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
415 if (psf->peak_info)
416 { psf_log_printf (psf, "*** Found existing peak info, using last one.\n") ;
417 free (psf->peak_info) ;
418 psf->peak_info = NULL ;
420 if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
424 psf_binheader_readf (psf, "E4", & (psf->peak_info->edit_number)) ;
425 psf_log_printf (psf, " edit count : %d\n", psf->peak_info->edit_number) ;
427 psf_log_printf (psf, " Ch Position Value\n") ;
428 for (k = 0 ; k < psf->sf.channels ; k++)
432 psf_binheader_readf (psf, "Ef8", &value, &position) ;
433 psf->peak_info->peaks [k].value = value ;
434 psf->peak_info->peaks [k].position = position ;
437 psf_log_printf (psf, ubuf.cbuf) ;
440 psf->peak_info->peak_loc = SF_PEAK_START ;
445 { psf_log_printf (psf, "%M : %D (should be >= 12)\n", marker, chunk_size) ;
446 psf_binheader_readf (psf, "j", (size_t) chunk_size) ;
450 psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ;
452 if ((error = caf_read_chanmap (psf, chunk_size)))
457 psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ;
458 psf_binheader_readf (psf, "j", (size_t) chunk_size) ;
462 psf_binheader_readf (psf, "E4", &k) ;
464 { psf_log_printf (psf, "%M : -1\n") ;
465 chunk_size = psf->filelength - psf->header.indx ;
467 else if (psf->filelength > 0 && chunk_size > psf->filelength - psf->header.indx + 10)
468 { psf_log_printf (psf, "%M : %D (should be %D)\n", marker, chunk_size, psf->filelength - psf->header.indx - 8) ;
469 psf->datalength = psf->filelength - psf->header.indx - 8 ;
472 { psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ;
474 psf->datalength = chunk_size - 4 ;
477 psf_log_printf (psf, " edit : %u\n", k) ;
479 psf->dataoffset = psf->header.indx ;
480 if (psf->datalength + psf->dataoffset < psf->filelength)
481 psf->dataend = psf->datalength + psf->dataoffset ;
483 psf_binheader_readf (psf, "j", (size_t) psf->datalength) ;
488 psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ;
489 pcaf->alac.kuki_offset = psf_ftell (psf) - 12 ;
490 psf_binheader_readf (psf, "j", (size_t) chunk_size) ;
495 { psf_log_printf (psf, "%M : %D (should be > 24)\n", marker, chunk_size) ;
498 else if (chunk_size > psf->filelength - psf->header.indx)
499 { psf_log_printf (psf, "%M : %D (should be < %D)\n", marker, chunk_size, psf->filelength - psf->header.indx) ;
503 psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ;
505 psf_binheader_readf (psf, "E8844", &pcaf->alac.packets, &pcaf->alac.valid_frames,
508 psf_log_printf (psf,
519 psf_log_printf (psf, "*** 'pakt' chunk header is all zero.\n") ;
521 pcaf->alac.pakt_offset = psf_ftell (psf) - 12 ;
522 psf_binheader_readf (psf, "j", (size_t) chunk_size - 24) ;
527 { psf_log_printf (psf, "%M : %D (should be > 4)\n", marker, chunk_size) ;
530 else if (chunk_size > psf->filelength - psf->header.indx)
531 { psf_log_printf (psf, "%M : %D (should be < %D)\n", marker, chunk_size, psf->filelength - psf->header.indx) ;
534 psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ;
536 caf_read_strings (psf, chunk_size - 4) ;
540 psf_log_printf (psf, "%M : %D (skipped)\n", marker, chunk_size) ;
541 psf_binheader_readf (psf, "j", (size_t) chunk_size) ;
548 if (! psf->sf.seekable && have_data)
551 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (chunk_size))
552 { psf_log_printf (psf, "End\n") ;
558 { psf_log_printf (psf, "**** Error, could not find 'data' chunk.\n") ;
562 psf->endian = (desc.fmt_flags & 2) ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
564 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
566 if ((psf->sf.format = decode_desc_chunk (psf, &desc)) == 0)
569 if (psf->bytewidth > 0)
570 psf->sf.frames = psf->datalength / psf->bytewidth ;
579 caf_write_header (SF_PRIVATE *psf, int calc_length)
587 if ((pcaf = psf->container_data) == NULL)
592 current = psf_ftell (psf) ;
595 { psf->filelength = psf_get_filelen (psf) ;
597 psf->datalength = psf->filelength - psf->dataoffset ;
599 if (psf->dataend)
600 psf->datalength -= psf->filelength - psf->dataend ;
602 if (psf->bytewidth > 0)
603 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
607 psf->header.ptr [0] = 0 ;
608 psf->header.indx = 0 ;
609 psf_fseek (psf, 0, SEEK_SET) ;
612 psf_binheader_writef (psf, "Em22", BHWm (caff_MARKER), BHW2 (1), BHW2 (0)) ;
615 psf_binheader_writef (psf, "Em8", BHWm (desc_MARKER), BHW8 ((sf_count_t) (sizeof (DESC_CHUNK)))) ;
617 double64_be_write (1.0 * psf->sf.samplerate, ubuf.ucbuf) ;
618 psf_binheader_writef (psf, "b", BHWv (ubuf.ucbuf), BHWz (8)) ;
620 subformat = SF_CODEC (psf->sf.format) ;
622 psf->endian = SF_ENDIAN (psf->sf.format) ;
624 if (CPU_IS_BIG_ENDIAN && (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU))
625 psf->endian = SF_ENDIAN_BIG ;
626 else if (CPU_IS_LITTLE_ENDIAN && (psf->endian == SF_ENDIAN_LITTLE || psf->endian == SF_ENDIAN_CPU))
627 psf->endian = SF_ENDIAN_LITTLE ;
629 if (psf->endian == SF_ENDIAN_LITTLE)
632 psf->endian = SF_ENDIAN_BIG ;
638 psf->bytewidth = 1 ;
639 desc.pkt_bytes = psf->bytewidth * psf->sf.channels ;
641 desc.channels_per_frame = psf->sf.channels ;
647 psf->bytewidth = 2 ;
648 desc.pkt_bytes = psf->bytewidth * psf->sf.channels ;
650 desc.channels_per_frame = psf->sf.channels ;
655 psf->bytewidth = 3 ;
656 desc.pkt_bytes = psf->bytewidth * psf->sf.channels ;
658 desc.channels_per_frame = psf->sf.channels ;
665 psf->bytewidth = 4 ;
666 desc.pkt_bytes = psf->bytewidth * psf->sf.channels ;
668 desc.channels_per_frame = psf->sf.channels ;
675 psf->bytewidth = 4 ;
676 desc.pkt_bytes = psf->bytewidth * psf->sf.channels ;
678 desc.channels_per_frame = psf->sf.channels ;
685 psf->bytewidth = 8 ;
686 desc.pkt_bytes = psf->bytewidth * psf->sf.channels ;
688 desc.channels_per_frame = psf->sf.channels ;
694 psf->bytewidth = 1 ;
695 desc.pkt_bytes = psf->bytewidth * psf->sf.channels ;
697 desc.channels_per_frame = psf->sf.channels ;
703 psf->bytewidth = 1 ;
704 desc.pkt_bytes = psf->bytewidth * psf->sf.channels ;
706 desc.channels_per_frame = psf->sf.channels ;
715 desc.pkt_bytes = psf->bytewidth * psf->sf.channels ;
716 desc.channels_per_frame = psf->sf.channels ;
725 psf_binheader_writef (psf, "mE44444", BHWm (desc.fmt_id), BHW4 (desc.fmt_flags), BHW4 (desc.pkt_bytes), BHW4 (desc.frames_per_packet), BHW4 (desc.channels_per_frame), BHW4 (desc.bits_per_chan)) ;
727 caf_write_strings (psf, SF_STR_LOCATE_START) ;
729 if (psf->peak_info != NULL)
731 psf_binheader_writef (psf, "Em84", BHWm (peak_MARKER), BHW8 ((sf_count_t) CAF_PEAK_CHUNK_SIZE (psf->sf.channels)), BHW4 (psf->peak_info->edit_number)) ;
732 for (k = 0 ; k < psf->sf.channels ; k++)
733 psf_binheader_writef (psf, "Ef8", BHWf ((float) psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
736 if (psf->channel_map && pcaf->chanmap_tag)
737 psf_binheader_writef (psf, "Em8444", BHWm (chan_MARKER), BHW8 ((sf_count_t) 12), BHW4 (pcaf->chanmap_tag), BHW4 (0), BHW4 (0)) ;
740 for (uk = 0 ; uk < psf->wchunks.used ; uk++)
741 psf_binheader_writef (psf, "m44b", BHWm ((int) psf->wchunks.chunks [uk].mark32), BHW4 (0), BHW4 (psf->wchunks.chunks [uk].len), BHWv (psf->wchunks.chunks [uk].data), BHWz (psf->wchunks.chunks [uk].len)) ;
745 sf_count_t free_len = 0x1000 - psf->header.indx - 16 - 12 ;
748 psf_binheader_writef (psf, "Em8z", BHWm (free_MARKER), BHW8 (free_len), BHWz (free_len)) ;
751 psf_binheader_writef (psf, "Em84", BHWm (data_MARKER), BHW8 (psf->datalength + 4), BHW4 (0)) ;
753 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
754 if (psf->error)
755 return psf->error ;
757 psf->dataoffset = psf->header.indx ;
758 if (current < psf->dataoffset)
759 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
761 psf_fseek (psf, current, SEEK_SET) ;
763 return psf->error ;
767 caf_write_tailer (SF_PRIVATE *psf)
770 psf->header.ptr [0] = 0 ;
771 psf->header.indx = 0 ;
773 if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
774 { psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
775 psf->dataend = psf->dataoffset + psf->datalength ;
778 if (psf->dataend > 0)
779 psf_fseek (psf, psf->dataend, SEEK_SET) ;
781 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
783 if (psf->dataend & 1)
784 psf_binheader_writef (psf, "z", BHWz (1)) ;
786 if (psf->strings.flags & SF_STR_LOCATE_END)
787 caf_write_strings (psf, SF_STR_LOCATE_END) ;
790 if (psf->header.indx > 0)
791 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
797 caf_read_chanmap (SF_PRIVATE * psf, sf_count_t chunk_size)
802 bytesread = psf_binheader_readf (psf, "E444", &layout_tag, &channel_bitmap, &channel_decriptions) ;
806 psf_log_printf (psf, " Tag : %x\n", layout_tag) ;
808 psf_log_printf (psf, " Layout : %s\n", map_info->name) ;
811 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
814 { size_t chanmap_size = SF_MIN (psf->sf.channels, layout_tag & 0xff) * sizeof (psf->channel_map [0]) ;
816 free (psf->channel_map) ;
818 if ((psf->channel_map = malloc (chanmap_size)) == NULL)
821 memcpy (psf->channel_map, map_info->channel_map, chanmap_size) ;
841 caf_read_strings (SF_PRIVATE * psf, sf_count_t chunk_size)
847 return (psf->error = SFE_MALLOC_FAILED) ;
849 psf_binheader_readf (psf, "E4b", &count, buf, (size_t) chunk_size) ;
850 psf_log_printf (psf, " count: %u\n", count) ;
859 psf_log_printf (psf, " %-12s : %s\n", key, value) ;
864 psf_store_string (psf, SF_STR_TITLE, value) ;
867 psf_store_string (psf, SF_STR_SOFTWARE, value) ;
870 psf_store_string (psf, SF_STR_COPYRIGHT, value) ;
873 psf_store_string (psf, SF_STR_ARTIST, value) ;
876 psf_store_string (psf, SF_STR_GENRE, value) ;
880 psf_store_string (psf, SF_STR_COMMENT, value) ;
883 psf_store_string (psf, SF_STR_TRACKNUMBER, value) ;
886 psf_store_string (psf, SF_STR_DATE, value) ;
889 psf_store_string (psf, SF_STR_ALBUM, value) ;
892 psf_store_string (psf, SF_STR_LICENSE, value) ;
895 psf_log_printf (psf, " Unhandled hash 0x%x : /* '%s' */\n", hash, key) ;
929 caf_write_strings (SF_PRIVATE * psf, int location)
937 { if (psf->strings.data [k].type == 0)
940 if (psf->strings.data [k].flags != location)
943 if ((cptr = psf_get_string (psf, psf->strings.data [k].type)) == NULL)
946 switch (psf->strings.data [k].type)
986 psf_binheader_writef (psf, "Em84b", BHWm (info_MARKER), BHW8 (buf.index + 4), BHW4 (string_count), BHWv (buf.s), BHWz (buf.index)) ;
993 caf_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
994 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
998 caf_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
999 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1003 caf_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1006 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1009 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1015 caf_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1019 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1025 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1026 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1028 pos = psf_ftell (psf) ;
1029 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1030 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1031 psf_fseek (psf, pos, SEEK_SET) ;