41 #define JP2_SIG_TYPE 0x6A502020 42 #define JP2_SIG_VALUE 0x0D0A870A 43 #define JP2_CODESTREAM 0x6A703263 106 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFF
u);
109 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
116 if (bytestream2_get_byte(&s->
g) == 0xff)
126 int sp = -1, curval = 0;
131 while (node && !node->
vis) {
139 curval = stack[sp]->
val;
141 while (curval < threshold && sp >= 0) {
142 if (curval < stack[sp]->val)
143 curval = stack[sp]->
val;
144 while (curval < threshold) {
154 stack[sp]->
val = curval;
171 s->
width = bytestream2_get_be32u(&s->
g);
172 s->
height = bytestream2_get_be32u(&s->
g);
179 ncomponents = bytestream2_get_be16u(&s->
g);
181 if (ncomponents <= 0) {
187 if (ncomponents > 4) {
206 uint8_t x = bytestream2_get_byteu(&s->
g);
207 s->
cbps[i] = (x & 0x7f) + 1;
209 s->
sgnd[i] = !!(x & 0x80);
210 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
211 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
213 if (s->
cdx[i] != 1 || s->
cdy[i] != 1) {
215 "CDxy values %d %d for component %d",
217 if (!s->
cdx[i] || !s->
cdy[i])
323 byte = bytestream2_get_byte(&s->
g);
344 tmp.
csty = bytestream2_get_byteu(&s->
g);
349 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
350 tmp.
mct = bytestream2_get_byteu(&s->
g);
354 "MCT %"PRIu8
" with too few components (%d)\n",
359 if ((ret =
get_cox(s, &tmp)) < 0)
362 for (compno = 0; compno < s->
ncomponents; compno++)
363 if (!(properties[compno] &
HAD_COC))
364 memcpy(c + compno, &tmp,
sizeof(tmp));
378 compno = bytestream2_get_byteu(&s->
g);
382 "Invalid compno %d. There are %d components in the image.\n",
388 c->
csty = bytestream2_get_byteu(&s->
g);
405 x = bytestream2_get_byteu(&s->
g);
415 for (i = 0; i < n; i++)
416 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
420 x = bytestream2_get_be16u(&s->
g);
421 q->
expn[0] = x >> 11;
422 q->
mant[0] = x & 0x7ff;
424 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
425 q->
expn[i] = curexpn;
433 for (i = 0; i < n; i++) {
434 x = bytestream2_get_be16u(&s->
g);
435 q->
expn[i] = x >> 11;
436 q->
mant[i] = x & 0x7ff;
449 if ((ret =
get_qcx(s, n, &tmp)) < 0)
451 for (compno = 0; compno < s->
ncomponents; compno++)
452 if (!(properties[compno] &
HAD_QCC))
453 memcpy(q + compno, &tmp,
sizeof(tmp));
467 compno = bytestream2_get_byteu(&s->
g);
471 "Invalid compno %d. There are %d components in the image.\n",
477 return get_qcx(s, n - 1, q + compno);
491 Isot = bytestream2_get_be16u(&s->
g);
499 Psot = bytestream2_get_be32u(&s->
g);
500 TPsot = bytestream2_get_byteu(&s->
g);
503 bytestream2_get_byteu(&s->
g);
541 uint8_t Stlm, ST, SP, tile_tlm, i;
542 bytestream2_get_byte(&s->
g);
543 Stlm = bytestream2_get_byte(&s->
g);
546 ST = (Stlm >> 4) & 0x03;
548 SP = (Stlm >> 6) & 0x01;
549 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
550 for (i = 0; i < tile_tlm; i++) {
555 bytestream2_get_byte(&s->
g);
558 bytestream2_get_be16(&s->
g);
561 bytestream2_get_be32(&s->
g);
565 bytestream2_get_be16(&s->
g);
567 bytestream2_get_be32(&s->
g);
583 for (compno = 0; compno < s->
ncomponents; compno++) {
600 s->
cbps[compno], s->
cdx[compno],
616 return num < 0 ? num : 3 + num;
618 return num < 0 ? num : 6 + num;
620 return num < 0 ? num : 37 + num;
637 int layno,
uint8_t *expn,
int numgbits)
639 int bandno, cblkno, ret, nb_code_blocks;
647 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
656 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
658 int incl, newpasses, llen;
670 int v = expn[bandno] + numgbits - 1 -
674 "nonzerobits %d invalid\n", v);
686 if (ret >
sizeof(cblk->
data)) {
688 "Block with lengthinc greater than %zu",
705 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
710 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
719 cblk->
data[0] = 0xFF;
720 cblk->
data[1] = 0xFF;
727 "Block length %"PRIu16
" > data size %zd\n",
738 int layno, reslevelno, compno, precno, ok_reslevel;
743 for (reslevelno = 0; ok_reslevel; reslevelno++) {
745 for (compno = 0; compno < s->
ncomponents; compno++) {
748 if (reslevelno < codsty->nreslevels) {
756 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
769 int layno, reslevelno, compno, precno;
772 for (compno = 0; compno < s->
ncomponents; compno++) {
784 for (y = 0; y < s->
height; y += 256) {
788 for (x = 0; x < s->
width; x += 256) {
789 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
809 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
862 int bpno,
int bandno,
int bpass_csty_symbol,
863 int vert_causal_ctx_csty_symbol)
865 int mask = 3 << (bpno - 1), y0, x, y;
867 for (y0 = 0; y0 <
height; y0 += 4)
868 for (x = 0; x <
width; x++)
869 for (y = y0; y < height && y < y0 + 4; y++) {
873 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
877 if (bpass_csty_symbol)
897 phalf = 1 << (bpno - 1);
900 for (y0 = 0; y0 <
height; y0 += 4)
901 for (x = 0; x <
width; x++)
902 for (y = y0; y < height && y < y0 + 4; y++)
908 t1->
data[y][x] += t1->
data[y][x] < 0 ? -
r :
r;
915 int seg_symbols,
int vert_causal_ctx_csty_symbol)
917 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
919 for (y0 = 0; y0 <
height; y0 += 4) {
920 for (x = 0; x <
width; x++) {
921 if (y0 + 3 < height &&
939 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
943 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
972 "Segmentation symbol value incorrect\n");
985 for (y = 0; y <
height; y++)
986 memset(t1->
data[y], 0, width *
sizeof(**t1->
data));
991 for (y = 0; y < height + 2; y++)
992 memset(t1->
flags[y], 0, (width + 2) *
sizeof(**t1->
flags));
1002 bpass_csty_symbol && (clnpass_cnt >= 4),
1003 vert_causal_ctx_csty_symbol);
1007 if (bpass_csty_symbol && clnpass_cnt >= 4)
1013 vert_causal_ctx_csty_symbol);
1014 clnpass_cnt = clnpass_cnt + 1;
1015 if (bpass_csty_symbol && clnpass_cnt >= 4)
1041 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1042 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1043 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1045 for (i = 0; i < w; ++i)
1056 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1057 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1060 for (i = 0; i < w; ++i)
1061 datap[i] = (src[i] * band->
i_stepsize + (1 << 15)) >> 16;
1070 for (i = 0; i < 3; i++)
1076 for (i = 0; i < 2; i++)
1086 int compno, reslevelno, bandno;
1090 for (compno = 0; compno < s->
ncomponents; compno++) {
1097 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1098 uint16_t nb_precincts, precno;
1100 int cblkno = 0, bandpos;
1101 bandpos = bandno + (reslevelno > 0);
1103 if (band->
coord[0][0] == band->
coord[0][1] ||
1109 for (precno = 0; precno < nb_precincts; precno++) {
1123 x = cblk->
coord[0][0];
1124 y = cblk->
coord[1][0];
1140 #define WRITE_FRAME(D, PIXEL) \ 1141 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \ 1142 AVFrame * picture) \ 1144 int linesize = picture->linesize[0] / sizeof(PIXEL); \ 1148 for (compno = 0; compno < s->ncomponents; compno++) { \ 1149 Jpeg2000Component *comp = tile->comp + compno; \ 1150 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \ 1152 float *datap = comp->f_data; \ 1153 int32_t *i_datap = comp->i_data; \ 1154 int cbps = s->cbps[compno]; \ 1155 int w = tile->comp[compno].coord[0][1] - s->image_offset_x; \ 1157 y = tile->comp[compno].coord[1][0] - s->image_offset_y; \ 1158 line = (PIXEL *)picture->data[0] + y * linesize; \ 1159 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) { \ 1162 x = tile->comp[compno].coord[0][0] - s->image_offset_x; \ 1163 dst = line + x * s->ncomponents + compno; \ 1165 if (codsty->transform == FF_DWT97) { \ 1166 for (; x < w; x += s->cdx[compno]) { \ 1167 int val = lrintf(*datap) + (1 << (cbps - 1)); \ 1169 val = av_clip(val, 0, (1 << cbps) - 1); \ 1170 *dst = val << (8 * sizeof(PIXEL) - cbps); \ 1172 dst += s->ncomponents; \ 1175 for (; x < w; x += s->cdx[compno]) { \ 1176 int val = *i_datap + (1 << (cbps - 1)); \ 1178 val = av_clip(val, 0, (1 << cbps) - 1); \ 1179 *dst = val << (8 * sizeof(PIXEL) - cbps); \ 1181 dst += s->ncomponents; \ 1205 write_frame_8(s, tile, picture);
1207 write_frame_16(s, tile, picture);
1217 for (compno = 0; compno < s->
ncomponents; compno++) {
1245 marker = bytestream2_get_be16u(&s->
g);
1271 len = bytestream2_get_be16u(&s->
g);
1280 ret =
get_coc(s, codsty, properties);
1283 ret =
get_cod(s, codsty, properties);
1286 ret =
get_qcc(s, len, qntsty, properties);
1289 ret =
get_qcd(s, len, qntsty, properties);
1292 if (!(ret =
get_sot(s, len))) {
1312 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
1319 "error during processing marker segment %.4"PRIx16
"\n",
1321 return ret ? ret : -1;
1349 uint32_t atom_size, atom;
1350 int found_codestream = 0, search_range = 10;
1352 while(!found_codestream && search_range
1355 atom_size = bytestream2_get_be32u(&s->
g);
1356 atom = bytestream2_get_be32u(&s->
g);
1358 found_codestream = 1;
1367 if (found_codestream)
1400 (bytestream2_get_be32u(&s->
g) == 12) &&
1405 "Could not find Jpeg2000 codestream atom.\n");
1452 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x) 1453 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM 1456 {
"lowres",
"Lower the decoding resolution by a power of two",
1461 static const AVClass class = {
1478 .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
void(* mct_decode[FF_DWT_NB])(void *src0, void *src1, void *src2, int csize)
#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 int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
Jpeg2000QuantStyle qntsty[4]
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
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)
static int decode_pgod_cprl(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
#define FF_ARRAY_ELEMS(a)
av_cold void ff_jpeg2000dsp_init(Jpeg2000DSPContext *c)
static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
static int init_tile(Jpeg2000DecoderContext *s, int tileno)
Jpeg2000CodingStyle codsty[4]
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 void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define WRITE_FRAME(D, PIXEL)
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 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)
int flags
AV_CODEC_FLAG_*.
const char * name
Name of the codec implementation.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
static int decode_pgod_lrcp(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
enum AVPictureType pict_type
Picture type of the frame.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_PIX_FMT_GRAY16
struct Jpeg2000TgtNode * parent
int width
picture width / height.
JPEG 2000 structures and defines common to encoder and decoder.
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
#define JPEG2000_MAX_RESLEVELS
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)
#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 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 const AVProfile profiles[]
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 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
Jpeg2000QuantStyle qntsty[4]
int ff_dwt_decode(DWTContext *s, void *t)
static av_cold int init(AVCodecParserContext *s)
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 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 ...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Jpeg2000CodingStyle codsty[4]
static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width, int height, int bandpos)
const AVProfile ff_jpeg2000_profiles[]
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)