Remove unecessary casting
[maemo-rb.git] / apps / codecs / libm4a / m4a.c
blob5fe778ac033c689f7496e72f806194e1484a5aff
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 uint16_t stream_read_uint16(stream_t *stream)
72 uint16_t v;
73 stream_read(stream, 2, &v);
74 #ifdef ROCKBOX_LITTLE_ENDIAN
75 _Swap16(v);
76 #endif
77 return v;
80 uint8_t stream_read_uint8(stream_t *stream)
82 uint8_t v;
83 stream_read(stream, 1, &v);
84 return v;
87 void stream_skip(stream_t *stream, size_t skip)
89 stream->ci->advance_buffer(skip);
92 void stream_seek(stream_t *stream, size_t offset)
94 stream->ci->seek_buffer(offset);
97 int stream_eof(stream_t *stream)
99 return stream->eof;
102 void stream_create(stream_t *stream,struct codec_api* ci)
104 stream->ci=ci;
105 stream->eof=0;
108 /* Check if there is a dedicated byte position contained for the given frame.
109 * Return this byte position in case of success or return -1. This allows to
110 * skip empty samples.
111 * During standard playback the search result (index i) will always increase.
112 * Therefor we save this index and let the caller set this value again as start
113 * index when calling m4a_check_sample_offset() for the next frame. This
114 * reduces the overall loop count significantly. */
115 int m4a_check_sample_offset(demux_res_t *demux_res, uint32_t frame, uint32_t *start)
117 uint32_t i = *start;
118 for (i=0; i<demux_res->num_lookup_table; ++i)
120 if (demux_res->lookup_table[i].sample > frame ||
121 demux_res->lookup_table[i].offset == 0)
122 return -1;
123 if (demux_res->lookup_table[i].sample == frame)
124 break;
126 *start = i;
127 return demux_res->lookup_table[i].offset;
130 /* Find the exact or preceding frame in lookup_table[]. Return both frame
131 * and byte position of this match. */
132 static void gather_offset(demux_res_t *demux_res, uint32_t *frame, uint32_t *offset)
134 uint32_t i = 0;
135 for (i=0; i<demux_res->num_lookup_table; ++i)
137 if (demux_res->lookup_table[i].offset == 0)
138 break;
139 if (demux_res->lookup_table[i].sample > *frame)
140 break;
142 i = (i>0) ? i-1 : 0; /* We want the last chunk _before_ *frame. */
143 *frame = demux_res->lookup_table[i].sample;
144 *offset = demux_res->lookup_table[i].offset;
147 /* Seek to desired sound sample location. Return 1 on success (and modify
148 * sound_samples_done and current_sample), 0 if failed.
150 * Find the sample (=frame) that contains the given sound sample, find a best
151 * fit for this sample in the lookup_table[], seek to the byte position. */
152 unsigned int m4a_seek(demux_res_t* demux_res, stream_t* stream,
153 uint32_t sound_sample_loc, uint32_t* sound_samples_done,
154 int* current_sample)
156 uint32_t i = 0;
157 uint32_t tmp_var, tmp_cnt, tmp_dur;
158 uint32_t new_sample = 0; /* Holds the amount of chunks/frames. */
159 uint32_t new_sound_sample = 0; /* Sums up total amount of samples. */
160 uint32_t new_pos; /* Holds the desired chunk/frame index. */
162 /* First check we have the appropriate metadata - we should always
163 * have it.
165 if (!demux_res->num_time_to_samples || !demux_res->num_sample_byte_sizes)
167 return 0;
170 /* Find the destination block from time_to_sample array */
171 time_to_sample_t *tab = demux_res->time_to_sample;
172 while (i < demux_res->num_time_to_samples)
174 tmp_cnt = tab[i].sample_count;
175 tmp_dur = tab[i].sample_duration;
176 tmp_var = tmp_cnt * tmp_dur;
177 if (sound_sample_loc <= new_sound_sample + tmp_var)
179 tmp_var = (sound_sample_loc - new_sound_sample);
180 new_sample += tmp_var / tmp_dur;
181 new_sound_sample += tmp_var;
182 break;
184 new_sample += tmp_cnt;
185 new_sound_sample += tmp_var;
186 ++i;
189 /* We know the new sample (=frame), now calculate the file position. */
190 gather_offset(demux_res, &new_sample, &new_pos);
192 /* We know the new file position, so let's try to seek to it */
193 if (stream->ci->seek_buffer(new_pos))
195 *sound_samples_done = new_sound_sample;
196 *current_sample = new_sample;
197 return 1;
200 return 0;
203 /* Seek to the sample containing file_loc. Return 1 on success (and modify
204 * sound_samples_done and current_sample), 0 if failed.
206 * Seeking uses the following arrays:
208 * 1) the lookup_table array contains the file offset for the first sample
209 * of each chunk.
211 * 2) the time_to_sample array contains the duration (in sound samples)
212 * of each sample of data.
214 * Locate the chunk containing location (using lookup_table), find the first
215 * sample of that chunk (using lookup_table). Then use time_to_sample to
216 * calculate the sound_samples_done value.
218 unsigned int m4a_seek_raw(demux_res_t* demux_res, stream_t* stream,
219 uint32_t file_loc, uint32_t* sound_samples_done,
220 int* current_sample)
222 uint32_t i;
223 uint32_t chunk_sample = 0;
224 uint32_t total_samples = 0;
225 uint32_t new_sound_sample = 0;
226 uint32_t tmp_dur;
227 uint32_t tmp_cnt;
228 uint32_t new_pos;
230 /* We know the desired byte offset, search for the chunk right before.
231 * Return the associated sample to this chunk as chunk_sample. */
232 for (i=0; i < demux_res->num_lookup_table; ++i)
234 if (demux_res->lookup_table[i].offset > file_loc)
235 break;
237 i = (i>0) ? i-1 : 0; /* We want the last chunk _before_ file_loc. */
238 chunk_sample = demux_res->lookup_table[i].sample;
239 new_pos = demux_res->lookup_table[i].offset;
241 /* Get sound sample offset. */
242 i = 0;
243 time_to_sample_t *tab2 = demux_res->time_to_sample;
244 while (i < demux_res->num_time_to_samples)
246 tmp_dur = tab2[i].sample_duration;
247 tmp_cnt = tab2[i].sample_count;
248 total_samples += tmp_cnt;
249 new_sound_sample += tmp_cnt * tmp_dur;
250 if (chunk_sample <= total_samples)
252 new_sound_sample += (chunk_sample - total_samples) * tmp_dur;
253 break;
255 ++i;
258 /* Go to the new file position. */
259 if (stream->ci->seek_buffer(new_pos))
261 *sound_samples_done = new_sound_sample;
262 *current_sample = chunk_sample;
263 return 1;
266 return 0;