Debian Codegraph @

src/dsdiff.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 #define PROP_CK (uint8_t)1
18 #define DSD_CK (uint8_t)2
19 #define DIIN_CK (uint8_t)4
20 #define ERROR_CK (uint8_t)128
21 #include "dsdiff.h"
22
23 typedef struct {
24 PerlIO *infile;
25 Buffer *buf;
26 char *file;
27 HV *info;
28 HV *tags;
29 uint32_t channel_num;
30 uint32_t sampling_frequency;
31 uint64_t metadata_offset;
32 uint64_t sample_count;
33 uint64_t offset;
34 uint64_t audio_offset;
35 char *tag_diar_artist;
36 char *tag_diti_title;
37 } dsdiff_info;
38
39 static uint8_t
40 parse_diin_chunk(dsdiff_info *dsdiff, uint64_t size)
41 {
42 uint64_t ck_offset = 0;
43
44 while (ck_offset < size) {
45 char chunk_id[5];
46 uint64_t chunk_size;
47 uint32_t count;
48
49 buffer_clear(dsdiff->buf);
50 PerlIO_seek(dsdiff->infile, dsdiff->offset + ck_offset, SEEK_SET);
51
52 if ( !_check_buf(dsdiff->infile, dsdiff->buf, 12, DSDIFF_BLOCK_SIZE) ) return ERROR_CK;
53 strncpy(chunk_id, (char *)buffer_ptr(dsdiff->buf), 4);
54 chunk_id[4] = '\0';
55 buffer_consume(dsdiff->buf, 4);
56 chunk_size = buffer_get_int64(dsdiff->buf);
57 ck_offset += 12;
58
59 DEBUG_TRACE(" diin: %s : %" PRIu64 ",%" PRIu64 "\n", chunk_id, dsdiff->offset + ck_offset, chunk_size);
60
61 if ( !strcmp(chunk_id, "DIAR") ) {
62 if ( !_check_buf(dsdiff->infile, dsdiff->buf, chunk_size, DSDIFF_BLOCK_SIZE) ) return ERROR_CK;
63 count = buffer_get_int(dsdiff->buf);;
64 dsdiff->tag_diar_artist = (char *)malloc(count + 1);
65 strncpy(dsdiff->tag_diar_artist, (char *)buffer_ptr(dsdiff->buf), count);
66 dsdiff->tag_diar_artist[count] = '\0';
67 } else if ( !strcmp(chunk_id, "DITI") ) {
68 if ( !_check_buf(dsdiff->infile, dsdiff->buf, chunk_size, DSDIFF_BLOCK_SIZE) ) return ERROR_CK;
69 count = buffer_get_int(dsdiff->buf);;
70 dsdiff->tag_diti_title = (char *)malloc(count + 1);
71 strncpy(dsdiff->tag_diti_title, (char *)buffer_ptr(dsdiff->buf), count);
72 dsdiff->tag_diti_title[count] = '\0';
73 }
74
75 ck_offset += chunk_size;
76 }
77
78 return DIIN_CK;
79 }
80
81 static uint8_t
82 parse_prop_chunk(dsdiff_info *dsdiff, uint64_t size)
83 {
84 uint64_t ck_offset = 0;
85
86 if ( !_check_buf(dsdiff->infile, dsdiff->buf, 4, DSDIFF_BLOCK_SIZE) ) return ERROR_CK;
87 if ( strncmp( (char *)buffer_ptr(dsdiff->buf), "SND ", 4 ) ) return 0;
88 ck_offset += 4;
89
90 while (ck_offset < size) {
91 char chunk_id[5];
92 uint64_t chunk_size;
93
94 buffer_clear(dsdiff->buf);
95 PerlIO_seek(dsdiff->infile, dsdiff->offset + ck_offset, SEEK_SET);
96
97 if ( !_check_buf(dsdiff->infile, dsdiff->buf, 16, DSDIFF_BLOCK_SIZE) ) return ERROR_CK;
98 strncpy(chunk_id, (char *)buffer_ptr(dsdiff->buf), 4);
99 chunk_id[4] = '\0';
100 buffer_consume(dsdiff->buf, 4);
101 chunk_size = buffer_get_int64(dsdiff->buf);
102 ck_offset += 12;
103
104 DEBUG_TRACE(" prop: %s : %" PRIu64 ",%" PRIu64 "\n", chunk_id, dsdiff->offset + ck_offset, chunk_size);
105
106 if ( !strcmp(chunk_id, "FS ") ) {
107 dsdiff->sampling_frequency = buffer_get_int(dsdiff->buf);
108 } else if ( !strcmp(chunk_id, "CHNL") ) {
109 dsdiff->channel_num = (uint32_t)buffer_get_short(dsdiff->buf);
110 } else if ( !strcmp(chunk_id, "ID3 ") ) {
111 dsdiff->metadata_offset = dsdiff->offset + ck_offset;
112 }
113 ck_offset += chunk_size;
114 }
115
116 if (dsdiff->channel_num == 0 || dsdiff->sampling_frequency == 0) return ERROR_CK;
117
118 return PROP_CK;
119 }
120
121 int
122 get_dsdiff_metadata(PerlIO *infile, char *file, HV *info, HV *tags)
123 {
124 Buffer buf;
125 uint8_t flags = 0;
126 off_t file_size;
127 int err = 0;
128 uint64_t total_size;
129 dsdiff_info dsdiff;
130 unsigned char *bptr;
131 uint32_t song_length_ms;
132
133 dsdiff.infile = infile;
134 dsdiff.buf = &buf;
135 dsdiff.file = file;
136 dsdiff.info = info;
137 dsdiff.tags = tags;
138 dsdiff.channel_num = 0;
139 dsdiff.sampling_frequency = 0;
140 dsdiff.metadata_offset = 0;
141 dsdiff.sample_count = 0;
142 dsdiff.offset = 0;
143 dsdiff.audio_offset = 0;
144 dsdiff.tag_diar_artist = NULL;
145 dsdiff.tag_diti_title = NULL;
146
147 file_size = _file_size(infile);
148
149 buffer_init(&buf, DSDIFF_BLOCK_SIZE);
150
151 if ( !_check_buf(infile, &buf, 16, DSDIFF_BLOCK_SIZE) ) {
152 err = -1;
153 goto out;
154 }
155
156 if ( !strncmp( (char *)buffer_ptr(&buf), "FRM8", 4 ) ) {
157 buffer_consume(&buf, 4);
158 total_size = buffer_get_int64(&buf) + 12;
159 dsdiff.offset += 12;
160
161 if (strncmp( (char *)buffer_ptr(&buf), "DSD ", 4 ) ) {
162 PerlIO_printf(PerlIO_stderr(), "Invalid DSDIFF file header: %s\n", file);
163 err = -1;
164 goto out;
165 }
166 dsdiff.offset += 4;
167
168 my_hv_store( info, "file_size", newSVuv(file_size) );
169
170 while (dsdiff.offset <= total_size - 12) {
171 char chunk_id[5];
172 uint64_t chunk_size;
173
174 buffer_clear(&buf);
175 PerlIO_seek(infile, dsdiff.offset, SEEK_SET);
176
177 if ( !_check_buf(infile, &buf, 12, DSDIFF_BLOCK_SIZE) ) {
178 PerlIO_printf(PerlIO_stderr(), "DSDIFF file error: %s\n", file);
179 err = -1;
180 goto out;
181 };
182
183 strncpy(chunk_id, (char *)buffer_ptr(&buf), 4);
184 chunk_id[4] = '\0';
185 buffer_consume(&buf, 4);
186 chunk_size = buffer_get_int64(&buf);
187 dsdiff.offset += 12;
188
189 DEBUG_TRACE("%s: %" PRIu64 ",%" PRIu64 "\n", chunk_id, dsdiff.offset, chunk_size);
190
191 if (!strcmp(chunk_id, "PROP")) {
192 flags |= parse_prop_chunk(&dsdiff, chunk_size);
193 } else if (!strcmp(chunk_id, "DIIN")) {
194 flags |= parse_diin_chunk(&dsdiff, chunk_size);
195 } else if (!strcmp(chunk_id, "DSD ")) {
196 dsdiff.sample_count = 8 * chunk_size / dsdiff.channel_num;
197 dsdiff.audio_offset = dsdiff.offset;
198 flags |= DSD_CK;
199 } else if ( !strcmp(chunk_id, "ID3 ") ) {
200 dsdiff.metadata_offset = dsdiff.offset;
201 }
202
203 if ( flags & ERROR_CK ) {
204 PerlIO_printf(PerlIO_stderr(), "DSDIFF chunk error: %s\n", file);
205 err = -1;
206 goto out;
207 };
208
209 dsdiff.offset += chunk_size;
210 }
211
212 DEBUG_TRACE("Finished parsing...\n");
213
214 if ((flags & DSD_CK) == 0 || (flags & PROP_CK) == 0) {
215 PerlIO_printf(PerlIO_stderr(), "DSDIFF file error: %s\n", file);
216 err = -1;
217 goto out;
218 };
219
220 song_length_ms = ((dsdiff.sample_count * 1.0) / dsdiff.sampling_frequency) * 1000;
221
222 DEBUG_TRACE("audio_offset: %" PRIu64 "\n", dsdiff.audio_offset);
223 DEBUG_TRACE("audio_size: %" PRIu64 "\n", dsdiff.sample_count / 8 * dsdiff.channel_num);
224 DEBUG_TRACE("samplerate: %" PRIu32 "\n", dsdiff.sampling_frequency);
225 DEBUG_TRACE("song_length_ms: %u\n", song_length_ms);
226 DEBUG_TRACE("channels: %" PRIu32 "\n", dsdiff.channel_num);
227
228 my_hv_store( info, "audio_offset", newSVuv(dsdiff.audio_offset) );
229 my_hv_store( info, "audio_size", newSVuv(dsdiff.sample_count / 8 * dsdiff.channel_num) );
230 my_hv_store( info, "samplerate", newSVuv(dsdiff.sampling_frequency) );
231 my_hv_store( info, "song_length_ms", newSVuv(song_length_ms) );
232 my_hv_store( info, "channels", newSVuv(dsdiff.channel_num) );
233 my_hv_store( info, "bits_per_sample", newSVuv(1) );
234 my_hv_store( info, "bitrate", newSVuv( _bitrate(file_size - dsdiff.audio_offset, song_length_ms) ) );
235
236 if (dsdiff.tag_diar_artist) {
237 my_hv_store( info, "tag_diar_artist", newSVpv(dsdiff.tag_diar_artist, 0) );
238 free(dsdiff.tag_diar_artist);
239 }
240
241 if (dsdiff.tag_diti_title) {
242 my_hv_store( info, "tag_diti_title", newSVpv(dsdiff.tag_diti_title, 0) );
243 free(dsdiff.tag_diti_title);
244 }
245
246 DEBUG_TRACE("Stored info values...\n");
247
248 if (dsdiff.metadata_offset) {
249 PerlIO_seek(infile, dsdiff.metadata_offset, SEEK_SET);
250 buffer_clear(&buf);
251 if ( !_check_buf(infile, &buf, 10, DSDIFF_BLOCK_SIZE) ) {
252 goto out;
253 }
254
255 bptr = buffer_ptr(&buf);
256 if (
257 (bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') &&
258 bptr[3] < 0xff && bptr[4] < 0xff &&
259 bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80
260 ) {
261 parse_id3(infile, file, info, tags, dsdiff.metadata_offset, file_size);
262 }
263 }
264 } else {
265 PerlIO_printf(PerlIO_stderr(), "Invalid DSF file: missing DSD header: %s\n", file);
266 err = -1;
267 goto out;
268 }
269
270 out:
271 buffer_free(&buf);
272
273 if (err) return err;
274
275 return 0;
276 }
277