File: ima4.c

package info (click to toggle)
libquicktime 2%3A0.9.7-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,824 kB
  • ctags: 4,320
  • sloc: ansic: 45,467; sh: 8,613; makefile: 480
file content (644 lines) | stat: -rw-r--r-- 19,731 bytes parent folder | download
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#include "funcprotos.h"
#include "ima4.h"
#include <quicktime/quicktime.h>
#define LQT_LIBQUICKTIME
#include <quicktime/lqt_codecapi.h>


typedef struct
{
/* During decoding the work_buffer contains the most recently read chunk. */
/* During encoding the work_buffer contains interlaced overflow samples  */
/* from the last chunk written. */
	int16_t *work_buffer;
	unsigned char *read_buffer;    /* Temporary buffer for drive reads. */

/* Starting information for all channels during encoding. */
	int *last_samples, *last_indexes;
	long chunk; /* Number of chunk in work buffer */
	int buffer_channel; /* Channel of work buffer */

/* Number of samples in largest chunk read. */
/* Number of samples plus overflow in largest chunk write, interlaced. */
	long work_size;     
	long work_overflow; /* Number of overflow samples from the last chunk written. */
	long read_size;     /* Size of read buffer. */

/* Decode specific stuff */
        int decode_buffer_size;
        int decode_buffer_alloc;
        uint8_t * decode_buffer;
        uint8_t * decode_buffer_ptr;

        int16_t ** decode_sample_buffer;
        int decode_sample_buffer_size;
        int decode_initialized;
  } quicktime_ima4_codec_t;

static int ima4_step[89] = 
{
    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};

static int ima4_index[16] = 
{
    -1, -1, -1, -1, 2, 4, 6, 8,
    -1, -1, -1, -1, 2, 4, 6, 8
};

/* Known by divine revelation */

#define BLOCK_SIZE 0x22
#define SAMPLES_PER_BLOCK 0x40

/* ================================== private for ima4 */


static void ima4_decode_sample(int *predictor, int *nibble, int *index, int *step)
{
	int difference, sign;

/* Get new index value */
	*index += ima4_index[*nibble];

	if(*index < 0) *index = 0; 
	else 
	if(*index > 88) *index = 88;

/* Get sign and magnitude from *nibble */
	sign = *nibble & 8;
	*nibble = *nibble & 7;

/* Get difference */
	difference = *step >> 3;
	if(*nibble & 4) difference += *step;
	if(*nibble & 2) difference += *step >> 1;
	if(*nibble & 1) difference += *step >> 2;

/* Predict value */
	if(sign) 
	*predictor -= difference;
	else 
	*predictor += difference;

	if(*predictor > 32767) *predictor = 32767;
	else
	if(*predictor < -32768) *predictor = -32768;

/* Update the step value */
	*step = ima4_step[*index];
}

static void ima4_decode_block(quicktime_audio_map_t *atrack, int16_t *output, unsigned char *input)
{
	int predictor;
	int index;
	int step;
	int nibble, nibble_count;
	unsigned char *input_end = input + BLOCK_SIZE;

/* Get the chunk header */
	predictor = *input++ << 8;
	predictor |= *input++;

	index = predictor & 0x7f;
	if(index > 88) index = 88;

	predictor &= 0xff80;
	if(predictor & 0x8000) predictor -= 0x10000;
	step = ima4_step[index];

/* Read the input buffer sequentially, one nibble at a time */
	nibble_count = 0;
	while(input < input_end)
	{
		nibble = nibble_count ? (*input++  >> 4) & 0x0f : *input & 0x0f;

		ima4_decode_sample(&predictor, &nibble, &index, &step);
		*output++ = predictor;

		nibble_count ^= 1;
	}
}

