Debian Codegraph @

src/mac.c

raw symbols SCIP
1 #include "mac.h"
2
3 static int
4 get_macfileinfo(PerlIO *infile, char *file, HV *info)
5 {
6 Buffer header;
7 char *bptr;
8 int32_t ret = 0;
9 int32_t header_end;
10
11 mac_streaminfo *si;
12 Newz(0, si, sizeof(mac_streaminfo), mac_streaminfo);
13
14 /*
15 There are two possible variations here.
16 1. There's an ID3V2 tag present at the beginning of the file
17 2. There's an APE tag present at the beginning of the file
18 (deprecated, but still possible)
19 For each type of tag, check for existence and then skip it before
20 looking for the MPC header
21 */
22 if ((header_end = skip_id3v2(infile)) < 0) {
23 PerlIO_printf(PerlIO_stderr(), "MAC: [Couldn't skip ID3v2]: %s\n", file);
24 Safefree(si);
25 return -1;
26 }
27
28 // seek to first byte of MAC data
29 if (PerlIO_seek(infile, header_end, SEEK_SET) < 0) {
30 PerlIO_printf(PerlIO_stderr(), "MAC: [Couldn't seek to offset %d]: %s\n", header_end, file);
31 Safefree(si);
32 return -1;
33 }
34
35 // Offset + MAC. Does this need the space as well, to be +4 ?
36 si->audio_start_offset = PerlIO_tell(infile) + 3;
37
38 // Skip the APETAGEX if it exists.
39 buffer_init(&header, APE_HEADER_LEN);
40
41 if (!_check_buf(infile, &header, APE_HEADER_LEN, APE_HEADER_LEN)) {
42 PerlIO_printf(PerlIO_stderr(), "MAC: [Couldn't read tag header]: %s\n", file);
43 goto out;
44 }
45
46 bptr = buffer_ptr(&header);
47
48 if (memcmp(bptr, "APETAGEX", 8) == 0) {
49 // Skip the ape tag structure
50 // XXXX - need to test this code path.
51 buffer_get_int_le(&header);
52 PerlIO_seek(infile, buffer_get_int_le(&header), SEEK_CUR);
53
54 } else {
55 // set the pointer back to original location
56 PerlIO_seek(infile, -APE_HEADER_LEN, SEEK_CUR);
57 }
58
59 buffer_clear(&header);
60
61 if (!_check_buf(infile, &header, 32, 32)) {
62 PerlIO_printf(PerlIO_stderr(), "MAC: [Couldn't read stream header]: %s\n", file);
63 goto out;
64 }
65
66 bptr = buffer_ptr(&header);
67
68 if (memcmp(bptr, "MAC ", 4) != 0) {
69 PerlIO_printf(PerlIO_stderr(), "MAC: [Couldn't couldn't find stream header]: %s\n", file);
70 goto out;
71 }
72
73 buffer_consume(&header, 4);
74 si->version = buffer_get_short_le(&header);
75
76 if (si->version < 3980) {
77 uint16_t compression_id = buffer_get_short_le(&header);
78 if (compression_id % 1000) {
79 si->compression = "";
80 }
81 else {
82 si->compression = mac_profile_names[ compression_id / 1000 ];
83 }
84
85 if (!_check_buf(infile, &header, MAC_397_HEADER_LEN, MAC_397_HEADER_LEN)) {
86 PerlIO_printf(PerlIO_stderr(), "MAC: [Couldn't read < 3.98 stream header]: %s\n", file);
87 goto out;
88 }
89
90 buffer_consume(&header, 2); // flags
91
92 si->channels = buffer_get_short_le(&header);
93
94 si->sample_rate = buffer_get_int_le(&header);
95
96 buffer_consume(&header, 4); // header size
97 buffer_consume(&header, 4); // terminating data bytes
98
99 si->total_frames = buffer_get_int_le(&header);
100 si->final_frame = buffer_get_int_le(&header);
101 si->blocks_per_frame = si->version >= 3950 ? (73728 * 4) : 73728;
102
103 } else {
104 unsigned char md5[16];
105 uint16_t profile;
106
107 if (!_check_buf(infile, &header, MAC_398_HEADER_LEN, MAC_398_HEADER_LEN)) {
108 PerlIO_printf(PerlIO_stderr(), "MAC: [Couldn't read > 3.98 stream header]: %s\n", file);
109 goto out;
110 }
111
112 buffer_consume(&header, 2);
113
114 // unused.
115 buffer_get_int_le(&header); // desc bytes
116 buffer_get_int_le(&header); // header bytes
117 buffer_get_int_le(&header); // seek table bytes
118 buffer_get_int_le(&header); // header data bytes
119 buffer_get_int_le(&header); // ape frame data bytes
120 buffer_get_int_le(&header); // ape frame data bytes high
121 buffer_get_int_le(&header); // terminating data bytes
122 buffer_get(&header, &md5, sizeof(md5));
123
124 // Header block
125 profile = buffer_get_short_le(&header);
126 if (profile % 1000) {
127 si->compression = "";
128 }
129 else {
130 si->compression = mac_profile_names[ profile / 1000 ];
131 }
132
133 buffer_get_short_le(&header); // flags
134
135 si->blocks_per_frame = buffer_get_int_le(&header);
136 si->final_frame = buffer_get_int_le(&header);
137 si->total_frames = buffer_get_int_le(&header);
138 si->bits = buffer_get_short_le(&header);
139 si->channels = buffer_get_short_le(&header);
140 si->sample_rate = buffer_get_int_le(&header);
141 }
142
143 si->file_size = _file_size(infile);
144
145 if (si->sample_rate) {
146 double total_samples = (double)(((si->blocks_per_frame * (si->total_frames - 1)) + si->final_frame));
147 uint32_t total_ms = (total_samples * 1000) / si->sample_rate;
148
149 my_hv_store(info, "samplerate", newSViv(si->sample_rate));
150 my_hv_store(info, "channels", newSViv(si->channels));
151 my_hv_store(info, "song_length_ms", newSVuv(total_ms));
152 my_hv_store(info, "bitrate", newSVuv( _bitrate(si->file_size - si->audio_start_offset, total_ms) ));
153
154 my_hv_store(info, "file_size", newSVnv(si->file_size));
155 my_hv_store(info, "audio_offset", newSVuv(si->audio_start_offset));
156 my_hv_store(info, "audio_size", newSVuv(si->file_size - si->audio_start_offset));
157 my_hv_store(info, "compression", newSVpv(si->compression, 0));
158 my_hv_store(info, "version", newSVpvf( "%0.2f", si->version * 1.0 / 1000 ) );
159 }
160
161 out:
162 buffer_free(&header);
163 Safefree(si);
164
165 return ret;
166 }