2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
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.
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id: mp4sample.c,v 1.15 2004/01/11 15:52:19 menno Exp $
32 static int32_t mp4ff_chunk_of_sample(const mp4ff_t *f, const int32_t track, const int32_t sample,
33 int32_t *chunk_sample, int32_t *chunk)
35 int32_t total_entries = 0;
37 int32_t chunk1, chunk2, chunk1samples, range_samples, total = 0;
39 if (f->track[track] == NULL)
44 total_entries = f->track[track]->stsc_entry_count;
52 chunk2 = f->track[track]->stsc_first_chunk[chunk2entry];
53 *chunk = chunk2 - chunk1;
54 range_samples = *chunk * chunk1samples;
56 if (sample < total + range_samples) break;
58 chunk1samples = f->track[track]->stsc_samples_per_chunk[chunk2entry];
61 if(chunk2entry < total_entries)
64 total += range_samples;
66 } while (chunk2entry < total_entries);
69 *chunk = (sample - total) / chunk1samples + chunk1;
73 *chunk_sample = total + (*chunk - chunk1) * chunk1samples;
78 static int32_t mp4ff_chunk_to_offset(const mp4ff_t *f, const int32_t track, const int32_t chunk)
80 const mp4ff_track_t * p_track = f->track[track];
82 if (p_track->stco_entry_count && (chunk > p_track->stco_entry_count))
84 return p_track->stco_chunk_offset[p_track->stco_entry_count - 1];
85 } else if (p_track->stco_entry_count) {
86 return p_track->stco_chunk_offset[chunk - 1];
94 static int32_t mp4ff_sample_range_size(const mp4ff_t *f, const int32_t track,
95 const int32_t chunk_sample, const int32_t sample)
98 const mp4ff_track_t * p_track = f->track[track];
100 if (p_track->stsz_sample_size)
102 return (sample - chunk_sample) * p_track->stsz_sample_size;
106 if (sample>=p_track->stsz_sample_count) return 0;//error
108 for(i = chunk_sample, total = 0; i < sample; i++)
110 total += p_track->stsz_table[i];
117 static int32_t mp4ff_sample_to_offset(const mp4ff_t *f, const int32_t track, const int32_t sample)
119 int32_t chunk, chunk_sample, chunk_offset1, chunk_offset2;
121 mp4ff_chunk_of_sample(f, track, sample, &chunk_sample, &chunk);
123 chunk_offset1 = mp4ff_chunk_to_offset(f, track, chunk);
124 chunk_offset2 = chunk_offset1 + mp4ff_sample_range_size(f, track, chunk_sample, sample);
126 return chunk_offset2;
129 int32_t mp4ff_audio_frame_size(const mp4ff_t *f, const int32_t track, const int32_t sample)
132 const mp4ff_track_t * p_track = f->track[track];
134 if (p_track->stsz_sample_size)
136 bytes = p_track->stsz_sample_size;
138 bytes = p_track->stsz_table[sample];
144 int32_t mp4ff_set_sample_position(mp4ff_t *f, const int32_t track, const int32_t sample)
148 offset = mp4ff_sample_to_offset(f, track, sample);
149 mp4ff_set_position(f, offset);