Commit 14bd2a9f authored by Loren Merritt's avatar Loren Merritt

replace brute force find_optimal_param() with a closed-form solution.

overall flac encoding: 4-15% faster.
output is not identical to the previous algorithm due to occasional rounding
errors, but the differece is less than .0005% bitrate.

Originally committed as revision 10612 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent f6215b1b
...@@ -447,20 +447,19 @@ static void copy_samples(FlacEncodeContext *s, int16_t *samples) ...@@ -447,20 +447,19 @@ static void copy_samples(FlacEncodeContext *s, int16_t *samples)
#define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k))) #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
/**
* Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0
*/
static int find_optimal_param(uint32_t sum, int n) static int find_optimal_param(uint32_t sum, int n)
{ {
int k, k_opt; int k;
uint32_t nbits[MAX_RICE_PARAM+1]; uint32_t sum2;
k_opt = 0; if(sum <= n>>1)
nbits[0] = UINT32_MAX; return 0;
for(k=0; k<=MAX_RICE_PARAM; k++) { sum2 = sum-(n>>1);
nbits[k] = rice_encode_count(sum, n, k); k = av_log2(n<256 ? FASTDIV(sum2,n) : sum2/n);
if(nbits[k] < nbits[k_opt]) { return FFMIN(k, MAX_RICE_PARAM);
k_opt = k;
}
}
return k_opt;
} }
static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder, static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment