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 void stream_seek(stream_t
*stream
, size_t offset
)
111 stream
->ci
->seek_buffer(offset
);
114 int stream_eof(stream_t
*stream
)
119 void stream_create(stream_t
*stream
,struct codec_api
* ci
)
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
) {
138 if (!demux_res
->num_time_to_samples
) {
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
) {
154 demux_res
->time_to_sample
[duration_cur_index
].sample_duration
;
155 *sample_byte_size
= demux_res
->sample_byte_size
[samplenum
];
160 unsigned int get_sample_offset(demux_res_t
*demux_res
, uint32_t sample
)
163 uint32_t range_samples
= 0;
164 uint32_t total_samples
= 0;
165 uint32_t chunk_sample
;
167 uint32_t prev_chunk_samples
;
168 uint32_t file_offset
;
171 /* First check we have the appropriate metadata - we should always
175 if (sample
>= demux_res
->num_sample_byte_sizes
||
176 !demux_res
->num_sample_to_chunks
||
177 !demux_res
->num_chunk_offsets
)
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
)
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
;
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];
224 file_offset
= demux_res
->chunk_offset
[chunk
- 1];
227 if (chunk_sample
> sample
)
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
)
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
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
,
273 uint32_t new_sound_sample
;
276 /* First check we have the appropriate metadata - we should always
280 if ((demux_res
->num_time_to_samples
==0) ||
281 (demux_res
->num_sample_byte_sizes
==0))
286 /* Find the destination block from time_to_sample array */
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
)
301 new_sound_sample
+= j
*
302 demux_res
->time_to_sample
[i
].sample_duration
;
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
;
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
;
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
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
,
355 uint32_t chunk_sample
= 0;
356 uint32_t total_samples
= 0;
357 uint32_t new_sound_sample
= 0;
362 if (!demux_res
->num_chunk_offsets
||
363 !demux_res
->num_sample_to_chunks
)
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
++)
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
])
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
++)
408 total_samples
+ demux_res
->time_to_sample
[i
].sample_count
)
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
;