2399
|
1 /*
|
|
2 IMA4:1 audio codec from QuickTime4Linux library. (http://www.heroinewarrior.com/)
|
|
3 */
|
|
4
|
|
5 #include "ima4.h"
|
|
6
|
|
7 static int quicktime_ima4_step[89] =
|
|
8 {
|
|
9 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
|
|
10 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
|
|
11 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
|
|
12 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
|
|
13 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
|
|
14 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
|
|
15 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
|
|
16 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
|
|
17 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
|
|
18 };
|
|
19
|
|
20 static int quicktime_ima4_index[16] =
|
|
21 {
|
|
22 -1, -1, -1, -1, 2, 4, 6, 8,
|
|
23 -1, -1, -1, -1, 2, 4, 6, 8
|
|
24 };
|
|
25
|
|
26 /* ================================== private for ima4 */
|
|
27
|
|
28
|
|
29 void ima4_decode_sample(int *predictor, int *nibble, int *index, int *step)
|
|
30 {
|
|
31 int difference, sign;
|
|
32
|
|
33 /* Get new index value */
|
|
34 *index += quicktime_ima4_index[*nibble];
|
|
35
|
|
36 if(*index < 0) *index = 0;
|
|
37 else
|
|
38 if(*index > 88) *index = 88;
|
|
39
|
|
40 /* Get sign and magnitude from *nibble */
|
|
41 sign = *nibble & 8;
|
|
42 *nibble = *nibble & 7;
|
|
43
|
|
44 /* Get difference */
|
|
45 difference = *step >> 3;
|
|
46 if(*nibble & 4) difference += *step;
|
|
47 if(*nibble & 2) difference += *step >> 1;
|
|
48 if(*nibble & 1) difference += *step >> 2;
|
|
49
|
|
50 /* Predict value */
|
|
51 if(sign)
|
|
52 *predictor -= difference;
|
|
53 else
|
|
54 *predictor += difference;
|
|
55
|
|
56 if(*predictor > 32767) *predictor = 32767;
|
|
57 else
|
|
58 if(*predictor < -32768) *predictor = -32768;
|
|
59
|
|
60 /* Update the step value */
|
|
61 *step = quicktime_ima4_step[*index];
|
|
62 }
|
|
63
|
2420
|
64 int ima4_decode_block(unsigned short *output, unsigned char *input, int maxlen)
|
2399
|
65 {
|
|
66 int predictor;
|
|
67 int index;
|
|
68 int step;
|
|
69 int i, nibble, nibble_count, block_size;
|
|
70 int olen = 0;
|
|
71 unsigned char *block_ptr;
|
2420
|
72 unsigned char *input_end = input + IMA4_BLOCK_SIZE;
|
2399
|
73 // quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
|
|
74
|
|
75 /* Get the chunk header */
|
|
76 predictor = *input++ << 8;
|
|
77 predictor |= *input++;
|
|
78
|
|
79 index = predictor & 0x7f;
|
|
80 if(index > 88) index = 88;
|
|
81
|
|
82 predictor &= 0xff80;
|
|
83 if(predictor & 0x8000) predictor -= 0x10000;
|
|
84 step = quicktime_ima4_step[index];
|
|
85
|
|
86 /* Read the input buffer sequentially, one nibble at a time */
|
|
87 nibble_count = 0;
|
|
88 while(input < input_end)
|
|
89 {
|
|
90 nibble = nibble_count ? (*input++ >> 4) & 0x0f : *input & 0x0f;
|
|
91
|
|
92 ima4_decode_sample(&predictor, &nibble, &index, &step);
|
|
93 if (olen+1 > maxlen)
|
|
94 break;
|
|
95 *output++ = predictor;
|
|
96 olen++;
|
|
97
|
|
98 nibble_count ^= 1;
|
|
99 }
|
|
100 return(olen);
|
|
101 }
|
|
102
|
|
103 #if 0
|
|
104 void ima4_encode_sample(int *last_sample, int *last_index, int *nibble, int next_sample)
|
|
105 {
|
|
106 int difference, new_difference, mask, step;
|
|
107
|
|
108 difference = next_sample - *last_sample;
|
|
109 *nibble = 0;
|
|
110 step = quicktime_ima4_step[*last_index];
|
|
111 new_difference = step >> 3;
|
|
112
|
|
113 if(difference < 0)
|
|
114 {
|
|
115 *nibble = 8;
|
|
116 difference = -difference;
|
|
117 }
|
|
118
|
|
119 mask = 4;
|
|
120 while(mask)
|
|
121 {
|
|
122 if(difference >= step)
|
|
123 {
|
|
124 *nibble |= mask;
|
|
125 difference -= step;
|
|
126 new_difference += step;
|
|
127 }
|
|
128
|
|
129 step >>= 1;
|
|
130 mask >>= 1;
|
|
131 }
|
|
132
|
|
133 if(*nibble & 8)
|
|
134 *last_sample -= new_difference;
|
|
135 else
|
|
136 *last_sample += new_difference;
|
|
137
|
|
138 if(*last_sample > 32767) *last_sample = 32767;
|
|
139 else
|
|
140 if(*last_sample < -32767) *last_sample = -32767;
|
|
141
|
|
142 *last_index += quicktime_ima4_index[*nibble];
|
|
143
|
|
144 if(*last_index < 0) *last_index = 0;
|
|
145 else
|
|
146 if(*last_index > 88) *last_index= 88;
|
|
147 }
|
|
148
|
|
149 #if 0
|
|
150 void ima4_encode_block(quicktime_audio_map_t *atrack, unsigned char *output, int16_t *input, int step, int channel)
|
|
151 {
|
|
152 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
|
|
153 int i, nibble_count = 0, nibble, header;
|
|
154
|
|
155 /* Get a fake starting sample */
|
|
156 header = codec->last_samples[channel];
|
|
157 /* Force rounding. */
|
|
158 if(header < 0x7fc0) header += 0x40;
|
|
159 if(header < 0) header += 0x10000;
|
|
160 header &= 0xff80;
|
|
161 *output++ = (header & 0xff00) >> 8;
|
|
162 *output++ = (header & 0x80) + (codec->last_indexes[channel] & 0x7f);
|
|
163
|
|
164 for(i = 0; i < SAMPLES_PER_BLOCK; i++)
|
|
165 {
|
|
166 ima4_encode_sample(&(codec->last_samples[channel]),
|
|
167 &(codec->last_indexes[channel]),
|
|
168 &nibble,
|
|
169 *input);
|
|
170
|
|
171 if(nibble_count)
|
|
172 *output++ |= (nibble << 4);
|
|
173 else
|
|
174 *output = nibble;
|
|
175
|
|
176 input += step;
|
|
177 nibble_count ^= 1;
|
|
178 }
|
|
179 }
|
|
180 #endif
|
|
181 /* Convert the number of samples in a chunk into the number of bytes in that */
|
|
182 /* chunk. The number of samples in a chunk should end on a block boundary. */
|
|
183 long ima4_samples_to_bytes(long samples, int channels)
|
|
184 {
|
|
185 long bytes = samples / SAMPLES_PER_BLOCK * BLOCK_SIZE * channels;
|
|
186 return bytes;
|
|
187 }
|
|
188
|
|
189 /* Decode the chunk into the work buffer */
|
|
190 int ima4_decode_chunk(quicktime_t *file, int track, long chunk, int channel)
|
|
191 {
|
|
192 int result = 0;
|
|
193 int i, j;
|
|
194 long chunk_samples, chunk_bytes;
|
|
195 unsigned char *chunk_ptr, *block_ptr;
|
|
196 quicktime_trak_t *trak = file->atracks[track].track;
|
|
197 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)file->atracks[track].codec)->priv;
|
|
198
|
|
199 /* Get the byte count to read. */
|
|
200 chunk_samples = quicktime_chunk_samples(trak, chunk);
|
|
201 chunk_bytes = ima4_samples_to_bytes(chunk_samples, file->atracks[track].channels);
|
|
202
|
|
203 /* Get the buffer to read into. */
|
|
204 if(codec->work_buffer && codec->work_size < chunk_samples)
|
|
205 {
|
|
206 free(codec->work_buffer);
|
|
207 codec->work_buffer = 0;
|
|
208 }
|
|
209
|
|
210 if(!codec->work_buffer)
|
|
211 {
|
|
212 codec->work_size = chunk_samples;
|
|
213 codec->work_buffer = malloc(sizeof(int16_t) * codec->work_size);
|
|
214 }
|
|
215
|
|
216 if(codec->read_buffer && codec->read_size < chunk_bytes)
|
|
217 {
|
|
218 free(codec->read_buffer);
|
|
219 codec->read_buffer = 0;
|
|
220 }
|
|
221
|
|
222 if(!codec->read_buffer)
|
|
223 {
|
|
224 codec->read_size = chunk_bytes;
|
|
225 codec->read_buffer = malloc(codec->read_size);
|
|
226 }
|
|
227
|
|
228 /* codec->work_size now holds the number of samples in the last chunk */
|
|
229 /* codec->read_size now holds number of bytes in the last read buffer */
|
|
230
|
|
231 /* Read the entire chunk regardless of where the desired sample range starts. */
|
|
232 result = quicktime_read_chunk(file, codec->read_buffer, track, chunk, 0, chunk_bytes);
|
|
233
|
|
234 /* Now decode the chunk, one block at a time, until the total samples in the chunk */
|
|
235 /* is reached. */
|
|
236
|
|
237 if(!result)
|
|
238 {
|
|
239 block_ptr = codec->read_buffer;
|
|
240 for(i = 0; i < chunk_samples; i += SAMPLES_PER_BLOCK)
|
|
241 {
|
|
242 for(j = 0; j < file->atracks[track].channels; j++)
|
|
243 {
|
|
244 if(j == channel)
|
|
245 ima4_decode_block(&(file->atracks[track]), &(codec->work_buffer[i]), block_ptr);
|
|
246
|
|
247 block_ptr += BLOCK_SIZE;
|
|
248 }
|
|
249 }
|
|
250 }
|
|
251 codec->buffer_channel = channel;
|
|
252 codec->chunk = chunk;
|
|
253
|
|
254 return result;
|
|
255 }
|
|
256
|
|
257
|
|
258 /* =================================== public for ima4 */
|
|
259
|
|
260 static int quicktime_delete_codec_ima4(quicktime_audio_map_t *atrack)
|
|
261 {
|
|
262 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
|
|
263
|
|
264 if(codec->work_buffer) free(codec->work_buffer);
|
|
265 if(codec->read_buffer) free(codec->read_buffer);
|
|
266 if(codec->last_samples) free(codec->last_samples);
|
|
267 if(codec->last_indexes) free(codec->last_indexes);
|
|
268 codec->last_samples = 0;
|
|
269 codec->last_indexes = 0;
|
|
270 codec->read_buffer = 0;
|
|
271 codec->work_buffer = 0;
|
|
272 codec->chunk = 0;
|
|
273 codec->buffer_channel = 0; /* Channel of work buffer */
|
|
274 codec->work_size = 0; /* Size of work buffer */
|
|
275 codec->read_size = 0;
|
|
276 free(codec);
|
|
277 return 0;
|
|
278 }
|
|
279
|
|
280 static int quicktime_decode_ima4(quicktime_t *file,
|
|
281 int16_t *output_i,
|
|
282 float *output_f,
|
|
283 long samples,
|
|
284 int track,
|
|
285 int channel)
|
|
286 {
|
|
287 int result = 0;
|
|
288 longest chunk, chunk_sample, chunk_bytes, chunk_samples;
|
|
289 longest i, chunk_start, chunk_end;
|
|
290 quicktime_trak_t *trak = file->atracks[track].track;
|
|
291 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)file->atracks[track].codec)->priv;
|
|
292
|
|
293 /* Get the first chunk with this routine and then increase the chunk number. */
|
|
294 quicktime_chunk_of_sample(&chunk_sample, &chunk, trak, file->atracks[track].current_position);
|
|
295
|
|
296 /* Read chunks and extract ranges of samples until the output is full. */
|
|
297 for(i = 0; i < samples && !result; )
|
|
298 {
|
|
299 /* Get chunk we're on. */
|
|
300 chunk_samples = quicktime_chunk_samples(trak, chunk);
|
|
301
|
|
302 if(!codec->work_buffer ||
|
|
303 codec->chunk != chunk ||
|
|
304 codec->buffer_channel != channel)
|
|
305 {
|
|
306 /* read a new chunk if necessary */
|
|
307 result = ima4_decode_chunk(file, track, chunk, channel);
|
|
308 }
|
|
309
|
|
310 /* Get boundaries from the chunk */
|
|
311 chunk_start = 0;
|
|
312 if(chunk_sample < file->atracks[track].current_position)
|
|
313 chunk_start = file->atracks[track].current_position - chunk_sample;
|
|
314
|
|
315 chunk_end = chunk_samples;
|
|
316 if(chunk_sample + chunk_end > file->atracks[track].current_position + samples)
|
|
317 chunk_end = file->atracks[track].current_position + samples - chunk_sample;
|
|
318
|
|
319 /* Read from the chunk */
|
|
320 if(output_i)
|
|
321 {
|
|
322 /*printf("decode_ima4 1 chunk %ld %ld-%ld output %ld\n", chunk, chunk_start + chunk_sample, chunk_end + chunk_sample, i); */
|
|
323 while(chunk_start < chunk_end)
|
|
324 {
|
|
325 output_i[i++] = codec->work_buffer[chunk_start++];
|
|
326 }
|
|
327 /*printf("decode_ima4 2\n"); */
|
|
328 }
|
|
329 else
|
|
330 if(output_f)
|
|
331 {
|
|
332 while(chunk_start < chunk_end)
|
|
333 {
|
|
334 output_f[i++] = (float)codec->work_buffer[chunk_start++] / 32767;
|
|
335 }
|
|
336 }
|
|
337
|
|
338 chunk++;
|
|
339 chunk_sample += chunk_samples;
|
|
340 }
|
|
341
|
|
342 return result;
|
|
343 }
|
|
344
|
|
345 static int quicktime_encode_ima4(quicktime_t *file,
|
|
346 int16_t **input_i,
|
|
347 float **input_f,
|
|
348 int track,
|
|
349 long samples)
|
|
350 {
|
|
351 int result = 0;
|
|
352 longest i, j, step;
|
|
353 longest chunk_bytes;
|
|
354 longest overflow_start;
|
|
355 longest offset;
|
|
356 longest chunk_samples; /* Samples in the current chunk to be written */
|
|
357 quicktime_audio_map_t *track_map = &(file->atracks[track]);
|
|
358 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
|
|
359 int16_t *input_ptr;
|
|
360 unsigned char *output_ptr;
|
|
361
|
|
362 /* Get buffer sizes */
|
|
363 if(codec->work_buffer && codec->work_size < (samples + codec->work_overflow + 1) * track_map->channels)
|
|
364 {
|
|
365 /* Create new buffer */
|
|
366 longest new_size = (samples + codec->work_overflow + 1) * track_map->channels;
|
|
367 int16_t *new_buffer = malloc(sizeof(int16_t) * new_size);
|
|
368
|
|
369 /* Copy overflow */
|
|
370 for(i = 0; i < codec->work_overflow * track_map->channels; i++)
|
|
371 new_buffer[i] = codec->work_buffer[i];
|
|
372
|
|
373 /* Swap pointers. */
|
|
374 free(codec->work_buffer);
|
|
375 codec->work_buffer = new_buffer;
|
|
376 codec->work_size = new_size;
|
|
377 }
|
|
378 else
|
|
379 if(!codec->work_buffer)
|
|
380 {
|
|
381 /* No buffer in the first place. */
|
|
382 codec->work_size = (samples + codec->work_overflow) * track_map->channels;
|
|
383 /* Make the allocation enough for at least the flush routine. */
|
|
384 if(codec->work_size < SAMPLES_PER_BLOCK * track_map->channels)
|
|
385 codec->work_size = SAMPLES_PER_BLOCK * track_map->channels;
|
|
386 codec->work_buffer = malloc(sizeof(int16_t) * codec->work_size);
|
|
387 }
|
|
388
|
|
389 /* Get output size */
|
|
390 chunk_bytes = ima4_samples_to_bytes(samples + codec->work_overflow, track_map->channels);
|
|
391 if(codec->read_buffer && codec->read_size < chunk_bytes)
|
|
392 {
|
|
393 free(codec->read_buffer);
|
|
394 codec->read_buffer = 0;
|
|
395 }
|
|
396
|
|
397 if(!codec->read_buffer)
|
|
398 {
|
|
399 codec->read_buffer = malloc(chunk_bytes);
|
|
400 codec->read_size = chunk_bytes;
|
|
401 }
|
|
402
|
|
403 if(!codec->last_samples)
|
|
404 {
|
|
405 codec->last_samples = malloc(sizeof(int) * track_map->channels);
|
|
406 for(i = 0; i < track_map->channels; i++)
|
|
407 {
|
|
408 codec->last_samples[i] = 0;
|
|
409 }
|
|
410 }
|
|
411
|
|
412 if(!codec->last_indexes)
|
|
413 {
|
|
414 codec->last_indexes = malloc(sizeof(int) * track_map->channels);
|
|
415 for(i = 0; i < track_map->channels; i++)
|
|
416 {
|
|
417 codec->last_indexes[i] = 0;
|
|
418 }
|
|
419 }
|
|
420
|
|
421 /* Arm the input buffer after the last overflow */
|
|
422 step = track_map->channels;
|
|
423 for(j = 0; j < track_map->channels; j++)
|
|
424 {
|
|
425 input_ptr = codec->work_buffer + codec->work_overflow * track_map->channels + j;
|
|
426
|
|
427 if(input_i)
|
|
428 {
|
|
429 for(i = 0; i < samples; i++)
|
|
430 {
|
|
431 *input_ptr = input_i[j][i];
|
|
432 input_ptr += step;
|
|
433 }
|
|
434 }
|
|
435 else
|
|
436 if(input_f)
|
|
437 {
|
|
438 for(i = 0; i < samples; i++)
|
|
439 {
|
|
440 *input_ptr = (int16_t)(input_f[j][i] * 32767);
|
|
441 input_ptr += step;
|
|
442 }
|
|
443 }
|
|
444 }
|
|
445
|
|
446 /* Encode from the input buffer to the read_buffer up to a multiple of */
|
|
447 /* blocks. */
|
|
448 input_ptr = codec->work_buffer;
|
|
449 output_ptr = codec->read_buffer;
|
|
450
|
|
451 for(i = 0;
|
|
452 i + SAMPLES_PER_BLOCK <= samples + codec->work_overflow;
|
|
453 i += SAMPLES_PER_BLOCK)
|
|
454 {
|
|
455 for(j = 0; j < track_map->channels; j++)
|
|
456 {
|
|
457 ima4_encode_block(track_map, output_ptr, input_ptr + j, track_map->channels, j);
|
|
458
|
|
459 output_ptr += BLOCK_SIZE;
|
|
460 }
|
|
461 input_ptr += SAMPLES_PER_BLOCK * track_map->channels;
|
|
462 }
|
|
463
|
|
464 /* Write to disk */
|
|
465 chunk_samples = (longest)((samples + codec->work_overflow) / SAMPLES_PER_BLOCK) * SAMPLES_PER_BLOCK;
|
|
466
|
|
467 /*printf("quicktime_encode_ima4 1 %ld\n", chunk_samples); */
|
|
468 /* The block division may result in 0 samples getting encoded. */
|
|
469 /* Don't write 0 samples. */
|
|
470 if(chunk_samples)
|
|
471 {
|
|
472 offset = quicktime_position(file);
|
|
473 result = quicktime_write_data(file, codec->read_buffer, chunk_bytes);
|
|
474 if(result) result = 0; else result = 1; /* defeat fwrite's return */
|
|
475 quicktime_update_tables(file,
|
|
476 track_map->track,
|
|
477 offset,
|
|
478 track_map->current_chunk,
|
|
479 track_map->current_position,
|
|
480 chunk_samples,
|
|
481 0);
|
|
482 file->atracks[track].current_chunk++;
|
|
483 }
|
|
484
|
|
485 /* Move the last overflow to the front */
|
|
486 overflow_start = i;
|
|
487 input_ptr = codec->work_buffer;
|
|
488 for(i = overflow_start * track_map->channels ;
|
|
489 i < (samples + codec->work_overflow) * track_map->channels;
|
|
490 i++)
|
|
491 {
|
|
492 *input_ptr++ = codec->work_buffer[i];
|
|
493 }
|
|
494 codec->work_overflow = samples + codec->work_overflow - overflow_start;
|
|
495
|
|
496 return result;
|
|
497 }
|
|
498
|
|
499 int quicktime_flush_ima4(quicktime_t *file, int track)
|
|
500 {
|
|
501 quicktime_audio_map_t *track_map = &(file->atracks[track]);
|
|
502 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
|
|
503 int result = 0;
|
|
504 int i;
|
|
505
|
|
506 /*printf("quicktime_flush_ima4 %ld\n", codec->work_overflow); */
|
|
507 if(codec->work_overflow)
|
|
508 {
|
|
509 /* Zero out enough to get a block */
|
|
510 i = codec->work_overflow * track_map->channels;
|
|
511 while(i < SAMPLES_PER_BLOCK * track_map->channels)
|
|
512 {
|
|
513 codec->work_buffer[i++] = 0;
|
|
514 }
|
|
515 codec->work_overflow = i / track_map->channels + 1;
|
|
516 /* Write the work_overflow only. */
|
|
517 result = quicktime_encode_ima4(file, 0, 0, track, 0);
|
|
518 }
|
|
519 return result;
|
|
520 }
|
|
521
|
|
522 void quicktime_init_codec_ima4(quicktime_audio_map_t *atrack)
|
|
523 {
|
|
524 quicktime_ima4_codec_t *codec;
|
|
525
|
|
526 /* Init public items */
|
|
527 ((quicktime_codec_t*)atrack->codec)->priv = calloc(1, sizeof(quicktime_ima4_codec_t));
|
|
528 ((quicktime_codec_t*)atrack->codec)->delete_acodec = quicktime_delete_codec_ima4;
|
|
529 ((quicktime_codec_t*)atrack->codec)->decode_video = 0;
|
|
530 ((quicktime_codec_t*)atrack->codec)->encode_video = 0;
|
|
531 ((quicktime_codec_t*)atrack->codec)->decode_audio = quicktime_decode_ima4;
|
|
532 ((quicktime_codec_t*)atrack->codec)->encode_audio = quicktime_encode_ima4;
|
|
533
|
|
534 /* Init private items */
|
|
535 codec = ((quicktime_codec_t*)atrack->codec)->priv;
|
|
536 codec->work_buffer = 0;
|
|
537 codec->read_buffer = 0;
|
|
538 codec->chunk = 0;
|
|
539 codec->buffer_channel = 0;
|
|
540 codec->work_overflow = 0;
|
|
541 codec->work_size = 0;
|
|
542 codec->read_size = 0;
|
|
543 codec->last_samples = 0;
|
|
544 codec->last_indexes = 0;
|
|
545 }
|
|
546 #endif
|