summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libm4a/m4a.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libm4a/m4a.c')
-rw-r--r--lib/rbcodec/codecs/libm4a/m4a.c267
1 files changed, 267 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libm4a/m4a.c b/lib/rbcodec/codecs/libm4a/m4a.c
new file mode 100644
index 0000000000..5fe778ac03
--- /dev/null
+++ b/lib/rbcodec/codecs/libm4a/m4a.c
@@ -0,0 +1,267 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Dave Chapman, 2011 Andree Buschmann
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include <codecs.h>
23#include <inttypes.h>
24#include "m4a.h"
25
26/* Implementation of the stream.h functions used by libalac */
27
28#define _Swap32(v) do { \
29 v = (((v) & 0x000000FF) << 0x18) | \
30 (((v) & 0x0000FF00) << 0x08) | \
31 (((v) & 0x00FF0000) >> 0x08) | \
32 (((v) & 0xFF000000) >> 0x18); } while(0)
33
34#define _Swap16(v) do { \
35 v = (((v) & 0x00FF) << 0x08) | \
36 (((v) & 0xFF00) >> 0x08); } while (0)
37
38/* A normal read without any byte-swapping */
39void stream_read(stream_t *stream, size_t size, void *buf)
40{
41 stream->ci->read_filebuf(buf,size);
42 if (stream->ci->curpos >= stream->ci->filesize) { stream->eof=1; }
43}
44
45int32_t stream_read_int32(stream_t *stream)
46{
47 int32_t v;
48 stream_read(stream, 4, &v);
49#ifdef ROCKBOX_LITTLE_ENDIAN
50 _Swap32(v);
51#endif
52 return v;
53}
54
55int32_t stream_tell(stream_t *stream)
56{
57 return stream->ci->curpos;
58}
59
60uint32_t stream_read_uint32(stream_t *stream)
61{
62 uint32_t v;
63 stream_read(stream, 4, &v);
64#ifdef ROCKBOX_LITTLE_ENDIAN
65 _Swap32(v);
66#endif
67 return v;
68}
69
70uint16_t stream_read_uint16(stream_t *stream)
71{
72 uint16_t v;
73 stream_read(stream, 2, &v);
74#ifdef ROCKBOX_LITTLE_ENDIAN
75 _Swap16(v);
76#endif
77 return v;
78}
79
80uint8_t stream_read_uint8(stream_t *stream)
81{
82 uint8_t v;
83 stream_read(stream, 1, &v);
84 return v;
85}
86
87void stream_skip(stream_t *stream, size_t skip)
88{
89 stream->ci->advance_buffer(skip);
90}
91
92void stream_seek(stream_t *stream, size_t offset)
93{
94 stream->ci->seek_buffer(offset);
95}
96
97int stream_eof(stream_t *stream)
98{
99 return stream->eof;
100}
101
102void stream_create(stream_t *stream,struct codec_api* ci)
103{
104 stream->ci=ci;
105 stream->eof=0;
106}
107
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. */
115int m4a_check_sample_offset(demux_res_t *demux_res, uint32_t frame, uint32_t *start)
116{
117 uint32_t i = *start;
118 for (i=0; i<demux_res->num_lookup_table; ++i)
119 {
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;
125 }
126 *start = i;
127 return demux_res->lookup_table[i].offset;
128}
129
130/* Find the exact or preceding frame in lookup_table[]. Return both frame
131 * and byte position of this match. */
132static void gather_offset(demux_res_t *demux_res, uint32_t *frame, uint32_t *offset)
133{
134 uint32_t i = 0;
135 for (i=0; i<demux_res->num_lookup_table; ++i)
136 {
137 if (demux_res->lookup_table[i].offset == 0)
138 break;
139 if (demux_res->lookup_table[i].sample > *frame)
140 break;
141 }
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;
145}
146
147/* Seek to desired sound sample location. Return 1 on success (and modify
148 * sound_samples_done and current_sample), 0 if failed.
149 *
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. */
152unsigned 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)
155{
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. */
161
162 /* First check we have the appropriate metadata - we should always
163 * have it.
164 */
165 if (!demux_res->num_time_to_samples || !demux_res->num_sample_byte_sizes)
166 {
167 return 0;
168 }
169
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)
173 {
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)
178 {
179 tmp_var = (sound_sample_loc - new_sound_sample);
180 new_sample += tmp_var / tmp_dur;
181 new_sound_sample += tmp_var;
182 break;
183 }
184 new_sample += tmp_cnt;
185 new_sound_sample += tmp_var;
186 ++i;
187 }
188
189 /* We know the new sample (=frame), now calculate the file position. */
190 gather_offset(demux_res, &new_sample, &new_pos);
191
192 /* We know the new file position, so let's try to seek to it */
193 if (stream->ci->seek_buffer(new_pos))
194 {
195 *sound_samples_done = new_sound_sample;
196 *current_sample = new_sample;
197 return 1;
198 }
199
200 return 0;
201}
202
203/* Seek to the sample containing file_loc. Return 1 on success (and modify
204 * sound_samples_done and current_sample), 0 if failed.
205 *
206 * Seeking uses the following arrays:
207 *
208 * 1) the lookup_table array contains the file offset for the first sample
209 * of each chunk.
210 *
211 * 2) the time_to_sample array contains the duration (in sound samples)
212 * of each sample of data.
213 *
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.
217 */
218unsigned 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)
221{
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;
229
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)
233 {
234 if (demux_res->lookup_table[i].offset > file_loc)
235 break;
236 }
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;
240
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)
245 {
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)
251 {
252 new_sound_sample += (chunk_sample - total_samples) * tmp_dur;
253 break;
254 }
255 ++i;
256 }
257
258 /* Go to the new file position. */
259 if (stream->ci->seek_buffer(new_pos))
260 {
261 *sound_samples_done = new_sound_sample;
262 *current_sample = chunk_sample;
263 return 1;
264 }
265
266 return 0;
267}