3 #include "funcprotos.h"
8 /* During decoding the work_buffer contains the most recently read chunk. */
9 /* During encoding the work_buffer contains interlaced overflow samples */
10 /* from the last chunk written. */
11 int16_t *write_buffer;
12 unsigned char *read_buffer; /* Temporary buffer for drive reads. */
14 /* Starting information for all channels during encoding a chunk. */
15 int *last_samples, *last_indexes;
16 long chunk; /* Number of chunk in work buffer */
17 int buffer_channel; /* Channel of work buffer */
19 /* Number of samples in largest chunk read. */
20 /* Number of samples plus overflow in largest chunk write, interlaced. */
21 long write_size; /* Size of write buffer. */
22 long read_size; /* Size of read buffer. */
23 } quicktime_wmx2_codec_t;
25 static int quicktime_wmx2_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 quicktime_wmx2_index[16] =
40 -1, -1, -1, -1, 2, 4, 6, 8,
41 -1, -1, -1, -1, 2, 4, 6, 8
44 /* ================================== private for wmx2 */
47 static int wmx2_decode_sample(int *predictor, int *nibble, int *index, int *step)
51 /* Get new index value */
52 *index += quicktime_wmx2_index[*nibble];
54 if(*index < 0) *index = 0;
56 if(*index > 88) *index = 88;
58 /* Get sign and magnitude from *nibble */
60 *nibble = *nibble & 7;
63 difference = *step >> 3;
64 if(*nibble & 4) difference += *step;
65 if(*nibble & 2) difference += *step >> 1;
66 if(*nibble & 1) difference += *step >> 2;
70 *predictor -= difference;
72 *predictor += difference;
74 if(*predictor > 32767) *predictor = 32767;
76 if(*predictor < -32768) *predictor = -32768;
78 /* Update the step value */
79 *step = quicktime_wmx2_step[*index];
85 static int wmx2_decode_block(quicktime_audio_map_t *atrack, int16_t *output, unsigned char *input, int samples)
87 int predictor, index, step;
88 int nibble, nibble_count;
89 int16_t *output_end = output + samples;
91 /* Get the chunk header */
92 predictor = *input++ << 8;
93 predictor |= *input++;
94 if(predictor & 0x8000) predictor -= 0x10000;
96 if(index > 88) index = 88;
98 /*printf("input %d %d\n", predictor, index); */
99 step = quicktime_wmx2_step[index];
101 /* Read the input buffer sequentially, one nibble at a time */
103 while(output < output_end)
105 nibble = nibble_count ? (*input++ >> 4) & 0x0f : *input & 0x0f;
107 wmx2_decode_sample(&predictor, &nibble, &index, &step);
108 *output++ = predictor;
115 static int wmx2_encode_sample(int *last_sample, int *last_index, int *nibble, int next_sample)
117 int difference, new_difference, mask, step;
119 difference = next_sample - *last_sample;
121 step = quicktime_wmx2_step[*last_index];
122 new_difference = step >> 3;
127 difference = -difference;
133 if(difference >= step)
137 new_difference += step;
145 *last_sample -= new_difference;
147 *last_sample += new_difference;
149 if(*last_sample > 32767) *last_sample = 32767;
151 if(*last_sample < -32767) *last_sample = -32767;
153 *last_index += quicktime_wmx2_index[*nibble];
155 if(*last_index < 0) *last_index = 0;
157 if(*last_index > 88) *last_index= 88;
162 static int wmx2_encode_block(quicktime_audio_map_t *atrack, unsigned char *output, int16_t *input, int step, int channel, int samples)
164 quicktime_wmx2_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
165 int i, nibble_count = 0, nibble, header;
167 /* Get a fake starting sample */
168 header = codec->last_samples[channel];
169 /*printf("output %d %d\n", header, codec->last_indexes[channel]); */
170 /* Force rounding. */
171 /* if(header < 0x7fc0) header += 0x40; */
172 /* header &= 0xff80; */
173 if(header < 0) header += 0x10000;
174 *output++ = (header & 0xff00) >> 8;
175 *output++ = (header & 0xff);
176 *output++ = (codec->last_indexes[channel] & 0x7f);
178 for(i = 0; i < samples; i++)
180 wmx2_encode_sample(&(codec->last_samples[channel]),
181 &(codec->last_indexes[channel]),
186 *output++ |= (nibble << 4);
197 /* Convert the number of samples in a chunk into the number of bytes in that */
198 /* chunk. The number of samples in a chunk should end on a block boundary. */
199 static long wmx2_samples_to_bytes(long samples, int channels)
201 long bytes = samples / 2;
202 if(bytes * 2 < samples) bytes++;
204 bytes += HEADER_SIZE * channels;
208 /* Decode the chunk into the work buffer */
209 static int wmx2_decode_chunk(quicktime_t *file, int track, long chunk, int channel)
212 long chunk_samples, chunk_bytes;
213 unsigned char *block_ptr;
214 quicktime_trak_t *trak = file->atracks[track].track;
215 quicktime_wmx2_codec_t *codec =
216 ((quicktime_codec_t*)file->atracks[track].codec)->priv;
218 /* Get the byte count to read. */
219 chunk_samples = quicktime_chunk_samples(trak, chunk);
220 chunk_bytes = wmx2_samples_to_bytes(chunk_samples, file->atracks[track].channels);
222 /* Get the buffer to read into. */
223 if(codec->write_buffer && codec->write_size < chunk_samples)
225 free(codec->write_buffer);
226 codec->write_buffer = 0;
229 if(!codec->write_buffer)
231 codec->write_size = chunk_samples;
232 codec->write_buffer = malloc(sizeof(int16_t) * codec->write_size);
235 if(codec->read_buffer && codec->read_size < chunk_bytes)
237 free(codec->read_buffer);
238 codec->read_buffer = 0;
241 if(!codec->read_buffer)
243 codec->read_size = chunk_bytes;
244 codec->read_buffer = malloc(codec->read_size);
247 /* codec->work_size now holds the number of samples in the last chunk */
248 /* codec->read_size now holds number of bytes in the last read buffer */
250 /* Read the entire chunk regardless of where the desired sample range starts. */
251 result = quicktime_read_chunk(file, (char*)codec->read_buffer, track, chunk, 0, chunk_bytes);
253 /* Now decode the chunk, one block at a time, until the total samples in the chunk */
258 block_ptr = codec->read_buffer;
259 for(j = 0; j < file->atracks[track].channels; j++)
262 wmx2_decode_block(&(file->atracks[track]), codec->write_buffer, block_ptr, chunk_samples);
264 block_ptr += chunk_bytes / file->atracks[track].channels;
267 codec->buffer_channel = channel;
268 codec->chunk = chunk;
274 /* =================================== public for wmx2 */
276 static int decode(quicktime_t *file,
284 long chunk, chunk_sample, chunk_samples;
285 long i, chunk_start, chunk_end;
286 quicktime_trak_t *trak = file->atracks[track].track;
287 quicktime_wmx2_codec_t *codec =
288 ((quicktime_codec_t*)file->atracks[track].codec)->priv;
290 /* Get the first chunk with this routine and then increase the chunk number. */
291 quicktime_chunk_of_sample(&chunk_sample, &chunk, trak, file->atracks[track].current_position);
293 /* Read chunks until the output is full. */
294 for(i = 0; i < samples && !result; )
296 /* Get chunk we're on. */
297 chunk_samples = quicktime_chunk_samples(trak, chunk);
299 if(!codec->write_buffer ||
300 codec->chunk != chunk ||
301 codec->buffer_channel != channel)
303 /* read a new chunk if necessary */
304 result = wmx2_decode_chunk(file, track, chunk, channel);
307 /* Get boundaries from the chunk */
309 if(chunk_sample < file->atracks[track].current_position)
310 chunk_start = file->atracks[track].current_position - chunk_sample;
312 chunk_end = chunk_samples;
313 if(chunk_sample + chunk_end > file->atracks[track].current_position + samples)
314 chunk_end = file->atracks[track].current_position + samples - chunk_sample;
316 /* Read from the chunk */
319 while(chunk_start < chunk_end)
321 output_i[i++] = codec->write_buffer[chunk_start++];
327 while(chunk_start < chunk_end)
329 output_f[i++] = (float)codec->write_buffer[chunk_start++] / 32767;
334 chunk_sample += chunk_samples;
340 static int encode(quicktime_t *file,
349 quicktime_audio_map_t *track_map = &(file->atracks[track]);
350 quicktime_wmx2_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
351 quicktime_trak_t *trak = track_map->track;
353 unsigned char *output_ptr;
354 quicktime_atom_t chunk_atom;
356 /* Get buffer sizes */
357 if(codec->write_buffer && codec->write_size < samples * track_map->channels)
359 /* Create new buffer */
360 long new_size = samples * track_map->channels;
361 int16_t *new_buffer = malloc(sizeof(int16_t) * new_size);
364 free(codec->write_buffer);
365 codec->write_buffer = new_buffer;
366 codec->write_size = new_size;
369 if(!codec->write_buffer)
371 /* No buffer in the first place. */
372 codec->write_size = samples * track_map->channels;
373 codec->write_buffer = malloc(sizeof(int16_t) * codec->write_size);
376 /* Get output size */
377 chunk_bytes = wmx2_samples_to_bytes(samples, track_map->channels);
378 if(codec->read_buffer && codec->read_size < chunk_bytes)
380 free(codec->read_buffer);
381 codec->read_buffer = 0;
384 if(!codec->read_buffer)
386 codec->read_buffer = malloc(chunk_bytes);
387 codec->read_size = chunk_bytes;
390 if(!codec->last_samples)
392 codec->last_samples = malloc(sizeof(int) * track_map->channels);
393 for(i = 0; i < track_map->channels; i++)
395 codec->last_samples[i] = 0;
399 if(!codec->last_indexes)
401 codec->last_indexes = malloc(sizeof(int) * track_map->channels);
402 for(i = 0; i < track_map->channels; i++)
404 codec->last_indexes[i] = 0;
408 /* Arm the input buffer */
409 step = track_map->channels;
410 for(j = 0; j < track_map->channels; j++)
412 input_ptr = codec->write_buffer + j;
416 for(i = 0; i < samples; i++)
418 *input_ptr = input_i[j][i];
425 for(i = 0; i < samples; i++)
427 *input_ptr = (int16_t)(input_f[j][i] * 32767);
433 /* Encode from the input buffer to the read_buffer. */
434 input_ptr = codec->write_buffer;
435 output_ptr = codec->read_buffer;
437 for(j = 0; j < track_map->channels; j++)
439 wmx2_encode_block(track_map, output_ptr, input_ptr + j, track_map->channels, j, samples);
441 output_ptr += chunk_bytes / track_map->channels;
446 //int offset = quicktime_position(file);
447 quicktime_write_chunk_header(file, trak, &chunk_atom);
448 result = !quicktime_write_data(file, (char*)codec->read_buffer, chunk_bytes);
449 quicktime_write_chunk_footer(file, trak,
450 track_map->current_chunk, &chunk_atom, 1);
451 track_map->current_chunk++;
457 static void delete_codec(quicktime_audio_map_t *atrack)
459 quicktime_wmx2_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
460 if(codec->write_buffer) free(codec->write_buffer);
461 if(codec->read_buffer) free(codec->read_buffer);
462 if(codec->last_samples) free(codec->last_samples);
463 if(codec->last_indexes) free(codec->last_indexes);
464 codec->last_samples = 0;
465 codec->last_indexes = 0;
466 codec->read_buffer = 0;
467 codec->write_buffer = 0;
469 codec->buffer_channel = 0; /* Channel of work buffer */
470 codec->write_size = 0; /* Size of work buffer */
471 codec->read_size = 0;
475 void quicktime_init_codec_wmx2(quicktime_audio_map_t *atrack)
477 quicktime_codec_t *codec_base = (quicktime_codec_t*)atrack->codec;
479 /* Init public items */
480 codec_base->priv = calloc(1, sizeof(quicktime_wmx2_codec_t));
481 codec_base->delete_acodec = delete_codec;
482 codec_base->decode_video = 0;
483 codec_base->encode_video = 0;
484 codec_base->decode_audio = decode;
485 codec_base->encode_audio = encode;
486 codec_base->fourcc = QUICKTIME_WMX2;
487 codec_base->title = "IMA4 on steroids";
488 codec_base->desc = "IMA4 on steroids. (Not standardized)";
489 codec_base->wav_id = 0x11;