Debian Codegraph @

src/dsf.c

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17 #include "dsf.h"
18
19 int
20 get_dsf_metadata(PerlIO *infile, char *file, HV *info, HV *tags)
21 {
22 Buffer buf;
23 off_t file_size;
24 int err = 0;
25 uint64_t chunk_size, total_size, metadata_offset, sample_count, sample_bytes;
26 uint32_t format_version, format_id, channel_type, channel_num,
27 sampling_frequency, block_size_per_channel, bits_per_sample, song_length_ms;
28 unsigned char *bptr;
29
30 file_size = _file_size(infile);
31
32 buffer_init(&buf, DSF_BLOCK_SIZE);
33
34 if ( !_check_buf(infile, &buf, 80, DSF_BLOCK_SIZE) ) {
35 err = -1;
36 goto out;
37 }
38
39 if ( !strncmp( (char *)buffer_ptr(&buf), "DSD ", 4 ) ) {
40 buffer_consume(&buf, 4);
41
42 my_hv_store( info, "file_size", newSVuv(file_size) );
43
44 chunk_size = buffer_get_int64_le(&buf);
45 total_size = buffer_get_int64_le(&buf);
46 metadata_offset = buffer_get_int64_le(&buf);
47
48 if ((chunk_size != 28) ||
49 metadata_offset > total_size) {
50 PerlIO_printf(PerlIO_stderr(), "Invalid DSF file header: %s\n", file);
51 err = -1;
52 goto out;
53 }
54
55 if ( strncmp( (char *)buffer_ptr(&buf), "fmt ", 4 ) ) {
56 PerlIO_printf(PerlIO_stderr(), "Invalid DSF file: missing fmt header: %s\n", file);
57 err = -1;
58 goto out;
59 }
60
61 buffer_consume(&buf, 4);
62 chunk_size = buffer_get_int64_le(&buf);
63 format_version = buffer_get_int_le(&buf);
64 format_id = buffer_get_int_le(&buf);
65 channel_type = buffer_get_int_le(&buf);
66 channel_num = buffer_get_int_le(&buf);
67 sampling_frequency = buffer_get_int_le(&buf);
68 bits_per_sample = buffer_get_int_le(&buf);
69 sample_count = buffer_get_int64_le(&buf);
70 block_size_per_channel = buffer_get_int_le(&buf);
71
72 if ( (chunk_size != 52) ||
73 (format_version != 1) ||
74 (format_id != 0) ||
75 (block_size_per_channel != 4096) ||
76 strncmp( (char *)buffer_ptr(&buf), "\0\0\0\0", 4 ) ) {
77 PerlIO_printf(PerlIO_stderr(), "Invalid DSF file: unsupported fmt header: %s\n", file);
78 err = -1;
79 goto out;
80 }
81
82 buffer_consume(&buf, 4);
83
84 if ( strncmp( (char *)buffer_ptr(&buf), "data", 4 ) ) {
85 PerlIO_printf(PerlIO_stderr(), "Invalid DSF file: missing data header: %s\n", file);
86 err = -1;
87 goto out;
88 }
89
90 buffer_consume(&buf, 4);
91
92 sample_bytes = buffer_get_int64_le(&buf) - 12;
93
94 song_length_ms = ((sample_count * 1.0) / sampling_frequency) * 1000;
95
96 my_hv_store( info, "audio_offset", newSVuv( 28 + 52 + 12 ) );
97 my_hv_store( info, "audio_size", newSVuv(sample_bytes) );
98 my_hv_store( info, "samplerate", newSVuv(sampling_frequency) );
99 my_hv_store( info, "song_length_ms", newSVuv(song_length_ms) );
100 my_hv_store( info, "channels", newSVuv(channel_num) );
101 my_hv_store( info, "bits_per_sample", newSVuv(1) );
102 my_hv_store( info, "block_size_per_channel", newSVuv(block_size_per_channel) );
103 my_hv_store( info, "bitrate", newSVuv( _bitrate(file_size - (28 + 52 + 12), song_length_ms) ) );
104
105 if (metadata_offset) {
106 PerlIO_seek(infile, metadata_offset, SEEK_SET);
107 buffer_clear(&buf);
108 if ( !_check_buf(infile, &buf, 10, DSF_BLOCK_SIZE) ) {
109 goto out;
110 }
111
112 bptr = buffer_ptr(&buf);
113 if (
114 (bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') &&
115 bptr[3] < 0xff && bptr[4] < 0xff &&
116 bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80
117 ) {
118 parse_id3(infile, file, info, tags, metadata_offset, file_size);
119 }
120 }
121 }
122 else {
123 PerlIO_printf(PerlIO_stderr(), "Invalid DSF file: missing DSD header: %s\n", file);
124 err = -1;
125 goto out;
126 }
127
128 out:
129 buffer_free(&buf);
130
131 if (err) return err;
132
133 return 0;
134 }