Mercurial > libavcodec.hg
comparison flacenc.c @ 3477:30ac8a424448 libavcodec
Add lpc order search. This creates new compression levels 6 to 12.
author | jbr |
---|---|
date | Sun, 16 Jul 2006 13:34:47 +0000 |
parents | fa545ed305c9 |
children | 5a71102c2770 |
comparison
equal
deleted
inserted
replaced
3476:33a177588139 | 3477:30ac8a424448 |
---|---|
219 s->options.compression_level = avctx->compression_level; | 219 s->options.compression_level = avctx->compression_level; |
220 } | 220 } |
221 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", s->options.compression_level); | 221 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", s->options.compression_level); |
222 | 222 |
223 level= s->options.compression_level; | 223 level= s->options.compression_level; |
224 if(level > 5) { | 224 if(level > 12) { |
225 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n", | 225 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n", |
226 s->options.compression_level); | 226 s->options.compression_level); |
227 return -1; | 227 return -1; |
228 } | 228 } |
229 | 229 |
230 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105})[level]; | 230 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level]; |
231 s->options.use_lpc = ((int[]){ 0, 0, 0, 1, 1, 1})[level]; | 231 s->options.use_lpc = ((int[]){ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level]; |
232 s->options.min_prediction_order= ((int[]){ 2, 0, 0, 1, 1, 1})[level]; | 232 s->options.min_prediction_order= ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level]; |
233 s->options.max_prediction_order= ((int[]){ 3, 4, 4, 6, 8, 8})[level]; | 233 s->options.max_prediction_order= ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level]; |
234 s->options.prediction_order_method = ORDER_METHOD_EST; | 234 s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST, |
235 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0})[level]; | 235 ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST, |
236 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8})[level]; | 236 ORDER_METHOD_2LEVEL, ORDER_METHOD_4LEVEL, ORDER_METHOD_4LEVEL, |
237 ORDER_METHOD_8LEVEL, ORDER_METHOD_SEARCH, ORDER_METHOD_8LEVEL, | |
238 ORDER_METHOD_SEARCH})[level]; | |
239 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level]; | |
240 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level]; | |
237 | 241 |
238 /* set compression option overrides from AVCodecContext */ | 242 /* set compression option overrides from AVCodecContext */ |
239 if(avctx->use_lpc >= 0) { | 243 if(avctx->use_lpc >= 0) { |
240 s->options.use_lpc = clip(avctx->use_lpc, 0, 11); | 244 s->options.use_lpc = clip(avctx->use_lpc, 0, 11); |
241 } | 245 } |
292 avctx->prediction_order_method); | 296 avctx->prediction_order_method); |
293 return -1; | 297 return -1; |
294 } | 298 } |
295 s->options.prediction_order_method = avctx->prediction_order_method; | 299 s->options.prediction_order_method = avctx->prediction_order_method; |
296 } | 300 } |
297 switch(avctx->prediction_order_method) { | 301 switch(s->options.prediction_order_method) { |
298 case ORDER_METHOD_EST: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", | 302 case ORDER_METHOD_EST: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", |
299 "estimate"); break; | 303 "estimate"); break; |
300 case ORDER_METHOD_2LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", | 304 case ORDER_METHOD_2LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", |
301 "2-level"); break; | 305 "2-level"); break; |
302 case ORDER_METHOD_4LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", | 306 case ORDER_METHOD_4LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", |
726 /** | 730 /** |
727 * Calculate LPC coefficients for multiple orders | 731 * Calculate LPC coefficients for multiple orders |
728 */ | 732 */ |
729 static int lpc_calc_coefs(const int32_t *samples, int blocksize, int max_order, | 733 static int lpc_calc_coefs(const int32_t *samples, int blocksize, int max_order, |
730 int precision, int32_t coefs[][MAX_LPC_ORDER], | 734 int precision, int32_t coefs[][MAX_LPC_ORDER], |
731 int *shift, int use_lpc) | 735 int *shift, int use_lpc, int omethod) |
732 { | 736 { |
733 double autoc[MAX_LPC_ORDER+1]; | 737 double autoc[MAX_LPC_ORDER+1]; |
734 double ref[MAX_LPC_ORDER]; | 738 double ref[MAX_LPC_ORDER]; |
735 double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER]; | 739 double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER]; |
736 int i, j, pass; | 740 int i, j, pass; |
774 ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000; | 778 ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000; |
775 } | 779 } |
776 for(i=max_order-1; i>0; i--) | 780 for(i=max_order-1; i>0; i--) |
777 ref[i] = ref[i-1] - ref[i]; | 781 ref[i] = ref[i-1] - ref[i]; |
778 } | 782 } |
779 opt_order = estimate_best_order(ref, max_order); | 783 opt_order = max_order; |
780 | 784 |
781 i = opt_order-1; | 785 if(omethod == ORDER_METHOD_EST) { |
782 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]); | 786 opt_order = estimate_best_order(ref, max_order); |
787 i = opt_order-1; | |
788 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]); | |
789 } else { | |
790 for(i=0; i<max_order; i++) { | |
791 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]); | |
792 } | |
793 } | |
783 | 794 |
784 return opt_order; | 795 return opt_order; |
785 } | 796 } |
786 | 797 |
787 | 798 |
837 } | 848 } |
838 | 849 |
839 static int encode_residual(FlacEncodeContext *ctx, int ch) | 850 static int encode_residual(FlacEncodeContext *ctx, int ch) |
840 { | 851 { |
841 int i, n; | 852 int i, n; |
842 int min_order, max_order, opt_order, precision; | 853 int min_order, max_order, opt_order, precision, omethod; |
843 int min_porder, max_porder; | 854 int min_porder, max_porder; |
844 FlacFrame *frame; | 855 FlacFrame *frame; |
845 FlacSubframe *sub; | 856 FlacSubframe *sub; |
846 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER]; | 857 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER]; |
847 int shift[MAX_LPC_ORDER]; | 858 int shift[MAX_LPC_ORDER]; |
873 min_order = ctx->options.min_prediction_order; | 884 min_order = ctx->options.min_prediction_order; |
874 max_order = ctx->options.max_prediction_order; | 885 max_order = ctx->options.max_prediction_order; |
875 min_porder = ctx->options.min_partition_order; | 886 min_porder = ctx->options.min_partition_order; |
876 max_porder = ctx->options.max_partition_order; | 887 max_porder = ctx->options.max_partition_order; |
877 precision = ctx->options.lpc_coeff_precision; | 888 precision = ctx->options.lpc_coeff_precision; |
889 omethod = ctx->options.prediction_order_method; | |
878 | 890 |
879 /* FIXED */ | 891 /* FIXED */ |
880 if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) { | 892 if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) { |
881 uint32_t bits[MAX_FIXED_ORDER+1]; | 893 uint32_t bits[MAX_FIXED_ORDER+1]; |
882 if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER; | 894 if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER; |
900 } | 912 } |
901 return bits[sub->order]; | 913 return bits[sub->order]; |
902 } | 914 } |
903 | 915 |
904 /* LPC */ | 916 /* LPC */ |
905 sub->order = lpc_calc_coefs(smp, n, max_order, precision, coefs, shift, ctx->options.use_lpc); | 917 opt_order = lpc_calc_coefs(smp, n, max_order, precision, coefs, shift, ctx->options.use_lpc, omethod); |
918 | |
919 if(omethod == ORDER_METHOD_2LEVEL || | |
920 omethod == ORDER_METHOD_4LEVEL || | |
921 omethod == ORDER_METHOD_8LEVEL) { | |
922 int levels = 1 << omethod; | |
923 uint32_t bits[levels]; | |
924 int order; | |
925 int opt_index = levels-1; | |
926 opt_order = max_order-1; | |
927 bits[opt_index] = UINT32_MAX; | |
928 for(i=levels-1; i>=0; i--) { | |
929 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1; | |
930 if(order < 0) order = 0; | |
931 encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]); | |
932 bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder, | |
933 res, n, order+1, sub->obits, precision); | |
934 if(bits[i] < bits[opt_index]) { | |
935 opt_index = i; | |
936 opt_order = order; | |
937 } | |
938 } | |
939 opt_order++; | |
940 } else if(omethod == ORDER_METHOD_SEARCH) { | |
941 // brute-force optimal order search | |
942 uint32_t bits[MAX_LPC_ORDER]; | |
943 opt_order = 0; | |
944 bits[0] = UINT32_MAX; | |
945 for(i=min_order-1; i<max_order; i++) { | |
946 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]); | |
947 bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder, | |
948 res, n, i+1, sub->obits, precision); | |
949 if(bits[i] < bits[opt_order]) { | |
950 opt_order = i; | |
951 } | |
952 } | |
953 opt_order++; | |
954 } | |
955 | |
956 sub->order = opt_order; | |
906 sub->type = FLAC_SUBFRAME_LPC; | 957 sub->type = FLAC_SUBFRAME_LPC; |
907 sub->type_code = sub->type | (sub->order-1); | 958 sub->type_code = sub->type | (sub->order-1); |
908 sub->shift = shift[sub->order-1]; | 959 sub->shift = shift[sub->order-1]; |
909 for(i=0; i<sub->order; i++) { | 960 for(i=0; i<sub->order; i++) { |
910 sub->coefs[i] = coefs[sub->order-1][i]; | 961 sub->coefs[i] = coefs[sub->order-1][i]; |