48 #define JP2_SIG_TYPE 0x6A502020
49 #define JP2_SIG_VALUE 0x0D0A870A
50 #define JP2_CODESTREAM 0x6A703263
51 #define JP2_HEADER 0x6A703268
66 if (
s->bit_index == 0) {
67 s->bit_index = 7 + (bytestream2_get_byte(&
s->g) != 0xFF
u);
70 res |= (bytestream2_peek_byte(&
s->g) >>
s->bit_index) & 1;
77 if (bytestream2_get_byte(&
s->g) == 0xff)
87 int sp = -1, curval = 0;
94 while (node && !node->
vis) {
102 curval = stack[
sp]->
val;
104 while (curval < threshold && sp >= 0) {
105 if (curval < stack[
sp]->
val)
106 curval = stack[
sp]->
val;
107 while (curval < threshold) {
117 stack[
sp]->
val = curval;
124 int bpc, uint32_t log2_chroma_wh,
int pal8)
131 if (
desc->nb_components != components) {
135 switch (components) {
137 match = match &&
desc->comp[3].depth >= bpc &&
138 (log2_chroma_wh >> 14 & 3) == 0 &&
139 (log2_chroma_wh >> 12 & 3) == 0;
141 match = match &&
desc->comp[2].depth >= bpc &&
142 (log2_chroma_wh >> 10 & 3) ==
desc->log2_chroma_w &&
143 (log2_chroma_wh >> 8 & 3) ==
desc->log2_chroma_h;
145 match = match &&
desc->comp[1].depth >= bpc &&
146 (log2_chroma_wh >> 6 & 3) ==
desc->log2_chroma_w &&
147 (log2_chroma_wh >> 4 & 3) ==
desc->log2_chroma_h;
150 match = match &&
desc->comp[0].depth >= bpc &&
151 (log2_chroma_wh >> 2 & 3) == 0 &&
152 (log2_chroma_wh & 3) == 0 &&
160 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
161 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
162 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
163 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
164 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
165 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
166 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
167 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
168 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
169 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
170 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
171 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
172 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
173 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
191 uint32_t log2_chroma_wh = 0;
193 int possible_fmts_nb = 0;
203 s->avctx->profile = bytestream2_get_be16u(&
s->g);
204 s->width = bytestream2_get_be32u(&
s->g);
205 s->height = bytestream2_get_be32u(&
s->g);
206 s->image_offset_x = bytestream2_get_be32u(&
s->g);
207 s->image_offset_y = bytestream2_get_be32u(&
s->g);
208 s->tile_width = bytestream2_get_be32u(&
s->g);
209 s->tile_height = bytestream2_get_be32u(&
s->g);
210 s->tile_offset_x = bytestream2_get_be32u(&
s->g);
211 s->tile_offset_y = bytestream2_get_be32u(&
s->g);
212 ncomponents = bytestream2_get_be16u(&
s->g);
219 if (ncomponents <= 0) {
225 if (ncomponents > 4) {
231 if (
s->tile_offset_x < 0 ||
s->tile_offset_y < 0 ||
232 s->image_offset_x <
s->tile_offset_x ||
233 s->image_offset_y <
s->tile_offset_y ||
234 s->tile_width + (int64_t)
s->tile_offset_x <=
s->image_offset_x ||
235 s->tile_height + (int64_t)
s->tile_offset_y <=
s->image_offset_y
241 if (
s->image_offset_x >=
s->width ||
s->image_offset_y >=
s->height) {
246 if (
s->reduction_factor && (
s->image_offset_x ||
s->image_offset_y) ){
247 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction factor with image offsets is not fully implemented");
251 s->ncomponents = ncomponents;
253 if (
s->tile_width <= 0 ||
s->tile_height <= 0) {
255 s->tile_width,
s->tile_height);
260 av_log(
s->avctx,
AV_LOG_ERROR,
"Insufficient space for %d components in SIZ\n",
s->ncomponents);
264 for (
i = 0;
i <
s->ncomponents;
i++) {
265 uint8_t x = bytestream2_get_byteu(&
s->g);
266 s->cbps[
i] = (x & 0x7f) + 1;
267 s->precision =
FFMAX(
s->cbps[
i],
s->precision);
268 s->sgnd[
i] = !!(x & 0x80);
269 s->cdx[
i] = bytestream2_get_byteu(&
s->g);
270 s->cdy[
i] = bytestream2_get_byteu(&
s->g);
271 if ( !
s->cdx[
i] ||
s->cdx[
i] == 3 ||
s->cdx[
i] > 4
272 || !
s->cdy[
i] ||
s->cdy[
i] == 3 ||
s->cdy[
i] > 4) {
276 log2_chroma_wh |=
s->cdy[
i] >> 1 <<
i * 4 |
s->cdx[
i] >> 1 <<
i * 4 + 2;
283 if (
s->numXtiles * (uint64_t)
s->numYtiles > INT_MAX/
sizeof(*
s->tile) ||
286 s->numXtiles =
s->numYtiles = 0;
290 s->tile =
av_calloc(
s->numXtiles *
s->numYtiles,
sizeof(*
s->tile));
292 s->numXtiles =
s->numYtiles = 0;
296 for (
i = 0;
i <
s->numXtiles *
s->numYtiles;
i++) {
306 s->reduction_factor);
308 s->reduction_factor);
311 for (
i = 1;
i <
s->ncomponents;
i++) {
325 switch (
s->colour_space) {
345 && !
pix_fmt_match(
s->avctx->pix_fmt, ncomponents,
s->precision, log2_chroma_wh,
s->pal8))
348 for (
i = 0;
i < possible_fmts_nb; ++
i) {
349 if (
pix_fmt_match(possible_fmts[
i], ncomponents,
s->precision, log2_chroma_wh,
s->pal8)) {
350 s->avctx->pix_fmt = possible_fmts[
i];
355 if (
i == possible_fmts_nb) {
356 if (ncomponents == 4 &&
357 s->cdy[0] == 1 &&
s->cdx[0] == 1 &&
358 s->cdy[1] == 1 &&
s->cdx[1] == 1 &&
359 s->cdy[2] ==
s->cdy[3] &&
s->cdx[2] ==
s->cdx[3]) {
360 if (
s->precision == 8 &&
s->cdy[2] == 2 &&
s->cdx[2] == 2 && !
s->pal8) {
368 }
else if (ncomponents == 3 &&
s->precision == 8 &&
369 s->cdx[0] ==
s->cdx[1] &&
s->cdx[0] ==
s->cdx[2] &&
370 s->cdy[0] ==
s->cdy[1] &&
s->cdy[0] ==
s->cdy[2]) {
373 }
else if (ncomponents == 2 &&
s->precision == 8 &&
374 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
377 }
else if (ncomponents == 2 &&
s->precision == 16 &&
378 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
381 }
else if (ncomponents == 1 &&
s->precision == 8) {
388 if (
i == possible_fmts_nb) {
390 "Unknown pix_fmt, profile: %d, colour_space: %d, "
391 "components: %d, precision: %d\n"
392 "cdx[0]: %d, cdy[0]: %d\n"
393 "cdx[1]: %d, cdy[1]: %d\n"
394 "cdx[2]: %d, cdy[2]: %d\n"
395 "cdx[3]: %d, cdy[3]: %d\n",
396 s->avctx->profile,
s->colour_space, ncomponents,
s->precision,
399 ncomponents > 1 ?
s->cdx[1] : 0,
400 ncomponents > 1 ?
s->cdy[1] : 0,
401 ncomponents > 2 ?
s->cdx[2] : 0,
402 ncomponents > 2 ?
s->cdy[2] : 0,
403 ncomponents > 3 ?
s->cdx[3] : 0,
404 ncomponents > 3 ?
s->cdy[3] : 0);
407 s->avctx->bits_per_raw_sample =
s->precision;
423 c->nreslevels = bytestream2_get_byteu(&
s->g) + 1;
429 if (
c->nreslevels <=
s->reduction_factor) {
434 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction_factor too large for this bitstream, max is %d\n",
c->nreslevels - 1);
435 s->reduction_factor =
c->nreslevels - 1;
440 c->nreslevels2decode =
c->nreslevels -
s->reduction_factor;
442 c->log2_cblk_width = (bytestream2_get_byteu(&
s->g) & 15) + 2;
443 c->log2_cblk_height = (bytestream2_get_byteu(&
s->g) & 15) + 2;
445 if (
c->log2_cblk_width > 10 ||
c->log2_cblk_height > 10 ||
446 c->log2_cblk_width +
c->log2_cblk_height > 12) {
451 c->cblk_style = bytestream2_get_byteu(&
s->g);
452 if (
c->cblk_style != 0) {
461 c->transform = bytestream2_get_byteu(&
s->g);
471 for (
i = 0;
i <
c->nreslevels;
i++) {
472 byte = bytestream2_get_byte(&
s->g);
473 c->log2_prec_widths[
i] =
byte & 0x0F;
474 c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;
476 if (
c->log2_prec_widths[
i] == 0 ||
c->log2_prec_heights[
i] == 0) {
478 c->log2_prec_widths[
i],
c->log2_prec_heights[
i]);
479 c->log2_prec_widths[
i] =
c->log2_prec_heights[
i] = 1;
484 memset(
c->log2_prec_widths , 15,
sizeof(
c->log2_prec_widths ));
485 memset(
c->log2_prec_heights, 15,
sizeof(
c->log2_prec_heights));
502 tmp.csty = bytestream2_get_byteu(&
s->g);
505 tmp.prog_order = bytestream2_get_byteu(&
s->g);
507 tmp.nlayers = bytestream2_get_be16u(&
s->g);
508 tmp.mct = bytestream2_get_byteu(&
s->g);
510 if (
tmp.mct &&
s->ncomponents < 3) {
512 "MCT %"PRIu8
" with too few components (%d)\n",
513 tmp.mct,
s->ncomponents);
520 for (compno = 0; compno <
s->ncomponents; compno++)
521 if (!(properties[compno] &
HAD_COC))
522 memcpy(
c + compno, &
tmp,
sizeof(
tmp));
532 uint8_t has_eph, has_sop;
539 compno = bytestream2_get_byteu(&
s->g);
541 if (compno >=
s->ncomponents) {
543 "Invalid compno %d. There are %d components in the image.\n",
544 compno,
s->ncomponents);
551 c->csty = bytestream2_get_byteu(&
s->g);
566 compno = (
s->ncomponents < 257)? bytestream2_get_byte(&
s->g):
567 bytestream2_get_be16u(&
s->g);
568 if (bytestream2_get_byte(&
s->g)) {
575 if (compno < s->ncomponents) {
577 if (
s->curtileno == -1) {
578 v = bytestream2_get_byte(&
s->g);
581 s->roi_shift[compno] = v;
583 if (
s->tile[
s->curtileno].tp_idx != 0)
585 v = bytestream2_get_byte(&
s->g);
588 s->tile[
s->curtileno].comp[compno].roi_shift = v;
603 x = bytestream2_get_byteu(&
s->g);
613 for (
i = 0;
i < n;
i++)
614 q->
expn[
i] = bytestream2_get_byteu(&
s->g) >> 3;
618 x = bytestream2_get_be16u(&
s->g);
619 q->
expn[0] = x >> 11;
620 q->
mant[0] = x & 0x7ff;
622 int curexpn =
FFMAX(0, q->
expn[0] - (
i - 1) / 3);
623 q->
expn[
i] = curexpn;
631 for (
i = 0;
i < n;
i++) {
632 x = bytestream2_get_be16u(&
s->g);
633 q->
expn[
i] = x >> 11;
634 q->
mant[
i] = x & 0x7ff;
647 memset(&
tmp, 0,
sizeof(
tmp));
651 for (compno = 0; compno <
s->ncomponents; compno++)
652 if (!(properties[compno] &
HAD_QCC))
653 memcpy(q + compno, &
tmp,
sizeof(
tmp));
667 compno = bytestream2_get_byteu(&
s->g);
669 if (compno >=
s->ncomponents) {
671 "Invalid compno %d. There are %d components in the image.\n",
672 compno,
s->ncomponents);
677 return get_qcx(
s, n - 1, q + compno);
683 int elem_size =
s->ncomponents <= 257 ? 7 : 9;
696 tmp.nb_poc = (
size - 2) / elem_size;
702 for (
i = 0;
i<
tmp.nb_poc;
i++) {
704 e->
RSpoc = bytestream2_get_byteu(&
s->g);
705 e->
CSpoc = bytestream2_get_byteu(&
s->g);
706 e->
LYEpoc = bytestream2_get_be16u(&
s->g);
707 e->
REpoc = bytestream2_get_byteu(&
s->g);
708 e->
CEpoc = bytestream2_get_byteu(&
s->g);
709 e->
Ppoc = bytestream2_get_byteu(&
s->g);
712 if (e->
CEpoc >
s->ncomponents)
713 e->
CEpoc =
s->ncomponents;
753 Isot = bytestream2_get_be16u(&
s->g);
754 if (Isot >=
s->numXtiles *
s->numYtiles)
758 Psot = bytestream2_get_be32u(&
s->g);
759 TPsot = bytestream2_get_byteu(&
s->g);
762 bytestream2_get_byteu(&
s->g);
777 s->tile[Isot].tp_idx = TPsot;
778 tp =
s->tile[Isot].tile_part + TPsot;
780 tp->
tp_end =
s->g.buffer + Psot - n - 2;
788 memcpy(&tile->
poc , &
s->poc ,
sizeof(tile->
poc));
797 if (
s->ncomponents*4 != n - 2) {
814 uint8_t Stlm, ST,
SP, tile_tlm,
i;
815 bytestream2_get_byte(&
s->g);
816 Stlm = bytestream2_get_byte(&
s->g);
819 ST = (Stlm >> 4) & 0x03;
825 SP = (Stlm >> 6) & 0x01;
826 tile_tlm = (n - 4) / ((
SP + 1) * 2 + ST);
827 for (
i = 0;
i < tile_tlm;
i++) {
832 bytestream2_get_byte(&
s->g);
835 bytestream2_get_be16(&
s->g);
838 bytestream2_get_be32(&
s->g);
842 bytestream2_get_be16(&
s->g);
844 bytestream2_get_be32(&
s->g);
861 bytestream2_get_byte(&
s->g);
863 for (
i = 0;
i < n - 3;
i++) {
864 v = bytestream2_get_byte(&
s->g);
880 bytestream2_get_byte(&
s->g);
882 s->packed_headers_size + n - 3);
884 s->packed_headers =
new;
888 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
891 s->packed_headers_size += n - 3;
905 if (
s->curtileno < 0)
908 tile = &
s->tile[
s->curtileno];
911 "PPT marker can occur only on first tile part of a tile.\n");
916 bytestream2_get_byte(&
s->g);
935 int tilex = tileno %
s->numXtiles;
936 int tiley = tileno /
s->numXtiles;
942 tile->
coord[0][0] =
av_clip(tilex * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
943 tile->
coord[0][1] =
av_clip((tilex + 1) * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
944 tile->
coord[1][0] =
av_clip(tiley * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
945 tile->
coord[1][1] =
av_clip((tiley + 1) * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
947 for (compno = 0; compno <
s->ncomponents; compno++) {
968 if (!
comp->roi_shift)
969 comp->roi_shift =
s->roi_shift[compno];
973 s->cbps[compno],
s->cdx[compno],
974 s->cdy[compno],
s->avctx))
989 return num < 0 ? num : 3 + num;
991 return num < 0 ? num : 6 + num;
993 return num < 0 ? num : 37 + num;
1031 av_log(
s->avctx,
AV_LOG_ERROR,
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1038 int layno, uint8_t *expn,
int numgbits)
1040 int bandno, cblkno,
ret, nb_code_blocks;
1043 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1060 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1064 if (band->
coord[0][0] == band->
coord[0][1] ||
1069 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1071 int incl, newpasses, llen;
1085 int v = expn[bandno] + numgbits - 1 - zbp;
1087 if (v < 0 || v > 30) {
1089 "nonzerobits %d invalid or unsupported\n", v);
1106 "Block with length beyond 16 bits");
1125 while (newpasses1 < newpasses) {
1135 int href_passes = (cblk->
npasses + newpasses - 1) % 3;
1136 int eb =
av_log2(newpasses - href_passes);
1137 int extra_bit = newpasses > 2 ? 1 : 0;
1166 newpasses -= newpasses1;
1176 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1187 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1192 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1196 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1209 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1237 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1251 int RSpoc,
int CSpoc,
1252 int LYEpoc,
int REpoc,
int CEpoc,
1253 int Ppoc,
int *tp_index)
1256 int layno, reslevelno, compno, precno, ok_reslevel;
1264 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1266 for (layno = 0; layno < LYEpoc; layno++) {
1267 for (compno = CSpoc; compno < CEpoc; compno++) {
1270 if (reslevelno < codsty->nreslevels) {
1278 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1289 for (layno = 0; layno < LYEpoc; layno++) {
1291 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1293 for (compno = CSpoc; compno < CEpoc; compno++) {
1296 if (reslevelno < codsty->nreslevels) {
1304 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1315 for (compno = CSpoc; compno < CEpoc; compno++) {
1325 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1326 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1331 if (step_x >= 31 || step_y >= 31){
1338 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1339 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1340 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1341 unsigned prcx, prcy;
1342 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1344 int xc = x /
s->cdx[compno];
1345 int yc = y /
s->cdy[compno];
1367 for (layno = 0; layno < LYEpoc; layno++) {
1370 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1383 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1387 for (compno = CSpoc; compno < CEpoc; compno++) {
1391 if (reslevelno < codsty->nreslevels) {
1392 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1401 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1402 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1403 for (compno = CSpoc; compno < CEpoc; compno++) {
1407 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1409 unsigned prcx, prcy;
1412 if (!
s->cdx[compno] || !
s->cdy[compno])
1421 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1422 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1425 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1426 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1444 for (layno = 0; layno < LYEpoc; layno++) {
1448 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1462 for (compno = CSpoc; compno < CEpoc; compno++) {
1466 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1467 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1473 if (step_x >= 31 || step_y >= 31){
1480 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1481 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1482 for (compno = CSpoc; compno < CEpoc; compno++) {
1487 if (!
s->cdx[compno] || !
s->cdy[compno])
1490 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1491 unsigned prcx, prcy;
1492 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1499 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1500 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1503 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1504 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1521 for (layno = 0; layno < LYEpoc; layno++) {
1524 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1579 int bpno,
int bandno,
1580 int vert_causal_ctx_csty_symbol)
1582 int mask = 3 << (bpno - 1), y0, x, y;
1584 for (y0 = 0; y0 <
height; y0 += 4)
1585 for (x = 0; x <
width; x++)
1586 for (y = y0; y <
height && y < y0 + 4; y++) {
1587 int flags_mask = -1;
1588 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1601 t1->data[(y) *
t1->stride + x] < 0);
1609 int bpno,
int vert_causal_ctx_csty_symbol)
1614 phalf = 1 << (bpno - 1);
1617 for (y0 = 0; y0 <
height; y0 += 4)
1618 for (x = 0; x <
width; x++)
1619 for (y = y0; y <
height && y < y0 + 4; y++)
1621 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1625 t1->mqc.cx_states + ctxno)
1627 t1->data[(y) *
t1->stride + x] +=
t1->data[(y) *
t1->stride + x] < 0 ? -
r :
r;
1634 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1636 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1638 for (y0 = 0; y0 <
height; y0 += 4) {
1639 for (x = 0; x <
width; x++) {
1640 int flags_mask = -1;
1641 if (vert_causal_ctx_csty_symbol)
1661 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1662 int flags_mask = -1;
1663 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1676 t1->mqc.cx_states + ctxno) ^
1694 "Segmentation symbol value incorrect\n");
1700 int width,
int height,
int bandpos, uint8_t roi_shift)
1711 memset(
t1->data, 0,
t1->stride *
height *
sizeof(*
t1->data));
1717 memset(
t1->flags, 0,
t1->stride * (
height + 2) *
sizeof(*
t1->flags));
1724 if (bpno < 0 || bpno > 29) {
1731 vert_causal_ctx_csty_symbol);
1740 vert_causal_ctx_csty_symbol);
1785 roi_shift =
comp->roi_shift;
1786 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1788 if (
val > (1 << roi_shift))
1789 return (quan_parameter < 0)?-(
val >> roi_shift):(
val >> roi_shift);
1790 return quan_parameter;
1806 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1807 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
1808 int *
src =
t1->data + j*
t1->stride;
1809 for (
i = 0;
i <
w; ++
i)
1821 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1823 int *
src =
t1->data + j*
t1->stride;
1825 for (
i = 0;
i <
w; ++
i)
1826 datap[
i] =
src[
i] / 2;
1829 for (
i = 0;
i <
w; ++
i)
1841 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1843 int *
src =
t1->data + j*
t1->stride;
1844 for (
i = 0;
i <
w; ++
i)
1854 for (
i = 1;
i < 3;
i++) {
1865 for (
i = 0;
i < 3;
i++)
1871 for (
i = 0;
i < 2;
i++)
1883 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1884 int *
src =
t1->data + j*
t1->stride;
1885 for (
i = 0;
i <
w; ++
i)
1894 int compno, reslevelno, bandno;
1897 for (compno = 0; compno <
s->ncomponents; compno++) {
1911 for (bandno = 0; bandno < rlevel->
nbands; bandno++, subbandno++) {
1912 int nb_precincts, precno;
1914 int cblkno = 0, bandpos;
1918 bandpos = bandno + (reslevelno > 0);
1920 if (band->
coord[0][0] == band->
coord[0][1] ||
1931 for (precno = 0; precno < nb_precincts; precno++) {
1946 magp,
comp->roi_shift);
1951 bandpos,
comp->roi_shift);
1960 if (
comp->roi_shift)
1981 #define WRITE_FRAME(D, PIXEL) \
1982 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1983 AVFrame * picture, int precision) \
1985 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1986 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1987 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1992 for (compno = 0; compno < s->ncomponents; compno++) { \
1993 Jpeg2000Component *comp = tile->comp + compno; \
1994 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1996 float *datap = comp->f_data; \
1997 int32_t *i_datap = comp->i_data; \
1998 int cbps = s->cbps[compno]; \
1999 int w = tile->comp[compno].coord[0][1] - \
2000 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2001 int h = tile->comp[compno].coord[1][1] - \
2002 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2006 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2008 y = tile->comp[compno].coord[1][0] - \
2009 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2010 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2011 for (; y < h; y++) { \
2014 x = tile->comp[compno].coord[0][0] - \
2015 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2016 dst = line + x * pixelsize + compno*!planar; \
2018 if (codsty->transform == FF_DWT97) { \
2019 for (; x < w; x++) { \
2020 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2022 val = av_clip(val, 0, (1 << cbps) - 1); \
2023 *dst = val << (precision - cbps); \
2028 for (; x < w; x++) { \
2029 int val = *i_datap + (1 << (cbps - 1)); \
2031 val = av_clip(val, 0, (1 << cbps) - 1); \
2032 *dst = val << (precision - cbps); \
2037 line += picture->linesize[plane] / sizeof(PIXEL); \
2049 int jobnr,
int threadnr)
2063 if (
s->precision <= 8) {
2064 write_frame_8(
s, tile, picture, 8);
2071 write_frame_16(
s, tile, picture, precision);
2080 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2081 if (
s->tile[tileno].comp) {
2082 for (compno = 0; compno <
s->ncomponents; compno++) {
2089 av_freep(&
s->tile[tileno].packed_headers);
2090 s->tile[tileno].packed_headers_size = 0;
2094 s->packed_headers_size = 0;
2095 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2097 memset(
s->codsty, 0,
sizeof(
s->codsty));
2098 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2099 memset(
s->properties, 0,
sizeof(
s->properties));
2100 memset(&
s->poc , 0,
sizeof(
s->poc));
2101 s->numXtiles =
s->numYtiles = 0;
2110 uint8_t *properties =
s->properties;
2122 marker = bytestream2_get_be16u(&
s->g);
2124 if (marker >= 0xFF30 && marker <= 0xFF3F)
2134 if (
s->curtileno < 0) {
2139 tile =
s->tile +
s->curtileno;
2141 if (tp->
tp_end <
s->g.buffer) {
2147 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2165 len = bytestream2_get_be16(&
s->g);
2177 if (
s->ncomponents) {
2183 s->numXtiles =
s->numYtiles = 0;
2204 if (!
s->in_tile_headers) {
2205 s->in_tile_headers = 1;
2212 codsty =
s->tile[
s->curtileno].codsty;
2213 qntsty =
s->tile[
s->curtileno].qntsty;
2214 poc = &
s->tile[
s->curtileno].poc;
2215 properties =
s->tile[
s->curtileno].properties;
2237 if (
s->in_tile_headers) {
2247 "Cannot have both PPT and PPM marker.\n");
2255 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2262 "error during processing marker segment %.4"PRIx16
"\n",
2276 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2291 uint32_t atom_size, atom, atom_end;
2292 int search_range = 10;
2297 atom_size = bytestream2_get_be32u(&
s->g);
2298 atom = bytestream2_get_be32u(&
s->g);
2299 if (atom_size == 1) {
2300 if (bytestream2_get_be32u(&
s->g)) {
2304 atom_size = bytestream2_get_be32u(&
s->g);
2305 if (atom_size < 16 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 16 > INT_MAX)
2309 if (atom_size < 8 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 8 > INT_MAX)
2322 uint32_t atom2_size, atom2, atom2_end;
2326 atom2_size = bytestream2_get_be32u(&
s->g);
2327 atom2 = bytestream2_get_be32u(&
s->g);
2329 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2334 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2335 int method = bytestream2_get_byteu(&
s->g);
2338 s->colour_space = bytestream2_get_be32u(&
s->g);
2340 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2341 int i,
size, colour_count, colour_channels, colour_depth[3];
2342 colour_count = bytestream2_get_be16u(&
s->g);
2343 colour_channels = bytestream2_get_byteu(&
s->g);
2345 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2346 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2347 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2348 size = (colour_depth[0] + 7 >> 3) * colour_count +
2349 (colour_depth[1] + 7 >> 3) * colour_count +
2350 (colour_depth[2] + 7 >> 3) * colour_count;
2352 colour_channels != 3 ||
2353 colour_depth[0] > 16 ||
2354 colour_depth[1] > 16 ||
2355 colour_depth[2] > 16 ||
2356 atom2_size <
size) {
2362 for (
i = 0;
i < colour_count;
i++) {
2364 if (colour_depth[0] <= 8) {
2365 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2366 r |=
r >> colour_depth[0];
2368 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2370 if (colour_depth[1] <= 8) {
2371 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2372 g |=
g >> colour_depth[1];
2374 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2376 if (colour_depth[2] <= 8) {
2377 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2378 b |=
b >> colour_depth[2];
2380 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2382 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2384 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2385 int n = bytestream2_get_be16u(&
s->g);
2387 int cn = bytestream2_get_be16(&
s->g);
2388 int av_unused typ = bytestream2_get_be16(&
s->g);
2389 int asoc = bytestream2_get_be16(&
s->g);
2390 if (cn < 4 && asoc < 4)
2393 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2394 int64_t vnum, vden, hnum, hden, vexp, hexp;
2397 resx = bytestream2_get_be32u(&
s->g);
2398 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2402 vnum = bytestream2_get_be16u(&
s->g);
2403 vden = bytestream2_get_be16u(&
s->g);
2404 hnum = bytestream2_get_be16u(&
s->g);
2405 hden = bytestream2_get_be16u(&
s->g);
2406 vexp = bytestream2_get_byteu(&
s->g);
2407 hexp = bytestream2_get_byteu(&
s->g);
2408 if (!vnum || !vden || !hnum || !hden) {
2420 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2421 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2423 hnum * vden * pow(10, hexp),
2424 vnum * hden * pow(10, vexp),
2428 }
while (atom_end - atom2_end >= 8);
2445 s->reduction_factor = avctx->
lowres;
2465 memset(
s->cdef, -1,
sizeof(
s->cdef));
2474 (bytestream2_get_be32u(&
s->g) == 12) &&
2479 "Could not find Jpeg2000 codestream atom.\n");
2498 if (
s->sar.num &&
s->sar.den)
2500 s->sar.num =
s->sar.den = 0;
2516 for (
int x = 0; x <
s->ncomponents; x++) {
2517 if (
s->cdef[x] < 0) {
2518 for (x = 0; x <
s->ncomponents; x++) {
2521 if ((
s->ncomponents & 1) == 0)
2522 s->cdef[
s->ncomponents-1] = 0;
2534 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2543 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2544 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2547 {
"lowres",
"Lower the decoding resolution by a power of two",
2560 .
p.
name =
"jpeg2000",