File: twos.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 (619 lines) | stat: -rw-r--r-- 19,782 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
#include "funcprotos.h"
#include <quicktime/quicktime.h>
#define LQT_LIBQUICKTIME
#include <quicktime/lqt_codecapi.h>

#include "twos.h"

/* We decode this many samples at a time */

#define SAMPLES_PER_BLOCK 1024



/* =================================== private for twos */


typedef struct
{
	uint8_t *work_buffer;
	long buffer_size;
        int le; /* For 8 bits means unsigned operation, for more bits, little endian */
        int encode_initialized;

/* 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_i;
        float ** decode_sample_buffer_f;
        int decode_sample_buffer_size;
        int decode_sample_buffer_pos;
        int decode_initialized;
        int decode_block_align;

  } quicktime_twos_codec_t;

static int byte_order(void)
{                /* 1 if little endian */
	int16_t byteordertest;
	int byteorder;

	byteordertest = 0x0001;
	byteorder = *((unsigned char *)&byteordertest);
	return byteorder;
}

static int get_work_buffer(quicktime_t *file, int track, long bytes)
{
	quicktime_twos_codec_t *codec = ((quicktime_codec_t*)file->atracks[track].codec)->priv;

        if(codec->buffer_size < bytes)
          {
          codec->buffer_size = bytes + 128;
          codec->work_buffer = realloc(codec->work_buffer, codec->buffer_size);
          }
        return 0;
}

/* =================================== public for twos */

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

	if(codec->work_buffer) free(codec->work_buffer);
	codec->work_buffer = 0;
	codec->buffer_size = 0;

        if(codec->decode_sample_buffer_i)
          {
          free(codec->decode_sample_buffer_i[0]);
          free(codec->decode_sample_buffer_i);
          }
        if(codec->decode_sample_buffer_f)
          {
          free(codec->decode_sample_buffer_f[0]);
          free(codec->decode_sample_buffer_f);
          }
        if(codec->decode_buffer)
          free(codec->decode_buffer);


        free(codec);
	return 0;
}

static int swap_bytes(uint8_t *buffer, long samples, int bits)
{
	long i = 0;
	char byte1;
	uint8_t *buffer1, *buffer2;

	switch(bits)
	{
                case 8:
                  /* Swap sign */
                  for(i = 0; i < samples; i++)
                    buffer[i] ^= 0x80;
                  break;

		case 16:
                        if(!byte_order()) return 0;
			buffer1 = buffer;
			buffer2 = buffer + 1;
			while(i < samples * 2)
			{
				byte1 = buffer2[i];
				buffer2[i] = buffer1[i];
				buffer1[i] = byte1;
				i += 2;
			}
			break;

		case 24:
                        if(!byte_order()) return 0;
			buffer1 = buffer;
			buffer2 = buffer + 2;
			while(i < samples * 3)
                          {
                                byte1 = buffer2[i];
				buffer2[i] = buffer1[i];
				buffer1[i] = byte1;
				i += 3;
			}
			break;

		default:
			break;
	}
	return 0;
}


