1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
21 **
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24 **
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27 **
28 ** $Id: mp4ff.c,v 1.22 2009/01/26 23:01:40 menno Exp $
29 **/
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include "mp4ffint.h"
35 
36 //#define trace(...) { fprintf(stderr, __VA_ARGS__); }
37 #define trace(fmt,...)
38 
39 static int32_t parse_atoms_int (mp4ff_t *f,int meta_only,int stop_on_mdat);
40 
mp4ff_open_read_int(mp4ff_callback_t * f,int is_streaming)41 static mp4ff_t *mp4ff_open_read_int(mp4ff_callback_t *f, int is_streaming)
42 {
43     mp4ff_t *ff = malloc(sizeof(mp4ff_t));
44 
45     memset(ff, 0, sizeof(mp4ff_t));
46 
47     ff->stream = f;
48 
49     if (parse_atoms_int(ff,is_streaming,is_streaming) < 0) {
50         mp4ff_close (ff);
51         return NULL;
52     }
53 
54     return ff;
55 }
56 
mp4ff_open_read(mp4ff_callback_t * f)57 mp4ff_t *mp4ff_open_read(mp4ff_callback_t *f)
58 {
59     return mp4ff_open_read_int (f, 0);
60 }
61 
mp4ff_open_read_streaming(mp4ff_callback_t * f)62 mp4ff_t *mp4ff_open_read_streaming(mp4ff_callback_t *f)
63 {
64     return mp4ff_open_read_int (f, 1);
65 }
66 
mp4ff_open_read_metaonly(mp4ff_callback_t * f)67 mp4ff_t *mp4ff_open_read_metaonly(mp4ff_callback_t *f)
68 {
69     mp4ff_t *ff = malloc(sizeof(mp4ff_t));
70 
71     memset(ff, 0, sizeof(mp4ff_t));
72 
73     ff->stream = f;
74 
75     parse_atoms(ff,1);
76 
77     return ff;
78 }
79 
mp4ff_track_free(mp4ff_track_t * trk)80 void mp4ff_track_free (mp4ff_track_t *trk) {
81 #if 0
82     if (trk->chunk_sample_first) {
83         free (trk->chunk_sample_first);
84     }
85     if (trk->chunk_first_dts) {
86         free (trk->chunk_first_dts);
87     }
88     if (trk->chunk_last_dts) {
89         free (trk->chunk_last_dts);
90     }
91     if (trk->p_sample_count_dts) {
92         for (int i = 0; i < trk->stsc_entry_count; i++) {
93             free (trk->p_sample_count_dts[i]);
94         }
95         free (trk->p_sample_count_dts);
96     }
97     if (trk->p_sample_delta_dts) {
98         for (int i = 0; i < trk->stsc_entry_count; i++) {
99             free (trk->p_sample_delta_dts[i]);
100         }
101         free (trk->p_sample_delta_dts);
102     }
103     if (trk->p_sample_count_pts) {
104         for (int i = 0; i < trk->stsc_entry_count; i++) {
105             free (trk->p_sample_count_pts[i]);
106         }
107         free (trk->p_sample_count_pts);
108     }
109     if (trk->p_sample_offset_pts) {
110         for (int i = 0; i < trk->stsc_entry_count; i++) {
111             free (trk->p_sample_offset_pts[i]);
112         }
113         free (trk->p_sample_offset_pts);
114     }
115 #endif
116     free (trk);
117 }
118 
mp4ff_close(mp4ff_t * ff)119 void mp4ff_close(mp4ff_t *ff)
120 {
121     int32_t i;
122 
123     for (i = 0; i < ff->total_tracks; i++)
124     {
125         if (ff->track[i])
126         {
127             if (ff->track[i]->stsz_table)
128                 free(ff->track[i]->stsz_table);
129             if (ff->track[i]->stts_sample_count)
130                 free(ff->track[i]->stts_sample_count);
131             if (ff->track[i]->stts_sample_delta)
132                 free(ff->track[i]->stts_sample_delta);
133             if (ff->track[i]->stsc_first_chunk)
134                 free(ff->track[i]->stsc_first_chunk);
135             if (ff->track[i]->stsc_samples_per_chunk)
136                 free(ff->track[i]->stsc_samples_per_chunk);
137             if (ff->track[i]->stsc_sample_desc_index)
138                 free(ff->track[i]->stsc_sample_desc_index);
139             if (ff->track[i]->stco_chunk_offset)
140                 free(ff->track[i]->stco_chunk_offset);
141             if (ff->track[i]->decoderConfig)
142                 free(ff->track[i]->decoderConfig);
143 			if (ff->track[i]->ctts_sample_count)
144 				free(ff->track[i]->ctts_sample_count);
145 			if (ff->track[i]->ctts_sample_offset)
146 				free(ff->track[i]->ctts_sample_offset);
147 #ifdef ITUNES_DRM
148             if (ff->track[i]->p_drms)
149                 drms_free(ff->track[i]->p_drms);
150 #endif
151             mp4ff_track_free (ff->track[i]);
152         }
153     }
154 
155 #ifdef USE_TAGGING
156     mp4ff_tag_delete(&(ff->tags));
157 #endif
158 
159     mp4ff_chapters_free (ff);
160     mp4ff_tref_free (ff);
161 
162     if (ff) free(ff);
163 }
164 
mp4ff_track_add(mp4ff_t * f)165 void mp4ff_track_add(mp4ff_t *f)
166 {
167     f->total_tracks++;
168 
169     f->track[f->total_tracks - 1] = malloc(sizeof(mp4ff_track_t));
170 
171     memset(f->track[f->total_tracks - 1], 0, sizeof(mp4ff_track_t));
172 }
173 
need_parse_when_meta_only(uint8_t atom_type)174 static int need_parse_when_meta_only(uint8_t atom_type)
175 {
176 	switch(atom_type)
177 	{
178 	case ATOM_EDTS:
179 //	case ATOM_MDIA:
180 //	case ATOM_MINF:
181 	case ATOM_DRMS:
182 	case ATOM_SINF:
183 	case ATOM_SCHI:
184 //	case ATOM_STBL:
185 //	case ATOM_STSD:
186 	case ATOM_STTS:
187 	case ATOM_STSZ:
188 	case ATOM_STZ2:
189 	case ATOM_STCO:
190 	case ATOM_STSC:
191 //	case ATOM_CTTS:
192 	case ATOM_FRMA:
193 	case ATOM_IVIV:
194 	case ATOM_PRIV:
195 		return 0;
196 	default:
197 		return 1;
198 	}
199 }
200 
201 /* parse atoms that are sub atoms of other atoms */
parse_sub_atoms(mp4ff_t * f,const uint64_t total_size,int meta_only)202 int32_t parse_sub_atoms(mp4ff_t *f, const uint64_t total_size,int meta_only)
203 {
204     uint64_t size;
205     uint8_t atom_type = 0;
206     uint64_t counted_size = 0;
207     uint8_t header_size = 0;
208 
209     while (counted_size < total_size)
210     {
211         size = mp4ff_atom_read_header(f, &atom_type, &header_size);
212         counted_size += size;
213 
214         /* check for end of file */
215         if (size == 0)
216             break;
217 
218         /* we're starting to read a new track, update index,
219          * so that all data and tables get written in the right place
220          */
221         if (atom_type == ATOM_TRAK)
222         {
223             mp4ff_track_add(f);
224         }
225 
226         /* parse subatoms */
227 		if (meta_only && !need_parse_when_meta_only(atom_type))
228 		{
229 			mp4ff_set_position(f, mp4ff_position(f)+size-header_size);
230 		} else if (atom_type < SUBATOMIC)
231         {
232             parse_sub_atoms(f, size-header_size,meta_only);
233         } else {
234             mp4ff_atom_read(f, (uint32_t)size, atom_type);
235         }
236 #if 0
237         if (atom_type == ATOM_TRAK)
238         {
239             trace ("mp4ff_track_create_chunks_index\n");
240             mp4ff_track_create_chunks_index (f, f->track[f->total_tracks-1]);
241             trace ("mp4ff_track_create_samples_index\n");
242             mp4ff_track_create_samples_index (f, f->track[f->total_tracks-1]);
243         }
244 #endif
245     }
246 
247     return 0;
248 }
249 
250 /* parse root atoms */
parse_atoms_int(mp4ff_t * f,int meta_only,int stop_on_mdat)251 static int32_t parse_atoms_int (mp4ff_t *f,int meta_only,int stop_on_mdat)
252 {
253     uint64_t size;
254     uint8_t atom_type = 0;
255     uint8_t header_size = 0;
256     int had_valid_atoms = 0;
257 
258     f->file_size = 0;
259 
260     while ((size = mp4ff_atom_read_header(f, &atom_type, &header_size)) != 0)
261     {
262         // FIXME: ATOM_UNKNOWN is returned both when a valid (but unknown) atom is encontered,
263         // and for invalid data. They need to be handled separately.
264         if (atom_type == ATOM_UNKNOWN && !had_valid_atoms && mp4ff_position(f)+size > 100) {
265             return -1;
266         }
267 
268         had_valid_atoms = 1;
269 
270         f->file_size += size;
271         f->last_atom = atom_type;
272 
273         if (atom_type == ATOM_MDAT && f->moov_read)
274         {
275             /* moov atom is before mdat, we can stop reading when mdat is encountered */
276             /* file position will stay at beginning of mdat data */
277             if (!stop_on_mdat) {
278                 break;
279             }
280         }
281 
282         if (atom_type == ATOM_MOOV && size > header_size)
283         {
284             f->moov_read = 1;
285             f->moov_offset = mp4ff_position(f)-header_size;
286             f->moov_size = size;
287         }
288 
289         /* parse subatoms */
290 		if (meta_only && !need_parse_when_meta_only(atom_type))
291 		{
292 			mp4ff_set_position(f, mp4ff_position(f)+size-header_size);
293 		} else if (atom_type < SUBATOMIC)
294         {
295             parse_sub_atoms(f, size-header_size,meta_only);
296         } else {
297             /* skip this atom */
298             mp4ff_set_position(f, mp4ff_position(f)+size-header_size);
299         }
300     }
301 
302     return 0;
303 }
304 
parse_atoms(mp4ff_t * f,int meta_only)305 int32_t parse_atoms(mp4ff_t *f,int meta_only)
306 {
307     return parse_atoms_int (f, meta_only, 0);
308 }
309 
mp4ff_get_decoder_config(const mp4ff_t * f,const int32_t track,uint8_t ** ppBuf,uint32_t * pBufSize)310 int32_t mp4ff_get_decoder_config(const mp4ff_t *f, const int32_t track,
311                                  uint8_t** ppBuf, uint32_t* pBufSize)
312 {
313     if (track >= f->total_tracks)
314     {
315         *ppBuf = NULL;
316         *pBufSize = 0;
317         return 1;
318     }
319 
320     if (f->track[track]->decoderConfig == NULL || f->track[track]->decoderConfigLen == 0)
321     {
322         *ppBuf = NULL;
323         *pBufSize = 0;
324     } else {
325         *ppBuf = malloc(f->track[track]->decoderConfigLen);
326         if (*ppBuf == NULL)
327         {
328             *pBufSize = 0;
329             return 1;
330         }
331         memcpy(*ppBuf, f->track[track]->decoderConfig, f->track[track]->decoderConfigLen);
332         *pBufSize = f->track[track]->decoderConfigLen;
333     }
334 
335     return 0;
336 }
337 
mp4ff_get_track_type(const mp4ff_t * f,const int track)338 int32_t mp4ff_get_track_type(const mp4ff_t *f, const int track)
339 {
340 	return f->track[track]->type;
341 }
342 
mp4ff_get_track_id(const mp4ff_t * f,const int track)343 int32_t mp4ff_get_track_id(const mp4ff_t *f, const int track)
344 {
345 	return f->track[track]->id;
346 }
347 
mp4ff_total_tracks(const mp4ff_t * f)348 int32_t mp4ff_total_tracks(const mp4ff_t *f)
349 {
350     return f->total_tracks;
351 }
352 
mp4ff_time_scale(const mp4ff_t * f,const int32_t track)353 int32_t mp4ff_time_scale(const mp4ff_t *f, const int32_t track)
354 {
355     return f->track[track]->timeScale;
356 }
357 
mp4ff_get_avg_bitrate(const mp4ff_t * f,const int32_t track)358 uint32_t mp4ff_get_avg_bitrate(const mp4ff_t *f, const int32_t track)
359 {
360 	return f->track[track]->avgBitrate;
361 }
362 
mp4ff_get_max_bitrate(const mp4ff_t * f,const int32_t track)363 uint32_t mp4ff_get_max_bitrate(const mp4ff_t *f, const int32_t track)
364 {
365 	return f->track[track]->maxBitrate;
366 }
367 
mp4ff_get_track_duration(const mp4ff_t * f,const int32_t track)368 int64_t mp4ff_get_track_duration(const mp4ff_t *f, const int32_t track)
369 {
370 	return f->track[track]->duration;
371 }
372 
mp4ff_get_track_duration_use_offsets(const mp4ff_t * f,const int32_t track)373 int64_t mp4ff_get_track_duration_use_offsets(const mp4ff_t *f, const int32_t track)
374 {
375 	int64_t duration = mp4ff_get_track_duration(f,track);
376 	if (duration!=-1)
377 	{
378 		int64_t offset = mp4ff_get_sample_offset(f,track,0);
379 		if (offset > duration) duration = 0;
380 		else duration -= offset;
381 	}
382 	return duration;
383 }
384 
385 
mp4ff_num_samples(const mp4ff_t * f,const int32_t track)386 int32_t mp4ff_num_samples(const mp4ff_t *f, const int32_t track)
387 {
388     int32_t i;
389     int32_t total = 0;
390 
391     for (i = 0; i < f->track[track]->stts_entry_count; i++)
392     {
393         total += f->track[track]->stts_sample_count[i];
394     }
395     return total;
396 }
397 
398 
399 
400 
mp4ff_get_sample_rate(const mp4ff_t * f,const int32_t track)401 uint32_t mp4ff_get_sample_rate(const mp4ff_t *f, const int32_t track)
402 {
403 	return f->track[track]->sampleRate;
404 }
405 
mp4ff_get_channel_count(const mp4ff_t * f,const int32_t track)406 uint32_t mp4ff_get_channel_count(const mp4ff_t * f,const int32_t track)
407 {
408 	return f->track[track]->channelCount;
409 }
410 
mp4ff_get_audio_type(const mp4ff_t * f,const int32_t track)411 uint32_t mp4ff_get_audio_type(const mp4ff_t * f,const int32_t track)
412 {
413 	return f->track[track]->audioType;
414 }
415 
mp4ff_get_sample_duration_use_offsets(const mp4ff_t * f,const int32_t track,const int32_t sample)416 int32_t mp4ff_get_sample_duration_use_offsets(const mp4ff_t *f, const int32_t track, const int32_t sample)
417 {
418 	int32_t d,o;
419 	d = mp4ff_get_sample_duration(f,track,sample);
420 	if (d!=-1)
421 	{
422 		o = mp4ff_get_sample_offset(f,track,sample);
423 		if (o>d) d = 0;
424 		else d -= o;
425 	}
426 	return d;
427 }
428 
mp4ff_get_sample_duration(const mp4ff_t * f,const int32_t track,const int32_t sample)429 int32_t mp4ff_get_sample_duration(const mp4ff_t *f, const int32_t track, const int32_t sample)
430 {
431     int32_t i, co = 0;
432 
433     for (i = 0; i < f->track[track]->stts_entry_count; i++)
434     {
435 		int32_t delta = f->track[track]->stts_sample_count[i];
436 		if (sample < co + delta)
437 			return f->track[track]->stts_sample_delta[i];
438 		co += delta;
439     }
440     return (int32_t)(-1);
441 }
442 
mp4ff_get_num_sample_byte_sizes(const mp4ff_t * f,const int32_t track)443 int32_t mp4ff_get_num_sample_byte_sizes (const mp4ff_t *f, const int32_t track) {
444     return f->track[track]->stsz_sample_count;
445 }
446 
mp4ff_get_sample_info(const mp4ff_t * f,const int32_t track,const int32_t samplenum,uint32_t * sample_duration,uint32_t * sample_byte_size)447 int32_t mp4ff_get_sample_info(const mp4ff_t *f, const int32_t track, const int32_t samplenum, uint32_t *sample_duration, uint32_t *sample_byte_size) {
448 // num_sample_byte_sizes --- stsz_sample_count
449 // num_time_to_samples --- stts_entry_count
450 // time_to_sample[i].sample_count --- p_track->stts_sample_count[i]
451 // time_to_sample[i].sample_duration --- p_track->stts_sample_delta[i]
452 // sample_byte_size[i] --- p_track->stsz_table[i]
453     unsigned int duration_index_accum = 0;
454     unsigned int duration_cur_index = 0;
455 
456     if (samplenum >= f->track[track]->stsz_sample_count)
457     {
458         fprintf(stderr, "sample %i does not exist\n", samplenum);
459         return 0;
460     }
461 
462     if (!f->track[track]->stts_entry_count)
463     {
464         fprintf(stderr, "no time to samples\n");
465         return 0;
466     }
467     while ((f->track[track]->stts_sample_count[duration_cur_index] + duration_index_accum) <= samplenum)
468     {
469         duration_index_accum += f->track[track]->stts_sample_count[duration_cur_index];
470         duration_cur_index++;
471         if (duration_cur_index >= f->track[track]->stts_entry_count)
472         {
473             fprintf(stderr, "sample %i does not have a duration\n", samplenum);
474             return 0;
475         }
476     }
477 
478     *sample_duration = f->track[track]->stts_sample_delta[duration_cur_index];
479     *sample_byte_size = f->track[track]->stsz_table[samplenum];
480 
481     return 1;
482 }
483 
mp4ff_get_sample_position(const mp4ff_t * f,const int32_t track,const int32_t sample)484 int64_t mp4ff_get_sample_position(const mp4ff_t *f, const int32_t track, const int32_t sample)
485 {
486     int32_t i, co = 0;
487 	int64_t acc = 0;
488 
489     for (i = 0; i < f->track[track]->stts_entry_count; i++)
490     {
491 		int32_t delta = f->track[track]->stts_sample_count[i];
492 		if (sample < co + delta)
493 		{
494 			acc += f->track[track]->stts_sample_delta[i] * (sample - co);
495 			return acc;
496 		}
497 		else
498 		{
499 			acc += f->track[track]->stts_sample_delta[i] * delta;
500 		}
501 		co += delta;
502     }
503     return (int64_t)(-1);
504 }
505 
mp4ff_get_sample_offset(const mp4ff_t * f,const int32_t track,const int32_t sample)506 int32_t mp4ff_get_sample_offset(const mp4ff_t *f, const int32_t track, const int32_t sample)
507 {
508     int32_t i, co = 0;
509 
510     for (i = 0; i < f->track[track]->ctts_entry_count; i++)
511     {
512 		int32_t delta = f->track[track]->ctts_sample_count[i];
513 		if (sample < co + delta)
514 			return f->track[track]->ctts_sample_offset[i];
515 		co += delta;
516     }
517     return 0;
518 }
519 
mp4ff_find_sample(const mp4ff_t * f,const int32_t track,const int64_t offset,int32_t * toskip)520 int32_t mp4ff_find_sample(const mp4ff_t *f, const int32_t track, const int64_t offset,int32_t * toskip)
521 {
522 	int32_t i, co = 0;
523 	int64_t offset_total = 0;
524 	mp4ff_track_t * p_track = f->track[track];
525 
526 	for (i = 0; i < p_track->stts_entry_count; i++)
527 	{
528 		int32_t sample_count = p_track->stts_sample_count[i];
529 		int32_t sample_delta = p_track->stts_sample_delta[i];
530 		int64_t offset_delta = (int64_t)sample_delta * (int64_t)sample_count;
531 		if (offset < offset_total + offset_delta)
532 		{
533 			int64_t offset_fromstts = offset - offset_total;
534 			if (toskip) *toskip = (int32_t)(offset_fromstts % sample_delta);
535 			return co + (int32_t)(offset_fromstts / sample_delta);
536 		}
537 		else
538 		{
539 			offset_total += offset_delta;
540 		}
541 		co += sample_count;
542 	}
543 	return (int32_t)(-1);
544 }
545 
mp4ff_find_sample_use_offsets(const mp4ff_t * f,const int32_t track,const int64_t offset,int32_t * toskip)546 int32_t mp4ff_find_sample_use_offsets(const mp4ff_t *f, const int32_t track, const int64_t offset,int32_t * toskip)
547 {
548 	return mp4ff_find_sample(f,track,offset + mp4ff_get_sample_offset(f,track,0),toskip);
549 }
550 
mp4ff_read_sample(mp4ff_t * f,const int32_t track,const int32_t sample,uint8_t ** audio_buffer,uint32_t * bytes)551 int32_t mp4ff_read_sample(mp4ff_t *f, const int32_t track, const int32_t sample,
552                           uint8_t **audio_buffer,  uint32_t *bytes)
553 {
554     int32_t result = 0;
555 
556     *bytes = mp4ff_audio_frame_size(f, track, sample);
557 
558 	if (*bytes==0) return 0;
559 
560     *audio_buffer = (uint8_t*)malloc(*bytes);
561     if (!(*audio_buffer)) {
562         //fprintf (stderr, "mp4ff_read_sample: malloc failure (tried to alloc %d bytes). possible mp4ff bug or memleak! please report a bug to deadbeef developers (i'm serious).\n", *bytes);
563         return 0;
564     }
565 
566     mp4ff_set_sample_position(f, track, sample);
567 
568     result = mp4ff_read_data(f, *audio_buffer, *bytes);
569 
570     if (!result)
571 	{
572 		free(*audio_buffer);
573 		*audio_buffer = 0;
574         return 0;
575 	}
576 
577 #ifdef ITUNES_DRM
578     if (f->track[track]->p_drms != NULL)
579     {
580         drms_decrypt(f->track[track]->p_drms, (uint32_t*)*audio_buffer, *bytes);
581     }
582 #endif
583 
584     return *bytes;
585 }
586 
587 
mp4ff_read_sample_v2(mp4ff_t * f,const int track,const int sample,unsigned char * buffer)588 int32_t mp4ff_read_sample_v2(mp4ff_t *f, const int track, const int sample,unsigned char *buffer)
589 {
590     int32_t result = 0;
591 	int32_t size = mp4ff_audio_frame_size(f,track,sample);
592 	if (size<=0) return 0;
593 	mp4ff_set_sample_position(f, track, sample);
594 	result = mp4ff_read_data(f,buffer,size);
595 
596 #ifdef ITUNES_DRM
597     if (f->track[track]->p_drms != NULL)
598     {
599         drms_decrypt(f->track[track]->p_drms, (uint32_t*)buffer, size);
600     }
601 #endif
602 
603     return result;
604 }
605 
mp4ff_read_sample_getsize(mp4ff_t * f,const int track,const int sample)606 int32_t mp4ff_read_sample_getsize(mp4ff_t *f, const int track, const int sample)
607 {
608 	int32_t temp = mp4ff_audio_frame_size(f, track, sample);
609 	if (temp<0) temp = 0;
610 	return temp;
611 }
612