diff mbox

[FFmpeg-devel,2/5] af_hdcd: hdcd_analyze_gen() using int instead of float

Message ID 1473074325-20959-3-git-send-email-pburt0@gmail.com
State Accepted
Commit 38445d58f1a1cdc69bb11309e07aa99360bf466b
Headers show

Commit Message

Burt P Sept. 5, 2016, 11:18 a.m. UTC
Signed-off-by: Burt P <pburt0@gmail.com>
---
 libavfilter/af_hdcd.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

Comments

Michael Niedermayer Sept. 6, 2016, 10:07 p.m. UTC | #1
On Mon, Sep 05, 2016 at 06:18:42AM -0500, Burt P wrote:
> Signed-off-by: Burt P <pburt0@gmail.com>
> ---
>  libavfilter/af_hdcd.c | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/libavfilter/af_hdcd.c b/libavfilter/af_hdcd.c
> index cde2340..c8bda82 100644
> --- a/libavfilter/af_hdcd.c
> +++ b/libavfilter/af_hdcd.c
> @@ -1316,11 +1316,10 @@ static void hdcd_analyze_prepare(hdcd_state *state, int32_t *samples, int count,
>  /** encode a value in the given sample by adjusting the amplitude */
>  static int32_t hdcd_analyze_gen(int32_t sample, unsigned int v, unsigned int maxv)
>  {
> -    float sflt = sample, vv = v;
> -    vv /= maxv;
> -    if (vv > 1.0) vv = 1.0;
> -    sflt *= 1.0 + (vv * 18);
> -    return (int32_t)sflt;
> +    static const int r = 18, m = 1024;
> +    int64_t s64 = sample;
> +    v = m + (v * r * m / maxv);

maxv is always a constant (for each call) and division is slow, maybe
you want to replace it by multiplication by its inverse and adjust
the /m later (that is fast as m is a constant power of 2 in unsigned
context)

[...[
Burt P Sept. 7, 2016, 4:14 p.m. UTC | #2
applied

as is for now.

Thanks for the comment. I will look into a faster way of doing it.
I only needed to change from the float version because it was giving
different results for i686 than for amd64.
I don't know why, exactly, but I do know that this version gives
bit-perfect results everywhere I've tried it.
diff mbox

Patch

diff --git a/libavfilter/af_hdcd.c b/libavfilter/af_hdcd.c
index cde2340..c8bda82 100644
--- a/libavfilter/af_hdcd.c
+++ b/libavfilter/af_hdcd.c
@@ -1316,11 +1316,10 @@  static void hdcd_analyze_prepare(hdcd_state *state, int32_t *samples, int count,
 /** encode a value in the given sample by adjusting the amplitude */
 static int32_t hdcd_analyze_gen(int32_t sample, unsigned int v, unsigned int maxv)
 {
-    float sflt = sample, vv = v;
-    vv /= maxv;
-    if (vv > 1.0) vv = 1.0;
-    sflt *= 1.0 + (vv * 18);
-    return (int32_t)sflt;
+    static const int r = 18, m = 1024;
+    int64_t s64 = sample;
+    v = m + (v * r * m / maxv);
+    return (int32_t)(s64 * v / m);
 }
 
 /** behaves like hdcd_envelope(), but encodes processing information in