39 #define JP2_SIG_TYPE 0x6A502020 40 #define JP2_SIG_VALUE 0x0D0A870A 41 #define JP2_CODESTREAM 0x6A703263 103 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFFu);
106 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
113 if (bytestream2_get_byte(&s->
g) == 0xff)
123 int sp = -1, curval = 0;
128 while (node && !node->
vis) {
136 curval = stack[sp]->
val;
138 while (curval < threshold && sp >= 0) {
139 if (curval < stack[sp]->val)
140 curval = stack[sp]->
val;
141 while (curval < threshold) {
151 stack[sp]->
val = curval;
168 s->
width = bytestream2_get_be32u(&s->
g);
169 s->
height = bytestream2_get_be32u(&s->
g);
176 ncomponents = bytestream2_get_be16u(&s->
g);
178 if (ncomponents <= 0) {
184 if (ncomponents > 4) {
203 uint8_t x = bytestream2_get_byteu(&s->
g);
204 s->
cbps[i] = (x & 0x7f) + 1;
206 s->
sgnd[i] = !!(x & 0x80);
207 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
208 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
210 if (s->
cdx[i] != 1 || s->
cdy[i] != 1) {
212 "CDxy values %d %d for component %d",
214 if (!s->
cdx[i] || !s->
cdy[i])
320 byte = bytestream2_get_byte(&s->
g);
341 tmp.
csty = bytestream2_get_byteu(&s->
g);
346 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
347 tmp.
mct = bytestream2_get_byteu(&s->
g);
351 "MCT %"PRIu8
" with too few components (%d)\n",
356 if ((ret =
get_cox(s, &tmp)) < 0)
359 for (compno = 0; compno < s->
ncomponents; compno++)
360 if (!(properties[compno] &
HAD_COC))
361 memcpy(c + compno, &tmp,
sizeof(tmp));
375 compno = bytestream2_get_byteu(&s->
g);
379 "Invalid compno %d. There are %d components in the image.\n",
385 c->
csty = bytestream2_get_byteu(&s->
g);
402 x = bytestream2_get_byteu(&s->
g);
412 for (i = 0; i < n; i++)
413 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
417 x = bytestream2_get_be16u(&s->
g);
418 q->
expn[0] = x >> 11;
419 q->
mant[0] = x & 0x7ff;
421 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
422 q->
expn[i] = curexpn;
430 for (i = 0; i < n; i++) {
431 x = bytestream2_get_be16u(&s->
g);
432 q->
expn[i] = x >> 11;
433 q->
mant[i] = x & 0x7ff;
446 if ((ret =
get_qcx(s, n, &tmp)) < 0)
448 for (compno = 0; compno < s->
ncomponents; compno++)
449 if (!(properties[compno] &
HAD_QCC))
450 memcpy(q + compno, &tmp,
sizeof(tmp));
464 compno = bytestream2_get_byteu(&s->
g);
468 "Invalid compno %d. There are %d components in the image.\n",
474 return get_qcx(s, n - 1, q + compno);
488 Isot = bytestream2_get_be16u(&s->
g);
496 Psot = bytestream2_get_be32u(&s->
g);
497 TPsot = bytestream2_get_byteu(&s->
g);
500 bytestream2_get_byteu(&s->
g);
538 uint8_t Stlm, ST, SP, tile_tlm, i;
539 bytestream2_get_byte(&s->
g);
540 Stlm = bytestream2_get_byte(&s->
g);
543 ST = (Stlm >> 4) & 0x03;
545 SP = (Stlm >> 6) & 0x01;
546 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
547 for (i = 0; i < tile_tlm; i++) {
552 bytestream2_get_byte(&s->
g);
555 bytestream2_get_be16(&s->
g);
558 bytestream2_get_be32(&s->
g);
562 bytestream2_get_be16(&s->
g);
564 bytestream2_get_be32(&s->
g);
580 for (compno = 0; compno < s->
ncomponents; compno++) {
597 s->
cbps[compno], s->
cdx[compno],
613 return num < 0 ? num : 3 + num;
615 return num < 0 ? num : 6 + num;
617 return num < 0 ? num : 37 + num;
634 int layno,
uint8_t *expn,
int numgbits)
636 int bandno, cblkno, ret, nb_code_blocks;
644 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
653 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
655 int incl, newpasses, llen;
667 int v = expn[bandno] + numgbits - 1 -
671 "nonzerobits %d invalid\n", v);
683 if (ret >
sizeof(cblk->
data)) {
685 "Block with lengthinc greater than %zu",
702 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
707 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
716 cblk->
data[0] = 0xFF;
717 cblk->
data[1] = 0xFF;
724 "Block length %"PRIu16
" > data size %zd\n",
736 int layno, reslevelno, compno, precno, ok_reslevel;
744 for (reslevelno = 0; ok_reslevel; reslevelno++) {
746 for (compno = 0; compno < s->
ncomponents; compno++) {
749 if (reslevelno < codsty->nreslevels) {
757 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
767 for (compno = 0; compno < s->
ncomponents; compno++) {
779 for (y = 0; y < s->
height; y += 256) {
783 for (x = 0; x < s->
width; x += 256) {
784 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
804 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
841 int bpno,
int bandno,
int bpass_csty_symbol,
842 int vert_causal_ctx_csty_symbol)
844 int mask = 3 << (bpno - 1), y0, x, y;
846 for (y0 = 0; y0 <
height; y0 += 4)
847 for (x = 0; x <
width; x++)
848 for (y = y0; y < height && y < y0 + 4; y++) {
852 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
856 if (bpass_csty_symbol)
876 phalf = 1 << (bpno - 1);
879 for (y0 = 0; y0 <
height; y0 += 4)
880 for (x = 0; x <
width; x++)
881 for (y = y0; y < height && y < y0 + 4; y++)
887 t1->
data[y][x] += t1->
data[y][x] < 0 ? -r :
r;
894 int seg_symbols,
int vert_causal_ctx_csty_symbol)
896 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
898 for (y0 = 0; y0 <
height; y0 += 4) {
899 for (x = 0; x <
width; x++) {
900 if (y0 + 3 < height &&
918 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
922 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
951 "Segmentation symbol value incorrect\n");
964 for (y = 0; y <
height; y++)
965 memset(t1->
data[y], 0, width *
sizeof(**t1->
data));
970 for (y = 0; y < height + 2; y++)
971 memset(t1->
flags[y], 0, (width + 2) *
sizeof(**t1->
flags));
981 bpass_csty_symbol && (clnpass_cnt >= 4),
982 vert_causal_ctx_csty_symbol);
986 if (bpass_csty_symbol && clnpass_cnt >= 4)
992 vert_causal_ctx_csty_symbol);
993 clnpass_cnt = clnpass_cnt + 1;
994 if (bpass_csty_symbol && clnpass_cnt >= 4)
1020 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1021 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1022 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1023 int *src = t1->
data[j];
1024 for (i = 0; i < w; ++i)
1035 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1036 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1038 int *src = t1->
data[j];
1039 for (i = 0; i < w; ++i)
1040 datap[i] = (src[i] * band->
i_stepsize + (1 << 15)) >> 16;
1063 float *srcf[3], i0f, i1f,
i2f;
1065 for (i = 0; i < 3; i++)
1071 for (i = 0; i < 2; i++)
1075 for (i = 0; i < csize; i++) {
1086 for (i = 0; i < csize; i++) {
1087 i0 = *src[0] + (((
i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1088 i1 = *src[0] - (((
i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1090 i2 = *src[0] + (((
i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1097 for (i = 0; i < csize; i++) {
1098 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1112 int compno, reslevelno, bandno;
1119 for (compno = 0; compno < s->
ncomponents; compno++) {
1126 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1127 uint16_t nb_precincts, precno;
1129 int cblkno = 0, bandpos;
1130 bandpos = bandno + (reslevelno > 0);
1132 if (band->
coord[0][0] == band->
coord[0][1] ||
1138 for (precno = 0; precno < nb_precincts; precno++) {
1150 x = cblk->
coord[0][0];
1151 y = cblk->
coord[1][0];
1171 for (compno = 0; compno < s->
ncomponents; compno++) {
1174 float *datap = comp->
f_data;
1176 int cbps = s->
cbps[compno];
1188 for (; x < w; x += s->
cdx[compno]) {
1189 int val =
lrintf(*datap) + (1 << (cbps - 1));
1191 val = av_clip(val, 0, (1 << cbps) - 1);
1192 *dst = val << (8 - cbps);
1197 for (; x < w; x += s->
cdx[compno]) {
1198 int val = *i_datap + (1 << (cbps - 1));
1200 val = av_clip(val, 0, (1 << cbps) - 1);
1201 *dst = val << (8 - cbps);
1210 for (compno = 0; compno < s->
ncomponents; compno++) {
1213 float *datap = comp->
f_data;
1216 int cbps = s->
cbps[compno];
1220 linel = (uint16_t *)picture->
data[0] + y * (picture->
linesize[0] >> 1);
1226 for (; x < w; x += s-> cdx[compno]) {
1227 int val =
lrintf(*datap) + (1 << (cbps - 1));
1229 val = av_clip(val, 0, (1 << cbps) - 1);
1231 *dst = val << (16 - cbps);
1236 for (; x < w; x += s-> cdx[compno]) {
1237 int val = *i_datap + (1 << (cbps - 1));
1239 val = av_clip(val, 0, (1 << cbps) - 1);
1241 *dst = val << (16 - cbps);
1246 linel += picture->
linesize[0] >> 1;
1258 for (compno = 0; compno < s->
ncomponents; compno++) {
1286 marker = bytestream2_get_be16u(&s->
g);
1312 len = bytestream2_get_be16u(&s->
g);
1321 ret =
get_coc(s, codsty, properties);
1324 ret =
get_cod(s, codsty, properties);
1327 ret =
get_qcc(s, len, qntsty, properties);
1330 ret =
get_qcd(s, len, qntsty, properties);
1333 if (!(ret =
get_sot(s, len))) {
1349 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
1356 "error during processing marker segment %.4"PRIx16
"\n",
1358 return ret ? ret : -1;
1386 uint32_t atom_size, atom;
1387 int found_codestream = 0, search_range = 10;
1389 while(!found_codestream && search_range
1392 atom_size = bytestream2_get_be32u(&s->
g);
1393 atom = bytestream2_get_be32u(&s->
g);
1395 found_codestream = 1;
1404 if (found_codestream)
1428 (bytestream2_get_be32u(&s->
g) == 12) &&
1433 "Could not find Jpeg2000 codestream atom.\n");
1480 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x) 1481 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM 1484 {
"lowres",
"Lower the decoding resolution by a power of two",
1498 static const AVClass class = {
1514 .priv_class = &
class,
static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int seg_symbols, int vert_causal_ctx_csty_symbol)
#define JPEG2000_CBLK_VSC
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
void av_cold ff_jpeg2000_init_tier1_luts(void)
int flags[JPEG2000_MAX_CBLKW+2][JPEG2000_MAX_CBLKH+2]
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
This structure describes decoded (raw) audio or video data.
static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
Jpeg2000TgtNode * cblkincl
static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, AVFrame *picture)
#define JPEG2000_T1_SIG_NB
packed RGB 8:8:8, 24bpp, RGBRGB...
#define JPEG2000_PGOD_PCRL
#define JPEG2000_PGOD_RLCP
static void jpeg2000_flush(Jpeg2000DecoderContext *s)
static Float11 * i2f(int i, Float11 *f)
static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
Jpeg2000QuantStyle qntsty[4]
static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
#define FF_ARRAY_ELEMS(a)
static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
static int init_tile(Jpeg2000DecoderContext *s, int tileno)
Jpeg2000CodingStyle codsty[4]
#define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1
uint16_t nb_codeblocks_height
static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band)
static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Macro definitions for various function/variable attributes.
static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band)
static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION
Multithreading support functions.
Jpeg2000TgtNode * zerobits
static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, int threshold)
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define JPEG2000_T1_SIG_SE
static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
#define JPEG2000_CBLK_SEGSYM
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
#define JPEG2000_CSTY_EPH
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int getnpasses(Jpeg2000DecoderContext *s)
static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol, int vert_causal_ctx_csty_symbol)
static const uint16_t mask[17]
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH]
void ff_mqc_initdec(MqcState *mqc, uint8_t *bp)
Initialize MQ-decoder.
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define JPEG2000_PGOD_CPRL
static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
enum AVPictureType pict_type
Picture type of the frame.
#define AV_PIX_FMT_GRAY16
struct Jpeg2000TgtNode * parent
int width
picture width / height.
JPEG 2000 structures and defines common to encoder and decoder.
#define FF_PROFILE_UNKNOWN
#define JPEG2000_MAX_RESLEVELS
#define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0
static av_always_inline av_const long int lrintf(float x)
static av_cold void jpeg2000_init_static_data(AVCodec *codec)
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
#define FF_PROFILE_JPEG2000_DCINEMA_4K
#define JPEG2000_MAX_DECLEVELS
static int get_siz(Jpeg2000DecoderContext *s)
static const AVProfile profiles[]
static const float f_ict_params[4]
#define JPEG2000_PGOD_RPCL
static int ff_jpeg2000_ceildivpow2(int a, int b)
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
static av_always_inline int bytestream2_tell(GetByteContext *g)
Libavcodec external API header.
uint8_t nreslevels2decode
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate)
MQ decoder.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
AVCodec ff_jpeg2000_decoder
void av_cold ff_mqc_init_context_tables(void)
MQ-coder Initialize context tables (QE, NLPS, NMPS)
static int getlblockinc(Jpeg2000DecoderContext *s)
Describe the class of an AVClass context structure.
uint16_t nb_codeblocks_width
static const AVOption options[]
#define JPEG2000_T1_SIG_S
Jpeg2000ResLevel * reslevel
static int ff_jpeg2000_ceildiv(int a, int b)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
static int get_sot(Jpeg2000DecoderContext *s, int n)
static int jp2_find_codestream(Jpeg2000DecoderContext *s)
static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
common internal api header.
common internal and external API header
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Jpeg2000QuantStyle qntsty[4]
int ff_dwt_decode(DWTContext *s, void *t)
Jpeg2000TilePart tile_part[3]
#define JPEG2000_CBLK_BYPASS
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy, AVCodecContext *avctx)
#define JPEG2000_T1_SIG_SW
#define JPEG2000_PGOD_LRCP
static int get_bits(Jpeg2000DecoderContext *s, int n)
static const int i_ict_params[4]
static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000ResLevel *rlevel, int precno, int layno, uint8_t *expn, int numgbits)
#define FF_PROFILE_JPEG2000_DCINEMA_2K
int key_frame
1 -> keyframe, 0-> not
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
static void * av_mallocz_array(size_t nmemb, size_t size)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
static int ff_jpeg2000_getrefctxno(int flag)
#define JPEG2000_CSTY_PREC
uint32_t mant[JPEG2000_MAX_DECLEVELS *3]
static void decode_refpass(Jpeg2000T1Context *t1, int width, int height, int bpno)
This structure stores compressed data.
static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Jpeg2000CodingStyle codsty[4]
static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width, int height, int bandpos)
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)