initial commit
[goodguy/history.git] / cinelerra-5.0 / quicktime / ima4.c
1 #include "funcprotos.h"
2 #include "ima4.h"
3 #include "quicktime.h"
4
5 typedef struct
6 {
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. */
10         int16_t *work_buffer;
11         unsigned char *read_buffer;    /* Temporary buffer for drive reads. */
12
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 */
17
18 /* Number of samples in largest chunk read. */
19 /* Number of samples plus overflow in largest chunk write, interlaced. */
20         long work_size;     
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;
24
25 static int ima4_step[89] = 
26 {
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
36 };
37
38 static int ima4_index[16] = 
39 {
40     -1, -1, -1, -1, 2, 4, 6, 8,
41     -1, -1, -1, -1, 2, 4, 6, 8
42 };
43
44 /* Known by divine revelation */
45
46 #define BLOCK_SIZE 0x22
47 #define SAMPLES_PER_BLOCK 0x40
48
49 /* ================================== private for ima4 */
50
51
52 void ima4_decode_sample(int *predictor, int *nibble, int *index, int *step)
53 {
54         int difference, sign;
55
56 /* Get new index value */
57         *index += ima4_index[*nibble];
58
59         if(*index < 0) *index = 0; 
60         else 
61         if(*index > 88) *index = 88;
62
63 /* Get sign and magnitude from *nibble */
64         sign = *nibble & 8;
65         *nibble = *nibble & 7;
66
67 /* Get difference */
68         difference = *step >> 3;
69         if(*nibble & 4) difference += *step;
70         if(*nibble & 2) difference += *step >> 1;
71         if(*nibble & 1) difference += *step >> 2;
72
73 /* Predict value */
74         if(sign) 
75         *predictor -= difference;
76         else 
77         *predictor += difference;
78
79         if(*predictor > 32767) *predictor = 32767;
80         else
81         if(*predictor < -32768) *predictor = -32768;
82
83 /* Update the step value */
84         *step = ima4_step[*index];
85 }
86
87 void ima4_decode_block(quicktime_audio_map_t *atrack, int16_t *output, unsigned char *input)
88 {
89         int predictor, index, step;
90         int nibble, nibble_count;
91         unsigned char *input_end = input + BLOCK_SIZE;
92
93 /* Get the chunk header */
94         predictor = *input++ << 8;
95         predictor |= *input++;
96
97         index = predictor & 0x7f;
98         if(index > 88) index = 88;
99
100         predictor &= 0xff80;
101         if(predictor & 0x8000) predictor -= 0x10000;
102         step = ima4_step[index];
103
104 /* Read the input buffer sequentially, one nibble at a time */
105         nibble_count = 0;
106         while(input < input_end)
107         {
108                 nibble = nibble_count ? (*input++  >> 4) & 0x0f : *input & 0x0f;
109
110                 ima4_decode_sample(&predictor, &nibble, &index, &step);
111                 *output++ = predictor;
112
113                 nibble_count ^= 1;
114         }
115 }
116
117
118 void ima4_encode_sample(int *last_sample, int *last_index, int *nibble, int next_sample)
119 {
120         int difference, new_difference, mask, step;
121
122         difference = next_sample - *last_sample;
123         *nibble = 0;
124         step = ima4_step[*last_index];
125         new_difference = step >> 3;
126
127         if(difference < 0)
128         {
129                 *nibble = 8;
130                 difference = -difference;
131         }
132
133         mask = 4;
134         while(mask)
135         {
136                 if(difference >= step)
137                 {
138                         *nibble |= mask;
139                         difference -= step;
140                         new_difference += step;
141                 }
142
143                 step >>= 1;
144                 mask >>= 1;
145         }
146
147         if(*nibble & 8)
148                 *last_sample -= new_difference;
149         else
150                 *last_sample += new_difference;
151
152         if(*last_sample > 32767) *last_sample = 32767;
153         else
154         if(*last_sample < -32767) *last_sample = -32767;
155
156         *last_index += ima4_index[*nibble];
157
158         if(*last_index < 0) *last_index = 0;
159         else
160         if(*last_index > 88) *last_index= 88;
161 }
162
163 void ima4_encode_block(quicktime_audio_map_t *atrack, unsigned char *output, int16_t *input, int step, int channel)
164 {
165         quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
166         int i, nibble_count = 0, nibble, header;
167
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;
173         header &= 0xff80;
174         *output++ = (header & 0xff00) >> 8;
175         *output++ = (header & 0x80) + (codec->last_indexes[channel] & 0x7f);
176
177         for(i = 0; i < SAMPLES_PER_BLOCK; i++)
178         {
179                 ima4_encode_sample(&(codec->last_samples[channel]), 
180                                                         &(codec->last_indexes[channel]), 
181                                                         &nibble, 
182                                                         *input);
183
184                 if(nibble_count)
185                         *output++ |= (nibble << 4);
186                 else
187                         *output = nibble;
188
189                 input += step;
190                 nibble_count ^= 1;
191         }
192 }
193
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)
197 {
198         long bytes = samples / SAMPLES_PER_BLOCK * BLOCK_SIZE * channels;
199         return bytes;
200 }
201
202 /* Decode the chunk into the work buffer */
203 int ima4_decode_chunk(quicktime_t *file, int track, long chunk, int channel)
204 {
205         int result = 0;
206         int i, j;
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;
211
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);
215
216 /* Get the buffer to read into. */
217         if(codec->work_buffer && codec->work_size < chunk_samples)
218         {
219                 free(codec->work_buffer);
220                 codec->work_buffer = 0;
221         }
222
223         if(!codec->work_buffer)
224         {
225                 codec->work_size = chunk_samples;
226                 codec->work_buffer = malloc(sizeof(int16_t) * codec->work_size);
227         }
228
229         if(codec->read_buffer && codec->read_size < chunk_bytes)
230         {
231                 free(codec->read_buffer);
232                 codec->read_buffer = 0;
233         }
234
235         if(!codec->read_buffer)
236         {
237                 codec->read_size = chunk_bytes;
238                 codec->read_buffer = malloc(codec->read_size);
239         }
240
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 */
243
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);
247
248 /* Now decode the chunk, one block at a time, until the total samples in the chunk */
249 /* is reached. */
250
251         if(!result)
252         {
253                 block_ptr = codec->read_buffer;
254                 for(i = 0; i < chunk_samples; i += SAMPLES_PER_BLOCK)
255                 {
256                         for(j = 0; j < file->atracks[track].channels; j++)
257                         {
258                                 if(j == channel)
259                                         ima4_decode_block(&(file->atracks[track]), &(codec->work_buffer[i]), block_ptr);
260
261                                 block_ptr += BLOCK_SIZE;
262                         }
263                 }
264         }
265         codec->buffer_channel = channel;
266         codec->chunk = chunk;
267
268         return result;
269 }
270
271
272 /* =================================== public for ima4 */
273
274 static void delete_codec(quicktime_audio_map_t *atrack)
275 {
276         quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
277
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;
286         codec->chunk = 0;
287         codec->buffer_channel = 0; /* Channel of work buffer */
288         codec->work_size = 0;          /* Size of work buffer */
289         codec->read_size = 0;
290         free(codec);
291 }
292
293 static int decode(quicktime_t *file, 
294                                         int16_t *output_i, 
295                                         float *output_f,
296                                         long samples, 
297                                         int track, 
298                                         int channel)
299 {
300         int result = 0;
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;
305
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);
308
309 /* Read chunks and extract ranges of samples until the output is full. */
310         for(i = 0; i < samples && !result; )
311         {
312 /* Get chunk we're on. */
313                 chunk_samples = quicktime_chunk_samples(trak, chunk);
314
315                 if(!codec->work_buffer ||
316                         codec->chunk != chunk ||
317                         codec->buffer_channel != channel)
318                 {
319 /* read a new chunk if necessary */
320                         result = ima4_decode_chunk(file, track, chunk, channel);
321                 }
322
323 /* Get boundaries from the chunk */
324                 chunk_start = 0;
325                 if(chunk_sample < file->atracks[track].current_position)
326                         chunk_start = file->atracks[track].current_position - chunk_sample;
327
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;
331
332 /* Read from the chunk */
333                 if(output_i)
334                 {
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)
337                         {
338                                 output_i[i++] = codec->work_buffer[chunk_start++];
339                         }
340 /*printf("decode_ima4 2\n"); */
341                 }
342                 else
343                 if(output_f)
344                 {
345                         while(chunk_start < chunk_end)
346                         {
347                                 output_f[i++] = (float)codec->work_buffer[chunk_start++] / 32767;
348                         }
349                 }
350
351                 chunk++;
352                 chunk_sample += chunk_samples;
353         }
354
355         return result;
356 }
357
358 static int encode(quicktime_t *file, 
359                                                 int16_t **input_i, 
360                                                 float **input_f, 
361                                                 int track, 
362                                                 long samples)
363 {
364         int result = 0;
365         int64_t i, j, step;
366         int64_t chunk_bytes;
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;
372         int16_t *input_ptr;
373         unsigned char *output_ptr;
374         quicktime_atom_t chunk_atom;
375
376 /* Get buffer sizes */
377         if(codec->work_buffer && codec->work_size < (samples + codec->work_overflow + 1) * track_map->channels)
378         {
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);
382
383 /* Copy overflow */
384                 for(i = 0; i < codec->work_overflow * track_map->channels; i++)
385                         new_buffer[i] = codec->work_buffer[i];
386
387 /* Swap pointers. */
388                 free(codec->work_buffer);
389                 codec->work_buffer = new_buffer;
390                 codec->work_size = new_size;
391         }
392         else
393         if(!codec->work_buffer)
394         {
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);
401         }
402
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)
406         {
407                 free(codec->read_buffer);
408                 codec->read_buffer = 0;
409         }
410
411         if(!codec->read_buffer)
412         {
413                 codec->read_buffer = malloc(chunk_bytes);
414                 codec->read_size = chunk_bytes;
415         }
416
417         if(!codec->last_samples)
418         {
419                 codec->last_samples = malloc(sizeof(int) * track_map->channels);
420                 for(i = 0; i < track_map->channels; i++)
421                 {
422                         codec->last_samples[i] = 0;
423                 }
424         }
425
426         if(!codec->last_indexes)
427         {
428                 codec->last_indexes = malloc(sizeof(int) * track_map->channels);
429                 for(i = 0; i < track_map->channels; i++)
430                 {
431                         codec->last_indexes[i] = 0;
432                 }
433         }
434
435 /* Arm the input buffer after the last overflow */
436         step = track_map->channels;
437         for(j = 0; j < track_map->channels; j++)
438         {
439                 input_ptr = codec->work_buffer + codec->work_overflow * track_map->channels + j;
440
441                 if(input_i)
442                 {
443                         for(i = 0; i < samples; i++)
444                         {
445                                 *input_ptr = input_i[j][i];
446                                 input_ptr += step;
447                         }
448                 }
449                 else
450                 if(input_f)
451                 {
452                         for(i = 0; i < samples; i++)
453                         {
454                                 *input_ptr = (int16_t)(input_f[j][i] * 32767);
455                                 input_ptr += step;
456                         }
457                 }
458         }
459
460 /* Encode from the input buffer to the read_buffer up to a multiple of  */
461 /* blocks. */
462         input_ptr = codec->work_buffer;
463         output_ptr = codec->read_buffer;
464
465         for(i = 0; 
466                 i + SAMPLES_PER_BLOCK <= samples + codec->work_overflow; 
467                 i += SAMPLES_PER_BLOCK)
468         {
469                 for(j = 0; j < track_map->channels; j++)
470                 {
471                         ima4_encode_block(track_map, output_ptr, input_ptr + j, track_map->channels, j);
472
473                         output_ptr += BLOCK_SIZE;
474                 }
475                 input_ptr += SAMPLES_PER_BLOCK * track_map->channels;
476         }
477
478 /* Write to disk */
479         chunk_samples = (int64_t)((samples + codec->work_overflow) / SAMPLES_PER_BLOCK) * SAMPLES_PER_BLOCK;
480
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. */
484         if(chunk_samples)
485         {
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, 
489                         trak,
490                         track_map->current_chunk,
491                         &chunk_atom, 
492                         chunk_samples);
493
494                 if(result) 
495                         result = 0; 
496                 else 
497                         result = 1; /* defeat fwrite's return */
498
499                 track_map->current_chunk++;
500         }
501
502 /* Move the last overflow to the front */
503         overflow_start = i;
504         input_ptr = codec->work_buffer;
505         for(i = overflow_start * track_map->channels ; 
506                 i < (samples + codec->work_overflow) * track_map->channels; 
507                 i++)
508         {
509                 *input_ptr++ = codec->work_buffer[i];
510         }
511         codec->work_overflow = samples + codec->work_overflow - overflow_start;
512
513         return result;
514 }
515
516 void flush(quicktime_t *file, int track)
517 {
518         quicktime_audio_map_t *track_map = &(file->atracks[track]);
519         quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
520         int i;
521
522 /*printf("quicktime_flush_ima4 %ld\n", codec->work_overflow); */
523         if(codec->work_overflow)
524         {
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)
528                 {
529                         codec->work_buffer[i++] = 0;
530                 }
531                 codec->work_overflow = i / track_map->channels + 1;
532 /* Write the work_overflow only. */
533                 encode(file, 0, 0, track, 0);
534         }
535 }
536
537 void quicktime_init_codec_ima4(quicktime_audio_map_t *atrack)
538 {
539         quicktime_codec_t *codec_base = (quicktime_codec_t*)atrack->codec;
540 //      quicktime_ima4_codec_t *codec = codec_base->priv;
541
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;
554
555 /* Init private items */
556 }