From aa16bbaf9bcaef880f70c834d46cb51cf823722f Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Mon, 1 Jul 2013 10:01:29 +0200 Subject: [PATCH 1/3] jpeg2000: Refactor SOT marker parsing Signed-off-by: Luca Barbato --- libavcodec/jpeg2000dec.c | 42 ++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 15dfc2bcd3..ac44a29ff1 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -41,9 +41,8 @@ #define HAD_QCC 0x02 typedef struct Jpeg2000TilePart { - uint16_t tp_idx; // Tile-part index uint8_t tile_index; // Tile index who refers the tile-part - uint32_t tp_len; // Length of tile-part + const uint8_t *tp_end; GetByteContext tpg; // bit stream in tile-part } Jpeg2000TilePart; @@ -55,6 +54,7 @@ typedef struct Jpeg2000Tile { Jpeg2000CodingStyle codsty[4]; Jpeg2000QuantStyle qntsty[4]; Jpeg2000TilePart tile_part[3]; + uint16_t tp_idx; // Tile-part index } Jpeg2000Tile; typedef struct Jpeg2000DecoderContext { @@ -503,25 +503,19 @@ static int get_sot(Jpeg2000DecoderContext *s, int n) return AVERROR_PATCHWELCOME; } - tp = s->tile[s->curtileno].tile_part + TPsot; + s->tile[Isot].tp_idx = TPsot; + tp = s->tile[Isot].tile_part + TPsot; tp->tile_index = Isot; - tp->tp_len = Psot; - tp->tp_idx = TPsot; + tp->tp_end = s->g.buffer + Psot - n - 2; - /* Start of bit stream. Pointer to SOD marker - * Check SOD marker is present. */ - if (JPEG2000_SOD == bytestream2_get_be16(&s->g)) { - bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_len - n - 4); - bytestream2_skip(&s->g, tp->tp_len - n - 4); - } else { - av_log(s->avctx, AV_LOG_ERROR, "SOD marker not found \n"); - return AVERROR_INVALIDDATA; + if (!TPsot) { + Jpeg2000Tile *tile = s->tile + s->curtileno; + + /* copy defaults */ + memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle)); + memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle)); } - /* End address of bit stream = - * start address + (Psot - size of SOT HEADER(n) - * - size of SOT MARKER(2) - size of SOD marker(2) */ - return 0; } @@ -577,12 +571,6 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno) if (!tile->comp) return AVERROR(ENOMEM); - /* copy codsty, qnsty to tile. TODO: Is it the best way? - * codsty, qnsty is an array of 4 structs Jpeg2000CodingStyle - * and Jpeg2000QuantStyle */ - memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(*tile->codsty)); - memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(*tile->qntsty)); - for (compno = 0; compno < s->ncomponents; compno++) { Jpeg2000Component *comp = tile->comp + compno; Jpeg2000CodingStyle *codsty = tile->codsty + compno; @@ -1269,7 +1257,11 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s) ret = get_qcd(s, len, qntsty, properties); break; case JPEG2000_SOT: - ret = get_sot(s, len); + if (!(ret = get_sot(s, len))) { + codsty = s->tile[s->curtileno].codsty; + qntsty = s->tile[s->curtileno].qntsty; + properties = s->tile[s->curtileno].properties; + } break; case JPEG2000_COM: // the comment is ignored @@ -1286,7 +1278,7 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s) bytestream2_skip(&s->g, len - 2); break; } - if (((bytestream2_tell(&s->g) - oldpos != len) && (marker != JPEG2000_SOT)) || ret) { + if (bytestream2_tell(&s->g) - oldpos != len || ret) { av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker); return ret ? ret : -1; From 09d5929f3721613fbb9ac9e74265c89c70df2ce0 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Mon, 1 Jul 2013 10:01:30 +0200 Subject: [PATCH 2/3] jpeg2000: Do not crash on NULL node in tag_tree_decode Signed-off-by: Luca Barbato --- libavcodec/jpeg2000dec.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index ac44a29ff1..172314f40a 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -119,6 +119,9 @@ static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, Jpeg2000TgtNode *stack[30]; int sp = -1, curval = 0; + if (!node) + return AVERROR_INVALIDDATA; + while (node && !node->vis) { stack[++sp] = node; node = node->parent; From d57c737ac30cf079a342fa649fd4888c2d059dd9 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Mon, 1 Jul 2013 10:01:31 +0200 Subject: [PATCH 3/3] jpeg2000: Simplify jpeg2000_decode_packets() Raise PATCHWELCOME error in case of non-implemented progression order. Signed-off-by: Luca Barbato Signed-off-by: Nicolas Bertrand Signed-off-by: Luca Barbato --- libavcodec/jpeg2000dec.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 172314f40a..6e7808304e 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -722,13 +722,12 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile) { - int layno, reslevelno, compno, precno, ok_reslevel, ret; - uint8_t prog_order = tile->codsty[0].prog_order; - uint16_t x; - uint16_t y; + int ret = 0; + int layno, reslevelno, compno, precno, ok_reslevel; + int x, y; s->bit_index = 8; - switch (prog_order) { + switch (tile->codsty[0].prog_order) { case JPEG2000_PGOD_LRCP: for (layno = 0; layno < tile->codsty[0].nlayers; layno++) { ok_reslevel = 1; @@ -802,6 +801,21 @@ static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile } break; + case JPEG2000_PGOD_RLCP: + avpriv_request_sample(s->avctx, "Progression order RLCP"); + ret = AVERROR_PATCHWELCOME; + break; + + case JPEG2000_PGOD_RPCL: + avpriv_request_sample(s->avctx, "Progression order RPCL"); + ret = AVERROR_PATCHWELCOME; + break; + + case JPEG2000_PGOD_PCRL: + avpriv_request_sample(s->avctx, "Progression order PCRL"); + ret = AVERROR_PATCHWELCOME; + break; + default: break; } @@ -809,7 +823,7 @@ static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile /* EOC marker reached */ bytestream2_skip(&s->g, 2); - return 0; + return ret; } /* TIER-1 routines */