36 #define JP2_SIG_TYPE 0x6A502020
37 #define JP2_SIG_VALUE 0x0D0A870A
38 #define JP2_CODESTREAM 0x6A703263
100 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFFu);
103 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
110 if (bytestream2_get_byte(&s->
g) == 0xff)
120 int sp = -1, curval = 0;
125 while (node && !node->
vis) {
133 curval = stack[sp]->
val;
135 while (curval < threshold && sp >= 0) {
136 if (curval < stack[sp]->val)
137 curval = stack[sp]->
val;
138 while (curval < threshold) {
148 stack[sp]->
val = curval;
165 s->
width = bytestream2_get_be32u(&s->
g);
166 s->
height = bytestream2_get_be32u(&s->
g);
173 ncomponents = bytestream2_get_be16u(&s->
g);
175 if (ncomponents <= 0) {
181 if (ncomponents > 3) {
200 uint8_t x = bytestream2_get_byteu(&s->
g);
201 s->
cbps[i] = (x & 0x7f) + 1;
203 s->
sgnd[i] = !!(x & 0x80);
204 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
205 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
207 if (s->
cdx[i] != 1 || s->
cdy[i] != 1) {
209 "CDxy values %d %d for component %d",
211 if (!s->
cdx[i] || !s->
cdy[i])
317 byte = bytestream2_get_byte(&s->
g);
338 tmp.
csty = bytestream2_get_byteu(&s->
g);
343 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
344 tmp.
mct = bytestream2_get_byteu(&s->
g);
348 "MCT %d with too few components (%d)\n",
353 if ((ret =
get_cox(s, &tmp)) < 0)
356 for (compno = 0; compno < s->
ncomponents; compno++)
357 if (!(properties[compno] &
HAD_COC))
358 memcpy(c + compno, &tmp,
sizeof(tmp));
372 compno = bytestream2_get_byteu(&s->
g);
376 "Invalid compno %d. There are %d components in the image.\n",
382 c->
csty = bytestream2_get_byteu(&s->
g);
399 x = bytestream2_get_byteu(&s->
g);
409 for (i = 0; i < n; i++)
410 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
414 x = bytestream2_get_be16u(&s->
g);
415 q->
expn[0] = x >> 11;
416 q->
mant[0] = x & 0x7ff;
418 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
419 q->
expn[i] = curexpn;
427 for (i = 0; i < n; i++) {
428 x = bytestream2_get_be16u(&s->
g);
429 q->
expn[i] = x >> 11;
430 q->
mant[i] = x & 0x7ff;
443 if ((ret =
get_qcx(s, n, &tmp)) < 0)
445 for (compno = 0; compno < s->
ncomponents; compno++)
446 if (!(properties[compno] &
HAD_QCC))
447 memcpy(q + compno, &tmp,
sizeof(tmp));
461 compno = bytestream2_get_byteu(&s->
g);
465 "Invalid compno %d. There are %d components in the image.\n",
471 return get_qcx(s, n - 1, q + compno);
485 Isot = bytestream2_get_be16u(&s->
g);
493 Psot = bytestream2_get_be32u(&s->
g);
494 TPsot = bytestream2_get_byteu(&s->
g);
497 bytestream2_get_byteu(&s->
g);
535 uint8_t Stlm, ST, SP, tile_tlm, i;
536 bytestream2_get_byte(&s->
g);
537 Stlm = bytestream2_get_byte(&s->
g);
540 ST = (Stlm >> 4) & 0x03;
542 SP = (Stlm >> 6) & 0x01;
543 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
544 for (i = 0; i < tile_tlm; i++) {
549 bytestream2_get_byte(&s->
g);
552 bytestream2_get_be16(&s->
g);
555 bytestream2_get_be32(&s->
g);
559 bytestream2_get_be16(&s->
g);
561 bytestream2_get_be32(&s->
g);
577 for (compno = 0; compno < s->
ncomponents; compno++) {
594 s->
cbps[compno], s->
cdx[compno],
610 return num < 0 ? num : 3 + num;
612 return num < 0 ? num : 6 + num;
614 return num < 0 ? num : 37 + num;
631 int layno,
uint8_t *expn,
int numgbits)
633 int bandno, cblkno, ret, nb_code_blocks;
641 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
650 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
652 int incl, newpasses, llen;
664 int v = expn[bandno] + numgbits - 1 -
668 "nonzerobits %d invalid\n", v);
680 if (ret >
sizeof(cblk->
data)) {
682 "Block with lengthinc greater than %zu",
699 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
704 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
713 cblk->
data[0] = 0xFF;
714 cblk->
data[1] = 0xFF;
721 "Block length %d > data size %zd\n",
733 int layno, reslevelno, compno, precno, ok_reslevel;
741 for (reslevelno = 0; ok_reslevel; reslevelno++) {
743 for (compno = 0; compno < s->
ncomponents; compno++) {
746 if (reslevelno < codsty->nreslevels) {
754 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
764 for (compno = 0; compno < s->
ncomponents; compno++) {
776 for (y = 0; y < s->
height; y += 256) {
780 for (x = 0; x < s->
width; x += 256) {
781 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
801 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
838 int bpno,
int bandno,
int bpass_csty_symbol,
839 int vert_causal_ctx_csty_symbol)
841 int mask = 3 << (bpno - 1), y0, x, y;
843 for (y0 = 0; y0 <
height; y0 += 4)
844 for (x = 0; x <
width; x++)
845 for (y = y0; y < height && y < y0 + 4; y++) {
849 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
853 if (bpass_csty_symbol)
873 phalf = 1 << (bpno - 1);
876 for (y0 = 0; y0 <
height; y0 += 4)
877 for (x = 0; x <
width; x++)
878 for (y = y0; y < height && y < y0 + 4; y++)
884 t1->
data[y][x] += t1->
data[y][x] < 0 ? -r :
r;
891 int seg_symbols,
int vert_causal_ctx_csty_symbol)
893 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
895 for (y0 = 0; y0 <
height; y0 += 4) {
896 for (x = 0; x <
width; x++) {
897 if (y0 + 3 < height &&
915 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
919 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
948 "Segmentation symbol value incorrect\n");
961 for (y = 0; y <
height; y++)
962 memset(t1->
data[y], 0, width *
sizeof(**t1->
data));
967 for (y = 0; y < height + 2; y++)
968 memset(t1->
flags[y], 0, (width + 2) *
sizeof(**t1->
flags));
978 bpass_csty_symbol && (clnpass_cnt >= 4),
979 vert_causal_ctx_csty_symbol);
983 if (bpass_csty_symbol && clnpass_cnt >= 4)
989 vert_causal_ctx_csty_symbol);
990 clnpass_cnt = clnpass_cnt + 1;
991 if (bpass_csty_symbol && clnpass_cnt >= 4)
1017 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1018 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1019 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1020 int *src = t1->
data[j];
1021 for (i = 0; i < w; ++i)
1032 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1033 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1035 int *src = t1->
data[j];
1036 for (i = 0; i < w; ++i)
1037 datap[i] = (src[i] * band->
i_stepsize + (1 << 15)) >> 16;
1060 float *srcf[3], i0f, i1f,
i2f;
1062 for (i = 0; i < 3; i++)
1068 for (i = 0; i < 2; i++)
1072 for (i = 0; i < csize; i++) {
1083 for (i = 0; i < csize; i++) {
1084 i0 = *src[0] + (((
i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1085 i1 = *src[0] - (((
i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1087 i2 = *src[0] + (((
i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1094 for (i = 0; i < csize; i++) {
1095 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1109 int compno, reslevelno, bandno;
1116 for (compno = 0; compno < s->
ncomponents; compno++) {
1123 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1124 uint16_t nb_precincts, precno;
1126 int cblkno = 0, bandpos;
1127 bandpos = bandno + (reslevelno > 0);
1129 if (band->
coord[0][0] == band->
coord[0][1] ||
1135 for (precno = 0; precno < nb_precincts; precno++) {
1147 x = cblk->
coord[0][0];
1148 y = cblk->
coord[1][0];
1168 for (compno = 0; compno < s->
ncomponents; compno++) {
1171 float *datap = comp->
f_data;
1173 int cbps = s->
cbps[compno];
1185 for (; x < w; x += s->
cdx[compno]) {
1186 int val =
lrintf(*datap) + (1 << (cbps - 1));
1188 val = av_clip(val, 0, (1 << cbps) - 1);
1189 *dst = val << (8 - cbps);
1194 for (; x < w; x += s->
cdx[compno]) {
1195 int val = *i_datap + (1 << (cbps - 1));
1197 val = av_clip(val, 0, (1 << cbps) - 1);
1198 *dst = val << (8 - cbps);
1207 for (compno = 0; compno < s->
ncomponents; compno++) {
1210 float *datap = comp->
f_data;
1213 int cbps = s->
cbps[compno];
1217 linel = (uint16_t *)picture->
data[0] + y * (picture->
linesize[0] >> 1);
1223 for (; x < w; x += s-> cdx[compno]) {
1224 int val =
lrintf(*datap) + (1 << (cbps - 1));
1226 val = av_clip(val, 0, (1 << cbps) - 1);
1228 *dst = val << (16 - cbps);
1233 for (; x < w; x += s-> cdx[compno]) {
1234 int val = *i_datap + (1 << (cbps - 1));
1236 val = av_clip(val, 0, (1 << cbps) - 1);
1238 *dst = val << (16 - cbps);
1243 linel += picture->
linesize[0] >> 1;
1255 for (compno = 0; compno < s->
ncomponents; compno++) {
1283 marker = bytestream2_get_be16u(&s->
g);
1309 len = bytestream2_get_be16u(&s->
g);
1318 ret =
get_coc(s, codsty, properties);
1321 ret =
get_cod(s, codsty, properties);
1324 ret =
get_qcc(s, len, qntsty, properties);
1327 ret =
get_qcd(s, len, qntsty, properties);
1330 if (!(ret =
get_sot(s, len))) {
1346 "unsupported marker 0x%.4X at pos 0x%X\n",
1353 "error during processing marker segment %.4x\n", marker);
1354 return ret ? ret : -1;
1382 uint32_t atom_size, atom;
1383 int found_codestream = 0, search_range = 10;
1385 while(!found_codestream && search_range
1388 atom_size = bytestream2_get_be32u(&s->
g);
1389 atom = bytestream2_get_be32u(&s->
g);
1391 found_codestream = 1;
1400 if (found_codestream)
1424 (bytestream2_get_be32u(&s->
g) == 12) &&
1429 "Could not find Jpeg2000 codestream atom.\n");
1476 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1477 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1480 {
"lowres",
"Lower the decoding resolution by a power of two",
1494 static const AVClass class = {
1510 .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.
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)
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 void jpeg2000_init_static_data(AVCodec *codec)
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
void ff_jpeg2000_init_tier1_luts(void)
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 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)
void ff_mqc_init_context_tables(void)
MQ-coder Initialize context tables (QE, NLPS, NMPS)
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
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)