static int decode(quicktime_t *file, 
					int16_t **output_i, 
					float **output_f, 
					long samples, 
					int track) 
{
        int j;
        quicktime_audio_map_t *track_map = &(file->atracks[track]);
	quicktime_twos_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;

	int64_t chunk, chunk_sample;
	int64_t i;

        int samples_decoded, samples_copied;

        int64_t samples_to_skip = 0;
        int bits;
        
        bits = quicktime_audio_bits(file, track);

        //        fprintf(stderr, "Bits: %d\n", bits);
        
        if(!codec->decode_initialized)
          {
          codec->decode_initialized = 1;

          if(bits <= 16)
            {
            codec->decode_sample_buffer_i    = malloc(sizeof(*(codec->decode_sample_buffer_i)) * file->atracks[track].channels);
            codec->decode_sample_buffer_i[0] = malloc(sizeof(*(codec->decode_sample_buffer_i[0])) * file->atracks[track].channels *
                                                      SAMPLES_PER_BLOCK);
            for(i = 1; i < file->atracks[track].channels; i++)
              codec->decode_sample_buffer_i[i] = codec->decode_sample_buffer_i[0] + i * SAMPLES_PER_BLOCK;
            
            }
          else
            {
            codec->decode_sample_buffer_f    = malloc(sizeof(*(codec->decode_sample_buffer_f)) * file->atracks[track].channels);
            codec->decode_sample_buffer_f[0] = malloc(sizeof(*(codec->decode_sample_buffer_f[0])) * file->atracks[track].channels *
                                                      SAMPLES_PER_BLOCK);

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

          codec->decode_block_align = track_map->channels * bits / 8;

          /* 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;
          /* Handle AVI byte order */
          if(codec->le)
            swap_bytes(codec->decode_buffer, 
                       (codec->decode_buffer_size * track_map->channels) / codec->decode_block_align,
                       quicktime_audio_bits(file, track));
          
          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)
            {
            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;

            if(codec->le)
              swap_bytes(codec->decode_buffer, 
                         (codec->decode_buffer_size * track_map->channels) / codec->decode_block_align,
                         quicktime_audio_bits(file, track));
            }
          else
            {
            codec->decode_buffer_size += (int)(codec->decode_buffer_ptr - codec->decode_buffer);
            codec->decode_buffer_ptr = codec->decode_buffer;
            }

          /* Skip frames */
          
          samples_to_skip = file->atracks[track].current_position - chunk_sample;
          if(samples_to_skip < 0)
            {
            fprintf(stderr, "twos: Cannot skip backwards\n");
            samples_to_skip = 0;
            }
          
          codec->decode_buffer_ptr = codec->decode_buffer + samples_to_skip * codec->decode_block_align;
          codec->decode_buffer_size -= samples_to_skip * codec->decode_block_align;
          codec->decode_sample_buffer_size = 0;
          codec->decode_sample_buffer_pos = 0;
          }

        /* 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;
              /* Handle AVI byte order */
              if(codec->le)
		swap_bytes(codec->decode_buffer, 
                           (codec->decode_buffer_size * track_map->channels) / codec->decode_block_align,
                           quicktime_audio_bits(file, track));
              
              codec->decode_buffer_ptr = codec->decode_buffer;
              }
            
            codec->decode_sample_buffer_size = codec->decode_buffer_size / codec->decode_block_align;
            if(codec->decode_sample_buffer_size > SAMPLES_PER_BLOCK)
              {
              codec->decode_sample_buffer_size = SAMPLES_PER_BLOCK;
              }

            codec->decode_sample_buffer_pos = 0;
            
            /* Decode the stuff */
            
            switch(bits)
              {
              case 8:
                for(i = 0; i < codec->decode_sample_buffer_size; i++)
                  {
                  for(j = 0; j < track_map->channels; j++)
                    {
                    codec->decode_sample_buffer_i[j][i] = ((int16_t)(*codec->decode_buffer_ptr)) << 8;
                    codec->decode_buffer_ptr++;
                    codec->decode_buffer_size--;
                    }
                  }
                break;
              case 16:
                for(i = 0; i < codec->decode_sample_buffer_size; i++)
                  {
                  for(j = 0; j < track_map->channels; j++)
                    {
                    codec->decode_sample_buffer_i[j][i] = ((int16_t)codec->decode_buffer_ptr[0]) << 8 |
                      ((unsigned char)codec->decode_buffer_ptr[1]);
                    codec->decode_buffer_ptr+= 2;
                    codec->decode_buffer_size-=2;
                    }
                  }
                break;
              case 24:
                for(i = 0; i < codec->decode_sample_buffer_size; i++)
                  {
                  for(j = 0; j < track_map->channels; j++)
                    {
                    codec->decode_sample_buffer_i[j][i] = (float)((((int)codec->decode_buffer_ptr[0]) << 16) | 
                                                           (((unsigned char)codec->decode_buffer_ptr[1]) << 8) |
                                                           ((unsigned char)codec->decode_buffer_ptr[2])) / 0x7fffff;
                    codec->decode_buffer_ptr+= 3;
                    codec->decode_buffer_size-=3;
                    }
                  }
                break;
              default:
                break;
              }
            }
          
          /* Copy samples */
          //          fprintf(stderr, "Copy samples: %d\n", codec->decode_buffer_size);
          samples_copied = lqt_copy_audio(output_i,                                     // int16_t ** dst_i
                                          output_f,                                     // float ** dst_f
                                          codec->decode_sample_buffer_i,                  // int16_t ** src_i
                                          codec->decode_sample_buffer_f,                  // float ** src_f
                                          samples_decoded,                              // int dst_pos
                                          codec->decode_sample_buffer_pos,              // 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;
          codec->decode_sample_buffer_pos += samples_copied;
          }
        
        file->atracks[track].last_position = file->atracks[track].current_position + samples_decoded;
        return samples_decoded;

        
