Debian Codegraph @

src/mp4.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 "mp4.h"
18
19 static int
20 get_mp4tags(PerlIO *infile, char *file, HV *info, HV *tags)
21 {
22 mp4info *mp4 = _mp4_parse(infile, file, info, tags, 0);
23
24 Safefree(mp4);
25
26 return 0;
27 }
28
29 // wrapper to return just the file offset
30 off_t
31 mp4_find_frame(PerlIO *infile, char *file, int offset)
32 {
33 HV *info = newHV();
34 int frame_offset = -1;
35
36 mp4_find_frame_return_info(infile, file, offset, info);
37
38 if ( my_hv_exists(info, "seek_offset") ) {
39 frame_offset = SvIV( *(my_hv_fetch(info, "seek_offset") ) );
40 }
41
42 SvREFCNT_dec(info);
43
44 return frame_offset;
45 }
46
47 // offset is in ms
48 // This is based on code from Rockbox
49 int
50 mp4_find_frame_return_info(PerlIO *infile, char *file, int offset, HV *info)
51 {
52 int ret = 1;
53 uint32_t samplerate = 0;
54 uint32_t sound_sample_loc;
55 uint32_t i = 0;
56 uint32_t j = 0;
57 uint32_t new_sample = 0;
58 uint32_t new_sound_sample = 0;
59
60 uint32_t chunk = 1;
61 uint32_t range_samples = 0;
62 uint32_t total_samples = 0;
63 uint32_t skipped_samples = 0;
64 uint32_t chunk_sample;
65 uint32_t prev_chunk;
66 uint32_t prev_chunk_samples;
67 uint32_t file_offset;
68 uint32_t chunk_offset;
69
70 uint32_t box_size = 0;
71 Buffer tmp_buf;
72 char tmp_size[4];
73
74 // We need to read all info first to get some data we need to calculate
75 HV *tags = newHV();
76 mp4info *mp4 = _mp4_parse(infile, file, info, tags, 1);
77
78 // Init seek buffer
79 // Newz(0, &tmp_buf, sizeof(Buffer), Buffer);
80 buffer_init(&tmp_buf, MP4_BLOCK_SIZE);
81
82 // Seeking not yet supported for files with multiple tracks
83 if (mp4->track_count > 1) {
84 ret = -1;
85 goto out;
86 }
87
88 if ( !my_hv_exists(info, "samplerate") ) {
89 PerlIO_printf(PerlIO_stderr(), "find_frame: unknown sample rate\n");
90 ret = -1;
91 goto out;
92 }
93
94 // Pull out the samplerate
95 samplerate = SvIV( *( my_hv_fetch( info, "samplerate" ) ) );
96
97 // convert offset to sound_sample_loc
98 sound_sample_loc = (offset / 10) * (samplerate / 100);
99 DEBUG_TRACE("Looking for target sample %u\n", sound_sample_loc);
100
101 // Make sure we have the necessary metadata
102 if (
103 !mp4->num_time_to_samples
104 || !mp4->num_sample_byte_sizes
105 || !mp4->num_sample_to_chunks
106 || !mp4->num_chunk_offsets
107 ) {
108 PerlIO_printf(PerlIO_stderr(), "find_frame: File does not contain seek metadata: %s\n", file);
109 ret = -1;
110 goto out;
111 }
112
113 // Find the destination block from time_to_sample array
114 while ( (i < mp4->num_time_to_samples) &&
115 (new_sound_sample < sound_sample_loc)
116 ) {
117 j = (sound_sample_loc - new_sound_sample) / mp4->time_to_sample[i].sample_duration;
118
119 DEBUG_TRACE(
120 "i = %d / j = %d, sample_count[i]: %d, sample_duration[i]: %d\n",
121 i, j,
122 mp4->time_to_sample[i].sample_count,
123 mp4->time_to_sample[i].sample_duration
124 );
125
126 if (j <= mp4->time_to_sample[i].sample_count) {
127 new_sample += j;
128 new_sound_sample += j * mp4->time_to_sample[i].sample_duration;
129 break;
130 }
131 else {
132 // XXX need test for this bit of code (variable stts)
133 new_sound_sample += (mp4->time_to_sample[i].sample_duration
134 * mp4->time_to_sample[i].sample_count);
135 new_sample += mp4->time_to_sample[i].sample_count;
136 i++;
137 }
138 }
139
140 if ( new_sample >= mp4->num_sample_byte_sizes ) {
141 PerlIO_printf(PerlIO_stderr(), "find_frame: Offset out of range (%d >= %d)\n", new_sample, mp4->num_sample_byte_sizes);
142 ret = -1;
143 goto out;
144 }
145
146 DEBUG_TRACE("new_sample: %d, new_sound_sample: %d\n", new_sample, new_sound_sample);
147
148 // Write new stts box
149 {
150 int i;
151 uint32_t total_sample_count = _mp4_total_samples(mp4);
152 uint32_t stts_entries = total_sample_count - new_sample;
153 uint32_t cur_duration = 0;
154 struct tts *stts;
155 int32_t stts_index = -1;
156
157 Newz(0, stts, stts_entries * sizeof(*stts), struct tts);
158
159 for (i = new_sample; i < total_sample_count; i++) {
160 uint32_t duration = _mp4_get_sample_duration(mp4, i);
161
162 if (cur_duration && cur_duration == duration) {
163 // same as previous entry, combine together
164 stts_entries--;
165 stts[stts_index].sample_count++;
166 }
167 else {
168 stts_index++;
169 stts[stts_index].sample_count = 1;
170 stts[stts_index].sample_duration = duration;
171 cur_duration = duration;
172 }
173 }
174
175 DEBUG_TRACE("Writing new stts (entries: %d)\n", stts_entries);
176 buffer_put_int(&tmp_buf, stts_entries);
177
178 for (i = 0; i < stts_entries; i++) {
179 DEBUG_TRACE(" sample_count %d, sample_duration %d\n", stts[i].sample_count, stts[i].sample_duration);
180 buffer_put_int(&tmp_buf, stts[i].sample_count);
181 buffer_put_int(&tmp_buf, stts[i].sample_duration);
182 }
183
184 mp4->new_stts = newSVpv("", 0);
185 put_u32( tmp_size, buffer_len(&tmp_buf) + 12 );
186 sv_catpvn( mp4->new_stts, tmp_size, 4 );
187 sv_catpvn( mp4->new_stts, "stts", 4 );
188 sv_catpvn( mp4->new_stts, "\0\0\0\0", 4 );
189 sv_catpvn( mp4->new_stts, (char *)buffer_ptr(&tmp_buf), buffer_len(&tmp_buf) );
190 //buffer_dump(&tmp_buf, 0);
191 buffer_clear(&tmp_buf);
192
193 Safefree(stts);
194 }
195
196 // We know the new block, now calculate the file position
197
198 /* Locate the chunk containing the sample */
199 prev_chunk = mp4->sample_to_chunk[0].first_chunk;
200 prev_chunk_samples = mp4->sample_to_chunk[0].samples_per_chunk;
201
202 for (i = 1; i < mp4->num_sample_to_chunks; i++) {
203 chunk = mp4->sample_to_chunk[i].first_chunk;
204 range_samples = (chunk - prev_chunk) * prev_chunk_samples;
205
206 DEBUG_TRACE("prev_chunk: %d, prev_chunk_samples: %d, chunk: %d, range_samples: %d\n",
207 prev_chunk, prev_chunk_samples, chunk, range_samples);
208
209 if (new_sample < total_samples + range_samples)
210 break;
211
212 total_samples += range_samples;
213 prev_chunk = mp4->sample_to_chunk[i].first_chunk;
214 prev_chunk_samples = mp4->sample_to_chunk[i].samples_per_chunk;
215 }
216
217 DEBUG_TRACE("prev_chunk: %d, prev_chunk_samples: %d, total_samples: %d\n", prev_chunk, prev_chunk_samples, total_samples);
218
219 if (new_sample >= mp4->sample_to_chunk[0].samples_per_chunk) {
220 chunk = prev_chunk + (new_sample - total_samples) / prev_chunk_samples;
221 }
222 else {
223 chunk = 1;
224 }
225
226 DEBUG_TRACE("chunk: %d\n", chunk);
227
228 /* Get sample of the first sample in the chunk */
229 chunk_sample = total_samples + (chunk - prev_chunk) * prev_chunk_samples;
230
231 DEBUG_TRACE("chunk_sample: %d\n", chunk_sample);
232
233 /* Get offset in file */
234
235 if (chunk > mp4->num_chunk_offsets) {
236 file_offset = mp4->chunk_offset[mp4->num_chunk_offsets - 1];
237 }
238 else {
239 file_offset = mp4->chunk_offset[chunk - 1];
240 }
241
242 DEBUG_TRACE("file_offset: %d\n", file_offset);
243
244 if (chunk_sample > new_sample) {
245 PerlIO_printf(PerlIO_stderr(), "find_frame: sample out of range (%d > %d)\n", chunk_sample, new_sample);
246 ret = -1;
247 goto out;
248 }
249
250 // Move offset within the chunk to the correct sample range
251 for (i = chunk_sample; i < new_sample; i++) {
252 file_offset += mp4->sample_byte_size[i];
253 skipped_samples++;
254 DEBUG_TRACE(" file_offset + %d: %d\n", mp4->sample_byte_size[i], file_offset);
255 }
256
257 if (file_offset > mp4->audio_offset + mp4->audio_size) {
258 PerlIO_printf(PerlIO_stderr(), "find_frame: file offset out of range (%d > %lld)\n", file_offset, mp4->audio_offset + mp4->audio_size);
259 ret = -1;
260 goto out;
261 }
262
263 // Write new stsc box
264 {
265 int i;
266 uint32_t stsc_entries = mp4->num_chunk_offsets - chunk + 1;
267 uint32_t cur_samples_per_chunk = 0;
268 struct stc *stsc;
269 int32_t stsc_index = -1;
270 uint32_t chunk_delta = 1;
271 j = 1;
272
273 Newz(0, stsc, stsc_entries * sizeof(*stsc), struct stc);
274
275 for (i = chunk; i <= mp4->num_chunk_offsets; i++) {
276 // Find the number of samples in chunk i
277 uint32_t samples_in_chunk = _mp4_samples_in_chunk(mp4, i);
278
279 if (cur_samples_per_chunk && cur_samples_per_chunk == samples_in_chunk) {
280 // same as previous entry, combine together
281 stsc_entries--;
282 }
283 else {
284 stsc_index++;
285
286 stsc[stsc_index].first_chunk = chunk_delta;
287
288 if (j == 1) {
289 // The first chunk may have less samples in it due to seeking within a chunk
290 stsc[stsc_index].samples_per_chunk = samples_in_chunk - skipped_samples;
291 cur_samples_per_chunk = samples_in_chunk - skipped_samples;
292 j++;
293 }
294 else {
295 stsc[stsc_index].samples_per_chunk = samples_in_chunk;
296 cur_samples_per_chunk = samples_in_chunk;
297 }
298 }
299
300 chunk_delta++;
301 }
302
303 DEBUG_TRACE("Writing new stsc (entries: %d)\n", stsc_entries);
304 buffer_put_int(&tmp_buf, stsc_entries);
305
306 for (i = 0; i < stsc_entries; i++) {
307 DEBUG_TRACE(" first_chunk %d, samples_per_chunk %d\n", stsc[i].first_chunk, stsc[i].samples_per_chunk);
308 buffer_put_int(&tmp_buf, stsc[i].first_chunk);
309 buffer_put_int(&tmp_buf, stsc[i].samples_per_chunk);
310 buffer_put_int(&tmp_buf, 1); // XXX sample description index, is this OK?
311 }
312
313 mp4->new_stsc = newSVpv("", 0);
314 put_u32( tmp_size, buffer_len(&tmp_buf) + 12 );
315 sv_catpvn( mp4->new_stsc, tmp_size, 4 );
316 sv_catpvn( mp4->new_stsc, "stsc", 4 );
317 sv_catpvn( mp4->new_stsc, "\0\0\0\0", 4 );
318 sv_catpvn( mp4->new_stsc, (char *)buffer_ptr(&tmp_buf), buffer_len(&tmp_buf) );
319 DEBUG_TRACE("Created new stsc\n");
320 //buffer_dump(&tmp_buf, 0);
321 buffer_clear(&tmp_buf);
322
323 Safefree(stsc);
324 }
325
326 // Write new stsz box, num_sample_byte_sizes -= $new_sample, skip $new_sample items
327 buffer_put_int(&tmp_buf, 0);
328 buffer_put_int(&tmp_buf, mp4->num_sample_byte_sizes - new_sample);
329 DEBUG_TRACE("Writing new stsz: %d items\n", mp4->num_sample_byte_sizes - new_sample);
330 j = 1;
331 for (i = new_sample; i < mp4->num_sample_byte_sizes; i++) {
332 DEBUG_TRACE(" sample %d sample_byte_size %d\n", j++, mp4->sample_byte_size[i]);
333 buffer_put_int(&tmp_buf, mp4->sample_byte_size[i]);
334 }
335
336 mp4->new_stsz = newSVpv("", 0);
337 put_u32( tmp_size, buffer_len(&tmp_buf) + 12 );
338 sv_catpvn( mp4->new_stsz, tmp_size, 4 );
339 sv_catpvn( mp4->new_stsz, "stsz", 4 );
340 sv_catpvn( mp4->new_stsz, "\0\0\0\0", 4 );
341 sv_catpvn( mp4->new_stsz, (char *)buffer_ptr(&tmp_buf), buffer_len(&tmp_buf) );
342 DEBUG_TRACE("Created new stsz\n");
343 //buffer_dump(&tmp_buf, 0);
344 buffer_clear(&tmp_buf);
345
346 // Total up size of 4 new st* boxes
347 // stco is calculated directly since we can't write it without offsets
348 mp4->new_st_size
349 = sv_len(mp4->new_stts)
350 + sv_len(mp4->new_stsc)
351 + sv_len(mp4->new_stsz)
352 + 12 + ( 4 * (mp4->num_chunk_offsets - chunk + 2) ); // stco size
353
354 DEBUG_TRACE("new_st_size: %d, old_st_size: %d\n", mp4->new_st_size, mp4->old_st_size);
355
356 // Calculate offset for each chunk
357 chunk_offset = SvIV( *( my_hv_fetch(info, "audio_offset") ) );
358 chunk_offset -= ( mp4->old_st_size - mp4->new_st_size );
359 chunk_offset += 8; // mdat size + fourcc
360
361 DEBUG_TRACE("chunk_offset: %d\n", chunk_offset);
362
363 // Write new stco box, num_chunk_offsets -= $chunk, skip $chunk items
364 buffer_put_int(&tmp_buf, mp4->num_chunk_offsets - chunk + 1);
365 DEBUG_TRACE("Writing new stco: %d items\n", mp4->num_chunk_offsets - chunk + 1);
366 for (i = chunk - 1; i < mp4->num_chunk_offsets; i++) {
367 if (i == chunk - 1) {
368 // The first chunk offset is the start of mdat (chunk_offset)
369 buffer_put_int( &tmp_buf, chunk_offset );
370 DEBUG_TRACE( " offset %d (orig %d)\n", chunk_offset, mp4->chunk_offset[i] );
371 }
372 else {
373 buffer_put_int( &tmp_buf, mp4->chunk_offset[i] - file_offset + chunk_offset );
374 DEBUG_TRACE( " offset %d (orig %d)\n", mp4->chunk_offset[i] - file_offset + chunk_offset, mp4->chunk_offset[i] );
375 }
376 }
377
378 mp4->new_stco = newSVpv("", 0);
379 put_u32( tmp_size, buffer_len(&tmp_buf) + 12 );
380 sv_catpvn( mp4->new_stco, tmp_size, 4 );
381 sv_catpvn( mp4->new_stco, "stco", 4 );
382 sv_catpvn( mp4->new_stco, "\0\0\0\0", 4 );
383 sv_catpvn( mp4->new_stco, (char *)buffer_ptr(&tmp_buf), buffer_len(&tmp_buf) );
384 DEBUG_TRACE("Created new stco\n");
385 //buffer_dump(&tmp_buf, 0);
386 buffer_clear(&tmp_buf);
387
388 DEBUG_TRACE("real st size: %ld\n",
389 sv_len(mp4->new_stts)
390 + sv_len(mp4->new_stsc)
391 + sv_len(mp4->new_stsz)
392 + sv_len(mp4->new_stco)
393 );
394
395 // Make second pass through header, reducing size of all parent boxes by st* size difference
396 // Copy all boxes, replacing st* boxes with new ones
397 mp4->seekhdr = newSVpv("", 0);
398
399 PerlIO_seek(mp4->infile, 0, SEEK_SET);
400
401 // XXX this is ugly, because we are reading a second time we have to reset
402 // various things in the mp4 struct
403 Newz(0, mp4->buf, sizeof(Buffer), Buffer);
404 buffer_init(mp4->buf, MP4_BLOCK_SIZE);
405
406 mp4->audio_offset = 0;
407 mp4->current_track = 0;
408 mp4->track_count = 0;
409
410 // free seek structs because we will be reading them a second time
411 if (mp4->time_to_sample) Safefree(mp4->time_to_sample);
412 if (mp4->sample_to_chunk) Safefree(mp4->sample_to_chunk);
413 if (mp4->sample_byte_size) Safefree(mp4->sample_byte_size);
414 if (mp4->chunk_offset) Safefree(mp4->chunk_offset);
415
416 mp4->time_to_sample = NULL;
417 mp4->sample_to_chunk = NULL;
418 mp4->sample_byte_size = NULL;
419 mp4->chunk_offset = NULL;
420
421 while ( (box_size = _mp4_read_box(mp4)) > 0 ) {
422 mp4->audio_offset += box_size;
423 DEBUG_TRACE("seek pass 2: read box of size %d\n", box_size);
424
425 if (mp4->audio_offset >= mp4->file_size)
426 break;
427 }
428
429 my_hv_store( info, "seek_offset", newSVuv(file_offset) );
430 my_hv_store( info, "seek_header", mp4->seekhdr );
431
432 if (mp4->buf) {
433 buffer_free(mp4->buf);
434 Safefree(mp4->buf);
435 }
436
437 out:
438 // Don't leak
439 SvREFCNT_dec(tags);
440
441 if (mp4->new_stts) SvREFCNT_dec(mp4->new_stts);
442 if (mp4->new_stsc) SvREFCNT_dec(mp4->new_stsc);
443 if (mp4->new_stsz) SvREFCNT_dec(mp4->new_stsz);
444 if (mp4->new_stco) SvREFCNT_dec(mp4->new_stco);
445
446 // free seek structs
447 if (mp4->time_to_sample) Safefree(mp4->time_to_sample);
448 if (mp4->sample_to_chunk) Safefree(mp4->sample_to_chunk);
449 if (mp4->sample_byte_size) Safefree(mp4->sample_byte_size);
450 if (mp4->chunk_offset) Safefree(mp4->chunk_offset);
451
452 // free seek buffer
453 buffer_free(&tmp_buf);
454
455 Safefree(mp4);
456
457 if (ret == -1) {
458 my_hv_store( info, "seek_offset", newSViv(-1) );
459 }
460
461 return ret;
462 }
463
464 mp4info *
465 _mp4_parse(PerlIO *infile, char *file, HV *info, HV *tags, uint8_t seeking)
466 {
467 off_t file_size;
468 uint32_t box_size = 0;
469
470 mp4info *mp4;
471 Newz(0, mp4, sizeof(mp4info), mp4info);
472 Newz(0, mp4->buf, sizeof(Buffer), Buffer);
473
474 mp4->audio_offset = 0;
475 mp4->infile = infile;
476 mp4->file = file;
477 mp4->info = info;
478 mp4->tags = tags;
479 mp4->current_track = 0;
480 mp4->track_count = 0;
481 mp4->seen_moov = 0;
482 mp4->seeking = seeking ? 1 : 0;
483
484 mp4->time_to_sample = NULL;
485 mp4->sample_to_chunk = NULL;
486 mp4->sample_byte_size = NULL;
487 mp4->chunk_offset = NULL;
488
489 buffer_init(mp4->buf, MP4_BLOCK_SIZE);
490
491 file_size = _file_size(infile);
492 mp4->file_size = file_size;
493
494 my_hv_store( info, "file_size", newSVuv(file_size) );
495
496 // Create empty tracks array
497 my_hv_store( info, "tracks", newRV_noinc( (SV *)newAV() ) );
498
499 while ( (box_size = _mp4_read_box(mp4)) > 0 ) {
500 mp4->audio_offset += box_size;
501 DEBUG_TRACE("read box of size %d / audio_offset %llu\n", box_size, mp4->audio_offset);
502
503 if (mp4->audio_offset >= file_size)
504 break;
505 }
506
507 // XXX: if no ftyp was found, assume it is brand 'mp41'
508
509 // if no bitrate was found (i.e. ALAC), calculate based on file_size/song_length_ms
510 if ( !my_hv_exists(info, "avg_bitrate") ) {
511 SV **entry = my_hv_fetch(info, "song_length_ms");
512 if (entry) {
513 SV **audio_offset = my_hv_fetch(info, "audio_offset");
514 if (audio_offset) {
515 uint32_t song_length_ms = SvIV(*entry);
516 uint32_t bitrate = _bitrate(file_size - SvIV(*audio_offset), song_length_ms);
517
518 my_hv_store( info, "avg_bitrate", newSVuv(bitrate) );
519 mp4->bitrate = bitrate;
520 }
521 }
522 }
523
524 // DLNA detection, based on code from libdlna
525 if (!mp4->dlna_invalid && mp4->samplerate && mp4->bitrate && mp4->channels) {
526 switch (mp4->audio_object_type) {
527 case AAC_LC:
528 case AAC_LC_ER:
529 {
530 if (mp4->samplerate < 8000 || mp4->samplerate > 48000)
531 break;
532
533 if (mp4->channels <= 2) {
534 if (mp4->bitrate <= 192000)
535 my_hv_store( info, "dlna_profile", newSVpv("AAC_ISO_192", 0) );
536 else if (mp4->bitrate <= 320000)
537 my_hv_store( info, "dlna_profile", newSVpv("AAC_ISO_320", 0) );
538 else if (mp4->bitrate <= 576000)
539 my_hv_store( info, "dlna_profile", newSVpv("AAC_ISO", 0) );
540 }
541 else if (mp4->channels <= 6) {
542 if (mp4->bitrate <= 1440000)
543 my_hv_store( info, "dlna_profile", newSVpv("AAC_MULT5_ISO", 0) );
544 }
545
546 break;
547 }
548
549 case AAC_LTP:
550 case AAC_LTP_ER:
551 {
552 if (mp4->samplerate < 8000)
553 break;
554
555 if (mp4->samplerate <= 48000) {
556 if (mp4->channels <= 2 && mp4->bitrate <= 576000)
557 my_hv_store( info, "dlna_profile", newSVpv("AAC_LTP_ISO", 0) );
558 }
559 else if (mp4->samplerate <= 96000) {
560 if (mp4->channels <= 6 && mp4->bitrate <= 2880000)
561 my_hv_store( info, "dlna_profile", newSVpv("AAC_LTP_MULT5_ISO", 0) );
562 else if (mp4->channels <= 8 && mp4->bitrate <= 4032000)
563 my_hv_store( info, "dlna_profile", newSVpv("AAC_LTP_MULT7_ISO", 0) );
564 }
565
566 break;
567 }
568
569 case AAC_HE:
570 {
571 if (mp4->samplerate < 8000)
572 break;
573
574 if (mp4->samplerate <= 24000) {
575 if (mp4->channels > 2)
576 break;
577
578 if (mp4->bitrate <= 128000)
579 my_hv_store( info, "dlna_profile", newSVpv("HEAAC_L2_ISO_128", 0) );
580 else if (mp4->bitrate <= 320000)
581 my_hv_store( info, "dlna_profile", newSVpv("HEAAC_L2_ISO_320", 0) );
582 else if (mp4->bitrate <= 576000)
583 my_hv_store( info, "dlna_profile", newSVpv("HEAAC_L2_ISO", 0) );
584 }
585 else if (mp4->samplerate <= 48000) {
586 if (mp4->channels <= 2 && mp4->bitrate <= 576000)
587 my_hv_store( info, "dlna_profile", newSVpv("HEAAC_L3_ISO", 0) );
588 else if (mp4->channels <= 6 && mp4->bitrate <= 1440000)
589 my_hv_store( info, "dlna_profile", newSVpv("HEAAC_MULT5_ISO", 0) );
590 else if (mp4->channels <= 8 && mp4->bitrate <= 4032000)
591 my_hv_store( info, "dlna_profile", newSVpv("HEAAC_MULT7", 0) );
592 }
593 else if (mp4->samplerate <= 96000) {
594 if (mp4->channels <= 8 && mp4->bitrate <= 4032000)
595 my_hv_store( info, "dlna_profile", newSVpv("HEAAC_MULT7", 0) );
596 }
597
598 break;
599 }
600
601 case AAC_PARAM_ER:
602 case AAC_PS:
603 {
604 if (mp4->samplerate < 8000)
605 break;
606
607 if (mp4->samplerate <= 24000) {
608 if (mp4->channels > 2)
609 break;
610
611 if (mp4->bitrate <= 128000)
612 my_hv_store( info, "dlna_profile", newSVpv("HEAACv2_L2_128", 0) );
613 else if (mp4->bitrate <= 320000)
614 my_hv_store( info, "dlna_profile", newSVpv("HEAACv2_L2_320", 0) );
615 else if (mp4->bitrate <= 576000)
616 my_hv_store( info, "dlna_profile", newSVpv("HEAACv2_L2", 0) );
617 }
618 else if (mp4->samplerate <= 48000) {
619 if (mp4->channels <= 2 && mp4->bitrate <= 576000)
620 my_hv_store( info, "dlna_profile", newSVpv("HEAACv2_L3", 0) );
621 else if (mp4->channels <= 6 && mp4->bitrate <= 1440000)
622 my_hv_store( info, "dlna_profile", newSVpv("HEAACv2_L4", 0) );
623 else if (mp4->channels <= 6 && mp4->bitrate <= 2880000)
624 my_hv_store( info, "dlna_profile", newSVpv("HEAACv2_MULT5", 0) );
625 else if (mp4->channels <= 8 && mp4->bitrate <= 4032000)
626 my_hv_store( info, "dlna_profile", newSVpv("HEAACv2_MULT7", 0) );
627 }
628 else if (mp4->samplerate <= 96000) {
629 if (mp4->channels <= 8 && mp4->bitrate <= 4032000)
630 my_hv_store( info, "dlna_profile", newSVpv("HEAACv2_MULT7", 0) );
631 }
632
633 break;
634 }
635
636 case AAC_BSAC_ER:
637 {
638 if (mp4->samplerate < 16000 || mp4->samplerate > 48000)
639 break;
640
641 if (mp4->bitrate > 128000)
642 break;
643
644 if (mp4->channels <= 2)
645 my_hv_store( info, "dlna_profile", newSVpv("BSAC_ISO", 0) );
646 else if (mp4->channels <= 6)
647 my_hv_store( info, "dlna_profile", newSVpv("BSAC_MULT5_ISO", 0) );
648
649 break;
650 }
651
652 default:
653 break;
654 }
655 }
656
657 buffer_free(mp4->buf);
658 Safefree(mp4->buf);
659
660 return mp4;
661 }
662
663 int
664 _mp4_read_box(mp4info *mp4)
665 {
666 uint64_t size; // total size of box
667 char type[5];
668 uint8_t skip = 0;
669
670 mp4->rsize = 0; // remaining size in box
671
672 if ( !_check_buf(mp4->infile, mp4->buf, 8, MP4_BLOCK_SIZE) ) {
673 return 0;
674 }
675
676 size = buffer_get_int(mp4->buf);
677 strncpy( type, (char *)buffer_ptr(mp4->buf), 4 );
678 type[4] = '\0';
679 buffer_consume(mp4->buf, 4);
680
681 mp4->hsize = 8;
682 // Check for 64-bit size
683 if (size == 1) {
684 if ( !_check_buf(mp4->infile, mp4->buf, 8, MP4_BLOCK_SIZE) ) {
685 return 0;
686 }
687 size = buffer_get_int64(mp4->buf);
688 mp4->hsize = 16;
689 }
690
691 if (size == 0) {
692 // XXX: box extends to end of file
693 /*nothing to do*/ ; // rsize=size=0
694 }
695 else if (size < mp4->hsize) {
696 PerlIO_printf(PerlIO_stderr(), "Invalid box size in: %s\n", mp4->file);
697 return 0;
698 }
699 else {
700 // set size of the remainder of the box
701 mp4->rsize = size - mp4->hsize;
702 }
703
704 mp4->size = size;
705
706 DEBUG_TRACE("%s size %llu\n", type, size);
707
708 if (size == mp4->hsize) {
709 PerlIO_printf(PerlIO_stderr(), "Ignoring empty box of type %s in: %s\n", type, mp4->file);
710 return size;
711 }
712
713 if (mp4->seekhdr) {
714 // Copy and adjust header if seeking
715 char tmp_size[4];
716
717 if (
718 FOURCC_EQ(type, "moov")
719 || FOURCC_EQ(type, "trak")
720 || FOURCC_EQ(type, "mdia")
721 || FOURCC_EQ(type, "minf")
722 || FOURCC_EQ(type, "stbl")
723 ) {
724 // Container box, adjust size
725 put_u32(tmp_size, size - (mp4->old_st_size - mp4->new_st_size));
726 DEBUG_TRACE(" Box is parent of st*, changed size to %llu\n", size - (mp4->old_st_size - mp4->new_st_size));
727 sv_catpvn( mp4->seekhdr, tmp_size, 4 );
728 sv_catpvn( mp4->seekhdr, type, 4 );
729 }
730 // Replace st* boxes with our new versions
731 else if ( FOURCC_EQ(type, "stts") ) {
732 DEBUG_TRACE("adding new stts of size %ld\n", sv_len(mp4->new_stts));
733 sv_catsv( mp4->seekhdr, mp4->new_stts );
734 }
735 else if ( FOURCC_EQ(type, "stsc") ) {
736 DEBUG_TRACE("adding new stsc of size %ld\n", sv_len(mp4->new_stsc));
737 sv_catsv( mp4->seekhdr, mp4->new_stsc );
738 }
739 else if ( FOURCC_EQ(type, "stsz") ) {
740 DEBUG_TRACE("adding new stsz of size %ld\n", sv_len(mp4->new_stsz));
741 sv_catsv( mp4->seekhdr, mp4->new_stsz );
742 }
743 else if ( FOURCC_EQ(type, "stco") ) {
744 DEBUG_TRACE("adding new stco of size %ld\n", sv_len(mp4->new_stco));
745 sv_catsv( mp4->seekhdr, mp4->new_stco );
746 }
747 else {
748 // Normal box, copy it
749 put_u32(tmp_size, size);
750 sv_catpvn( mp4->seekhdr, tmp_size, 4 );
751 sv_catpvn( mp4->seekhdr, type, 4 );
752
753 // stsd is special and contains real bytes and is also a container
754 if ( FOURCC_EQ(type, "stsd") ) {
755 sv_catpvn( mp4->seekhdr, (char *)buffer_ptr(mp4->buf), 8 );
756 }
757
758 // mp4a is special, ugh
759 else if ( FOURCC_EQ(type, "mp4a") ) {
760 sv_catpvn( mp4->seekhdr, (char *)buffer_ptr(mp4->buf), 28 );
761 }
762
763 // and so is meta
764 else if ( FOURCC_EQ(type, "meta") ) {
765 sv_catpvn( mp4->seekhdr, (char *)buffer_ptr(mp4->buf), mp4->meta_size );
766 }
767
768 // Copy contents unless it's a container
769 else if (
770 !FOURCC_EQ(type, "edts")
771 && !FOURCC_EQ(type, "dinf")
772 && !FOURCC_EQ(type, "udta")
773 && !FOURCC_EQ(type, "mdat")
774 ) {
775 if ( !_check_buf(mp4->infile, mp4->buf, size - 8, MP4_BLOCK_SIZE) ) {
776 return 0;
777 }
778
779 // XXX find a way to skip udta completely when rewriting seek header
780 // to avoid useless copying of artwork. Will require adjusting offsets
781 // differently.
782
783 sv_catpvn( mp4->seekhdr, (char *)buffer_ptr(mp4->buf), size - 8 );
784 }
785 }
786
787 // XXX should probably return size here and avoid reading info a second time
788 // or move the header copying code to somewhere else
789 }
790
791 if ( FOURCC_EQ(type, "ftyp") ) {
792 if ( !_mp4_parse_ftyp(mp4) ) {
793 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad ftyp box): %s\n", mp4->file);
794 return 0;
795 }
796 }
797 else if (
798 FOURCC_EQ(type, "moov")
799 || FOURCC_EQ(type, "edts")
800 || FOURCC_EQ(type, "mdia")
801 || FOURCC_EQ(type, "minf")
802 || FOURCC_EQ(type, "dinf")
803 || FOURCC_EQ(type, "stbl")
804 || FOURCC_EQ(type, "udta")
805 || FOURCC_EQ(type, "trak")
806 ) {
807 // These boxes are containers for nested boxes, return only the fact that
808 // we read the header size of the container. Read the nested box the next call to this fn.
809 size = mp4->hsize;
810
811 if ( FOURCC_EQ(type, "trak") ) {
812 // Also a container, but we need to increment track_count too
813 mp4->track_count++;
814 }
815 }
816 else if ( FOURCC_EQ(type, "mvhd") ) {
817 mp4->seen_moov = 1;
818
819 if ( !_mp4_parse_mvhd(mp4) ) {
820 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad mvhd box): %s\n", mp4->file);
821 return 0;
822 }
823 }
824 else if ( FOURCC_EQ(type, "tkhd") ) {
825 if ( !_mp4_parse_tkhd(mp4) ) {
826 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad tkhd box): %s\n", mp4->file);
827 return 0;
828 }
829 }
830 else if ( FOURCC_EQ(type, "mdhd") ) {
831 if ( !_mp4_parse_mdhd(mp4) ) {
832 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad mdhd box): %s\n", mp4->file);
833 return 0;
834 }
835 }
836 else if ( FOURCC_EQ(type, "hdlr") ) {
837 if ( !_mp4_parse_hdlr(mp4) ) {
838 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad hdlr box): %s\n", mp4->file);
839 return 0;
840 }
841 }
842 else if ( FOURCC_EQ(type, "stsd") ) {
843 if ( !_mp4_parse_stsd(mp4) ) {
844 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad stsd box): %s\n", mp4->file);
845 return 0;
846 }
847
848 // stsd is a special real box + container, count only the real bytes (8)
849 size = 8 + mp4->hsize;
850 }
851 else if ( FOURCC_EQ(type, "mp4a") ) {
852 if ( !_mp4_parse_mp4a(mp4) ) {
853 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad mp4a box): %s\n", mp4->file);
854 return 0;
855 }
856
857 // mp4a is a special real box + container, count only the real bytes (28)
858 size = 28 + mp4->hsize;
859 }
860 else if ( FOURCC_EQ(type, "alac") ) {
861 if ( !_mp4_parse_alac(mp4) ) {
862 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad alac box): %s\n", mp4->file);
863 return 0;
864 }
865
866 // skip rest (alac description)
867 mp4->rsize -= 28;
868 skip = 1;
869 }
870 else if ( FOURCC_EQ(type, "drms") ) {
871 // Mark encoding
872 HV *trackinfo = _mp4_get_current_trackinfo(mp4);
873
874 my_hv_store( trackinfo, "encoding", newSVpvn("drms", 4) );
875
876 // Skip rest
877 skip = 1;
878 }
879 else if ( FOURCC_EQ(type, "esds") ) {
880 if ( !_mp4_parse_esds(mp4) ) {
881 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad esds box): %s\n", mp4->file);
882 return 0;
883 }
884 }
885 else if ( FOURCC_EQ(type, "stts") ) {
886 if ( mp4->seeking && mp4->track_count == 1 ) {
887 if ( !_mp4_parse_stts(mp4) ) {
888 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad stts box): %s\n", mp4->file);
889 return 0;
890 }
891 mp4->old_st_size += size;
892 }
893 else {
894 skip = 1;
895 }
896 }
897 else if ( FOURCC_EQ(type, "stsc") ) {
898 if ( mp4->seeking && mp4->track_count == 1 ) {
899 if ( !_mp4_parse_stsc(mp4) ) {
900 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad stsc box): %s\n", mp4->file);
901 return 0;
902 }
903 mp4->old_st_size += size;
904 }
905 else {
906 skip = 1;
907 }
908 }
909 else if ( FOURCC_EQ(type, "stsz") ) {
910 if ( mp4->seeking && mp4->track_count == 1 ) {
911 if ( !_mp4_parse_stsz(mp4) ) {
912 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad stsz box): %s\n", mp4->file);
913 return 0;
914 }
915 mp4->old_st_size += size;
916 }
917 else {
918 skip = 1;
919 }
920 }
921 else if ( FOURCC_EQ(type, "stco") ) {
922 if ( mp4->seeking && mp4->track_count == 1 ) {
923 if ( !_mp4_parse_stco(mp4) ) {
924 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad stco box): %s\n", mp4->file);
925 return 0;
926 }
927 mp4->old_st_size += size;
928 }
929 else {
930 skip = 1;
931 }
932 }
933 else if ( FOURCC_EQ(type, "meta") ) {
934 uint8_t meta_size = _mp4_parse_meta(mp4);
935 if ( !meta_size ) {
936 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad meta box): %s\n", mp4->file);
937 return 0;
938 }
939
940 mp4->meta_size = meta_size;
941
942 // meta is a special real box + container, count only the real bytes
943 size = meta_size + mp4->hsize;
944 }
945 else if ( FOURCC_EQ(type, "ilst") ) {
946 if ( !_mp4_parse_ilst(mp4) ) {
947 PerlIO_printf(PerlIO_stderr(), "Invalid MP4 file (bad ilst box): %s\n", mp4->file);
948 return 0;
949 }
950 }
951 else if ( FOURCC_EQ(type, "mdat") ) {
952 // Audio data here, there may be boxes after mdat, so we have to skip it
953 skip = 1;
954
955 // If we haven't seen moov yet, set a flag so we can print a warning
956 // or handle it some other way
957 if ( !mp4->seen_moov ) {
958 my_hv_store( mp4->info, "leading_mdat", newSVuv(1) );
959 mp4->dlna_invalid = 1; // DLNA 8.6.34.8, moov must be before mdat
960 }
961
962 // Record audio offset and length
963 my_hv_store( mp4->info, "audio_offset", newSVuv(mp4->audio_offset) );
964 my_hv_store( mp4->info, "audio_size", newSVuv(size) );
965 mp4->audio_size = size;
966 }
967 else {
968 DEBUG_TRACE(" Unhandled box, skipping\n");
969 skip = 1;
970 }
971
972 if (skip) {
973 _mp4_skip(mp4, mp4->rsize);
974 }
975
976 return size;
977 }
978
979 uint8_t
980 _mp4_parse_ftyp(mp4info *mp4)
981 {
982 AV *compatible_brands = newAV();
983
984 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
985 return 0;
986 }
987
988 my_hv_store( mp4->info, "major_brand", newSVpvn( buffer_ptr(mp4->buf), 4 ) );
989 buffer_consume(mp4->buf, 4);
990
991 my_hv_store( mp4->info, "minor_version", newSVuv( buffer_get_int(mp4->buf) ) );
992
993 mp4->rsize -= 8;
994
995 if (mp4->rsize % 4) {
996 // invalid ftyp
997 return 0;
998 }
999
1000 while (mp4->rsize > 0) {
1001 av_push( compatible_brands, newSVpvn( buffer_ptr(mp4->buf), 4 ) );
1002 buffer_consume(mp4->buf, 4);
1003 mp4->rsize -= 4;
1004 }
1005
1006 my_hv_store( mp4->info, "compatible_brands", newRV_noinc( (SV *)compatible_brands ) );
1007
1008 return 1;
1009 }
1010
1011 uint8_t
1012 _mp4_parse_mvhd(mp4info *mp4)
1013 {
1014 uint32_t timescale;
1015 uint8_t version;
1016
1017 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
1018 return 0;
1019 }
1020
1021 version = buffer_get_char(mp4->buf);
1022 buffer_consume(mp4->buf, 3); // flags
1023
1024 if (version == 0) { // 32-bit values
1025 // Skip ctime and mtime
1026 buffer_consume(mp4->buf, 8);
1027
1028 timescale = buffer_get_int(mp4->buf);
1029 my_hv_store( mp4->info, "mv_timescale", newSVuv(timescale) );
1030
1031 my_hv_store( mp4->info, "song_length_ms", newSVuv( (buffer_get_int(mp4->buf) * 1.0 / timescale ) * 1000 ) );
1032 }
1033 else if (version == 1) { // 64-bit values
1034 // Skip ctime and mtime
1035 buffer_consume(mp4->buf, 16);
1036
1037 timescale = buffer_get_int(mp4->buf);
1038 my_hv_store( mp4->info, "mv_timescale", newSVuv(timescale) );
1039
1040 my_hv_store( mp4->info, "song_length_ms", newSVuv( (buffer_get_int64(mp4->buf) * 1.0 / timescale ) * 1000 ) );
1041 }
1042 else {
1043 return 0;
1044 }
1045
1046 // Skip rest
1047 buffer_consume(mp4->buf, 80);
1048
1049 return 1;
1050 }
1051
1052 uint8_t
1053 _mp4_parse_tkhd(mp4info *mp4)
1054 {
1055 AV *tracks = (AV *)SvRV( *(my_hv_fetch(mp4->info, "tracks")) );
1056 HV *trackinfo = newHV();
1057 uint32_t id;
1058 double width;
1059 double height;
1060 uint8_t version;
1061
1062 uint32_t timescale = SvIV( *(my_hv_fetch(mp4->info, "mv_timescale")) );
1063
1064 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
1065 return 0;
1066 }
1067
1068 version = buffer_get_char(mp4->buf);
1069 buffer_consume(mp4->buf, 3); // flags
1070
1071 // XXX DLNA Requirement [8.6.34.5]: For the default audio track, "Track_enabled"
1072 // must be set to the value of 1 in the "flags" field of Track Header Box of the track.
1073
1074 if (version == 0) { // 32-bit values
1075 // Skip ctime and mtime
1076 buffer_consume(mp4->buf, 8);
1077
1078 id = buffer_get_int(mp4->buf);
1079
1080 my_hv_store( trackinfo, "id", newSVuv(id) );
1081
1082 // Skip reserved
1083 buffer_consume(mp4->buf, 4);
1084
1085 my_hv_store( trackinfo, "duration", newSVuv( (buffer_get_int(mp4->buf) * 1.0 / timescale ) * 1000 ) );
1086 }
1087 else if (version == 1) { // 64-bit values
1088 // Skip ctime and mtime
1089 buffer_consume(mp4->buf, 16);
1090
1091 id = buffer_get_int(mp4->buf);
1092
1093 my_hv_store( trackinfo, "id", newSVuv(id) );
1094
1095 // Skip reserved
1096 buffer_consume(mp4->buf, 4);
1097
1098 my_hv_store( trackinfo, "duration", newSVuv( (buffer_get_int64(mp4->buf) * 1.0 / timescale ) * 1000 ) );
1099 }
1100 else {
1101 return 0;
1102 }
1103
1104 // Skip reserved, layer, alternate_group, volume, reserved, matrix
1105 buffer_consume(mp4->buf, 52);
1106
1107 // width/height are fixed-point 16.16
1108 width = buffer_get_short(mp4->buf);
1109 width += buffer_get_short(mp4->buf) / 65536.;
1110 if (width > 0) {
1111 my_hv_store( trackinfo, "width", newSVnv(width) );
1112 }
1113
1114 height = buffer_get_short(mp4->buf);
1115 height += buffer_get_short(mp4->buf) / 65536.;
1116 if (height > 0) {
1117 my_hv_store( trackinfo, "height", newSVnv(height) );
1118 }
1119
1120 av_push( tracks, newRV_noinc( (SV *)trackinfo ) );
1121
1122 // Remember the current track we're dealing with
1123 mp4->current_track = id;
1124
1125 return 1;
1126 }
1127
1128 uint8_t
1129 _mp4_parse_mdhd(mp4info *mp4)
1130 {
1131 uint32_t timescale;
1132 uint8_t version;
1133
1134 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
1135 return 0;
1136 }
1137
1138 version = buffer_get_char(mp4->buf);
1139 buffer_consume(mp4->buf, 3); // flags
1140
1141 if (version == 0) { // 32-bit values
1142 // Skip ctime and mtime
1143 buffer_consume(mp4->buf, 8);
1144
1145 timescale = buffer_get_int(mp4->buf);
1146 my_hv_store( mp4->info, "samplerate", newSVuv(timescale) );
1147
1148 // Skip duration, if have song_length_ms from mvhd
1149 if ( my_hv_exists( mp4->info, "song_length_ms" ) ) {
1150 buffer_consume(mp4->buf, 4);
1151 }
1152 else {
1153 my_hv_store( mp4->info, "song_length_ms", newSVuv( (buffer_get_int(mp4->buf) * 1.0 / timescale ) * 1000 ) );
1154 }
1155 }
1156 else if (version == 1) { // 64-bit values
1157 // Skip ctime and mtime
1158 buffer_consume(mp4->buf, 16);
1159
1160 timescale = buffer_get_int(mp4->buf);
1161 my_hv_store( mp4->info, "samplerate", newSVuv(timescale) );
1162
1163 // Skip duration, if have song_length_ms from mvhd
1164 if ( my_hv_exists( mp4->info, "song_length_ms" ) ) {
1165 buffer_consume(mp4->buf, 8);
1166 }
1167 else {
1168 my_hv_store( mp4->info, "song_length_ms", newSVuv( (buffer_get_int64(mp4->buf) * 1.0 / timescale ) * 1000 ) );
1169 }
1170 }
1171 else {
1172 return 0;
1173 }
1174
1175 mp4->samplerate = timescale;
1176
1177 // Skip rest
1178 buffer_consume(mp4->buf, 4);
1179
1180 return 1;
1181 }
1182
1183 uint8_t
1184 _mp4_parse_hdlr(mp4info *mp4)
1185 {
1186 HV *trackinfo = _mp4_get_current_trackinfo(mp4);
1187 SV *handler_name;
1188
1189 if (!trackinfo) {
1190 return 0;
1191 }
1192
1193 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
1194 return 0;
1195 }
1196
1197 // Skip version, flags, pre_defined
1198 buffer_consume(mp4->buf, 8);
1199
1200 my_hv_store( trackinfo, "handler_type", newSVpvn( buffer_ptr(mp4->buf), 4 ) );
1201 buffer_consume(mp4->buf, 4);
1202
1203 // Skip reserved
1204 buffer_consume(mp4->buf, 12);
1205
1206 handler_name = newSVpv( buffer_ptr(mp4->buf), 0 );
1207 sv_utf8_decode(handler_name);
1208 my_hv_store( trackinfo, "handler_name", handler_name );
1209
1210 buffer_consume(mp4->buf, mp4->rsize - 24);
1211
1212 return 1;
1213 }
1214
1215 uint8_t
1216 _mp4_parse_stsd(mp4info *mp4)
1217 {
1218 uint32_t entry_count;
1219
1220 if ( !_check_buf(mp4->infile, mp4->buf, 8, MP4_BLOCK_SIZE) ) {
1221 return 0;
1222 }
1223
1224 // Skip version/flags
1225 buffer_consume(mp4->buf, 4);
1226
1227 entry_count = buffer_get_int(mp4->buf);
1228
1229 return 1;
1230 }
1231
1232 uint8_t
1233 _mp4_parse_mp4a(mp4info *mp4)
1234 {
1235 HV *trackinfo = _mp4_get_current_trackinfo(mp4);
1236
1237 if ( !_check_buf(mp4->infile, mp4->buf, 28, MP4_BLOCK_SIZE) ) {
1238 return 0;
1239 }
1240
1241 my_hv_store( trackinfo, "encoding", newSVpvn("mp4a", 4) );
1242
1243 // Skip reserved
1244 buffer_consume(mp4->buf, 16);
1245
1246 mp4->channels = buffer_get_short(mp4->buf);
1247 my_hv_store( trackinfo, "channels", newSVuv(mp4->channels) );
1248 my_hv_store( trackinfo, "bits_per_sample", newSVuv( buffer_get_short(mp4->buf) ) );
1249
1250 // Skip reserved
1251 buffer_consume(mp4->buf, 4);
1252
1253 // Skip bogus samplerate
1254 buffer_consume(mp4->buf, 2);
1255
1256 // Skip reserved
1257 buffer_consume(mp4->buf, 2);
1258
1259 return 1;
1260 }
1261
1262 uint8_t
1263 _mp4_parse_esds(mp4info *mp4)
1264 {
1265 HV *trackinfo = _mp4_get_current_trackinfo(mp4);
1266 uint32_t len = 0;
1267 uint32_t avg_bitrate;
1268
1269 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
1270 return 0;
1271 }
1272
1273 // Skip version/flags
1274 buffer_consume(mp4->buf, 4);
1275
1276 // Public docs on esds are hard to find, this is based on faad
1277 // and http://www.geocities.com/xhelmboyx/quicktime/formats/mp4-layout.txt
1278
1279 // verify ES_DescrTag
1280 if (buffer_get_char(mp4->buf) == 0x03) {
1281 // read length
1282 if ( _mp4_descr_length(mp4->buf) < 5 + 15 ) {
1283 return 0;
1284 }
1285
1286 // skip 3 bytes
1287 buffer_consume(mp4->buf, 3);
1288 }
1289 else {
1290 // skip 2 bytes
1291 buffer_consume(mp4->buf, 2);
1292 }
1293
1294 // verify DecoderConfigDescrTab
1295 if (buffer_get_char(mp4->buf) != 0x04) {
1296 return 0;
1297 }
1298
1299 // read length
1300 if ( _mp4_descr_length(mp4->buf) < 13 ) {
1301 return 0;
1302 }
1303
1304 // XXX: map to string
1305 my_hv_store( trackinfo, "audio_type", newSVuv( buffer_get_char(mp4->buf) ) );
1306
1307 buffer_consume(mp4->buf, 4);
1308
1309 my_hv_store( trackinfo, "max_bitrate", newSVuv( buffer_get_int(mp4->buf) ) );
1310
1311 avg_bitrate = buffer_get_int(mp4->buf);
1312 if (avg_bitrate) {
1313 if ( my_hv_exists(mp4->info, "avg_bitrate") ) {
1314 // If there are multiple tracks, just add up the bitrates
1315 avg_bitrate += SvIV(*(my_hv_fetch(mp4->info, "avg_bitrate")));
1316 }
1317 my_hv_store( mp4->info, "avg_bitrate", newSVuv(avg_bitrate) );
1318 mp4->bitrate = avg_bitrate;
1319 }
1320
1321 // verify DecSpecificInfoTag
1322 if (buffer_get_char(mp4->buf) != 0x05) {
1323 return 0;
1324 }
1325
1326 // Read audio object type
1327 // 5 bits, if 0x1F, read 6 more bits
1328 len = _mp4_descr_length(mp4->buf);
1329 if (len > 0) {
1330 uint32_t aot;
1331
1332 len *= 8; // count the number of bits left
1333
1334 aot = buffer_get_bits(mp4->buf, 5);
1335 len -= 5;
1336
1337 if ( aot == 0x1F ) {
1338 aot = 32 + buffer_get_bits(mp4->buf, 6);
1339 len -= 6;
1340 }
1341
1342 // samplerate: 4 bits
1343 // if 0xF, samplerate is next 24 bits
1344 // else lookup in samplerate table
1345 {
1346 uint32_t samplerate = buffer_get_bits(mp4->buf, 4);
1347 len -= 4;
1348
1349 if (samplerate == 0xF) { // XXX need test file with 24-bit samplerate field
1350 samplerate = buffer_get_bits(mp4->buf, 24);
1351 len -= 24;
1352 }
1353 else {
1354 samplerate = samplerate_table[samplerate];
1355 }
1356
1357 // Channel configuration (4 bits)
1358 // XXX This is sometimes wrong (1 when it should be 2)
1359 mp4->channels = buffer_get_bits(mp4->buf, 4);
1360 my_hv_store( trackinfo, "channels", newSVuv(mp4->channels) );
1361 len -= 4;
1362
1363 if (aot == AAC_SLS) {
1364 // Read some SLS-specific config
1365 // bits per sample (3 bits) { 8, 16, 20, 24 }
1366 uint8_t bps = buffer_get_bits(mp4->buf, 3);
1367 len -= 3;
1368
1369 my_hv_store( trackinfo, "bits_per_sample", newSVuv( bps_table[bps] ) );
1370 }
1371 else if (aot == AAC_HE || aot == AAC_PS) {
1372 // Read extended samplerate info
1373 samplerate = buffer_get_bits(mp4->buf, 4);
1374 len -= 4;
1375 if (samplerate == 0xF) { // XXX need test file with 24-bit samplerate field
1376 samplerate = buffer_get_bits(mp4->buf, 24);
1377 len -= 24;
1378 }
1379 else {
1380 samplerate = samplerate_table[samplerate];
1381 }
1382 }
1383
1384 my_hv_store( trackinfo, "samplerate", newSVuv(samplerate) );
1385 mp4->samplerate = samplerate;
1386 }
1387
1388 my_hv_store( trackinfo, "audio_object_type", newSVuv(aot) );
1389 mp4->audio_object_type = aot;
1390
1391 // Skip rest of box
1392 buffer_get_bits(mp4->buf, len);
1393 }
1394
1395 // verify SL config descriptor type tag
1396 if (buffer_get_char(mp4->buf) != 0x06) {
1397 return 0;
1398 }
1399
1400 _mp4_descr_length(mp4->buf);
1401
1402 // verify SL value
1403 if (buffer_get_char(mp4->buf) != 0x02) {
1404 return 0;
1405 }
1406
1407 return 1;
1408 }
1409
1410 uint8_t
1411 _mp4_parse_alac(mp4info *mp4)
1412 {
1413 HV *trackinfo = _mp4_get_current_trackinfo(mp4);
1414
1415 if ( !_check_buf(mp4->infile, mp4->buf, 28, MP4_BLOCK_SIZE) ) {
1416 return 0;
1417 }
1418
1419 my_hv_store( trackinfo, "encoding", newSVpvn("alac", 4) );
1420
1421 // Skip reserved
1422 buffer_consume(mp4->buf, 16);
1423
1424 mp4->channels = buffer_get_short(mp4->buf);
1425 my_hv_store( trackinfo, "channels", newSVuv(mp4->channels) );
1426 my_hv_store( trackinfo, "bits_per_sample", newSVuv( buffer_get_short(mp4->buf) ) );
1427
1428 // Skip reserved
1429 buffer_consume(mp4->buf, 4);
1430
1431 // Skip bogus samplerate
1432 buffer_consume(mp4->buf, 2);
1433
1434 // Skip reserved
1435 buffer_consume(mp4->buf, 2);
1436
1437 return 1;
1438 }
1439
1440 uint8_t
1441 _mp4_parse_stts(mp4info *mp4)
1442 {
1443 int i;
1444
1445 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
1446 return 0;
1447 }
1448
1449 // Skip version/flags
1450 buffer_consume(mp4->buf, 4);
1451
1452 mp4->num_time_to_samples = buffer_get_int(mp4->buf);
1453 DEBUG_TRACE(" num_time_to_samples %d\n", mp4->num_time_to_samples);
1454
1455 New(0,
1456 mp4->time_to_sample,
1457 mp4->num_time_to_samples * sizeof(*mp4->time_to_sample),
1458 struct tts
1459 );
1460
1461 if ( !mp4->time_to_sample ) {
1462 PerlIO_printf(PerlIO_stderr(), "Unable to parse stts: too large\n");
1463 return 0;
1464 }
1465
1466 for (i = 0; i < mp4->num_time_to_samples; i++) {
1467 mp4->time_to_sample[i].sample_count = buffer_get_int(mp4->buf);
1468 mp4->time_to_sample[i].sample_duration = buffer_get_int(mp4->buf);
1469
1470 DEBUG_TRACE(
1471 " sample_count %d sample_duration %d\n",
1472 mp4->time_to_sample[i].sample_count,
1473 mp4->time_to_sample[i].sample_duration
1474 );
1475 }
1476
1477 return 1;
1478 }
1479
1480 uint8_t
1481 _mp4_parse_stsc(mp4info *mp4)
1482 {
1483 int i;
1484
1485 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
1486 return 0;
1487 }
1488
1489 // Skip version/flags
1490 buffer_consume(mp4->buf, 4);
1491
1492 mp4->num_sample_to_chunks = buffer_get_int(mp4->buf);
1493 DEBUG_TRACE(" num_sample_to_chunks %d\n", mp4->num_sample_to_chunks);
1494
1495 New(0,
1496 mp4->sample_to_chunk,
1497 mp4->num_sample_to_chunks * sizeof(*mp4->sample_to_chunk),
1498 struct stc
1499 );
1500
1501 if ( !mp4->sample_to_chunk ) {
1502 PerlIO_printf(PerlIO_stderr(), "Unable to parse stsc: too large\n");
1503 return 0;
1504 }
1505
1506 for (i = 0; i < mp4->num_sample_to_chunks; i++) {
1507 mp4->sample_to_chunk[i].first_chunk = buffer_get_int(mp4->buf);
1508 mp4->sample_to_chunk[i].samples_per_chunk = buffer_get_int(mp4->buf);
1509
1510 // Skip sample desc index
1511 buffer_consume(mp4->buf, 4);
1512
1513 DEBUG_TRACE(" first_chunk %d samples_per_chunk %d\n",
1514 mp4->sample_to_chunk[i].first_chunk,
1515 mp4->sample_to_chunk[i].samples_per_chunk
1516 );
1517 }
1518
1519 return 1;
1520 }
1521
1522 uint8_t
1523 _mp4_parse_stsz(mp4info *mp4)
1524 {
1525 int i;
1526
1527 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
1528 return 0;
1529 }
1530
1531 // Skip version/flags
1532 buffer_consume(mp4->buf, 4);
1533
1534 // Check sample size is 0
1535 if ( buffer_get_int(mp4->buf) != 0 ) {
1536 DEBUG_TRACE(" stsz uses fixed sample size\n");
1537 buffer_consume(mp4->buf, 4);
1538 return 1;
1539 }
1540
1541 mp4->num_sample_byte_sizes = buffer_get_int(mp4->buf);
1542
1543 DEBUG_TRACE(" num_sample_byte_sizes %d\n", mp4->num_sample_byte_sizes);
1544
1545 New(0,
1546 mp4->sample_byte_size,
1547 mp4->num_sample_byte_sizes * sizeof(*mp4->sample_byte_size),
1548 uint16_t
1549 );
1550
1551 if ( !mp4->sample_byte_size ) {
1552 PerlIO_printf(PerlIO_stderr(), "Unable to parse stsz: too large\n");
1553 return 0;
1554 }
1555
1556 for (i = 0; i < mp4->num_sample_byte_sizes; i++) {
1557 uint32_t v = buffer_get_int(mp4->buf);
1558
1559 if (v > 0x0000ffff) {
1560 DEBUG_TRACE("stsz[%d] > 65 kB (%ld)\n", i, (long)v);
1561 return 0;
1562 }
1563
1564 mp4->sample_byte_size[i] = v;
1565
1566 //DEBUG_TRACE(" sample_byte_size %d\n", v);
1567 }
1568
1569 return 1;
1570 }
1571
1572 uint8_t
1573 _mp4_parse_stco(mp4info *mp4)
1574 {
1575 int i;
1576
1577 if ( !_check_buf(mp4->infile, mp4->buf, mp4->rsize, MP4_BLOCK_SIZE) ) {
1578 return 0;
1579 }
1580
1581 // Skip version/flags
1582 buffer_consume(mp4->buf, 4);
1583
1584 mp4->num_chunk_offsets = buffer_get_int(mp4->buf);
1585 DEBUG_TRACE(" num_chunk_offsets %d\n", mp4->num_chunk_offsets);
1586
1587 New(0,
1588 mp4->chunk_offset,
1589 mp4->num_chunk_offsets * sizeof(*mp4->chunk_offset),
1590 uint32_t
1591 );
1592
1593 if ( !mp4->chunk_offset ) {
1594 PerlIO_printf(PerlIO_stderr(), "Unable to parse stco: too large\n");
1595 return 0;
1596 }
1597
1598 for (i = 0; i < mp4->num_chunk_offsets; i++) {
1599 mp4->chunk_offset[i] = buffer_get_int(mp4->buf);
1600
1601 //DEBUG_TRACE(" chunk_offset %d\n", mp4->chunk_offset[i]);
1602 }
1603
1604 return 1;
1605 }
1606
1607 uint8_t
1608 _mp4_parse_meta(mp4info *mp4)
1609 {
1610 uint32_t hdlr_size;
1611 char type[5];
1612
1613 if ( !_check_buf(mp4->infile, mp4->buf, 12, MP4_BLOCK_SIZE) ) {
1614 return 0;
1615 }
1616
1617 // Skip version/flags
1618 buffer_consume(mp4->buf, 4);
1619
1620 // Parse/skip meta version of hdlr
1621 hdlr_size = buffer_get_int(mp4->buf);
1622 strncpy( type, (char *)buffer_ptr(mp4->buf), 4 );
1623 type[4] = '\0';
1624 buffer_consume(mp4->buf, 4);
1625
1626 if ( !FOURCC_EQ(type, "hdlr") ) {
1627 return 0;
1628 }
1629
1630 // Skip rest of hdlr
1631 if ( !_check_buf(mp4->infile, mp4->buf, hdlr_size - 8, MP4_BLOCK_SIZE) ) {
1632 return 0;
1633 }
1634
1635 buffer_consume(mp4->buf, hdlr_size - 8);
1636
1637 return 12 + hdlr_size - 8;
1638 }
1639
1640 uint8_t
1641 _mp4_parse_ilst(mp4info *mp4)
1642 {
1643 while (mp4->rsize) {
1644 uint32_t size;
1645 char key[5];
1646
1647 if ( !_check_buf(mp4->infile, mp4->buf, 8, MP4_BLOCK_SIZE) ) {
1648 return 0;
1649 }
1650
1651 DEBUG_TRACE(" ilst rsize %llu\n", mp4->rsize);
1652
1653 // Read Apple annotation box
1654 size = buffer_get_int(mp4->buf);
1655 strncpy( key, (char *)buffer_ptr(mp4->buf), 4 );
1656 key[4] = '\0';
1657 buffer_consume(mp4->buf, 4);
1658
1659 DEBUG_TRACE(" %s size %d\n", key, size);
1660
1661 // Note: extra _check_buf calls in this function and other ilst functions
1662 // are to avoid reading in the full size of ilst in the case of large artwork
1663
1664 upcase(key);
1665
1666 if ( FOURCC_EQ(key, "----") ) {
1667 // user-specified key/value pair
1668 if ( !_mp4_parse_ilst_custom(mp4, size - 8) ) {
1669 return 0;
1670 }
1671 }
1672 else {
1673 uint32_t bsize;
1674
1675 // Ensure we have 8 bytes
1676 if ( !_check_buf(mp4->infile, mp4->buf, 8, MP4_BLOCK_SIZE) ) {
1677 return 0;
1678 }
1679
1680 // Verify data box
1681 bsize = buffer_get_int(mp4->buf);
1682
1683 DEBUG_TRACE(" box size %d\n", bsize);
1684
1685 // Sanity check for bad data size
1686 if ( bsize <= size - 8 ) {
1687 SV *skey;
1688
1689 char *bptr = buffer_ptr(mp4->buf);
1690 if ( !FOURCC_EQ(bptr, "data") ) {
1691 return 0;
1692 }
1693
1694 buffer_consume(mp4->buf, 4);
1695
1696 skey = newSVpv(key, 0);
1697
1698 if ( !_mp4_parse_ilst_data(mp4, bsize - 8, skey) ) {
1699 SvREFCNT_dec(skey);
1700 return 0;
1701 }
1702
1703 SvREFCNT_dec(skey);
1704
1705 // XXX: bug 14476, files with multiple COVR images aren't handled here, just skipped for now
1706 if ( bsize < size - 8 ) {
1707 DEBUG_TRACE(" skipping rest of box, %d\n", size - 8 - bsize );
1708 _mp4_skip(mp4, size - 8 - bsize);
1709 }
1710 }
1711 else {
1712 DEBUG_TRACE(" invalid data size %d, skipping value\n", bsize);
1713 _mp4_skip(mp4, size - 12);
1714 }
1715 }
1716
1717 mp4->rsize -= size;
1718 }
1719
1720 return 1;
1721 }
1722
1723 uint8_t
1724 _mp4_parse_ilst_data(mp4info *mp4, uint32_t size, SV *key)
1725 {
1726 uint32_t flags;
1727 unsigned char *ckey;
1728 SV *value;
1729
1730 ckey = (unsigned char *)SvPVX(key);
1731 if ( FOURCC_EQ(ckey, "COVR") && _env_true("AUDIO_SCAN_NO_ARTWORK") ) {
1732 // Skip artwork if requested and avoid the memory cost
1733 value = newSVuv(size - 8);
1734
1735 my_hv_store( mp4->tags, "COVR_offset", newSVuv(mp4->audio_offset + (mp4->size - mp4->rsize) + 24) );
1736
1737 _mp4_skip(mp4, size);
1738 }
1739 else {
1740 // Read the full ilst value
1741 if ( !_check_buf(mp4->infile, mp4->buf, size, MP4_BLOCK_SIZE) ) {
1742 return 0;
1743 }
1744
1745 // Version(0) + Flags
1746 flags = buffer_get_int(mp4->buf);
1747
1748 // Skip reserved
1749 buffer_consume(mp4->buf, 4);
1750
1751 DEBUG_TRACE(" flags %d\n", flags);
1752
1753 if ( !flags || flags == 21 ) {
1754 if ( FOURCC_EQ( SvPVX(key), "TRKN" ) || FOURCC_EQ( SvPVX(key), "DISK" ) ) {
1755 // Special case trkn, disk (pair of 16-bit ints)
1756 uint16_t num = 0;
1757 uint16_t total = 0;
1758
1759 buffer_consume(mp4->buf, 2); // padding
1760
1761 num = buffer_get_short(mp4->buf);
1762
1763 // Total may not always be present
1764 if (size > 12) {
1765 total = buffer_get_short(mp4->buf);
1766 buffer_consume(mp4->buf, size - 14); // optional padding
1767 }
1768
1769 DEBUG_TRACE(" %d/%d\n", num, total);
1770
1771 if (total) {
1772 my_hv_store_ent( mp4->tags, key, newSVpvf( "%d/%d", num, total ) );
1773 }
1774 else if (num) {
1775 my_hv_store_ent( mp4->tags, key, newSVuv(num) );
1776 }
1777
1778 return 1;
1779 }
1780 else if ( FOURCC_EQ( SvPVX(key), "GNRE" ) ) {
1781 // Special case genre, 16-bit int as id3 genre code
1782 char const *genre_string;
1783 uint16_t genre_num = buffer_get_short(mp4->buf);
1784
1785 if (genre_num > 0 && genre_num < NGENRES + 1) {
1786 genre_string = _id3_genre_index(genre_num - 1);
1787 my_hv_store_ent( mp4->tags, key, newSVpv( genre_string, 0 ) );
1788 }
1789
1790 return 1;
1791 }
1792 else {
1793 // Other binary type, try to guess type based on size
1794 uint32_t dsize = size - 8;
1795
1796 if (dsize == 1) {
1797 value = newSVuv( buffer_get_char(mp4->buf) );
1798 }
1799 else if (dsize == 2) {
1800 value = newSVuv( buffer_get_short(mp4->buf) );
1801 }
1802 else if (dsize == 4) {
1803 value = newSVuv( buffer_get_int(mp4->buf) );
1804 }
1805 else if (dsize == 8) {
1806 value = newSVuv( buffer_get_int64(mp4->buf) );
1807 }
1808 else {
1809 value = newSVpvn( buffer_ptr(mp4->buf), dsize );
1810 buffer_consume(mp4->buf, dsize);
1811 }
1812 }
1813 }
1814 else { // text data
1815 value = newSVpvn( buffer_ptr(mp4->buf), size - 8 );
1816 sv_utf8_decode(value);
1817
1818 // strip copyright symbol 0xA9 out of key
1819 if ( ckey[0] == 0xA9 ) {
1820 ckey++;
1821 }
1822
1823 DEBUG_TRACE(" %s = %s\n", ckey, SvPVX(value));
1824
1825 buffer_consume(mp4->buf, size - 8);
1826 }
1827 }
1828
1829 // if key exists, create array
1830 if ( my_hv_exists( mp4->tags, (char *)ckey ) ) {
1831 SV **entry = my_hv_fetch( mp4->tags, (char *)ckey );
1832 if (entry != NULL) {
1833 if ( SvROK(*entry) && SvTYPE(SvRV(*entry)) == SVt_PVAV ) {
1834 av_push( (AV *)SvRV(*entry), value );
1835 }
1836 else {
1837 // A non-array entry, convert to array.
1838 AV *ref = newAV();
1839 av_push( ref, newSVsv(*entry) );
1840 av_push( ref, value );
1841 my_hv_store( mp4->tags, (char *)ckey, newRV_noinc( (SV*)ref ) );
1842 }
1843 }
1844 }
1845 else {
1846 my_hv_store( mp4->tags, (char *)ckey, value );
1847 }
1848
1849 return 1;
1850 }
1851
1852 uint8_t
1853 _mp4_parse_ilst_custom(mp4info *mp4, uint32_t size)
1854 {
1855 SV *key = NULL;
1856
1857 while (size) {
1858 char type[5];
1859 uint32_t bsize;
1860
1861 // Ensure we have 8 bytes to get the size and type
1862 if ( !_check_buf(mp4->infile, mp4->buf, 8, MP4_BLOCK_SIZE) ) {
1863 return 0;
1864 }
1865
1866 // Read box
1867 bsize = buffer_get_int(mp4->buf);
1868 strncpy( type, (char *)buffer_ptr(mp4->buf), 4 );
1869 type[4] = '\0';
1870 buffer_consume(mp4->buf, 4);
1871
1872 DEBUG_TRACE(" %s size %d\n", type, bsize);
1873
1874 if ( FOURCC_EQ(type, "name") ) {
1875 // Ensure we have bsize bytes
1876 if ( !_check_buf(mp4->infile, mp4->buf, bsize, MP4_BLOCK_SIZE) ) {
1877 return 0;
1878 }
1879
1880 buffer_consume(mp4->buf, 4); // padding
1881 key = newSVpvn( buffer_ptr(mp4->buf), bsize - 12);
1882 sv_utf8_decode(key);
1883 upcase(SvPVX(key));
1884 buffer_consume(mp4->buf, bsize - 12);
1885
1886 DEBUG_TRACE(" %s\n", SvPVX(key));
1887 }
1888 else if ( FOURCC_EQ(type, "data") ) {
1889 if (!key) {
1890 // No key yet, data is out of order
1891 return 0;
1892 }
1893
1894 if ( !_mp4_parse_ilst_data(mp4, bsize - 8, key) ) {
1895 SvREFCNT_dec(key);
1896 return 0;
1897 }
1898 }
1899 else {
1900 // skip (mean, or other boxes)
1901 if ( !_check_buf(mp4->infile, mp4->buf, bsize - 8, MP4_BLOCK_SIZE) ) {
1902 return 0;
1903 }
1904
1905 buffer_consume(mp4->buf, bsize - 8);
1906 }
1907
1908 size -= bsize;
1909 }
1910
1911 SvREFCNT_dec(key);
1912
1913 return 1;
1914 }
1915
1916 HV *
1917 _mp4_get_current_trackinfo(mp4info *mp4)
1918 {
1919 // Return the trackinfo hash for track id == mp4->current_track
1920 AV *tracks;
1921 HV *trackinfo;
1922 int i;
1923
1924 SV **entry = my_hv_fetch(mp4->info, "tracks");
1925 if (entry != NULL) {
1926 tracks = (AV *)SvRV(*entry);
1927 }
1928 else {
1929 return NULL;
1930 }
1931
1932 // Find entry for this stream number
1933 for (i = 0; av_len(tracks) >= 0 && i <= av_len(tracks); i++) {
1934 SV **info = av_fetch(tracks, i, 0);
1935 if (info != NULL) {
1936 SV **tid;
1937
1938 trackinfo = (HV *)SvRV(*info);
1939 tid = my_hv_fetch( trackinfo, "id" );
1940 if (tid != NULL) {
1941 if ( SvIV(*tid) == mp4->current_track ) {
1942 return trackinfo;
1943 }
1944 }
1945 }
1946 }
1947
1948 return NULL;
1949 }
1950
1951 uint32_t
1952 _mp4_descr_length(Buffer *buf)
1953 {
1954 uint8_t b;
1955 uint8_t num_bytes = 0;
1956 uint32_t length = 0;
1957
1958 do {
1959 b = buffer_get_char(buf);
1960 num_bytes++;
1961 length = (length << 7) | (b & 0x7f);
1962 } while ( (b & 0x80) && num_bytes < 4 );
1963
1964 return length;
1965 }
1966
1967 void
1968 _mp4_skip(mp4info *mp4, uint32_t size)
1969 {
1970 if ( buffer_len(mp4->buf) >= size ) {
1971 //buffer_dump(mp4->buf, size);
1972 buffer_consume(mp4->buf, size);
1973
1974 DEBUG_TRACE(" skipped buffer data size %d\n", size);
1975 }
1976 else {
1977 PerlIO_seek(mp4->infile, size - buffer_len(mp4->buf), SEEK_CUR);
1978 buffer_clear(mp4->buf);
1979
1980 DEBUG_TRACE(" seeked past %d bytes to %d\n", size, (int)PerlIO_tell(mp4->infile));
1981 }
1982 }
1983
1984 uint32_t
1985 _mp4_samples_in_chunk(mp4info *mp4, uint32_t chunk)
1986 {
1987 int i;
1988
1989 for (i = mp4->num_sample_to_chunks - 1; i >= 0; i--) {
1990 if (mp4->sample_to_chunk[i].first_chunk <= chunk) {
1991 return mp4->sample_to_chunk[i].samples_per_chunk;
1992 }
1993 }
1994
1995 return mp4->sample_to_chunk[0].samples_per_chunk;
1996 }
1997
1998 uint32_t
1999 _mp4_total_samples(mp4info *mp4)
2000 {
2001 int i;
2002 uint32_t total = 0;
2003
2004 for (i = 0; i < mp4->num_time_to_samples; i++) {
2005 total += mp4->time_to_sample[i].sample_count;
2006 }
2007
2008 return total;
2009 }
2010
2011 uint32_t
2012 _mp4_get_sample_duration(mp4info *mp4, uint32_t sample)
2013 {
2014 int i;
2015 uint32_t co = 0;
2016
2017 for (i = 0; i < mp4->num_time_to_samples; i++) {
2018 uint32_t delta = mp4->time_to_sample[i].sample_count;
2019 if (sample < co + delta) {
2020 return mp4->time_to_sample[i].sample_duration;
2021 }
2022
2023 co += delta;
2024 }
2025
2026 return 0;
2027 }