comparison alacenc.c @ 7619:0753d03d232a libavcodec

alacenc: last few hunks approved by michael
author jai_menon
date Tue, 19 Aug 2008 17:20:41 +0000
parents 2b023daf5329
children 5c2299115d1f
comparison
equal deleted inserted replaced
7618:2b023daf5329 7619:0753d03d232a
215 s->interlacing_shift = 1; 215 s->interlacing_shift = 1;
216 break; 216 break;
217 } 217 }
218 } 218 }
219 219
220 static void alac_linear_predictor(AlacEncodeContext *s, int ch)
221 {
222 int i;
223 LPCContext lpc = s->lpc[ch];
224
225 if(lpc.lpc_order == 31) {
226 s->predictor_buf[0] = s->sample_buf[ch][0];
227
228 for(i=1; i<s->avctx->frame_size; i++)
229 s->predictor_buf[i] = s->sample_buf[ch][i] - s->sample_buf[ch][i-1];
230
231 return;
232 }
233
234 // generalised linear predictor
235
236 if(lpc.lpc_order > 0) {
237 int32_t *samples = s->sample_buf[ch];
238 int32_t *residual = s->predictor_buf;
239
240 // generate warm-up samples
241 residual[0] = samples[0];
242 for(i=1;i<=lpc.lpc_order;i++)
243 residual[i] = samples[i] - samples[i-1];
244
245 // perform lpc on remaining samples
246 for(i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) {
247 int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
248
249 for (j = 0; j < lpc.lpc_order; j++) {
250 sum += (samples[lpc.lpc_order-j] - samples[0]) *
251 lpc.lpc_coeff[j];
252 }
253
254 sum >>= lpc.lpc_quant;
255 sum += samples[0];
256 residual[i] = samples[lpc.lpc_order+1] - sum;
257 res_val = residual[i];
258
259 if(res_val) {
260 int index = lpc.lpc_order - 1;
261 int neg = (res_val < 0);
262
263 while(index >= 0 && (neg ? (res_val < 0):(res_val > 0))) {
264 int val = samples[0] - samples[lpc.lpc_order - index];
265 int sign = (val ? FFSIGN(val) : 0);
266
267 if(neg)
268 sign*=-1;
269
270 lpc.lpc_coeff[index] -= sign;
271 val *= sign;
272 res_val -= ((val >> lpc.lpc_quant) *
273 (lpc.lpc_order - index));
274 index--;
275 }
276 }
277 samples++;
278 }
279 }
280 }
281
282 static void alac_entropy_coder(AlacEncodeContext *s)
283 {
284 unsigned int history = s->rc.initial_history;
285 int sign_modifier = 0, i, k;
286 int32_t *samples = s->predictor_buf;
287
288 for(i=0;i < s->avctx->frame_size;) {
289 int x;
290
291 k = av_log2((history >> 9) + 3);
292
293 x = -2*(*samples)-1;
294 x ^= (x>>31);
295
296 samples++;
297 i++;
298
299 encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
300
301 history += x * s->rc.history_mult
302 - ((history * s->rc.history_mult) >> 9);
303
304 sign_modifier = 0;
305 if(x > 0xFFFF)
306 history = 0xFFFF;
307
308 if((history < 128) && (i < s->avctx->frame_size)) {
309 unsigned int block_size = 0;
310
311 k = 7 - av_log2(history) + ((history + 16) >> 6);
312
313 while((*samples == 0) && (i < s->avctx->frame_size)) {
314 samples++;
315 i++;
316 block_size++;
317 }
318 encode_scalar(s, block_size, k, 16);
319
320 sign_modifier = (block_size <= 0xFFFF);
321
322 history = 0;
323 }
324
325 }
326 }
327
220 static void write_compressed_frame(AlacEncodeContext *s) 328 static void write_compressed_frame(AlacEncodeContext *s)
221 { 329 {
222 int i, j; 330 int i, j;
223 331
224 /* only simple mid/side decorrelation supported as of now */ 332 /* only simple mid/side decorrelation supported as of now */
293 AV_WB8(alac_extradata+18, s->rc.history_mult); 401 AV_WB8(alac_extradata+18, s->rc.history_mult);
294 AV_WB8(alac_extradata+19, s->rc.initial_history); 402 AV_WB8(alac_extradata+19, s->rc.initial_history);
295 AV_WB8(alac_extradata+20, s->rc.k_modifier); 403 AV_WB8(alac_extradata+20, s->rc.k_modifier);
296 } 404 }
297 405
406 s->min_prediction_order = DEFAULT_MIN_PRED_ORDER;
298 if(avctx->min_prediction_order >= 0) { 407 if(avctx->min_prediction_order >= 0) {
299 if(avctx->min_prediction_order < MIN_LPC_ORDER || 408 if(avctx->min_prediction_order < MIN_LPC_ORDER ||
300 avctx->min_prediction_order > MAX_LPC_ORDER) { 409 avctx->min_prediction_order > MAX_LPC_ORDER) {
301 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order); 410 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order);
302 return -1; 411 return -1;
303 } 412 }
304 413
305 s->min_prediction_order = avctx->min_prediction_order; 414 s->min_prediction_order = avctx->min_prediction_order;
306 } 415 }
307 416
417 s->max_prediction_order = DEFAULT_MAX_PRED_ORDER;
308 if(avctx->max_prediction_order >= 0) { 418 if(avctx->max_prediction_order >= 0) {
309 if(avctx->max_prediction_order < MIN_LPC_ORDER || 419 if(avctx->max_prediction_order < MIN_LPC_ORDER ||
310 avctx->max_prediction_order > MAX_LPC_ORDER) { 420 avctx->max_prediction_order > MAX_LPC_ORDER) {
311 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order); 421 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order);
312 return -1; 422 return -1;