Micro-optimization: use directly opj_bio_putbit() instead of opj_bio_write() to emit...
authorEven Rouault <even.rouault@spatialys.com>
Thu, 11 Aug 2022 14:41:57 +0000 (16:41 +0200)
committerEven Rouault <even.rouault@spatialys.com>
Thu, 11 Aug 2022 14:41:57 +0000 (16:41 +0200)
src/lib/openjp2/bio.c
src/lib/openjp2/bio.h
src/lib/openjp2/t2.c
src/lib/openjp2/tgt.c

index 09dcd7f524926da0a89509c7a025ccd34102c279..8106df754edbab0c22b3dfbbb0a41b6db2c7ef86 100644 (file)
 /** @name Local static functions */
 /*@{*/
 
-/**
-Write a bit
-@param bio BIO handle
-@param b Bit to write (0 or 1)
-*/
-static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b);
 /**
 Read a bit
 @param bio BIO handle
@@ -100,16 +94,6 @@ static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio)
     return OPJ_TRUE;
 }
 
-static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b)
-{
-    if (bio->ct == 0) {
-        opj_bio_byteout(
-            bio); /* MSD: why not check the return value of this function ? */
-    }
-    bio->ct--;
-    bio->buf |= b << bio->ct;
-}
-
 static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio)
 {
     if (bio->ct == 0) {
@@ -162,6 +146,16 @@ void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len)
     bio->ct = 0;
 }
 
+void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b)
+{
+    if (bio->ct == 0) {
+        opj_bio_byteout(
+            bio); /* MSD: why not check the return value of this function ? */
+    }
+    bio->ct--;
+    bio->buf |= b << bio->ct;
+}
+
 void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n)
 {
     OPJ_INT32 i;
index 448fdda2190c95fba08c64717c7b8749321c03f5..d482f9ead5a7a960c9ea2dbba324f2b93fd6e613 100644 (file)
@@ -106,6 +106,14 @@ Write bits
 @param n Number of bits to write
 */
 void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n);
+
+/**
+Write a bit
+@param bio BIO handle
+@param b Bit to write (0 or 1)
+*/
+void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b);
+
 /**
 Read bits
 @param bio BIO handle
index ebda005267e64967fe61006497ac248604d41199..a61aa196ab398d6cd0b9172b709224f51341fe9d 100644 (file)
@@ -167,9 +167,9 @@ static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n)
 {
     while (--n >= 0) {
-        opj_bio_write(bio, 1, 1);
+        opj_bio_putbit(bio, 1);
     }
-    opj_bio_write(bio, 0, 1);
+    opj_bio_putbit(bio, 0);
 }
 
 static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
@@ -184,7 +184,7 @@ static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
 static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n)
 {
     if (n == 1) {
-        opj_bio_write(bio, 0, 1);
+        opj_bio_putbit(bio, 0);
     } else if (n == 2) {
         opj_bio_write(bio, 2, 2);
     } else if (n <= 5) {
@@ -801,7 +801,7 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
         }
     }
 #endif
-    opj_bio_write(bio, packet_empty ? 0 : 1, 1);           /* Empty header bit */
+    opj_bio_putbit(bio, packet_empty ? 0 : 1);           /* Empty header bit */
 
     /* Writing Packet header */
     band = res->bands;
@@ -849,7 +849,7 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
             if (!cblk->numpasses) {
                 opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
             } else {
-                opj_bio_write(bio, layer->numpasses != 0, 1);
+                opj_bio_putbit(bio, layer->numpasses != 0);
             }
 
             /* if cblk not included, go to the next cblk  */
@@ -978,7 +978,9 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
                 return OPJ_FALSE;
             }
 
-            memcpy(c, layer->data, layer->len);
+            if (p_t2_mode == FINAL_PASS) {
+                memcpy(c, layer->data, layer->len);
+            }
             cblk->numpasses += layer->numpasses;
             c += layer->len;
             length -= layer->len;
index 0cbad12c42ef5e187f9553c12fffb33f8add009d..711d753f46c1c1919a0641595017eabb35c33cfa 100644 (file)
@@ -287,12 +287,12 @@ void opj_tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno,
         while (low < threshold) {
             if (low >= node->value) {
                 if (!node->known) {
-                    opj_bio_write(bio, 1, 1);
+                    opj_bio_putbit(bio, 1);
                     node->known = 1;
                 }
                 break;
             }
-            opj_bio_write(bio, 0, 1);
+            opj_bio_putbit(bio, 0);
             ++low;
         }