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

Showing the changes to libavcodec (3 of 3 files). Show all

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
391 } else if (ncomponents == 1 && s->precision == 8) {391 } else if (ncomponents == 1 && s->precision == 8) {
392 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;392 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
393 i = 0;393 i = 0;
394 } else if (ncomponents == 1 && s->precision == 12) {
395 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
396 i = 0;
394 }397 }
395 }398 }
396 399
@@ -2204,7 +2207,7 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile · 1807 unmodified lines
2204 Jpeg2000Band *band = rlevel->band + bandno;2207 Jpeg2000Band *band = rlevel->band + bandno;
2205 int cblkno = 0, bandpos;2208 int cblkno = 0, bandpos;
2206 /* See Rec. ITU-T T.800, Equation E-2 */2209 /* 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;
2208 2211
2209 bandpos = bandno + (reslevelno > 0);2212 bandpos = bandno + (reslevelno > 0);
2210 2213
@@ -2212,8 +2215,8 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile · 1 unmodified line
2212 band->coord[1][0] == band->coord[1][1])2215 band->coord[1][0] == band->coord[1][1])
2213 continue;2216 continue;
2214 2217
2215 if ((codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F) && magp >= 31) {2218 if ((codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F) && M_b >= 31) {
2216 avpriv_request_sample(s->avctx, "JPEG2000_CTSY_HTJ2K_F and magp >= 31");2219 avpriv_request_sample(s->avctx, "JPEG2000_CTSY_HTJ2K_F and M_b >= 31");
2217 return AVERROR_PATCHWELCOME;2220 return AVERROR_PATCHWELCOME;
2218 }2221 }
2219 2222
@@ -2234,7 +2237,7 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile · 14 unmodified lines
2234 ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk,2237 ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk,
2235 cblk->coord[0][1] - cblk->coord[0][0],2238 cblk->coord[0][1] - cblk->coord[0][0],
2236 cblk->coord[1][1] - cblk->coord[1][0],2239 cblk->coord[1][1] - cblk->coord[1][0],
2237 magp, comp->roi_shift);2240 M_b, comp->roi_shift);
2238 else2241 else
2239 ret = decode_cblk(s, codsty, &t1, cblk,2242 ret = decode_cblk(s, codsty, &t1, cblk,
2240 cblk->coord[0][1] - cblk->coord[0][0],2243 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
122 122
123static void jpeg2000_init_mag_ref(StateVars *s, uint32_t Lref)123static void jpeg2000_init_mag_ref(StateVars *s, uint32_t Lref)
124{124{
125 s->pos = Lref - 2;125 s->pos = Lref - 1;
126 s->bits = 0;126 s->bits = 0;
127 s->last = 0xFF;127 s->last = 0xFF;
128 s->tmp = 0;128 s->tmp = 0;
@@ -145,9 +145,10 @@ static void jpeg2000_init_mel_decoder(MelDecoderState *mel_state) · 16 unmodified lines
145static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *array)145static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *array)
146{146{
147 uint64_t tmp = 0;147 uint64_t tmp = 0;
148 int32_t position = buffer->pos - 4;
149 uint32_t new_bits = 32;148 uint32_t new_bits = 32;
150 149
150 buffer->last = array[buffer->pos + 1];
151
151 if (buffer->bits_left >= 32)152 if (buffer->bits_left >= 32)
152 return 0; // enough data, no need to pull in more bits153 return 0; // enough data, no need to pull in more bits
153 154
@@ -157,9 +158,24 @@ static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *ar · 3 unmodified lines
157 * the bottom most bits.158 * the bottom most bits.
158 */159 */
159 160
160 for(int i = FFMAX(0, position + 1); i <= buffer->pos + 1; i++)161 if (buffer->pos >= 3) { // Common case; we have at least 4 bytes available
161 tmp = 256*tmp + array[i];162 tmp = array[buffer->pos - 3];
162 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
163 if ((tmp & 0x7FFF000000) > 0x7F8F000000) {179 if ((tmp & 0x7FFF000000) > 0x7F8F000000) {
164 tmp &= 0x7FFFFFFFFF;180 tmp &= 0x7FFFFFFFFF;
165 new_bits--;181 new_bits--;
@@ -176,13 +192,11 @@ static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *ar · 10 unmodified lines
176 tmp = (tmp & 0x0000007FFF) + ((tmp & 0xFFFFFF0000) >> 1);192 tmp = (tmp & 0x0000007FFF) + ((tmp & 0xFFFFFF0000) >> 1);
177 new_bits--;193 new_bits--;
178 }194 }
179 195 tmp >>= 8; // Shifts away the extra byte we imported
180 tmp >>= 8; // Remove temporary byte loaded
181 196
182 /* Add bits to the MSB of the bit buffer */197 /* Add bits to the MSB of the bit buffer */
183 buffer->bit_buf |= tmp << buffer->bits_left;198 buffer->bit_buf |= tmp << buffer->bits_left;
184 buffer->bits_left += new_bits;199 buffer->bits_left += new_bits;
185 buffer->pos = FFMAX(0, position);
186 return 0;200 return 0;
187}201}
188 202
@@ -406,6 +420,7 @@ static void recover_mag_sgn(StateVars *mag_sgn, uint8_t pos, uint16_t q, int32_t · 217 unmodified lines
406 E[n] = 32 - ff_clz(v[pos][i] | 1);420 E[n] = 32 - ff_clz(v[pos][i] | 1);
407 mu_n[n] = (v[pos][i] >> 1) + 1;421 mu_n[n] = (v[pos][i] >> 1) + 1;
408 mu_n[n] <<= pLSB;422 mu_n[n] <<= pLSB;
423 mu_n[n] |= (1 << (pLSB - 1)); // Add 0.5 (reconstruction parameter = 1/2)
409 mu_n[n] |= ((uint32_t) (v[pos][i] & 1)) << 31; // sign bit.424 mu_n[n] |= ((uint32_t) (v[pos][i] & 1)) << 31; // sign bit.
410 }425 }
411 }426 }
@@ -414,7 +429,7 @@ static void recover_mag_sgn(StateVars *mag_sgn, uint8_t pos, uint16_t q, int32_t · 2 unmodified lines
414static int jpeg2000_import_bit(StateVars *stream, const uint8_t *array, uint32_t length)429static int jpeg2000_import_bit(StateVars *stream, const uint8_t *array, uint32_t length)
415{430{
416 int cond = stream->pos < length;431 int cond = stream->pos < length;
417 int pos = FFMIN(stream->pos, length);432 int pos = FFMIN(stream->pos, length - 1);
418 if (stream->bits == 0) {433 if (stream->bits == 0) {
419 stream->bits = (stream->tmp == 0xFF) ? 7 : 8;434 stream->bits = (stream->tmp == 0xFF) ? 7 : 8;
420 stream->pos += cond;435 stream->pos += cond;
@@ -426,14 +441,22 @@ static int jpeg2000_import_bit(StateVars *stream, const uint8_t *array, uint32_t · 5 unmodified lines
426 441
427static int jpeg2000_peek_bit(StateVars *stream, const uint8_t *array, uint32_t length)442static int jpeg2000_peek_bit(StateVars *stream, const uint8_t *array, uint32_t length)
428{443{
444 uint8_t bit;
445
429 if (stream->bits == 0) {446 if (stream->bits == 0) {
430 int cond = stream->pos < length;447 stream->bits = (stream->last == 0xFF) ? 7 : 8;
431 int pos = FFMIN(stream->pos, length);448 if (stream->pos < length) {
432 stream->bits = (stream->tmp == 0xFF) ? 7 : 8;449 stream->tmp = array[stream->pos];
433 stream->pos += cond;450 stream->pos++;
434 stream->tmp = cond ? array[pos] : 0xFF;451 } else {
452 stream->tmp = 0;
453 }
454 stream->last = stream->tmp;
435 }455 }
436 return (stream->tmp >> stream->bits) & 1;456 bit = stream->tmp & 1;
457 stream->tmp >>= 1;
458 stream->bits--;
459 return bit;
437}460}
438 461
439static int jpeg2000_decode_mel_sym(MelDecoderState *mel_state,462static 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
994 const uint32_t mbr_info, uint8_t causal_cond,1017 const uint32_t mbr_info, uint8_t causal_cond,
995 uint8_t *block_states, int stride)1018 uint8_t *block_states, int stride)
996{1019{
997 int local_mbr = 0;1020 uint8_t *state_p0 = block_states + i * stride + j;
998 1021 uint8_t *state_p1 = block_states + (i + 1) * stride + j;
999 local_mbr |= jpeg2000_get_state(i - 1, j - 1, stride, HT_SHIFT_SIGMA, block_states);1022 uint8_t *state_p2 = block_states + (i + 2) * stride + j;
1000 local_mbr |= jpeg2000_get_state(i - 1, j + 0, stride, HT_SHIFT_SIGMA, block_states);1023
1001 local_mbr |= jpeg2000_get_state(i - 1, j + 1, stride, HT_SHIFT_SIGMA, block_states);1024 uint8_t mbr0 = state_p0[0] | state_p0[1] | state_p0[2];
1002 1025 uint8_t mbr1 = state_p1[0] | state_p1[2];
1003 local_mbr |= jpeg2000_get_state(i + 0, j - 1, stride, HT_SHIFT_SIGMA, block_states);1026 uint8_t mbr2 = state_p2[0] | state_p2[1] | state_p2[2];
1004 local_mbr |= jpeg2000_get_state(i + 0, j + 1, stride, HT_SHIFT_SIGMA, block_states);1027 *mbr = mbr0 | mbr1 | (mbr2 & causal_cond);
1005 1028 *mbr |= (mbr0 >> HT_SHIFT_REF) & (mbr0 >> HT_SHIFT_SCAN);
1006 local_mbr |= jpeg2000_get_state(i + 1, j - 1, stride, HT_SHIFT_SIGMA, block_states) * causal_cond;1029 *mbr |= (mbr1 >> HT_SHIFT_REF) & (mbr1 >> HT_SHIFT_SCAN);
1007 local_mbr |= jpeg2000_get_state(i + 1, j + 0, stride, HT_SHIFT_SIGMA, block_states) * causal_cond;1030 *mbr |= (mbr2 >> HT_SHIFT_REF) & (mbr2 >> HT_SHIFT_SCAN) & causal_cond;
1008 local_mbr |= jpeg2000_get_state(i + 1, j + 1, stride, HT_SHIFT_SIGMA, block_states) * causal_cond;1031 *mbr &= 1;
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;
1030}1032}
1031 1033
1032static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s,1034static 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
1060 jpeg2000_modify_state(i, j, stride, modify_state, block_states);1062 jpeg2000_modify_state(i, j, stride, modify_state, block_states);
1061 }1063 }
1062 }1064 }
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 }
1063}1077}
1064 1078
1065/**1079/**
@@ -1130,7 +1144,8 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int · 64 unmodified lines
1130 uint16_t height = 4;1144 uint16_t height = 4;
1131 uint16_t i_start = 0;1145 uint16_t i_start = 0;
1132 int32_t *sp;1146 int32_t *sp;
1133 1147 int32_t bit;
1148 int32_t tmp;
1134 jpeg2000_init_mag_ref(&mag_ref, magref_length);1149 jpeg2000_init_mag_ref(&mag_ref, magref_length);
1135 1150
1136 for (int n1 = 0; n1 < num_v_stripe; n1++) {1151 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
1141 * Rec. ITU-T T.814, Figure 7.1156 * Rec. ITU-T T.814, Figure 7.
1142 */1157 */
1143 sp = &sample_buf[j + i * stride];1158 sp = &sample_buf[j + i * stride];
1144 if (jpeg2000_get_state(i, j, width, HT_SHIFT_SIGMA, block_states) != 0) {1159 if (jpeg2000_get_state(i, j, stride, HT_SHIFT_SIGMA, block_states) != 0) {
1145 jpeg2000_modify_state(i, j, width, 1 << HT_SHIFT_REF_IND, block_states);1160 jpeg2000_modify_state(i, j, stride, 1 << HT_SHIFT_REF_IND, block_states);
1146 *sp |= jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length) << pLSB;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)
1147 }1166 }
1148 }1167 }
1149 }1168 }
@@ -1153,9 +1172,13 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int · 3 unmodified lines
1153 for (int j = 0; j < width; j++) {1172 for (int j = 0; j < width; j++) {
1154 for (int i = i_start; i < i_start + height; i++) {1173 for (int i = i_start; i < i_start + height; i++) {
1155 sp = &sample_buf[j + i * stride];1174 sp = &sample_buf[j + i * stride];
1156 if (jpeg2000_get_state(i, j, width, HT_SHIFT_SIGMA, block_states) != 0) {1175 if (jpeg2000_get_state(i, j, stride, HT_SHIFT_SIGMA, block_states) != 0) {
1157 jpeg2000_modify_state(i, j, width, 1 << HT_SHIFT_REF_IND, block_states);1176 jpeg2000_modify_state(i, j, stride, 1 << HT_SHIFT_REF_IND, block_states);
1158 *sp |= jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length) << pLSB;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)
1159 }1182 }
1160 }1183 }
1161 }1184 }
@@ -1233,6 +1256,9 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c · 71 unmodified lines
1233 }1256 }
1234 Dcup = cblk->data;1257 Dcup = cblk->data;
1235 Dref = cblk->data + Lcup; // Dref comes after the refinement segment1258 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
1236 S_blk = p0 + cblk->zbp;1262 S_blk = p0 + cblk->zbp;
1237 cblk->zbp = S_blk - 1;1263 cblk->zbp = S_blk - 1;
1238 pLSB = 30 - S_blk;1264 pLSB = 30 - S_blk;
libavcodec/jpeg2000htdec.h+1−1
@@ -29,6 +29,6 @@ · 28 unmodified lines
29 29
30int ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,30int ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
31 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width,31 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);
33 33
34#endif /* AVCODEC_JPEG2000HTDEC_H */34#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