1 // Utility functions for vbr audio
3 #include "funcprotos.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
)
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
)
29 if(ptr
->output_buffer
)
31 for(i
= 0; i
< ptr
->channels
; i
++)
32 free(ptr
->output_buffer
[i
]);
33 free(ptr
->output_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
)
67 "quicktime_align_vbr: can't decode more than %p samples at a time.\n",
74 int quicktime_align_vbr(quicktime_audio_map_t
*atrack
,
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
,
90 ptr
->buffer_end
= start_time
;
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
,
105 int size
= quicktime_sample_size(trak
, vbr
->sample
);
106 int new_allocation
= vbr
->input_size
+ size
;
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
;
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
)
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
,
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
];
153 if(vbr
->buffer_ptr
>= MAX_VBR_BUFFER
)
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
,
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
)
181 void quicktime_copy_vbr_int16(quicktime_vbr_t
*vbr
,
182 int64_t start_position
,
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
)