static void ima4_encode_sample(int *last_sample, int *last_index, int *nibble, int next_sample)
{
	int difference, new_difference, mask, step;

	difference = next_sample - *last_sample;
	*nibble = 0;
	step = ima4_step[*last_index];
	new_difference = step >> 3;

	if(difference < 0)
	{
		*nibble = 8;
		difference = -difference;
	}

	mask = 4;
	while(mask)
	{
		if(difference >= step)
		{
			*nibble |= mask;
			difference -= step;
			new_difference += step;
		}

		step >>= 1;
		mask >>= 1;
	}

	if(*nibble & 8)
		*last_sample -= new_difference;
	else
		*last_sample += new_difference;

	if(*last_sample > 32767) *last_sample = 32767;
	else
	if(*last_sample < -32767) *last_sample = -32767;

	*last_index += ima4_index[*nibble];

	if(*last_index < 0) *last_index = 0;
	else
	if(*last_index > 88) *last_index= 88;
}

static void ima4_encode_block(quicktime_audio_map_t *atrack, unsigned char *output, int16_t *input, int step, int channel)
{
	quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
	int i, nibble_count = 0, nibble, header;

/* Get a fake starting sample */
	header = codec->last_samples[channel];
/* Force rounding. */
	if(header < 0x7fc0) header += 0x40;
	if(header < 0) header += 0x10000;
	header &= 0xff80;
	*output++ = (header & 0xff00) >> 8;
	*output++ = (header & 0x80) + (codec->last_indexes[channel] & 0x7f);

	for(i = 0; i < SAMPLES_PER_BLOCK; i++)
	{
		ima4_encode_sample(&(codec->last_samples[channel]), 
							&(codec->last_indexes[channel]), 
							&nibble, 
							*input);

		if(nibble_count)
			*output++ |= (nibble << 4);
		else
			*output = nibble;

		input += step;
		nibble_count ^= 1;
	}
}

/* Convert the number of samples in a chunk into the number of bytes in that */
/* chunk.  The number of samples in a chunk should end on a block boundary. */
static long ima4_samples_to_bytes(long samples, int channels)
{
	long bytes = samples / SAMPLES_PER_BLOCK * BLOCK_SIZE * channels;
	return bytes;
}

/* =================================== public for ima4 */

static int delete_codec(quicktime_audio_map_t *atrack)
{
	quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;

	if(codec->work_buffer) free(codec->work_buffer);
	if(codec->read_buffer) free(codec->read_buffer);
	if(codec->last_samples) free(codec->last_samples);
	if(codec->last_indexes) free(codec->last_indexes);

        if(codec->decode_sample_buffer)
          {
          free(codec->decode_sample_buffer[0]);
          free(codec->decode_sample_buffer);
          }
        if(codec->decode_buffer)
          free(codec->decode_buffer);
        
        free(codec);
	return 0;
}

