[FFmpeg-devel] [PATCH] jpeg2000dec: support SOD not immediately following SOT.

Reimar Döffinger Reimar.Doeffinger at gmx.de
Fri May 3 00:13:10 CEST 2013


Slightly improved behaviour for
http://www.openjpeg.org/samples/Speedway.mj2

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger at gmx.de>
---
 libavcodec/jpeg2000dec.c |   46 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 12 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 7f1be80..a15ff1e 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -85,6 +85,7 @@ typedef struct Jpeg2000DecoderContext {
 
     int16_t         curtileno;
     Jpeg2000Tile    *tile;
+    Jpeg2000TilePart *tp;
 
     /*options parameters*/
     int16_t         lowres;
@@ -404,6 +405,26 @@ static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
     return get_qcx(s, n - 1, q + compno);
 }
 
+static uint8_t setup_sod(Jpeg2000DecoderContext *s)
+{
+    s->buf -= 2;
+    if (!s->tp) {
+        av_log(s->avctx, AV_LOG_ERROR, "SOD marker found before SOT\n");
+        return -1;
+    }
+    if (s->buf > s->tp->tp_end_bstrm) {
+        av_log(s->avctx, AV_LOG_ERROR, "Data end before SOD\n");
+        return -1;
+    }
+    /* Start of bit stream. Pointer to SOD marker
+     */
+    s->tp->tp_start_bstrm = s->buf;
+
+    // set buffer pointer to end of data
+    s->buf = s->tp->tp_end_bstrm;
+    return 0;
+}
+
 /* Get start of tile segment. */
 static uint8_t get_sot(Jpeg2000DecoderContext *s, int n)
 {
@@ -411,6 +432,7 @@ static uint8_t get_sot(Jpeg2000DecoderContext *s, int n)
     uint16_t Isot;
     uint32_t Psot;
     uint8_t TPsot;
+    uint32_t len;
 
     if (s->buf_end - s->buf < 4)
         return AVERROR(EINVAL);
@@ -427,27 +449,23 @@ static uint8_t get_sot(Jpeg2000DecoderContext *s, int n)
     /* Read TNSot but not used */
     bytestream_get_byte(&s->buf);               // TNsot
 
+    s->tp =
     tp             = s->tile[s->curtileno].tile_part + TPsot;
     tp->tile_index = Isot;
     tp->tp_len     = Psot;
     tp->tp_idx     = TPsot;
 
-    /* Start of bit stream. Pointer to SOD marker
-     * Check SOD marker is present. */
-    if (JPEG2000_SOD == bytestream_get_be16(&s->buf))
-        tp->tp_start_bstrm = s->buf;
-    else {
-        av_log(s->avctx, AV_LOG_ERROR, "SOD marker not found \n");
+    if (!tp->tp_len) tp->tp_len = s->buf_end - s->buf + n;
+    len = tp->tp_len - n - 2;
+    if (len >= tp->tp_len || len > s->buf_end - s->buf) {
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid Psot\n");
         return -1;
     }
 
     /* End address of bit stream =
      *     start address + (Psot - size of SOT HEADER(n)
-     *     - size of SOT MARKER(2)  - size of SOD marker(2) */
-    tp->tp_end_bstrm = s->buf + (tp->tp_len - n - 4);
-
-    // set buffer pointer to end of tile part header
-    s->buf = tp->tp_end_bstrm;
+     *     - size of SOT MARKER(2) */
+    s->tp->tp_end_bstrm = s->buf + len;
 
     return 0;
 }
@@ -1139,6 +1157,7 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
     Jpeg2000QuantStyle *qntsty  = s->qntsty;
     uint8_t *properties         = s->properties;
 
+    s->tp = NULL;
     for (;;) {
         int len, ret = 0;
         uint16_t marker;
@@ -1177,6 +1196,9 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
         case JPEG2000_SOT:
             ret = get_sot(s, len);
             break;
+        case JPEG2000_SOD:
+            ret = setup_sod(s);
+            break;
         case JPEG2000_COM:
             // the comment is ignored
             s->buf += len - 2;
@@ -1192,7 +1214,7 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
             s->buf += len - 2;
             break;
         }
-        if (((s->buf - oldbuf != len) && (marker != JPEG2000_SOT)) || ret) {
+        if (((s->buf - oldbuf != len) && (marker != JPEG2000_SOD)) || ret) {
             av_log(s->avctx, AV_LOG_ERROR,
                    "error during processing marker segment %.4x\n", marker);
             return ret ? ret : -1;
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list