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) {
528 c->transform = bytestream2_get_byteu(&
s->g);
538 for (
i = 0;
i <
c->nreslevels;
i++) {
539 byte = bytestream2_get_byte(&
s->g);
540 c->log2_prec_widths[
i] =
byte & 0x0F;
541 c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;
543 if (
c->log2_prec_widths[
i] == 0 ||
c->log2_prec_heights[
i] == 0) {
545 c->log2_prec_widths[
i],
c->log2_prec_heights[
i]);
546 c->log2_prec_widths[
i] =
c->log2_prec_heights[
i] = 1;
551 memset(
c->log2_prec_widths , 15,
sizeof(
c->log2_prec_widths ));
552 memset(
c->log2_prec_heights, 15,
sizeof(
c->log2_prec_heights));
569 tmp.csty = bytestream2_get_byteu(&
s->g);
572 tmp.prog_order = bytestream2_get_byteu(&
s->g);
574 tmp.nlayers = bytestream2_get_be16u(&
s->g);
575 tmp.mct = bytestream2_get_byteu(&
s->g);
577 if (
tmp.mct &&
s->ncomponents < 3) {
579 "MCT %"PRIu8
" with too few components (%d)\n",
580 tmp.mct,
s->ncomponents);
587 for (compno = 0; compno <
s->ncomponents; compno++)
588 if (!(properties[compno] &
HAD_COC))
589 memcpy(
c + compno, &
tmp,
sizeof(
tmp));
599 uint8_t has_eph, has_sop;
606 compno = bytestream2_get_byteu(&
s->g);
608 if (compno >=
s->ncomponents) {
610 "Invalid compno %d. There are %d components in the image.\n",
611 compno,
s->ncomponents);
618 c->csty = bytestream2_get_byteu(&
s->g);
633 compno = (
s->ncomponents < 257)? bytestream2_get_byte(&
s->g):
634 bytestream2_get_be16u(&
s->g);
635 if (bytestream2_get_byte(&
s->g)) {
642 if (compno < s->ncomponents) {
644 if (
s->curtileno == -1) {
645 v = bytestream2_get_byte(&
s->g);
648 s->roi_shift[compno] = v;
650 if (
s->tile[
s->curtileno].tp_idx != 0)
652 v = bytestream2_get_byte(&
s->g);
655 s->tile[
s->curtileno].comp[compno].roi_shift = v;
670 x = bytestream2_get_byteu(&
s->g);
680 for (
i = 0;
i < n;
i++)
681 q->
expn[
i] = bytestream2_get_byteu(&
s->g) >> 3;
685 x = bytestream2_get_be16u(&
s->g);
686 q->
expn[0] = x >> 11;
687 q->
mant[0] = x & 0x7ff;
689 int curexpn =
FFMAX(0, q->
expn[0] - (
i - 1) / 3);
690 q->
expn[
i] = curexpn;
698 for (
i = 0;
i < n;
i++) {
699 x = bytestream2_get_be16u(&
s->g);
700 q->
expn[
i] = x >> 11;
701 q->
mant[
i] = x & 0x7ff;
714 memset(&
tmp, 0,
sizeof(
tmp));
718 for (compno = 0; compno <
s->ncomponents; compno++)
719 if (!(properties[compno] &
HAD_QCC))
720 memcpy(q + compno, &
tmp,
sizeof(
tmp));
734 compno = bytestream2_get_byteu(&
s->g);
736 if (compno >=
s->ncomponents) {
738 "Invalid compno %d. There are %d components in the image.\n",
739 compno,
s->ncomponents);
744 return get_qcx(
s, n - 1, q + compno);
750 int elem_size =
s->ncomponents <= 257 ? 7 : 9;
763 tmp.nb_poc = (
size - 2) / elem_size;
769 for (
i = 0;
i<
tmp.nb_poc;
i++) {
771 e->
RSpoc = bytestream2_get_byteu(&
s->g);
772 e->
CSpoc = bytestream2_get_byteu(&
s->g);
773 e->
LYEpoc = bytestream2_get_be16u(&
s->g);
774 e->
REpoc = bytestream2_get_byteu(&
s->g);
775 e->
CEpoc = bytestream2_get_byteu(&
s->g);
776 e->
Ppoc = bytestream2_get_byteu(&
s->g);
779 if (e->
CEpoc >
s->ncomponents)
780 e->
CEpoc =
s->ncomponents;
820 Isot = bytestream2_get_be16u(&
s->g);
821 if (Isot >=
s->numXtiles *
s->numYtiles)
825 Psot = bytestream2_get_be32u(&
s->g);
826 TPsot = bytestream2_get_byteu(&
s->g);
829 bytestream2_get_byteu(&
s->g);
844 s->tile[Isot].tp_idx = TPsot;
845 tp =
s->tile[Isot].tile_part + TPsot;
847 tp->
tp_end =
s->g.buffer + Psot - n - 2;
855 memcpy(&tile->
poc , &
s->poc ,
sizeof(tile->
poc));
864 if (
s->ncomponents*4 != n - 2) {
881 uint8_t Stlm, ST,
SP, tile_tlm,
i;
882 bytestream2_get_byte(&
s->g);
883 Stlm = bytestream2_get_byte(&
s->g);
886 ST = (Stlm >> 4) & 0x03;
892 SP = (Stlm >> 6) & 0x01;
893 tile_tlm = (n - 4) / ((
SP + 1) * 2 + ST);
894 for (
i = 0;
i < tile_tlm;
i++) {
899 bytestream2_get_byte(&
s->g);
902 bytestream2_get_be16(&
s->g);
905 bytestream2_get_be32(&
s->g);
909 bytestream2_get_be16(&
s->g);
911 bytestream2_get_be32(&
s->g);
928 bytestream2_get_byte(&
s->g);
930 for (
i = 0;
i < n - 3;
i++) {
931 v = bytestream2_get_byte(&
s->g);
947 bytestream2_get_byte(&
s->g);
949 s->packed_headers_size + n - 3);
951 s->packed_headers =
new;
955 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
958 s->packed_headers_size += n - 3;
972 if (
s->curtileno < 0)
975 tile = &
s->tile[
s->curtileno];
978 "PPT marker can occur only on first tile part of a tile.\n");
983 bytestream2_get_byte(&
s->g);
1002 int tilex = tileno %
s->numXtiles;
1003 int tiley = tileno /
s->numXtiles;
1009 tile->
coord[0][0] =
av_clip(tilex * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
1010 tile->
coord[0][1] =
av_clip((tilex + 1) * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
1011 tile->
coord[1][0] =
av_clip(tiley * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1012 tile->
coord[1][1] =
av_clip((tiley + 1) * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1014 for (compno = 0; compno <
s->ncomponents; compno++) {
1020 comp->coord_o[0][0] = tile->
coord[0][0];
1021 comp->coord_o[0][1] = tile->
coord[0][1];
1022 comp->coord_o[1][0] = tile->
coord[1][0];
1023 comp->coord_o[1][1] = tile->
coord[1][1];
1035 if (!
comp->roi_shift)
1036 comp->roi_shift =
s->roi_shift[compno];
1040 s->cbps[compno],
s->cdx[compno],
1041 s->cdy[compno],
s->avctx))
1056 return num < 0 ? num : 3 + num;
1058 return num < 0 ? num : 6 + num;
1060 return num < 0 ? num : 37 + num;
1098 av_log(
s->avctx,
AV_LOG_ERROR,
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1105 int layno, uint8_t *expn,
int numgbits)
1107 int bandno, cblkno,
ret, nb_code_blocks;
1110 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1127 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1131 if (band->
coord[0][0] == band->
coord[0][1] ||
1136 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1138 int incl, newpasses, llen;
1151 int v = expn[bandno] + numgbits - 1 -
1153 if (v < 0 || v > 30) {
1155 "nonzerobits %d invalid or unsupported\n", v);
1171 "Block with length beyond 16 bits");
1190 while (newpasses1 < newpasses) {
1216 newpasses -= newpasses1;
1226 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1237 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1242 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1246 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1259 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1287 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1301 int RSpoc,
int CSpoc,
1302 int LYEpoc,
int REpoc,
int CEpoc,
1303 int Ppoc,
int *tp_index)
1306 int layno, reslevelno, compno, precno, ok_reslevel;
1314 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1316 for (layno = 0; layno < LYEpoc; layno++) {
1317 for (compno = CSpoc; compno < CEpoc; compno++) {
1320 if (reslevelno < codsty->nreslevels) {
1328 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1339 for (layno = 0; layno < LYEpoc; layno++) {
1341 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1343 for (compno = CSpoc; compno < CEpoc; compno++) {
1346 if (reslevelno < codsty->nreslevels) {
1354 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1365 for (compno = CSpoc; compno < CEpoc; compno++) {
1375 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1376 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1381 if (step_x >= 31 || step_y >= 31){
1388 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1389 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1390 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1391 unsigned prcx, prcy;
1392 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1394 int xc = x /
s->cdx[compno];
1395 int yc = y /
s->cdy[compno];
1417 for (layno = 0; layno < LYEpoc; layno++) {
1420 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1433 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1437 for (compno = CSpoc; compno < CEpoc; compno++) {
1441 if (reslevelno < codsty->nreslevels) {
1442 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1451 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1452 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1453 for (compno = CSpoc; compno < CEpoc; compno++) {
1457 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1459 unsigned prcx, prcy;
1462 if (!
s->cdx[compno] || !
s->cdy[compno])
1471 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1472 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1475 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1476 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1494 for (layno = 0; layno < LYEpoc; layno++) {
1498 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1512 for (compno = CSpoc; compno < CEpoc; compno++) {
1516 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1517 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1523 if (step_x >= 31 || step_y >= 31){
1530 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1531 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1532 for (compno = CSpoc; compno < CEpoc; compno++) {
1537 if (!
s->cdx[compno] || !
s->cdy[compno])
1540 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1541 unsigned prcx, prcy;
1542 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1549 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1550 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1553 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1554 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1571 for (layno = 0; layno < LYEpoc; layno++) {
1574 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1629 int bpno,
int bandno,
1630 int vert_causal_ctx_csty_symbol)
1632 int mask = 3 << (bpno - 1), y0, x, y;
1634 for (y0 = 0; y0 <
height; y0 += 4)
1635 for (x = 0; x <
width; x++)
1636 for (y = y0; y <
height && y < y0 + 4; y++) {
1637 int flags_mask = -1;
1638 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1651 t1->data[(y) *
t1->stride + x] < 0);
1659 int bpno,
int vert_causal_ctx_csty_symbol)
1664 phalf = 1 << (bpno - 1);
1667 for (y0 = 0; y0 <
height; y0 += 4)
1668 for (x = 0; x <
width; x++)
1669 for (y = y0; y <
height && y < y0 + 4; y++)
1671 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1675 t1->mqc.cx_states + ctxno)
1677 t1->data[(y) *
t1->stride + x] +=
t1->data[(y) *
t1->stride + x] < 0 ? -
r :
r;
1684 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1686 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1688 for (y0 = 0; y0 <
height; y0 += 4) {
1689 for (x = 0; x <
width; x++) {
1690 int flags_mask = -1;
1691 if (vert_causal_ctx_csty_symbol)
1711 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1712 int flags_mask = -1;
1713 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1726 t1->mqc.cx_states + ctxno) ^
1744 "Segmentation symbol value incorrect\n");
1750 int width,
int height,
int bandpos, uint8_t roi_shift)
1761 memset(
t1->data, 0,
t1->stride *
height *
sizeof(*
t1->data));
1767 memset(
t1->flags, 0,
t1->stride * (
height + 2) *
sizeof(*
t1->flags));
1774 if (bpno < 0 || bpno > 29) {
1781 vert_causal_ctx_csty_symbol);
1790 vert_causal_ctx_csty_symbol);
1835 roi_shift =
comp->roi_shift;
1836 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1838 if (
val > (1 << roi_shift))
1839 return (quan_parameter < 0)?-(
val >> roi_shift):(
val >> roi_shift);
1840 return quan_parameter;
1856 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1857 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
1858 int *
src =
t1->data + j*
t1->stride;
1859 for (
i = 0;
i <
w; ++
i)
1871 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1873 int *
src =
t1->data + j*
t1->stride;
1875 for (
i = 0;
i <
w; ++
i)
1876 datap[
i] =
src[
i] / 2;
1879 for (
i = 0;
i <
w; ++
i)
1891 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1893 int *
src =
t1->data + j*
t1->stride;
1894 for (
i = 0;
i <
w; ++
i)
1904 for (
i = 1;
i < 3;
i++) {
1915 for (
i = 0;
i < 3;
i++)
1921 for (
i = 0;
i < 2;
i++)
1933 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1934 int *
src =
t1->data + j*
t1->stride;
1935 for (
i = 0;
i <
w; ++
i)
1944 int compno, reslevelno, bandno;
1947 for (compno = 0; compno <
s->ncomponents; compno++) {
1958 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1959 int nb_precincts, precno;
1961 int cblkno = 0, bandpos;
1963 bandpos = bandno + (reslevelno > 0);
1965 if (band->
coord[0][0] == band->
coord[0][1] ||
1971 for (precno = 0; precno < nb_precincts; precno++) {
1983 bandpos,
comp->roi_shift);
1991 if (
comp->roi_shift)
2011 #define WRITE_FRAME(D, PIXEL) \
2012 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2013 AVFrame * picture, int precision) \
2015 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2016 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2017 int pixelsize = planar ? 1 : pixdesc->nb_components; \
2022 for (compno = 0; compno < s->ncomponents; compno++) { \
2023 Jpeg2000Component *comp = tile->comp + compno; \
2024 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2026 float *datap = comp->f_data; \
2027 int32_t *i_datap = comp->i_data; \
2028 int cbps = s->cbps[compno]; \
2029 int w = tile->comp[compno].coord[0][1] - \
2030 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2031 int h = tile->comp[compno].coord[1][1] - \
2032 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2036 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2038 y = tile->comp[compno].coord[1][0] - \
2039 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2040 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2041 for (; y < h; y++) { \
2044 x = tile->comp[compno].coord[0][0] - \
2045 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2046 dst = line + x * pixelsize + compno*!planar; \
2048 if (codsty->transform == FF_DWT97) { \
2049 for (; x < w; x++) { \
2050 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2052 val = av_clip(val, 0, (1 << cbps) - 1); \
2053 *dst = val << (precision - cbps); \
2058 for (; x < w; x++) { \
2059 int val = *i_datap + (1 << (cbps - 1)); \
2061 val = av_clip(val, 0, (1 << cbps) - 1); \
2062 *dst = val << (precision - cbps); \
2067 line += picture->linesize[plane] / sizeof(PIXEL); \
2079 int jobnr,
int threadnr)
2091 if (
s->precision <= 8) {
2092 write_frame_8(
s, tile, picture, 8);
2099 write_frame_16(
s, tile, picture, precision);
2108 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2109 if (
s->tile[tileno].comp) {
2110 for (compno = 0; compno <
s->ncomponents; compno++) {
2117 av_freep(&
s->tile[tileno].packed_headers);
2118 s->tile[tileno].packed_headers_size = 0;
2122 s->packed_headers_size = 0;
2123 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2125 memset(
s->codsty, 0,
sizeof(
s->codsty));
2126 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2127 memset(
s->properties, 0,
sizeof(
s->properties));
2128 memset(&
s->poc , 0,
sizeof(
s->poc));
2129 s->numXtiles =
s->numYtiles = 0;
2138 uint8_t *properties =
s->properties;
2150 marker = bytestream2_get_be16u(&
s->g);
2152 if (marker >= 0xFF30 && marker <= 0xFF3F)
2162 if (
s->curtileno < 0) {
2167 tile =
s->tile +
s->curtileno;
2169 if (tp->
tp_end <
s->g.buffer) {
2175 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2193 len = bytestream2_get_be16(&
s->g);
2205 if (
s->ncomponents) {
2211 s->numXtiles =
s->numYtiles = 0;
2232 if (!
s->in_tile_headers) {
2233 s->in_tile_headers = 1;
2240 codsty =
s->tile[
s->curtileno].codsty;
2241 qntsty =
s->tile[
s->curtileno].qntsty;
2242 poc = &
s->tile[
s->curtileno].poc;
2243 properties =
s->tile[
s->curtileno].properties;
2265 if (
s->in_tile_headers) {
2275 "Cannot have both PPT and PPM marker.\n");
2283 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2290 "error during processing marker segment %.4"PRIx16
"\n",
2304 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2319 uint32_t atom_size, atom, atom_end;
2320 int search_range = 10;
2325 atom_size = bytestream2_get_be32u(&
s->g);
2326 atom = bytestream2_get_be32u(&
s->g);
2327 if (atom_size == 1) {
2328 if (bytestream2_get_be32u(&
s->g)) {
2332 atom_size = bytestream2_get_be32u(&
s->g);
2333 if (atom_size < 16 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 16 > INT_MAX)
2337 if (atom_size < 8 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 8 > INT_MAX)
2350 uint32_t atom2_size, atom2, atom2_end;
2354 atom2_size = bytestream2_get_be32u(&
s->g);
2355 atom2 = bytestream2_get_be32u(&
s->g);
2357 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2362 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2363 int method = bytestream2_get_byteu(&
s->g);
2366 s->colour_space = bytestream2_get_be32u(&
s->g);
2368 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2369 int i,
size, colour_count, colour_channels, colour_depth[3];
2370 colour_count = bytestream2_get_be16u(&
s->g);
2371 colour_channels = bytestream2_get_byteu(&
s->g);
2373 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2374 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2375 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2376 size = (colour_depth[0] + 7 >> 3) * colour_count +
2377 (colour_depth[1] + 7 >> 3) * colour_count +
2378 (colour_depth[2] + 7 >> 3) * colour_count;
2380 colour_channels != 3 ||
2381 colour_depth[0] > 16 ||
2382 colour_depth[1] > 16 ||
2383 colour_depth[2] > 16 ||
2384 atom2_size <
size) {
2390 for (
i = 0;
i < colour_count;
i++) {
2392 if (colour_depth[0] <= 8) {
2393 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2394 r |=
r >> colour_depth[0];
2396 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2398 if (colour_depth[1] <= 8) {
2399 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2400 g |=
g >> colour_depth[1];
2402 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2404 if (colour_depth[2] <= 8) {
2405 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2406 b |=
b >> colour_depth[2];
2408 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2410 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2412 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2413 int n = bytestream2_get_be16u(&
s->g);
2415 int cn = bytestream2_get_be16(&
s->g);
2416 int av_unused typ = bytestream2_get_be16(&
s->g);
2417 int asoc = bytestream2_get_be16(&
s->g);
2418 if (cn < 4 && asoc < 4)
2421 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2422 int64_t vnum, vden, hnum, hden, vexp, hexp;
2425 resx = bytestream2_get_be32u(&
s->g);
2426 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2430 vnum = bytestream2_get_be16u(&
s->g);
2431 vden = bytestream2_get_be16u(&
s->g);
2432 hnum = bytestream2_get_be16u(&
s->g);
2433 hden = bytestream2_get_be16u(&
s->g);
2434 vexp = bytestream2_get_byteu(&
s->g);
2435 hexp = bytestream2_get_byteu(&
s->g);
2436 if (!vnum || !vden || !hnum || !hden) {
2448 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2449 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2451 hnum * vden * pow(10, hexp),
2452 vnum * hden * pow(10, vexp),
2456 }
while (atom_end - atom2_end >= 8);
2485 memset(
s->cdef, -1,
sizeof(
s->cdef));
2494 (bytestream2_get_be32u(&
s->g) == 12) &&
2499 "Could not find Jpeg2000 codestream atom.\n");
2527 for (
int x = 0; x <
s->ncomponents; x++) {
2528 if (
s->cdef[x] < 0) {
2529 for (x = 0; x <
s->ncomponents; x++) {
2532 if ((
s->ncomponents & 1) == 0)
2533 s->cdef[
s->ncomponents-1] = 0;
2545 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2546 if (
s->sar.num &&
s->sar.den)
2548 s->sar.num =
s->sar.den = 0;
2557 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2558 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2561 {
"lowres",
"Lower the decoding resolution by a power of two",
2574 .
p.
name =
"jpeg2000",