[FFmpeg-cvslog] jpeg2000dec: simplify jpeg2000_read_bitstream_packets()

Michael Niedermayer git at videolan.org
Fri Aug 2 03:55:16 CEST 2013


ffmpeg | branch: master | Michael Niedermayer <michaelni at gmx.at> | Tue Jul 23 21:59:01 2013 +0200| [bff371e34ce2177ab8aaa74b71a7fbea500a786b] | committer: Michael Niedermayer

jpeg2000dec: simplify jpeg2000_read_bitstream_packets()

23 lines less
less error prone as theres no more need to update *_size on every read

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

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

 libavcodec/jpeg2000dec.c |   63 +++++++++++++++-------------------------------
 1 file changed, 20 insertions(+), 43 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index c6f6f6c..fb4e52b 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1469,42 +1469,39 @@ static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
 
 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
 {
-    uint32_t atom_size, atom;
-    int found_codestream = 0, search_range = 10;
+    uint32_t atom_size, atom, atom_end;
+    int search_range = 10;
 
-    while (!found_codestream && search_range
+    while (search_range
            &&
            bytestream2_get_bytes_left(&s->g) >= 8) {
         atom_size = bytestream2_get_be32u(&s->g);
         atom      = bytestream2_get_be32u(&s->g);
-        if (atom == JP2_CODESTREAM) {
-            found_codestream = 1;
-        } else if (atom == JP2_HEADER &&
-                   bytestream2_get_bytes_left(&s->g) >= atom_size &&
+        atom_end  = bytestream2_tell(&s->g) + atom_size - 8;
+
+        if (atom == JP2_CODESTREAM)
+            return 1;
+
+        if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
+            return 0;
+
+        if (atom == JP2_HEADER &&
                    atom_size >= 16) {
-            uint32_t atom2_size, atom2;
-            atom_size -= 8;
+            uint32_t atom2_size, atom2, atom2_end;
             do {
                 atom2_size = bytestream2_get_be32u(&s->g);
                 atom2      = bytestream2_get_be32u(&s->g);
-                atom_size  -= 8;
-                if (atom2_size < 8 || atom2_size - 8 > atom_size)
+                atom2_end  = bytestream2_tell(&s->g) + atom2_size - 8;
+                if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
                     break;
-                atom2_size -= 8;
                 if (atom2 == JP2_CODESTREAM) {
                     return 1;
                 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
                     int method = bytestream2_get_byteu(&s->g);
                     bytestream2_skipu(&s->g, 2);
-                    atom_size  -= 3;
-                    atom2_size -= 3;
                     if (method == 1) {
                         s->colour_space = bytestream2_get_be32u(&s->g);
-                        atom_size  -= 4;
-                        atom2_size -= 4;
                     }
-                    bytestream2_skipu(&s->g, atom2_size);
-                    atom_size -= atom2_size;
                 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
                     int i, size, colour_count, colour_channels, colour_depth[3];
                     uint32_t r, g, b;
@@ -1514,8 +1511,6 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s)
                     colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
                     colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
                     colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
-                    atom_size  -= 6;
-                    atom2_size -= 6;
                     size = (colour_depth[0] + 7 >> 3) * colour_count +
                            (colour_depth[1] + 7 >> 3) * colour_count +
                            (colour_depth[2] + 7 >> 3) * colour_count;
@@ -1526,8 +1521,7 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s)
                         colour_depth[2] > 16 ||
                         atom2_size < size) {
                         avpriv_request_sample(s->avctx, "Unknown palette");
-                        bytestream2_skipu(&s->g, atom2_size);
-                        atom_size -= atom2_size;
+                        bytestream2_seek(&s->g, atom2_end, SEEK_SET);
                         continue;
                     }
                     s->pal8 = 1;
@@ -1552,41 +1546,24 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s)
                         }
                         s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
                     }
-                    atom_size  -= size;
-                    atom2_size -= size;
-                    bytestream2_skipu(&s->g, atom2_size);
-                    atom_size -= atom2_size;
-                } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2 &&
-                    bytestream2_get_bytes_left(&s->g) >= atom2_size) {
+                } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
                     int n = bytestream2_get_be16u(&s->g);
-                    atom_size  -= 2;
-                    atom2_size -= 2;
                     for (; n>0; n--) {
                         int cn   = bytestream2_get_be16(&s->g);
                         int av_unused typ  = bytestream2_get_be16(&s->g);
                         int asoc = bytestream2_get_be16(&s->g);
                         if (cn < 4 || asoc < 4)
                             s->cdef[cn] = asoc;
-                        atom_size  -= 6;
-                        atom2_size -= 6;
                     }
-                    bytestream2_skipu(&s->g, atom2_size);
-                } else {
-                    bytestream2_skipu(&s->g, atom2_size);
-                    atom_size -= atom2_size;
                 }
-            } while (atom_size >= 8);
-            bytestream2_skipu(&s->g, atom_size);
+                bytestream2_seek(&s->g, atom2_end, SEEK_SET);
+            } while (atom_end - atom2_end >= 8);
         } else {
-            if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
-                return 0;
-            bytestream2_skipu(&s->g, atom_size - 8);
             search_range--;
         }
+        bytestream2_seek(&s->g, atom_end, SEEK_SET);
     }
 
-    if (found_codestream)
-        return 1;
     return 0;
 }
 



More information about the ffmpeg-cvslog mailing list