static int decode(quicktime_t *file, 
					int16_t **output_i, 
					float **output_f,
					long samples, 
					int track)
{
	int64_t chunk_sample;
	int64_t i;
        int64_t chunk;
        int samples_decoded, samples_copied;
	quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)file->atracks[track].codec)->priv;
        int samples_to_skip = 0;

        if(!codec->decode_initialized)
          {
          codec->decode_initialized = 1;

          codec->decode_sample_buffer    = malloc(sizeof(*(codec->decode_sample_buffer)) * file->atracks[track].channels);
          codec->decode_sample_buffer[0] = malloc(sizeof(*(codec->decode_sample_buffer[0])) * file->atracks[track].channels *
                                           SAMPLES_PER_BLOCK);

          for(i = 1; i < file->atracks[track].channels; i++)
            codec->decode_sample_buffer[i] = codec->decode_sample_buffer[0] + i * SAMPLES_PER_BLOCK;

          /* Read first chunk */
          
          codec->decode_buffer_size = lqt_read_audio_chunk(file,
                                                           track, file->atracks[track].current_chunk,
                                                           &(codec->decode_buffer),
                                                           &(codec->decode_buffer_alloc));
          if(codec->decode_buffer_size <= 0)
            return 0;
          codec->decode_buffer_ptr = codec->decode_buffer;
          }
        
        if(file->atracks[track].current_position != file->atracks[track].last_position)
          {
          /* Seeking happened */
          quicktime_chunk_of_sample(&chunk_sample, &chunk,
                                    file->atracks[track].track,
                                    file->atracks[track].current_position);

          /* Read the chunk */

          if(file->atracks[track].current_chunk != chunk)
            //          if(1)
            {
            file->atracks[track].current_chunk = chunk;
            codec->decode_buffer_size = lqt_read_audio_chunk(file,
                                                             track, file->atracks[track].current_chunk,
                                                             &(codec->decode_buffer),
                                                             &(codec->decode_buffer_alloc));
            if(codec->decode_buffer_size <= 0)
              return 0;
            codec->decode_buffer_ptr = codec->decode_buffer;
            }
          else
            {
            codec->decode_buffer_size += (int)(codec->decode_buffer_ptr - codec->decode_buffer);
            codec->decode_buffer_ptr = codec->decode_buffer;
            }
          
#if 0          
          fprintf(stderr, "Seeking done, pos: %lld, chunk_sample: %lld, chunk: %lld\n",
                  file->atracks[track].current_position,
                  chunk_sample,
                  file->atracks[track].current_chunk);
#endif    
          samples_to_skip = file->atracks[track].current_position - chunk_sample;
          if(samples_to_skip < 0)
            {
            fprintf(stderr, "ima4: Cannot skip backwards\n");
            samples_to_skip = 0;
            }
          
          /* Skip frames */

          while(samples_to_skip > SAMPLES_PER_BLOCK)
            {
            codec->decode_buffer_ptr += file->atracks[track].channels * BLOCK_SIZE;
            codec->decode_buffer_size -= file->atracks[track].channels * BLOCK_SIZE;
            samples_to_skip -= SAMPLES_PER_BLOCK;
            }

          /* Decode frame */

          for(i = 0; i < file->atracks[track].channels; i++)
            {
            ima4_decode_block(&(file->atracks[track]), codec->decode_sample_buffer[i], codec->decode_buffer_ptr);
            codec->decode_buffer_ptr += BLOCK_SIZE;
            codec->decode_buffer_size -= BLOCK_SIZE;
            }

          /* Skip samples */
          
          codec->decode_sample_buffer_size = SAMPLES_PER_BLOCK - samples_to_skip;
          }
        
        /* Decode until we are done */

        samples_decoded = 0;

        while(samples_decoded < samples)
          {
          /* Decode new frame if necessary */
          if(!codec->decode_sample_buffer_size)
            {
            /* Get new chunk if necessary */
            
            if(!codec->decode_buffer_size)
              {
              file->atracks[track].current_chunk++;
              codec->decode_buffer_size = lqt_read_audio_chunk(file,
                                                               track, file->atracks[track].current_chunk,
                                                               &(codec->decode_buffer),
                                                               &(codec->decode_buffer_alloc));
              if(codec->decode_buffer_size <= 0)
                break;
              codec->decode_buffer_ptr = codec->decode_buffer;
              }
            
            /* Decode one frame */

            for(i = 0; i < file->atracks[track].channels; i++)
              {
              ima4_decode_block(&(file->atracks[track]), codec->decode_sample_buffer[i], codec->decode_buffer_ptr);
              codec->decode_buffer_ptr += BLOCK_SIZE;
              codec->decode_buffer_size -= BLOCK_SIZE;
              }
            codec->decode_sample_buffer_size = SAMPLES_PER_BLOCK;
            }
          
          /* Copy samples */

          samples_copied = lqt_copy_audio(output_i,                                     // int16_t ** dst_i
                                          output_f,                                     // float ** dst_f
                                          codec->decode_sample_buffer,                         // int16_t ** src_i
                                          NULL,                                         // float ** src_f
                                          samples_decoded,                              // int dst_pos
                                          SAMPLES_PER_BLOCK - codec->decode_sample_buffer_size,// int src_pos
                                          samples - samples_decoded,                    // int dst_size
                                          codec->decode_sample_buffer_size,                    // int src_size
                                          file->atracks[track].channels);               // int num_channels
          
          samples_decoded += samples_copied;
          codec->decode_sample_buffer_size -= samples_copied;
          }
        
        file->atracks[track].last_position = file->atracks[track].current_position + samples_decoded;
        return samples_decoded;
#if 0 /* Old version, hopefully obsolete */
        
