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->reduction_factor && (
s->image_offset_x ||
s->image_offset_y) ){
242 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction factor with image offsets is not fully implemented");
246 s->ncomponents = ncomponents;
248 if (
s->tile_width <= 0 ||
s->tile_height <= 0) {
250 s->tile_width,
s->tile_height);
255 av_log(
s->avctx,
AV_LOG_ERROR,
"Insufficient space for %d components in SIZ\n",
s->ncomponents);
259 for (
i = 0;
i <
s->ncomponents;
i++) {
260 uint8_t x = bytestream2_get_byteu(&
s->g);
261 s->cbps[
i] = (x & 0x7f) + 1;
262 s->precision =
FFMAX(
s->cbps[
i],
s->precision);
263 s->sgnd[
i] = !!(x & 0x80);
264 s->cdx[
i] = bytestream2_get_byteu(&
s->g);
265 s->cdy[
i] = bytestream2_get_byteu(&
s->g);
266 if ( !
s->cdx[
i] ||
s->cdx[
i] == 3 ||
s->cdx[
i] > 4
267 || !
s->cdy[
i] ||
s->cdy[
i] == 3 ||
s->cdy[
i] > 4) {
271 log2_chroma_wh |=
s->cdy[
i] >> 1 <<
i * 4 |
s->cdx[
i] >> 1 <<
i * 4 + 2;
278 if (
s->numXtiles * (uint64_t)
s->numYtiles > INT_MAX/
sizeof(*
s->tile) ||
281 s->numXtiles =
s->numYtiles = 0;
285 s->tile =
av_calloc(
s->numXtiles *
s->numYtiles,
sizeof(*
s->tile));
287 s->numXtiles =
s->numYtiles = 0;
291 for (
i = 0;
i <
s->numXtiles *
s->numYtiles;
i++) {
301 s->reduction_factor);
303 s->reduction_factor);
306 for (
i = 1;
i <
s->ncomponents;
i++) {
320 switch (
s->colour_space) {
340 && !
pix_fmt_match(
s->avctx->pix_fmt, ncomponents,
s->precision, log2_chroma_wh,
s->pal8))
343 for (
i = 0;
i < possible_fmts_nb; ++
i) {
344 if (
pix_fmt_match(possible_fmts[
i], ncomponents,
s->precision, log2_chroma_wh,
s->pal8)) {
345 s->avctx->pix_fmt = possible_fmts[
i];
350 if (
i == possible_fmts_nb) {
351 if (ncomponents == 4 &&
352 s->cdy[0] == 1 &&
s->cdx[0] == 1 &&
353 s->cdy[1] == 1 &&
s->cdx[1] == 1 &&
354 s->cdy[2] ==
s->cdy[3] &&
s->cdx[2] ==
s->cdx[3]) {
355 if (
s->precision == 8 &&
s->cdy[2] == 2 &&
s->cdx[2] == 2 && !
s->pal8) {
363 }
else if (ncomponents == 3 &&
s->precision == 8 &&
364 s->cdx[0] ==
s->cdx[1] &&
s->cdx[0] ==
s->cdx[2] &&
365 s->cdy[0] ==
s->cdy[1] &&
s->cdy[0] ==
s->cdy[2]) {
368 }
else if (ncomponents == 2 &&
s->precision == 8 &&
369 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
372 }
else if (ncomponents == 2 &&
s->precision == 16 &&
373 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
376 }
else if (ncomponents == 1 &&
s->precision == 8) {
383 if (
i == possible_fmts_nb) {
385 "Unknown pix_fmt, profile: %d, colour_space: %d, "
386 "components: %d, precision: %d\n"
387 "cdx[0]: %d, cdy[0]: %d\n"
388 "cdx[1]: %d, cdy[1]: %d\n"
389 "cdx[2]: %d, cdy[2]: %d\n"
390 "cdx[3]: %d, cdy[3]: %d\n",
391 s->avctx->profile,
s->colour_space, ncomponents,
s->precision,
394 ncomponents > 1 ?
s->cdx[1] : 0,
395 ncomponents > 1 ?
s->cdy[1] : 0,
396 ncomponents > 2 ?
s->cdx[2] : 0,
397 ncomponents > 2 ?
s->cdy[2] : 0,
398 ncomponents > 3 ?
s->cdx[3] : 0,
399 ncomponents > 3 ?
s->cdy[3] : 0);
402 s->avctx->bits_per_raw_sample =
s->precision;
418 c->nreslevels = bytestream2_get_byteu(&
s->g) + 1;
424 if (
c->nreslevels <=
s->reduction_factor) {
429 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction_factor too large for this bitstream, max is %d\n",
c->nreslevels - 1);
430 s->reduction_factor =
c->nreslevels - 1;
435 c->nreslevels2decode =
c->nreslevels -
s->reduction_factor;
437 c->log2_cblk_width = (bytestream2_get_byteu(&
s->g) & 15) + 2;
438 c->log2_cblk_height = (bytestream2_get_byteu(&
s->g) & 15) + 2;
440 if (
c->log2_cblk_width > 10 ||
c->log2_cblk_height > 10 ||
441 c->log2_cblk_width +
c->log2_cblk_height > 12) {
446 c->cblk_style = bytestream2_get_byteu(&
s->g);
447 if (
c->cblk_style != 0) {
456 c->transform = bytestream2_get_byteu(&
s->g);
466 for (
i = 0;
i <
c->nreslevels;
i++) {
467 byte = bytestream2_get_byte(&
s->g);
468 c->log2_prec_widths[
i] =
byte & 0x0F;
469 c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;
471 if (
c->log2_prec_widths[
i] == 0 ||
c->log2_prec_heights[
i] == 0) {
473 c->log2_prec_widths[
i],
c->log2_prec_heights[
i]);
474 c->log2_prec_widths[
i] =
c->log2_prec_heights[
i] = 1;
479 memset(
c->log2_prec_widths , 15,
sizeof(
c->log2_prec_widths ));
480 memset(
c->log2_prec_heights, 15,
sizeof(
c->log2_prec_heights));
497 tmp.csty = bytestream2_get_byteu(&
s->g);
500 tmp.prog_order = bytestream2_get_byteu(&
s->g);
502 tmp.nlayers = bytestream2_get_be16u(&
s->g);
503 tmp.mct = bytestream2_get_byteu(&
s->g);
505 if (
tmp.mct &&
s->ncomponents < 3) {
507 "MCT %"PRIu8
" with too few components (%d)\n",
508 tmp.mct,
s->ncomponents);
515 for (compno = 0; compno <
s->ncomponents; compno++)
516 if (!(properties[compno] &
HAD_COC))
517 memcpy(
c + compno, &
tmp,
sizeof(
tmp));
527 uint8_t has_eph, has_sop;
534 compno = bytestream2_get_byteu(&
s->g);
536 if (compno >=
s->ncomponents) {
538 "Invalid compno %d. There are %d components in the image.\n",
539 compno,
s->ncomponents);
546 c->csty = bytestream2_get_byteu(&
s->g);
561 compno = (
s->ncomponents < 257)? bytestream2_get_byte(&
s->g):
562 bytestream2_get_be16u(&
s->g);
563 if (bytestream2_get_byte(&
s->g)) {
570 if (compno < s->ncomponents) {
572 if (
s->curtileno == -1) {
573 v = bytestream2_get_byte(&
s->g);
576 s->roi_shift[compno] = v;
578 if (
s->tile[
s->curtileno].tp_idx != 0)
580 v = bytestream2_get_byte(&
s->g);
583 s->tile[
s->curtileno].comp[compno].roi_shift = v;
598 x = bytestream2_get_byteu(&
s->g);
608 for (
i = 0;
i < n;
i++)
609 q->
expn[
i] = bytestream2_get_byteu(&
s->g) >> 3;
613 x = bytestream2_get_be16u(&
s->g);
614 q->
expn[0] = x >> 11;
615 q->
mant[0] = x & 0x7ff;
617 int curexpn =
FFMAX(0, q->
expn[0] - (
i - 1) / 3);
618 q->
expn[
i] = curexpn;
626 for (
i = 0;
i < n;
i++) {
627 x = bytestream2_get_be16u(&
s->g);
628 q->
expn[
i] = x >> 11;
629 q->
mant[
i] = x & 0x7ff;
642 memset(&
tmp, 0,
sizeof(
tmp));
646 for (compno = 0; compno <
s->ncomponents; compno++)
647 if (!(properties[compno] &
HAD_QCC))
648 memcpy(q + compno, &
tmp,
sizeof(
tmp));
662 compno = bytestream2_get_byteu(&
s->g);
664 if (compno >=
s->ncomponents) {
666 "Invalid compno %d. There are %d components in the image.\n",
667 compno,
s->ncomponents);
672 return get_qcx(
s, n - 1, q + compno);
678 int elem_size =
s->ncomponents <= 257 ? 7 : 9;
691 tmp.nb_poc = (
size - 2) / elem_size;
697 for (
i = 0;
i<
tmp.nb_poc;
i++) {
699 e->
RSpoc = bytestream2_get_byteu(&
s->g);
700 e->
CSpoc = bytestream2_get_byteu(&
s->g);
701 e->
LYEpoc = bytestream2_get_be16u(&
s->g);
702 e->
REpoc = bytestream2_get_byteu(&
s->g);
703 e->
CEpoc = bytestream2_get_byteu(&
s->g);
704 e->
Ppoc = bytestream2_get_byteu(&
s->g);
707 if (e->
CEpoc >
s->ncomponents)
708 e->
CEpoc =
s->ncomponents;
748 Isot = bytestream2_get_be16u(&
s->g);
749 if (Isot >=
s->numXtiles *
s->numYtiles)
753 Psot = bytestream2_get_be32u(&
s->g);
754 TPsot = bytestream2_get_byteu(&
s->g);
757 bytestream2_get_byteu(&
s->g);
772 s->tile[Isot].tp_idx = TPsot;
773 tp =
s->tile[Isot].tile_part + TPsot;
775 tp->
tp_end =
s->g.buffer + Psot - n - 2;
783 memcpy(&tile->
poc , &
s->poc ,
sizeof(tile->
poc));
792 if (
s->ncomponents*4 != n - 2) {
809 uint8_t Stlm, ST,
SP, tile_tlm,
i;
810 bytestream2_get_byte(&
s->g);
811 Stlm = bytestream2_get_byte(&
s->g);
814 ST = (Stlm >> 4) & 0x03;
820 SP = (Stlm >> 6) & 0x01;
821 tile_tlm = (n - 4) / ((
SP + 1) * 2 + ST);
822 for (
i = 0;
i < tile_tlm;
i++) {
827 bytestream2_get_byte(&
s->g);
830 bytestream2_get_be16(&
s->g);
833 bytestream2_get_be32(&
s->g);
837 bytestream2_get_be16(&
s->g);
839 bytestream2_get_be32(&
s->g);
856 bytestream2_get_byte(&
s->g);
858 for (
i = 0;
i < n - 3;
i++) {
859 v = bytestream2_get_byte(&
s->g);
875 bytestream2_get_byte(&
s->g);
877 s->packed_headers_size + n - 3);
879 s->packed_headers =
new;
883 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
886 s->packed_headers_size += n - 3;
900 if (
s->curtileno < 0)
903 tile = &
s->tile[
s->curtileno];
906 "PPT marker can occur only on first tile part of a tile.\n");
911 bytestream2_get_byte(&
s->g);
930 int tilex = tileno %
s->numXtiles;
931 int tiley = tileno /
s->numXtiles;
937 tile->
coord[0][0] =
av_clip(tilex * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
938 tile->
coord[0][1] =
av_clip((tilex + 1) * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
939 tile->
coord[1][0] =
av_clip(tiley * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
940 tile->
coord[1][1] =
av_clip((tiley + 1) * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
942 for (compno = 0; compno <
s->ncomponents; compno++) {
963 if (!
comp->roi_shift)
964 comp->roi_shift =
s->roi_shift[compno];
968 s->cbps[compno],
s->cdx[compno],
969 s->cdy[compno],
s->avctx))
984 return num < 0 ? num : 3 + num;
986 return num < 0 ? num : 6 + num;
988 return num < 0 ? num : 37 + num;
1026 av_log(
s->avctx,
AV_LOG_ERROR,
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1033 int layno, uint8_t *expn,
int numgbits)
1035 int bandno, cblkno,
ret, nb_code_blocks;
1038 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1055 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1059 if (band->
coord[0][0] == band->
coord[0][1] ||
1064 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1066 int incl, newpasses, llen;
1080 int v = expn[bandno] + numgbits - 1 - zbp;
1082 if (v < 0 || v > 30) {
1084 "nonzerobits %d invalid or unsupported\n", v);
1101 "Block with length beyond 16 bits");
1120 while (newpasses1 < newpasses) {
1130 int href_passes = (cblk->
npasses + newpasses - 1) % 3;
1131 int eb =
av_log2(newpasses - href_passes);
1132 int extra_bit = newpasses > 2 ? 1 : 0;
1161 newpasses -= newpasses1;
1171 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1182 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1187 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1191 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1204 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1232 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1246 int RSpoc,
int CSpoc,
1247 int LYEpoc,
int REpoc,
int CEpoc,
1248 int Ppoc,
int *tp_index)
1251 int layno, reslevelno, compno, precno, ok_reslevel;
1259 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1261 for (layno = 0; layno < LYEpoc; layno++) {
1262 for (compno = CSpoc; compno < CEpoc; compno++) {
1265 if (reslevelno < codsty->nreslevels) {
1273 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1284 for (layno = 0; layno < LYEpoc; layno++) {
1286 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1288 for (compno = CSpoc; compno < CEpoc; compno++) {
1291 if (reslevelno < codsty->nreslevels) {
1299 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1310 for (compno = CSpoc; compno < CEpoc; compno++) {
1320 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1321 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1326 if (step_x >= 31 || step_y >= 31){
1333 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1334 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1335 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1336 unsigned prcx, prcy;
1337 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1339 int xc = x /
s->cdx[compno];
1340 int yc = y /
s->cdy[compno];
1362 for (layno = 0; layno < LYEpoc; layno++) {
1365 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1378 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1382 for (compno = CSpoc; compno < CEpoc; compno++) {
1386 if (reslevelno < codsty->nreslevels) {
1387 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1396 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1397 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1398 for (compno = CSpoc; compno < CEpoc; compno++) {
1402 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1404 unsigned prcx, prcy;
1407 if (!
s->cdx[compno] || !
s->cdy[compno])
1416 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1417 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1420 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1421 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1439 for (layno = 0; layno < LYEpoc; layno++) {
1443 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1457 for (compno = CSpoc; compno < CEpoc; compno++) {
1461 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1462 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1468 if (step_x >= 31 || step_y >= 31){
1475 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1476 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1477 for (compno = CSpoc; compno < CEpoc; compno++) {
1482 if (!
s->cdx[compno] || !
s->cdy[compno])
1485 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1486 unsigned prcx, prcy;
1487 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1494 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1495 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1498 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1499 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1516 for (layno = 0; layno < LYEpoc; layno++) {
1519 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1574 int bpno,
int bandno,
1575 int vert_causal_ctx_csty_symbol)
1577 int mask = 3 << (bpno - 1), y0, x, y;
1579 for (y0 = 0; y0 <
height; y0 += 4)
1580 for (x = 0; x <
width; x++)
1581 for (y = y0; y <
height && y < y0 + 4; y++) {
1582 int flags_mask = -1;
1583 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1596 t1->data[(y) *
t1->stride + x] < 0);
1604 int bpno,
int vert_causal_ctx_csty_symbol)
1609 phalf = 1 << (bpno - 1);
1612 for (y0 = 0; y0 <
height; y0 += 4)
1613 for (x = 0; x <
width; x++)
1614 for (y = y0; y <
height && y < y0 + 4; y++)
1616 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1620 t1->mqc.cx_states + ctxno)
1622 t1->data[(y) *
t1->stride + x] +=
t1->data[(y) *
t1->stride + x] < 0 ? -
r :
r;
1629 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1631 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1633 for (y0 = 0; y0 <
height; y0 += 4) {
1634 for (x = 0; x <
width; x++) {
1635 int flags_mask = -1;
1636 if (vert_causal_ctx_csty_symbol)
1656 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1657 int flags_mask = -1;
1658 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1671 t1->mqc.cx_states + ctxno) ^
1689 "Segmentation symbol value incorrect\n");
1695 int width,
int height,
int bandpos, uint8_t roi_shift)
1706 memset(
t1->data, 0,
t1->stride *
height *
sizeof(*
t1->data));
1712 memset(
t1->flags, 0,
t1->stride * (
height + 2) *
sizeof(*
t1->flags));
1719 if (bpno < 0 || bpno > 29) {
1726 vert_causal_ctx_csty_symbol);
1735 vert_causal_ctx_csty_symbol);
1780 roi_shift =
comp->roi_shift;
1781 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1783 if (
val > (1 << roi_shift))
1784 return (quan_parameter < 0)?-(
val >> roi_shift):(
val >> roi_shift);
1785 return quan_parameter;
1801 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1802 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
1803 int *
src =
t1->data + j*
t1->stride;
1804 for (
i = 0;
i <
w; ++
i)
1816 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1818 int *
src =
t1->data + j*
t1->stride;
1820 for (
i = 0;
i <
w; ++
i)
1821 datap[
i] =
src[
i] / 2;
1824 for (
i = 0;
i <
w; ++
i)
1836 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1838 int *
src =
t1->data + j*
t1->stride;
1839 for (
i = 0;
i <
w; ++
i)
1849 for (
i = 1;
i < 3;
i++) {
1860 for (
i = 0;
i < 3;
i++)
1866 for (
i = 0;
i < 2;
i++)
1878 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1879 int *
src =
t1->data + j*
t1->stride;
1880 for (
i = 0;
i <
w; ++
i)
1889 int compno, reslevelno, bandno;
1892 for (compno = 0; compno <
s->ncomponents; compno++) {
1906 for (bandno = 0; bandno < rlevel->
nbands; bandno++, subbandno++) {
1907 int nb_precincts, precno;
1909 int cblkno = 0, bandpos;
1911 bandpos = bandno + (reslevelno > 0);
1913 if (band->
coord[0][0] == band->
coord[0][1] ||
1919 for (precno = 0; precno < nb_precincts; precno++) {
1936 magp,
comp->roi_shift);
1941 bandpos,
comp->roi_shift);
1950 if (
comp->roi_shift)
1970 #define WRITE_FRAME(D, PIXEL) \
1971 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1972 AVFrame * picture, int precision) \
1974 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1975 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1976 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1981 for (compno = 0; compno < s->ncomponents; compno++) { \
1982 Jpeg2000Component *comp = tile->comp + compno; \
1983 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1985 float *datap = comp->f_data; \
1986 int32_t *i_datap = comp->i_data; \
1987 int cbps = s->cbps[compno]; \
1988 int w = tile->comp[compno].coord[0][1] - \
1989 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
1990 int h = tile->comp[compno].coord[1][1] - \
1991 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
1995 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
1997 y = tile->comp[compno].coord[1][0] - \
1998 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
1999 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2000 for (; y < h; y++) { \
2003 x = tile->comp[compno].coord[0][0] - \
2004 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2005 dst = line + x * pixelsize + compno*!planar; \
2007 if (codsty->transform == FF_DWT97) { \
2008 for (; x < w; x++) { \
2009 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2011 val = av_clip(val, 0, (1 << cbps) - 1); \
2012 *dst = val << (precision - cbps); \
2017 for (; x < w; x++) { \
2018 int val = *i_datap + (1 << (cbps - 1)); \
2020 val = av_clip(val, 0, (1 << cbps) - 1); \
2021 *dst = val << (precision - cbps); \
2026 line += picture->linesize[plane] / sizeof(PIXEL); \
2038 int jobnr,
int threadnr)
2050 if (
s->precision <= 8) {
2051 write_frame_8(
s, tile, picture, 8);
2058 write_frame_16(
s, tile, picture, precision);
2067 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2068 if (
s->tile[tileno].comp) {
2069 for (compno = 0; compno <
s->ncomponents; compno++) {
2076 av_freep(&
s->tile[tileno].packed_headers);
2077 s->tile[tileno].packed_headers_size = 0;
2081 s->packed_headers_size = 0;
2082 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2084 memset(
s->codsty, 0,
sizeof(
s->codsty));
2085 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2086 memset(
s->properties, 0,
sizeof(
s->properties));
2087 memset(&
s->poc , 0,
sizeof(
s->poc));
2088 s->numXtiles =
s->numYtiles = 0;
2097 uint8_t *properties =
s->properties;
2109 marker = bytestream2_get_be16u(&
s->g);
2111 if (marker >= 0xFF30 && marker <= 0xFF3F)
2121 if (
s->curtileno < 0) {
2126 tile =
s->tile +
s->curtileno;
2128 if (tp->
tp_end <
s->g.buffer) {
2134 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2152 len = bytestream2_get_be16(&
s->g);
2164 if (
s->ncomponents) {
2170 s->numXtiles =
s->numYtiles = 0;
2191 if (!
s->in_tile_headers) {
2192 s->in_tile_headers = 1;
2199 codsty =
s->tile[
s->curtileno].codsty;
2200 qntsty =
s->tile[
s->curtileno].qntsty;
2201 poc = &
s->tile[
s->curtileno].poc;
2202 properties =
s->tile[
s->curtileno].properties;
2224 if (
s->in_tile_headers) {
2234 "Cannot have both PPT and PPM marker.\n");
2242 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2249 "error during processing marker segment %.4"PRIx16
"\n",
2263 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2278 uint32_t atom_size, atom, atom_end;
2279 int search_range = 10;
2284 atom_size = bytestream2_get_be32u(&
s->g);
2285 atom = bytestream2_get_be32u(&
s->g);
2286 if (atom_size == 1) {
2287 if (bytestream2_get_be32u(&
s->g)) {
2291 atom_size = bytestream2_get_be32u(&
s->g);
2292 if (atom_size < 16 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 16 > INT_MAX)
2296 if (atom_size < 8 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 8 > INT_MAX)
2309 uint32_t atom2_size, atom2, atom2_end;
2313 atom2_size = bytestream2_get_be32u(&
s->g);
2314 atom2 = bytestream2_get_be32u(&
s->g);
2316 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2321 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2322 int method = bytestream2_get_byteu(&
s->g);
2325 s->colour_space = bytestream2_get_be32u(&
s->g);
2327 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2328 int i,
size, colour_count, colour_channels, colour_depth[3];
2329 colour_count = bytestream2_get_be16u(&
s->g);
2330 colour_channels = bytestream2_get_byteu(&
s->g);
2332 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2333 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2334 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2335 size = (colour_depth[0] + 7 >> 3) * colour_count +
2336 (colour_depth[1] + 7 >> 3) * colour_count +
2337 (colour_depth[2] + 7 >> 3) * colour_count;
2339 colour_channels != 3 ||
2340 colour_depth[0] > 16 ||
2341 colour_depth[1] > 16 ||
2342 colour_depth[2] > 16 ||
2343 atom2_size <
size) {
2349 for (
i = 0;
i < colour_count;
i++) {
2351 if (colour_depth[0] <= 8) {
2352 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2353 r |=
r >> colour_depth[0];
2355 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2357 if (colour_depth[1] <= 8) {
2358 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2359 g |=
g >> colour_depth[1];
2361 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2363 if (colour_depth[2] <= 8) {
2364 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2365 b |=
b >> colour_depth[2];
2367 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2369 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2371 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2372 int n = bytestream2_get_be16u(&
s->g);
2374 int cn = bytestream2_get_be16(&
s->g);
2375 int av_unused typ = bytestream2_get_be16(&
s->g);
2376 int asoc = bytestream2_get_be16(&
s->g);
2377 if (cn < 4 && asoc < 4)
2380 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2381 int64_t vnum, vden, hnum, hden, vexp, hexp;
2384 resx = bytestream2_get_be32u(&
s->g);
2385 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2389 vnum = bytestream2_get_be16u(&
s->g);
2390 vden = bytestream2_get_be16u(&
s->g);
2391 hnum = bytestream2_get_be16u(&
s->g);
2392 hden = bytestream2_get_be16u(&
s->g);
2393 vexp = bytestream2_get_byteu(&
s->g);
2394 hexp = bytestream2_get_byteu(&
s->g);
2395 if (!vnum || !vden || !hnum || !hden) {
2407 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2408 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2410 hnum * vden * pow(10, hexp),
2411 vnum * hden * pow(10, vexp),
2415 }
while (atom_end - atom2_end >= 8);
2432 s->reduction_factor = avctx->
lowres;
2452 memset(
s->cdef, -1,
sizeof(
s->cdef));
2461 (bytestream2_get_be32u(&
s->g) == 12) &&
2466 "Could not find Jpeg2000 codestream atom.\n");
2485 if (
s->sar.num &&
s->sar.den)
2487 s->sar.num =
s->sar.den = 0;
2503 for (
int x = 0; x <
s->ncomponents; x++) {
2504 if (
s->cdef[x] < 0) {
2505 for (x = 0; x <
s->ncomponents; x++) {
2508 if ((
s->ncomponents & 1) == 0)
2509 s->cdef[
s->ncomponents-1] = 0;
2521 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2530 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2531 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2534 {
"lowres",
"Lower the decoding resolution by a power of two",
2547 .
p.
name =
"jpeg2000",