1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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
)
48 stream_read(stream
, 4, &v
);
49 #ifdef ROCKBOX_LITTLE_ENDIAN
55 int32_t stream_tell(stream_t
*stream
)
57 return stream
->ci
->curpos
;
60 uint32_t stream_read_uint32(stream_t
*stream
)
63 stream_read(stream
, 4, &v
);
64 #ifdef ROCKBOX_LITTLE_ENDIAN
70 int16_t stream_read_int16(stream_t
*stream
)
73 stream_read(stream
, 2, &v
);
74 #ifdef ROCKBOX_LITTLE_ENDIAN
80 uint16_t stream_read_uint16(stream_t
*stream
)
83 stream_read(stream
, 2, &v
);
84 #ifdef ROCKBOX_LITTLE_ENDIAN
90 int8_t stream_read_int8(stream_t
*stream
)
93 stream_read(stream
, 1, &v
);
97 uint8_t stream_read_uint8(stream_t
*stream
)
100 stream_read(stream
, 1, &v
);
104 void stream_skip(stream_t
*stream
, size_t skip
)
106 stream
->ci
->advance_buffer(skip
);
109 int stream_eof(stream_t
*stream
)
114 void stream_create(stream_t
*stream
,struct codec_api
* ci
)
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
) {
133 if (!demux_res
->num_time_to_samples
) {
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
) {
149 demux_res
->time_to_sample
[duration_cur_index
].sample_duration
;
150 *sample_byte_size
= demux_res
->sample_byte_size
[samplenum
];
155 unsigned int get_sample_offset(demux_res_t
*demux_res
, uint32_t sample
)
158 uint32_t range_samples
= 0;
159 uint32_t total_samples
= 0;
160 uint32_t chunk_sample
;
162 uint32_t prev_chunk_samples
;
163 uint32_t file_offset
;
166 /* First check we have the appropriate metadata - we should always
170 if (sample
>= demux_res
->num_sample_byte_sizes
||
171 !demux_res
->num_sample_to_chunks
||
172 !demux_res
->num_chunk_offsets
)
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
)
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
;
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];
218 file_offset
= demux_res
->chunk_offset
[chunk
- 1];
221 if (chunk_sample
> sample
)
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
)
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
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
,
267 uint32_t new_sound_sample
;
270 /* First check we have the appropriate metadata - we should always
274 if ((demux_res
->num_time_to_samples
==0) ||
275 (demux_res
->num_sample_byte_sizes
==0))
280 /* Find the destination block from time_to_sample array */
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
)
295 new_sound_sample
+= j
*
296 demux_res
->time_to_sample
[i
].sample_duration
;
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
;
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
;
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
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
,
349 uint32_t chunk_sample
= 0;
350 uint32_t total_samples
= 0;
351 uint32_t new_sound_sample
= 0;
356 if (!demux_res
->num_chunk_offsets
||
357 !demux_res
->num_sample_to_chunks
)
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
++)
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
])
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
++)
402 total_samples
+ demux_res
->time_to_sample
[i
].sample_count
)
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
;