Commit first part of FS#10832 by Juliusz Chroboczek. Allows playback of unstreamable...
[kugel-rb.git] / apps / codecs / libm4a / m4a.c
blob42295e76eec67220aa3ef61aa42f352ea75a6192
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 void stream_seek(stream_t *stream, size_t offset)
111 stream->ci->seek_buffer(offset);
114 int stream_eof(stream_t *stream)
116 return stream->eof;
119 void stream_create(stream_t *stream,struct codec_api* ci)
121 stream->ci=ci;
122 stream->eof=0;
125 /* This function was part of the original alac decoder implementation */
127 int get_sample_info(demux_res_t *demux_res, uint32_t samplenum,
128 uint32_t *sample_duration,
129 uint32_t *sample_byte_size)
131 unsigned int duration_index_accum = 0;
132 unsigned int duration_cur_index = 0;
134 if (samplenum >= demux_res->num_sample_byte_sizes) {
135 return 0;
138 if (!demux_res->num_time_to_samples) {
139 return 0;
142 while ((demux_res->time_to_sample[duration_cur_index].sample_count
143 + duration_index_accum) <= samplenum) {
144 duration_index_accum +=
145 demux_res->time_to_sample[duration_cur_index].sample_count;
147 duration_cur_index++;
148 if (duration_cur_index >= demux_res->num_time_to_samples) {
149 return 0;
153 *sample_duration =
154 demux_res->time_to_sample[duration_cur_index].sample_duration;
155 *sample_byte_size = demux_res->sample_byte_size[samplenum];
157 return 1;
160 unsigned int get_sample_offset(demux_res_t *demux_res, uint32_t sample)
162 uint32_t chunk = 1;
163 uint32_t range_samples = 0;
164 uint32_t total_samples = 0;
165 uint32_t chunk_sample;
166 uint32_t prev_chunk;
167 uint32_t prev_chunk_samples;
168 uint32_t file_offset;
169 uint32_t i;
171 /* First check we have the appropriate metadata - we should always
172 * have it.
175 if (sample >= demux_res->num_sample_byte_sizes ||
176 !demux_res->num_sample_to_chunks ||
177 !demux_res->num_chunk_offsets)
179 return 0;
182 /* Locate the chunk containing the sample */
184 prev_chunk = demux_res->sample_to_chunk[0].first_chunk;
185 prev_chunk_samples = demux_res->sample_to_chunk[0].num_samples;
187 for (i = 1; i < demux_res->num_sample_to_chunks; i++)
189 chunk = demux_res->sample_to_chunk[i].first_chunk;
190 range_samples = (chunk - prev_chunk) * prev_chunk_samples;
192 if (sample < total_samples + range_samples)
194 break;
197 total_samples += range_samples;
198 prev_chunk = demux_res->sample_to_chunk[i].first_chunk;
199 prev_chunk_samples = demux_res->sample_to_chunk[i].num_samples;
202 if (prev_chunk_samples > 0 &&
203 sample >= demux_res->sample_to_chunk[0].num_samples)
205 chunk = prev_chunk + (sample - total_samples) / prev_chunk_samples;
207 else
209 chunk = 1;
212 /* Get sample of the first sample in the chunk */
214 chunk_sample = total_samples + (chunk - prev_chunk) * prev_chunk_samples;
216 /* Get offset in file */
218 if (chunk > demux_res->num_chunk_offsets)
220 file_offset = demux_res->chunk_offset[demux_res->num_chunk_offsets - 1];
222 else
224 file_offset = demux_res->chunk_offset[chunk - 1];
227 if (chunk_sample > sample)
229 return 0;
232 for (i = chunk_sample; i < sample; i++)
234 file_offset += demux_res->sample_byte_size[i];
237 if (file_offset > demux_res->mdat_offset + demux_res->mdat_len)
239 return 0;
242 return file_offset;
245 /* Seek to the sample containing sound_sample_loc. Return 1 on success
246 * (and modify sound_samples_done and current_sample), 0 if failed.
248 * Seeking uses the following arrays:
250 * 1) the time_to_sample array contains the duration (in sound samples)
251 * of each sample of data.
253 * 2) the sample_byte_size array contains the length in bytes of each
254 * sample.
256 * 3) the sample_to_chunk array contains information about which chunk
257 * of samples each sample belongs to.
259 * 4) the chunk_offset array contains the file offset of each chunk.
261 * So find the sample number we are going to seek to (using time_to_sample)
262 * and then find the offset in the file (using sample_to_chunk,
263 * chunk_offset sample_byte_size, in that order.).
266 unsigned int alac_seek(demux_res_t* demux_res, stream_t* stream,
267 uint32_t sound_sample_loc, uint32_t* sound_samples_done,
268 int* current_sample)
270 uint32_t i;
271 uint32_t j;
272 uint32_t new_sample;
273 uint32_t new_sound_sample;
274 uint32_t new_pos;
276 /* First check we have the appropriate metadata - we should always
277 * have it.
280 if ((demux_res->num_time_to_samples==0) ||
281 (demux_res->num_sample_byte_sizes==0))
283 return 0;
286 /* Find the destination block from time_to_sample array */
288 i = 0;
289 new_sample = 0;
290 new_sound_sample = 0;
292 while ((i < demux_res->num_time_to_samples) &&
293 (new_sound_sample < sound_sample_loc))
295 j = (sound_sample_loc - new_sound_sample) /
296 demux_res->time_to_sample[i].sample_duration;
298 if (j <= demux_res->time_to_sample[i].sample_count)
300 new_sample += j;
301 new_sound_sample += j *
302 demux_res->time_to_sample[i].sample_duration;
303 break;
305 else
307 new_sound_sample += (demux_res->time_to_sample[i].sample_duration
308 * demux_res->time_to_sample[i].sample_count);
309 new_sample += demux_res->time_to_sample[i].sample_count;
310 i++;
314 /* We know the new block, now calculate the file position. */
316 new_pos = get_sample_offset(demux_res, new_sample);
318 /* We know the new file position, so let's try to seek to it */
320 if (stream->ci->seek_buffer(new_pos))
322 *sound_samples_done = new_sound_sample;
323 *current_sample = new_sample;
324 return 1;
327 return 0;
330 /* Seek to the sample containing file_loc. Return 1 on success (and modify
331 * sound_samples_done and current_sample), 0 if failed.
333 * Seeking uses the following arrays:
335 * 1) the chunk_offset array contains the file offset of each chunk.
337 * 2) the sample_to_chunk array contains information about which chunk
338 * of samples each sample belongs to.
340 * 3) the sample_byte_size array contains the length in bytes of each
341 * sample.
343 * 4) the time_to_sample array contains the duration (in sound samples)
344 * of each sample of data.
346 * Locate the chunk containing location (using chunk_offset), find the
347 * sample of that chunk (using sample_to_chunk) and finally the location
348 * of that sample (using sample_byte_size). Then use time_to_sample to
349 * calculate the sound_samples_done value.
351 unsigned int alac_seek_raw(demux_res_t* demux_res, stream_t* stream,
352 uint32_t file_loc, uint32_t* sound_samples_done,
353 int* current_sample)
355 uint32_t chunk_sample = 0;
356 uint32_t total_samples = 0;
357 uint32_t new_sound_sample = 0;
358 uint32_t new_pos;
359 uint32_t chunk;
360 uint32_t i;
362 if (!demux_res->num_chunk_offsets ||
363 !demux_res->num_sample_to_chunks)
365 return 0;
368 /* Locate the chunk containing file_loc. */
370 for (i = 0; i < demux_res->num_chunk_offsets &&
371 file_loc < demux_res->chunk_offset[i]; i++)
375 chunk = i + 1;
376 new_pos = demux_res->chunk_offset[chunk - 1];
378 /* Get the first sample of the chunk. */
380 for (i = 1; i < demux_res->num_sample_to_chunks &&
381 chunk < demux_res->sample_to_chunk[i - 1].first_chunk; i++)
383 chunk_sample += demux_res->sample_to_chunk[i - 1].num_samples *
384 (demux_res->sample_to_chunk[i].first_chunk -
385 demux_res->sample_to_chunk[i - 1].first_chunk);
388 chunk_sample += (chunk - demux_res->sample_to_chunk[i - 1].first_chunk) *
389 demux_res->sample_to_chunk[i - 1].num_samples;
391 /* Get the position within the chunk. */
393 for (; chunk_sample < demux_res->num_sample_byte_sizes; chunk_sample++)
395 if (file_loc < new_pos + demux_res->sample_byte_size[chunk_sample])
397 break;
400 new_pos += demux_res->sample_byte_size[chunk_sample];
403 /* Get sound sample offset. */
405 for (i = 0; i < demux_res->num_time_to_samples; i++)
407 if (chunk_sample <
408 total_samples + demux_res->time_to_sample[i].sample_count)
410 break;
413 total_samples += demux_res->time_to_sample[i].sample_count;
414 new_sound_sample += demux_res->time_to_sample[i].sample_count
415 * demux_res->time_to_sample[i].sample_duration;
418 new_sound_sample += (chunk_sample - total_samples)
419 * demux_res->time_to_sample[i].sample_duration;
421 /* Go to the new file position. */
423 if (stream->ci->seek_buffer(new_pos))
425 *sound_samples_done = new_sound_sample;
426 *current_sample = chunk_sample;
427 return 1;
430 return 0;