Commit bff371e3 authored by Michael Niedermayer's avatar 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: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent 5936d982
...@@ -1469,42 +1469,39 @@ static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s) ...@@ -1469,42 +1469,39 @@ static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
static int jp2_find_codestream(Jpeg2000DecoderContext *s) static int jp2_find_codestream(Jpeg2000DecoderContext *s)
{ {
uint32_t atom_size, atom; uint32_t atom_size, atom, atom_end;
int found_codestream = 0, search_range = 10; int search_range = 10;
while (!found_codestream && search_range while (search_range
&& &&
bytestream2_get_bytes_left(&s->g) >= 8) { bytestream2_get_bytes_left(&s->g) >= 8) {
atom_size = bytestream2_get_be32u(&s->g); atom_size = bytestream2_get_be32u(&s->g);
atom = bytestream2_get_be32u(&s->g); atom = bytestream2_get_be32u(&s->g);
if (atom == JP2_CODESTREAM) { atom_end = bytestream2_tell(&s->g) + atom_size - 8;
found_codestream = 1;
} else if (atom == JP2_HEADER && if (atom == JP2_CODESTREAM)
bytestream2_get_bytes_left(&s->g) >= atom_size && return 1;
if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
return 0;
if (atom == JP2_HEADER &&
atom_size >= 16) { atom_size >= 16) {
uint32_t atom2_size, atom2; uint32_t atom2_size, atom2, atom2_end;
atom_size -= 8;
do { do {
atom2_size = bytestream2_get_be32u(&s->g); atom2_size = bytestream2_get_be32u(&s->g);
atom2 = bytestream2_get_be32u(&s->g); atom2 = bytestream2_get_be32u(&s->g);
atom_size -= 8; atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
if (atom2_size < 8 || atom2_size - 8 > atom_size) if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
break; break;
atom2_size -= 8;
if (atom2 == JP2_CODESTREAM) { if (atom2 == JP2_CODESTREAM) {
return 1; return 1;
} else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) { } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
int method = bytestream2_get_byteu(&s->g); int method = bytestream2_get_byteu(&s->g);
bytestream2_skipu(&s->g, 2); bytestream2_skipu(&s->g, 2);
atom_size -= 3;
atom2_size -= 3;
if (method == 1) { if (method == 1) {
s->colour_space = bytestream2_get_be32u(&s->g); 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) { } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
int i, size, colour_count, colour_channels, colour_depth[3]; int i, size, colour_count, colour_channels, colour_depth[3];
uint32_t r, g, b; uint32_t r, g, b;
...@@ -1514,8 +1511,6 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s) ...@@ -1514,8 +1511,6 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s)
colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1; colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
colour_depth[1] = (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; colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
atom_size -= 6;
atom2_size -= 6;
size = (colour_depth[0] + 7 >> 3) * colour_count + size = (colour_depth[0] + 7 >> 3) * colour_count +
(colour_depth[1] + 7 >> 3) * colour_count + (colour_depth[1] + 7 >> 3) * colour_count +
(colour_depth[2] + 7 >> 3) * colour_count; (colour_depth[2] + 7 >> 3) * colour_count;
...@@ -1526,8 +1521,7 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s) ...@@ -1526,8 +1521,7 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s)
colour_depth[2] > 16 || colour_depth[2] > 16 ||
atom2_size < size) { atom2_size < size) {
avpriv_request_sample(s->avctx, "Unknown palette"); avpriv_request_sample(s->avctx, "Unknown palette");
bytestream2_skipu(&s->g, atom2_size); bytestream2_seek(&s->g, atom2_end, SEEK_SET);
atom_size -= atom2_size;
continue; continue;
} }
s->pal8 = 1; s->pal8 = 1;
...@@ -1552,41 +1546,24 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s) ...@@ -1552,41 +1546,24 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s)
} }
s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b; s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
} }
atom_size -= size; } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
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) {
int n = bytestream2_get_be16u(&s->g); int n = bytestream2_get_be16u(&s->g);
atom_size -= 2;
atom2_size -= 2;
for (; n>0; n--) { for (; n>0; n--) {
int cn = bytestream2_get_be16(&s->g); int cn = bytestream2_get_be16(&s->g);
int av_unused typ = bytestream2_get_be16(&s->g); int av_unused typ = bytestream2_get_be16(&s->g);
int asoc = bytestream2_get_be16(&s->g); int asoc = bytestream2_get_be16(&s->g);
if (cn < 4 || asoc < 4) if (cn < 4 || asoc < 4)
s->cdef[cn] = asoc; 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_seek(&s->g, atom2_end, SEEK_SET);
bytestream2_skipu(&s->g, atom_size); } while (atom_end - atom2_end >= 8);
} else { } else {
if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
return 0;
bytestream2_skipu(&s->g, atom_size - 8);
search_range--; search_range--;
} }
bytestream2_seek(&s->g, atom_end, SEEK_SET);
} }
if (found_codestream)
return 1;
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment