allow ffmpeg video to resample curr_pos, add bluray format
[goodguy/history.git] / cinelerra-5.0 / quicktime / wmx2.c
1 #include "qtprivate.h"
2 #include "quicktime.h"
3 #include "funcprotos.h"
4 #include "wmx2.h"
5
6 typedef struct
7 {
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. */
13
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 */
18
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;
24
25 static int quicktime_wmx2_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 quicktime_wmx2_index[16] = 
39 {
40     -1, -1, -1, -1, 2, 4, 6, 8,
41     -1, -1, -1, -1, 2, 4, 6, 8
42 };
43
44 /* ================================== private for wmx2 */
45 #define HEADER_SIZE 3
46
47 static int wmx2_decode_sample(int *predictor, int *nibble, int *index, int *step)
48 {
49         int difference, sign;
50
51 /* Get new index value */
52         *index += quicktime_wmx2_index[*nibble];
53
54         if(*index < 0) *index = 0; 
55         else 
56         if(*index > 88) *index = 88;
57
58 /* Get sign and magnitude from *nibble */
59         sign = *nibble & 8;
60         *nibble = *nibble & 7;
61
62 /* Get difference */
63         difference = *step >> 3;
64         if(*nibble & 4) difference += *step;
65         if(*nibble & 2) difference += *step >> 1;
66         if(*nibble & 1) difference += *step >> 2;
67
68 /* Predict value */
69         if(sign) 
70         *predictor -= difference;
71         else 
72         *predictor += difference;
73
74         if(*predictor > 32767) *predictor = 32767;
75         else
76         if(*predictor < -32768) *predictor = -32768;
77
78 /* Update the step value */
79         *step = quicktime_wmx2_step[*index];
80
81         return 0;
82 }
83
84
85 static int wmx2_decode_block(quicktime_audio_map_t *atrack, int16_t *output, unsigned char *input, int samples)
86 {
87         int predictor, index, step;
88         int nibble, nibble_count;
89         int16_t *output_end = output + samples;
90
91 /* Get the chunk header */
92         predictor = *input++ << 8;
93         predictor |= *input++;
94         if(predictor & 0x8000) predictor -= 0x10000;
95         index = *input++;
96         if(index > 88) index = 88;
97
98 /*printf("input %d %d\n", predictor, index); */
99         step = quicktime_wmx2_step[index];
100
101 /* Read the input buffer sequentially, one nibble at a time */
102         nibble_count = 0;
103         while(output < output_end)
104         {
105                 nibble = nibble_count ? (*input++  >> 4) & 0x0f : *input & 0x0f;
106
107                 wmx2_decode_sample(&predictor, &nibble, &index, &step);
108                 *output++ = predictor;
109
110                 nibble_count ^= 1;
111         }
112         return 0;
113 }
114
115 static int wmx2_encode_sample(int *last_sample, int *last_index, int *nibble, int next_sample)
116 {
117         int difference, new_difference, mask, step;
118
119         difference = next_sample - *last_sample;
120         *nibble = 0;
121         step = quicktime_wmx2_step[*last_index];
122         new_difference = step >> 3;
123
124         if(difference < 0)
125         {
126                 *nibble = 8;
127                 difference = -difference;
128         }
129
130         mask = 4;
131         while(mask)
132         {
133                 if(difference >= step)
134                 {
135                         *nibble |= mask;
136                         difference -= step;
137                         new_difference += step;
138                 }
139
140                 step >>= 1;
141                 mask >>= 1;
142         }
143
144         if(*nibble & 8)
145                 *last_sample -= new_difference;
146         else
147                 *last_sample += new_difference;
148
149         if(*last_sample > 32767) *last_sample = 32767;
150         else
151         if(*last_sample < -32767) *last_sample = -32767;
152
153         *last_index += quicktime_wmx2_index[*nibble];
154
155         if(*last_index < 0) *last_index = 0;
156         else
157         if(*last_index > 88) *last_index= 88;
158
159         return 0;
160 }
161
162 static int wmx2_encode_block(quicktime_audio_map_t *atrack, unsigned char *output, int16_t *input, int step, int channel, int samples)
163 {
164         quicktime_wmx2_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
165         int i, nibble_count = 0, nibble, header;
166
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);
177
178         for(i = 0; i < samples; i++)
179         {
180                 wmx2_encode_sample(&(codec->last_samples[channel]), 
181                                                         &(codec->last_indexes[channel]), 
182                                                         &nibble, 
183                                                         *input);
184
185                 if(nibble_count)
186                         *output++ |= (nibble << 4);
187                 else
188                         *output = nibble;
189
190                 input += step;
191                 nibble_count ^= 1;
192         }
193
194         return 0;
195 }
196
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)
200 {
201         long bytes = samples / 2;
202         if(bytes * 2 < samples) bytes++;
203         bytes *= channels;
204         bytes += HEADER_SIZE * channels;
205         return bytes;
206 }
207
208 /* Decode the chunk into the work buffer */
209 static int wmx2_decode_chunk(quicktime_t *file, int track, long chunk, int channel)
210 {
211         int j, result;
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;
217
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);
221
222 /* Get the buffer to read into. */
223         if(codec->write_buffer && codec->write_size < chunk_samples)
224         {
225                 free(codec->write_buffer);
226                 codec->write_buffer = 0;
227         }
228
229         if(!codec->write_buffer)
230         {
231                 codec->write_size = chunk_samples;
232                 codec->write_buffer = malloc(sizeof(int16_t) * codec->write_size);
233         }
234
235         if(codec->read_buffer && codec->read_size < chunk_bytes)
236         {
237                 free(codec->read_buffer);
238                 codec->read_buffer = 0;
239         }
240
241         if(!codec->read_buffer)
242         {
243                 codec->read_size = chunk_bytes;
244                 codec->read_buffer = malloc(codec->read_size);
245         }
246
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 */
249
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);
252
253 /* Now decode the chunk, one block at a time, until the total samples in the chunk */
254 /* is reached. */
255
256         if(!result)
257         {
258                 block_ptr = codec->read_buffer;
259                 for(j = 0; j < file->atracks[track].channels; j++)
260                 {
261                         if(j == channel)
262                                 wmx2_decode_block(&(file->atracks[track]), codec->write_buffer, block_ptr, chunk_samples);
263
264                         block_ptr += chunk_bytes / file->atracks[track].channels;
265                 }
266         }
267         codec->buffer_channel = channel;
268         codec->chunk = chunk;
269
270         return result;
271 }
272
273
274 /* =================================== public for wmx2 */
275
276 static int decode(quicktime_t *file, 
277                                         int16_t *output_i, 
278                                         float *output_f,
279                                         long samples, 
280                                         int track, 
281                                         int channel)
282 {
283         int result = 0;
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;
289
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);
292
293 /* Read chunks until the output is full. */
294         for(i = 0; i < samples && !result; )
295         {
296 /* Get chunk we're on. */
297                 chunk_samples = quicktime_chunk_samples(trak, chunk);
298
299                 if(!codec->write_buffer ||
300                         codec->chunk != chunk ||
301                         codec->buffer_channel != channel)
302                 {
303 /* read a new chunk if necessary */
304                         result = wmx2_decode_chunk(file, track, chunk, channel);
305                 }
306
307 /* Get boundaries from the chunk */
308                 chunk_start = 0;
309                 if(chunk_sample < file->atracks[track].current_position)
310                         chunk_start = file->atracks[track].current_position - chunk_sample;
311
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;
315
316 /* Read from the chunk */
317                 if(output_i)
318                 {
319                         while(chunk_start < chunk_end)
320                         {
321                                 output_i[i++] = codec->write_buffer[chunk_start++];
322                         }
323                 }
324                 else
325                 if(output_f)
326                 {
327                         while(chunk_start < chunk_end)
328                         {
329                                 output_f[i++] = (float)codec->write_buffer[chunk_start++] / 32767;
330                         }
331                 }
332
333                 chunk++;
334                 chunk_sample += chunk_samples;
335         }
336
337         return result;
338 }
339
340 static int encode(quicktime_t *file, 
341                                                 int16_t **input_i, 
342                                                 float **input_f, 
343                                                 int track, 
344                                                 long samples)
345 {
346         int result = 0;
347         long i, j, step;
348         long chunk_bytes;
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;
352         int16_t *input_ptr;
353         unsigned char *output_ptr;
354         quicktime_atom_t chunk_atom;
355
356 /* Get buffer sizes */
357         if(codec->write_buffer && codec->write_size < samples * track_map->channels)
358         {
359 /* Create new buffer */
360                 long new_size = samples * track_map->channels;
361                 int16_t *new_buffer = malloc(sizeof(int16_t) * new_size);
362
363 /* Swap pointers. */
364                 free(codec->write_buffer);
365                 codec->write_buffer = new_buffer;
366                 codec->write_size = new_size;
367         }
368         else
369         if(!codec->write_buffer)
370         {
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);
374         }
375
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)
379         {
380                 free(codec->read_buffer);
381                 codec->read_buffer = 0;
382         }
383
384         if(!codec->read_buffer)
385         {
386                 codec->read_buffer = malloc(chunk_bytes);
387                 codec->read_size = chunk_bytes;
388         }
389
390         if(!codec->last_samples)
391         {
392                 codec->last_samples = malloc(sizeof(int) * track_map->channels);
393                 for(i = 0; i < track_map->channels; i++)
394                 {
395                         codec->last_samples[i] = 0;
396                 }
397         }
398
399         if(!codec->last_indexes)
400         {
401                 codec->last_indexes = malloc(sizeof(int) * track_map->channels);
402                 for(i = 0; i < track_map->channels; i++)
403                 {
404                         codec->last_indexes[i] = 0;
405                 }
406         }
407
408 /* Arm the input buffer */
409         step = track_map->channels;
410         for(j = 0; j < track_map->channels; j++)
411         {
412                 input_ptr = codec->write_buffer + j;
413
414                 if(input_i)
415                 {
416                         for(i = 0; i < samples; i++)
417                         {
418                                 *input_ptr = input_i[j][i];
419                                 input_ptr += step;
420                         }
421                 }
422                 else
423                 if(input_f)
424                 {
425                         for(i = 0; i < samples; i++)
426                         {
427                                 *input_ptr = (int16_t)(input_f[j][i] * 32767);
428                                 input_ptr += step;
429                         }
430                 }
431         }
432
433 /* Encode from the input buffer to the read_buffer. */
434         input_ptr = codec->write_buffer;
435         output_ptr = codec->read_buffer;
436
437         for(j = 0; j < track_map->channels; j++)
438         {
439                 wmx2_encode_block(track_map, output_ptr, input_ptr + j, track_map->channels, j, samples);
440
441                 output_ptr += chunk_bytes / track_map->channels;
442         }
443
444         if(samples)
445         {
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++;
452         }
453
454         return result;
455 }
456
457 static void delete_codec(quicktime_audio_map_t *atrack)
458 {
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;
468         codec->chunk = 0;
469         codec->buffer_channel = 0; /* Channel of work buffer */
470         codec->write_size = 0;          /* Size of work buffer */
471         codec->read_size = 0;
472         free(codec);
473 }
474
475 void quicktime_init_codec_wmx2(quicktime_audio_map_t *atrack)
476 {
477         quicktime_codec_t *codec_base = (quicktime_codec_t*)atrack->codec;
478
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;
490 }