/* Get the first chunk with this routine and then increase the chunk number. */
	quicktime_chunk_of_sample(&chunk_sample, &chunk, trak, file->atracks[track].current_position);

/* Read chunks and extract ranges of samples until the output is full. */
	for(i = 0; i < samples && !result; )
	{
/* Get chunk we're on. */
		chunk_samples = quicktime_chunk_samples(trak, chunk);

		if(!codec->work_buffer ||
			codec->chunk != chunk ||
			codec->buffer_channel != channel)
		{
/* read a new chunk if necessary */
			result = ima4_decode_chunk(file, track, chunk, channel);
		}

/* Get boundaries from the chunk */
		chunk_start = 0;
		if(chunk_sample < file->atracks[track].current_position)
			chunk_start = file->atracks[track].current_position - chunk_sample;

		chunk_end = chunk_samples;
		if(chunk_sample + chunk_end > file->atracks[track].current_position + samples)
			chunk_end = file->atracks[track].current_position + samples - chunk_sample;

/* Read from the chunk */
		if(output_i)
		{
/*printf("decode_ima4 1 chunk %ld %ld-%ld output %ld\n", chunk, chunk_start + chunk_sample, chunk_end + chunk_sample, i); */
			while(chunk_start < chunk_end)
			{
				output_i[i++] = codec->work_buffer[chunk_start++];
			}
/*printf("decode_ima4 2\n"); */
		}
		else
		if(output_f)
		{
			while(chunk_start < chunk_end)
			{
				output_f[i++] = (float)codec->work_buffer[chunk_start++] / 32767;
			}
		}

		chunk++;
		chunk_sample += chunk_samples;
	}

	return result;
#endif
}

