Rework m4a seek/resume code. Seek/resume does now also work properly with files havin...
[kugel-rb.git] / apps / codecs / libm4a / m4a.c
blob0825132b8531a1b45aa87b2fb52553166cfdfc2f
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 = 0;
271 uint32_t tmp_var, tmp_cnt, tmp_dur;
272 uint32_t new_sample = 0; /* Holds the amount of chunks/frames. */
273 uint32_t new_sound_sample = 0; /* Sums up total amount of samples. */
274 uint32_t new_pos; /* Holds the desired chunk/frame index. */
276 /* First check we have the appropriate metadata - we should always
277 * have it.
279 if (!demux_res->num_time_to_samples || !demux_res->num_sample_byte_sizes)
281 return 0;
284 /* Find the destination block from time_to_sample array */
285 time_to_sample_t *tab = demux_res->time_to_sample;
286 while (i < demux_res->num_time_to_samples)
288 tmp_cnt = tab[i].sample_count;
289 tmp_dur = tab[i].sample_duration;
290 tmp_var = tmp_cnt * tmp_dur;
291 if (sound_sample_loc <= new_sound_sample + tmp_var)
293 tmp_var = (sound_sample_loc - new_sound_sample);
294 new_sample += tmp_var / tmp_dur;
295 new_sound_sample += tmp_var;
296 break;
298 new_sample += tmp_cnt;
299 new_sound_sample += tmp_var;
300 ++i;
303 /* We know the new block, now calculate the file position. */
304 new_pos = get_sample_offset(demux_res, new_sample);
306 /* We know the new file position, so let's try to seek to it */
307 if (stream->ci->seek_buffer(new_pos))
309 *sound_samples_done = new_sound_sample;
310 *current_sample = new_sample;
311 return 1;
314 return 0;
317 /* Seek to the sample containing file_loc. Return 1 on success (and modify
318 * sound_samples_done and current_sample), 0 if failed.
320 * Seeking uses the following arrays:
322 * 1) the chunk_offset array contains the file offset of each chunk.
324 * 2) the sample_to_chunk array contains information about which chunk
325 * of samples each sample belongs to.
327 * 3) the sample_byte_size array contains the length in bytes of each
328 * sample.
330 * 4) the time_to_sample array contains the duration (in sound samples)
331 * of each sample of data.
333 * Locate the chunk containing location (using chunk_offset), find the
334 * sample of that chunk (using sample_to_chunk) and finally the location
335 * of that sample (using sample_byte_size). Then use time_to_sample to
336 * calculate the sound_samples_done value.
338 unsigned int alac_seek_raw(demux_res_t* demux_res, stream_t* stream,
339 uint32_t file_loc, uint32_t* sound_samples_done,
340 int* current_sample)
342 uint32_t chunk_sample = 0; /* Holds the chunk/frame index. */
343 uint32_t total_samples = 0; /* Sums up total amount of chunks/frames. */
344 uint32_t new_sound_sample = 0; /* Sums up total amount of samples. */
345 uint32_t new_pos;
346 uint32_t chunk;
347 uint32_t i, tmp_dur, tmp_cnt;
349 /* There is no metadata available to perform raw seek. */
350 if (!demux_res->num_chunk_offsets || !demux_res->num_sample_to_chunks)
352 return 0;
355 /* Locate the chunk containing file_loc. */
356 chunk = 0;
357 while (chunk < demux_res->num_chunk_offsets)
359 if (file_loc < demux_res->chunk_offset[chunk++])
361 break;
364 new_pos = demux_res->chunk_offset[chunk-1];
366 /* Get the first sample of the chunk. */
367 i = 1;
368 sample_to_chunk_t *tab1 = demux_res->sample_to_chunk;
369 while (i < demux_res->num_sample_to_chunks)
371 if (chunk <= tab1[i].first_chunk)
373 break;
375 chunk_sample += tab1[i-1].num_samples * (tab1[i].first_chunk - tab1[i-1].first_chunk);
376 ++i;
378 chunk_sample += (chunk - tab1[i-1].first_chunk) * tab1[i-1].num_samples;
380 /* Get the position within the chunk. */
381 while (chunk_sample < demux_res->num_sample_byte_sizes &&
382 file_loc >= new_pos + demux_res->sample_byte_size[chunk_sample])
384 new_pos += demux_res->sample_byte_size[chunk_sample++];
387 /* Get sound sample offset. */
388 i = 0;
389 time_to_sample_t *tab2 = demux_res->time_to_sample;
390 while (i < demux_res->num_time_to_samples)
392 tmp_dur = tab2[i].sample_duration;
393 tmp_cnt = tab2[i].sample_count;
394 total_samples += tmp_cnt;
395 new_sound_sample += tmp_cnt * tmp_dur;
396 if (chunk_sample <= total_samples)
398 new_sound_sample += (chunk_sample - total_samples) * tmp_dur;
399 break;
401 ++i;
404 /* Go to the new file position. */
405 if (stream->ci->seek_buffer(new_pos))
407 *sound_samples_done = new_sound_sample;
408 *current_sample = chunk_sample;
409 return 1;
412 return 0;