46 #define JP2_SIG_TYPE 0x6A502020
47 #define JP2_SIG_VALUE 0x0D0A870A
48 #define JP2_CODESTREAM 0x6A703263
49 #define JP2_HEADER 0x6A703268
151 if (
s->bit_index == 0) {
152 s->bit_index = 7 + (bytestream2_get_byte(&
s->g) != 0xFF
u);
155 res |= (bytestream2_peek_byte(&
s->g) >>
s->bit_index) & 1;
162 if (bytestream2_get_byte(&
s->g) == 0xff)
172 int sp = -1, curval = 0;
179 while (node && !node->
vis) {
187 curval = stack[
sp]->
val;
189 while (curval < threshold && sp >= 0) {
190 if (curval < stack[
sp]->
val)
191 curval = stack[
sp]->
val;
192 while (curval < threshold) {
202 stack[
sp]->
val = curval;
209 int bpc, uint32_t log2_chroma_wh,
int pal8)
216 if (
desc->nb_components != components) {
220 switch (components) {
222 match = match &&
desc->comp[3].depth >= bpc &&
223 (log2_chroma_wh >> 14 & 3) == 0 &&
224 (log2_chroma_wh >> 12 & 3) == 0;
226 match = match &&
desc->comp[2].depth >= bpc &&
227 (log2_chroma_wh >> 10 & 3) ==
desc->log2_chroma_w &&
228 (log2_chroma_wh >> 8 & 3) ==
desc->log2_chroma_h;
230 match = match &&
desc->comp[1].depth >= bpc &&
231 (log2_chroma_wh >> 6 & 3) ==
desc->log2_chroma_w &&
232 (log2_chroma_wh >> 4 & 3) ==
desc->log2_chroma_h;
235 match = match &&
desc->comp[0].depth >= bpc &&
236 (log2_chroma_wh >> 2 & 3) == 0 &&
237 (log2_chroma_wh & 3) == 0 &&
245 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
246 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
247 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
248 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
249 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
250 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
251 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
252 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
253 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
254 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
255 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
256 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
257 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
258 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
276 uint32_t log2_chroma_wh = 0;
278 int possible_fmts_nb = 0;
288 s->avctx->profile = bytestream2_get_be16u(&
s->g);
289 s->width = bytestream2_get_be32u(&
s->g);
290 s->height = bytestream2_get_be32u(&
s->g);
291 s->image_offset_x = bytestream2_get_be32u(&
s->g);
292 s->image_offset_y = bytestream2_get_be32u(&
s->g);
293 s->tile_width = bytestream2_get_be32u(&
s->g);
294 s->tile_height = bytestream2_get_be32u(&
s->g);
295 s->tile_offset_x = bytestream2_get_be32u(&
s->g);
296 s->tile_offset_y = bytestream2_get_be32u(&
s->g);
297 ncomponents = bytestream2_get_be16u(&
s->g);
304 if (ncomponents <= 0) {
310 if (ncomponents > 4) {
316 if (
s->tile_offset_x < 0 ||
s->tile_offset_y < 0 ||
317 s->image_offset_x <
s->tile_offset_x ||
318 s->image_offset_y <
s->tile_offset_y ||
319 s->tile_width + (int64_t)
s->tile_offset_x <=
s->image_offset_x ||
320 s->tile_height + (int64_t)
s->tile_offset_y <=
s->image_offset_y
326 s->ncomponents = ncomponents;
328 if (
s->tile_width <= 0 ||
s->tile_height <= 0) {
330 s->tile_width,
s->tile_height);
335 av_log(
s->avctx,
AV_LOG_ERROR,
"Insufficient space for %d components in SIZ\n",
s->ncomponents);
339 for (
i = 0;
i <
s->ncomponents;
i++) {
340 uint8_t x = bytestream2_get_byteu(&
s->g);
341 s->cbps[
i] = (x & 0x7f) + 1;
342 s->precision =
FFMAX(
s->cbps[
i],
s->precision);
343 s->sgnd[
i] = !!(x & 0x80);
344 s->cdx[
i] = bytestream2_get_byteu(&
s->g);
345 s->cdy[
i] = bytestream2_get_byteu(&
s->g);
346 if ( !
s->cdx[
i] ||
s->cdx[
i] == 3 ||
s->cdx[
i] > 4
347 || !
s->cdy[
i] ||
s->cdy[
i] == 3 ||
s->cdy[
i] > 4) {
351 log2_chroma_wh |=
s->cdy[
i] >> 1 <<
i * 4 |
s->cdx[
i] >> 1 <<
i * 4 + 2;
358 if (
s->numXtiles * (uint64_t)
s->numYtiles > INT_MAX/
sizeof(*
s->tile) ||
361 s->numXtiles =
s->numYtiles = 0;
365 s->tile =
av_calloc(
s->numXtiles *
s->numYtiles,
sizeof(*
s->tile));
367 s->numXtiles =
s->numYtiles = 0;
371 for (
i = 0;
i <
s->numXtiles *
s->numYtiles;
i++) {
381 s->reduction_factor);
383 s->reduction_factor);
386 for (
i = 1;
i <
s->ncomponents;
i++) {
400 switch (
s->colour_space) {
420 && !
pix_fmt_match(
s->avctx->pix_fmt, ncomponents,
s->precision, log2_chroma_wh,
s->pal8))
423 for (
i = 0;
i < possible_fmts_nb; ++
i) {
424 if (
pix_fmt_match(possible_fmts[
i], ncomponents,
s->precision, log2_chroma_wh,
s->pal8)) {
425 s->avctx->pix_fmt = possible_fmts[
i];
430 if (
i == possible_fmts_nb) {
431 if (ncomponents == 4 &&
432 s->cdy[0] == 1 &&
s->cdx[0] == 1 &&
433 s->cdy[1] == 1 &&
s->cdx[1] == 1 &&
434 s->cdy[2] ==
s->cdy[3] &&
s->cdx[2] ==
s->cdx[3]) {
435 if (
s->precision == 8 &&
s->cdy[2] == 2 &&
s->cdx[2] == 2 && !
s->pal8) {
443 }
else if (ncomponents == 3 &&
s->precision == 8 &&
444 s->cdx[0] ==
s->cdx[1] &&
s->cdx[0] ==
s->cdx[2] &&
445 s->cdy[0] ==
s->cdy[1] &&
s->cdy[0] ==
s->cdy[2]) {
448 }
else if (ncomponents == 2 &&
s->precision == 8 &&
449 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
452 }
else if (ncomponents == 1 &&
s->precision == 8) {
459 if (
i == possible_fmts_nb) {
461 "Unknown pix_fmt, profile: %d, colour_space: %d, "
462 "components: %d, precision: %d\n"
463 "cdx[0]: %d, cdy[0]: %d\n"
464 "cdx[1]: %d, cdy[1]: %d\n"
465 "cdx[2]: %d, cdy[2]: %d\n"
466 "cdx[3]: %d, cdy[3]: %d\n",
467 s->avctx->profile,
s->colour_space, ncomponents,
s->precision,
470 ncomponents > 1 ?
s->cdx[1] : 0,
471 ncomponents > 1 ?
s->cdy[1] : 0,
472 ncomponents > 2 ?
s->cdx[2] : 0,
473 ncomponents > 2 ?
s->cdy[2] : 0,
474 ncomponents > 3 ?
s->cdx[3] : 0,
475 ncomponents > 3 ?
s->cdy[3] : 0);
478 s->avctx->bits_per_raw_sample =
s->precision;
494 c->nreslevels = bytestream2_get_byteu(&
s->g) + 1;
500 if (
c->nreslevels <=
s->reduction_factor) {
505 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction_factor too large for this bitstream, max is %d\n",
c->nreslevels - 1);
506 s->reduction_factor =
c->nreslevels - 1;
511 c->nreslevels2decode =
c->nreslevels -
s->reduction_factor;
513 c->log2_cblk_width = (bytestream2_get_byteu(&
s->g) & 15) + 2;
514 c->log2_cblk_height = (bytestream2_get_byteu(&
s->g) & 15) + 2;
516 if (
c->log2_cblk_width > 10 ||
c->log2_cblk_height > 10 ||
517 c->log2_cblk_width +
c->log2_cblk_height > 12) {
522 c->cblk_style = bytestream2_get_byteu(&
s->g);
523 if (
c->cblk_style != 0) {
525 av_log(
s->avctx,
AV_LOG_ERROR,
"Support for High throughput JPEG 2000 is not yet available\n");
532 c->transform = bytestream2_get_byteu(&
s->g);
542 for (
i = 0;
i <
c->nreslevels;
i++) {
543 byte = bytestream2_get_byte(&
s->g);
544 c->log2_prec_widths[
i] =
byte & 0x0F;
545 c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;
547 if (
c->log2_prec_widths[
i] == 0 ||
c->log2_prec_heights[
i] == 0) {
549 c->log2_prec_widths[
i],
c->log2_prec_heights[
i]);
550 c->log2_prec_widths[
i] =
c->log2_prec_heights[
i] = 1;
555 memset(
c->log2_prec_widths , 15,
sizeof(
c->log2_prec_widths ));
556 memset(
c->log2_prec_heights, 15,
sizeof(
c->log2_prec_heights));
573 tmp.csty = bytestream2_get_byteu(&
s->g);
576 tmp.prog_order = bytestream2_get_byteu(&
s->g);
578 tmp.nlayers = bytestream2_get_be16u(&
s->g);
579 tmp.mct = bytestream2_get_byteu(&
s->g);
581 if (
tmp.mct &&
s->ncomponents < 3) {
583 "MCT %"PRIu8
" with too few components (%d)\n",
584 tmp.mct,
s->ncomponents);
591 for (compno = 0; compno <
s->ncomponents; compno++)
592 if (!(properties[compno] &
HAD_COC))
593 memcpy(
c + compno, &
tmp,
sizeof(
tmp));
603 uint8_t has_eph, has_sop;
610 compno = bytestream2_get_byteu(&
s->g);
612 if (compno >=
s->ncomponents) {
614 "Invalid compno %d. There are %d components in the image.\n",
615 compno,
s->ncomponents);
622 c->csty = bytestream2_get_byteu(&
s->g);
637 compno = (
s->ncomponents < 257)? bytestream2_get_byte(&
s->g):
638 bytestream2_get_be16u(&
s->g);
639 if (bytestream2_get_byte(&
s->g)) {
646 if (compno < s->ncomponents) {
648 if (
s->curtileno == -1) {
649 v = bytestream2_get_byte(&
s->g);
652 s->roi_shift[compno] = v;
654 if (
s->tile[
s->curtileno].tp_idx != 0)
656 v = bytestream2_get_byte(&
s->g);
659 s->tile[
s->curtileno].comp[compno].roi_shift = v;
674 x = bytestream2_get_byteu(&
s->g);
684 for (
i = 0;
i < n;
i++)
685 q->
expn[
i] = bytestream2_get_byteu(&
s->g) >> 3;
689 x = bytestream2_get_be16u(&
s->g);
690 q->
expn[0] = x >> 11;
691 q->
mant[0] = x & 0x7ff;
693 int curexpn =
FFMAX(0, q->
expn[0] - (
i - 1) / 3);
694 q->
expn[
i] = curexpn;
702 for (
i = 0;
i < n;
i++) {
703 x = bytestream2_get_be16u(&
s->g);
704 q->
expn[
i] = x >> 11;
705 q->
mant[
i] = x & 0x7ff;
718 memset(&
tmp, 0,
sizeof(
tmp));
722 for (compno = 0; compno <
s->ncomponents; compno++)
723 if (!(properties[compno] &
HAD_QCC))
724 memcpy(q + compno, &
tmp,
sizeof(
tmp));
738 compno = bytestream2_get_byteu(&
s->g);
740 if (compno >=
s->ncomponents) {
742 "Invalid compno %d. There are %d components in the image.\n",
743 compno,
s->ncomponents);
748 return get_qcx(
s, n - 1, q + compno);
754 int elem_size =
s->ncomponents <= 257 ? 7 : 9;
767 tmp.nb_poc = (
size - 2) / elem_size;
773 for (
i = 0;
i<
tmp.nb_poc;
i++) {
775 e->
RSpoc = bytestream2_get_byteu(&
s->g);
776 e->
CSpoc = bytestream2_get_byteu(&
s->g);
777 e->
LYEpoc = bytestream2_get_be16u(&
s->g);
778 e->
REpoc = bytestream2_get_byteu(&
s->g);
779 e->
CEpoc = bytestream2_get_byteu(&
s->g);
780 e->
Ppoc = bytestream2_get_byteu(&
s->g);
783 if (e->
CEpoc >
s->ncomponents)
784 e->
CEpoc =
s->ncomponents;
824 Isot = bytestream2_get_be16u(&
s->g);
825 if (Isot >=
s->numXtiles *
s->numYtiles)
829 Psot = bytestream2_get_be32u(&
s->g);
830 TPsot = bytestream2_get_byteu(&
s->g);
833 bytestream2_get_byteu(&
s->g);
848 s->tile[Isot].tp_idx = TPsot;
849 tp =
s->tile[Isot].tile_part + TPsot;
851 tp->
tp_end =
s->g.buffer + Psot - n - 2;
859 memcpy(&tile->
poc , &
s->poc ,
sizeof(tile->
poc));
868 if (
s->ncomponents*4 != n - 2) {
885 uint8_t Stlm, ST,
SP, tile_tlm,
i;
886 bytestream2_get_byte(&
s->g);
887 Stlm = bytestream2_get_byte(&
s->g);
890 ST = (Stlm >> 4) & 0x03;
896 SP = (Stlm >> 6) & 0x01;
897 tile_tlm = (n - 4) / ((
SP + 1) * 2 + ST);
898 for (
i = 0;
i < tile_tlm;
i++) {
903 bytestream2_get_byte(&
s->g);
906 bytestream2_get_be16(&
s->g);
909 bytestream2_get_be32(&
s->g);
913 bytestream2_get_be16(&
s->g);
915 bytestream2_get_be32(&
s->g);
932 bytestream2_get_byte(&
s->g);
934 for (
i = 0;
i < n - 3;
i++) {
935 v = bytestream2_get_byte(&
s->g);
951 bytestream2_get_byte(&
s->g);
953 s->packed_headers_size + n - 3);
955 s->packed_headers =
new;
959 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
962 s->packed_headers_size += n - 3;
976 if (
s->curtileno < 0)
979 tile = &
s->tile[
s->curtileno];
982 "PPT marker can occur only on first tile part of a tile.\n");
987 bytestream2_get_byte(&
s->g);
1006 int tilex = tileno %
s->numXtiles;
1007 int tiley = tileno /
s->numXtiles;
1013 tile->
coord[0][0] =
av_clip(tilex * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
1014 tile->
coord[0][1] =
av_clip((tilex + 1) * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
1015 tile->
coord[1][0] =
av_clip(tiley * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1016 tile->
coord[1][1] =
av_clip((tiley + 1) * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1018 for (compno = 0; compno <
s->ncomponents; compno++) {
1024 comp->coord_o[0][0] = tile->
coord[0][0];
1025 comp->coord_o[0][1] = tile->
coord[0][1];
1026 comp->coord_o[1][0] = tile->
coord[1][0];
1027 comp->coord_o[1][1] = tile->
coord[1][1];
1039 if (!
comp->roi_shift)
1040 comp->roi_shift =
s->roi_shift[compno];
1044 s->cbps[compno],
s->cdx[compno],
1045 s->cdy[compno],
s->avctx))
1060 return num < 0 ? num : 3 + num;
1062 return num < 0 ? num : 6 + num;
1064 return num < 0 ? num : 37 + num;
1102 av_log(
s->avctx,
AV_LOG_ERROR,
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1109 int layno, uint8_t *expn,
int numgbits)
1111 int bandno, cblkno,
ret, nb_code_blocks;
1114 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1131 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1135 if (band->
coord[0][0] == band->
coord[0][1] ||
1140 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1142 int incl, newpasses, llen;
1155 int v = expn[bandno] + numgbits - 1 -
1157 if (v < 0 || v > 30) {
1159 "nonzerobits %d invalid or unsupported\n", v);
1175 "Block with length beyond 16 bits");
1194 while (newpasses1 < newpasses) {
1220 newpasses -= newpasses1;
1230 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1241 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1246 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1250 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1263 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1291 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1305 int RSpoc,
int CSpoc,
1306 int LYEpoc,
int REpoc,
int CEpoc,
1307 int Ppoc,
int *tp_index)
1310 int layno, reslevelno, compno, precno, ok_reslevel;
1318 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1320 for (layno = 0; layno < LYEpoc; layno++) {
1321 for (compno = CSpoc; compno < CEpoc; compno++) {
1324 if (reslevelno < codsty->nreslevels) {
1332 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1343 for (layno = 0; layno < LYEpoc; layno++) {
1345 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1347 for (compno = CSpoc; compno < CEpoc; compno++) {
1350 if (reslevelno < codsty->nreslevels) {
1358 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1369 for (compno = CSpoc; compno < CEpoc; compno++) {
1379 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1380 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1385 if (step_x >= 31 || step_y >= 31){
1392 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1393 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1394 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1395 unsigned prcx, prcy;
1396 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1398 int xc = x /
s->cdx[compno];
1399 int yc = y /
s->cdy[compno];
1421 for (layno = 0; layno < LYEpoc; layno++) {
1424 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1437 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1441 for (compno = CSpoc; compno < CEpoc; compno++) {
1445 if (reslevelno < codsty->nreslevels) {
1446 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1455 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1456 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1457 for (compno = CSpoc; compno < CEpoc; compno++) {
1461 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1463 unsigned prcx, prcy;
1466 if (!
s->cdx[compno] || !
s->cdy[compno])
1475 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1476 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1479 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1480 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1498 for (layno = 0; layno < LYEpoc; layno++) {
1502 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1516 for (compno = CSpoc; compno < CEpoc; compno++) {
1520 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1521 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1527 if (step_x >= 31 || step_y >= 31){
1534 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1535 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1536 for (compno = CSpoc; compno < CEpoc; compno++) {
1541 if (!
s->cdx[compno] || !
s->cdy[compno])
1544 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1545 unsigned prcx, prcy;
1546 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1553 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1554 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1557 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1558 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1575 for (layno = 0; layno < LYEpoc; layno++) {
1578 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1633 int bpno,
int bandno,
1634 int vert_causal_ctx_csty_symbol)
1636 int mask = 3 << (bpno - 1), y0, x, y;
1638 for (y0 = 0; y0 <
height; y0 += 4)
1639 for (x = 0; x <
width; x++)
1640 for (y = y0; y <
height && y < y0 + 4; y++) {
1641 int flags_mask = -1;
1642 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1655 t1->data[(y) *
t1->stride + x] < 0);
1663 int bpno,
int vert_causal_ctx_csty_symbol)
1668 phalf = 1 << (bpno - 1);
1671 for (y0 = 0; y0 <
height; y0 += 4)
1672 for (x = 0; x <
width; x++)
1673 for (y = y0; y <
height && y < y0 + 4; y++)
1675 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1679 t1->mqc.cx_states + ctxno)
1681 t1->data[(y) *
t1->stride + x] +=
t1->data[(y) *
t1->stride + x] < 0 ? -
r :
r;
1688 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1690 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1692 for (y0 = 0; y0 <
height; y0 += 4) {
1693 for (x = 0; x <
width; x++) {
1694 int flags_mask = -1;
1695 if (vert_causal_ctx_csty_symbol)
1715 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1716 int flags_mask = -1;
1717 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1730 t1->mqc.cx_states + ctxno) ^
1748 "Segmentation symbol value incorrect\n");
1754 int width,
int height,
int bandpos, uint8_t roi_shift)
1765 memset(
t1->data, 0,
t1->stride *
height *
sizeof(*
t1->data));
1771 memset(
t1->flags, 0,
t1->stride * (
height + 2) *
sizeof(*
t1->flags));
1778 if (bpno < 0 || bpno > 29) {
1785 vert_causal_ctx_csty_symbol);
1794 vert_causal_ctx_csty_symbol);
1839 roi_shift =
comp->roi_shift;
1840 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1842 if (
val > (1 << roi_shift))
1843 return (quan_parameter < 0)?-(
val >> roi_shift):(
val >> roi_shift);
1844 return quan_parameter;
1860 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1861 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
1862 int *
src =
t1->data + j*
t1->stride;
1863 for (
i = 0;
i <
w; ++
i)
1875 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1877 int *
src =
t1->data + j*
t1->stride;
1879 for (
i = 0;
i <
w; ++
i)
1880 datap[
i] =
src[
i] / 2;
1883 for (
i = 0;
i <
w; ++
i)
1895 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1897 int *
src =
t1->data + j*
t1->stride;
1898 for (
i = 0;
i <
w; ++
i)
1908 for (
i = 1;
i < 3;
i++) {
1919 for (
i = 0;
i < 3;
i++)
1925 for (
i = 0;
i < 2;
i++)
1937 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1938 int *
src =
t1->data + j*
t1->stride;
1939 for (
i = 0;
i <
w; ++
i)
1948 int compno, reslevelno, bandno;
1951 for (compno = 0; compno <
s->ncomponents; compno++) {
1962 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1963 int nb_precincts, precno;
1965 int cblkno = 0, bandpos;
1967 bandpos = bandno + (reslevelno > 0);
1969 if (band->
coord[0][0] == band->
coord[0][1] ||
1975 for (precno = 0; precno < nb_precincts; precno++) {
1987 bandpos,
comp->roi_shift);
1995 if (
comp->roi_shift)
2015 #define WRITE_FRAME(D, PIXEL) \
2016 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2017 AVFrame * picture, int precision) \
2019 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2020 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2021 int pixelsize = planar ? 1 : pixdesc->nb_components; \
2026 for (compno = 0; compno < s->ncomponents; compno++) { \
2027 Jpeg2000Component *comp = tile->comp + compno; \
2028 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2030 float *datap = comp->f_data; \
2031 int32_t *i_datap = comp->i_data; \
2032 int cbps = s->cbps[compno]; \
2033 int w = tile->comp[compno].coord[0][1] - \
2034 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2035 int h = tile->comp[compno].coord[1][1] - \
2036 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2040 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2042 y = tile->comp[compno].coord[1][0] - \
2043 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2044 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2045 for (; y < h; y++) { \
2048 x = tile->comp[compno].coord[0][0] - \
2049 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2050 dst = line + x * pixelsize + compno*!planar; \
2052 if (codsty->transform == FF_DWT97) { \
2053 for (; x < w; x++) { \
2054 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2056 val = av_clip(val, 0, (1 << cbps) - 1); \
2057 *dst = val << (precision - cbps); \
2062 for (; x < w; x++) { \
2063 int val = *i_datap + (1 << (cbps - 1)); \
2065 val = av_clip(val, 0, (1 << cbps) - 1); \
2066 *dst = val << (precision - cbps); \
2071 line += picture->linesize[plane] / sizeof(PIXEL); \
2083 int jobnr,
int threadnr)
2095 if (
s->precision <= 8) {
2096 write_frame_8(
s, tile, picture, 8);
2103 write_frame_16(
s, tile, picture, precision);
2112 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2113 if (
s->tile[tileno].comp) {
2114 for (compno = 0; compno <
s->ncomponents; compno++) {
2121 av_freep(&
s->tile[tileno].packed_headers);
2122 s->tile[tileno].packed_headers_size = 0;
2126 s->packed_headers_size = 0;
2127 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2129 memset(
s->codsty, 0,
sizeof(
s->codsty));
2130 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2131 memset(
s->properties, 0,
sizeof(
s->properties));
2132 memset(&
s->poc , 0,
sizeof(
s->poc));
2133 s->numXtiles =
s->numYtiles = 0;
2142 uint8_t *properties =
s->properties;
2154 marker = bytestream2_get_be16u(&
s->g);
2156 if (marker >= 0xFF30 && marker <= 0xFF3F)
2166 if (
s->curtileno < 0) {
2171 tile =
s->tile +
s->curtileno;
2173 if (tp->
tp_end <
s->g.buffer) {
2179 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2197 len = bytestream2_get_be16(&
s->g);
2209 if (
s->ncomponents) {
2215 s->numXtiles =
s->numYtiles = 0;
2236 if (!
s->in_tile_headers) {
2237 s->in_tile_headers = 1;
2244 codsty =
s->tile[
s->curtileno].codsty;
2245 qntsty =
s->tile[
s->curtileno].qntsty;
2246 poc = &
s->tile[
s->curtileno].poc;
2247 properties =
s->tile[
s->curtileno].properties;
2269 if (
s->in_tile_headers) {
2279 "Cannot have both PPT and PPM marker.\n");
2287 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2294 "error during processing marker segment %.4"PRIx16
"\n",
2308 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2323 uint32_t atom_size, atom, atom_end;
2324 int search_range = 10;
2329 atom_size = bytestream2_get_be32u(&
s->g);
2330 atom = bytestream2_get_be32u(&
s->g);
2331 if (atom_size == 1) {
2332 if (bytestream2_get_be32u(&
s->g)) {
2336 atom_size = bytestream2_get_be32u(&
s->g);
2337 if (atom_size < 16 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 16 > INT_MAX)
2341 if (atom_size < 8 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 8 > INT_MAX)
2354 uint32_t atom2_size, atom2, atom2_end;
2358 atom2_size = bytestream2_get_be32u(&
s->g);
2359 atom2 = bytestream2_get_be32u(&
s->g);
2361 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2366 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2367 int method = bytestream2_get_byteu(&
s->g);
2370 s->colour_space = bytestream2_get_be32u(&
s->g);
2372 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2373 int i,
size, colour_count, colour_channels, colour_depth[3];
2374 colour_count = bytestream2_get_be16u(&
s->g);
2375 colour_channels = bytestream2_get_byteu(&
s->g);
2377 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2378 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2379 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2380 size = (colour_depth[0] + 7 >> 3) * colour_count +
2381 (colour_depth[1] + 7 >> 3) * colour_count +
2382 (colour_depth[2] + 7 >> 3) * colour_count;
2384 colour_channels != 3 ||
2385 colour_depth[0] > 16 ||
2386 colour_depth[1] > 16 ||
2387 colour_depth[2] > 16 ||
2388 atom2_size <
size) {
2394 for (
i = 0;
i < colour_count;
i++) {
2396 if (colour_depth[0] <= 8) {
2397 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2398 r |=
r >> colour_depth[0];
2400 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2402 if (colour_depth[1] <= 8) {
2403 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2404 g |=
g >> colour_depth[1];
2406 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2408 if (colour_depth[2] <= 8) {
2409 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2410 b |=
b >> colour_depth[2];
2412 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2414 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2416 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2417 int n = bytestream2_get_be16u(&
s->g);
2419 int cn = bytestream2_get_be16(&
s->g);
2420 int av_unused typ = bytestream2_get_be16(&
s->g);
2421 int asoc = bytestream2_get_be16(&
s->g);
2422 if (cn < 4 && asoc < 4)
2425 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2426 int64_t vnum, vden, hnum, hden, vexp, hexp;
2429 resx = bytestream2_get_be32u(&
s->g);
2430 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2434 vnum = bytestream2_get_be16u(&
s->g);
2435 vden = bytestream2_get_be16u(&
s->g);
2436 hnum = bytestream2_get_be16u(&
s->g);
2437 hden = bytestream2_get_be16u(&
s->g);
2438 vexp = bytestream2_get_byteu(&
s->g);
2439 hexp = bytestream2_get_byteu(&
s->g);
2440 if (!vnum || !vden || !hnum || !hden) {
2452 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2453 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2455 hnum * vden * pow(10, hexp),
2456 vnum * hden * pow(10, vexp),
2460 }
while (atom_end - atom2_end >= 8);
2489 memset(
s->cdef, -1,
sizeof(
s->cdef));
2498 (bytestream2_get_be32u(&
s->g) == 12) &&
2503 "Could not find Jpeg2000 codestream atom.\n");
2522 if (
s->sar.num &&
s->sar.den)
2524 s->sar.num =
s->sar.den = 0;
2540 for (
int x = 0; x <
s->ncomponents; x++) {
2541 if (
s->cdef[x] < 0) {
2542 for (x = 0; x <
s->ncomponents; x++) {
2545 if ((
s->ncomponents & 1) == 0)
2546 s->cdef[
s->ncomponents-1] = 0;
2558 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2567 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2568 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2571 {
"lowres",
"Lower the decoding resolution by a power of two",
2584 .
p.
name =
"jpeg2000",