Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / quicktime / vbraudio.c
blob284972e51d31b7f23cf17fb3848b086568b79d53
1 // Utility functions for vbr audio
3 #include "funcprotos.h"
4 #include "quicktime.h"
8 // Maximum samples to store in output buffer
9 #define MAX_VBR_BUFFER 0x200000
13 void quicktime_init_vbr(quicktime_vbr_t *ptr, int channels)
15 ptr->channels = channels;
16 if(!ptr->output_buffer)
18 int i;
19 ptr->output_buffer = calloc(channels, sizeof(double*));
20 for(i = 0; i < channels; i++)
21 ptr->output_buffer[i] = calloc(MAX_VBR_BUFFER, sizeof(double));
25 void quicktime_clear_vbr(quicktime_vbr_t *ptr)
27 int i;
29 if(ptr->output_buffer)
31 for(i = 0; i < ptr->channels; i++)
32 free(ptr->output_buffer[i]);
33 free(ptr->output_buffer);
36 if(ptr->input_buffer)
38 free(ptr->input_buffer);
42 void quicktime_vbr_set_channels(quicktime_vbr_t *ptr, int channels)
44 ptr->channels = channels;
47 int64_t quicktime_vbr_end(quicktime_vbr_t *ptr)
49 return ptr->buffer_end;
52 unsigned char* quicktime_vbr_input(quicktime_vbr_t *ptr)
54 return ptr->input_buffer;
57 int quicktime_vbr_input_size(quicktime_vbr_t *ptr)
59 return ptr->input_size;
62 static int limit_samples(int samples)
64 if(samples > MAX_VBR_BUFFER)
66 fprintf(stderr,
67 "quicktime_align_vbr: can't decode more than %p samples at a time.\n",
68 MAX_VBR_BUFFER);
69 return 1;
71 return 0;
74 int quicktime_align_vbr(quicktime_audio_map_t *atrack,
75 int samples)
77 quicktime_vbr_t *ptr = &atrack->vbr;
78 int64_t start_position = atrack->current_position;
80 if(limit_samples(samples)) return 1;
82 // Desired start point is outside existing range. Reposition buffer pointer
83 // to start time of nearest frame.
84 if(start_position < ptr->buffer_end - ptr->buffer_size ||
85 start_position > ptr->buffer_end)
87 int64_t start_time = start_position;
88 ptr->sample = quicktime_time_to_sample(&atrack->track->mdia.minf.stbl.stts,
89 &start_time);
90 ptr->buffer_end = start_time;
91 ptr->buffer_size = 0;
94 return 0;
97 int quicktime_read_vbr(quicktime_t *file,
98 quicktime_audio_map_t *atrack)
100 quicktime_vbr_t *vbr = &atrack->vbr;
101 quicktime_trak_t *trak = atrack->track;
102 int64_t offset = quicktime_sample_to_offset(file,
103 trak,
104 vbr->sample);
105 int size = quicktime_sample_size(trak, vbr->sample);
106 int new_allocation = vbr->input_size + size;
107 int result = 0;
109 if(vbr->input_allocation < new_allocation)
111 vbr->input_buffer = realloc(vbr->input_buffer, new_allocation);
112 vbr->input_allocation = new_allocation;
116 quicktime_set_position(file, offset);
117 result = !quicktime_read_data(file, vbr->input_buffer + vbr->input_size, size);
118 vbr->input_size += size;
119 vbr->sample++;
120 return result;
123 void quicktime_shift_vbr(quicktime_audio_map_t *atrack, int bytes)
125 quicktime_vbr_t *vbr = &atrack->vbr;
126 if(bytes >= vbr->input_size)
128 vbr->input_size = 0;
130 else
132 int i, j;
133 for(i = 0, j = bytes; j < vbr->input_size; i++, j++)
134 vbr->input_buffer[i] = vbr->input_buffer[j];
135 vbr->input_size -= bytes;
139 void quicktime_store_vbr_float(quicktime_audio_map_t *atrack,
140 float *samples,
141 int sample_count)
143 int i, j;
144 quicktime_vbr_t *vbr = &atrack->vbr;
145 for(i = 0; i < sample_count; i++)
147 for(j = 0; j < vbr->channels; j++)
149 vbr->output_buffer[j][vbr->buffer_ptr] =
150 samples[i * vbr->channels + j];
152 vbr->buffer_ptr++;
153 if(vbr->buffer_ptr >= MAX_VBR_BUFFER)
154 vbr->buffer_ptr = 0;
156 vbr->buffer_end += sample_count;
157 vbr->buffer_size += sample_count;
158 if(vbr->buffer_size > MAX_VBR_BUFFER) vbr->buffer_size = MAX_VBR_BUFFER;
161 void quicktime_copy_vbr_float(quicktime_vbr_t *vbr,
162 int64_t start_position,
163 int samples,
164 float *output,
165 int channel)
167 int i, j;
168 int input_ptr = vbr->buffer_ptr -
169 (vbr->buffer_end - start_position);
170 while(input_ptr < 0) input_ptr += MAX_VBR_BUFFER;
172 for(i = 0; i < samples; i++)
174 output[i] = vbr->output_buffer[channel][input_ptr++];
175 if(input_ptr >= MAX_VBR_BUFFER)
176 input_ptr = 0;
181 void quicktime_copy_vbr_int16(quicktime_vbr_t *vbr,
182 int64_t start_position,
183 int samples,
184 int16_t *output,
185 int channel)
187 int i, j;
188 int input_ptr = vbr->buffer_ptr -
189 (vbr->buffer_end - start_position);
190 while(input_ptr < 0) input_ptr += MAX_VBR_BUFFER;
192 for(i = 0; i < samples; i++)
194 output[i] = (int)(vbr->output_buffer[channel][input_ptr++] * 32767);
195 if(input_ptr >= MAX_VBR_BUFFER)
196 input_ptr = 0;