static int encode(quicktime_t *file, 
						int16_t **input_i, 
						float **input_f, 
						int track, 
						long samples)
{
	int result = 0;
	int64_t i, j, step;
	int64_t chunk_bytes;
	int64_t overflow_start;
	int64_t chunk_samples; /* Samples in the current chunk to be written */
	quicktime_audio_map_t *track_map = &(file->atracks[track]);
	quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
	quicktime_trak_t *trak = track_map->track;
	int16_t *input_ptr;
	unsigned char *output_ptr;
	quicktime_atom_t chunk_atom;

/* Get buffer sizes */
	if(codec->work_buffer && codec->work_size < (samples + codec->work_overflow + 1) * track_map->channels)
	{
/* Create new buffer */
		int64_t new_size = (samples + codec->work_overflow + 1) * track_map->channels;
		int16_t *new_buffer = malloc(sizeof(int16_t) * new_size);

/* Copy overflow */
		for(i = 0; i < codec->work_overflow * track_map->channels; i++)
			new_buffer[i] = codec->work_buffer[i];

/* Swap pointers. */
		free(codec->work_buffer);
		codec->work_buffer = new_buffer;
		codec->work_size = new_size;
	}
	else
	if(!codec->work_buffer)
	{
/* No buffer in the first place. */
		codec->work_size = (samples + codec->work_overflow) * track_map->channels;
/* Make the allocation enough for at least the flush routine. */
		if(codec->work_size < SAMPLES_PER_BLOCK * track_map->channels)
			codec->work_size = SAMPLES_PER_BLOCK * track_map->channels;
		codec->work_buffer = malloc(sizeof(int16_t) * codec->work_size);
	}

/* Get output size */
	chunk_bytes = ima4_samples_to_bytes(samples + codec->work_overflow, track_map->channels);
	if(codec->read_buffer && codec->read_size < chunk_bytes)
	{
		free(codec->read_buffer);
		codec->read_buffer = 0;
	}

	if(!codec->read_buffer)
	{
		codec->read_buffer = malloc(chunk_bytes);
		codec->read_size = chunk_bytes;
	}

	if(!codec->last_samples)
	{
		codec->last_samples = malloc(sizeof(int) * track_map->channels);
		for(i = 0; i < track_map->channels; i++)
		{
			codec->last_samples[i] = 0;
		}
	}

	if(!codec->last_indexes)
	{
		codec->last_indexes = malloc(sizeof(int) * track_map->channels);
		for(i = 0; i < track_map->channels; i++)
		{
			codec->last_indexes[i] = 0;
		}
	}

/* Arm the input buffer after the last overflow */
	step = track_map->channels;
	for(j = 0; j < track_map->channels; j++)
	{
		input_ptr = codec->work_buffer + codec->work_overflow * track_map->channels + j;

		if(input_i)
		{
			for(i = 0; i < samples; i++)
			{
				*input_ptr = input_i[j][i];
				input_ptr += step;
			}
		}
		else
		if(input_f)
		{
			for(i = 0; i < samples; i++)
			{
				*input_ptr = (int16_t)(input_f[j][i] * 32767);
				input_ptr += step;
			}
		}
	}

/* Encode from the input buffer to the read_buffer up to a multiple of  */
/* blocks. */
	input_ptr = codec->work_buffer;
	output_ptr = codec->read_buffer;

	for(i = 0; 
		i + SAMPLES_PER_BLOCK <= samples + codec->work_overflow; 
		i += SAMPLES_PER_BLOCK)
	{
		for(j = 0; j < track_map->channels; j++)
		{
			ima4_encode_block(track_map, output_ptr, input_ptr + j, track_map->channels, j);

			output_ptr += BLOCK_SIZE;
		}
		input_ptr += SAMPLES_PER_BLOCK * track_map->channels;
	}

/* Write to disk */
	chunk_samples = (int64_t)((samples + codec->work_overflow) / SAMPLES_PER_BLOCK) * SAMPLES_PER_BLOCK;

/*printf("quicktime_encode_ima4 1 %ld\n", chunk_samples); */
/* The block division may result in 0 samples getting encoded. */
/* Don't write 0 samples. */
	if(chunk_samples)
	{
		quicktime_write_chunk_header(file, trak, &chunk_atom);
		result = quicktime_write_data(file, codec->read_buffer, chunk_bytes);
		quicktime_write_chunk_footer(file, 
			trak,
			track_map->current_chunk,
			&chunk_atom, 
			chunk_samples);

		if(result) 
			result = 0; 
		else 
			result = 1; /* defeat fwrite's return */

		track_map->current_chunk++;
	}

/* Move the last overflow to the front */
	overflow_start = i;
	input_ptr = codec->work_buffer;
	for(i = overflow_start * track_map->channels ; 
		i < (samples + codec->work_overflow) * track_map->channels; 
		i++)
	{
		*input_ptr++ = codec->work_buffer[i];
	}
	codec->work_overflow = samples + codec->work_overflow - overflow_start;

	return result;
}

static void flush(quicktime_t *file, int track)
{
	quicktime_audio_map_t *track_map = &(file->atracks[track]);
	quicktime_ima4_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
	int result = 0;
	int i;

/*printf("quicktime_flush_ima4 %ld\n", codec->work_overflow); */
	if(codec->work_overflow)
	{
/* Zero out enough to get a block */
		i = codec->work_overflow * track_map->channels;
		while(i < SAMPLES_PER_BLOCK * track_map->channels)
		{
			codec->work_buffer[i++] = 0;
		}
		codec->work_overflow = i / track_map->channels + 1;
/* Write the work_overflow only. */
		result = encode(file, 0, 0, track, 0);
	}
}

void quicktime_init_codec_ima4(quicktime_audio_map_t *atrack)
{
	quicktime_codec_t *codec_base = (quicktime_codec_t*)atrack->codec;
	quicktime_ima4_codec_t *codec;

/* Init public items */
	codec_base->priv = calloc(1, sizeof(quicktime_ima4_codec_t));
	codec_base->delete_acodec = delete_codec;
	codec_base->decode_video = 0;
	codec_base->encode_video = 0;
	codec_base->decode_audio = decode;
	codec_base->encode_audio = encode;
	codec_base->flush = flush;
	codec_base->fourcc = QUICKTIME_IMA4;
	codec_base->title = "IMA 4";
	codec_base->desc = "IMA 4";
	codec_base->wav_id = 0x11;

/* Init private items */
	codec = codec_base->priv;
}