comparison flacenc.c @ 7588:f6a1bff47a3b libavcodec

flacenc: Allow more flexible shift calculation in LPC.
author ramiro
date Sat, 16 Aug 2008 17:03:57 +0000
parents 85ab7655ad4d
children 8b695a99e8df
comparison
equal deleted inserted replaced
7587:6248b6b16e47 7588:f6a1bff47a3b
678 678
679 /** 679 /**
680 * Quantize LPC coefficients 680 * Quantize LPC coefficients
681 */ 681 */
682 static void quantize_lpc_coefs(double *lpc_in, int order, int precision, 682 static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
683 int32_t *lpc_out, int *shift) 683 int32_t *lpc_out, int *shift, int max_shift, int zero_shift)
684 { 684 {
685 int i; 685 int i;
686 double cmax, error; 686 double cmax, error;
687 int32_t qmax; 687 int32_t qmax;
688 int sh; 688 int sh;
695 for(i=0; i<order; i++) { 695 for(i=0; i<order; i++) {
696 cmax= FFMAX(cmax, fabs(lpc_in[i])); 696 cmax= FFMAX(cmax, fabs(lpc_in[i]));
697 } 697 }
698 698
699 /* if maximum value quantizes to zero, return all zeros */ 699 /* if maximum value quantizes to zero, return all zeros */
700 if(cmax * (1 << MAX_LPC_SHIFT) < 1.0) { 700 if(cmax * (1 << max_shift) < 1.0) {
701 *shift = 0; 701 *shift = zero_shift;
702 memset(lpc_out, 0, sizeof(int32_t) * order); 702 memset(lpc_out, 0, sizeof(int32_t) * order);
703 return; 703 return;
704 } 704 }
705 705
706 /* calculate level shift which scales max coeff to available bits */ 706 /* calculate level shift which scales max coeff to available bits */
707 sh = MAX_LPC_SHIFT; 707 sh = max_shift;
708 while((cmax * (1 << sh) > qmax) && (sh > 0)) { 708 while((cmax * (1 << sh) > qmax) && (sh > 0)) {
709 sh--; 709 sh--;
710 } 710 }
711 711
712 /* since negative shift values are unsupported in decoder, scale down 712 /* since negative shift values are unsupported in decoder, scale down
743 } 743 }
744 744
745 /** 745 /**
746 * Calculate LPC coefficients for multiple orders 746 * Calculate LPC coefficients for multiple orders
747 */ 747 */
748 static int lpc_calc_coefs(FlacEncodeContext *s, 748 static int lpc_calc_coefs(DSPContext *s,
749 const int32_t *samples, int blocksize, int max_order, 749 const int32_t *samples, int blocksize, int max_order,
750 int precision, int32_t coefs[][MAX_LPC_ORDER], 750 int precision, int32_t coefs[][MAX_LPC_ORDER],
751 int *shift, int use_lpc, int omethod) 751 int *shift, int use_lpc, int omethod, int max_shift, int zero_shift)
752 { 752 {
753 double autoc[MAX_LPC_ORDER+1]; 753 double autoc[MAX_LPC_ORDER+1];
754 double ref[MAX_LPC_ORDER]; 754 double ref[MAX_LPC_ORDER];
755 double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER]; 755 double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
756 int i, j, pass; 756 int i, j, pass;
757 int opt_order; 757 int opt_order;
758 758
759 assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER); 759 assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER);
760 760
761 if(use_lpc == 1){ 761 if(use_lpc == 1){
762 s->dsp.flac_compute_autocorr(samples, blocksize, max_order, autoc); 762 s->flac_compute_autocorr(samples, blocksize, max_order, autoc);
763 763
764 compute_lpc_coefs(autoc, max_order, lpc, ref); 764 compute_lpc_coefs(autoc, max_order, lpc, ref);
765 }else{ 765 }else{
766 LLSModel m[2]; 766 LLSModel m[2];
767 double var[MAX_LPC_ORDER+1], weight; 767 double var[MAX_LPC_ORDER+1], weight;
802 opt_order = max_order; 802 opt_order = max_order;
803 803
804 if(omethod == ORDER_METHOD_EST) { 804 if(omethod == ORDER_METHOD_EST) {
805 opt_order = estimate_best_order(ref, max_order); 805 opt_order = estimate_best_order(ref, max_order);
806 i = opt_order-1; 806 i = opt_order-1;
807 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]); 807 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
808 } else { 808 } else {
809 for(i=0; i<max_order; i++) { 809 for(i=0; i<max_order; i++) {
810 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]); 810 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
811 } 811 }
812 } 812 }
813 813
814 return opt_order; 814 return opt_order;
815 } 815 }
1040 } 1040 }
1041 return bits[sub->order]; 1041 return bits[sub->order];
1042 } 1042 }
1043 1043
1044 /* LPC */ 1044 /* LPC */
1045 opt_order = lpc_calc_coefs(ctx, smp, n, max_order, precision, coefs, shift, ctx->options.use_lpc, omethod); 1045 opt_order = lpc_calc_coefs(&ctx->dsp, smp, n, max_order, precision, coefs,
1046 shift, ctx->options.use_lpc, omethod, MAX_LPC_SHIFT, 0);
1046 1047
1047 if(omethod == ORDER_METHOD_2LEVEL || 1048 if(omethod == ORDER_METHOD_2LEVEL ||
1048 omethod == ORDER_METHOD_4LEVEL || 1049 omethod == ORDER_METHOD_4LEVEL ||
1049 omethod == ORDER_METHOD_8LEVEL) { 1050 omethod == ORDER_METHOD_8LEVEL) {
1050 int levels = 1 << omethod; 1051 int levels = 1 << omethod;