49 #define JP2_SIG_TYPE 0x6A502020
50 #define JP2_SIG_VALUE 0x0D0A870A
51 #define JP2_CODESTREAM 0x6A703263
52 #define JP2_HEADER 0x6A703268
67 if (
s->bit_index == 0) {
68 s->bit_index = 7 + (bytestream2_get_byte(&
s->g) != 0xFF
u);
71 res |= (bytestream2_peek_byte(&
s->g) >>
s->bit_index) & 1;
78 if (bytestream2_get_byte(&
s->g) == 0xff)
88 int sp = -1, curval = 0;
95 while (node && !node->
vis) {
103 curval = stack[
sp]->
val;
105 while (curval < threshold && sp >= 0) {
106 if (curval < stack[
sp]->
val)
107 curval = stack[
sp]->
val;
108 while (curval < threshold) {
118 stack[
sp]->
val = curval;
125 int bpc, uint32_t log2_chroma_wh,
int pal8)
132 if (
desc->nb_components != components) {
136 switch (components) {
138 match = match &&
desc->comp[3].depth >= bpc &&
139 (log2_chroma_wh >> 14 & 3) == 0 &&
140 (log2_chroma_wh >> 12 & 3) == 0;
142 match = match &&
desc->comp[2].depth >= bpc &&
143 (log2_chroma_wh >> 10 & 3) ==
desc->log2_chroma_w &&
144 (log2_chroma_wh >> 8 & 3) ==
desc->log2_chroma_h;
146 match = match &&
desc->comp[1].depth >= bpc &&
147 (log2_chroma_wh >> 6 & 3) ==
desc->log2_chroma_w &&
148 (log2_chroma_wh >> 4 & 3) ==
desc->log2_chroma_h;
151 match = match &&
desc->comp[0].depth >= bpc &&
152 (log2_chroma_wh >> 2 & 3) == 0 &&
153 (log2_chroma_wh & 3) == 0 &&
161 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
162 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
163 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
164 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
165 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
166 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
167 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
168 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
169 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
170 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
171 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
172 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
173 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
174 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
192 uint32_t log2_chroma_wh = 0;
194 int possible_fmts_nb = 0;
204 s->avctx->profile = bytestream2_get_be16u(&
s->g);
205 s->width = bytestream2_get_be32u(&
s->g);
206 s->height = bytestream2_get_be32u(&
s->g);
207 s->image_offset_x = bytestream2_get_be32u(&
s->g);
208 s->image_offset_y = bytestream2_get_be32u(&
s->g);
209 s->tile_width = bytestream2_get_be32u(&
s->g);
210 s->tile_height = bytestream2_get_be32u(&
s->g);
211 s->tile_offset_x = bytestream2_get_be32u(&
s->g);
212 s->tile_offset_y = bytestream2_get_be32u(&
s->g);
213 ncomponents = bytestream2_get_be16u(&
s->g);
220 if (ncomponents <= 0) {
226 if (ncomponents > 4) {
232 if (
s->tile_offset_x < 0 ||
s->tile_offset_y < 0 ||
233 s->image_offset_x <
s->tile_offset_x ||
234 s->image_offset_y <
s->tile_offset_y ||
235 s->tile_width + (int64_t)
s->tile_offset_x <=
s->image_offset_x ||
236 s->tile_height + (int64_t)
s->tile_offset_y <=
s->image_offset_y
242 if (
s->image_offset_x >=
s->width ||
s->image_offset_y >=
s->height) {
247 if (
s->reduction_factor && (
s->image_offset_x ||
s->image_offset_y) ){
248 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction factor with image offsets is not fully implemented");
252 s->ncomponents = ncomponents;
254 if (
s->tile_width <= 0 ||
s->tile_height <= 0) {
256 s->tile_width,
s->tile_height);
261 av_log(
s->avctx,
AV_LOG_ERROR,
"Insufficient space for %d components in SIZ\n",
s->ncomponents);
265 for (
i = 0;
i <
s->ncomponents;
i++) {
266 uint8_t x = bytestream2_get_byteu(&
s->g);
267 s->cbps[
i] = (x & 0x7f) + 1;
268 s->precision =
FFMAX(
s->cbps[
i],
s->precision);
269 s->sgnd[
i] = !!(x & 0x80);
270 s->cdx[
i] = bytestream2_get_byteu(&
s->g);
271 s->cdy[
i] = bytestream2_get_byteu(&
s->g);
272 if ( !
s->cdx[
i] ||
s->cdx[
i] == 3 ||
s->cdx[
i] > 4
273 || !
s->cdy[
i] ||
s->cdy[
i] == 3 ||
s->cdy[
i] > 4) {
277 log2_chroma_wh |=
s->cdy[
i] >> 1 <<
i * 4 |
s->cdx[
i] >> 1 <<
i * 4 + 2;
284 if (
s->numXtiles * (uint64_t)
s->numYtiles > INT_MAX/
sizeof(*
s->tile) ||
287 s->numXtiles =
s->numYtiles = 0;
291 s->tile =
av_calloc(
s->numXtiles *
s->numYtiles,
sizeof(*
s->tile));
293 s->numXtiles =
s->numYtiles = 0;
297 for (
i = 0;
i <
s->numXtiles *
s->numYtiles;
i++) {
307 s->reduction_factor);
309 s->reduction_factor);
312 for (
i = 1;
i <
s->ncomponents;
i++) {
326 switch (
s->colour_space) {
346 && !
pix_fmt_match(
s->avctx->pix_fmt, ncomponents,
s->precision, log2_chroma_wh,
s->pal8))
349 for (
i = 0;
i < possible_fmts_nb; ++
i) {
350 if (
pix_fmt_match(possible_fmts[
i], ncomponents,
s->precision, log2_chroma_wh,
s->pal8)) {
351 s->avctx->pix_fmt = possible_fmts[
i];
356 if (
i == possible_fmts_nb) {
357 if (ncomponents == 4 &&
358 s->cdy[0] == 1 &&
s->cdx[0] == 1 &&
359 s->cdy[1] == 1 &&
s->cdx[1] == 1 &&
360 s->cdy[2] ==
s->cdy[3] &&
s->cdx[2] ==
s->cdx[3]) {
361 if (
s->precision == 8 &&
s->cdy[2] == 2 &&
s->cdx[2] == 2 && !
s->pal8) {
369 }
else if (ncomponents == 3 &&
s->precision == 8 &&
370 s->cdx[0] ==
s->cdx[1] &&
s->cdx[0] ==
s->cdx[2] &&
371 s->cdy[0] ==
s->cdy[1] &&
s->cdy[0] ==
s->cdy[2]) {
374 }
else if (ncomponents == 2 &&
s->precision == 8 &&
375 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
378 }
else if (ncomponents == 2 &&
s->precision == 16 &&
379 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
382 }
else if (ncomponents == 1 &&
s->precision == 8) {
389 if (
i == possible_fmts_nb) {
391 "Unknown pix_fmt, profile: %d, colour_space: %d, "
392 "components: %d, precision: %d\n"
393 "cdx[0]: %d, cdy[0]: %d\n"
394 "cdx[1]: %d, cdy[1]: %d\n"
395 "cdx[2]: %d, cdy[2]: %d\n"
396 "cdx[3]: %d, cdy[3]: %d\n",
397 s->avctx->profile,
s->colour_space, ncomponents,
s->precision,
400 ncomponents > 1 ?
s->cdx[1] : 0,
401 ncomponents > 1 ?
s->cdy[1] : 0,
402 ncomponents > 2 ?
s->cdx[2] : 0,
403 ncomponents > 2 ?
s->cdy[2] : 0,
404 ncomponents > 3 ?
s->cdx[3] : 0,
405 ncomponents > 3 ?
s->cdy[3] : 0);
408 s->avctx->bits_per_raw_sample =
s->precision;
424 c->nreslevels = bytestream2_get_byteu(&
s->g) + 1;
430 if (
c->nreslevels <=
s->reduction_factor) {
435 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction_factor too large for this bitstream, max is %d\n",
c->nreslevels - 1);
436 s->reduction_factor =
c->nreslevels - 1;
441 c->nreslevels2decode =
c->nreslevels -
s->reduction_factor;
443 c->log2_cblk_width = (bytestream2_get_byteu(&
s->g) & 15) + 2;
444 c->log2_cblk_height = (bytestream2_get_byteu(&
s->g) & 15) + 2;
446 if (
c->log2_cblk_width > 10 ||
c->log2_cblk_height > 10 ||
447 c->log2_cblk_width +
c->log2_cblk_height > 12) {
452 c->cblk_style = bytestream2_get_byteu(&
s->g);
453 if (
c->cblk_style != 0) {
462 c->transform = bytestream2_get_byteu(&
s->g);
472 for (
i = 0;
i <
c->nreslevels;
i++) {
473 byte = bytestream2_get_byte(&
s->g);
474 c->log2_prec_widths[
i] =
byte & 0x0F;
475 c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;
477 if (
c->log2_prec_widths[
i] == 0 ||
c->log2_prec_heights[
i] == 0) {
479 c->log2_prec_widths[
i],
c->log2_prec_heights[
i]);
480 c->log2_prec_widths[
i] =
c->log2_prec_heights[
i] = 1;
485 memset(
c->log2_prec_widths , 15,
sizeof(
c->log2_prec_widths ));
486 memset(
c->log2_prec_heights, 15,
sizeof(
c->log2_prec_heights));
493 const uint8_t *properties)
503 tmp.csty = bytestream2_get_byteu(&
s->g);
506 tmp.prog_order = bytestream2_get_byteu(&
s->g);
508 tmp.nlayers = bytestream2_get_be16u(&
s->g);
509 tmp.mct = bytestream2_get_byteu(&
s->g);
511 if (
tmp.mct &&
s->ncomponents < 3) {
513 "MCT %"PRIu8
" with too few components (%d)\n",
514 tmp.mct,
s->ncomponents);
521 for (compno = 0; compno <
s->ncomponents; compno++)
522 if (!(properties[compno] &
HAD_COC))
523 memcpy(
c + compno, &
tmp,
sizeof(
tmp));
533 uint8_t has_eph, has_sop;
540 compno = bytestream2_get_byteu(&
s->g);
542 if (compno >=
s->ncomponents) {
544 "Invalid compno %d. There are %d components in the image.\n",
545 compno,
s->ncomponents);
552 c->csty = bytestream2_get_byteu(&
s->g);
567 compno = (
s->ncomponents < 257)? bytestream2_get_byte(&
s->g):
568 bytestream2_get_be16u(&
s->g);
569 if (bytestream2_get_byte(&
s->g)) {
576 if (compno < s->ncomponents) {
578 if (
s->curtileno == -1) {
579 v = bytestream2_get_byte(&
s->g);
582 s->roi_shift[compno] = v;
584 if (
s->tile[
s->curtileno].tp_idx != 0)
586 v = bytestream2_get_byte(&
s->g);
589 s->tile[
s->curtileno].comp[compno].roi_shift = v;
604 x = bytestream2_get_byteu(&
s->g);
614 for (
i = 0;
i < n;
i++)
615 q->
expn[
i] = bytestream2_get_byteu(&
s->g) >> 3;
619 x = bytestream2_get_be16u(&
s->g);
620 q->
expn[0] = x >> 11;
621 q->
mant[0] = x & 0x7ff;
623 int curexpn =
FFMAX(0, q->
expn[0] - (
i - 1) / 3);
624 q->
expn[
i] = curexpn;
632 for (
i = 0;
i < n;
i++) {
633 x = bytestream2_get_be16u(&
s->g);
634 q->
expn[
i] = x >> 11;
635 q->
mant[
i] = x & 0x7ff;
643 const uint8_t *properties)
648 memset(&
tmp, 0,
sizeof(
tmp));
652 for (compno = 0; compno <
s->ncomponents; compno++)
653 if (!(properties[compno] &
HAD_QCC))
654 memcpy(q + compno, &
tmp,
sizeof(
tmp));
668 compno = bytestream2_get_byteu(&
s->g);
670 if (compno >=
s->ncomponents) {
672 "Invalid compno %d. There are %d components in the image.\n",
673 compno,
s->ncomponents);
678 return get_qcx(
s, n - 1, q + compno);
684 int elem_size =
s->ncomponents <= 257 ? 7 : 9;
697 tmp.nb_poc = (
size - 2) / elem_size;
703 for (
i = 0;
i<
tmp.nb_poc;
i++) {
705 e->
RSpoc = bytestream2_get_byteu(&
s->g);
706 e->
CSpoc = bytestream2_get_byteu(&
s->g);
707 e->
LYEpoc = bytestream2_get_be16u(&
s->g);
708 e->
REpoc = bytestream2_get_byteu(&
s->g);
709 e->
CEpoc = bytestream2_get_byteu(&
s->g);
710 e->
Ppoc = bytestream2_get_byteu(&
s->g);
713 if (e->
CEpoc >
s->ncomponents)
714 e->
CEpoc =
s->ncomponents;
754 Isot = bytestream2_get_be16u(&
s->g);
755 if (Isot >=
s->numXtiles *
s->numYtiles)
759 Psot = bytestream2_get_be32u(&
s->g);
760 TPsot = bytestream2_get_byteu(&
s->g);
763 bytestream2_get_byteu(&
s->g);
778 s->tile[Isot].tp_idx = TPsot;
779 tp =
s->tile[Isot].tile_part + TPsot;
781 tp->
tp_end =
s->g.buffer + Psot - n - 2;
789 memcpy(&tile->
poc , &
s->poc ,
sizeof(tile->
poc));
798 if (
s->ncomponents*4 != n - 2) {
815 uint8_t Stlm, ST,
SP, tile_tlm,
i;
816 bytestream2_get_byte(&
s->g);
817 Stlm = bytestream2_get_byte(&
s->g);
820 ST = (Stlm >> 4) & 0x03;
826 SP = (Stlm >> 6) & 0x01;
827 tile_tlm = (n - 4) / ((
SP + 1) * 2 + ST);
828 for (
i = 0;
i < tile_tlm;
i++) {
833 bytestream2_get_byte(&
s->g);
836 bytestream2_get_be16(&
s->g);
839 bytestream2_get_be32(&
s->g);
843 bytestream2_get_be16(&
s->g);
845 bytestream2_get_be32(&
s->g);
862 bytestream2_get_byte(&
s->g);
864 for (
i = 0;
i < n - 3;
i++) {
865 v = bytestream2_get_byte(&
s->g);
881 bytestream2_get_byte(&
s->g);
883 s->packed_headers_size + n - 3);
885 s->packed_headers =
new;
889 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
892 s->packed_headers_size += n - 3;
906 if (
s->curtileno < 0)
909 tile = &
s->tile[
s->curtileno];
912 "PPT marker can occur only on first tile part of a tile.\n");
917 bytestream2_get_byte(&
s->g);
934 int tilex = tileno %
s->numXtiles;
935 int tiley = tileno /
s->numXtiles;
941 tile->
coord[0][0] =
av_clip(tilex * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
942 tile->
coord[0][1] =
av_clip((tilex + 1) * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
943 tile->
coord[1][0] =
av_clip(tiley * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
944 tile->
coord[1][1] =
av_clip((tiley + 1) * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
946 for (compno = 0; compno <
s->ncomponents; compno++) {
967 if (!
comp->roi_shift)
968 comp->roi_shift =
s->roi_shift[compno];
972 s->cbps[compno],
s->cdx[compno],
973 s->cdy[compno],
s->avctx))
988 return num < 0 ? num : 3 + num;
990 return num < 0 ? num : 6 + num;
992 return num < 0 ? num : 37 + num;
1030 av_log(
s->avctx,
AV_LOG_ERROR,
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1037 int layno,
const uint8_t *expn,
int numgbits)
1039 int bandno, cblkno,
ret, nb_code_blocks;
1042 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1059 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1063 if (band->
coord[0][0] == band->
coord[0][1] ||
1068 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1070 int incl, newpasses, llen;
1084 int v = expn[bandno] + numgbits - 1 - zbp;
1086 if (v < 0 || v > 30) {
1088 "nonzerobits %d invalid or unsupported\n", v);
1105 "Block with length beyond 16 bits");
1124 while (newpasses1 < newpasses) {
1134 int href_passes = (cblk->
npasses + newpasses - 1) % 3;
1135 int eb =
av_log2(newpasses - href_passes);
1136 int extra_bit = newpasses > 2 ? 1 : 0;
1165 newpasses -= newpasses1;
1175 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1186 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1191 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1195 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1208 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1236 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1250 int RSpoc,
int CSpoc,
1251 int LYEpoc,
int REpoc,
int CEpoc,
1252 int Ppoc,
int *tp_index)
1255 int layno, reslevelno, compno, precno, ok_reslevel;
1263 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1265 for (layno = 0; layno < LYEpoc; layno++) {
1266 for (compno = CSpoc; compno < CEpoc; compno++) {
1269 if (reslevelno < codsty->nreslevels) {
1277 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1288 for (layno = 0; layno < LYEpoc; layno++) {
1290 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1292 for (compno = CSpoc; compno < CEpoc; compno++) {
1295 if (reslevelno < codsty->nreslevels) {
1303 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1314 for (compno = CSpoc; compno < CEpoc; compno++) {
1324 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1325 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1330 if (step_x >= 31 || step_y >= 31){
1337 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1338 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1339 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1340 unsigned prcx, prcy;
1341 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1343 int xc = x /
s->cdx[compno];
1344 int yc = y /
s->cdy[compno];
1366 for (layno = 0; layno < LYEpoc; layno++) {
1369 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1382 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1386 for (compno = CSpoc; compno < CEpoc; compno++) {
1390 if (reslevelno < codsty->nreslevels) {
1391 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1400 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1401 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1402 for (compno = CSpoc; compno < CEpoc; compno++) {
1406 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1408 unsigned prcx, prcy;
1411 if (!
s->cdx[compno] || !
s->cdy[compno])
1420 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1421 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1424 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1425 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1443 for (layno = 0; layno < LYEpoc; layno++) {
1447 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1461 for (compno = CSpoc; compno < CEpoc; compno++) {
1465 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1466 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1472 if (step_x >= 31 || step_y >= 31){
1479 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1480 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1481 for (compno = CSpoc; compno < CEpoc; compno++) {
1486 if (!
s->cdx[compno] || !
s->cdy[compno])
1489 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1490 unsigned prcx, prcy;
1491 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1498 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1499 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1502 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1503 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1520 for (layno = 0; layno < LYEpoc; layno++) {
1523 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1578 int bpno,
int bandno,
1579 int vert_causal_ctx_csty_symbol)
1581 int mask = 3 << (bpno - 1), y0, x, y;
1583 for (y0 = 0; y0 <
height; y0 += 4)
1584 for (x = 0; x <
width; x++)
1585 for (y = y0; y <
height && y < y0 + 4; y++) {
1586 int flags_mask = -1;
1587 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1600 t1->data[(y) *
t1->stride + x] < 0);
1608 int bpno,
int vert_causal_ctx_csty_symbol)
1613 phalf = 1 << (bpno - 1);
1616 for (y0 = 0; y0 <
height; y0 += 4)
1617 for (x = 0; x <
width; x++)
1618 for (y = y0; y <
height && y < y0 + 4; y++)
1620 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1624 t1->mqc.cx_states + ctxno)
1626 t1->data[(y) *
t1->stride + x] +=
t1->data[(y) *
t1->stride + x] < 0 ? -
r :
r;
1633 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1635 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1637 for (y0 = 0; y0 <
height; y0 += 4) {
1638 for (x = 0; x <
width; x++) {
1639 int flags_mask = -1;
1640 if (vert_causal_ctx_csty_symbol)
1660 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1661 int flags_mask = -1;
1662 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1675 t1->mqc.cx_states + ctxno) ^
1693 "Segmentation symbol value incorrect\n");
1699 int width,
int height,
int bandpos, uint8_t roi_shift)
1710 memset(
t1->data, 0,
t1->stride *
height *
sizeof(*
t1->data));
1716 memset(
t1->flags, 0,
t1->stride * (
height + 2) *
sizeof(*
t1->flags));
1723 if (bpno < 0 || bpno > 29) {
1730 vert_causal_ctx_csty_symbol);
1739 vert_causal_ctx_csty_symbol);
1784 roi_shift =
comp->roi_shift;
1785 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1787 if (
val > (1 << roi_shift))
1788 return (quan_parameter < 0)?-(
val >> roi_shift):(
val >> roi_shift);
1789 return quan_parameter;
1805 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1806 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
1807 int *
src =
t1->data + j*
t1->stride;
1808 for (
i = 0;
i <
w; ++
i)
1820 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1822 int *
src =
t1->data + j*
t1->stride;
1824 for (
i = 0;
i <
w; ++
i)
1825 datap[
i] =
src[
i] / 2;
1828 for (
i = 0;
i <
w; ++
i)
1840 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1842 int *
src =
t1->data + j*
t1->stride;
1843 for (
i = 0;
i <
w; ++
i)
1853 for (
i = 1;
i < 3;
i++) {
1864 for (
i = 0;
i < 3;
i++)
1870 for (
i = 0;
i < 2;
i++)
1882 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1883 int *
src =
t1->data + j*
t1->stride;
1884 for (
i = 0;
i <
w; ++
i)
1893 int compno, reslevelno, bandno;
1896 for (compno = 0; compno <
s->ncomponents; compno++) {
1910 for (bandno = 0; bandno < rlevel->
nbands; bandno++, subbandno++) {
1911 int nb_precincts, precno;
1913 int cblkno = 0, bandpos;
1917 bandpos = bandno + (reslevelno > 0);
1919 if (band->
coord[0][0] == band->
coord[0][1] ||
1930 for (precno = 0; precno < nb_precincts; precno++) {
1945 magp,
comp->roi_shift);
1950 bandpos,
comp->roi_shift);
1959 if (
comp->roi_shift)
1980 #define WRITE_FRAME(D, PIXEL) \
1981 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1982 AVFrame * picture, int precision) \
1984 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1985 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1986 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1991 for (compno = 0; compno < s->ncomponents; compno++) { \
1992 Jpeg2000Component *comp = tile->comp + compno; \
1993 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1995 float *datap = comp->f_data; \
1996 int32_t *i_datap = comp->i_data; \
1997 int cbps = s->cbps[compno]; \
1998 int w = tile->comp[compno].coord[0][1] - \
1999 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2000 int h = tile->comp[compno].coord[1][1] - \
2001 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2005 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2007 y = tile->comp[compno].coord[1][0] - \
2008 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2009 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2010 for (; y < h; y++) { \
2013 x = tile->comp[compno].coord[0][0] - \
2014 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2015 dst = line + x * pixelsize + compno*!planar; \
2017 if (codsty->transform == FF_DWT97) { \
2018 for (; x < w; x++) { \
2019 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2021 val = av_clip(val, 0, (1 << cbps) - 1); \
2022 *dst = val << (precision - cbps); \
2027 for (; x < w; x++) { \
2028 int val = *i_datap + (1 << (cbps - 1)); \
2030 val = av_clip(val, 0, (1 << cbps) - 1); \
2031 *dst = val << (precision - cbps); \
2036 line += picture->linesize[plane] / sizeof(PIXEL); \
2048 int jobnr,
int threadnr)
2062 if (
s->precision <= 8) {
2063 write_frame_8(
s, tile, picture, 8);
2070 write_frame_16(
s, tile, picture, precision);
2079 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2080 if (
s->tile[tileno].comp) {
2081 for (compno = 0; compno <
s->ncomponents; compno++) {
2088 av_freep(&
s->tile[tileno].packed_headers);
2089 s->tile[tileno].packed_headers_size = 0;
2093 s->packed_headers_size = 0;
2094 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2096 memset(
s->codsty, 0,
sizeof(
s->codsty));
2097 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2098 memset(
s->properties, 0,
sizeof(
s->properties));
2099 memset(&
s->poc , 0,
sizeof(
s->poc));
2100 s->numXtiles =
s->numYtiles = 0;
2109 uint8_t *properties =
s->properties;
2121 marker = bytestream2_get_be16u(&
s->g);
2123 if (marker >= 0xFF30 && marker <= 0xFF3F)
2133 if (
s->curtileno < 0) {
2138 tile =
s->tile +
s->curtileno;
2140 if (tp->
tp_end <
s->g.buffer) {
2146 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2164 len = bytestream2_get_be16(&
s->g);
2176 if (
s->ncomponents) {
2182 s->numXtiles =
s->numYtiles = 0;
2203 if (!
s->in_tile_headers) {
2204 s->in_tile_headers = 1;
2211 codsty =
s->tile[
s->curtileno].codsty;
2212 qntsty =
s->tile[
s->curtileno].qntsty;
2213 poc = &
s->tile[
s->curtileno].poc;
2214 properties =
s->tile[
s->curtileno].properties;
2236 if (
s->in_tile_headers) {
2246 "Cannot have both PPT and PPM marker.\n");
2254 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2261 "error during processing marker segment %.4"PRIx16
"\n",
2275 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2290 uint32_t atom_size, atom, atom_end;
2291 int search_range = 10;
2296 atom_size = bytestream2_get_be32u(&
s->g);
2297 atom = bytestream2_get_be32u(&
s->g);
2298 if (atom_size == 1) {
2299 if (bytestream2_get_be32u(&
s->g)) {
2303 atom_size = bytestream2_get_be32u(&
s->g);
2304 if (atom_size < 16 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 16 > INT_MAX)
2308 if (atom_size < 8 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 8 > INT_MAX)
2321 uint32_t atom2_size, atom2, atom2_end;
2325 atom2_size = bytestream2_get_be32u(&
s->g);
2326 atom2 = bytestream2_get_be32u(&
s->g);
2328 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2333 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2334 int method = bytestream2_get_byteu(&
s->g);
2337 s->colour_space = bytestream2_get_be32u(&
s->g);
2339 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2340 int i,
size, colour_count, colour_channels, colour_depth[3];
2341 colour_count = bytestream2_get_be16u(&
s->g);
2342 colour_channels = bytestream2_get_byteu(&
s->g);
2344 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2345 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2346 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2347 size = (colour_depth[0] + 7 >> 3) * colour_count +
2348 (colour_depth[1] + 7 >> 3) * colour_count +
2349 (colour_depth[2] + 7 >> 3) * colour_count;
2351 colour_channels != 3 ||
2352 colour_depth[0] > 16 ||
2353 colour_depth[1] > 16 ||
2354 colour_depth[2] > 16 ||
2355 atom2_size <
size) {
2361 for (
i = 0;
i < colour_count;
i++) {
2363 if (colour_depth[0] <= 8) {
2364 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2365 r |=
r >> colour_depth[0];
2367 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2369 if (colour_depth[1] <= 8) {
2370 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2371 g |=
g >> colour_depth[1];
2373 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2375 if (colour_depth[2] <= 8) {
2376 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2377 b |=
b >> colour_depth[2];
2379 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2381 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2383 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2384 int n = bytestream2_get_be16u(&
s->g);
2386 int cn = bytestream2_get_be16(&
s->g);
2387 int av_unused typ = bytestream2_get_be16(&
s->g);
2388 int asoc = bytestream2_get_be16(&
s->g);
2389 if (cn < 4 && asoc < 4)
2392 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2393 int64_t vnum, vden, hnum, hden, vexp, hexp;
2396 resx = bytestream2_get_be32u(&
s->g);
2397 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2401 vnum = bytestream2_get_be16u(&
s->g);
2402 vden = bytestream2_get_be16u(&
s->g);
2403 hnum = bytestream2_get_be16u(&
s->g);
2404 hden = bytestream2_get_be16u(&
s->g);
2405 vexp = bytestream2_get_byteu(&
s->g);
2406 hexp = bytestream2_get_byteu(&
s->g);
2407 if (!vnum || !vden || !hnum || !hden) {
2419 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2420 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2422 hnum * vden * pow(10, hexp),
2423 vnum * hden * pow(10, vexp),
2427 }
while (atom_end - atom2_end >= 8);
2444 s->reduction_factor = avctx->
lowres;
2464 memset(
s->cdef, -1,
sizeof(
s->cdef));
2473 (bytestream2_get_be32u(&
s->g) == 12) &&
2478 "Could not find Jpeg2000 codestream atom.\n");
2497 if (
s->sar.num &&
s->sar.den)
2499 s->sar.num =
s->sar.den = 0;
2515 for (
int x = 0; x <
s->ncomponents; x++) {
2516 if (
s->cdef[x] < 0) {
2517 for (x = 0; x <
s->ncomponents; x++) {
2520 if ((
s->ncomponents & 1) == 0)
2521 s->cdef[
s->ncomponents-1] = 0;
2533 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2542 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2543 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2546 {
"lowres",
"Lower the decoding resolution by a power of two",
2559 .
p.
name =
"jpeg2000",