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);
840 bytestream2_get_be16(&
s->g);
842 bytestream2_get_be32(&
s->g);
859 bytestream2_get_byte(&
s->g);
861 for (
i = 0;
i < n - 3;
i++) {
862 v = bytestream2_get_byte(&
s->g);
878 bytestream2_get_byte(&
s->g);
880 s->packed_headers_size + n - 3);
882 s->packed_headers =
new;
886 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
889 s->packed_headers_size += n - 3;
903 if (
s->curtileno < 0)
906 tile = &
s->tile[
s->curtileno];
909 "PPT marker can occur only on first tile part of a tile.\n");
914 bytestream2_get_byte(&
s->g);
931 int tilex = tileno %
s->numXtiles;
932 int tiley = tileno /
s->numXtiles;
938 tile->
coord[0][0] =
av_clip(tilex * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
939 tile->
coord[0][1] =
av_clip((tilex + 1) * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
940 tile->
coord[1][0] =
av_clip(tiley * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
941 tile->
coord[1][1] =
av_clip((tiley + 1) * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
943 for (compno = 0; compno <
s->ncomponents; compno++) {
964 if (!
comp->roi_shift)
965 comp->roi_shift =
s->roi_shift[compno];
969 s->cbps[compno],
s->cdx[compno],
970 s->cdy[compno],
s->avctx))
985 return num < 0 ? num : 3 + num;
987 return num < 0 ? num : 6 + num;
989 return num < 0 ? num : 37 + num;
1027 av_log(
s->avctx,
AV_LOG_ERROR,
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1034 int layno,
const uint8_t *expn,
int numgbits)
1036 int bandno, cblkno,
ret, nb_code_blocks;
1039 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1056 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1060 if (band->
coord[0][0] == band->
coord[0][1] ||
1065 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1067 int incl, newpasses, llen;
1081 int v = expn[bandno] + numgbits - 1 - zbp;
1083 if (v < 0 || v > 30) {
1085 "nonzerobits %d invalid or unsupported\n", v);
1102 "Block with length beyond 16 bits");
1121 while (newpasses1 < newpasses) {
1131 int href_passes = (cblk->
npasses + newpasses - 1) % 3;
1132 int eb =
av_log2(newpasses - href_passes);
1133 int extra_bit = newpasses > 2 ? 1 : 0;
1162 newpasses -= newpasses1;
1172 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1183 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1188 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1192 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1205 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1233 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1247 int RSpoc,
int CSpoc,
1248 int LYEpoc,
int REpoc,
int CEpoc,
1249 int Ppoc,
int *tp_index)
1252 int layno, reslevelno, compno, precno, ok_reslevel;
1260 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1262 for (layno = 0; layno < LYEpoc; layno++) {
1263 for (compno = CSpoc; compno < CEpoc; compno++) {
1266 if (reslevelno < codsty->nreslevels) {
1274 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1285 for (layno = 0; layno < LYEpoc; layno++) {
1287 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1289 for (compno = CSpoc; compno < CEpoc; compno++) {
1292 if (reslevelno < codsty->nreslevels) {
1300 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1311 for (compno = CSpoc; compno < CEpoc; compno++) {
1321 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1322 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1327 if (step_x >= 31 || step_y >= 31){
1334 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1335 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1336 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1337 unsigned prcx, prcy;
1338 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1340 int xc = x /
s->cdx[compno];
1341 int yc = y /
s->cdy[compno];
1363 for (layno = 0; layno < LYEpoc; layno++) {
1366 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1379 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1383 for (compno = CSpoc; compno < CEpoc; compno++) {
1387 if (reslevelno < codsty->nreslevels) {
1388 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1397 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1398 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1399 for (compno = CSpoc; compno < CEpoc; compno++) {
1403 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1405 unsigned prcx, prcy;
1408 if (!
s->cdx[compno] || !
s->cdy[compno])
1417 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1418 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1421 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1422 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1440 for (layno = 0; layno < LYEpoc; layno++) {
1444 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1458 for (compno = CSpoc; compno < CEpoc; compno++) {
1462 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1463 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1469 if (step_x >= 31 || step_y >= 31){
1476 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1477 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1478 for (compno = CSpoc; compno < CEpoc; compno++) {
1483 if (!
s->cdx[compno] || !
s->cdy[compno])
1486 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1487 unsigned prcx, prcy;
1488 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1495 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1496 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1499 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1500 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1517 for (layno = 0; layno < LYEpoc; layno++) {
1520 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1575 int bpno,
int bandno,
1576 int vert_causal_ctx_csty_symbol)
1578 int mask = 3 << (bpno - 1), y0, x, y;
1580 for (y0 = 0; y0 <
height; y0 += 4)
1581 for (x = 0; x <
width; x++)
1582 for (y = y0; y <
height && y < y0 + 4; y++) {
1583 int flags_mask = -1;
1584 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1597 t1->data[(y) *
t1->stride + x] < 0);
1605 int bpno,
int vert_causal_ctx_csty_symbol)
1610 phalf = 1 << (bpno - 1);
1613 for (y0 = 0; y0 <
height; y0 += 4)
1614 for (x = 0; x <
width; x++)
1615 for (y = y0; y <
height && y < y0 + 4; y++)
1617 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1621 t1->mqc.cx_states + ctxno)
1623 t1->data[(y) *
t1->stride + x] +=
t1->data[(y) *
t1->stride + x] < 0 ? -
r :
r;
1630 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1632 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1634 for (y0 = 0; y0 <
height; y0 += 4) {
1635 for (x = 0; x <
width; x++) {
1636 int flags_mask = -1;
1637 if (vert_causal_ctx_csty_symbol)
1657 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1658 int flags_mask = -1;
1659 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1672 t1->mqc.cx_states + ctxno) ^
1690 "Segmentation symbol value incorrect\n");
1696 int width,
int height,
int bandpos, uint8_t roi_shift)
1707 memset(
t1->data, 0,
t1->stride *
height *
sizeof(*
t1->data));
1713 memset(
t1->flags, 0,
t1->stride * (
height + 2) *
sizeof(*
t1->flags));
1720 if (bpno < 0 || bpno > 29) {
1727 vert_causal_ctx_csty_symbol);
1736 vert_causal_ctx_csty_symbol);
1781 roi_shift =
comp->roi_shift;
1782 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1784 if (
val > (1 << roi_shift))
1785 return (quan_parameter < 0)?-(
val >> roi_shift):(
val >> roi_shift);
1786 return quan_parameter;
1802 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1803 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
1804 int *
src =
t1->data + j*
t1->stride;
1805 for (
i = 0;
i <
w; ++
i)
1817 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1819 int *
src =
t1->data + j*
t1->stride;
1821 for (
i = 0;
i <
w; ++
i)
1822 datap[
i] =
src[
i] / 2;
1825 for (
i = 0;
i <
w; ++
i)
1837 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1839 int *
src =
t1->data + j*
t1->stride;
1840 for (
i = 0;
i <
w; ++
i)
1850 for (
i = 1;
i < 3;
i++) {
1861 for (
i = 0;
i < 3;
i++)
1867 for (
i = 0;
i < 2;
i++)
1879 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1880 int *
src =
t1->data + j*
t1->stride;
1881 for (
i = 0;
i <
w; ++
i)
1890 int compno, reslevelno, bandno;
1893 for (compno = 0; compno <
s->ncomponents; compno++) {
1907 for (bandno = 0; bandno < rlevel->
nbands; bandno++, subbandno++) {
1908 int nb_precincts, precno;
1910 int cblkno = 0, bandpos;
1914 bandpos = bandno + (reslevelno > 0);
1916 if (band->
coord[0][0] == band->
coord[0][1] ||
1927 for (precno = 0; precno < nb_precincts; precno++) {
1942 magp,
comp->roi_shift);
1947 bandpos,
comp->roi_shift);
1956 if (
comp->roi_shift)
1977 #define WRITE_FRAME(D, PIXEL) \
1978 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1979 AVFrame * picture, int precision) \
1981 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1982 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1983 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1988 for (compno = 0; compno < s->ncomponents; compno++) { \
1989 Jpeg2000Component *comp = tile->comp + compno; \
1990 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1992 float *datap = comp->f_data; \
1993 int32_t *i_datap = comp->i_data; \
1994 int cbps = s->cbps[compno]; \
1995 int w = tile->comp[compno].coord[0][1] - \
1996 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
1997 int h = tile->comp[compno].coord[1][1] - \
1998 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2002 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2004 y = tile->comp[compno].coord[1][0] - \
2005 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2006 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2007 for (; y < h; y++) { \
2010 x = tile->comp[compno].coord[0][0] - \
2011 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2012 dst = line + x * pixelsize + compno*!planar; \
2014 if (codsty->transform == FF_DWT97) { \
2015 for (; x < w; x++) { \
2016 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2018 val = av_clip(val, 0, (1 << cbps) - 1); \
2019 *dst = val << (precision - cbps); \
2024 for (; x < w; x++) { \
2025 int val = *i_datap + (1 << (cbps - 1)); \
2027 val = av_clip(val, 0, (1 << cbps) - 1); \
2028 *dst = val << (precision - cbps); \
2033 line += picture->linesize[plane] / sizeof(PIXEL); \
2045 int jobnr,
int threadnr)
2059 if (
s->precision <= 8) {
2060 write_frame_8(
s, tile, picture, 8);
2067 write_frame_16(
s, tile, picture, precision);
2076 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2077 if (
s->tile[tileno].comp) {
2078 for (compno = 0; compno <
s->ncomponents; compno++) {
2085 av_freep(&
s->tile[tileno].packed_headers);
2086 s->tile[tileno].packed_headers_size = 0;
2090 s->packed_headers_size = 0;
2091 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2093 memset(
s->codsty, 0,
sizeof(
s->codsty));
2094 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2095 memset(
s->properties, 0,
sizeof(
s->properties));
2096 memset(&
s->poc , 0,
sizeof(
s->poc));
2097 s->numXtiles =
s->numYtiles = 0;
2106 uint8_t *properties =
s->properties;
2118 marker = bytestream2_get_be16u(&
s->g);
2120 if (marker >= 0xFF30 && marker <= 0xFF3F)
2130 if (
s->curtileno < 0) {
2135 tile =
s->tile +
s->curtileno;
2137 if (tp->
tp_end <
s->g.buffer) {
2143 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2161 len = bytestream2_get_be16(&
s->g);
2173 if (
s->ncomponents) {
2179 s->numXtiles =
s->numYtiles = 0;
2200 if (!
s->in_tile_headers) {
2201 s->in_tile_headers = 1;
2208 codsty =
s->tile[
s->curtileno].codsty;
2209 qntsty =
s->tile[
s->curtileno].qntsty;
2210 poc = &
s->tile[
s->curtileno].poc;
2211 properties =
s->tile[
s->curtileno].properties;
2233 if (
s->in_tile_headers) {
2243 "Cannot have both PPT and PPM marker.\n");
2251 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2258 "error during processing marker segment %.4"PRIx16
"\n",
2272 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2287 uint32_t atom_size, atom, atom_end;
2288 int search_range = 10;
2293 atom_size = bytestream2_get_be32u(&
s->g);
2294 atom = bytestream2_get_be32u(&
s->g);
2295 if (atom_size == 1) {
2296 if (bytestream2_get_be32u(&
s->g)) {
2300 atom_size = bytestream2_get_be32u(&
s->g);
2301 if (atom_size < 16 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 16 > INT_MAX)
2305 if (atom_size < 8 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 8 > INT_MAX)
2318 uint32_t atom2_size, atom2, atom2_end;
2322 atom2_size = bytestream2_get_be32u(&
s->g);
2323 atom2 = bytestream2_get_be32u(&
s->g);
2325 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2330 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2331 int method = bytestream2_get_byteu(&
s->g);
2334 s->colour_space = bytestream2_get_be32u(&
s->g);
2336 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2337 int i,
size, colour_count, colour_channels, colour_depth[3];
2338 colour_count = bytestream2_get_be16u(&
s->g);
2339 colour_channels = bytestream2_get_byteu(&
s->g);
2341 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2342 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2343 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2344 size = (colour_depth[0] + 7 >> 3) * colour_count +
2345 (colour_depth[1] + 7 >> 3) * colour_count +
2346 (colour_depth[2] + 7 >> 3) * colour_count;
2348 colour_channels != 3 ||
2349 colour_depth[0] > 16 ||
2350 colour_depth[1] > 16 ||
2351 colour_depth[2] > 16 ||
2352 atom2_size <
size) {
2358 for (
i = 0;
i < colour_count;
i++) {
2360 if (colour_depth[0] <= 8) {
2361 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2362 r |=
r >> colour_depth[0];
2364 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2366 if (colour_depth[1] <= 8) {
2367 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2368 g |=
g >> colour_depth[1];
2370 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2372 if (colour_depth[2] <= 8) {
2373 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2374 b |=
b >> colour_depth[2];
2376 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2378 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2380 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2381 int n = bytestream2_get_be16u(&
s->g);
2383 int cn = bytestream2_get_be16(&
s->g);
2384 int av_unused typ = bytestream2_get_be16(&
s->g);
2385 int asoc = bytestream2_get_be16(&
s->g);
2386 if (cn < 4 && asoc < 4)
2389 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2390 int64_t vnum, vden, hnum, hden, vexp, hexp;
2393 resx = bytestream2_get_be32u(&
s->g);
2394 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2398 vnum = bytestream2_get_be16u(&
s->g);
2399 vden = bytestream2_get_be16u(&
s->g);
2400 hnum = bytestream2_get_be16u(&
s->g);
2401 hden = bytestream2_get_be16u(&
s->g);
2402 vexp = bytestream2_get_byteu(&
s->g);
2403 hexp = bytestream2_get_byteu(&
s->g);
2404 if (!vnum || !vden || !hnum || !hden) {
2416 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2417 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2419 hnum * vden * pow(10, hexp),
2420 vnum * hden * pow(10, vexp),
2424 }
while (atom_end - atom2_end >= 8);
2441 s->reduction_factor = avctx->
lowres;
2461 memset(
s->cdef, -1,
sizeof(
s->cdef));
2470 (bytestream2_get_be32u(&
s->g) == 12) &&
2475 "Could not find Jpeg2000 codestream atom.\n");
2494 if (
s->sar.num &&
s->sar.den)
2496 s->sar.num =
s->sar.den = 0;
2512 for (
int x = 0; x <
s->ncomponents; x++) {
2513 if (
s->cdef[x] < 0) {
2514 for (x = 0; x <
s->ncomponents; x++) {
2517 if ((
s->ncomponents & 1) == 0)
2518 s->cdef[
s->ncomponents-1] = 0;
2530 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2539 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2540 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2543 {
"lowres",
"Lower the decoding resolution by a power of two",
2556 .
p.
name =
"jpeg2000",