49 #define JP2_SIG_TYPE 0x6A502020
50 #define JP2_SIG_VALUE 0x0D0A870A
51 #define JP2_CODESTREAM 0x6A703263
52 #define JP2_HEADER 0x6A703268
63 #define HT_MIXED 0x80 // bit 7 of SPcod/SPcoc
76 if (
s->bit_index == 0) {
77 s->bit_index = 7 + (bytestream2_get_byte(&
s->g) != 0xFF
u);
80 res |= (bytestream2_peek_byte(&
s->g) >>
s->bit_index) & 1;
87 if (bytestream2_get_byte(&
s->g) == 0xff)
97 int sp = -1, curval = 0;
104 while (node && !node->
vis) {
112 curval = stack[sp]->
val;
114 while (curval < threshold && sp >= 0) {
115 if (curval < stack[sp]->
val)
116 curval = stack[sp]->
val;
117 while (curval < threshold) {
127 stack[sp]->
val = curval;
134 int bpc, uint32_t log2_chroma_wh,
int pal8)
141 if (
desc->nb_components != components) {
145 switch (components) {
147 match = match &&
desc->comp[3].depth >= bpc &&
148 (log2_chroma_wh >> 14 & 3) == 0 &&
149 (log2_chroma_wh >> 12 & 3) == 0;
151 match = match &&
desc->comp[2].depth >= bpc &&
152 (log2_chroma_wh >> 10 & 3) ==
desc->log2_chroma_w &&
153 (log2_chroma_wh >> 8 & 3) ==
desc->log2_chroma_h;
155 match = match &&
desc->comp[1].depth >= bpc &&
156 (log2_chroma_wh >> 6 & 3) ==
desc->log2_chroma_w &&
157 (log2_chroma_wh >> 4 & 3) ==
desc->log2_chroma_h;
160 match = match &&
desc->comp[0].depth >= bpc &&
161 (log2_chroma_wh >> 2 & 3) == 0 &&
162 (log2_chroma_wh & 3) == 0 &&
170 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
171 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
172 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
173 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
174 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
175 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
176 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
177 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
178 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
179 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
180 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
181 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
182 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
183 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
201 uint32_t log2_chroma_wh = 0;
203 int possible_fmts_nb = 0;
213 s->avctx->profile = bytestream2_get_be16u(&
s->g);
214 s->width = bytestream2_get_be32u(&
s->g);
215 s->height = bytestream2_get_be32u(&
s->g);
216 s->image_offset_x = bytestream2_get_be32u(&
s->g);
217 s->image_offset_y = bytestream2_get_be32u(&
s->g);
218 s->tile_width = bytestream2_get_be32u(&
s->g);
219 s->tile_height = bytestream2_get_be32u(&
s->g);
220 s->tile_offset_x = bytestream2_get_be32u(&
s->g);
221 s->tile_offset_y = bytestream2_get_be32u(&
s->g);
222 ncomponents = bytestream2_get_be16u(&
s->g);
229 if (ncomponents <= 0) {
235 if (ncomponents > 4) {
241 if (
s->tile_offset_x < 0 ||
s->tile_offset_y < 0 ||
242 s->image_offset_x <
s->tile_offset_x ||
243 s->image_offset_y <
s->tile_offset_y ||
244 s->tile_width + (
int64_t)
s->tile_offset_x <=
s->image_offset_x ||
245 s->tile_height + (
int64_t)
s->tile_offset_y <=
s->image_offset_y
251 if (
s->image_offset_x >=
s->width ||
s->image_offset_y >=
s->height) {
256 if (
s->reduction_factor && (
s->image_offset_x ||
s->image_offset_y) ){
257 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction factor with image offsets is not fully implemented");
261 s->ncomponents = ncomponents;
263 if (
s->tile_width <= 0 ||
s->tile_height <= 0) {
265 s->tile_width,
s->tile_height);
270 av_log(
s->avctx,
AV_LOG_ERROR,
"Insufficient space for %d components in SIZ\n",
s->ncomponents);
274 for (
i = 0;
i <
s->ncomponents;
i++) {
275 uint8_t x = bytestream2_get_byteu(&
s->g);
276 s->cbps[
i] = (x & 0x7f) + 1;
277 s->precision =
FFMAX(
s->cbps[
i],
s->precision);
278 s->sgnd[
i] = !!(x & 0x80);
279 s->cdx[
i] = bytestream2_get_byteu(&
s->g);
280 s->cdy[
i] = bytestream2_get_byteu(&
s->g);
281 if ( !
s->cdx[
i] ||
s->cdx[
i] == 3 ||
s->cdx[
i] > 4
282 || !
s->cdy[
i] ||
s->cdy[
i] == 3 ||
s->cdy[
i] > 4) {
286 log2_chroma_wh |=
s->cdy[
i] >> 1 <<
i * 4 |
s->cdx[
i] >> 1 <<
i * 4 + 2;
293 if (
s->numXtiles * (uint64_t)
s->numYtiles > INT_MAX/
sizeof(*
s->tile) ||
296 s->numXtiles =
s->numYtiles = 0;
300 s->tile =
av_calloc(
s->numXtiles *
s->numYtiles,
sizeof(*
s->tile));
302 s->numXtiles =
s->numYtiles = 0;
306 for (
i = 0;
i <
s->numXtiles *
s->numYtiles;
i++) {
316 s->reduction_factor);
318 s->reduction_factor);
321 for (
i = 1;
i <
s->ncomponents;
i++) {
335 switch (
s->colour_space) {
355 && !
pix_fmt_match(
s->avctx->pix_fmt, ncomponents,
s->precision, log2_chroma_wh,
s->pal8))
358 for (
i = 0;
i < possible_fmts_nb; ++
i) {
359 if (
pix_fmt_match(possible_fmts[
i], ncomponents,
s->precision, log2_chroma_wh,
s->pal8)) {
360 s->avctx->pix_fmt = possible_fmts[
i];
365 if (
i == possible_fmts_nb) {
366 if (ncomponents == 4 &&
367 s->cdy[0] == 1 &&
s->cdx[0] == 1 &&
368 s->cdy[1] == 1 &&
s->cdx[1] == 1 &&
369 s->cdy[2] ==
s->cdy[3] &&
s->cdx[2] ==
s->cdx[3]) {
370 if (
s->precision == 8 &&
s->cdy[2] == 2 &&
s->cdx[2] == 2 && !
s->pal8) {
378 }
else if (ncomponents == 3 &&
s->precision == 8 &&
379 s->cdx[0] ==
s->cdx[1] &&
s->cdx[0] ==
s->cdx[2] &&
380 s->cdy[0] ==
s->cdy[1] &&
s->cdy[0] ==
s->cdy[2]) {
383 }
else if (ncomponents == 2 &&
s->precision == 8 &&
384 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
387 }
else if (ncomponents == 2 &&
s->precision == 16 &&
388 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
391 }
else if (ncomponents == 1 &&
s->precision == 8) {
394 }
else if (ncomponents == 1 &&
s->precision == 12) {
401 if (
i == possible_fmts_nb) {
403 "Unknown pix_fmt, profile: %d, colour_space: %d, "
404 "components: %d, precision: %d\n"
405 "cdx[0]: %d, cdy[0]: %d\n"
406 "cdx[1]: %d, cdy[1]: %d\n"
407 "cdx[2]: %d, cdy[2]: %d\n"
408 "cdx[3]: %d, cdy[3]: %d\n",
409 s->avctx->profile,
s->colour_space, ncomponents,
s->precision,
412 ncomponents > 1 ?
s->cdx[1] : 0,
413 ncomponents > 1 ?
s->cdy[1] : 0,
414 ncomponents > 2 ?
s->cdx[2] : 0,
415 ncomponents > 2 ?
s->cdy[2] : 0,
416 ncomponents > 3 ?
s->cdx[3] : 0,
417 ncomponents > 3 ?
s->cdy[3] : 0);
420 s->avctx->bits_per_raw_sample =
s->precision;
427 uint16_t Ccap_i[32] = { 0 };
436 Pcap = bytestream2_get_be32u(&
s->g);
437 s->isHT = (Pcap >> (31 - (15 - 1))) & 1;
438 for (
int i = 0;
i < 32;
i++) {
439 if ((Pcap >> (31 -
i)) & 1)
440 Ccap_i[
i] = bytestream2_get_be16u(&
s->g);
442 Ccap_15 = Ccap_i[14];
446 switch ((Ccap_15 >> 14) & 0x3) {
462 if ((Ccap_15 >> 13) & 1) {
467 s->Ccap15_b12 = (Ccap_15 >> 12) & 1;
469 s->Ccap15_b11 = (Ccap_15 >> 11) & 1;
471 s->Ccap15_b05 = (Ccap_15 >> 5) & 1;
479 s->HT_B = 4 * (
P - 19) + 27;
503 c->nreslevels = bytestream2_get_byteu(&
s->g) + 1;
509 if (
c->nreslevels <=
s->reduction_factor) {
514 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction_factor too large for this bitstream, max is %d\n",
c->nreslevels - 1);
515 s->reduction_factor =
c->nreslevels - 1;
520 c->nreslevels2decode =
c->nreslevels -
s->reduction_factor;
522 c->log2_cblk_width = (bytestream2_get_byteu(&
s->g) & 15) + 2;
523 c->log2_cblk_height = (bytestream2_get_byteu(&
s->g) & 15) + 2;
525 if (
c->log2_cblk_width > 10 ||
c->log2_cblk_height > 10 ||
526 c->log2_cblk_width +
c->log2_cblk_height > 12) {
531 c->cblk_style = bytestream2_get_byteu(&
s->g);
532 if (
c->cblk_style != 0) {
541 c->transform = bytestream2_get_byteu(&
s->g);
551 for (
i = 0;
i <
c->nreslevels;
i++) {
552 byte = bytestream2_get_byte(&
s->g);
553 c->log2_prec_widths[
i] =
byte & 0x0F;
554 c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;
556 if (
c->log2_prec_widths[
i] == 0 ||
c->log2_prec_heights[
i] == 0) {
558 c->log2_prec_widths[
i],
c->log2_prec_heights[
i]);
559 c->log2_prec_widths[
i] =
c->log2_prec_heights[
i] = 1;
564 memset(
c->log2_prec_widths , 15,
sizeof(
c->log2_prec_widths ));
565 memset(
c->log2_prec_heights, 15,
sizeof(
c->log2_prec_heights));
572 const uint8_t *properties)
582 tmp.csty = bytestream2_get_byteu(&
s->g);
585 tmp.prog_order = bytestream2_get_byteu(&
s->g);
587 tmp.nlayers = bytestream2_get_be16u(&
s->g);
588 tmp.mct = bytestream2_get_byteu(&
s->g);
590 if (
tmp.mct &&
s->ncomponents < 3) {
592 "MCT %"PRIu8
" with too few components (%d)\n",
593 tmp.mct,
s->ncomponents);
600 for (compno = 0; compno <
s->ncomponents; compno++)
601 if (!(properties[compno] &
HAD_COC))
602 memcpy(
c + compno, &
tmp,
sizeof(
tmp));
612 uint8_t has_eph, has_sop;
619 compno = bytestream2_get_byteu(&
s->g);
621 if (compno >=
s->ncomponents) {
623 "Invalid compno %d. There are %d components in the image.\n",
624 compno,
s->ncomponents);
631 c->csty = bytestream2_get_byteu(&
s->g);
646 compno = (
s->ncomponents < 257)? bytestream2_get_byte(&
s->g):
647 bytestream2_get_be16u(&
s->g);
648 if (bytestream2_get_byte(&
s->g)) {
655 if (compno < s->ncomponents) {
657 if (
s->curtileno == -1) {
658 v = bytestream2_get_byte(&
s->g);
661 s->roi_shift[compno] = v;
663 if (
s->tile[
s->curtileno].tp_idx != 0)
665 v = bytestream2_get_byte(&
s->g);
668 s->tile[
s->curtileno].comp[compno].roi_shift = v;
683 x = bytestream2_get_byteu(&
s->g);
693 for (
i = 0;
i < n;
i++)
694 q->
expn[
i] = bytestream2_get_byteu(&
s->g) >> 3;
698 x = bytestream2_get_be16u(&
s->g);
699 q->
expn[0] = x >> 11;
700 q->
mant[0] = x & 0x7ff;
702 int curexpn =
FFMAX(0, q->
expn[0] - (
i - 1) / 3);
703 q->
expn[
i] = curexpn;
711 for (
i = 0;
i < n;
i++) {
712 x = bytestream2_get_be16u(&
s->g);
713 q->
expn[
i] = x >> 11;
714 q->
mant[
i] = x & 0x7ff;
722 const uint8_t *properties)
727 memset(&
tmp, 0,
sizeof(
tmp));
731 for (compno = 0; compno <
s->ncomponents; compno++)
732 if (!(properties[compno] &
HAD_QCC))
733 memcpy(q + compno, &
tmp,
sizeof(
tmp));
747 compno = bytestream2_get_byteu(&
s->g);
749 if (compno >=
s->ncomponents) {
751 "Invalid compno %d. There are %d components in the image.\n",
752 compno,
s->ncomponents);
757 return get_qcx(
s, n - 1, q + compno);
763 int elem_size =
s->ncomponents <= 257 ? 7 : 9;
776 tmp.nb_poc = (
size - 2) / elem_size;
782 for (
i = 0;
i<
tmp.nb_poc;
i++) {
784 e->
RSpoc = bytestream2_get_byteu(&
s->g);
785 e->
CSpoc = bytestream2_get_byteu(&
s->g);
786 e->
LYEpoc = bytestream2_get_be16u(&
s->g);
787 e->
REpoc = bytestream2_get_byteu(&
s->g);
788 e->
CEpoc = bytestream2_get_byteu(&
s->g);
789 e->
Ppoc = bytestream2_get_byteu(&
s->g);
792 if (e->
CEpoc >
s->ncomponents)
793 e->
CEpoc =
s->ncomponents;
833 Isot = bytestream2_get_be16u(&
s->g);
834 if (Isot >=
s->numXtiles *
s->numYtiles)
838 Psot = bytestream2_get_be32u(&
s->g);
839 TPsot = bytestream2_get_byteu(&
s->g);
842 bytestream2_get_byteu(&
s->g);
857 s->tile[Isot].tp_idx = TPsot;
858 tp =
s->tile[Isot].tile_part + TPsot;
860 tp->
tp_end =
s->g.buffer + Psot - n - 2;
868 memcpy(&tile->
poc , &
s->poc ,
sizeof(tile->
poc));
877 if (
s->ncomponents*4 != n - 2) {
903 uint8_t Stlm, ST,
SP, tile_tlm,
i;
904 bytestream2_get_byte(&
s->g);
905 Stlm = bytestream2_get_byte(&
s->g);
908 ST = (Stlm >> 4) & 0x03;
914 SP = (Stlm >> 6) & 0x01;
915 tile_tlm = (n - 4) / ((
SP + 1) * 2 + ST);
916 for (
i = 0;
i < tile_tlm;
i++) {
921 bytestream2_get_byte(&
s->g);
924 bytestream2_get_be16(&
s->g);
928 bytestream2_get_be16(&
s->g);
930 bytestream2_get_be32(&
s->g);
947 bytestream2_get_byte(&
s->g);
949 for (
i = 0;
i < n - 3;
i++) {
950 v = bytestream2_get_byte(&
s->g);
966 bytestream2_get_byte(&
s->g);
968 s->packed_headers_size + n - 3);
970 s->packed_headers =
new;
974 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
977 s->packed_headers_size += n - 3;
991 if (
s->curtileno < 0)
994 tile = &
s->tile[
s->curtileno];
997 "PPT marker can occur only on first tile part of a tile.\n");
1002 bytestream2_get_byte(&
s->g);
1019 int tilex = tileno %
s->numXtiles;
1020 int tiley = tileno /
s->numXtiles;
1027 tile->
coord[0][1] =
av_clip((tilex + 1) * (
int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
1028 tile->
coord[1][0] =
av_clip(tiley * (
int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1029 tile->
coord[1][1] =
av_clip((tiley + 1) * (
int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1031 for (compno = 0; compno <
s->ncomponents; compno++) {
1037 comp->coord_o[0][0] = tile->
coord[0][0];
1038 comp->coord_o[0][1] = tile->
coord[0][1];
1039 comp->coord_o[1][0] = tile->
coord[1][0];
1040 comp->coord_o[1][1] = tile->
coord[1][1];
1052 if (!
comp->roi_shift)
1053 comp->roi_shift =
s->roi_shift[compno];
1056 if (
s->isHT && (!
s->Ccap15_b05) && (!codsty->
transform)) {
1057 av_log(
s->avctx,
AV_LOG_ERROR,
"Transformation = 0 (lossy DWT) is found in HTREV HT set\n");
1061 av_log(
s->avctx,
AV_LOG_ERROR,
"SPcod/SPcoc value does not match bit 14-15 values of Ccap15\n");
1065 s->cbps[compno],
s->cdx[compno],
1066 s->cdy[compno],
s->avctx))
1081 return num < 0 ? num : 3 + num;
1083 return num < 0 ? num : 6 + num;
1085 return num < 0 ? num : 37 + num;
1120 while (is_endof_tp) {
1132 av_log(
s->avctx,
AV_LOG_ERROR,
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1139 int layno,
const uint8_t *expn,
int numgbits)
1141 int bandno, cblkno,
ret, nb_code_blocks;
1144 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1161 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1165 if (band->
coord[0][0] == band->
coord[0][1] ||
1170 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1172 int incl, newpasses, llen;
1186 int v = expn[bandno] + numgbits - 1 - (zbp - tile->
comp->
roi_shift);
1187 if (v < 0 || v > 30) {
1189 "nonzerobits %d invalid or unsupported\n", v);
1202 uint8_t bypass_term_threshold = 0;
1203 uint8_t bits_to_read = 0;
1204 uint32_t segment_bytes = 0;
1206 uint8_t next_segment_passes = 0;
1207 int32_t href_passes, pass_bound;
1208 uint32_t tmp_length = 0;
1209 int32_t newpasses_copy, npasses_copy;
1221 "Block with length beyond 16 bits");
1238 newpasses_copy = newpasses;
1244 while (newpasses1 < newpasses_copy) {
1251 npasses_copy += newpasses1;
1252 newpasses_copy -= newpasses1;
1253 }
while (newpasses_copy);
1257 href_passes = (cblk->
npasses + newpasses - 1) % 3;
1258 segment_passes = newpasses - href_passes;
1260 bits_to_read = cblk->
lblock;
1261 if (segment_passes < 1) {
1264 segment_passes = newpasses;
1265 while (pass_bound <= segment_passes) {
1267 pass_bound += pass_bound;
1269 segment_bytes =
get_bits(
s, bits_to_read);
1270 if (segment_bytes) {
1280 while (pass_bound <= segment_passes) {
1282 pass_bound += pass_bound;
1284 segment_bytes =
get_bits(
s, bits_to_read);
1285 if (segment_bytes) {
1289 if (segment_bytes < 2)
1291 next_segment_passes = 2;
1295 }
else if (cblk->
lblock > 3 && segment_bytes > 1
1296 && (segment_bytes >> (bits_to_read - 1)) == 0) {
1298 next_segment_passes = 2;
1306 segment_passes = newpasses;
1307 while (pass_bound <= segment_passes) {
1309 pass_bound += pass_bound;
1310 segment_bytes <<= 1;
1320 segment_passes = newpasses;
1321 if (pass_bound <= segment_passes) {
1324 pass_bound += pass_bound;
1325 segment_bytes <<= 1;
1327 if (pass_bound > segment_passes)
1330 if (segment_bytes) {
1343 if(bits_to_read != 0)
1345 segment_passes = cblk->
npasses % 3;
1346 if (segment_passes == 0) {
1349 next_segment_passes = 2;
1350 if (segment_bytes == 1)
1355 segment_passes = newpasses > 1 ? 3 - segment_passes : 1;
1356 next_segment_passes = 1;
1357 bits_to_read =
av_log2(segment_passes);
1359 bits_to_read = (uint8_t) (bits_to_read + cblk->
lblock);
1360 segment_bytes =
get_bits(
s, bits_to_read);
1365 bits_to_read = (uint8_t) cblk->
lblock +
av_log2((uint8_t) newpasses);
1366 segment_bytes =
get_bits(
s, bits_to_read);
1367 segment_passes = newpasses;
1370 bits_to_read = cblk->
lblock;
1371 segment_bytes =
get_bits(
s, bits_to_read);
1373 next_segment_passes = 1;
1376 bypass_term_threshold = 10;
1377 if(bits_to_read != 0)
1379 if (cblk->
npasses < bypass_term_threshold) {
1381 segment_passes = bypass_term_threshold - cblk->
npasses;
1382 if (segment_passes > newpasses)
1383 segment_passes = newpasses;
1384 while ((2 << bits_to_read) <= segment_passes)
1386 next_segment_passes = 2;
1387 }
else if ((cblk->
npasses - bypass_term_threshold) % 3 < 2) {
1389 segment_passes = newpasses > 1 ? 2 - (cblk->
npasses - bypass_term_threshold) % 3 : 1;
1390 bits_to_read =
av_log2(segment_passes);
1391 next_segment_passes = 1;
1395 next_segment_passes = 2;
1397 bits_to_read = (uint8_t) (bits_to_read + cblk->
lblock);
1398 segment_bytes =
get_bits(
s, bits_to_read);
1405 newpasses -= (uint8_t) segment_passes;
1406 while (newpasses > 0) {
1407 segment_passes = newpasses > 1 ? next_segment_passes : 1;
1408 next_segment_passes = (uint8_t) (3 - next_segment_passes);
1409 bits_to_read = (uint8_t) (cblk->
lblock +
av_log2(segment_passes));
1410 segment_bytes =
get_bits(
s, bits_to_read);
1411 newpasses -= (uint8_t) (segment_passes);
1420 newpasses -= (uint8_t) (segment_passes);
1421 while (newpasses > 0) {
1422 if (bypass_term_threshold != 0) {
1423 segment_passes = newpasses > 1 ? next_segment_passes : 1;
1424 next_segment_passes = (uint8_t) (3 - next_segment_passes);
1425 bits_to_read = (uint8_t) (cblk->
lblock +
av_log2(segment_passes));
1430 bits_to_read = cblk->
lblock;
1432 segment_bytes =
get_bits(
s, bits_to_read);
1433 newpasses -= (uint8_t) (segment_passes);
1442 tmp_length = (tmp_length < cblk->lengthinc[
i]) ? cblk->
lengthinc[
i] : tmp_length;
1470 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1481 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1486 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1490 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1503 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1531 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1545 int RSpoc,
int CSpoc,
1546 int LYEpoc,
int REpoc,
int CEpoc,
1547 int Ppoc,
int *tp_index)
1550 int layno, reslevelno, compno, precno, ok_reslevel;
1558 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1560 for (layno = 0; layno < LYEpoc; layno++) {
1561 for (compno = CSpoc; compno < CEpoc; compno++) {
1564 if (reslevelno < codsty->nreslevels) {
1572 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1583 for (layno = 0; layno < LYEpoc; layno++) {
1585 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1587 for (compno = CSpoc; compno < CEpoc; compno++) {
1590 if (reslevelno < codsty->nreslevels) {
1598 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1609 for (compno = CSpoc; compno < CEpoc; compno++) {
1619 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1620 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1625 if (step_x >= 31 || step_y >= 31){
1632 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1633 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1634 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1635 unsigned prcx, prcy;
1636 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1638 int xc = x /
s->cdx[compno];
1639 int yc = y /
s->cdy[compno];
1661 for (layno = 0; layno < LYEpoc; layno++) {
1664 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1677 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1681 for (compno = CSpoc; compno < CEpoc; compno++) {
1685 if (reslevelno < codsty->nreslevels) {
1686 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1695 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1696 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1697 for (compno = CSpoc; compno < CEpoc; compno++) {
1701 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1703 unsigned prcx, prcy;
1706 if (!
s->cdx[compno] || !
s->cdy[compno])
1715 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1719 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1738 for (layno = 0; layno < LYEpoc; layno++) {
1742 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1756 for (compno = CSpoc; compno < CEpoc; compno++) {
1760 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1761 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1767 if (step_x >= 31 || step_y >= 31){
1774 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1775 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1776 for (compno = CSpoc; compno < CEpoc; compno++) {
1781 if (!
s->cdx[compno] || !
s->cdy[compno])
1784 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1785 unsigned prcx, prcy;
1786 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1793 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1797 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1815 for (layno = 0; layno < LYEpoc; layno++) {
1818 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1873 int bpno,
int bandno,
1874 int vert_causal_ctx_csty_symbol)
1876 int mask = 3 << (bpno - 1), y0, x, y;
1878 for (y0 = 0; y0 <
height; y0 += 4)
1879 for (x = 0; x <
width; x++)
1880 for (y = y0; y <
height && y < y0 + 4; y++) {
1881 int flags_mask = -1;
1882 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1904 int bpno,
int vert_causal_ctx_csty_symbol)
1909 phalf = 1 << (bpno - 1);
1911 for (y0 = 0; y0 <
height; y0 += 4)
1912 for (x = 0; x <
width; x++)
1913 for (y = y0; y <
height && y < y0 + 4; y++)
1915 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1920 t1->
data[(y) * t1->
stride + x] |= phalf << 1;
1922 t1->
data[(y) * t1->
stride + x] &= ~(phalf << 1);
1931 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1933 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1935 for (y0 = 0; y0 <
height; y0 += 4) {
1936 for (x = 0; x <
width; x++) {
1937 int flags_mask = -1;
1938 if (vert_causal_ctx_csty_symbol)
1958 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1959 int flags_mask = -1;
1960 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1989 "Segmentation symbol value incorrect\n");
1995 int width,
int height,
int bandpos, uint8_t roi_shift,
const int M_b)
1997 int passno = cblk->
npasses, pass_t = 2, bpno = cblk->
nonzerobits - 1 + 31 - M_b - 1 - roi_shift;
2019 if (bpno < 0 || bpno > 29) {
2026 vert_causal_ctx_csty_symbol);
2035 vert_causal_ctx_csty_symbol);
2073 for (
int y = 0; y <
height; y++) {
2074 for (
int x = 0; x <
width; x++) {
2076 const uint32_t
mask = UINT32_MAX >> (M_b + 1);
2078 n = x + (y * t1->
stride);
2080 sign =
val & INT32_MIN;
2083 if (roi_shift && (((uint32_t)
val & ~
mask) == 0))
2104 const int downshift = 31 - M_b;
2106 fscale /= (
float)(1 << downshift);
2107 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
2108 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
2110 for (
i = 0;
i <
w; ++
i) {
2113 val = -(
val & INT32_MAX);
2125 const int downshift = 31 - M_b;
2127 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
2131 for (
i = 0;
i <
w; ++
i) {
2134 val = -((
val & INT32_MAX) >> downshift);
2141 for (
i = 0;
i <
w; ++
i) {
2144 val = -((
val & INT32_MAX) >> downshift);
2160 const int downshift = 31 - M_b;
2161 const int PRESCALE = 6;
2164 fscale /= (
float)(1 << downshift);
2165 fscale *= (
float)(1 << PRESCALE);
2167 scale = (int)(fscale + 0.5);
2169 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
2172 for (
i = 0;
i <
w; ++
i) {
2175 val = -(
val & INT32_MAX);
2177 val = (
val + (1 << (PRESCALE - 1))) >> PRESCALE;
2188 for (
i = 1;
i < 3;
i++) {
2199 for (
i = 0;
i < 3;
i++)
2205 for (
i = 0;
i < 2;
i++)
2216 int compno, reslevelno, bandno;
2219 for (compno = 0; compno <
s->ncomponents; compno++) {
2233 for (bandno = 0; bandno < rlevel->
nbands; bandno++, subbandno++) {
2234 int nb_precincts, precno;
2236 int cblkno = 0, bandpos;
2240 bandpos = bandno + (reslevelno > 0);
2242 if (band->
coord[0][0] == band->
coord[0][1] ||
2253 for (precno = 0; precno < nb_precincts; precno++) {
2268 M_b,
comp->roi_shift);
2273 bandpos,
comp->roi_shift, M_b);
2301 #define WRITE_FRAME(D, PIXEL) \
2302 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2303 AVFrame * picture, int precision) \
2305 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2306 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2307 int pixelsize = planar ? 1 : pixdesc->nb_components; \
2312 for (compno = 0; compno < s->ncomponents; compno++) { \
2313 Jpeg2000Component *comp = tile->comp + compno; \
2314 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2316 float *datap = comp->f_data; \
2317 int32_t *i_datap = comp->i_data; \
2318 int cbps = s->cbps[compno]; \
2319 int w = tile->comp[compno].coord[0][1] - \
2320 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2321 int h = tile->comp[compno].coord[1][1] - \
2322 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2326 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2328 y = tile->comp[compno].coord[1][0] - \
2329 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2330 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2331 for (; y < h; y++) { \
2334 x = tile->comp[compno].coord[0][0] - \
2335 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2336 dst = line + x * pixelsize + compno*!planar; \
2338 if (codsty->transform == FF_DWT97) { \
2339 for (; x < w; x++) { \
2340 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2342 val = av_clip(val, 0, (1 << cbps) - 1); \
2343 *dst = val << (precision - cbps); \
2348 for (; x < w; x++) { \
2349 int val = *i_datap + (1 << (cbps - 1)); \
2351 val = av_clip(val, 0, (1 << cbps) - 1); \
2352 *dst = val << (precision - cbps); \
2357 line += picture->linesize[plane] / sizeof(PIXEL); \
2369 int jobnr,
int threadnr)
2383 if (
s->precision <= 8) {
2384 write_frame_8(
s, tile, picture, 8);
2391 write_frame_16(
s, tile, picture, precision);
2400 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2401 if (
s->tile[tileno].comp) {
2402 for (compno = 0; compno <
s->ncomponents; compno++) {
2409 av_freep(&
s->tile[tileno].packed_headers);
2410 s->tile[tileno].packed_headers_size = 0;
2414 s->packed_headers_size = 0;
2415 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2417 memset(
s->codsty, 0,
sizeof(
s->codsty));
2418 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2419 memset(
s->properties, 0,
sizeof(
s->properties));
2420 memset(&
s->poc , 0,
sizeof(
s->poc));
2421 s->numXtiles =
s->numYtiles = 0;
2430 uint8_t *properties =
s->properties;
2431 uint8_t in_tile_headers = 0;
2443 marker = bytestream2_get_be16u(&
s->g);
2445 if (marker >= 0xFF30 && marker <= 0xFF3F)
2455 if (
s->curtileno < 0) {
2460 tile =
s->tile +
s->curtileno;
2462 if (tp->
tp_end <
s->g.buffer) {
2468 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2486 len = bytestream2_get_be16(&
s->g);
2498 if (
s->ncomponents) {
2504 s->numXtiles =
s->numYtiles = 0;
2507 if (!
s->ncomponents) {
2514 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2516 "COC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2522 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2524 "COD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2530 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2532 "RGN marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2536 if ((!
s->Ccap15_b12) &&
s->isHT) {
2537 av_log(
s->avctx,
AV_LOG_ERROR,
"RGN marker found but the codestream belongs to the RGNFREE set\n");
2542 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2544 "QCC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2550 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2552 "QCD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2558 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2560 "POC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2566 if (!in_tile_headers) {
2567 in_tile_headers = 1;
2574 codsty =
s->tile[
s->curtileno].codsty;
2575 qntsty =
s->tile[
s->curtileno].qntsty;
2576 poc = &
s->tile[
s->curtileno].poc;
2577 properties =
s->tile[
s->curtileno].properties;
2599 if (in_tile_headers) {
2609 "Cannot have both PPT and PPM marker.\n");
2612 if ((!
s->Ccap15_b11) &&
s->isHT) {
2613 av_log(
s->avctx,
AV_LOG_ERROR,
"PPT marker found but the codestream belongs to the HOMOGENEOUS set\n");
2624 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2631 "error during processing marker segment %.4"PRIx16
"\n",
2645 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2660 uint32_t atom_size, atom, atom_end;
2661 int search_range = 10;
2666 atom_size = bytestream2_get_be32u(&
s->g);
2667 atom = bytestream2_get_be32u(&
s->g);
2668 if (atom_size == 1) {
2669 if (bytestream2_get_be32u(&
s->g)) {
2673 atom_size = bytestream2_get_be32u(&
s->g);
2691 uint32_t atom2_size, atom2, atom2_end;
2695 atom2_size = bytestream2_get_be32u(&
s->g);
2696 atom2 = bytestream2_get_be32u(&
s->g);
2698 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2703 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2704 int method = bytestream2_get_byteu(&
s->g);
2707 s->colour_space = bytestream2_get_be32u(&
s->g);
2709 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2710 int i,
size, colour_count, colour_channels, colour_depth[3];
2711 colour_count = bytestream2_get_be16u(&
s->g);
2712 colour_channels = bytestream2_get_byteu(&
s->g);
2714 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2715 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2716 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2717 size = (colour_depth[0] + 7 >> 3) * colour_count +
2718 (colour_depth[1] + 7 >> 3) * colour_count +
2719 (colour_depth[2] + 7 >> 3) * colour_count;
2721 colour_channels != 3 ||
2722 colour_depth[0] > 16 ||
2723 colour_depth[1] > 16 ||
2724 colour_depth[2] > 16 ||
2725 atom2_size <
size) {
2731 for (
i = 0;
i < colour_count;
i++) {
2733 if (colour_depth[0] <= 8) {
2734 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2735 r |=
r >> colour_depth[0];
2737 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2739 if (colour_depth[1] <= 8) {
2740 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2741 g |=
g >> colour_depth[1];
2743 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2745 if (colour_depth[2] <= 8) {
2746 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2747 b |=
b >> colour_depth[2];
2749 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2751 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2753 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2754 int n = bytestream2_get_be16u(&
s->g);
2756 int cn = bytestream2_get_be16(&
s->g);
2757 int av_unused typ = bytestream2_get_be16(&
s->g);
2758 int asoc = bytestream2_get_be16(&
s->g);
2759 if (cn < 4 && asoc < 4)
2762 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2763 int64_t vnum, vden, hnum, hden, vexp, hexp;
2766 resx = bytestream2_get_be32u(&
s->g);
2767 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2771 vnum = bytestream2_get_be16u(&
s->g);
2772 vden = bytestream2_get_be16u(&
s->g);
2773 hnum = bytestream2_get_be16u(&
s->g);
2774 hden = bytestream2_get_be16u(&
s->g);
2775 vexp = bytestream2_get_byteu(&
s->g);
2776 hexp = bytestream2_get_byteu(&
s->g);
2777 if (!vnum || !vden || !hnum || !hden) {
2789 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2790 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2792 hnum * vden * pow(10, hexp),
2793 vnum * hden * pow(10, vexp),
2797 }
while (atom_end - atom2_end >= 8);
2814 s->reduction_factor = avctx->
lowres;
2834 memset(
s->cdef, -1,
sizeof(
s->cdef));
2843 (bytestream2_get_be32u(&
s->g) == 12) &&
2848 "Could not find Jpeg2000 codestream atom.\n");
2867 if (
s->sar.num &&
s->sar.den)
2869 s->sar.num =
s->sar.den = 0;
2883 for (
int x = 0; x <
s->ncomponents; x++) {
2884 if (
s->cdef[x] < 0) {
2885 for (x = 0; x <
s->ncomponents; x++) {
2888 if ((
s->ncomponents & 1) == 0)
2889 s->cdef[
s->ncomponents-1] = 0;
2901 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2910 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2911 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2914 {
"lowres",
"Lower the decoding resolution by a power of two",
2927 .
p.
name =
"jpeg2000",