src/mp3cut.c

filter: Statementstmt 92.7% 537 / 579 Branchbran 78.5% 267 / 340 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.4% 804 / 919 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/common.c ↑ Summary Gapless.xs »
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 "mp3cut.h"
18  
19 static uint16_t
20 _crc16(uint16_t crc, uint8_t value)
21 {
22 10152 uint32_t tmp = crc ^ value;
23 10152 crc = (crc >> 8) ^ CRC16_TABLE[tmp & 0xFF];
24 return crc;
25 }
26  
27 static uint32_t
28 _get_side_info_start(mp3frame *frame)
29 {
30 24 ▶ return frame->crc16_used ? 6 : 4;
Branch: 1 branch
BranchTrueFalseTotal
return frame->crc16_used ? 6 : 4; 22 2 24
31 }
32  
33 static uint32_t
34 _get_side_info_size(mp3frame *frame)
35 {
36 304 ▶ if (frame->mpegID == MPEG1_ID) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->mpegID == MPEG1_ID) { 115 17 132
37 1338 ▶ return frame->channels == 2 ? 32 : 17;
Branch: 4 branches
BranchTrueFalseTotal
return frame->channels == 2 ? 32 : 17; 5 121 126
return frame->channels == 2 ? 32 : 17; 2 22 24
return frame->channels == 2 ? 32 : 17; 63 1010 1073
return frame->channels == 2 ? 32 : 17; 4 111 115
38 }
39 else {
40 173 ▶ return frame->channels == 2 ? 17 : 9;
Branch: 4 branches
BranchTrueFalseTotal
return frame->channels == 2 ? 17 : 9; 18 0 18
return frame->channels == 2 ? 17 : 9; 4 0 4
return frame->channels == 2 ? 17 : 9; 134 0 134
return frame->channels == 2 ? 17 : 9; 17 0 17
41 }
42 }
43  
44 static uint32_t
45 132 ▶_get_side_info_end(mp3frame *frame)
Branch: 1 branch
BranchTrueFalseTotal
_get_side_info_end(mp3frame *frame) 130 2 132
46 {
47 132 return _get_side_info_start(frame) + _get_side_info_size(frame);
48 }
49  
50 static uint32_t
51 4223 _get_frame_file_offset(mp3cut *mp3c, uint32_t frame_index)
52 {
53 4223 uint32_t offset = mp3c->first_frame_offset;
54 4223 unsigned char *bptr = buffer_ptr(mp3c->mllt_buf);
55 4223 uint32_t max_frame_size = get_u24(&bptr[2]);
56 4223 uint32_t bits_for_bytes = bptr[8];
57 int f = 1;
58 int i = 10;
59
60 // If the file has a Xing/Info tag, we really want frame_index + 1
61 4223 ▶ if (mp3c->xilt_frame->xing_tag || mp3c->xilt_frame->info_tag)
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->xilt_frame->xing_tag || mp3c->xilt_frame->info_tag) 3932 291 4223
62 3932 frame_index++;
63  
64 // Improve performance of sequential offset lookups by caching the previous
65 // lookup values
66 4223 ▶ if (mp3c->cache_frame && mp3c->cache_frame <= frame_index) {
Branch: 2 branches
BranchTrueFalseTotal
if (mp3c->cache_frame && mp3c->cache_frame <= frame_index) { 4193 30 4223
if (mp3c->cache_frame && mp3c->cache_frame <= frame_index) { 4084 109 4193
67 4084 f = mp3c->cache_frame + 1;
68 4084 offset = mp3c->cache_offset;
69 4084 i = mp3c->cache_i;
70 //DEBUG_TRACE("Using cached frame %d offset %d i %d\n", mp3c->cache_frame, offset, i);
71 }
72
73 // frame_index of 0 will be 0 (start of file)
74
75 12709 ▶ while (f <= frame_index) {
Branch: 1 branch
BranchTrueFalseTotal
while (f <= frame_index) { 8486 4223 12709
76 8486 switch (bits_for_bytes) {
77 5939 case 4:
78 5939 ▶ if (f % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (f % 2 != 0) { 2994 2945 5939
79 2994 offset += max_frame_size - ((bptr[i] & 0xF0) >> 4);
80 }
81 else {
82 2945 offset += max_frame_size - (bptr[i] & 0xF);
83 2945 i++;
84 }
85 break;
86 571 case 8:
87 571 offset += max_frame_size - bptr[i++];
88 571 break;
89 1976 case 12:
90 1976 ▶ if (f % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (f % 2 != 0) { 996 980 1976
91 996 offset += max_frame_size - (((bptr[i] & 0xFF) << 4) | ((bptr[i+1] & 0xF0) >> 4));
92 i++;
93 }
94 else {
95 980 offset += max_frame_size - (((bptr[i] & 0x0F) << 8) | (bptr[i+1] & 0xFF));
96 980 i += 2;
97 }
98 break;
99 }
100
101 8486 f++;
102 }
103
104 // Cache offsets to speed up sequential access
105 4223 mp3c->cache_frame = frame_index;
106 4223 mp3c->cache_offset = offset;
107 4223 mp3c->cache_i = i;
108
109 DEBUG_TRACE("MLLT offset(%d) = %d\n", frame_index, offset);
110
111 4223 return offset;
112 }
113  
114 static void
115 1265 _set_curr_frame(mp3cut *mp3c, uint32_t frame_index)
116 {
117 1265 uint32_t frame_offset = _get_frame_file_offset(mp3c, frame_index);
118 int fh32;
119
120 1265 ▶ if (frame_offset >= mp3c->offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame_offset >= mp3c->offset) { 1217 48 1265
121 DEBUG_TRACE("_set_curr_frame(%d), skipping to %d\n", frame_index, frame_offset);
122 1217 _mp3cut_skip(mp3c, frame_offset - mp3c->offset);
123 }
124 else {
125 // Seek directly
126 DEBUG_TRACE("_set_curr_frame(%d), seeking to %d\n", frame_index, frame_offset);
127 48 buffer_clear(mp3c->buf);
128 48 PerlIO_seek(mp3c->fh, frame_offset, SEEK_SET);
129 }
130
131 // Make sure the header is loaded
132 1265 ▶ if ( !_check_buf(mp3c->fh, mp3c->buf, 4, MP3_BLOCK_SIZE) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !_check_buf(mp3c->fh, mp3c->buf, 4, MP3_BLOCK_SIZE) ) { 0 1265 1265
133 0 croak("Unable to read frame %d", frame_index);
134 }
135
136 // Parse frame header
137 1265 fh32 = get_u32( buffer_ptr(mp3c->buf) );
138 1265 ▶ if ( !_mp3cut_decode_frame(fh32, mp3c->curr_frame) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !_mp3cut_decode_frame(fh32, mp3c->curr_frame) ) { 0 1265 1265
139 // we got the wrong offset, this should never happen
140 0 croak("Invalid frame offset %d for frame %d (%x)", frame_offset, frame_index, fh32);
141 }
142
143 // Make sure the entire frame is loaded
144 1265 ▶ if ( !_check_buf(mp3c->fh, mp3c->buf, mp3c->curr_frame->frame_size, MP3_BLOCK_SIZE) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !_check_buf(mp3c->fh, mp3c->buf, mp3c->curr_frame->frame_size, MP3_BLOCK_SIZE) ) { 0 1265 1265
145 0 croak("Unable to read frame %d", frame_index);
146 }
147
148 1265 mp3c->offset = frame_offset;
149 1265 }
150  
151 static uint16_t
152 _get_frame_size(mp3cut *mp3c)
153 {
154 1217 return mp3c->curr_frame->frame_size;
155 }
156  
157 static uint16_t
158 1198 _get_bit_res_ptr(mp3cut *mp3c)
159 {
160 1198 unsigned char *bptr = buffer_ptr(mp3c->buf);
161 1198 ▶ uint8_t sis = _get_side_info_start(mp3c->curr_frame);
Branch: 1 branch
BranchTrueFalseTotal
uint8_t sis = _get_side_info_start(mp3c->curr_frame); 1115 83 1198
162 1198 uint16_t br_pointer = bptr[sis] & 0xFF;
163
164 1198 ▶ if (mp3c->curr_frame->mpegID == MPEG1_ID)
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->curr_frame->mpegID == MPEG1_ID) 1064 134 1198
165 1064 br_pointer = (br_pointer << 1) | ((bptr[sis+1] & 0x80) >> 7);
166
167 1198 return br_pointer;
168 }
169  
170 static uint16_t
171 1207 _get_main_data_size(mp3cut *mp3c)
172 {
173 1207 return mp3c->curr_frame->frame_size
174 2331 ▶ - _get_side_info_start(mp3c->curr_frame)
Branch: 2 branches
BranchTrueFalseTotal
- _get_side_info_start(mp3c->curr_frame) 1124 83 1207
- _get_side_info_start(mp3c->curr_frame) 1073 134 1207
175 1207 - _get_side_info_size(mp3c->curr_frame);
176 }
177  
178 int
179 29 _mp3cut_init(HV *self, mp3cut *mp3c)
180 {
181 29 PerlIO *fh = IoIFP(sv_2io(*(my_hv_fetch(self, "_fh"))));
182 mp3frame *frame;
183 bool first_frame_found = FALSE;
184 int firstkbps = 0;
185 int frame_counter = 0;
186 unsigned char *bptr;
187 bool check_bitrate = TRUE;
188
189 29 Newz(0, frame, sizeof(mp3frame), mp3frame);
190
191 29 mp3c->fh = fh;
192 29 mp3c->filter = 0; // Bug 17441, used to be FILTER_LAYER3 but we need to detect non-MP3 files and abort
193 29 mp3c->offset = 0;
194 29 mp3c->first_frame_offset = -1;
195 29 mp3c->music_frame_count = 0;
196 29 mp3c->max_res = 0;
197 29 mp3c->samples_per_frame = 0;
198 29 mp3c->enc_delay = 576;
199 29 mp3c->enc_padding = 576 * 3;
200 29 mp3c->is_vbr = FALSE;
201 29 mp3c->start_sample = UNKNOWN_START_SAMPLE;
202 29 mp3c->file_size = _file_size(fh);
203 29 mp3c->next_processed_frame = 0;
204
205 29 mp3c->has_mllt = FALSE;
206 29 mp3c->max_frame_size = 0;
207 29 mp3c->min_frame_size = 0;
208 29 mp3c->last_frame_size = 0;
209
210 29 mp3c->cache_frame = 0;
211 29 mp3c->cache_offset = 0;
212 29 mp3c->cache_i = 0;
213
214 // Load cache file if available
215 29 ▶ if (my_hv_exists(self, "cache_file")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_hv_exists(self, "cache_file")) { 3 26 29
216 3 char *cache = SvPVX(*(my_hv_fetch(self, "cache_file")));
217 // XXX add -e test
218 3 _mp3cut_mllt_load(mp3c, cache);
219 }
220
221 // Seek past ID3 tag
222 29 _mp3cut_skip_id3v2(mp3c);
223
224 4728 ▶ while ( _mp3cut_get_next_frame(mp3c, frame) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( _mp3cut_get_next_frame(mp3c, frame) ) { 4700 27 4727
225 4700 bptr = buffer_ptr(mp3c->buf);
226
227 // Track all frame sizes for MLLT
228 // Don't call if we loaded MLLT from cache
229 4700 ▶ if (!mp3c->has_mllt)
Branch: 1 branch
BranchTrueFalseTotal
if (!mp3c->has_mllt) 4698 2 4700
230 4698 _mp3cut_mllt_mark_frame(mp3c, frame->frame_size);
231
232 4700 ▶ if (!first_frame_found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!first_frame_found) { 28 4672 4700
233 first_frame_found = TRUE;
234 28 firstkbps = frame->bitrate_kbps;
235 28 mp3c->samples_per_frame = frame->samples_per_frame;
236 28 ▶ mp3c->max_res = frame->mpegID == MPEG1_ID ? 511 : 255;
Branch: 1 branch
BranchTrueFalseTotal
mp3c->max_res = frame->mpegID == MPEG1_ID ? 511 : 255; 4 24 28
237 28 Copy(frame, mp3c->first_frame, 1, mp3frame);
238
239 // Tweak filter to make it match frames like this one
240 28 mp3c->filter = _mp3cut_filter_for(frame);
241
242 // Parse Xing/Info/LAME tag
243 28 ▶ if ( _mp3cut_parse_xing(mp3c) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( _mp3cut_parse_xing(mp3c) ) { 26 2 28
244 26 ▶ if (mp3c->xilt_frame->xing_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->xilt_frame->xing_tag) { 8 18 26
245 DEBUG_TRACE("File has Xing tag, is VBR\n");
246 8 mp3c->is_vbr = TRUE;
247 }
248 26 frame_counter--;
249 26 ▶ if (mp3c->xilt_frame->lame_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->xilt_frame->lame_tag) { 20 6 26
250 20 mp3c->enc_delay = mp3c->xilt_frame->enc_delay;
251 20 mp3c->enc_padding = mp3c->xilt_frame->enc_padding;
252 }
253 }
254 }
255 else {
256 4672 ▶ if (frame_counter == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame_counter == 0) { 26 4646 4672
257 int sie;
258 bool pcut_frame;
259
260 check_bitrate = FALSE;
261
262 // first music frame, might be a PCUT-tag
263 // reservoir-filler frame
264 26 sie = _get_side_info_end(frame);
265
266 26 pcut_frame = (sie + 10 <= frame->frame_size)
267 26 ▶ && (bptr[sie] == 'P')
Branch: 1 branch
BranchTrueFalseTotal
&& (bptr[sie] == 'P') 2 24 26
268 2 ▶ && (bptr[sie+1] == 'C')
Branch: 1 branch
BranchTrueFalseTotal
&& (bptr[sie+1] == 'C') 2 0 2
269 2 ▶ && (bptr[sie+2] == 'U')
Branch: 1 branch
BranchTrueFalseTotal
&& (bptr[sie+2] == 'U') 2 0 2
270 28 ▶ && (bptr[sie+3] == 'T');
Branch: 2 branches
BranchTrueFalseTotal
&& (bptr[sie+3] == 'T'); 26 0 26
&& (bptr[sie+3] == 'T'); 2 0 2
271
272 if (pcut_frame) {
273 // bptr[sie+4] tag revision (always 0 for now)
274 2 int64_t t = bptr[sie + 5]; // fetch 40-bit start sample
275 2 t = (t << 8) | (bptr[sie + 6] & 0xFF);
276 2 t = (t << 8) | (bptr[sie + 7] & 0xFF);
277 2 t = (t << 8) | (bptr[sie + 8] & 0xFF);
278 2 t = (t << 8) | (bptr[sie + 9] & 0xFF);
279 2 mp3c->start_sample = t;
280
281 DEBUG_TRACE("Found PCUT tag, start sample %llu\n", mp3c->start_sample);
282 }
283 else {
284 int o, e;
285 74 ▶ for (o = _get_side_info_start(frame), e = _get_side_info_end(frame); o < e; o++) {
Branch: 1 branch
BranchTrueFalseTotal
for (o = _get_side_info_start(frame), e = _get_side_info_end(frame); o < e; o++) { 74 0 74
286 74 ▶ if (bptr[o] != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (bptr[o] != 0) { 50 24 74
287 check_bitrate = TRUE;
288 break;
289 }
290 }
291 }
292 }
293 // we don't want the first "music frame" to be checked if it's possibly a PCUT generated reservoir frame
294 4670 ▶ if (check_bitrate && frame->bitrate_kbps != firstkbps) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_bitrate && frame->bitrate_kbps != firstkbps) { 2894 1776 4670
if (check_bitrate && frame->bitrate_kbps != firstkbps) { 8 2886 2894
295 DEBUG_TRACE("Bitrate changed, file is VBR\n");
296 8 mp3c->is_vbr = TRUE;
297 check_bitrate = FALSE;
298 }
299 }
300
301 // Break out if we have a cache file
302 4700 ▶ if (mp3c->has_mllt && frame_counter >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mp3c->has_mllt && frame_counter >= 0) { 2 4698 4700
if (mp3c->has_mllt && frame_counter >= 0) { 1 1 2
303 1 frame_counter += _mp3cut_mllt_get_frame_count(mp3c);
304
305 1 ▶ if (mp3c->xilt_frame->xing_tag || mp3c->xilt_frame->info_tag)
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->xilt_frame->xing_tag || mp3c->xilt_frame->info_tag) 1 0 1
306 1 frame_counter--;
307
308 break;
309 }
310
311 4699 frame_counter++;
312
313 4699 mp3c->offset += frame->frame_size;
314 4699 ▶ if (mp3c->offset > mp3c->file_size)
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->offset > mp3c->file_size) 0 4699 4699
315 0 mp3c->offset = mp3c->file_size;
316
317 4699 _mp3cut_skip(mp3c, frame->frame_size);
318 }
319
320 28 mp3c->music_frame_count = frame_counter;
321
322 DEBUG_TRACE("Frame count: %u\n", frame_counter);
323  
324 28 Safefree(frame);
325
326 // Construct MLLT data, unless we have this from cache
327 28 ▶ if (!mp3c->has_mllt) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mp3c->has_mllt) { 27 1 28
328 27 _mp3cut_mllt_construct(mp3c);
329
330 27 mp3c->has_mllt = TRUE;
331
332 // Save cache file if requested
333 27 ▶ if (my_hv_exists(self, "cache_file")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_hv_exists(self, "cache_file")) { 1 26 27
334 1 char *file = SvPVX(*(my_hv_fetch(self, "cache_file")));
335 1 _mp3cut_mllt_save(mp3c, file);
336 }
337 }
338
339 28 return 1;
340 }
341  
342 static void
343 4701 _mp3cut_setup_filter(int filter, int *masker, int *masked)
344 {
345 4701 *masker = 0xFFE00000;
346 4701 *masked = 0xFFE00000;
347
348 4701 ▶ if ((filter & FILTER_MPEG1) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((filter & FILTER_MPEG1) != 0) { 4094 607 4701
349 4094 *masker |= 0x00180000;
350 4094 *masked |= 0x00180000;
351 }
352 607 ▶ else if ((filter & FILTER_MPEG2) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((filter & FILTER_MPEG2) != 0) { 386 221 607
353 386 *masker |= 0x00180000;
354 386 *masked |= 0x00100000;
355 }
356
357 4701 ▶ if ((filter & FILTER_LAYER1) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((filter & FILTER_LAYER1) != 0) { 0 4701 4701
358 0 *masker |= 0x00060000;
359 0 *masked |= 0x00060000;
360 }
361 4701 ▶ else if ((filter & FILTER_LAYER2) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((filter & FILTER_LAYER2) != 0) { 0 4701 4701
362 0 *masker |= 0x00060000;
363 0 *masked |= 0x00040000;
364 }
365 4701 ▶ else if ((filter & FILTER_LAYER3) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((filter & FILTER_LAYER3) != 0) { 4672 29 4701
366 4672 *masker |= 0x00060000;
367 4672 *masked |= 0x00020000;
368 }
369
370 4701 ▶ if ((filter & FILTER_32000HZ) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((filter & FILTER_32000HZ) != 0) { 280 4421 4701
371 280 *masker |= 0x00000C00;
372 280 *masked |= 0x00000800;
373 }
374 4421 ▶ else if ((filter & FILTER_44100HZ) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((filter & FILTER_44100HZ) != 0) { 3972 449 4421
375 3972 *masker |= 0x00000C00;
376 *masked |= 0x00000000;
377 }
378 449 ▶ else if ((filter & FILTER_48000HZ) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((filter & FILTER_48000HZ) != 0) { 420 29 449
379 420 *masker |= 0x00000C00;
380 420 *masked |= 0x00000400;
381 }
382
383 4701 ▶ if ((filter & FILTER_MONO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((filter & FILTER_MONO) != 0) { 280 4421 4701
384 280 *masker |= 0x000000C0;
385 280 *masked |= 0x000000C0;
386 }
387 4701 }
388  
389 int
390 28 _mp3cut_filter_for(mp3frame *frame)
391 {
392 int filter = 0;
393
394 28 ▶ if ( !frame->valid )
Branch: 1 branch
BranchTrueFalseTotal
if ( !frame->valid ) 28 0 28
395 return 0;
396
397 28 ▶ switch (frame->mpegID) {
Branch: 1 branch
BranchTrueFalseTotal
switch (frame->mpegID) { 0 28 28
398 case MPEG1_ID:
399 filter |= FILTER_MPEG1;
400 break;
401 case MPEG2_ID:
402 filter |= FILTER_MPEG2;
403 break;
404 case MPEG25_ID:
405 filter |= FILTER_MPEG25;
406 break;
407 }
408
409 28 ▶ if (frame->channels == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (frame->channels == 1) 2 26 28
410 2 filter |= FILTER_MONO;
411 else
412 26 filter |= FILTER_STEREO;
413
414 28 switch (frame->samplingrate_index) {
415 2 case SR_32000HZ:
416 2 filter |= FILTER_32000HZ;
417 2 break;
418 24 case SR_44100HZ:
419 24 filter |= FILTER_44100HZ;
420 24 break;
421 2 case SR_48000HZ:
422 2 filter |= FILTER_48000HZ;
423 2 break;
424 }
425
426 28 switch (frame->layerID) {
427 0 case LAYER1_ID:
428 0 filter |= FILTER_LAYER1;
429 0 break;
430 0 case LAYER2_ID:
431 0 filter |= FILTER_LAYER2;
432 0 break;
433 28 case LAYER3_ID:
434 28 filter |= FILTER_LAYER3;
435 28 break;
436 }
437
438 return filter;
439 }
440  
441 int
442 4728 _mp3cut_get_next_frame(mp3cut *mp3c, mp3frame *frame)
443 {
444 int masker, masked;
445 int ret = 0;
446 unsigned char *bptr;
447 int len;
448 int i = 0;
449
450 4728 ▶ if ((int)(mp3c->file_size - mp3c->offset) < 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ((int)(mp3c->file_size - mp3c->offset) < 10) { 27 4701 4728
451 // Reached the end of the file
452 27 goto out;
453 }
454
455 4701 ▶ if ( !_check_buf(mp3c->fh, mp3c->buf, 10, MP3_BLOCK_SIZE) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !_check_buf(mp3c->fh, mp3c->buf, 10, MP3_BLOCK_SIZE) ) { 0 4701 4701
456 0 goto out;
457 }
458
459 4701 _mp3cut_setup_filter(mp3c->filter, &masker, &masked);
460
461 4701 bptr = buffer_ptr(mp3c->buf);
462 4701 len = buffer_len(mp3c->buf);
463
464 6789 ▶ for (i = 0; i < len - 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len - 4; i++) { 6789 0 6789
465 6789 ▶ if ( bptr[i] == 0xFF ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( bptr[i] == 0xFF ) { 4701 2088 6789
466 // sync-word
467 4701 int header32 = bptr[i] << 24 | bptr[i+1] << 16 | bptr[i+2] << 8 | bptr[i+3];
468
469 4701 ▶ if ((header32 & masker) == masked) {
Branch: 1 branch
BranchTrueFalseTotal
if ((header32 & masker) == masked) { 4701 0 4701
470 // header
471 DEBUG_TRACE("header @ %d: %x\n", i, header32);
472
473 4701 ▶ if ( _mp3cut_decode_frame(header32, frame) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( _mp3cut_decode_frame(header32, frame) ) { 4701 0 4701
474 // Abort if this is not an MP3 frame, we can't process layer 1 or layer 2 files
475 4701 ▶ if (frame->layerID != LAYER3_ID)
Branch: 1 branch
BranchTrueFalseTotal
if (frame->layerID != LAYER3_ID) 1 4700 4701
476 1 croak("Cannot gaplessly process file, the first frame was not an MP3 frame.\n");
477
478 // valid frame, skip to it in the buffer
479 4700 buffer_consume(mp3c->buf, i);
480 4700 mp3c->offset += i;
481
482 // Remember the first frame's offset
483 4700 ▶ if (mp3c->first_frame_offset == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->first_frame_offset == -1) 28 4672 4700
484 28 mp3c->first_frame_offset = mp3c->offset;
485
486 DEBUG_TRACE("Frame @ %d: %dkbps %dkHz size %d\n", mp3c->offset, frame->bitrate_kbps, frame->samplerate, frame->frame_size);
487
488 ret = 1;
489 4700 goto out;
490 }
491 }
492 }
493 }
494
495 0 buffer_clear(mp3c->buf);
496
497 4727 out:
498 4727 return ret;
499 }
500  
501 int
502 6506 _mp3cut_decode_frame(int header32, mp3frame *frame)
503 {
504 6506 frame->header32 = header32;
505
506 6506 frame->mpegID = (frame->header32 >> 19) & 3;
507 6506 frame->layerID = (frame->header32 >> 17) & 3;
508 6506 frame->crc16_used = (frame->header32 & 0x00010000) == 0;
509 6506 frame->bitrate_index = (frame->header32 >> 12) & 0xF;
510 6506 frame->samplingrate_index = (frame->header32 >> 10) & 3;
511 6506 frame->padding = (frame->header32 & 0x00000200) != 0;
512 6506 frame->private_bit_set = (frame->header32 & 0x00000100) != 0;
513 6506 frame->mode = (frame->header32 >> 6) & 3;
514 6506 frame->mode_extension = (frame->header32 >> 4) & 3;
515 6506 frame->copyrighted = (frame->header32 & 0x00000008) != 0;
516 6506 frame->original = (frame->header32 & 0x00000004) == 0; // bit set -> copy
517 6506 frame->emphasis = frame->header32 & 3;
518
519 6506 frame->valid = (frame->mpegID != ILLEGAL_MPEG_ID)
520 6506 ▶ && (frame->layerID != ILLEGAL_LAYER_ID)
Branch: 1 branch
BranchTrueFalseTotal
&& (frame->layerID != ILLEGAL_LAYER_ID) 6506 0 6506
521 6506 ▶ && (frame->bitrate_index != 0)
Branch: 1 branch
BranchTrueFalseTotal
&& (frame->bitrate_index != 0) 6506 0 6506
522 6506 ▶ && (frame->bitrate_index != 15)
Branch: 1 branch
BranchTrueFalseTotal
&& (frame->bitrate_index != 15) 6506 0 6506
523 13012 ▶ && (frame->samplingrate_index != ILLEGAL_SR);
Branch: 2 branches
BranchTrueFalseTotal
&& (frame->samplingrate_index != ILLEGAL_SR); 6506 0 6506
&& (frame->samplingrate_index != ILLEGAL_SR); 0 6506 6506
524
525 6506 ▶ if (!frame->valid) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frame->valid) { 6506 0 6506
526 return 0;
527 }
528
529 6506 frame->samplerate = sample_rate_tbl[ frame->samplingrate_index ];
530
531 6506 ▶ if (frame->mpegID == MPEG2_ID)
Branch: 1 branch
BranchTrueFalseTotal
if (frame->mpegID == MPEG2_ID) 514 5992 6506
532 514 frame->samplerate >>= 1; // 16,22,48 kHz
533
534 6506 ▶ if (frame->mpegID == MPEG25_ID)
Branch: 1 branch
BranchTrueFalseTotal
if (frame->mpegID == MPEG25_ID) 283 6223 6506
535 283 frame->samplerate >>= 2; // 8,11,24 kHz
536
537 6506 ▶ frame->channels = (frame->mode == MODE_MONO) ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
frame->channels = (frame->mode == MODE_MONO) ? 1 : 2; 6125 381 6506
538
539 6506 frame->bitrate_kbps = bitrate_map[ frame->mpegID ][ frame->layerID ][ frame->bitrate_index ];
540
541 6506 ▶ if (frame->layerID == LAYER1_ID) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->layerID == LAYER1_ID) { 0 6506 6506
542 // layer 1: always 384 samples/frame and 4byte-slots
543 0 frame->samples_per_frame = 384;
544 0 frame->bytes_per_slot = 4;
545 }
546 else {
547 // layer 2: always 1152 samples/frame
548 // layer 3: MPEG1: 1152 samples/frame, MPEG2/2.5: 576 samples/frame
549 6506 ▶ frame->samples_per_frame = ((frame->mpegID == MPEG1_ID) || (frame->layerID == LAYER2_ID)) ? 1152 : 576;
Branch: 2 branches
BranchTrueFalseTotal
frame->samples_per_frame = ((frame->mpegID == MPEG1_ID) || (frame->layerID == LAYER2_ID)) ? 1152 : 576; 797 5709 6506
frame->samples_per_frame = ((frame->mpegID == MPEG1_ID) || (frame->layerID == LAYER2_ID)) ? 1152 : 576; 0 797 797
550 6506 frame->bytes_per_slot = 1;
551 }
552
553 6506 frame->frame_size = ((frame->bitrate_kbps * 125) * frame->samples_per_frame) / frame->samplerate;
554
555 6506 ▶ if (frame->bytes_per_slot > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (frame->bytes_per_slot > 1) 0 6506 6506
556 0 frame->frame_size -= frame->frame_size % frame->bytes_per_slot;
557
558 6506 ▶ if (frame->padding)
Branch: 1 branch
BranchTrueFalseTotal
if (frame->padding) 3497 3009 6506
559 3497 frame->frame_size += frame->bytes_per_slot;
560  
561 return 1;
562 }
563  
564 int
565 28 _mp3cut_parse_xing(mp3cut *mp3c)
566 {
567 mp3frame frame;
568 int ofs = 0;
569 28 unsigned char *data = buffer_ptr(mp3c->buf);
570 28 int fh32 = get_u32(data);
571 uint8_t flags;
572 int tag_end_ofs;
573 int crc = 0;
574 int i, t;
575
576 28 _mp3cut_decode_frame(fh32, &frame);
577
578 28 ofs += 4 + _get_side_info_size(&frame);
579
580 28 ▶ mp3c->xilt_frame->xing_tag = (data[ofs] == 'X' && data[ofs+1] == 'i' && data[ofs+2] == 'n' && data[ofs+3] == 'g');
Branch: 4 branches
BranchTrueFalseTotal
mp3c->xilt_frame->xing_tag = (data[ofs] == 'X' && data[ofs+1] == 'i' && data[ofs+2] == 'n' && data[ofs+3] == 'g'); 8 20 28
mp3c->xilt_frame->xing_tag = (data[ofs] == 'X' && data[ofs+1] == 'i' && data[ofs+2] == 'n' && data[ofs+3] == 'g'); 8 0 8
mp3c->xilt_frame->xing_tag = (data[ofs] == 'X' && data[ofs+1] == 'i' && data[ofs+2] == 'n' && data[ofs+3] == 'g'); 8 0 8
mp3c->xilt_frame->xing_tag = (data[ofs] == 'X' && data[ofs+1] == 'i' && data[ofs+2] == 'n' && data[ofs+3] == 'g'); 0 8 8
581 28 ▶ mp3c->xilt_frame->info_tag = (data[ofs] == 'I' && data[ofs+1] == 'n' && data[ofs+2] == 'f' && data[ofs+3] == 'o');
Branch: 4 branches
BranchTrueFalseTotal
mp3c->xilt_frame->info_tag = (data[ofs] == 'I' && data[ofs+1] == 'n' && data[ofs+2] == 'f' && data[ofs+3] == 'o'); 18 10 28
mp3c->xilt_frame->info_tag = (data[ofs] == 'I' && data[ofs+1] == 'n' && data[ofs+2] == 'f' && data[ofs+3] == 'o'); 18 0 18
mp3c->xilt_frame->info_tag = (data[ofs] == 'I' && data[ofs+1] == 'n' && data[ofs+2] == 'f' && data[ofs+3] == 'o'); 18 0 18
mp3c->xilt_frame->info_tag = (data[ofs] == 'I' && data[ofs+1] == 'n' && data[ofs+2] == 'f' && data[ofs+3] == 'o'); 0 18 18
582 28 mp3c->xilt_frame->lame_tag = FALSE;
583 28 mp3c->xilt_frame->frame_count = 0;
584
585 #ifdef AUDIO_SCAN_DEBUG
586 if (mp3c->xilt_frame->xing_tag) {
587 DEBUG_TRACE("Valid Xing tag found\n");
588 }
589 else if (mp3c->xilt_frame->info_tag) {
590 DEBUG_TRACE("Valid Info tag found\n");
591 }
592 #endif
593
594 28 ▶ if ( !mp3c->xilt_frame->xing_tag && !mp3c->xilt_frame->info_tag ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( !mp3c->xilt_frame->xing_tag && !mp3c->xilt_frame->info_tag ) { 20 8 28
if ( !mp3c->xilt_frame->xing_tag && !mp3c->xilt_frame->info_tag ) { 18 2 20
595 DEBUG_TRACE("No Xing/Info tag found\n");
596 return 0;
597 }
598
599 ofs += 4;
600
601 26 mp3c->xilt_frame->frame_size = frame.frame_size;
602
603 26 buffer_init(mp3c->xilt_frame->tag, frame.frame_size);
604 26 Copy( buffer_ptr(mp3c->buf), buffer_ptr(mp3c->xilt_frame->tag), frame.frame_size, uint8_t );
605 26 mp3c->xilt_frame->tag->end = frame.frame_size;
606
607 26 flags = data[ofs+3] & 0xFF;
608 26 ofs += 4;
609
610 26 ▶ if ((flags & 0x01) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & 0x01) != 0) { 26 0 26
611 26 unsigned char *f = data + ofs;
612 26 mp3c->xilt_frame->frame_count = GET_INT32BE(f);
613 26 ofs += 4;
614 DEBUG_TRACE("Xing Frames: %d\n", mp3c->xilt_frame->frame_count);
615 }
616
617 26 ▶ if ((flags & 0x02) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & 0x02) != 0) 26 0 26
618 26 ofs += 4; // skip byte count
619 26 ▶ if ((flags & 0x04) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & 0x04) != 0) 24 2 26
620 24 ofs += 100; // skip seek table
621 26 ▶ if ((flags & 0x08) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & 0x08) != 0) 24 2 26
622 24 ofs += 4; // skip VBR scale
623
624 tag_end_ofs = ofs + 0x24;
625
626 4668 ▶ for (i = 0; i < tag_end_ofs - 2; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tag_end_ofs - 2; i++) 4642 26 4668
627 4642 crc = _crc16(crc, data[i] & 0xFF);
628
629 26 ▶ mp3c->xilt_frame->lame_tag = (data[ofs] == 'L' && data[ofs+1] == 'A' && data[ofs+2] == 'M' && data[ofs+3] == 'E');
Branch: 4 branches
BranchTrueFalseTotal
mp3c->xilt_frame->lame_tag = (data[ofs] == 'L' && data[ofs+1] == 'A' && data[ofs+2] == 'M' && data[ofs+3] == 'E'); 22 4 26
mp3c->xilt_frame->lame_tag = (data[ofs] == 'L' && data[ofs+1] == 'A' && data[ofs+2] == 'M' && data[ofs+3] == 'E'); 22 0 22
mp3c->xilt_frame->lame_tag = (data[ofs] == 'L' && data[ofs+1] == 'A' && data[ofs+2] == 'M' && data[ofs+3] == 'E'); 22 0 22
mp3c->xilt_frame->lame_tag = (data[ofs] == 'L' && data[ofs+1] == 'A' && data[ofs+2] == 'M' && data[ofs+3] == 'E'); 0 22 22
630
631 26 ▶ if ( !mp3c->xilt_frame->lame_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !mp3c->xilt_frame->lame_tag ) { 4 22 26
632 8 ▶ mp3c->xilt_frame->lame_tag = (data[ofs] = 'G' && data[ofs+1] == 'O' && data[ofs+2] == 'G' && data[ofs+3] == 'O');
Branch: 3 branches
BranchTrueFalseTotal
mp3c->xilt_frame->lame_tag = (data[ofs] = 'G' && data[ofs+1] == 'O' && data[ofs+2] == 'G' && data[ofs+3] == 'O'); 0 4 4
mp3c->xilt_frame->lame_tag = (data[ofs] = 'G' && data[ofs+1] == 'O' && data[ofs+2] == 'G' && data[ofs+3] == 'O'); 0 0 0
mp3c->xilt_frame->lame_tag = (data[ofs] = 'G' && data[ofs+1] == 'O' && data[ofs+2] == 'G' && data[ofs+3] == 'O'); 0 0 0
633 }
634
635 // Verify CRC16
636 26 ▶ if (((((data[tag_end_ofs - 2] << 8) | (data[tag_end_ofs - 1] & 0xFF)) ^ crc) & 0xFFFF) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (((((data[tag_end_ofs - 2] << 8) | (data[tag_end_ofs - 1] & 0xFF)) ^ crc) & 0xFFFF) != 0) { 6 20 26
637 6 mp3c->xilt_frame->lame_tag = FALSE;
638 }
639
640 26 ▶ if (mp3c->xilt_frame->lame_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->xilt_frame->lame_tag) { 20 6 26
641 20 mp3c->xilt_frame->lame_tag_offset = ofs - 4;
642 DEBUG_TRACE("Valid LAME tag found\n");
643 }
644
645 // Read delay/padding
646 26 ofs += 0x15;
647 26 t = data[ofs+1] & 0xFF;
648 26 mp3c->xilt_frame->enc_delay = ((data[ofs] & 0xFF) << 4) | (t >> 4);
649 26 mp3c->xilt_frame->enc_padding = ((t & 0x0F) << 8) | (data[ofs+2] & 0xFF);
650
651 26 ▶ if ( !mp3c->xilt_frame->lame_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !mp3c->xilt_frame->lame_tag ) { 6 20 26
652 6 ▶ if (mp3c->xilt_frame->enc_delay > 2880 || mp3c->xilt_frame->enc_padding > 2304) {
Branch: 2 branches
BranchTrueFalseTotal
if (mp3c->xilt_frame->enc_delay > 2880 || mp3c->xilt_frame->enc_padding > 2304) { 6 0 6
if (mp3c->xilt_frame->enc_delay > 2880 || mp3c->xilt_frame->enc_padding > 2304) { 6 0 6
653 0 mp3c->xilt_frame->enc_delay = 576;
654 0 mp3c->xilt_frame->enc_padding = 0;
655 }
656 }
657
658 DEBUG_TRACE(" LAME delay %d, padding %d\n", mp3c->xilt_frame->enc_delay, mp3c->xilt_frame->enc_padding);
659
660 return 1;
661 }
662  
663 void
664 4698 _mp3cut_mllt_mark_frame(mp3cut *mp3c, uint16_t frame_size)
665 {
666 // Track min/max frame size
667 4698 ▶ if (frame_size > mp3c->max_frame_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame_size > mp3c->max_frame_size) { 56 4642 4698
668 56 mp3c->max_frame_size = frame_size;
669 DEBUG_TRACE("max_frame_size set to %d\n", mp3c->max_frame_size);
670 }
671
672 4698 ▶ if (!mp3c->min_frame_size || frame_size < mp3c->min_frame_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (!mp3c->min_frame_size || frame_size < mp3c->min_frame_size) { 4671 27 4698
if (!mp3c->min_frame_size || frame_size < mp3c->min_frame_size) { 28 4643 4671
673 55 mp3c->min_frame_size = frame_size;
674 DEBUG_TRACE("min_frame_size set to %d\n", mp3c->min_frame_size);
675 }
676
677 4698 mp3c->last_frame_size = frame_size;
678
679 // Store frame offset, this assumes we are called in frame order
680 4698 buffer_put_int(mp3c->mllt_buf, mp3c->offset);
681 4698 }
682  
683 void
684 27 _mp3cut_mllt_construct(mp3cut *mp3c)
685 {
686 uint16_t max_frame_deviation = 0;
687 uint32_t ms_per_frame = 0;
688 uint8_t bits_for_bytes = 0;
689 27 unsigned char *bptr = buffer_ptr(mp3c->mllt_buf);
690 27 uint32_t mllt_buf_len = buffer_len(mp3c->mllt_buf);
691 uint32_t o = 0; // 32-bit offsets
692 uint32_t m = 0; // packed MLLT offsets
693 uint32_t f = 0; // frame count
694
695 /*
696 MPEG frames between reference $xx xx $00 $01
697 Bytes between reference $xx xx xx <max frame size in file>
698 Milliseconds between reference $xx xx xx <ms per frame (rounded)>
699 Bits for bytes deviation $xx <max needed to represent max deviation>
700 Bits for milliseconds dev. $xx $00 (ms/frame is constant)
701 (bits + bits must = multiple of 4)
702  
703 Then for every reference the following data is included;
704 Deviation in bytes %xxx....
705 Deviation in milliseconds %xxx....
706 */
707
708 27 max_frame_deviation = mp3c->max_frame_size - mp3c->min_frame_size;
709 27 ms_per_frame = (int)(mp3c->first_frame->samplerate / mp3c->first_frame->samples_per_frame);
710
711 // Should never have a deviation > 4095 (12 bits)
712 27 ▶ if (max_frame_deviation > 0xFF)
Branch: 1 branch
BranchTrueFalseTotal
if (max_frame_deviation > 0xFF) 19 8 27
713 bits_for_bytes = 12;
714 19 ▶ else if (max_frame_deviation > 0xF)
Branch: 1 branch
BranchTrueFalseTotal
else if (max_frame_deviation > 0xF) 17 2 19
715 bits_for_bytes = 8;
716 else
717 bits_for_bytes = 4;
718
719 // We could get even more efficient here for CBR files,
720 // but this is necessary to match the MLLT spec which requires multiples of 4
721
722 DEBUG_TRACE("max_frame_deviation %d, ms_per_frame %d, bits_for_bytes %d\n", max_frame_deviation, ms_per_frame, bits_for_bytes);
723
724 // Replace the 32-bit offset values in mllt_buf with packed byte deviation data
725 4725 ▶ for (o = 0; o <= mllt_buf_len - 4; o += 4) {
Branch: 1 branch
BranchTrueFalseTotal
for (o = 0; o <= mllt_buf_len - 4; o += 4) { 4698 27 4725
726 4698 uint32_t offset = get_u32(&bptr[o]);
727 uint32_t next_offset;
728
729 4698 ▶ if (o <= mllt_buf_len - 8)
Branch: 1 branch
BranchTrueFalseTotal
if (o <= mllt_buf_len - 8) 4671 27 4698
730 4671 next_offset = get_u32(&bptr[o+4]);
731 else
732 27 next_offset = offset + mp3c->last_frame_size;
733
734 4698 ▶ if (bits_for_bytes == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits_for_bytes == 8) { 314 4384 4698
735 // simple case
736 // XXX need test
737 314 bptr[m++] = (uint8_t)(mp3c->max_frame_size - (next_offset - offset));
738 }
739 4384 ▶ else if (bits_for_bytes == 4) {
Branch: 1 branch
BranchTrueFalseTotal
else if (bits_for_bytes == 4) { 2902 1482 4384
740 2902 uint8_t dev = (mp3c->max_frame_size - (next_offset - offset)) & 0xF;
741 2902 ▶ if (f % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (f % 2 == 0) { 1453 1449 2902
742 1453 bptr[m] = dev;
743 }
744 else {
745 1449 bptr[m] = (bptr[m] << 4) | dev;
746 1449 m++;
747 }
748 }
749 else { // 12 bits
750 1482 uint16_t dev = (uint16_t)(mp3c->max_frame_size - (next_offset - offset)) & 0xFFF;
751 1482 ▶ if (f % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (f % 2 == 0) { 744 738 1482
752 744 bptr[m] = (dev >> 4) & 0xFF;
753 744 bptr[m+1] = (dev & 0xF) << 4;
754 744 m += 2;
755 }
756 else {
757 738 bptr[m-1] = bptr[m-1] | ((dev >> 8) & 0xF);
758 738 bptr[m] = dev & 0xFF;
759 738 m++;
760 }
761 }
762
763 4698 f++;
764 }
765
766 // Add space for the 10-byte MLLT header info
767 27 Move(mp3c->mllt_buf->buf, mp3c->mllt_buf->buf + 10, m, unsigned char);
768
769 // Construct MLLT header
770 27 put_u16(&bptr[0], 1); // Frames between reference, always 1
771 27 put_u24(&bptr[2], mp3c->max_frame_size); // Bytes between reference
772 27 put_u24(&bptr[5], ms_per_frame); // Milliseconds between reference
773 27 bptr[8] = bits_for_bytes; // Bits for bytes deviation
774 27 bptr[9] = 0; // Bits for milliseconds deviation
775
776 27 mp3c->mllt_buf->end = m + 10;
777
778 DEBUG_TRACE("MLLT data encoded to %d bytes\n", buffer_len(mp3c->mllt_buf));
779 //buffer_dump(mp3c->mllt_buf, 0);
780 27 }
781  
782 uint32_t
783 1 _mp3cut_mllt_get_frame_count(mp3cut *mp3c)
784 {
785 1 unsigned char *bptr = buffer_ptr(mp3c->mllt_buf);
786 1 uint8_t bits_for_bytes = bptr[8];
787
788 1 return ((buffer_len(mp3c->mllt_buf) - 10) * 8) / bits_for_bytes;
789 }
790  
791 void
792 1 _mp3cut_mllt_save(mp3cut *mp3c, const char *file)
793 {
794 1 PerlIO *cache = PerlIO_open(file, "w");
795 1 ▶ if (cache == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache == NULL) { 0 1 1
796 0 warn("Unable to open cache file %s for writing: %s\n", file, strerror(errno));
797 }
798 else {
799 1 unsigned char *bptr = buffer_ptr(mp3c->mllt_buf);
800 int n = 1;
801 1 int buf_size = buffer_len(mp3c->mllt_buf);
802
803 2 ▶ while (n > 0 && buf_size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (n > 0 && buf_size > 0) { 1 1 2
804 1 n = PerlIO_write(cache, bptr, buf_size > MP3_BLOCK_SIZE ? MP3_BLOCK_SIZE : buf_size);
805 1 bptr += n;
806 1 buf_size -= n;
807 }
808
809 if (n < 0) {
810 // XXX error writing, wipe cache file and warn
811 }
812 else {
813 DEBUG_TRACE("Saved MLLT cache file\n");
814 }
815
816 1 PerlIO_close(cache);
817 }
818 1 }
819  
820 void
821 3 _mp3cut_mllt_load(mp3cut *mp3c, const char *file)
822 {
823 3 PerlIO *cache = PerlIO_open(file, "r");
824 3 ▶ if (cache == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache == NULL) { 1 2 3
825 //warn("Unable to open cache file %s for reading: %s\n", file, strerror(errno));
826 }
827 else {
828 int read;
829 1 off_t mllt_file_size = _file_size(cache);
830 1 void *buf = buffer_append_space(mp3c->mllt_buf, mllt_file_size);
831
832 1 ▶ if ( (read = PerlIO_read(cache, buf, mllt_file_size)) != mllt_file_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (read = PerlIO_read(cache, buf, mllt_file_size)) != mllt_file_size ) { 0 1 1
833 0 ▶ if ( PerlIO_error(cache) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( PerlIO_error(cache) ) { 0 0 0
834 0 warn("Error reading cache file: %s\n", strerror(errno));
835 }
836 else {
837 0 warn("Error: Unable to read entire cache file.\n");
838 }
839 0 PerlIO_close(cache);
840 0 return;
841 }
842
843 1 PerlIO_close(cache);
844
845 1 mp3c->has_mllt = TRUE;
846
847 DEBUG_TRACE("Loaded MLLT cache file\n");
848 }
849 }
850
851 void
852 58 _mp3cut_skip_id3v2(mp3cut *mp3c)
853 {
854 unsigned char *bptr;
855 uint32_t id3_size = 0;
856
857 58 ▶ if ( !_check_buf(mp3c->fh, mp3c->buf, 10, MP3_BLOCK_SIZE) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !_check_buf(mp3c->fh, mp3c->buf, 10, MP3_BLOCK_SIZE) ) { 58 0 58
858 return;
859 }
860
861 58 bptr = buffer_ptr(mp3c->buf);
862 58 if (
863 58 ▶ (bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') &&
Branch: 3 branches
BranchTrueFalseTotal
(bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') && 12 46 58
(bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') && 12 0 12
(bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') && 12 0 12
864 12 ▶ bptr[3] < 0xff && bptr[4] < 0xff &&
Branch: 2 branches
BranchTrueFalseTotal
bptr[3] < 0xff && bptr[4] < 0xff && 12 0 12
bptr[3] < 0xff && bptr[4] < 0xff && 12 0 12
865 12 ▶ bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80
Branch: 4 branches
BranchTrueFalseTotal
bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80 12 0 12
bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80 12 0 12
bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80 12 0 12
bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80 12 0 12
866 ) {
867 12 id3_size = 10 + (bptr[6]<<21) + (bptr[7]<<14) + (bptr[8]<<7) + bptr[9];
868  
869 12 ▶ if (bptr[5] & 0x10) {
Branch: 1 branch
BranchTrueFalseTotal
if (bptr[5] & 0x10) { 0 12 12
870 // footer present
871 0 id3_size += 10;
872 }
873
874 DEBUG_TRACE("Skipping ID3v2 tag, size %d\n", id3_size);
875
876 12 _mp3cut_skip(mp3c, id3_size);
877
878 12 mp3c->offset = id3_size;
879 }
880 }
881  
882 void
883 5928 _mp3cut_skip(mp3cut *mp3c, uint32_t size)
884 {
885 5928 ▶ if ( buffer_len(mp3c->buf) >= size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( buffer_len(mp3c->buf) >= size ) { 5666 262 5928
886 5666 buffer_consume(mp3c->buf, size);
887
888 //DEBUG_TRACE(" skipped buffer data size %d\n", size);
889 }
890 else {
891 262 PerlIO_seek(mp3c->fh, size - buffer_len(mp3c->buf), SEEK_CUR);
892 262 buffer_clear(mp3c->buf);
893
894 //DEBUG_TRACE(" seeked past %d bytes to %d\n", size, (int)PerlIO_tell(mp3c->fh));
895 }
896 5928 }
897  
898 int
899 153 _mp3cut_read(HV *self, mp3cut *mp3c, SV *buf, int buf_size)
900 {
901 // Initialize buf SV
902 153 sv_setpvn(buf, "", 0);
903
904 153 ▶ if ( mp3c->next_processed_frame == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mp3c->next_processed_frame == 0 ) { 29 124 153
905 // Beginning of file, add pre-frames and Xing/LAME tag
906 int need_bytes_from_res;
907 int got_bytes_from_res = 0;
908 uint32_t need_pre_frames = 0;
909 uint32_t first_frame_num = 0;
910 uint64_t start_sample = 0;
911 uint64_t end_sample = 0;
912 29 uint64_t sample_count = mp3c->music_frame_count * mp3c->samples_per_frame - mp3c->enc_delay - mp3c->enc_padding;
913 Buffer res_frame;
914 Buffer seektable;
915 Buffer xing_frame;
916
917 29 mp3c->bit_res = 0;
918
919 // Reset input buffer
920 29 buffer_clear(mp3c->buf);
921 29 PerlIO_seek(mp3c->fh, 0, SEEK_SET);
922 29 mp3c->offset = 0;
923
924 // Seek past ID3 tag
925 29 _mp3cut_skip_id3v2(mp3c);
926
927 DEBUG_TRACE("sample_count %llu\n", sample_count);
928
929 // Convert milliseconds to samples
930 29 ▶ if (my_hv_exists(self, "start_ms")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_hv_exists(self, "start_ms")) { 29 0 29
931 29 uint32_t start_ms = SvIV(*(my_hv_fetch(self, "start_ms")));
932 29 start_sample = (int)((start_ms * 1.0 / 10) * (mp3c->first_frame->samplerate * 1.0 / 100));
933 DEBUG_TRACE("start_sample %llu\n", start_sample);
934 }
935
936 29 ▶ if (my_hv_exists(self, "end_ms")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_hv_exists(self, "end_ms")) { 28 1 29
937 28 uint32_t end_ms = SvIV(*(my_hv_fetch(self, "end_ms")));
938 28 end_sample = (int)((end_ms * 1.0 / 10) * (mp3c->first_frame->samplerate * 1.0 / 100));
939 DEBUG_TRACE("end_sample %llu\n", end_sample);
940 }
941 else {
942 end_sample = sample_count;
943 }
944  
945 //start_sample = MAX(start_sample, mp3c->enc_delay); // XXX pcutmp3 has -encDelay here, bug?
946 29 end_sample = MIN(end_sample, sample_count);
947  
948 DEBUG_TRACE("Sample count %llu, actual start sample %llu, actual end sample %llu, orig enc delay %u, orig enc padding %u\n",
949 sample_count, start_sample, end_sample, mp3c->enc_delay, mp3c->enc_padding);
950  
951 29 mp3c->first_frame_inclusive = MAX(0, (int)((start_sample + mp3c->enc_delay - MIN_OVERLAP_SAMPLES_START) / mp3c->samples_per_frame));
952 29 mp3c->last_frame_exclusive = MIN(mp3c->music_frame_count, (int)((end_sample + mp3c->enc_delay + MIN_OVERLAP_SAMPLES_END + mp3c->samples_per_frame - 1) / mp3c->samples_per_frame));
953 29 mp3c->new_enc_delay = mp3c->enc_delay + (int)(start_sample - mp3c->first_frame_inclusive * mp3c->samples_per_frame);
954 29 mp3c->new_enc_padding = (mp3c->last_frame_exclusive - mp3c->first_frame_inclusive) * mp3c->samples_per_frame - mp3c->new_enc_delay - (end_sample - start_sample);
955  
956 DEBUG_TRACE("first_frame_inclusive %u, last_frame_exclusive %u, new_enc_delay %u, new_enc_padding %u\n",
957 mp3c->first_frame_inclusive, mp3c->last_frame_exclusive, mp3c->new_enc_delay, mp3c->new_enc_padding);
958
959 29 mp3c->mask_ath = 0xFF;
960 29 ▶ if (start_sample != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (start_sample != 0) 15 14 29
961 15 mp3c->mask_ath &= MASK_ATH_KILL_NO_GAP_START;
962  
963 29 ▶ if (end_sample != sample_count)
Branch: 1 branch
BranchTrueFalseTotal
if (end_sample != sample_count) 28 1 29
964 28 mp3c->mask_ath &= MASK_ATH_KILL_NO_GAP_END;
965
966 first_frame_num = mp3c->first_frame_inclusive;
967
968 29 _set_curr_frame(mp3c, mp3c->first_frame_inclusive);
969 29 need_bytes_from_res = _get_bit_res_ptr(mp3c);
970
971 29 while (
972 48 mp3c->first_frame_inclusive - need_pre_frames > 0
973 34 ▶ && need_bytes_from_res > got_bytes_from_res
Branch: 1 branch
BranchTrueFalseTotal
&& need_bytes_from_res > got_bytes_from_res 19 15 34
974 67 ▶ && mp3c->new_enc_delay + 1152 <= 4095
Branch: 2 branches
BranchTrueFalseTotal
&& mp3c->new_enc_delay + 1152 <= 4095 34 14 48
&& mp3c->new_enc_delay + 1152 <= 4095 19 0 19
975 ) {
976 19 need_pre_frames++;
977 19 _set_curr_frame(mp3c, mp3c->first_frame_inclusive - need_pre_frames);
978 19 got_bytes_from_res += _get_main_data_size(mp3c);
979 }
980
981 DEBUG_TRACE("need_pre_frames: %d, got_bytes_from_res %d\n", need_pre_frames, got_bytes_from_res);
982
983 if (need_pre_frames == 0) {
984 // force writing of PCUT tag frame
985 need_pre_frames = 1;
986 }
987
988 if (need_pre_frames > 0) {
989 uint64_t new_abs_start_sample = start_sample;
990
991 29 first_frame_num--;
992 29 mp3c->new_enc_delay += mp3c->samples_per_frame;
993
994 29 buffer_init(&res_frame, MAX_FRAME_SIZE);
995
996 // Keep absolute start sample from previous PCUT tag if any
997 29 ▶ if (mp3c->start_sample != UNKNOWN_START_SAMPLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->start_sample != UNKNOWN_START_SAMPLE) { 2 27 29
998 2 new_abs_start_sample += mp3c->start_sample;
999 }
1000
1001 29 _mp3cut_construct_reservoir_frame(mp3c, &res_frame, need_bytes_from_res, new_abs_start_sample);
1002 }
1003
1004 // Construct new Xing seektable
1005 {
1006 int ofs00, ofsXX, i;
1007 float avg_bytes_per_frame;
1008 float avg_bytes_per_sec;
1009 unsigned char *seekptr;
1010
1011 29 buffer_init(&seektable, 100);
1012 29 seekptr = buffer_ptr(&seektable);
1013
1014 29 _set_curr_frame(mp3c, MAX(0, mp3c->last_frame_exclusive - 1)); // for ofsXX
1015
1016 29 ofs00 = _get_frame_file_offset(mp3c, mp3c->first_frame_inclusive) - buffer_len(&res_frame);
1017 29 ofsXX = _get_frame_file_offset(mp3c, MAX(0, mp3c->last_frame_exclusive - 1)) + _get_frame_size(mp3c);
1018 29 mp3c->musi_len = ofsXX - ofs00;
1019 29 avg_bytes_per_frame = (ofsXX * 1.0 - ofs00) / (mp3c->last_frame_exclusive - mp3c->first_frame_inclusive);
1020 29 avg_bytes_per_sec = avg_bytes_per_frame * mp3c->first_frame->samplerate / mp3c->first_frame->samples_per_frame;
1021 29 mp3c->avg_kbps = avg_bytes_per_sec / 125.0;
1022
1023 DEBUG_TRACE("ofs00 %d, ofsXX %d, musi_len %d\n", ofs00, ofsXX, mp3c->musi_len);
1024 DEBUG_TRACE("avg_bytes_per_frame %f, avg_bytes_per_sec %f, avg_kbps %f\n", avg_bytes_per_frame, avg_bytes_per_sec, mp3c->avg_kbps);
1025
1026 2929 ▶ for (i = 0; i < 100; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 100; i++) { 2900 29 2929
1027 2900 int fidx = (int)( 0.5 + (mp3c->first_frame_inclusive + (i + 1.0) / 101 * (mp3c->last_frame_exclusive - mp3c->first_frame_inclusive)) );
1028 2900 seekptr[i] = (uint8_t)( 0.5 + ((_get_frame_file_offset(mp3c, MAX(0, fidx)) - ofs00) * 255.0 / (ofsXX - ofs00)) );
1029 //DEBUG_TRACE(" fidx %d, seekptr[%d] %d\n", fidx, i, seekptr[i]);
1030 }
1031
1032 29 seektable.end = 100;
1033 }
1034
1035 // create new Xing/Info/LAME tag
1036 29 _mp3cut_construct_xing_frame(mp3c, &xing_frame, mp3c->last_frame_exclusive - first_frame_num, &seektable);
1037 29 sv_catpvn(buf, buffer_ptr(&xing_frame), buffer_len(&xing_frame));
1038 29 buf_size -= buffer_len(&xing_frame);
1039 29 buffer_free(&xing_frame);
1040 29 buffer_free(&seektable);
1041
1042 // add pre frame(s)
1043 if (need_pre_frames > 0) {
1044 DEBUG_TRACE("preframe, need_bytes_from_res %d\n", need_bytes_from_res);
1045 29 ▶ if (need_bytes_from_res > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (need_bytes_from_res > 0) { 16 13 29
1046 Buffer res;
1047 int fi;
1048
1049 16 buffer_init(&res, 511);
1050
1051 35 ▶ for (fi = mp3c->first_frame_inclusive - need_pre_frames; fi < mp3c->first_frame_inclusive; fi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (fi = mp3c->first_frame_inclusive - need_pre_frames; fi < mp3c->first_frame_inclusive; fi++) { 19 16 35
1052 int fl, mdss;
1053
1054 DEBUG_TRACE(" Handling res for frame %d\n", fi);
1055
1056 19 _set_curr_frame(mp3c, fi);
1057 fl = _get_frame_size(mp3c);
1058 19 mdss = _get_main_data_size(mp3c);
1059
1060 19 ▶ if (mdss >= 511) {
Branch: 1 branch
BranchTrueFalseTotal
if (mdss >= 511) { 1 18 19
1061 DEBUG_TRACE(" mdss %d, copying 511 bytes at offset %d\n", mdss, fl - 511);
1062 1 Copy((char *)buffer_ptr(mp3c->buf) + fl - 511, (char *)buffer_ptr(&res), 511, uint8_t);
1063 }
1064 else { // XXX need test
1065 18 int move = 511 - mdss;
1066 DEBUG_TRACE(" mdss %d, moving %d bytes\n", mdss, move);
1067
1068 18 Move((char *)buffer_ptr(&res) + 511 - move, (char *)buffer_ptr(&res), move, uint8_t);
1069 18 Copy((char *)buffer_ptr(mp3c->buf) + fl - mdss, (char *)buffer_ptr(&res) + move, mdss, uint8_t);
1070 }
1071 }
1072
1073 16 Copy(
1074 (char *)buffer_ptr(&res) + 511 - need_bytes_from_res,
1075 (char *)buffer_ptr(&res_frame) + buffer_len(&res_frame) - need_bytes_from_res,
1076 need_bytes_from_res,
1077 uint8_t
1078 );
1079
1080 16 buffer_free(&res);
1081 }
1082
1083 29 sv_catpvn(buf, buffer_ptr(&res_frame), buffer_len(&res_frame));
1084 29 buf_size -= buffer_len(&res_frame);
1085 29 mp3c->bit_res = need_bytes_from_res;
1086 }
1087
1088 29 buffer_free(&res_frame);
1089 }
1090  
1091 // add regular frames, up to buf_size, with at least one regular frame every time
1092 {
1093 int fi, fl;
1094
1095 1225 ▶ for (fi = MAX(mp3c->first_frame_inclusive, mp3c->next_processed_frame); fi < mp3c->last_frame_exclusive; fi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (fi = MAX(mp3c->first_frame_inclusive, mp3c->next_processed_frame); fi < mp3c->last_frame_exclusive; fi++) { 1169 56 1225
1096 DEBUG_TRACE("Handling frame %d\n", fi);
1097 1169 _set_curr_frame(mp3c, fi);
1098 fl = _get_frame_size(mp3c);
1099
1100 1169 ▶ if ( _get_bit_res_ptr(mp3c) > mp3c->bit_res ) { // XXX need test
Branch: 1 branch
BranchTrueFalseTotal
if ( _get_bit_res_ptr(mp3c) > mp3c->bit_res ) { // XXX need test 0 1169 1169
1101 DEBUG_TRACE(" Writing silence frame (bit res ptr %d > bit_res %d)\n", _get_bit_res_ptr(mp3c), mp3c->bit_res);
1102 0 _mp3cut_silence_frame(mp3c);
1103 }
1104
1105 1169 ▶ mp3c->bit_res = MIN(mp3c->bit_res + _get_main_data_size(mp3c), mp3c->max_res);
Branch: 1 branch
BranchTrueFalseTotal
mp3c->bit_res = MIN(mp3c->bit_res + _get_main_data_size(mp3c), mp3c->max_res); 13 1156 1169
1106 DEBUG_TRACE("bit_res %d\n", mp3c->bit_res);
1107
1108 1169 mp3c->next_processed_frame = fi + 1;
1109
1110 1169 sv_catpvn(buf, buffer_ptr(mp3c->buf), fl);
1111 1169 buf_size -= fl;
1112
1113 1169 ▶ if (buf_size <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (buf_size <= 0) 1072 97 1169
1114 break;
1115 }
1116 }
1117
1118 153 return sv_len(buf);
1119 }
1120  
1121 void
1122 29 _mp3cut_construct_reservoir_frame(mp3cut *mp3c, Buffer *res_frame, uint32_t min_res_size, uint64_t abs_start_sample)
1123 {
1124 29 unsigned char *dest = buffer_ptr(res_frame);
1125 29 int h32 = mp3c->first_frame->header32 | 0x00010000; // switch off CRC usage
1126 mp3frame frame;
1127 int bri, i;
1128
1129 // increase for 10-byte header inclusion
1130 29 min_res_size += 10;
1131
1132 // Find best res frame size from lowest bitrate to highest
1133 106 ▶ for (bri = 1; bri <= 14; bri++) {
Branch: 1 branch
BranchTrueFalseTotal
for (bri = 1; bri <= 14; bri++) { 106 0 106
1134 int side_info_end, main_data_size;
1135
1136 106 h32 = (h32 & 0xFFFF0FFF) + (bri << 12);
1137 106 _mp3cut_decode_frame(h32, &frame);
1138 106 side_info_end = _get_side_info_end(&frame);
1139 106 main_data_size = frame.frame_size - side_info_end;
1140
1141 106 ▶ if (main_data_size >= min_res_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (main_data_size >= min_res_size) { 29 77 106
1142 29 put_u32(dest, h32);
1143 867 ▶ for (i = 4; i < side_info_end; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 4; i < side_info_end; i++) 838 29 867
1144 838 dest[i] = 0;
1145 4926 ▶ for (i = side_info_end; i < frame.frame_size; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = side_info_end; i < frame.frame_size; i++) 4897 29 4926
1146 4897 dest[i] = 'x';
1147 29 dest[side_info_end] = 'P';
1148 29 dest[side_info_end + 1] = 'C';
1149 29 dest[side_info_end + 2] = 'U';
1150 29 dest[side_info_end + 3] = 'T';
1151 29 dest[side_info_end + 4] = 0; // revision 0
1152 29 dest[side_info_end + 5] = (abs_start_sample >> 32) & 0xFF;
1153 29 dest[side_info_end + 6] = (abs_start_sample >> 24) & 0xFF;
1154 29 dest[side_info_end + 7] = (abs_start_sample >> 16) & 0xFF;
1155 29 dest[side_info_end + 8] = (abs_start_sample >> 8) & 0xFF;
1156 29 dest[side_info_end + 9] = abs_start_sample & 0xFF;
1157
1158 29 res_frame->end = frame.frame_size;
1159 29 break;
1160 }
1161 }
1162
1163 DEBUG_TRACE("res frame size %d\n", buffer_len(res_frame));
1164 29 }
1165  
1166 void
1167 29 _mp3cut_construct_xing_frame(mp3cut *mp3c, Buffer *xing_frame, uint32_t frame_count, Buffer *seektable)
1168 {
1169 29 int fh32 = mp3c->first_frame->header32 | 0x00010000; // disable CRC if any
1170 int frame_size = 0;
1171 int tag_offset = 0;
1172 int i;
1173 unsigned char *dest;
1174 29 uint16_t enc_delay = mp3c->new_enc_delay;
1175 29 uint16_t enc_padding = mp3c->new_enc_padding;
1176 uint16_t crc = 0;
1177
1178 // Calculate optimal header frame size
1179 {
1180 mp3frame frame;
1181 float min_dist = 9999;
1182
1183 435 ▶ for (i = 1; i < 15; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < 15; i++) { 406 29 435
1184 406 int th32 = (fh32 & 0xFFFF0FFF) | (i << 12);
1185 406 _mp3cut_decode_frame(th32, &frame);
1186 406 ▶ if (frame.frame_size >= 0xC0) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame.frame_size >= 0xC0) { 290 116 406
1187 290 int ikbps = frame.bitrate_kbps;
1188 290 float dist = fabs(mp3c->avg_kbps - ikbps);
1189 290 ▶ if (dist < min_dist) {
Branch: 1 branch
BranchTrueFalseTotal
if (dist < min_dist) { 144 146 290
1190 min_dist = dist;
1191 fh32 = th32;
1192 frame_size = frame.frame_size;
1193 144 tag_offset = _get_side_info_size(&frame) + 4;
1194 }
1195 }
1196 }
1197
1198 DEBUG_TRACE("Xing frame size %d, tag offset %d\n", frame_size, tag_offset);
1199 29 buffer_init(xing_frame, frame_size);
1200 29 dest = buffer_ptr(xing_frame);
1201 }
1202
1203 29 Zero(dest, frame_size, uint8_t);
1204
1205 29 put_u32(dest, fh32);
1206
1207 29 ▶ if (mp3c->is_vbr) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->is_vbr) { 8 21 29
1208 8 dest[tag_offset++] = 'X';
1209 8 dest[tag_offset++] = 'i';
1210 8 dest[tag_offset++] = 'n';
1211 8 dest[tag_offset++] = 'g';
1212 }
1213 else {
1214 21 dest[tag_offset++] = 'I';
1215 21 dest[tag_offset++] = 'n';
1216 21 dest[tag_offset++] = 'f';
1217 21 dest[tag_offset++] = 'o';
1218 }
1219 29 dest[tag_offset++] = 0;
1220 29 dest[tag_offset++] = 0;
1221 29 dest[tag_offset++] = 0;
1222 29 dest[tag_offset++] = 0x0F;
1223 29 put_u32(&dest[tag_offset], frame_count);
1224 29 tag_offset += 4;
1225 29 put_u32(&dest[tag_offset], frame_size + mp3c->musi_len);
1226 29 tag_offset += 4;
1227 29 Copy(buffer_ptr(seektable), &dest[tag_offset], 100, uint8_t);
1228 29 tag_offset += 100;
1229 29 put_u32(&dest[tag_offset], 50); // vbr scale
1230 29 tag_offset += 4;
1231
1232 29 ▶ if (mp3c->xilt_frame->lame_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp3c->xilt_frame->lame_tag) { 21 8 29
1233 21 Copy((char *)buffer_ptr(mp3c->xilt_frame->tag) + mp3c->xilt_frame->lame_tag_offset, &dest[tag_offset - 4], 40, uint8_t);
1234 21 tag_offset += 4;
1235 // delete LAME's replaygain tag
1236 189 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 168 21 189
1237 168 dest[tag_offset + 0x07 + i] = 0;
1238 // delete no-gap flags
1239 21 dest[tag_offset + 0x0F] &= mp3c->mask_ath;
1240 }
1241 else {
1242 8 dest[tag_offset++] = 'L';
1243 8 dest[tag_offset++] = 'A';
1244 8 dest[tag_offset++] = 'M';
1245 8 dest[tag_offset++] = 'E';
1246 }
1247
1248 29 enc_delay = MAX(0, MIN(enc_delay, 4095));
1249 29 enc_padding = MAX(0, MIN(enc_padding, 4095));
1250 29 dest[tag_offset + 0x11] = (enc_delay >> 4) & 0xFF;
1251 29 dest[tag_offset + 0x12] = ((enc_delay & 0xF) << 4) | ((enc_padding >> 8) & 0xF);
1252 29 dest[tag_offset + 0x13] = enc_padding & 0xFF;
1253 29 put_u32(&dest[tag_offset + 0x18], frame_size + mp3c->musi_len);
1254
1255 5539 ▶ for (i = 0; i < 190; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 190; i++) 5510 29 5539
1256 5510 crc = _crc16(crc, dest[i] & 0xFF);
1257
1258 29 put_u16(&dest[tag_offset + 0x1E], crc);
1259
1260 29 xing_frame->end = frame_size;
1261 29 }
1262  
1263 void
1264 0 _mp3cut_silence_frame(mp3cut *mp3c)
1265 {
1266 0 unsigned char *data = buffer_ptr(mp3c->buf);
1267 0 uint8_t siend = _get_side_info_end(mp3c->first_frame);
1268 0 bool crc_used = ((data[1] & 1) == 0);
1269 int i;
1270
1271 0 ▶ for (i = 4; i <= siend; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 4; i <= siend; i++) 0 0 0
1272 0 data[i] = 0;
1273
1274 0 ▶ if (crc_used) {
Branch: 1 branch
BranchTrueFalseTotal
if (crc_used) { 0 0 0
1275 uint16_t crc = 0xFFFF;
1276 int o2;
1277
1278 0 crc = _crc16(crc, data[2]);
1279 0 crc = _crc16(crc, data[3]);
1280 0 ▶ for (o2 = 6; o2 < siend; o2++)
Branch: 1 branch
BranchTrueFalseTotal
for (o2 = 6; o2 < siend; o2++) 0 0 0
1281 0 crc = _crc16(crc, data[o2]);
1282 0 put_u16(&data[4], crc);
1283 }
1284 0 }
« src/common.c ↑ Summary Gapless.xs »