1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
Index: apps/codecs/libm4a/m4a.c
===================================================================
--- apps/codecs/libm4a/m4a.c  (revision 29175)
+++ apps/codecs/libm4a/m4a.c  (working copy)
@@ -267,57 +267,44 @@
     uint32_t sound_sample_loc, uint32_t* sound_samples_done, 
     int* current_sample)
 {
-    uint32_t i;
-    uint32_t j;
-    uint32_t new_sample;
-    uint32_t new_sound_sample;
-    uint32_t new_pos;
+    uint32_t i = 0;
+    uint32_t tmp_var, tmp_cnt, tmp_dur;
+    uint32_t new_sample = 0;       /* Holds the amount of chunks/frames. */
+    uint32_t new_sound_sample = 0; /* Sums up total amount of samples. */
+    uint32_t new_pos;              /* Holds the desired chunk/frame index. */

     /* First check we have the appropriate metadata - we should always
      * have it.
      */
-       
-    if ((demux_res->num_time_to_samples==0) ||
-       (demux_res->num_sample_byte_sizes==0))
+    if (!demux_res->num_time_to_samples || !demux_res->num_sample_byte_sizes)
     { 
         return 0; 
     }

     /* Find the destination block from time_to_sample array */
-    
-    i = 0;
-    new_sample = 0;
-    new_sound_sample = 0;
-
-    while ((i < demux_res->num_time_to_samples) &&
-        (new_sound_sample < sound_sample_loc)) 
+    time_to_sample_t *tab = demux_res->time_to_sample;
+    while (i < demux_res->num_time_to_samples)
     {
-        j = (sound_sample_loc - new_sound_sample) /
-            demux_res->time_to_sample[i].sample_duration;
-    
-        if (j <= demux_res->time_to_sample[i].sample_count)
+        tmp_cnt = tab[i].sample_count;
+        tmp_dur = tab[i].sample_duration;
+        tmp_var = tmp_cnt * tmp_dur;
+        if (sound_sample_loc <= new_sound_sample + tmp_var)
         {
-            new_sample += j;
-            new_sound_sample += j * 
-                demux_res->time_to_sample[i].sample_duration;
+            tmp_var = (sound_sample_loc - new_sound_sample);
+            new_sample       += tmp_var / tmp_dur;
+            new_sound_sample += tmp_var;
             break;
-        } 
-        else 
-        {
-            new_sound_sample += (demux_res->time_to_sample[i].sample_duration
-                * demux_res->time_to_sample[i].sample_count);
-            new_sample += demux_res->time_to_sample[i].sample_count;
-            i++;
         }
+        new_sample       += tmp_cnt;
+        new_sound_sample += tmp_var;
+        ++i;
     }

     /* We know the new block, now calculate the file position. */
-  
     new_pos = get_sample_offset(demux_res, new_sample);

     /* We know the new file position, so let's try to seek to it */
-  
-    if (stream->ci->seek_buffer(new_pos)) 
+    if (stream->ci->seek_buffer(new_pos))
     {
         *sound_samples_done = new_sound_sample;
         *current_sample = new_sample;
@@ -352,74 +339,69 @@
     uint32_t file_loc, uint32_t* sound_samples_done, 
     int* current_sample)
 {
-    uint32_t chunk_sample = 0;
-    uint32_t total_samples = 0;
-    uint32_t new_sound_sample = 0;
+    uint32_t chunk_sample = 0;     /* Holds the chunk/frame index. */
+    uint32_t total_samples = 0;    /* Sums up total amount of chunks/frames. */
+    uint32_t new_sound_sample = 0; /* Sums up total amount of samples. */
     uint32_t new_pos;
     uint32_t chunk;
-    uint32_t i;
+    uint32_t i, tmp_dur, tmp_cnt;

-    if (!demux_res->num_chunk_offsets ||
-        !demux_res->num_sample_to_chunks) 
+    /* There is no metadata available to perform raw seek. */
+    if (!demux_res->num_chunk_offsets || !demux_res->num_sample_to_chunks) 
     {
         return 0;
     }

     /* Locate the chunk containing file_loc. */
-
-    for (i = 0; i < demux_res->num_chunk_offsets && 
-        file_loc > demux_res->chunk_offset[i]; i++)
+    chunk = 0;
+    while (chunk < demux_res->num_chunk_offsets)
     {
+        if (file_loc < demux_res->chunk_offset[chunk++])
+        {
+            break;
+        }
     }
-    
-    chunk = i + 1;
-    new_pos = demux_res->chunk_offset[chunk - 1];
+    new_pos = demux_res->chunk_offset[chunk-1];

     /* Get the first sample of the chunk. */
-    
-    for (i = 1; i < demux_res->num_sample_to_chunks &&
-        chunk > demux_res->sample_to_chunk[i - 1].first_chunk; i++) 
+    i = 1;
+    sample_to_chunk_t *tab1 = demux_res->sample_to_chunk;
+    while (i < demux_res->num_sample_to_chunks)
     {
-        chunk_sample += demux_res->sample_to_chunk[i - 1].num_samples *
-            (demux_res->sample_to_chunk[i].first_chunk - 
-                demux_res->sample_to_chunk[i - 1].first_chunk);
+        if (chunk <= tab1[i].first_chunk)
+        {
+            break;
+        }
+        chunk_sample += tab1[i-1].num_samples * (tab1[i].first_chunk - tab1[i-1].first_chunk);
+        ++i;
     }
+    chunk_sample += (chunk - tab1[i-1].first_chunk) * tab1[i-1].num_samples;

-    chunk_sample += (chunk - demux_res->sample_to_chunk[i - 1].first_chunk) *
-        demux_res->sample_to_chunk[i - 1].num_samples;
-
     /* Get the position within the chunk. */
-    
-    for (; chunk_sample < demux_res->num_sample_byte_sizes; chunk_sample++)
+    while (chunk_sample < demux_res->num_sample_byte_sizes &&
+           file_loc >= new_pos + demux_res->sample_byte_size[chunk_sample])
     {
-        if (file_loc < new_pos + demux_res->sample_byte_size[chunk_sample])
-        {
-            break;
-        }
-        
-        new_pos += demux_res->sample_byte_size[chunk_sample];
+        new_pos += demux_res->sample_byte_size[chunk_sample++];
     }

     /* Get sound sample offset. */
-
-    for (i = 0; i < demux_res->num_time_to_samples; i++)
+    i = 0;
+    time_to_sample_t *tab2 = demux_res->time_to_sample;
+    while (i < demux_res->num_time_to_samples)
     {
-        if (chunk_sample < 
-            total_samples + demux_res->time_to_sample[i].sample_count)
+        tmp_dur = tab2[i].sample_duration;
+        tmp_cnt = tab2[i].sample_count;
+        total_samples    += tmp_cnt;
+        new_sound_sample += tmp_cnt * tmp_dur;
+        if (chunk_sample <= total_samples)
         {
+            new_sound_sample += (chunk_sample - total_samples) * tmp_dur;
             break;
         }
-        
-        total_samples += demux_res->time_to_sample[i].sample_count;
-        new_sound_sample += demux_res->time_to_sample[i].sample_count 
-            * demux_res->time_to_sample[i].sample_duration;
+        ++i;
     }
-    
-    new_sound_sample += (chunk_sample - total_samples) 
-        * demux_res->time_to_sample[i].sample_duration;

     /* Go to the new file position. */
-    
     if (stream->ci->seek_buffer(new_pos)) 
     {
         *sound_samples_done = new_sound_sample;
Index: apps/codecs/libm4a/m4a.h
===================================================================
--- apps/codecs/libm4a/m4a.h  (revision 29173)
+++ apps/codecs/libm4a/m4a.h  (working copy)
@@ -45,6 +45,18 @@

 typedef uint32_t fourcc_t;

+typedef struct 
+{
+    uint32_t first_chunk;
+    uint32_t num_samples;
+} sample_to_chunk_t;
+
+typedef struct 
+{
+    uint32_t sample_count;
+    uint32_t sample_duration;
+} time_to_sample_t;
+
 typedef struct
 {
     uint16_t num_channels;
@@ -53,19 +65,13 @@
     fourcc_t format;
     void *buf;

-    struct {
-        uint32_t first_chunk;
-        uint32_t num_samples;
-    } *sample_to_chunk;
+    sample_to_chunk_t *sample_to_chunk;
     uint32_t num_sample_to_chunks;

     uint32_t *chunk_offset;
     uint32_t num_chunk_offsets;

-    struct {
-        uint32_t sample_count;
-        uint32_t sample_duration;
-    } *time_to_sample;
+    time_to_sample_t *time_to_sample;
     uint32_t num_time_to_samples;

     uint16_t *sample_byte_size;