[FFmpeg-cvslog] avcodec/ituh263enc: Simplify encoding umotion vectors

Andreas Rheinhardt git at videolan.org
Sat Jun 21 23:21:35 EEST 2025


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Sat Jun 14 00:36:31 2025 +0200| [f8c0ac9984e38b2f7221cb8838cddde35d0181de] | committer: Andreas Rheinhardt

avcodec/ituh263enc: Simplify encoding umotion vectors

There is no need to use two loops.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=f8c0ac9984e38b2f7221cb8838cddde35d0181de
---

 libavcodec/ituh263enc.c | 38 ++++++++++----------------------------
 1 file changed, 10 insertions(+), 28 deletions(-)

diff --git a/libavcodec/ituh263enc.c b/libavcodec/ituh263enc.c
index d1d07bc5f8..903bb45fce 100644
--- a/libavcodec/ituh263enc.c
+++ b/libavcodec/ituh263enc.c
@@ -573,38 +573,20 @@ static void h263_encode_block(MPVEncContext *const s, int16_t block[], int n)
 /* Encode MV differences on H.263+ with Unrestricted MV mode */
 static void h263p_encode_umotion(PutBitContext *pb, int val)
 {
-    short sval = 0;
-    short i = 0;
-    short n_bits = 0;
-    short temp_val;
-    int code = 0;
-    int tcode;
-
     if ( val == 0)
         put_bits(pb, 1, 1);
-    else if (val == 1)
-        put_bits(pb, 3, 0);
-    else if (val == -1)
-        put_bits(pb, 3, 2);
     else {
-
-        sval = ((val < 0) ? (short)(-val):(short)val);
-        temp_val = sval;
-
-        while (temp_val != 0) {
-            temp_val = temp_val >> 1;
-            n_bits++;
-        }
-
-        i = n_bits - 1;
-        while (i > 0) {
-            tcode = (sval & (1 << (i-1))) >> (i-1);
-            tcode = (tcode << 1) | 1;
-            code = (code << 2) | tcode;
-            i--;
+        unsigned code = (val < 0) << 1;
+        unsigned aval = val < 0 ? -val : val;
+        unsigned n_bits = 2;
+
+        while (aval != 1) { // The leading digit is implicitly coded via length
+            unsigned tmp = (aval & 1) << 1 | 1;
+            aval  >>= 1;
+            code   |= tmp << n_bits;
+            n_bits += 2;
         }
-        code = ((code << 1) | (val < 0)) << 1;
-        put_bits(pb, (2*n_bits)+1, code);
+        put_bits(pb, n_bits + 1, code);
     }
 }
 



More information about the ffmpeg-cvslog mailing list