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));
492 const uint8_t *properties)
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;
642 const uint8_t *properties)
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);
839 bytestream2_get_be16(&
s->g);
841 bytestream2_get_be32(&
s->g);
858 bytestream2_get_byte(&
s->g);
860 for (
i = 0;
i < n - 3;
i++) {
861 v = bytestream2_get_byte(&
s->g);
877 bytestream2_get_byte(&
s->g);
879 s->packed_headers_size + n - 3);
881 s->packed_headers =
new;
885 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
888 s->packed_headers_size += n - 3;
902 if (
s->curtileno < 0)
905 tile = &
s->tile[
s->curtileno];
908 "PPT marker can occur only on first tile part of a tile.\n");
913 bytestream2_get_byte(&
s->g);
930 int tilex = tileno %
s->numXtiles;
931 int tiley = tileno /
s->numXtiles;
938 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][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,
const 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 ||
1420 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
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 ||
1498 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
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;
1913 bandpos = bandno + (reslevelno > 0);
1915 if (band->
coord[0][0] == band->
coord[0][1] ||
1926 for (precno = 0; precno < nb_precincts; precno++) {
1941 magp,
comp->roi_shift);
1946 bandpos,
comp->roi_shift);
1955 if (
comp->roi_shift)
1976 #define WRITE_FRAME(D, PIXEL) \
1977 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1978 AVFrame * picture, int precision) \
1980 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1981 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1982 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1987 for (compno = 0; compno < s->ncomponents; compno++) { \
1988 Jpeg2000Component *comp = tile->comp + compno; \
1989 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1991 float *datap = comp->f_data; \
1992 int32_t *i_datap = comp->i_data; \
1993 int cbps = s->cbps[compno]; \
1994 int w = tile->comp[compno].coord[0][1] - \
1995 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
1996 int h = tile->comp[compno].coord[1][1] - \
1997 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2001 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2003 y = tile->comp[compno].coord[1][0] - \
2004 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2005 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2006 for (; y < h; y++) { \
2009 x = tile->comp[compno].coord[0][0] - \
2010 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2011 dst = line + x * pixelsize + compno*!planar; \
2013 if (codsty->transform == FF_DWT97) { \
2014 for (; x < w; x++) { \
2015 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2017 val = av_clip(val, 0, (1 << cbps) - 1); \
2018 *dst = val << (precision - cbps); \
2023 for (; x < w; x++) { \
2024 int val = *i_datap + (1 << (cbps - 1)); \
2026 val = av_clip(val, 0, (1 << cbps) - 1); \
2027 *dst = val << (precision - cbps); \
2032 line += picture->linesize[plane] / sizeof(PIXEL); \
2044 int jobnr,
int threadnr)
2058 if (
s->precision <= 8) {
2059 write_frame_8(
s, tile, picture, 8);
2066 write_frame_16(
s, tile, picture, precision);
2075 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2076 if (
s->tile[tileno].comp) {
2077 for (compno = 0; compno <
s->ncomponents; compno++) {
2084 av_freep(&
s->tile[tileno].packed_headers);
2085 s->tile[tileno].packed_headers_size = 0;
2089 s->packed_headers_size = 0;
2090 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2092 memset(
s->codsty, 0,
sizeof(
s->codsty));
2093 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2094 memset(
s->properties, 0,
sizeof(
s->properties));
2095 memset(&
s->poc , 0,
sizeof(
s->poc));
2096 s->numXtiles =
s->numYtiles = 0;
2105 uint8_t *properties =
s->properties;
2117 marker = bytestream2_get_be16u(&
s->g);
2119 if (marker >= 0xFF30 && marker <= 0xFF3F)
2129 if (
s->curtileno < 0) {
2134 tile =
s->tile +
s->curtileno;
2136 if (tp->
tp_end <
s->g.buffer) {
2142 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2160 len = bytestream2_get_be16(&
s->g);
2172 if (
s->ncomponents) {
2178 s->numXtiles =
s->numYtiles = 0;
2199 if (!
s->in_tile_headers) {
2200 s->in_tile_headers = 1;
2207 codsty =
s->tile[
s->curtileno].codsty;
2208 qntsty =
s->tile[
s->curtileno].qntsty;
2209 poc = &
s->tile[
s->curtileno].poc;
2210 properties =
s->tile[
s->curtileno].properties;
2232 if (
s->in_tile_headers) {
2242 "Cannot have both PPT and PPM marker.\n");
2250 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2257 "error during processing marker segment %.4"PRIx16
"\n",
2271 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2286 uint32_t atom_size, atom, atom_end;
2287 int search_range = 10;
2292 atom_size = bytestream2_get_be32u(&
s->g);
2293 atom = bytestream2_get_be32u(&
s->g);
2294 if (atom_size == 1) {
2295 if (bytestream2_get_be32u(&
s->g)) {
2299 atom_size = bytestream2_get_be32u(&
s->g);
2317 uint32_t atom2_size, atom2, atom2_end;
2321 atom2_size = bytestream2_get_be32u(&
s->g);
2322 atom2 = bytestream2_get_be32u(&
s->g);
2324 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2329 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2330 int method = bytestream2_get_byteu(&
s->g);
2333 s->colour_space = bytestream2_get_be32u(&
s->g);
2335 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2336 int i,
size, colour_count, colour_channels, colour_depth[3];
2337 colour_count = bytestream2_get_be16u(&
s->g);
2338 colour_channels = bytestream2_get_byteu(&
s->g);
2340 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2341 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2342 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2343 size = (colour_depth[0] + 7 >> 3) * colour_count +
2344 (colour_depth[1] + 7 >> 3) * colour_count +
2345 (colour_depth[2] + 7 >> 3) * colour_count;
2347 colour_channels != 3 ||
2348 colour_depth[0] > 16 ||
2349 colour_depth[1] > 16 ||
2350 colour_depth[2] > 16 ||
2351 atom2_size <
size) {
2357 for (
i = 0;
i < colour_count;
i++) {
2359 if (colour_depth[0] <= 8) {
2360 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2361 r |=
r >> colour_depth[0];
2363 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2365 if (colour_depth[1] <= 8) {
2366 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2367 g |=
g >> colour_depth[1];
2369 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2371 if (colour_depth[2] <= 8) {
2372 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2373 b |=
b >> colour_depth[2];
2375 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2377 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2379 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2380 int n = bytestream2_get_be16u(&
s->g);
2382 int cn = bytestream2_get_be16(&
s->g);
2383 int av_unused typ = bytestream2_get_be16(&
s->g);
2384 int asoc = bytestream2_get_be16(&
s->g);
2385 if (cn < 4 && asoc < 4)
2388 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2389 int64_t vnum, vden, hnum, hden, vexp, hexp;
2392 resx = bytestream2_get_be32u(&
s->g);
2393 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2397 vnum = bytestream2_get_be16u(&
s->g);
2398 vden = bytestream2_get_be16u(&
s->g);
2399 hnum = bytestream2_get_be16u(&
s->g);
2400 hden = bytestream2_get_be16u(&
s->g);
2401 vexp = bytestream2_get_byteu(&
s->g);
2402 hexp = bytestream2_get_byteu(&
s->g);
2403 if (!vnum || !vden || !hnum || !hden) {
2415 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2416 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2418 hnum * vden * pow(10, hexp),
2419 vnum * hden * pow(10, vexp),
2423 }
while (atom_end - atom2_end >= 8);
2440 s->reduction_factor = avctx->
lowres;
2460 memset(
s->cdef, -1,
sizeof(
s->cdef));
2469 (bytestream2_get_be32u(&
s->g) == 12) &&
2474 "Could not find Jpeg2000 codestream atom.\n");
2493 if (
s->sar.num &&
s->sar.den)
2495 s->sar.num =
s->sar.den = 0;
2511 for (
int x = 0; x <
s->ncomponents; x++) {
2512 if (
s->cdef[x] < 0) {
2513 for (x = 0; x <
s->ncomponents; x++) {
2516 if ((
s->ncomponents & 1) == 0)
2517 s->cdef[
s->ncomponents-1] = 0;
2529 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2538 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2539 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2542 {
"lowres",
"Lower the decoding resolution by a power of two",
2555 .
p.
name =
"jpeg2000",