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);
545 #if FF_API_CODEC_PROPS
555 for (
i = 0;
i <
c->nreslevels;
i++) {
556 byte = bytestream2_get_byte(&
s->g);
557 c->log2_prec_widths[
i] =
byte & 0x0F;
558 c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;
560 if (
c->log2_prec_widths[
i] == 0 ||
c->log2_prec_heights[
i] == 0) {
562 c->log2_prec_widths[
i],
c->log2_prec_heights[
i]);
563 c->log2_prec_widths[
i] =
c->log2_prec_heights[
i] = 1;
568 memset(
c->log2_prec_widths , 15,
sizeof(
c->log2_prec_widths ));
569 memset(
c->log2_prec_heights, 15,
sizeof(
c->log2_prec_heights));
576 const uint8_t *properties)
586 tmp.csty = bytestream2_get_byteu(&
s->g);
589 tmp.prog_order = bytestream2_get_byteu(&
s->g);
591 tmp.nlayers = bytestream2_get_be16u(&
s->g);
592 tmp.mct = bytestream2_get_byteu(&
s->g);
594 if (
tmp.mct &&
s->ncomponents < 3) {
596 "MCT %"PRIu8
" with too few components (%d)\n",
597 tmp.mct,
s->ncomponents);
604 for (compno = 0; compno <
s->ncomponents; compno++)
605 if (!(properties[compno] &
HAD_COC))
606 memcpy(
c + compno, &
tmp,
sizeof(
tmp));
616 uint8_t has_eph, has_sop;
623 compno = bytestream2_get_byteu(&
s->g);
625 if (compno >=
s->ncomponents) {
627 "Invalid compno %d. There are %d components in the image.\n",
628 compno,
s->ncomponents);
635 c->csty = bytestream2_get_byteu(&
s->g);
650 compno = (
s->ncomponents < 257)? bytestream2_get_byte(&
s->g):
651 bytestream2_get_be16u(&
s->g);
652 if (bytestream2_get_byte(&
s->g)) {
659 if (compno < s->ncomponents) {
661 if (
s->curtileno == -1) {
662 v = bytestream2_get_byte(&
s->g);
665 s->roi_shift[compno] = v;
667 if (
s->tile[
s->curtileno].tp_idx != 0)
669 v = bytestream2_get_byte(&
s->g);
672 s->tile[
s->curtileno].comp[compno].roi_shift = v;
687 x = bytestream2_get_byteu(&
s->g);
697 for (
i = 0;
i < n;
i++)
698 q->
expn[
i] = bytestream2_get_byteu(&
s->g) >> 3;
702 x = bytestream2_get_be16u(&
s->g);
703 q->
expn[0] = x >> 11;
704 q->
mant[0] = x & 0x7ff;
706 int curexpn =
FFMAX(0, q->
expn[0] - (
i - 1) / 3);
707 q->
expn[
i] = curexpn;
715 for (
i = 0;
i < n;
i++) {
716 x = bytestream2_get_be16u(&
s->g);
717 q->
expn[
i] = x >> 11;
718 q->
mant[
i] = x & 0x7ff;
726 const uint8_t *properties)
731 memset(&
tmp, 0,
sizeof(
tmp));
735 for (compno = 0; compno <
s->ncomponents; compno++)
736 if (!(properties[compno] &
HAD_QCC))
737 memcpy(q + compno, &
tmp,
sizeof(
tmp));
751 compno = bytestream2_get_byteu(&
s->g);
753 if (compno >=
s->ncomponents) {
755 "Invalid compno %d. There are %d components in the image.\n",
756 compno,
s->ncomponents);
761 return get_qcx(
s, n - 1, q + compno);
767 int elem_size =
s->ncomponents <= 257 ? 7 : 9;
780 tmp.nb_poc = (
size - 2) / elem_size;
786 for (
i = 0;
i<
tmp.nb_poc;
i++) {
788 e->
RSpoc = bytestream2_get_byteu(&
s->g);
789 e->
CSpoc = bytestream2_get_byteu(&
s->g);
790 e->
LYEpoc = bytestream2_get_be16u(&
s->g);
791 e->
REpoc = bytestream2_get_byteu(&
s->g);
792 e->
CEpoc = bytestream2_get_byteu(&
s->g);
793 e->
Ppoc = bytestream2_get_byteu(&
s->g);
796 if (e->
CEpoc >
s->ncomponents)
797 e->
CEpoc =
s->ncomponents;
837 Isot = bytestream2_get_be16u(&
s->g);
838 if (Isot >=
s->numXtiles *
s->numYtiles)
842 Psot = bytestream2_get_be32u(&
s->g);
843 TPsot = bytestream2_get_byteu(&
s->g);
846 bytestream2_get_byteu(&
s->g);
861 s->tile[Isot].tp_idx = TPsot;
862 tp =
s->tile[Isot].tile_part + TPsot;
864 tp->
tp_end =
s->g.buffer + Psot - n - 2;
872 memcpy(&tile->
poc , &
s->poc ,
sizeof(tile->
poc));
881 if (
s->ncomponents*4 != n - 2) {
907 uint8_t Stlm, ST,
SP, tile_tlm,
i;
908 bytestream2_get_byte(&
s->g);
909 Stlm = bytestream2_get_byte(&
s->g);
912 ST = (Stlm >> 4) & 0x03;
918 SP = (Stlm >> 6) & 0x01;
919 tile_tlm = (n - 4) / ((
SP + 1) * 2 + ST);
920 for (
i = 0;
i < tile_tlm;
i++) {
925 bytestream2_get_byte(&
s->g);
928 bytestream2_get_be16(&
s->g);
932 bytestream2_get_be16(&
s->g);
934 bytestream2_get_be32(&
s->g);
951 bytestream2_get_byte(&
s->g);
953 for (
i = 0;
i < n - 3;
i++) {
954 v = bytestream2_get_byte(&
s->g);
970 bytestream2_get_byte(&
s->g);
972 s->packed_headers_size + n - 3);
974 s->packed_headers =
new;
978 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
981 s->packed_headers_size += n - 3;
995 if (
s->curtileno < 0)
998 tile = &
s->tile[
s->curtileno];
1001 "PPT marker can occur only on first tile part of a tile.\n");
1006 bytestream2_get_byte(&
s->g);
1023 int tilex = tileno %
s->numXtiles;
1024 int tiley = tileno /
s->numXtiles;
1031 tile->
coord[0][1] =
av_clip((tilex + 1) * (
int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
1032 tile->
coord[1][0] =
av_clip(tiley * (
int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1033 tile->
coord[1][1] =
av_clip((tiley + 1) * (
int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1035 for (compno = 0; compno <
s->ncomponents; compno++) {
1041 comp->coord_o[0][0] = tile->
coord[0][0];
1042 comp->coord_o[0][1] = tile->
coord[0][1];
1043 comp->coord_o[1][0] = tile->
coord[1][0];
1044 comp->coord_o[1][1] = tile->
coord[1][1];
1056 if (!
comp->roi_shift)
1057 comp->roi_shift =
s->roi_shift[compno];
1060 if (
s->isHT && (!
s->Ccap15_b05) && (!codsty->
transform)) {
1061 av_log(
s->avctx,
AV_LOG_ERROR,
"Transformation = 0 (lossy DWT) is found in HTREV HT set\n");
1065 av_log(
s->avctx,
AV_LOG_ERROR,
"SPcod/SPcoc value does not match bit 14-15 values of Ccap15\n");
1069 s->cbps[compno],
s->cdx[compno],
1070 s->cdy[compno],
s->avctx))
1085 return num < 0 ? num : 3 + num;
1087 return num < 0 ? num : 6 + num;
1089 return num < 0 ? num : 37 + num;
1124 while (is_endof_tp) {
1136 av_log(
s->avctx,
AV_LOG_ERROR,
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1143 int layno,
const uint8_t *expn,
int numgbits)
1145 int bandno, cblkno,
ret, nb_code_blocks;
1148 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1165 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1169 if (band->
coord[0][0] == band->
coord[0][1] ||
1174 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1176 int incl, newpasses, llen;
1190 int v = expn[bandno] + numgbits - 1 - (zbp - tile->
comp->
roi_shift);
1191 if (v < 0 || v > 30) {
1193 "nonzerobits %d invalid or unsupported\n", v);
1206 uint8_t bypass_term_threshold = 0;
1207 uint8_t bits_to_read = 0;
1208 uint32_t segment_bytes = 0;
1210 uint8_t next_segment_passes = 0;
1211 int32_t href_passes, pass_bound;
1212 uint32_t tmp_length = 0;
1213 int32_t newpasses_copy, npasses_copy;
1225 "Block with length beyond 16 bits");
1242 newpasses_copy = newpasses;
1248 while (newpasses1 < newpasses_copy) {
1255 npasses_copy += newpasses1;
1256 newpasses_copy -= newpasses1;
1257 }
while (newpasses_copy);
1261 href_passes = (cblk->
npasses + newpasses - 1) % 3;
1262 segment_passes = newpasses - href_passes;
1264 bits_to_read = cblk->
lblock;
1265 if (segment_passes < 1) {
1268 segment_passes = newpasses;
1269 while (pass_bound <= segment_passes) {
1271 pass_bound += pass_bound;
1273 segment_bytes =
get_bits(
s, bits_to_read);
1274 if (segment_bytes) {
1284 while (pass_bound <= segment_passes) {
1286 pass_bound += pass_bound;
1288 segment_bytes =
get_bits(
s, bits_to_read);
1289 if (segment_bytes) {
1293 if (segment_bytes < 2)
1295 next_segment_passes = 2;
1299 }
else if (cblk->
lblock > 3 && segment_bytes > 1
1300 && (segment_bytes >> (bits_to_read - 1)) == 0) {
1302 next_segment_passes = 2;
1310 segment_passes = newpasses;
1311 while (pass_bound <= segment_passes) {
1313 pass_bound += pass_bound;
1314 segment_bytes <<= 1;
1324 segment_passes = newpasses;
1325 if (pass_bound <= segment_passes) {
1328 pass_bound += pass_bound;
1329 segment_bytes <<= 1;
1331 if (pass_bound > segment_passes)
1334 if (segment_bytes) {
1347 if(bits_to_read != 0)
1349 segment_passes = cblk->
npasses % 3;
1350 if (segment_passes == 0) {
1353 next_segment_passes = 2;
1354 if (segment_bytes == 1)
1359 segment_passes = newpasses > 1 ? 3 - segment_passes : 1;
1360 next_segment_passes = 1;
1361 bits_to_read =
av_log2(segment_passes);
1363 bits_to_read = (uint8_t) (bits_to_read + cblk->
lblock);
1364 segment_bytes =
get_bits(
s, bits_to_read);
1369 bits_to_read = (uint8_t) cblk->
lblock +
av_log2((uint8_t) newpasses);
1370 segment_bytes =
get_bits(
s, bits_to_read);
1371 segment_passes = newpasses;
1374 bits_to_read = cblk->
lblock;
1375 segment_bytes =
get_bits(
s, bits_to_read);
1377 next_segment_passes = 1;
1380 bypass_term_threshold = 10;
1381 if(bits_to_read != 0)
1383 if (cblk->
npasses < bypass_term_threshold) {
1385 segment_passes = bypass_term_threshold - cblk->
npasses;
1386 if (segment_passes > newpasses)
1387 segment_passes = newpasses;
1388 while ((2 << bits_to_read) <= segment_passes)
1390 next_segment_passes = 2;
1391 }
else if ((cblk->
npasses - bypass_term_threshold) % 3 < 2) {
1393 segment_passes = newpasses > 1 ? 2 - (cblk->
npasses - bypass_term_threshold) % 3 : 1;
1394 bits_to_read =
av_log2(segment_passes);
1395 next_segment_passes = 1;
1399 next_segment_passes = 2;
1401 bits_to_read = (uint8_t) (bits_to_read + cblk->
lblock);
1402 segment_bytes =
get_bits(
s, bits_to_read);
1409 newpasses -= (uint8_t) segment_passes;
1410 while (newpasses > 0) {
1411 segment_passes = newpasses > 1 ? next_segment_passes : 1;
1412 next_segment_passes = (uint8_t) (3 - next_segment_passes);
1413 bits_to_read = (uint8_t) (cblk->
lblock +
av_log2(segment_passes));
1414 segment_bytes =
get_bits(
s, bits_to_read);
1415 newpasses -= (uint8_t) (segment_passes);
1424 newpasses -= (uint8_t) (segment_passes);
1425 while (newpasses > 0) {
1426 if (bypass_term_threshold != 0) {
1427 segment_passes = newpasses > 1 ? next_segment_passes : 1;
1428 next_segment_passes = (uint8_t) (3 - next_segment_passes);
1429 bits_to_read = (uint8_t) (cblk->
lblock +
av_log2(segment_passes));
1434 bits_to_read = cblk->
lblock;
1436 segment_bytes =
get_bits(
s, bits_to_read);
1437 newpasses -= (uint8_t) (segment_passes);
1446 tmp_length = (tmp_length < cblk->lengthinc[
i]) ? cblk->
lengthinc[
i] : tmp_length;
1474 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1485 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1490 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1494 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1507 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1536 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1550 int RSpoc,
int CSpoc,
1551 int LYEpoc,
int REpoc,
int CEpoc,
1552 int Ppoc,
int *tp_index)
1555 int layno, reslevelno, compno, precno, ok_reslevel;
1563 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1565 for (layno = 0; layno < LYEpoc; layno++) {
1566 for (compno = CSpoc; compno < CEpoc; compno++) {
1569 if (reslevelno < codsty->nreslevels) {
1577 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1588 for (layno = 0; layno < LYEpoc; layno++) {
1590 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1592 for (compno = CSpoc; compno < CEpoc; compno++) {
1595 if (reslevelno < codsty->nreslevels) {
1603 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1614 for (compno = CSpoc; compno < CEpoc; compno++) {
1624 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1625 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1630 if (step_x >= 31 || step_y >= 31){
1637 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1638 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1639 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1640 unsigned prcx, prcy;
1641 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1643 int xc = x /
s->cdx[compno];
1644 int yc = y /
s->cdy[compno];
1666 for (layno = 0; layno < LYEpoc; layno++) {
1669 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1682 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1686 for (compno = CSpoc; compno < CEpoc; compno++) {
1690 if (reslevelno < codsty->nreslevels) {
1691 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1700 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1701 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1702 for (compno = CSpoc; compno < CEpoc; compno++) {
1706 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1708 unsigned prcx, prcy;
1711 if (!
s->cdx[compno] || !
s->cdy[compno])
1720 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1724 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1743 for (layno = 0; layno < LYEpoc; layno++) {
1747 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1761 for (compno = CSpoc; compno < CEpoc; compno++) {
1765 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1766 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1772 if (step_x >= 31 || step_y >= 31){
1779 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1780 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1781 for (compno = CSpoc; compno < CEpoc; compno++) {
1786 if (!
s->cdx[compno] || !
s->cdy[compno])
1789 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1790 unsigned prcx, prcy;
1791 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1798 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1802 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1820 for (layno = 0; layno < LYEpoc; layno++) {
1823 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1878 int bpno,
int bandno,
1879 int vert_causal_ctx_csty_symbol)
1881 int mask = 3 << (bpno - 1), y0, x, y;
1883 for (y0 = 0; y0 <
height; y0 += 4)
1884 for (x = 0; x <
width; x++)
1885 for (y = y0; y <
height && y < y0 + 4; y++) {
1886 int flags_mask = -1;
1887 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1909 int bpno,
int vert_causal_ctx_csty_symbol)
1914 phalf = 1 << (bpno - 1);
1916 for (y0 = 0; y0 <
height; y0 += 4)
1917 for (x = 0; x <
width; x++)
1918 for (y = y0; y <
height && y < y0 + 4; y++)
1920 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1925 t1->
data[(y) * t1->
stride + x] |= phalf << 1;
1927 t1->
data[(y) * t1->
stride + x] &= ~(phalf << 1);
1936 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1938 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1940 for (y0 = 0; y0 <
height; y0 += 4) {
1941 for (x = 0; x <
width; x++) {
1942 int flags_mask = -1;
1943 if (vert_causal_ctx_csty_symbol)
1963 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1964 int flags_mask = -1;
1965 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1994 "Segmentation symbol value incorrect\n");
2000 int width,
int height,
int bandpos, uint8_t roi_shift,
const int M_b)
2002 int passno = cblk->
npasses, pass_t = 2, bpno = cblk->
nonzerobits - 1 + 31 - M_b - 1 - roi_shift;
2024 if (bpno < 0 || bpno > 29) {
2031 vert_causal_ctx_csty_symbol);
2040 vert_causal_ctx_csty_symbol);
2078 for (
int y = 0; y <
height; y++) {
2079 for (
int x = 0; x <
width; x++) {
2081 const uint32_t
mask = UINT32_MAX >> (M_b + 1);
2083 n = x + (y * t1->
stride);
2085 sign =
val & INT32_MIN;
2088 if (roi_shift && (((uint32_t)
val & ~
mask) == 0))
2109 const int downshift = 31 - M_b;
2111 fscale /= (
float)(1 << downshift);
2112 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
2113 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
2115 for (
i = 0;
i <
w; ++
i) {
2118 val = -(
val & INT32_MAX);
2130 const int downshift = 31 - M_b;
2132 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
2136 for (
i = 0;
i <
w; ++
i) {
2139 val = -((
val & INT32_MAX) >> downshift);
2146 for (
i = 0;
i <
w; ++
i) {
2149 val = -((
val & INT32_MAX) >> downshift);
2165 const int downshift = 31 - M_b;
2166 const int PRESCALE = 6;
2169 fscale /= (
float)(1 << downshift);
2170 fscale *= (
float)(1 << PRESCALE);
2172 scale = (int)(fscale + 0.5);
2174 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
2177 for (
i = 0;
i <
w; ++
i) {
2180 val = -(
val & INT32_MAX);
2182 val = (
val + (1 << (PRESCALE - 1))) >> PRESCALE;
2193 for (
i = 1;
i < 3;
i++) {
2204 for (
i = 0;
i < 3;
i++)
2210 for (
i = 0;
i < 2;
i++)
2221 int compno, reslevelno, bandno;
2224 for (compno = 0; compno <
s->ncomponents; compno++) {
2238 for (bandno = 0; bandno < rlevel->
nbands; bandno++, subbandno++) {
2239 int nb_precincts, precno;
2241 int cblkno = 0, bandpos;
2245 bandpos = bandno + (reslevelno > 0);
2247 if (band->
coord[0][0] == band->
coord[0][1] ||
2258 for (precno = 0; precno < nb_precincts; precno++) {
2273 M_b,
comp->roi_shift);
2278 bandpos,
comp->roi_shift, M_b);
2306 #define WRITE_FRAME(D, PIXEL) \
2307 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2308 AVFrame * picture, int precision) \
2310 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2311 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2312 int pixelsize = planar ? 1 : pixdesc->nb_components; \
2317 for (compno = 0; compno < s->ncomponents; compno++) { \
2318 Jpeg2000Component *comp = tile->comp + compno; \
2319 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2321 float *datap = comp->f_data; \
2322 int32_t *i_datap = comp->i_data; \
2323 int cbps = s->cbps[compno]; \
2324 int w = tile->comp[compno].coord[0][1] - \
2325 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2326 int h = tile->comp[compno].coord[1][1] - \
2327 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2331 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2333 y = tile->comp[compno].coord[1][0] - \
2334 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2335 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2336 for (; y < h; y++) { \
2339 x = tile->comp[compno].coord[0][0] - \
2340 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2341 dst = line + x * pixelsize + compno*!planar; \
2343 if (codsty->transform == FF_DWT97) { \
2344 for (; x < w; x++) { \
2345 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2347 val = av_clip(val, 0, (1 << cbps) - 1); \
2348 *dst = val << (precision - cbps); \
2353 for (; x < w; x++) { \
2354 int val = *i_datap + (1 << (cbps - 1)); \
2356 val = av_clip(val, 0, (1 << cbps) - 1); \
2357 *dst = val << (precision - cbps); \
2362 line += picture->linesize[plane] / sizeof(PIXEL); \
2374 int jobnr,
int threadnr)
2388 if (
s->precision <= 8) {
2389 write_frame_8(
s, tile, picture, 8);
2396 write_frame_16(
s, tile, picture, precision);
2405 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2406 if (
s->tile[tileno].comp) {
2407 for (compno = 0; compno <
s->ncomponents; compno++) {
2414 av_freep(&
s->tile[tileno].packed_headers);
2415 s->tile[tileno].packed_headers_size = 0;
2419 s->packed_headers_size = 0;
2420 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2422 memset(
s->codsty, 0,
sizeof(
s->codsty));
2423 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2424 memset(
s->properties, 0,
sizeof(
s->properties));
2425 memset(&
s->poc , 0,
sizeof(
s->poc));
2426 s->numXtiles =
s->numYtiles = 0;
2435 uint8_t *properties =
s->properties;
2436 uint8_t in_tile_headers = 0;
2448 marker = bytestream2_get_be16u(&
s->g);
2450 if (marker >= 0xFF30 && marker <= 0xFF3F)
2460 if (
s->curtileno < 0) {
2465 tile =
s->tile +
s->curtileno;
2467 if (tp->
tp_end <
s->g.buffer) {
2473 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2491 len = bytestream2_get_be16(&
s->g);
2503 if (
s->ncomponents) {
2509 s->numXtiles =
s->numYtiles = 0;
2512 if (!
s->ncomponents) {
2519 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2521 "COC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2527 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2529 "COD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2535 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2537 "RGN marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2541 if ((!
s->Ccap15_b12) &&
s->isHT) {
2542 av_log(
s->avctx,
AV_LOG_ERROR,
"RGN marker found but the codestream belongs to the RGNFREE set\n");
2547 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2549 "QCC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2555 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2557 "QCD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2563 if (in_tile_headers == 1 &&
s->isHT && (!
s->Ccap15_b11)) {
2565 "POC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2571 if (!in_tile_headers) {
2572 in_tile_headers = 1;
2579 codsty =
s->tile[
s->curtileno].codsty;
2580 qntsty =
s->tile[
s->curtileno].qntsty;
2581 poc = &
s->tile[
s->curtileno].poc;
2582 properties =
s->tile[
s->curtileno].properties;
2604 if (in_tile_headers) {
2614 "Cannot have both PPT and PPM marker.\n");
2617 if ((!
s->Ccap15_b11) &&
s->isHT) {
2618 av_log(
s->avctx,
AV_LOG_ERROR,
"PPT marker found but the codestream belongs to the HOMOGENEOUS set\n");
2629 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2636 "error during processing marker segment %.4"PRIx16
"\n",
2650 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2665 uint32_t atom_size, atom, atom_end;
2666 int search_range = 10;
2671 atom_size = bytestream2_get_be32u(&
s->g);
2672 atom = bytestream2_get_be32u(&
s->g);
2673 if (atom_size == 1) {
2674 if (bytestream2_get_be32u(&
s->g)) {
2678 atom_size = bytestream2_get_be32u(&
s->g);
2696 uint32_t atom2_size, atom2, atom2_end;
2700 atom2_size = bytestream2_get_be32u(&
s->g);
2701 atom2 = bytestream2_get_be32u(&
s->g);
2703 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2708 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2709 int method = bytestream2_get_byteu(&
s->g);
2712 s->colour_space = bytestream2_get_be32u(&
s->g);
2714 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2715 int i,
size, colour_count, colour_channels, colour_depth[3];
2716 colour_count = bytestream2_get_be16u(&
s->g);
2717 colour_channels = bytestream2_get_byteu(&
s->g);
2719 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2720 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2721 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2722 size = (colour_depth[0] + 7 >> 3) * colour_count +
2723 (colour_depth[1] + 7 >> 3) * colour_count +
2724 (colour_depth[2] + 7 >> 3) * colour_count;
2726 colour_channels != 3 ||
2727 colour_depth[0] > 16 ||
2728 colour_depth[1] > 16 ||
2729 colour_depth[2] > 16 ||
2730 atom2_size <
size) {
2736 for (
i = 0;
i < colour_count;
i++) {
2738 if (colour_depth[0] <= 8) {
2739 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2740 r |=
r >> colour_depth[0];
2742 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2744 if (colour_depth[1] <= 8) {
2745 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2746 g |=
g >> colour_depth[1];
2748 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2750 if (colour_depth[2] <= 8) {
2751 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2752 b |=
b >> colour_depth[2];
2754 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2756 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2758 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2759 int n = bytestream2_get_be16u(&
s->g);
2761 int cn = bytestream2_get_be16(&
s->g);
2762 int av_unused typ = bytestream2_get_be16(&
s->g);
2763 int asoc = bytestream2_get_be16(&
s->g);
2764 if (cn < 4 && asoc < 4)
2767 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2768 int64_t vnum, vden, hnum, hden, vexp, hexp;
2771 resx = bytestream2_get_be32u(&
s->g);
2772 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2776 vnum = bytestream2_get_be16u(&
s->g);
2777 vden = bytestream2_get_be16u(&
s->g);
2778 hnum = bytestream2_get_be16u(&
s->g);
2779 hden = bytestream2_get_be16u(&
s->g);
2780 vexp = bytestream2_get_byteu(&
s->g);
2781 hexp = bytestream2_get_byteu(&
s->g);
2782 if (!vnum || !vden || !hnum || !hden) {
2794 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2795 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2797 hnum * vden * pow(10, hexp),
2798 vnum * hden * pow(10, vexp),
2802 }
while (atom_end - atom2_end >= 8);
2819 s->reduction_factor = avctx->
lowres;
2839 memset(
s->cdef, -1,
sizeof(
s->cdef));
2848 (bytestream2_get_be32u(&
s->g) == 12) &&
2853 "Could not find Jpeg2000 codestream atom.\n");
2872 if (
s->sar.num &&
s->sar.den)
2874 s->sar.num =
s->sar.den = 0;
2888 for (
int x = 0; x <
s->ncomponents; x++) {
2889 if (
s->cdef[x] < 0) {
2890 for (x = 0; x <
s->ncomponents; x++) {
2893 if ((
s->ncomponents & 1) == 0)
2894 s->cdef[
s->ncomponents-1] = 0;
2899 for (
int x = 0; x <
s->ncomponents &&
s->codsty[x].transform ==
FF_DWT53;)
2900 if (++x ==
s->ncomponents)
2910 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2919 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2920 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2923 {
"lowres",
"Lower the decoding resolution by a power of two",
2936 .
p.
name =
"jpeg2000",