Major rework of the m4a parser for aac/alac playback, seek and resume support. As...
[kugel-rb.git] / apps / codecs / libm4a / m4a.c
blob836cdafda35d9a627117544b7b96e280558c36bc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman, 2011 Andree Buschmann
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 /* Check if there is a dedicated byte position contained for the given frame.
126 * Return this byte position in case of success or return -1. This allows to
127 * skip empty samples. */
128 int m4a_check_sample_offset(demux_res_t *demux_res, uint32_t frame)
130 uint32_t i = 0;
131 for (i=0; i<demux_res->num_lookup_table; ++i)
133 if (demux_res->lookup_table[i].sample > frame ||
134 demux_res->lookup_table[i].offset == 0)
135 return -1;
136 if (demux_res->lookup_table[i].sample == frame)
137 break;
139 return demux_res->lookup_table[i].offset;
142 /* Find the exact or preceding frame in lookup_table[]. Return both frame
143 * and byte position of this match. */
144 static void gather_offset(demux_res_t *demux_res, uint32_t *frame, uint32_t *offset)
146 uint32_t i = 0;
147 for (i=0; i<demux_res->num_lookup_table; ++i)
149 if (demux_res->lookup_table[i].offset == 0)
150 break;
151 if (demux_res->lookup_table[i].sample > *frame)
152 break;
154 i = (i>0) ? i-1 : 0; /* We want the last chunk _before_ *frame. */
155 *frame = demux_res->lookup_table[i].sample;
156 *offset = demux_res->lookup_table[i].offset;
159 /* Seek to desired sound sample location. Return 1 on success (and modify
160 * sound_samples_done and current_sample), 0 if failed.
162 * Find the sample (=frame) that contains the given sound sample, find a best
163 * fit for this sample in the lookup_table[], seek to the byte position. */
164 unsigned int m4a_seek(demux_res_t* demux_res, stream_t* stream,
165 uint32_t sound_sample_loc, uint32_t* sound_samples_done,
166 int* current_sample)
168 uint32_t i = 0;
169 uint32_t tmp_var, tmp_cnt, tmp_dur;
170 uint32_t new_sample = 0; /* Holds the amount of chunks/frames. */
171 uint32_t new_sound_sample = 0; /* Sums up total amount of samples. */
172 uint32_t new_pos; /* Holds the desired chunk/frame index. */
174 /* First check we have the appropriate metadata - we should always
175 * have it.
177 if (!demux_res->num_time_to_samples || !demux_res->num_sample_byte_sizes)
179 return 0;
182 /* Find the destination block from time_to_sample array */
183 time_to_sample_t *tab = demux_res->time_to_sample;
184 while (i < demux_res->num_time_to_samples)
186 tmp_cnt = tab[i].sample_count;
187 tmp_dur = tab[i].sample_duration;
188 tmp_var = tmp_cnt * tmp_dur;
189 if (sound_sample_loc <= new_sound_sample + tmp_var)
191 tmp_var = (sound_sample_loc - new_sound_sample);
192 new_sample += tmp_var / tmp_dur;
193 new_sound_sample += tmp_var;
194 break;
196 new_sample += tmp_cnt;
197 new_sound_sample += tmp_var;
198 ++i;
201 /* We know the new sample (=frame), now calculate the file position. */
202 gather_offset(demux_res, &new_sample, &new_pos);
204 /* We know the new file position, so let's try to seek to it */
205 if (stream->ci->seek_buffer(new_pos))
207 *sound_samples_done = new_sound_sample;
208 *current_sample = new_sample;
209 return 1;
212 return 0;
215 /* Seek to the sample containing file_loc. Return 1 on success (and modify
216 * sound_samples_done and current_sample), 0 if failed.
218 * Seeking uses the following arrays:
220 * 1) the lookup_table array contains the file offset for the first sample
221 * of each chunk.
223 * 2) the time_to_sample array contains the duration (in sound samples)
224 * of each sample of data.
226 * Locate the chunk containing location (using lookup_table), find the first
227 * sample of that chunk (using lookup_table). Then use time_to_sample to
228 * calculate the sound_samples_done value.
230 unsigned int m4a_seek_raw(demux_res_t* demux_res, stream_t* stream,
231 uint32_t file_loc, uint32_t* sound_samples_done,
232 int* current_sample)
234 uint32_t i;
235 uint32_t chunk_sample = 0;
236 uint32_t total_samples = 0;
237 uint32_t new_sound_sample = 0;
238 uint32_t tmp_dur;
239 uint32_t tmp_cnt;
240 uint32_t new_pos;
242 /* We know the desired byte offset, search for the chunk right before.
243 * Return the associated sample to this chunk as chunk_sample. */
244 for (i=0; i < demux_res->num_lookup_table; ++i)
246 if (demux_res->lookup_table[i].offset > file_loc)
247 break;
249 i = (i>0) ? i-1 : 0; /* We want the last chunk _before_ file_loc. */
250 chunk_sample = demux_res->lookup_table[i].sample;
251 new_pos = demux_res->lookup_table[i].offset;
253 /* Get sound sample offset. */
254 i = 0;
255 time_to_sample_t *tab2 = demux_res->time_to_sample;
256 while (i < demux_res->num_time_to_samples)
258 tmp_dur = tab2[i].sample_duration;
259 tmp_cnt = tab2[i].sample_count;
260 total_samples += tmp_cnt;
261 new_sound_sample += tmp_cnt * tmp_dur;
262 if (chunk_sample <= total_samples)
264 new_sound_sample += (chunk_sample - total_samples) * tmp_dur;
265 break;
267 ++i;
270 /* Go to the new file position. */
271 if (stream->ci->seek_buffer(new_pos))
273 *sound_samples_done = new_sound_sample;
274 *current_sample = chunk_sample;
275 return 1;
278 return 0;