History

avcodec/jpeg2000dec: Fix HT decoding

Fixes incorrect handling of MAGB_P value in Ccap15. Fixes bugs in HT block decoding.

Osamu Watanabe committed 2 years ago

Commit d88a9881 parent 48b14733 files · +89 −60Browse files

Unified · Split

3 files
  1. libavcodec/jpeg2000dec.c +7 −4
  2. libavcodec/jpeg2000htdec.c +81 −55
  3. libavcodec/jpeg2000htdec.h +1 −1
libavcodec/jpeg2000dec.c+7−4
@@ -391,6 +391,9 @@ static int get_siz(Jpeg2000DecoderContext *s) · 390 unmodified lines
391391 } else if (ncomponents == 1 && s->precision == 8) {
392392 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
393393 i = 0;
394+ } else if (ncomponents == 1 && s->precision == 12) {
395+ s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
396+ i = 0;
394397 }
395398 }
396399
@@ -2204,7 +2207,7 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile · 1807 unmodified lines
22042207 Jpeg2000Band *band = rlevel->band + bandno;
22052208 int cblkno = 0, bandpos;
22062209 /* See Rec. ITU-T T.800, Equation E-2 */
2207− int magp = quantsty->expn[subbandno] + quantsty->nguardbits - 1;
2210+ int M_b = quantsty->expn[subbandno] + quantsty->nguardbits - 1;
22082211
22092212 bandpos = bandno + (reslevelno > 0);
22102213
@@ -2212,8 +2215,8 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile · 1 unmodified line
22122215 band->coord[1][0] == band->coord[1][1])
22132216 continue;
22142217
2215− if ((codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F) && magp >= 31) {
2216− avpriv_request_sample(s->avctx, "JPEG2000_CTSY_HTJ2K_F and magp >= 31");
2218+ if ((codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F) && M_b >= 31) {
2219+ avpriv_request_sample(s->avctx, "JPEG2000_CTSY_HTJ2K_F and M_b >= 31");
22172220 return AVERROR_PATCHWELCOME;
22182221 }
22192222
@@ -2234,7 +2237,7 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile · 14 unmodified lines
22342237 ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk,
22352238 cblk->coord[0][1] - cblk->coord[0][0],
22362239 cblk->coord[1][1] - cblk->coord[1][0],
2237− magp, comp->roi_shift);
2240+ M_b, comp->roi_shift);
22382241 else
22392242 ret = decode_cblk(s, codsty, &t1, cblk,
22402243 cblk->coord[0][1] - cblk->coord[0][0],
libavcodec/jpeg2000htdec.c+81−55
@@ -122,7 +122,7 @@ static void jpeg2000_init_mel(StateVars *s, uint32_t Pcup) · 121 unmodified lines
122122
123123 static void jpeg2000_init_mag_ref(StateVars *s, uint32_t Lref)
124124 {
125− s->pos = Lref - 2;
125+ s->pos = Lref - 1;
126126 s->bits = 0;
127127 s->last = 0xFF;
128128 s->tmp = 0;
@@ -145,9 +145,10 @@ static void jpeg2000_init_mel_decoder(MelDecoderState *mel_state) · 16 unmodified lines
145145 static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *array)
146146 {
147147 uint64_t tmp = 0;
148− int32_t position = buffer->pos - 4;
149148 uint32_t new_bits = 32;
150149
150+ buffer->last = array[buffer->pos + 1];
151+
151152 if (buffer->bits_left >= 32)
152153 return 0; // enough data, no need to pull in more bits
153154
@@ -157,9 +158,24 @@ static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *ar · 3 unmodified lines
157158 * the bottom most bits.
158159 */
159160
160− for(int i = FFMAX(0, position + 1); i <= buffer->pos + 1; i++)
161− tmp = 256*tmp + array[i];
162−
161+ if (buffer->pos >= 3) { // Common case; we have at least 4 bytes available
162+ tmp = array[buffer->pos - 3];
163+ tmp = (tmp << 8) | array[buffer->pos - 2];
164+ tmp = (tmp << 8) | array[buffer->pos - 1];
165+ tmp = (tmp << 8) | array[buffer->pos];
166+ tmp = (tmp << 8) | buffer->last; // For stuffing bit detection
167+ buffer->pos -= 4;
168+ } else {
169+ if (buffer->pos >= 2)
170+ tmp = array[buffer->pos - 2];
171+ if (buffer->pos >= 1)
172+ tmp = (tmp << 8) | array[buffer->pos - 1];
173+ if (buffer->pos >= 0)
174+ tmp = (tmp << 8) | array[buffer->pos];
175+ buffer->pos = 0;
176+ tmp = (tmp << 8) | buffer->last; // For stuffing bit detection
177+ }
178+ // Now remove any stuffing bits, shifting things down as we go
163179 if ((tmp & 0x7FFF000000) > 0x7F8F000000) {
164180 tmp &= 0x7FFFFFFFFF;
165181 new_bits--;
@@ -176,13 +192,11 @@ static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *ar · 10 unmodified lines
176192 tmp = (tmp & 0x0000007FFF) + ((tmp & 0xFFFFFF0000) >> 1);
177193 new_bits--;
178194 }
179−
180− tmp >>= 8; // Remove temporary byte loaded
195+ tmp >>= 8; // Shifts away the extra byte we imported
181196
182197 /* Add bits to the MSB of the bit buffer */
183198 buffer->bit_buf |= tmp << buffer->bits_left;
184199 buffer->bits_left += new_bits;
185− buffer->pos = FFMAX(0, position);
186200 return 0;
187201 }
188202
@@ -406,6 +420,7 @@ static void recover_mag_sgn(StateVars *mag_sgn, uint8_t pos, uint16_t q, int32_t · 217 unmodified lines
406420 E[n] = 32 - ff_clz(v[pos][i] | 1);
407421 mu_n[n] = (v[pos][i] >> 1) + 1;
408422 mu_n[n] <<= pLSB;
423+ mu_n[n] |= (1 << (pLSB - 1)); // Add 0.5 (reconstruction parameter = 1/2)
409424 mu_n[n] |= ((uint32_t) (v[pos][i] & 1)) << 31; // sign bit.
410425 }
411426 }
@@ -414,7 +429,7 @@ static void recover_mag_sgn(StateVars *mag_sgn, uint8_t pos, uint16_t q, int32_t · 2 unmodified lines
414429 static int jpeg2000_import_bit(StateVars *stream, const uint8_t *array, uint32_t length)
415430 {
416431 int cond = stream->pos < length;
417− int pos = FFMIN(stream->pos, length);
432+ int pos = FFMIN(stream->pos, length - 1);
418433 if (stream->bits == 0) {
419434 stream->bits = (stream->tmp == 0xFF) ? 7 : 8;
420435 stream->pos += cond;
@@ -426,14 +441,22 @@ static int jpeg2000_import_bit(StateVars *stream, const uint8_t *array, uint32_t · 5 unmodified lines
426441
427442 static int jpeg2000_peek_bit(StateVars *stream, const uint8_t *array, uint32_t length)
428443 {
444+ uint8_t bit;
445+
429446 if (stream->bits == 0) {
430− int cond = stream->pos < length;
431− int pos = FFMIN(stream->pos, length);
432− stream->bits = (stream->tmp == 0xFF) ? 7 : 8;
433− stream->pos += cond;
434− stream->tmp = cond ? array[pos] : 0xFF;
447+ stream->bits = (stream->last == 0xFF) ? 7 : 8;
448+ if (stream->pos < length) {
449+ stream->tmp = array[stream->pos];
450+ stream->pos++;
451+ } else {
452+ stream->tmp = 0;
453+ }
454+ stream->last = stream->tmp;
435455 }
436− return (stream->tmp >> stream->bits) & 1;
456+ bit = stream->tmp & 1;
457+ stream->tmp >>= 1;
458+ stream->bits--;
459+ return bit;
437460 }
438461
439462 static int jpeg2000_decode_mel_sym(MelDecoderState *mel_state,
@@ -994,39 +1017,18 @@ static void jpeg2000_calc_mbr(uint8_t *mbr, const uint16_t i, const uint16_t j, · 554 unmodified lines
9941017 const uint32_t mbr_info, uint8_t causal_cond,
9951018 uint8_t *block_states, int stride)
9961019 {
997− int local_mbr = 0;
998−
999− local_mbr |= jpeg2000_get_state(i - 1, j - 1, stride, HT_SHIFT_SIGMA, block_states);
1000− local_mbr |= jpeg2000_get_state(i - 1, j + 0, stride, HT_SHIFT_SIGMA, block_states);
1001− local_mbr |= jpeg2000_get_state(i - 1, j + 1, stride, HT_SHIFT_SIGMA, block_states);
1002−
1003− local_mbr |= jpeg2000_get_state(i + 0, j - 1, stride, HT_SHIFT_SIGMA, block_states);
1004− local_mbr |= jpeg2000_get_state(i + 0, j + 1, stride, HT_SHIFT_SIGMA, block_states);
1005−
1006− local_mbr |= jpeg2000_get_state(i + 1, j - 1, stride, HT_SHIFT_SIGMA, block_states) * causal_cond;
1007− local_mbr |= jpeg2000_get_state(i + 1, j + 0, stride, HT_SHIFT_SIGMA, block_states) * causal_cond;
1008− local_mbr |= jpeg2000_get_state(i + 1, j + 1, stride, HT_SHIFT_SIGMA, block_states) * causal_cond;
1009−
1010− local_mbr |= jpeg2000_get_state(i - 1, j - 1, stride, HT_SHIFT_REF, block_states) *
1011− jpeg2000_get_state(i - 1, j - 1, stride, HT_SHIFT_SCAN, block_states);
1012− local_mbr |= jpeg2000_get_state(i - 1, j + 0, stride, HT_SHIFT_REF, block_states) *
1013− jpeg2000_get_state(i - 1, j - 1, stride, HT_SHIFT_SCAN, block_states);
1014− local_mbr |= jpeg2000_get_state(i - 1, j + 1, stride, HT_SHIFT_REF, block_states) *
1015− jpeg2000_get_state(i - 1, j + 1, stride, HT_SHIFT_SCAN, block_states);
1016−
1017− local_mbr |= jpeg2000_get_state(i + 0, j - 1, stride, HT_SHIFT_REF, block_states) *
1018− jpeg2000_get_state(i + 0, j - 1, stride, HT_SHIFT_SCAN, block_states);
1019− local_mbr |= jpeg2000_get_state(i + 0, j + 1, stride, HT_SHIFT_REF, block_states) *
1020− jpeg2000_get_state(i + 0, j + 1, stride, HT_SHIFT_SCAN, block_states);
1021−
1022− local_mbr |= jpeg2000_get_state(i + 1, j - 1, stride, HT_SHIFT_REF, block_states) *
1023− jpeg2000_get_state(i + 1, j - 1, stride, HT_SHIFT_SCAN, block_states) * causal_cond;
1024− local_mbr |= jpeg2000_get_state(i + 1, j + 0, stride, HT_SHIFT_REF, block_states) *
1025− jpeg2000_get_state(i + 1, j + 0, stride, HT_SHIFT_SCAN, block_states) * causal_cond;
1026− local_mbr |= jpeg2000_get_state(i + 1, j + 1, stride, HT_SHIFT_REF, block_states) *
1027− jpeg2000_get_state(i + 1, j + 1, stride, HT_SHIFT_SCAN, block_states) * causal_cond;
1028−
1029− *mbr |= local_mbr;
1020+ uint8_t *state_p0 = block_states + i * stride + j;
1021+ uint8_t *state_p1 = block_states + (i + 1) * stride + j;
1022+ uint8_t *state_p2 = block_states + (i + 2) * stride + j;
1023+
1024+ uint8_t mbr0 = state_p0[0] | state_p0[1] | state_p0[2];
1025+ uint8_t mbr1 = state_p1[0] | state_p1[2];
1026+ uint8_t mbr2 = state_p2[0] | state_p2[1] | state_p2[2];
1027+ *mbr = mbr0 | mbr1 | (mbr2 & causal_cond);
1028+ *mbr |= (mbr0 >> HT_SHIFT_REF) & (mbr0 >> HT_SHIFT_SCAN);
1029+ *mbr |= (mbr1 >> HT_SHIFT_REF) & (mbr1 >> HT_SHIFT_SCAN);
1030+ *mbr |= (mbr2 >> HT_SHIFT_REF) & (mbr2 >> HT_SHIFT_SCAN) & causal_cond;
1031+ *mbr &= 1;
10301032 }
10311033
10321034 static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s,
@@ -1060,6 +1062,18 @@ static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s · 27 unmodified lines
10601062 jpeg2000_modify_state(i, j, stride, modify_state, block_states);
10611063 }
10621064 }
1065+ // decode sign
1066+ for (int j = j_s; j < j_s + width; j++) {
1067+ for (int i = i_s; i < i_s + height; i++) {
1068+ uint8_t bit;
1069+ int32_t *sp = &sample_buf[j + (i * (stride))];
1070+ uint8_t *state_p = block_states + (i + 1) * stride + (j + 1);
1071+ if ((state_p[0] >> HT_SHIFT_REF) & 1) {
1072+ bit = jpeg2000_peek_bit(sig_prop, magref_segment, magref_length);
1073+ *sp |= (int32_t)bit << 31;
1074+ }
1075+ }
1076+ }
10631077 }
10641078
10651079 /**
@@ -1130,7 +1144,8 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int · 64 unmodified lines
11301144 uint16_t height = 4;
11311145 uint16_t i_start = 0;
11321146 int32_t *sp;
1133−
1147+ int32_t bit;
1148+ int32_t tmp;
11341149 jpeg2000_init_mag_ref(&mag_ref, magref_length);
11351150
11361151 for (int n1 = 0; n1 < num_v_stripe; n1++) {
@@ -1141,9 +1156,13 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int · 4 unmodified lines
11411156 * Rec. ITU-T T.814, Figure 7.
11421157 */
11431158 sp = &sample_buf[j + i * stride];
1144− if (jpeg2000_get_state(i, j, width, HT_SHIFT_SIGMA, block_states) != 0) {
1145− jpeg2000_modify_state(i, j, width, 1 << HT_SHIFT_REF_IND, block_states);
1146− *sp |= jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length) << pLSB;
1159+ if (jpeg2000_get_state(i, j, stride, HT_SHIFT_SIGMA, block_states) != 0) {
1160+ jpeg2000_modify_state(i, j, stride, 1 << HT_SHIFT_REF_IND, block_states);
1161+ bit = jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length);
1162+ tmp = 0xFFFFFFFE | (uint32_t)bit;
1163+ tmp <<= pLSB;
1164+ sp[0] &= tmp;
1165+ sp[0] |= 1 << (pLSB - 1); // Add 0.5 (reconstruction parameter = 1/2)
11471166 }
11481167 }
11491168 }
@@ -1153,9 +1172,13 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int · 3 unmodified lines
11531172 for (int j = 0; j < width; j++) {
11541173 for (int i = i_start; i < i_start + height; i++) {
11551174 sp = &sample_buf[j + i * stride];
1156− if (jpeg2000_get_state(i, j, width, HT_SHIFT_SIGMA, block_states) != 0) {
1157− jpeg2000_modify_state(i, j, width, 1 << HT_SHIFT_REF_IND, block_states);
1158− *sp |= jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length) << pLSB;
1175+ if (jpeg2000_get_state(i, j, stride, HT_SHIFT_SIGMA, block_states) != 0) {
1176+ jpeg2000_modify_state(i, j, stride, 1 << HT_SHIFT_REF_IND, block_states);
1177+ bit = jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length);
1178+ tmp = 0xFFFFFFFE | (uint32_t)bit;
1179+ tmp <<= pLSB;
1180+ sp[0] &= tmp;
1181+ sp[0] |= 1 << (pLSB - 1); // Add 0.5 (reconstruction parameter = 1/2)
11591182 }
11601183 }
11611184 }
@@ -1233,6 +1256,9 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c · 71 unmodified lines
12331256 }
12341257 Dcup = cblk->data;
12351258 Dref = cblk->data + Lcup; // Dref comes after the refinement segment
1259+
1260+ cblk->data[cblk->length] = 0xFF; // an extra byte for refinement segment (buffer->last)
1261+
12361262 S_blk = p0 + cblk->zbp;
12371263 cblk->zbp = S_blk - 1;
12381264 pLSB = 30 - S_blk;
libavcodec/jpeg2000htdec.h+1−1
@@ -29,6 +29,6 @@ · 28 unmodified lines
2929
3030 int ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
3131 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width,
32− int height, int magp, uint8_t roi_shift);
32+ int height, int M_b, uint8_t roi_shift);
3333
3434 #endif /* AVCODEC_JPEG2000HTDEC_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t