1 #include "funcprotos.h"
7 /* During decoding the work_buffer contains the most recently read chunk. */
8 /* During encoding the work_buffer contains interlaced overflow samples */
9 /* from the last chunk written. */
11 unsigned char *read_buffer; /* Temporary buffer for drive reads. */
13 /* Starting information for all channels during encoding. */
14 int *last_samples, *last_indexes;
15 long chunk; /* Number of chunk in work buffer */
16 int buffer_channel; /* Channel of work buffer */
18 /* Number of samples in largest chunk read. */
19 /* Number of samples plus overflow in largest chunk write, interlaced. */
21 long work_overflow; /* Number of overflow samples from the last chunk written. */
22 long read_size; /* Size of read buffer. */
23 } quicktime_ima4_codec_t;
25 static int ima4_step[89] =
27 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
28 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
29 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
30 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
31 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
32 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
33 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
34 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
35 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
38 static int ima4_index[16] =
40 -1, -1, -1, -1, 2, 4, 6, 8,
41 -1, -1, -1, -1, 2, 4, 6, 8
44 /* Known by divine revelation */
46 #define BLOCK_SIZE 0x22
47 #define SAMPLES_PER_BLOCK 0x40
49 /* ================================== private for ima4 */
52 void ima4_decode_sample(int *predictor, int *nibble, int *index, int *step)
56 /* Get new index value */
57 *index += ima4_index[*nibble];
59 if(*index < 0) *index = 0;
61 if(*index > 88) *index = 88;
63 /* Get sign and magnitude from *nibble */
65 *nibble = *nibble & 7;
68 difference = *step >> 3;
69 if(*nibble & 4) difference += *step;
70 if(*nibble & 2) difference += *step >> 1;
71 if(*nibble & 1) difference += *step >> 2;
75 *predictor -= difference;
77 *predictor += difference;
79 if(*predictor > 32767) *predictor = 32767;
81 if(*predictor < -32768) *predictor = -32768;
83 /* Update the step value */
84 *step = ima4_step[*index];
87 void ima4_decode_block(quicktime_audio_map_t *atrack, int16_t *output, unsigned char *input)
89 int predictor, index, step;
90 int nibble, nibble_count;
91 unsigned char *input_end = input + BLOCK_SIZE;
93 /* Get the chunk header */
94 predictor = *input++ << 8;
95 predictor |= *input++;
97 index = predictor & 0x7f;
98 if(index > 88) index = 88;
101 if(predictor & 0x8000) predictor -= 0x10000;
102 step = ima4_step[index];
104 /* Read the input buffer sequentially, one nibble at a time */
106 while(input < input_end)
108 nibble = nibble_count ? (*input++ >> 4) & 0x0f : *input & 0x0f;
110 ima4_decode_sample(&predictor, &nibble, &index, &step);
111 *output++ = predictor;
118 void ima4_encode_sample(int *last_sample, int *last_index, int *nibble, int next_sample)
120 int difference, new_difference, mask, step;
122 difference = next_sample - *last_sample;
124 step = ima4_step[*last_index];
125 new_difference = step >> 3;
130 difference = -difference;
136 if(difference >= step)
140 new_difference += step;
148 *last_sample -= new_difference;
150 *last_sample += new_difference;
152 if(*last_sample > 32767) *last_sample = 32767;
154 if(*last_sample < -32767) *last_sample = -32767;
156 *last_index += ima4_index[*nibble];
158 if(*last_index < 0) *last_index = 0;
160 if(*last_index > 88) *last_index= 88;
163 void ima4_encode_block(quicktime_audio_map_t *atrack, unsigned char *output, int16_t *input, int step, int channel)
165 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
166 int i, nibble_count = 0, nibble, header;
168 /* Get a fake starting sample */
169 header = codec->last_samples[channel];
170 /* Force rounding. */
171 if(header < 0x7fc0) header += 0x40;
172 if(header < 0) header += 0x10000;
174 *output++ = (header & 0xff00) >> 8;
175 *output++ = (header & 0x80) + (codec->last_indexes[channel] & 0x7f);
177 for(i = 0; i < SAMPLES_PER_BLOCK; i++)
179 ima4_encode_sample(&(codec->last_samples[channel]),
180 &(codec->last_indexes[channel]),
185 *output++ |= (nibble << 4);
194 /* Convert the number of samples in a chunk into the number of bytes in that */
195 /* chunk. The number of samples in a chunk should end on a block boundary. */
196 long ima4_samples_to_bytes(long samples, int channels)
198 long bytes = samples / SAMPLES_PER_BLOCK * BLOCK_SIZE * channels;
202 /* Decode the chunk into the work buffer */
203 int ima4_decode_chunk(quicktime_t *file, int track, long chunk, int channel)
207 long chunk_samples, chunk_bytes;
208 unsigned char *block_ptr;
209 quicktime_trak_t *trak = file->atracks[track].track;
210 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)file->atracks[track].codec)->priv;
212 /* Get the byte count to read. */
213 chunk_samples = quicktime_chunk_samples(trak, chunk);
214 chunk_bytes = ima4_samples_to_bytes(chunk_samples, file->atracks[track].channels);
216 /* Get the buffer to read into. */
217 if(codec->work_buffer && codec->work_size < chunk_samples)
219 free(codec->work_buffer);
220 codec->work_buffer = 0;
223 if(!codec->work_buffer)
225 codec->work_size = chunk_samples;
226 codec->work_buffer = malloc(sizeof(int16_t) * codec->work_size);
229 if(codec->read_buffer && codec->read_size < chunk_bytes)
231 free(codec->read_buffer);
232 codec->read_buffer = 0;
235 if(!codec->read_buffer)
237 codec->read_size = chunk_bytes;
238 codec->read_buffer = malloc(codec->read_size);
241 /* codec->work_size now holds the number of samples in the last chunk */
242 /* codec->read_size now holds number of bytes in the last read buffer */
244 /* Read the entire chunk regardless of where the desired sample range starts. */
245 result = quicktime_read_chunk(file, (char*)codec->read_buffer,
246 track, chunk, 0, chunk_bytes);
248 /* Now decode the chunk, one block at a time, until the total samples in the chunk */
253 block_ptr = codec->read_buffer;
254 for(i = 0; i < chunk_samples; i += SAMPLES_PER_BLOCK)
256 for(j = 0; j < file->atracks[track].channels; j++)
259 ima4_decode_block(&(file->atracks[track]), &(codec->work_buffer[i]), block_ptr);
261 block_ptr += BLOCK_SIZE;
265 codec->buffer_channel = channel;
266 codec->chunk = chunk;
272 /* =================================== public for ima4 */
274 static void delete_codec(quicktime_audio_map_t *atrack)
276 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
278 if(codec->work_buffer) free(codec->work_buffer);
279 if(codec->read_buffer) free(codec->read_buffer);
280 if(codec->last_samples) free(codec->last_samples);
281 if(codec->last_indexes) free(codec->last_indexes);
282 codec->last_samples = 0;
283 codec->last_indexes = 0;
284 codec->read_buffer = 0;
285 codec->work_buffer = 0;
287 codec->buffer_channel = 0; /* Channel of work buffer */
288 codec->work_size = 0; /* Size of work buffer */
289 codec->read_size = 0;
293 static int decode(quicktime_t *file,
301 int64_t chunk, chunk_sample, chunk_samples;
302 int64_t i, chunk_start, chunk_end;
303 quicktime_trak_t *trak = file->atracks[track].track;
304 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)file->atracks[track].codec)->priv;
306 /* Get the first chunk with this routine and then increase the chunk number. */
307 quicktime_chunk_of_sample(&chunk_sample, &chunk, trak, file->atracks[track].current_position);
309 /* Read chunks and extract ranges of samples until the output is full. */
310 for(i = 0; i < samples && !result; )
312 /* Get chunk we're on. */
313 chunk_samples = quicktime_chunk_samples(trak, chunk);
315 if(!codec->work_buffer ||
316 codec->chunk != chunk ||
317 codec->buffer_channel != channel)
319 /* read a new chunk if necessary */
320 result = ima4_decode_chunk(file, track, chunk, channel);
323 /* Get boundaries from the chunk */
325 if(chunk_sample < file->atracks[track].current_position)
326 chunk_start = file->atracks[track].current_position - chunk_sample;
328 chunk_end = chunk_samples;
329 if(chunk_sample + chunk_end > file->atracks[track].current_position + samples)
330 chunk_end = file->atracks[track].current_position + samples - chunk_sample;
332 /* Read from the chunk */
335 /*printf("decode_ima4 1 chunk %ld %ld-%ld output %ld\n", chunk, chunk_start + chunk_sample, chunk_end + chunk_sample, i); */
336 while(chunk_start < chunk_end)
338 output_i[i++] = codec->work_buffer[chunk_start++];
340 /*printf("decode_ima4 2\n"); */
345 while(chunk_start < chunk_end)
347 output_f[i++] = (float)codec->work_buffer[chunk_start++] / 32767;
352 chunk_sample += chunk_samples;
358 static int encode(quicktime_t *file,
367 int64_t overflow_start;
368 int64_t chunk_samples; /* Samples in the current chunk to be written */
369 quicktime_audio_map_t *track_map = &(file->atracks[track]);
370 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
371 quicktime_trak_t *trak = track_map->track;
373 unsigned char *output_ptr;
374 quicktime_atom_t chunk_atom;
376 /* Get buffer sizes */
377 if(codec->work_buffer && codec->work_size < (samples + codec->work_overflow + 1) * track_map->channels)
379 /* Create new buffer */
380 int64_t new_size = (samples + codec->work_overflow + 1) * track_map->channels;
381 int16_t *new_buffer = malloc(sizeof(int16_t) * new_size);
384 for(i = 0; i < codec->work_overflow * track_map->channels; i++)
385 new_buffer[i] = codec->work_buffer[i];
388 free(codec->work_buffer);
389 codec->work_buffer = new_buffer;
390 codec->work_size = new_size;
393 if(!codec->work_buffer)
395 /* No buffer in the first place. */
396 codec->work_size = (samples + codec->work_overflow) * track_map->channels;
397 /* Make the allocation enough for at least the flush routine. */
398 if(codec->work_size < SAMPLES_PER_BLOCK * track_map->channels)
399 codec->work_size = SAMPLES_PER_BLOCK * track_map->channels;
400 codec->work_buffer = malloc(sizeof(int16_t) * codec->work_size);
403 /* Get output size */
404 chunk_bytes = ima4_samples_to_bytes(samples + codec->work_overflow, track_map->channels);
405 if(codec->read_buffer && codec->read_size < chunk_bytes)
407 free(codec->read_buffer);
408 codec->read_buffer = 0;
411 if(!codec->read_buffer)
413 codec->read_buffer = malloc(chunk_bytes);
414 codec->read_size = chunk_bytes;
417 if(!codec->last_samples)
419 codec->last_samples = malloc(sizeof(int) * track_map->channels);
420 for(i = 0; i < track_map->channels; i++)
422 codec->last_samples[i] = 0;
426 if(!codec->last_indexes)
428 codec->last_indexes = malloc(sizeof(int) * track_map->channels);
429 for(i = 0; i < track_map->channels; i++)
431 codec->last_indexes[i] = 0;
435 /* Arm the input buffer after the last overflow */
436 step = track_map->channels;
437 for(j = 0; j < track_map->channels; j++)
439 input_ptr = codec->work_buffer + codec->work_overflow * track_map->channels + j;
443 for(i = 0; i < samples; i++)
445 *input_ptr = input_i[j][i];
452 for(i = 0; i < samples; i++)
454 *input_ptr = (int16_t)(input_f[j][i] * 32767);
460 /* Encode from the input buffer to the read_buffer up to a multiple of */
462 input_ptr = codec->work_buffer;
463 output_ptr = codec->read_buffer;
466 i + SAMPLES_PER_BLOCK <= samples + codec->work_overflow;
467 i += SAMPLES_PER_BLOCK)
469 for(j = 0; j < track_map->channels; j++)
471 ima4_encode_block(track_map, output_ptr, input_ptr + j, track_map->channels, j);
473 output_ptr += BLOCK_SIZE;
475 input_ptr += SAMPLES_PER_BLOCK * track_map->channels;
479 chunk_samples = (int64_t)((samples + codec->work_overflow) / SAMPLES_PER_BLOCK) * SAMPLES_PER_BLOCK;
481 /*printf("quicktime_encode_ima4 1 %ld\n", chunk_samples); */
482 /* The block division may result in 0 samples getting encoded. */
483 /* Don't write 0 samples. */
486 quicktime_write_chunk_header(file, trak, &chunk_atom);
487 result = quicktime_write_data(file, (char*)codec->read_buffer, chunk_bytes);
488 quicktime_write_chunk_footer(file,
490 track_map->current_chunk,
497 result = 1; /* defeat fwrite's return */
499 track_map->current_chunk++;
502 /* Move the last overflow to the front */
504 input_ptr = codec->work_buffer;
505 for(i = overflow_start * track_map->channels ;
506 i < (samples + codec->work_overflow) * track_map->channels;
509 *input_ptr++ = codec->work_buffer[i];
511 codec->work_overflow = samples + codec->work_overflow - overflow_start;
516 void flush(quicktime_t *file, int track)
518 quicktime_audio_map_t *track_map = &(file->atracks[track]);
519 quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
522 /*printf("quicktime_flush_ima4 %ld\n", codec->work_overflow); */
523 if(codec->work_overflow)
525 /* Zero out enough to get a block */
526 i = codec->work_overflow * track_map->channels;
527 while(i < SAMPLES_PER_BLOCK * track_map->channels)
529 codec->work_buffer[i++] = 0;
531 codec->work_overflow = i / track_map->channels + 1;
532 /* Write the work_overflow only. */
533 encode(file, 0, 0, track, 0);
537 void quicktime_init_codec_ima4(quicktime_audio_map_t *atrack)
539 quicktime_codec_t *codec_base = (quicktime_codec_t*)atrack->codec;
540 // quicktime_ima4_codec_t *codec = codec_base->priv;
542 /* Init public items */
543 codec_base->priv = calloc(1, sizeof(quicktime_ima4_codec_t));
544 codec_base->delete_acodec = delete_codec;
545 codec_base->decode_video = 0;
546 codec_base->encode_video = 0;
547 codec_base->decode_audio = decode;
548 codec_base->encode_audio = encode;
549 codec_base->flush = flush;
550 codec_base->fourcc = QUICKTIME_IMA4;
551 codec_base->title = "IMA 4";
552 codec_base->desc = "IMA 4";
553 codec_base->wav_id = 0x11;
555 /* Init private items */