#if 0 /* Old (hopefully obsolete version) */
        
        int step = track_map->channels * quicktime_audio_bits(file, track) / 8;

	get_work_buffer(file, track, samples * step);
/*
 * printf("decode 1 %d\n", quicktime_audio_bits(file, track));
 * sleep(1);
 */
	result = !quicktime_read_audio(file, codec->work_buffer, samples, track);


/* Undo increment since this is done in codecs.c */
	track_map->current_position -= samples;

/* Handle AVI byte order */
	if(codec->le)
		swap_bytes(codec->work_buffer, 
			samples, 
			track_map->channels, 
			quicktime_audio_bits(file, track));

	switch(quicktime_audio_bits(file, track))
	{
		case 8:
			if(output_i && !result)
			{
				for(i = 0, j = channel; i < samples; i++)
				{
					output_i[i] = ((int16_t)codec->work_buffer[j]) << 8;
					j += step;
				}
			}
			else
			if(output_f && !result)
			{
				for(i = 0, j = channel; i < samples; i++)
				{
					output_f[i] = ((float)codec->work_buffer[j]) / 0x7f;
					j += step;
				}
			}
			break;
		
		case 16:
			if(output_i && !result)
			{
				for(i = 0, j = channel * 2; i < samples; i++)
				{
					output_i[i] = ((int16_t)codec->work_buffer[j]) << 8 |
									((unsigned char)codec->work_buffer[j + 1]);
					j += step;
				}
			}
			else
			if(output_f && !result)
			{
				for(i = 0, j = channel * 2; i < samples; i++)
				{
					output_f[i] = (float)((((int16_t)codec->work_buffer[j]) << 8) |
									((unsigned char)codec->work_buffer[j + 1])) / 0x7fff;
					j += step;
				}
			}
			break;
		
		case 24:
			if(output_i && !result)
			{
				for(i = 0, j = channel * 3; i < samples; i++)
				{
					output_i[i] = (((int16_t)codec->work_buffer[j]) << 8) | 
									((unsigned char)codec->work_buffer[j + 1]);
					j += step;
				}
			}
			else
			if(output_f && !result)
			{
				for(i = 0, j = channel * 3; i < samples; i++)
				{
					output_f[i] = (float)((((int)codec->work_buffer[j]) << 16) | 
									(((unsigned char)codec->work_buffer[j + 1]) << 8) |
									((unsigned char)codec->work_buffer[j + 2])) / 0x7fffff;
					j += step;
				}
			}
			break;
		
		default:
			break;
	}


	return result;
#endif
}

#define CLAMP(x, y, z) ((x) = ((x) <  (y) ? (y) : ((x) > (z) ? (z) : (x))))

