diff mbox series

[FFmpeg-devel,3/8] avcodec/jpeglsenc: Move check out of loop

Message ID 20200904231716.16182-3-andreas.rheinhardt@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/8] avcodec/jpeglsenc: Don't use put bits API for byte-aligned writes
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 4, 2020, 11:17 p.m. UTC
ls_encode_line() encodes a line of input, going from left to right. In
order to calculate a predicted value it uses the left and upper-left
value of the output picture (that is, it uses how a decoder would see
the already encoded part of the picture), unless this is the very first
pixel of this line in which case one uses the first pixel of the last
(upper) line and the line before the last line. Therefore the loop
contained a check for whether this is the beginning of a new line. This
commit moves said check out of the loop by initializing these values
before the loop and by updating these values at the end of the loop
body; already read/calculated values are reused for this (the prediction
also needs the value of the upper pixel and this can be reused for the
upper left value of the next iteration of the loop).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/jpeglsenc.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

Comments

Paul B Mahol Sept. 6, 2020, 5:02 p.m. UTC | #1
On Sat, Sep 05, 2020 at 01:17:11AM +0200, Andreas Rheinhardt wrote:
> ls_encode_line() encodes a line of input, going from left to right. In
> order to calculate a predicted value it uses the left and upper-left
> value of the output picture (that is, it uses how a decoder would see
> the already encoded part of the picture), unless this is the very first
> pixel of this line in which case one uses the first pixel of the last
> (upper) line and the line before the last line. Therefore the loop
> contained a check for whether this is the beginning of a new line. This
> commit moves said check out of the loop by initializing these values
> before the loop and by updating these values at the end of the loop
> body; already read/calculated values are reused for this (the prediction
> also needs the value of the upper pixel and this can be reused for the
> upper left value of the next iteration of the loop).
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/jpeglsenc.c | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
> 

probably ok
diff mbox series

Patch

diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c
index 67897ce8ae..b9c04e979c 100644
--- a/libavcodec/jpeglsenc.c
+++ b/libavcodec/jpeglsenc.c
@@ -145,16 +145,14 @@  static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
                                   int stride, int comp, int bits)
 {
     int x = 0;
-    int Ra, Rb, Rc, Rd;
+    int Ra = R(last, 0), Rb, Rc = last2, Rd;
     int D0, D1, D2;
 
     while (x < w) {
         int err, pred, sign;
 
         /* compute gradients */
-        Ra = x ? R(cur, x - stride) : R(last, x);
         Rb = R(last, x);
-        Rc = x ? R(last, x - stride) : last2;
         Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
         D0 = Rd - Rb;
         D1 = Rb - Rc;
@@ -194,9 +192,9 @@  static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
                     Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
                 else
                     Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
-                W(cur, x, Ra);
             } else
-                W(cur, x, R(in, x));
+                Ra = R(in, x);
+            W(cur, x, Ra);
 
             if (err < 0)
                 err += state->range;
@@ -236,12 +234,13 @@  static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
                     Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
                 else
                     Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
-                W(cur, x, Ra);
             } else
-                W(cur, x, R(in, x));
+                Ra = R(in, x);
+            W(cur, x, Ra);
 
             ls_encode_regular(state, pb, context, err);
         }
+        Rc = Rb;
         x += stride;
     }
 }