From f46b6cf6b8733bc3111754d3603a01a7e32c980b Mon Sep 17 00:00:00 2001 From: Nathan Caldwell Date: Sun, 16 Aug 2009 20:53:32 -0600 Subject: [PATCH] Remove float calculation to scale probabilities. --- libavcodec/lagarith.c | 61 ++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/libavcodec/lagarith.c b/libavcodec/lagarith.c index 61f20430c2..dcb442d8ef 100644 --- a/libavcodec/lagarith.c +++ b/libavcodec/lagarith.c @@ -125,7 +125,7 @@ static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb) { int i, j; int prob = 0; - double scale_factor = 0; + int scale_factor = 0; unsigned cumul_prob = 0; unsigned cumulative_target = 1; unsigned scaled_cumul_prob = 0; @@ -145,42 +145,43 @@ static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb) } /* Scale probabilities so cumulative probability is an even power of 2. */ - cumulative_target = 2 << av_log2(cumul_prob-1); + scale_factor = cumul_prob ? av_log2(cumul_prob - 1) + 1 : 0; + if (cumul_prob & (cumul_prob - 1)) { + for (i = 1; i < 257; i++) { + rac->prob[i] = ((uint64_t)rac->prob[i] << scale_factor) / cumul_prob; + scaled_cumul_prob += rac->prob[i]; + } - scale_factor = cumulative_target / (double) cumul_prob; + cumulative_target = 1 << scale_factor; - for (i = 1; i < 257; i++) { - rac->prob[i] = (unsigned) (rac->prob[i] * scale_factor); - scaled_cumul_prob += rac->prob[i]; - } - - scaled_cumul_prob = cumulative_target - scaled_cumul_prob; + if (scaled_cumul_prob > cumulative_target) { + av_log(rac->avctx, AV_LOG_ERROR, + "Scaled probabilities are larger than target!"); + return -1; + } - if (scaled_cumul_prob > INT_MAX) { - av_log(rac->avctx, AV_LOG_ERROR, - "Scaled cumulative probability is larger than target!"); - return -1; - } + scaled_cumul_prob = cumulative_target - scaled_cumul_prob; - for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) { - if (rac->prob[i]) { - rac->prob[i]++; - scaled_cumul_prob--; + for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) { + if (rac->prob[i]) { + rac->prob[i]++; + scaled_cumul_prob--; + } + /* Comment from reference source: + * if (b & 0x80 == 0) { // order of operations is 'wrong'; it has been left this way + * // since the compression change is negligable and fixing it + * // breaks backwards compatibilty + * b =- (signed int)b; + * b &= 0xFF; + * } else { + * b++; + * b &= 0x7f; + * } + */ } - /* Comment from reference source: - * if (b & 0x80 == 0) { // order of operations is 'wrong'; it has been left this way - * // since the compression change is negligable and fixing it - * // breaks backwards compatibilty - * b =- (signed int)b; - * b &= 0xFF; - * } else { - * b++; - * b &= 0x7f; - * } - */ } - rac->scale = cumulative_target ? av_log2(cumulative_target) : 0; + rac->scale = scale_factor; /* Fill probability array with cumulative probability for each symbol. */ for (i = 1; i < 257; i++) -- 2.11.4.GIT