static int encode(quicktime_t *file, 
							int16_t **input_i, 
							float **input_f, 
							int track, 
							long samples)
{
	int result = 0;
	long i, j;
        quicktime_trak_t *trak;
	quicktime_audio_map_t *track_map = &(file->atracks[track]);
	quicktime_twos_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
	int step = track_map->channels * quicktime_audio_bits(file, track) / 8;
	int sample;
	float sample_f;
        int bits = quicktime_audio_bits(file, track);
        
        if(!codec->encode_initialized)
          {
          trak = track_map->track;
          if(trak->strl)
            {
            /* strh stuff */
            trak->strl->dwRate = (trak->mdia.minf.stbl.stsd.table[0].sample_rate * track_map->channels * bits) / 8;
            trak->strl->dwScale = (track_map->channels * bits) / 8;
            trak->strl->dwSampleSize = bits/8;
            
            /* WAVEFORMATEX stuff */
            
            trak->strl->nBlockAlign = trak->strl->dwScale;
            trak->strl->nAvgBytesPerSec =  trak->strl->dwRate;
            trak->strl->wBitsPerSample = bits;
            }
          codec->encode_initialized = 1;
          }

        get_work_buffer(file, track, samples * step);

	if(input_i)
	{
		for(i = 0; i < track_map->channels; i++)
		{
			switch(bits)
			{
				case 8:
					for(j = 0; j < samples; j++)
					{
						sample = input_i[i][j] >> 8;
						codec->work_buffer[j * step + i] = sample;
					}
					break;
				case 16:
					for(j = 0; j < samples; j++)
					{
						sample = input_i[i][j];
						codec->work_buffer[j * step + i * 2] = ((unsigned int)sample & 0xff00) >> 8;
						codec->work_buffer[j * step + i * 2 + 1] = ((unsigned int)sample) & 0xff;
					}
					break;
				case 24:
					for(j = 0; j < samples; j++)
					{
						sample = input_i[i][j];
						codec->work_buffer[j * step + i * 3] = ((unsigned int)sample & 0xff00) >> 8;
						codec->work_buffer[j * step + i * 3 + 1] = ((unsigned int)sample & 0xff);
						codec->work_buffer[j * step + i * 3 + 2] = 0;
					}
					break;
			}
		}
	}
	else
	{
		for(i = 0; i < track_map->channels; i++)
		{
			switch(quicktime_audio_bits(file, track))
			{
				case 8:
					for(j = 0; j < samples; j++)
					{
						sample_f = input_f[i][j];
						if(sample_f < 0)
							sample = (int)(sample_f * 0x7f - 0.5);
						else
							sample = (int)(sample_f * 0x7f + 0.5);
						CLAMP(sample, -0x7f, 0x7f);
						codec->work_buffer[j * step + i] = sample;
					}
					break;
				case 16:
					for(j = 0; j < samples; j++)
					{
						sample_f = input_f[i][j];
						if(sample_f < 0)
							sample = (int)(sample_f * 0x7fff - 0.5);
						else
							sample = (int)(sample_f * 0x7fff + 0.5);
						CLAMP(sample, -0x7fff, 0x7fff);
						codec->work_buffer[j * step + i * 2] = ((unsigned int)sample & 0xff00) >> 8;
						codec->work_buffer[j * step + i * 2 + 1] = ((unsigned int)sample) & 0xff;
					}
					break;
				case 24:
					for(j = 0; j < samples; j++)
					{
						sample_f = input_f[i][j];
						if(sample_f < 0)
							sample = (int)(sample_f * 0x7fffff - 0.5);
						else
							sample = (int)(sample_f * 0x7fffff + 0.5);
						CLAMP(sample, -0x7fffff, 0x7fffff);
						codec->work_buffer[j * step + i * 3] = ((unsigned int)sample & 0xff0000) >> 16;
						codec->work_buffer[j * step + i * 3 + 1] = ((unsigned int)sample & 0xff00) >> 8;
						codec->work_buffer[j * step + i * 3 + 2] = ((unsigned int)sample) & 0xff;
					}
					break;
			}
		}
	}

/* Handle AVI byte order */
	if(codec->le)
		swap_bytes(codec->work_buffer, 
                           samples * 
                           track_map->channels, 
			quicktime_audio_bits(file, track));

	result = quicktime_write_audio(file, codec->work_buffer, samples, track);

	return result;
}

void quicktime_init_codec_twos(quicktime_audio_map_t *atrack)
{
	quicktime_twos_codec_t *codec;
	quicktime_codec_t *codec_base = (quicktime_codec_t*)atrack->codec;

/* Init public items */
	codec_base->delete_acodec = delete_codec;
	codec_base->decode_audio = decode;
	codec_base->encode_audio = encode;
	codec_base->fourcc = QUICKTIME_TWOS;
	codec_base->title = "Twos complement";
	codec_base->desc = "Twos complement";

/* Init private items */
	codec = codec_base->priv = calloc(1, sizeof(quicktime_twos_codec_t));
	codec->work_buffer = 0;
	codec->buffer_size = 0;
}

void quicktime_init_codec_sowt(quicktime_audio_map_t *atrack)
{
	quicktime_twos_codec_t *codec;
	quicktime_codec_t *codec_base = (quicktime_codec_t*)atrack->codec;

/* Init public items */
	codec_base->delete_acodec = delete_codec;
	codec_base->decode_audio = decode;
	codec_base->encode_audio = encode;
	codec_base->fourcc = "SOWT";
	codec_base->title = "Twos complement (Little endian)";
	codec_base->desc = "Twos complement (Little endian)";
	codec_base->wav_id = 0x01;

/* Init private items */
	codec = codec_base->priv = calloc(1, sizeof(quicktime_twos_codec_t));
	codec->work_buffer = 0;
	codec->buffer_size = 0;
        codec->le = 1;
}