New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / codecs / libm4a / m4a.c
blobf0666403ed2d8833f96f65ca7e62f0ea0c65126d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <codecs.h>
23 #include <inttypes.h>
24 #include "m4a.h"
26 /* Implementation of the stream.h functions used by libalac */
28 #define _Swap32(v) do { \
29 v = (((v) & 0x000000FF) << 0x18) | \
30 (((v) & 0x0000FF00) << 0x08) | \
31 (((v) & 0x00FF0000) >> 0x08) | \
32 (((v) & 0xFF000000) >> 0x18); } while(0)
34 #define _Swap16(v) do { \
35 v = (((v) & 0x00FF) << 0x08) | \
36 (((v) & 0xFF00) >> 0x08); } while (0)
38 /* A normal read without any byte-swapping */
39 void stream_read(stream_t *stream, size_t size, void *buf)
41 stream->ci->read_filebuf(buf,size);
42 if (stream->ci->curpos >= stream->ci->filesize) { stream->eof=1; }
45 int32_t stream_read_int32(stream_t *stream)
47 int32_t v;
48 stream_read(stream, 4, &v);
49 #ifdef ROCKBOX_LITTLE_ENDIAN
50 _Swap32(v);
51 #endif
52 return v;
55 int32_t stream_tell(stream_t *stream)
57 return stream->ci->curpos;
60 uint32_t stream_read_uint32(stream_t *stream)
62 uint32_t v;
63 stream_read(stream, 4, &v);
64 #ifdef ROCKBOX_LITTLE_ENDIAN
65 _Swap32(v);
66 #endif
67 return v;
70 int16_t stream_read_int16(stream_t *stream)
72 int16_t v;
73 stream_read(stream, 2, &v);
74 #ifdef ROCKBOX_LITTLE_ENDIAN
75 _Swap16(v);
76 #endif
77 return v;
80 uint16_t stream_read_uint16(stream_t *stream)
82 uint16_t v;
83 stream_read(stream, 2, &v);
84 #ifdef ROCKBOX_LITTLE_ENDIAN
85 _Swap16(v);
86 #endif
87 return v;
90 int8_t stream_read_int8(stream_t *stream)
92 int8_t v;
93 stream_read(stream, 1, &v);
94 return v;
97 uint8_t stream_read_uint8(stream_t *stream)
99 uint8_t v;
100 stream_read(stream, 1, &v);
101 return v;
104 void stream_skip(stream_t *stream, size_t skip)
106 stream->ci->advance_buffer(skip);
109 int stream_eof(stream_t *stream)
111 return stream->eof;
114 void stream_create(stream_t *stream,struct codec_api* ci)
116 stream->ci=ci;
117 stream->eof=0;
120 /* This function was part of the original alac decoder implementation */
122 int get_sample_info(demux_res_t *demux_res, uint32_t samplenum,
123 uint32_t *sample_duration,
124 uint32_t *sample_byte_size)
126 unsigned int duration_index_accum = 0;
127 unsigned int duration_cur_index = 0;
129 if (samplenum >= demux_res->num_sample_byte_sizes) {
130 return 0;
133 if (!demux_res->num_time_to_samples) {
134 return 0;
137 while ((demux_res->time_to_sample[duration_cur_index].sample_count
138 + duration_index_accum) <= samplenum) {
139 duration_index_accum +=
140 demux_res->time_to_sample[duration_cur_index].sample_count;
142 duration_cur_index++;
143 if (duration_cur_index >= demux_res->num_time_to_samples) {
144 return 0;
148 *sample_duration =
149 demux_res->time_to_sample[duration_cur_index].sample_duration;
150 *sample_byte_size = demux_res->sample_byte_size[samplenum];
152 return 1;
155 unsigned int get_sample_offset(demux_res_t *demux_res, uint32_t sample)
157 uint32_t chunk = 1;
158 uint32_t range_samples = 0;
159 uint32_t total_samples = 0;
160 uint32_t chunk_sample;
161 uint32_t prev_chunk;
162 uint32_t prev_chunk_samples;
163 uint32_t file_offset;
164 uint32_t i;
166 /* First check we have the appropriate metadata - we should always
167 * have it.
170 if (sample >= demux_res->num_sample_byte_sizes ||
171 !demux_res->num_sample_to_chunks ||
172 !demux_res->num_chunk_offsets)
174 return 0;
177 /* Locate the chunk containing the sample */
179 prev_chunk = demux_res->sample_to_chunk[0].first_chunk;
180 prev_chunk_samples = demux_res->sample_to_chunk[0].num_samples;
182 for (i = 1; i < demux_res->num_sample_to_chunks; i++)
184 chunk = demux_res->sample_to_chunk[i].first_chunk;
185 range_samples = (chunk - prev_chunk) * prev_chunk_samples;
187 if (sample < total_samples + range_samples)
189 break;
192 total_samples += range_samples;
193 prev_chunk = demux_res->sample_to_chunk[i].first_chunk;
194 prev_chunk_samples = demux_res->sample_to_chunk[i].num_samples;
197 if (sample >= demux_res->sample_to_chunk[0].num_samples)
199 chunk = prev_chunk + (sample - total_samples) / prev_chunk_samples;
201 else
203 chunk = 1;
206 /* Get sample of the first sample in the chunk */
208 chunk_sample = total_samples + (chunk - prev_chunk) * prev_chunk_samples;
210 /* Get offset in file */
212 if (chunk > demux_res->num_chunk_offsets)
214 file_offset = demux_res->chunk_offset[demux_res->num_chunk_offsets - 1];
216 else
218 file_offset = demux_res->chunk_offset[chunk - 1];
221 if (chunk_sample > sample)
223 return 0;
226 for (i = chunk_sample; i < sample; i++)
228 file_offset += demux_res->sample_byte_size[i];
231 if (file_offset > demux_res->mdat_offset + demux_res->mdat_len)
233 return 0;
236 return file_offset;
239 /* Seek to the sample containing sound_sample_loc. Return 1 on success
240 * (and modify sound_samples_done and current_sample), 0 if failed.
242 * Seeking uses the following arrays:
244 * 1) the time_to_sample array contains the duration (in sound samples)
245 * of each sample of data.
247 * 2) the sample_byte_size array contains the length in bytes of each
248 * sample.
250 * 3) the sample_to_chunk array contains information about which chunk
251 * of samples each sample belongs to.
253 * 4) the chunk_offset array contains the file offset of each chunk.
255 * So find the sample number we are going to seek to (using time_to_sample)
256 * and then find the offset in the file (using sample_to_chunk,
257 * chunk_offset sample_byte_size, in that order.).
260 unsigned int alac_seek(demux_res_t* demux_res, stream_t* stream,
261 uint32_t sound_sample_loc, uint32_t* sound_samples_done,
262 int* current_sample)
264 uint32_t i;
265 uint32_t j;
266 uint32_t new_sample;
267 uint32_t new_sound_sample;
268 uint32_t new_pos;
270 /* First check we have the appropriate metadata - we should always
271 * have it.
274 if ((demux_res->num_time_to_samples==0) ||
275 (demux_res->num_sample_byte_sizes==0))
277 return 0;
280 /* Find the destination block from time_to_sample array */
282 i = 0;
283 new_sample = 0;
284 new_sound_sample = 0;
286 while ((i < demux_res->num_time_to_samples) &&
287 (new_sound_sample < sound_sample_loc))
289 j = (sound_sample_loc - new_sound_sample) /
290 demux_res->time_to_sample[i].sample_duration;
292 if (j <= demux_res->time_to_sample[i].sample_count)
294 new_sample += j;
295 new_sound_sample += j *
296 demux_res->time_to_sample[i].sample_duration;
297 break;
299 else
301 new_sound_sample += (demux_res->time_to_sample[i].sample_duration
302 * demux_res->time_to_sample[i].sample_count);
303 new_sample += demux_res->time_to_sample[i].sample_count;
304 i++;
308 /* We know the new block, now calculate the file position. */
310 new_pos = get_sample_offset(demux_res, new_sample);
312 /* We know the new file position, so let's try to seek to it */
314 if (stream->ci->seek_buffer(new_pos))
316 *sound_samples_done = new_sound_sample;
317 *current_sample = new_sample;
318 return 1;
321 return 0;
324 /* Seek to the sample containing file_loc. Return 1 on success (and modify
325 * sound_samples_done and current_sample), 0 if failed.
327 * Seeking uses the following arrays:
329 * 1) the chunk_offset array contains the file offset of each chunk.
331 * 2) the sample_to_chunk array contains information about which chunk
332 * of samples each sample belongs to.
334 * 3) the sample_byte_size array contains the length in bytes of each
335 * sample.
337 * 4) the time_to_sample array contains the duration (in sound samples)
338 * of each sample of data.
340 * Locate the chunk containing location (using chunk_offset), find the
341 * sample of that chunk (using sample_to_chunk) and finally the location
342 * of that sample (using sample_byte_size). Then use time_to_sample to
343 * calculate the sound_samples_done value.
345 unsigned int alac_seek_raw(demux_res_t* demux_res, stream_t* stream,
346 uint32_t file_loc, uint32_t* sound_samples_done,
347 int* current_sample)
349 uint32_t chunk_sample = 0;
350 uint32_t total_samples = 0;
351 uint32_t new_sound_sample = 0;
352 uint32_t new_pos;
353 uint32_t chunk;
354 uint32_t i;
356 if (!demux_res->num_chunk_offsets ||
357 !demux_res->num_sample_to_chunks)
359 return 0;
362 /* Locate the chunk containing file_loc. */
364 for (i = 0; i < demux_res->num_chunk_offsets &&
365 file_loc < demux_res->chunk_offset[i]; i++)
369 chunk = i + 1;
370 new_pos = demux_res->chunk_offset[chunk - 1];
372 /* Get the first sample of the chunk. */
374 for (i = 1; i < demux_res->num_sample_to_chunks &&
375 chunk < demux_res->sample_to_chunk[i - 1].first_chunk; i++)
377 chunk_sample += demux_res->sample_to_chunk[i - 1].num_samples *
378 (demux_res->sample_to_chunk[i].first_chunk -
379 demux_res->sample_to_chunk[i - 1].first_chunk);
382 chunk_sample += (chunk - demux_res->sample_to_chunk[i - 1].first_chunk) *
383 demux_res->sample_to_chunk[i - 1].num_samples;
385 /* Get the position within the chunk. */
387 for (; chunk_sample < demux_res->num_sample_byte_sizes; chunk_sample++)
389 if (file_loc < new_pos + demux_res->sample_byte_size[chunk_sample])
391 break;
394 new_pos += demux_res->sample_byte_size[chunk_sample];
397 /* Get sound sample offset. */
399 for (i = 0; i < demux_res->num_time_to_samples; i++)
401 if (chunk_sample <
402 total_samples + demux_res->time_to_sample[i].sample_count)
404 break;
407 total_samples += demux_res->time_to_sample[i].sample_count;
408 new_sound_sample += demux_res->time_to_sample[i].sample_count
409 * demux_res->time_to_sample[i].sample_duration;
412 new_sound_sample += (chunk_sample - total_samples)
413 * demux_res->time_to_sample[i].sample_duration;
415 /* Go to the new file position. */
417 if (stream->ci->seek_buffer(new_pos))
419 *sound_samples_done = new_sound_sample;
420 *current_sample = chunk_sample;
421 return 1;
424 return 0;