46 #define JP2_SIG_TYPE 0x6A502020
47 #define JP2_SIG_VALUE 0x0D0A870A
48 #define JP2_CODESTREAM 0x6A703263
49 #define JP2_HEADER 0x6A703268
138 if (
s->bit_index == 0) {
139 s->bit_index = 7 + (bytestream2_get_byte(&
s->g) != 0xFF
u);
142 res |= (bytestream2_peek_byte(&
s->g) >>
s->bit_index) & 1;
149 if (bytestream2_get_byte(&
s->g) == 0xff)
159 int sp = -1, curval = 0;
166 while (node && !node->
vis) {
174 curval = stack[
sp]->
val;
176 while (curval < threshold && sp >= 0) {
177 if (curval < stack[
sp]->
val)
178 curval = stack[
sp]->
val;
179 while (curval < threshold) {
189 stack[
sp]->
val = curval;
196 int bpc, uint32_t log2_chroma_wh,
int pal8)
203 if (
desc->nb_components != components) {
207 switch (components) {
209 match = match &&
desc->comp[3].depth >= bpc &&
210 (log2_chroma_wh >> 14 & 3) == 0 &&
211 (log2_chroma_wh >> 12 & 3) == 0;
213 match = match &&
desc->comp[2].depth >= bpc &&
214 (log2_chroma_wh >> 10 & 3) ==
desc->log2_chroma_w &&
215 (log2_chroma_wh >> 8 & 3) ==
desc->log2_chroma_h;
217 match = match &&
desc->comp[1].depth >= bpc &&
218 (log2_chroma_wh >> 6 & 3) ==
desc->log2_chroma_w &&
219 (log2_chroma_wh >> 4 & 3) ==
desc->log2_chroma_h;
222 match = match &&
desc->comp[0].depth >= bpc &&
223 (log2_chroma_wh >> 2 & 3) == 0 &&
224 (log2_chroma_wh & 3) == 0 &&
232 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
233 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
234 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
235 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
236 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
237 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
238 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
239 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
240 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
241 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
242 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
243 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
244 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
245 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
263 uint32_t log2_chroma_wh = 0;
265 int possible_fmts_nb = 0;
273 s->avctx->profile = bytestream2_get_be16u(&
s->g);
274 s->width = bytestream2_get_be32u(&
s->g);
275 s->height = bytestream2_get_be32u(&
s->g);
276 s->image_offset_x = bytestream2_get_be32u(&
s->g);
277 s->image_offset_y = bytestream2_get_be32u(&
s->g);
278 s->tile_width = bytestream2_get_be32u(&
s->g);
279 s->tile_height = bytestream2_get_be32u(&
s->g);
280 s->tile_offset_x = bytestream2_get_be32u(&
s->g);
281 s->tile_offset_y = bytestream2_get_be32u(&
s->g);
282 ncomponents = bytestream2_get_be16u(&
s->g);
284 if (
s->image_offset_x ||
s->image_offset_y) {
293 if (ncomponents <= 0) {
299 if (ncomponents > 4) {
305 if (
s->tile_offset_x < 0 ||
s->tile_offset_y < 0 ||
306 s->image_offset_x <
s->tile_offset_x ||
307 s->image_offset_y <
s->tile_offset_y ||
308 s->tile_width + (int64_t)
s->tile_offset_x <=
s->image_offset_x ||
309 s->tile_height + (int64_t)
s->tile_offset_y <=
s->image_offset_y
315 s->ncomponents = ncomponents;
317 if (
s->tile_width <= 0 ||
s->tile_height <= 0) {
319 s->tile_width,
s->tile_height);
324 av_log(
s->avctx,
AV_LOG_ERROR,
"Insufficient space for %d components in SIZ\n",
s->ncomponents);
328 for (
i = 0;
i <
s->ncomponents;
i++) {
329 uint8_t x = bytestream2_get_byteu(&
s->g);
330 s->cbps[
i] = (x & 0x7f) + 1;
331 s->precision =
FFMAX(
s->cbps[
i],
s->precision);
332 s->sgnd[
i] = !!(x & 0x80);
333 s->cdx[
i] = bytestream2_get_byteu(&
s->g);
334 s->cdy[
i] = bytestream2_get_byteu(&
s->g);
335 if ( !
s->cdx[
i] ||
s->cdx[
i] == 3 ||
s->cdx[
i] > 4
336 || !
s->cdy[
i] ||
s->cdy[
i] == 3 ||
s->cdy[
i] > 4) {
340 log2_chroma_wh |=
s->cdy[
i] >> 1 <<
i * 4 |
s->cdx[
i] >> 1 <<
i * 4 + 2;
347 if (
s->numXtiles * (uint64_t)
s->numYtiles > INT_MAX/
sizeof(*
s->tile) ||
350 s->numXtiles =
s->numYtiles = 0;
356 s->numXtiles =
s->numYtiles = 0;
360 for (
i = 0;
i <
s->numXtiles *
s->numYtiles;
i++) {
371 s->reduction_factor),
373 s->reduction_factor));
382 switch (
s->colour_space) {
401 for (
i = 0;
i < possible_fmts_nb; ++
i) {
402 if (
pix_fmt_match(possible_fmts[
i], ncomponents,
s->precision, log2_chroma_wh,
s->pal8)) {
403 s->avctx->pix_fmt = possible_fmts[
i];
408 if (
i == possible_fmts_nb) {
409 if (ncomponents == 4 &&
410 s->cdy[0] == 1 &&
s->cdx[0] == 1 &&
411 s->cdy[1] == 1 &&
s->cdx[1] == 1 &&
412 s->cdy[2] ==
s->cdy[3] &&
s->cdx[2] ==
s->cdx[3]) {
413 if (
s->precision == 8 &&
s->cdy[2] == 2 &&
s->cdx[2] == 2 && !
s->pal8) {
425 if (
i == possible_fmts_nb) {
427 "Unknown pix_fmt, profile: %d, colour_space: %d, "
428 "components: %d, precision: %d\n"
429 "cdx[0]: %d, cdy[0]: %d\n"
430 "cdx[1]: %d, cdy[1]: %d\n"
431 "cdx[2]: %d, cdy[2]: %d\n"
432 "cdx[3]: %d, cdy[3]: %d\n",
433 s->avctx->profile,
s->colour_space, ncomponents,
s->precision,
436 ncomponents > 1 ?
s->cdx[1] : 0,
437 ncomponents > 1 ?
s->cdy[1] : 0,
438 ncomponents > 2 ?
s->cdx[2] : 0,
439 ncomponents > 2 ?
s->cdy[2] : 0,
440 ncomponents > 3 ?
s->cdx[3] : 0,
441 ncomponents > 3 ?
s->cdy[3] : 0);
444 s->avctx->bits_per_raw_sample =
s->precision;
460 c->nreslevels = bytestream2_get_byteu(&
s->g) + 1;
466 if (
c->nreslevels <=
s->reduction_factor) {
471 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction_factor too large for this bitstream, max is %d\n",
c->nreslevels - 1);
472 s->reduction_factor =
c->nreslevels - 1;
477 c->nreslevels2decode =
c->nreslevels -
s->reduction_factor;
479 c->log2_cblk_width = (bytestream2_get_byteu(&
s->g) & 15) + 2;
480 c->log2_cblk_height = (bytestream2_get_byteu(&
s->g) & 15) + 2;
482 if (
c->log2_cblk_width > 10 ||
c->log2_cblk_height > 10 ||
483 c->log2_cblk_width +
c->log2_cblk_height > 12) {
488 c->cblk_style = bytestream2_get_byteu(&
s->g);
489 if (
c->cblk_style != 0) {
494 c->transform = bytestream2_get_byteu(&
s->g);
504 for (
i = 0;
i <
c->nreslevels;
i++) {
505 byte = bytestream2_get_byte(&
s->g);
506 c->log2_prec_widths[
i] =
byte & 0x0F;
507 c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;
509 if (
c->log2_prec_widths[
i] == 0 ||
c->log2_prec_heights[
i] == 0) {
511 c->log2_prec_widths[
i],
c->log2_prec_heights[
i]);
512 c->log2_prec_widths[
i] =
c->log2_prec_heights[
i] = 1;
517 memset(
c->log2_prec_widths , 15,
sizeof(
c->log2_prec_widths ));
518 memset(
c->log2_prec_heights, 15,
sizeof(
c->log2_prec_heights));
535 tmp.csty = bytestream2_get_byteu(&
s->g);
538 tmp.prog_order = bytestream2_get_byteu(&
s->g);
540 tmp.nlayers = bytestream2_get_be16u(&
s->g);
541 tmp.mct = bytestream2_get_byteu(&
s->g);
543 if (
tmp.mct &&
s->ncomponents < 3) {
545 "MCT %"PRIu8
" with too few components (%d)\n",
546 tmp.mct,
s->ncomponents);
553 for (compno = 0; compno <
s->ncomponents; compno++)
554 if (!(properties[compno] &
HAD_COC))
555 memcpy(
c + compno, &
tmp,
sizeof(
tmp));
571 compno = bytestream2_get_byteu(&
s->g);
573 if (compno >=
s->ncomponents) {
575 "Invalid compno %d. There are %d components in the image.\n",
576 compno,
s->ncomponents);
581 c->csty = bytestream2_get_byteu(&
s->g);
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);
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));
801 bytestream2_get_byte(&
s->g);
802 Stlm = bytestream2_get_byte(&
s->g);
805 ST = (Stlm >> 4) & 0x03;
807 SP = (Stlm >> 6) & 0x01;
808 tile_tlm = (
n - 4) / ((
SP + 1) * 2 + ST);
809 for (
i = 0;
i < tile_tlm;
i++) {
814 bytestream2_get_byte(&
s->g);
817 bytestream2_get_be16(&
s->g);
820 bytestream2_get_be32(&
s->g);
824 bytestream2_get_be16(&
s->g);
826 bytestream2_get_be32(&
s->g);
843 bytestream2_get_byte(&
s->g);
845 for (
i = 0;
i <
n - 3;
i++) {
846 v = bytestream2_get_byte(&
s->g);
857 int tilex = tileno %
s->numXtiles;
858 int tiley = tileno /
s->numXtiles;
864 tile->
coord[0][0] = av_clip(tilex * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
865 tile->
coord[0][1] = av_clip((tilex + 1) * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
866 tile->
coord[1][0] = av_clip(tiley * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
867 tile->
coord[1][1] = av_clip((tiley + 1) * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
869 for (compno = 0; compno <
s->ncomponents; compno++) {
880 comp->coord_o[0][0] /=
s->cdx[compno];
881 comp->coord_o[0][1] /=
s->cdx[compno];
882 comp->coord_o[1][0] /=
s->cdy[compno];
883 comp->coord_o[1][1] /=
s->cdy[compno];
892 s->cbps[compno],
s->cdx[compno],
893 s->cdy[compno],
s->avctx))
908 return num < 0 ? num : 3 + num;
910 return num < 0 ? num : 6 + num;
912 return num < 0 ? num : 37 + num;
929 int layno,
uint8_t *expn,
int numgbits)
931 int bandno, cblkno,
ret, nb_code_blocks;
934 if (layno < rlevel->band[0].prec[precno].decoded_layers)
953 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
962 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
964 int incl, newpasses, llen;
977 int v = expn[bandno] + numgbits - 1 -
979 if (v < 0 || v > 30) {
981 "nonzerobits %d invalid or unsupported\n", v);
997 "Block with length beyond 16 bits");
1016 while (newpasses1 < newpasses) {
1042 newpasses -= newpasses1;
1052 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1055 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1060 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1064 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1077 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1100 int RSpoc,
int CSpoc,
1101 int LYEpoc,
int REpoc,
int CEpoc,
1102 int Ppoc,
int *tp_index)
1105 int layno, reslevelno, compno, precno, ok_reslevel;
1113 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1115 for (layno = 0; layno < LYEpoc; layno++) {
1116 for (compno = CSpoc; compno < CEpoc; compno++) {
1119 if (reslevelno < codsty->nreslevels) {
1127 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1138 for (layno = 0; layno < LYEpoc; layno++) {
1140 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1142 for (compno = CSpoc; compno < CEpoc; compno++) {
1145 if (reslevelno < codsty->nreslevels) {
1153 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1164 for (compno = CSpoc; compno < CEpoc; compno++) {
1174 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1180 if (step_x >= 31 || step_y >= 31){
1187 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1188 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1189 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1190 unsigned prcx, prcy;
1193 int xc = x /
s->cdx[compno];
1194 int yc = y /
s->cdy[compno];
1216 for (layno = 0; layno < LYEpoc; layno++) {
1219 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1232 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1236 for (compno = CSpoc; compno < CEpoc; compno++) {
1240 if (reslevelno < codsty->nreslevels) {
1250 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1251 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1252 for (compno = CSpoc; compno < CEpoc; compno++) {
1258 unsigned prcx, prcy;
1260 int xc = x /
s->cdx[compno];
1261 int yc = y /
s->cdy[compno];
1287 for (layno = 0; layno < LYEpoc; layno++) {
1291 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1305 for (compno = CSpoc; compno < CEpoc; compno++) {
1309 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1316 if (step_x >= 31 || step_y >= 31){
1323 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1324 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1325 for (compno = CSpoc; compno < CEpoc; compno++) {
1329 int xc = x /
s->cdx[compno];
1330 int yc = y /
s->cdy[compno];
1332 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1333 unsigned prcx, prcy;
1357 for (layno = 0; layno < LYEpoc; layno++) {
1360 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1415 int bpno,
int bandno,
1416 int vert_causal_ctx_csty_symbol)
1418 int mask = 3 << (bpno - 1), y0, x, y;
1420 for (y0 = 0; y0 <
height; y0 += 4)
1421 for (x = 0; x <
width; x++)
1422 for (y = y0; y <
height && y < y0 + 4; y++) {
1423 int flags_mask = -1;
1424 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1437 t1->data[(y) *
t1->stride + x] < 0);
1445 int bpno,
int vert_causal_ctx_csty_symbol)
1450 phalf = 1 << (bpno - 1);
1453 for (y0 = 0; y0 <
height; y0 += 4)
1454 for (x = 0; x <
width; x++)
1455 for (y = y0; y <
height && y < y0 + 4; y++)
1457 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1461 t1->mqc.cx_states + ctxno)
1463 t1->data[(y) *
t1->stride + x] +=
t1->data[(y) *
t1->stride + x] < 0 ? -
r :
r;
1470 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1472 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1474 for (y0 = 0; y0 <
height; y0 += 4) {
1475 for (x = 0; x <
width; x++) {
1476 int flags_mask = -1;
1477 if (vert_causal_ctx_csty_symbol)
1497 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1498 int flags_mask = -1;
1499 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1512 t1->mqc.cx_states + ctxno) ^
1530 "Segmentation symbol value incorrect\n");
1547 memset(
t1->data, 0,
t1->stride *
height *
sizeof(*
t1->data));
1553 memset(
t1->flags, 0,
t1->stride * (
height + 2) *
sizeof(*
t1->flags));
1567 vert_causal_ctx_csty_symbol);
1576 vert_causal_ctx_csty_symbol);
1604 if (cblk->
data + cblk->
length - 2*(term_cnt < cblk->nb_terminations) !=
t1->mqc.bp) {
1606 cblk->
data + cblk->
length - 2*(term_cnt < cblk->nb_terminations) -
t1->mqc.bp);
1625 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1626 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
1627 int *
src =
t1->data + j*
t1->stride;
1628 for (
i = 0;
i <
w; ++
i)
1640 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1642 int *
src =
t1->data + j*
t1->stride;
1644 for (
i = 0;
i <
w; ++
i)
1645 datap[
i] =
src[
i] / 2;
1648 for (
i = 0;
i <
w; ++
i)
1660 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1662 int *
src =
t1->data + j*
t1->stride;
1663 for (
i = 0;
i <
w; ++
i)
1673 for (
i = 1;
i < 3;
i++) {
1684 for (
i = 0;
i < 3;
i++)
1690 for (
i = 0;
i < 2;
i++)
1700 int compno, reslevelno, bandno;
1703 for (compno = 0; compno <
s->ncomponents; compno++) {
1714 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1715 int nb_precincts, precno;
1717 int cblkno = 0, bandpos;
1719 bandpos = bandno + (reslevelno > 0);
1721 if (band->
coord[0][0] == band->
coord[0][1] ||
1727 for (precno = 0; precno < nb_precincts; precno++) {
1765 #define WRITE_FRAME(D, PIXEL) \
1766 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1767 AVFrame * picture, int precision) \
1769 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1770 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1771 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1776 for (compno = 0; compno < s->ncomponents; compno++) { \
1777 Jpeg2000Component *comp = tile->comp + compno; \
1778 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1780 float *datap = comp->f_data; \
1781 int32_t *i_datap = comp->i_data; \
1782 int cbps = s->cbps[compno]; \
1783 int w = tile->comp[compno].coord[0][1] - s->image_offset_x; \
1787 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
1789 y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno]; \
1790 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1791 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) { \
1794 x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno]; \
1795 dst = line + x * pixelsize + compno*!planar; \
1797 if (codsty->transform == FF_DWT97) { \
1798 for (; x < w; x++) { \
1799 int val = lrintf(*datap) + (1 << (cbps - 1)); \
1801 val = av_clip(val, 0, (1 << cbps) - 1); \
1802 *dst = val << (precision - cbps); \
1807 for (; x < w; x++) { \
1808 int val = *i_datap + (1 << (cbps - 1)); \
1810 val = av_clip(val, 0, (1 << cbps) - 1); \
1811 *dst = val << (precision - cbps); \
1816 line += picture->linesize[plane] / sizeof(PIXEL); \
1828 int jobnr,
int threadnr)
1841 for (x = 0; x <
s->ncomponents; x++) {
1842 if (
s->cdef[x] < 0) {
1843 for (x = 0; x <
s->ncomponents; x++) {
1846 if ((
s->ncomponents & 1) == 0)
1847 s->cdef[
s->ncomponents-1] = 0;
1852 if (
s->precision <= 8) {
1853 write_frame_8(
s, tile, picture, 8);
1860 write_frame_16(
s, tile, picture, precision);
1869 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
1870 if (
s->tile[tileno].comp) {
1871 for (compno = 0; compno <
s->ncomponents; compno++) {
1881 memset(
s->codsty, 0,
sizeof(
s->codsty));
1882 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
1883 memset(
s->properties, 0,
sizeof(
s->properties));
1884 memset(&
s->poc , 0,
sizeof(
s->poc));
1885 s->numXtiles =
s->numYtiles = 0;
1894 uint8_t *properties =
s->properties;
1906 marker = bytestream2_get_be16u(&
s->g);
1917 if (
s->curtileno < 0) {
1922 tile =
s->tile +
s->curtileno;
1924 if (tp->
tp_end <
s->g.buffer) {
1936 len = bytestream2_get_be16(&
s->g);
1944 if (
s->ncomponents) {
1950 s->numXtiles =
s->numYtiles = 0;
1970 codsty =
s->tile[
s->curtileno].codsty;
1971 qntsty =
s->tile[
s->curtileno].qntsty;
1972 poc = &
s->tile[
s->curtileno].poc;
1973 properties =
s->tile[
s->curtileno].properties;
1992 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
1999 "error during processing marker segment %.4"PRIx16
"\n",
2013 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2029 uint32_t atom_size, atom, atom_end;
2030 int search_range = 10;
2035 atom_size = bytestream2_get_be32u(&
s->g);
2036 atom = bytestream2_get_be32u(&
s->g);
2037 if (atom_size == 1) {
2038 if (bytestream2_get_be32u(&
s->g)) {
2042 atom_size = bytestream2_get_be32u(&
s->g);
2043 if (atom_size < 16 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 16 > INT_MAX)
2047 if (atom_size < 8 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 8 > INT_MAX)
2060 uint32_t atom2_size, atom2, atom2_end;
2064 atom2_size = bytestream2_get_be32u(&
s->g);
2065 atom2 = bytestream2_get_be32u(&
s->g);
2067 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2072 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2073 int method = bytestream2_get_byteu(&
s->g);
2076 s->colour_space = bytestream2_get_be32u(&
s->g);
2078 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2079 int i,
size, colour_count, colour_channels, colour_depth[3];
2080 colour_count = bytestream2_get_be16u(&
s->g);
2081 colour_channels = bytestream2_get_byteu(&
s->g);
2083 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2084 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2085 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2086 size = (colour_depth[0] + 7 >> 3) * colour_count +
2087 (colour_depth[1] + 7 >> 3) * colour_count +
2088 (colour_depth[2] + 7 >> 3) * colour_count;
2090 colour_channels != 3 ||
2091 colour_depth[0] > 16 ||
2092 colour_depth[1] > 16 ||
2093 colour_depth[2] > 16 ||
2094 atom2_size <
size) {
2100 for (
i = 0;
i < colour_count;
i++) {
2102 if (colour_depth[0] <= 8) {
2103 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2104 r |=
r >> colour_depth[0];
2106 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2108 if (colour_depth[1] <= 8) {
2109 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2110 g |=
g >> colour_depth[1];
2112 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2114 if (colour_depth[2] <= 8) {
2115 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2116 b |=
b >> colour_depth[2];
2118 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2120 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2122 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2123 int n = bytestream2_get_be16u(&
s->g);
2125 int cn = bytestream2_get_be16(&
s->g);
2126 int av_unused typ = bytestream2_get_be16(&
s->g);
2127 int asoc = bytestream2_get_be16(&
s->g);
2128 if (cn < 4 && asoc < 4)
2131 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2132 int64_t vnum, vden, hnum, hden, vexp, hexp;
2135 resx = bytestream2_get_be32u(&
s->g);
2136 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2140 vnum = bytestream2_get_be16u(&
s->g);
2141 vden = bytestream2_get_be16u(&
s->g);
2142 hnum = bytestream2_get_be16u(&
s->g);
2143 hden = bytestream2_get_be16u(&
s->g);
2144 vexp = bytestream2_get_byteu(&
s->g);
2145 hexp = bytestream2_get_byteu(&
s->g);
2146 if (!vnum || !vden || !hnum || !hden) {
2158 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2159 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2161 hnum * vden * pow(10, hexp),
2162 vnum * hden * pow(10, vexp),
2166 }
while (atom_end - atom2_end >= 8);
2204 memset(
s->cdef, -1,
sizeof(
s->cdef));
2213 (bytestream2_get_be32u(&
s->g) == 12) &&
2218 "Could not find Jpeg2000 codestream atom.\n");
2253 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2254 if (
s->sar.num &&
s->sar.den)
2256 s->sar.num =
s->sar.den = 0;
2265 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2266 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2269 {
"lowres",
"Lower the decoding resolution by a power of two",