00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028
00029
00030 #include "avcodec.h"
00031 #include "bytestream.h"
00032 #include "j2k.h"
00033 #include "libavutil/common.h"
00034
00035 #define JP2_SIG_TYPE 0x6A502020
00036 #define JP2_SIG_VALUE 0x0D0A870A
00037 #define JP2_CODESTREAM 0x6A703263
00038
00039 #define HAD_COC 0x01
00040 #define HAD_QCC 0x02
00041
00042 typedef struct {
00043 J2kComponent *comp;
00044 uint8_t properties[4];
00045 J2kCodingStyle codsty[4];
00046 J2kQuantStyle qntsty[4];
00047 } J2kTile;
00048
00049 typedef struct {
00050 AVCodecContext *avctx;
00051 AVFrame picture;
00052 GetByteContext g;
00053
00054 int width, height;
00055 int image_offset_x, image_offset_y;
00056 int tile_offset_x, tile_offset_y;
00057 uint8_t cbps[4];
00058 uint8_t sgnd[4];
00059 uint8_t properties[4];
00060 int cdx[4], cdy[4];
00061 int precision;
00062 int ncomponents;
00063 int tile_width, tile_height;
00064 int numXtiles, numYtiles;
00065 int maxtilelen;
00066
00067 J2kCodingStyle codsty[4];
00068 J2kQuantStyle qntsty[4];
00069
00070 int bit_index;
00071
00072 int16_t curtileno;
00073
00074 J2kTile *tile;
00075 } J2kDecoderContext;
00076
00077 static int get_bits(J2kDecoderContext *s, int n)
00078 {
00079 int res = 0;
00080
00081 while (--n >= 0){
00082 res <<= 1;
00083 if (s->bit_index == 0) {
00084 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
00085 }
00086 s->bit_index--;
00087 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
00088 }
00089 return res;
00090 }
00091
00092 static void j2k_flush(J2kDecoderContext *s)
00093 {
00094 if (bytestream2_get_byte(&s->g) == 0xff)
00095 bytestream2_skip(&s->g, 1);
00096 s->bit_index = 8;
00097 }
00098 #if 0
00099 void printcomp(J2kComponent *comp)
00100 {
00101 int i;
00102 for (i = 0; i < comp->y1 - comp->y0; i++)
00103 ff_j2k_printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
00104 }
00105
00106 static void nspaces(FILE *fd, int n)
00107 {
00108 while(n--) putc(' ', fd);
00109 }
00110
00111 static void dump(J2kDecoderContext *s, FILE *fd)
00112 {
00113 int tileno, compno, reslevelno, bandno, precno;
00114 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
00115 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
00116 "tiles:\n",
00117 s->width, s->height, s->tile_width, s->tile_height,
00118 s->numXtiles, s->numYtiles, s->ncomponents);
00119 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00120 J2kTile *tile = s->tile + tileno;
00121 nspaces(fd, 2);
00122 fprintf(fd, "tile %d:\n", tileno);
00123 for(compno = 0; compno < s->ncomponents; compno++){
00124 J2kComponent *comp = tile->comp + compno;
00125 nspaces(fd, 4);
00126 fprintf(fd, "component %d:\n", compno);
00127 nspaces(fd, 4);
00128 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
00129 comp->x0, comp->x1, comp->y0, comp->y1);
00130 for(reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00131 J2kResLevel *reslevel = comp->reslevel + reslevelno;
00132 nspaces(fd, 6);
00133 fprintf(fd, "reslevel %d:\n", reslevelno);
00134 nspaces(fd, 6);
00135 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
00136 reslevel->x0, reslevel->x1, reslevel->y0,
00137 reslevel->y1, reslevel->nbands);
00138 for(bandno = 0; bandno < reslevel->nbands; bandno++){
00139 J2kBand *band = reslevel->band + bandno;
00140 nspaces(fd, 8);
00141 fprintf(fd, "band %d:\n", bandno);
00142 nspaces(fd, 8);
00143 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
00144 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
00145 band->x0, band->x1,
00146 band->y0, band->y1,
00147 band->codeblock_width, band->codeblock_height,
00148 band->cblknx, band->cblkny);
00149 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
00150 J2kPrec *prec = band->prec + precno;
00151 nspaces(fd, 10);
00152 fprintf(fd, "prec %d:\n", precno);
00153 nspaces(fd, 10);
00154 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
00155 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
00156 }
00157 }
00158 }
00159 }
00160 }
00161 }
00162 #endif
00163
00165 static int tag_tree_decode(J2kDecoderContext *s, J2kTgtNode *node, int threshold)
00166 {
00167 J2kTgtNode *stack[30];
00168 int sp = -1, curval = 0;
00169
00170 while(node && !node->vis){
00171 stack[++sp] = node;
00172 node = node->parent;
00173 }
00174
00175 if (node)
00176 curval = node->val;
00177 else
00178 curval = stack[sp]->val;
00179
00180 while(curval < threshold && sp >= 0){
00181 if (curval < stack[sp]->val)
00182 curval = stack[sp]->val;
00183 while (curval < threshold){
00184 int ret;
00185 if ((ret = get_bits(s, 1)) > 0){
00186 stack[sp]->vis++;
00187 break;
00188 } else if (!ret)
00189 curval++;
00190 else
00191 return ret;
00192 }
00193 stack[sp]->val = curval;
00194 sp--;
00195 }
00196 return curval;
00197 }
00198
00199
00201 static int get_siz(J2kDecoderContext *s)
00202 {
00203 int i, ret;
00204
00205 if (bytestream2_get_bytes_left(&s->g) < 36)
00206 return AVERROR(EINVAL);
00207
00208 bytestream2_get_be16u(&s->g);
00209 s->width = bytestream2_get_be32u(&s->g);
00210 s->height = bytestream2_get_be32u(&s->g);
00211 s->image_offset_x = bytestream2_get_be32u(&s->g);
00212 s->image_offset_y = bytestream2_get_be32u(&s->g);
00213
00214 s->tile_width = bytestream2_get_be32u(&s->g);
00215 s->tile_height = bytestream2_get_be32u(&s->g);
00216 s->tile_offset_x = bytestream2_get_be32u(&s->g);
00217 s->tile_offset_y = bytestream2_get_be32u(&s->g);
00218 s->ncomponents = bytestream2_get_be16u(&s->g);
00219
00220 if(s->ncomponents <= 0 || s->ncomponents > 4) {
00221 av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
00222 return AVERROR(EINVAL);
00223 }
00224 if(s->tile_width<=0 || s->tile_height<=0)
00225 return AVERROR(EINVAL);
00226
00227 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
00228 return AVERROR(EINVAL);
00229
00230 for (i = 0; i < s->ncomponents; i++){
00231 uint8_t x = bytestream2_get_byteu(&s->g);
00232 s->cbps[i] = (x & 0x7f) + 1;
00233 s->precision = FFMAX(s->cbps[i], s->precision);
00234 s->sgnd[i] = !!(x & 0x80);
00235 s->cdx[i] = bytestream2_get_byteu(&s->g);
00236 s->cdy[i] = bytestream2_get_byteu(&s->g);
00237 }
00238
00239 s->numXtiles = ff_j2k_ceildiv(s->width - s->tile_offset_x, s->tile_width);
00240 s->numYtiles = ff_j2k_ceildiv(s->height - s->tile_offset_y, s->tile_height);
00241
00242 if(s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(J2kTile))
00243 return AVERROR(EINVAL);
00244
00245 s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(J2kTile));
00246 if (!s->tile)
00247 return AVERROR(ENOMEM);
00248
00249 for (i = 0; i < s->numXtiles * s->numYtiles; i++){
00250 J2kTile *tile = s->tile + i;
00251
00252 tile->comp = av_mallocz(s->ncomponents * sizeof(J2kComponent));
00253 if (!tile->comp)
00254 return AVERROR(ENOMEM);
00255 }
00256
00257 s->avctx->width = s->width - s->image_offset_x;
00258 s->avctx->height = s->height - s->image_offset_y;
00259
00260 switch(s->ncomponents){
00261 case 1:
00262 if (s->precision > 8) {
00263 s->avctx->pix_fmt = PIX_FMT_GRAY16;
00264 } else {
00265 s->avctx->pix_fmt = PIX_FMT_GRAY8;
00266 }
00267 break;
00268 case 3:
00269 if (s->precision > 8) {
00270 s->avctx->pix_fmt = PIX_FMT_RGB48;
00271 } else {
00272 s->avctx->pix_fmt = PIX_FMT_RGB24;
00273 }
00274 break;
00275 case 4:
00276 s->avctx->pix_fmt = PIX_FMT_RGBA;
00277 break;
00278 }
00279
00280 if (s->picture.data[0])
00281 s->avctx->release_buffer(s->avctx, &s->picture);
00282
00283 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0)
00284 return ret;
00285
00286 s->picture.pict_type = AV_PICTURE_TYPE_I;
00287 s->picture.key_frame = 1;
00288
00289 return 0;
00290 }
00291
00293 static int get_cox(J2kDecoderContext *s, J2kCodingStyle *c)
00294 {
00295 if (bytestream2_get_bytes_left(&s->g) < 5)
00296 return AVERROR(EINVAL);
00297 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
00298 c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2;
00299 c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2;
00300
00301 c->cblk_style = bytestream2_get_byteu(&s->g);
00302 if (c->cblk_style != 0){
00303 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
00304 }
00305 c->transform = bytestream2_get_byteu(&s->g);
00306 if (c->csty & J2K_CSTY_PREC) {
00307 int i;
00308
00309 for (i = 0; i < c->nreslevels; i++)
00310 bytestream2_get_byte(&s->g);
00311 }
00312 return 0;
00313 }
00314
00316 static int get_cod(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
00317 {
00318 J2kCodingStyle tmp;
00319 int compno;
00320
00321 if (bytestream2_get_bytes_left(&s->g) < 5)
00322 return AVERROR(EINVAL);
00323
00324 tmp.log2_prec_width =
00325 tmp.log2_prec_height = 15;
00326
00327 tmp.csty = bytestream2_get_byteu(&s->g);
00328
00329 if (bytestream2_get_byteu(&s->g)){
00330 av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
00331 return -1;
00332 }
00333
00334 tmp.nlayers = bytestream2_get_be16u(&s->g);
00335 tmp.mct = bytestream2_get_byteu(&s->g);
00336
00337 get_cox(s, &tmp);
00338 for (compno = 0; compno < s->ncomponents; compno++){
00339 if (!(properties[compno] & HAD_COC))
00340 memcpy(c + compno, &tmp, sizeof(J2kCodingStyle));
00341 }
00342 return 0;
00343 }
00344
00346 static int get_coc(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
00347 {
00348 int compno;
00349
00350 if (bytestream2_get_bytes_left(&s->g) < 2)
00351 return AVERROR(EINVAL);
00352
00353 compno = bytestream2_get_byteu(&s->g);
00354
00355 c += compno;
00356 c->csty = bytestream2_get_byte(&s->g);
00357 get_cox(s, c);
00358
00359 properties[compno] |= HAD_COC;
00360 return 0;
00361 }
00362
00364 static int get_qcx(J2kDecoderContext *s, int n, J2kQuantStyle *q)
00365 {
00366 int i, x;
00367
00368 if (bytestream2_get_bytes_left(&s->g) < 1)
00369 return AVERROR(EINVAL);
00370
00371 x = bytestream2_get_byteu(&s->g);
00372
00373 q->nguardbits = x >> 5;
00374 q->quantsty = x & 0x1f;
00375
00376 if (q->quantsty == J2K_QSTY_NONE){
00377 n -= 3;
00378 if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
00379 return AVERROR(EINVAL);
00380 for (i = 0; i < n; i++)
00381 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
00382 } else if (q->quantsty == J2K_QSTY_SI){
00383 if (bytestream2_get_bytes_left(&s->g) < 2)
00384 return AVERROR(EINVAL);
00385 x = bytestream2_get_be16u(&s->g);
00386 q->expn[0] = x >> 11;
00387 q->mant[0] = x & 0x7ff;
00388 for (i = 1; i < 32 * 3; i++){
00389 int curexpn = FFMAX(0, q->expn[0] - (i-1)/3);
00390 q->expn[i] = curexpn;
00391 q->mant[i] = q->mant[0];
00392 }
00393 } else{
00394 n = (n - 3) >> 1;
00395 if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
00396 return AVERROR(EINVAL);
00397 for (i = 0; i < n; i++){
00398 x = bytestream2_get_be16u(&s->g);
00399 q->expn[i] = x >> 11;
00400 q->mant[i] = x & 0x7ff;
00401 }
00402 }
00403 return 0;
00404 }
00405
00407 static int get_qcd(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
00408 {
00409 J2kQuantStyle tmp;
00410 int compno;
00411
00412 if (get_qcx(s, n, &tmp))
00413 return -1;
00414 for (compno = 0; compno < s->ncomponents; compno++)
00415 if (!(properties[compno] & HAD_QCC))
00416 memcpy(q + compno, &tmp, sizeof(J2kQuantStyle));
00417 return 0;
00418 }
00419
00421 static int get_qcc(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
00422 {
00423 int compno;
00424
00425 if (bytestream2_get_bytes_left(&s->g) < 1)
00426 return AVERROR(EINVAL);
00427
00428 compno = bytestream2_get_byteu(&s->g);
00429 properties[compno] |= HAD_QCC;
00430 return get_qcx(s, n-1, q+compno);
00431 }
00432
00434 static uint8_t get_sot(J2kDecoderContext *s)
00435 {
00436 if (bytestream2_get_bytes_left(&s->g) < 8)
00437 return AVERROR(EINVAL);
00438
00439 s->curtileno = bytestream2_get_be16u(&s->g);
00440 if((unsigned)s->curtileno >= s->numXtiles * s->numYtiles){
00441 s->curtileno=0;
00442 return AVERROR(EINVAL);
00443 }
00444
00445 bytestream2_skipu(&s->g, 4);
00446
00447 if (!bytestream2_get_byteu(&s->g)){
00448 J2kTile *tile = s->tile + s->curtileno;
00449
00450
00451 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(J2kCodingStyle));
00452 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(J2kQuantStyle));
00453 }
00454 bytestream2_get_byteu(&s->g);
00455
00456 return 0;
00457 }
00458
00459 static int init_tile(J2kDecoderContext *s, int tileno)
00460 {
00461 int compno,
00462 tilex = tileno % s->numXtiles,
00463 tiley = tileno / s->numXtiles;
00464 J2kTile *tile = s->tile + tileno;
00465
00466 if (!tile->comp)
00467 return AVERROR(ENOMEM);
00468 for (compno = 0; compno < s->ncomponents; compno++){
00469 J2kComponent *comp = tile->comp + compno;
00470 J2kCodingStyle *codsty = tile->codsty + compno;
00471 J2kQuantStyle *qntsty = tile->qntsty + compno;
00472 int ret;
00473
00474 comp->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
00475 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
00476 comp->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
00477 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
00478
00479 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno]))
00480 return ret;
00481 }
00482 return 0;
00483 }
00484
00486 static int getnpasses(J2kDecoderContext *s)
00487 {
00488 int num;
00489 if (!get_bits(s, 1))
00490 return 1;
00491 if (!get_bits(s, 1))
00492 return 2;
00493 if ((num = get_bits(s, 2)) != 3)
00494 return num < 0 ? num : 3 + num;
00495 if ((num = get_bits(s, 5)) != 31)
00496 return num < 0 ? num : 6 + num;
00497 num = get_bits(s, 7);
00498 return num < 0 ? num : 37 + num;
00499 }
00500
00501 static int getlblockinc(J2kDecoderContext *s)
00502 {
00503 int res = 0, ret;
00504 while (ret = get_bits(s, 1)){
00505 if (ret < 0)
00506 return ret;
00507 res++;
00508 }
00509 return res;
00510 }
00511
00512 static int decode_packet(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kResLevel *rlevel, int precno,
00513 int layno, uint8_t *expn, int numgbits)
00514 {
00515 int bandno, cblkny, cblknx, cblkno, ret;
00516
00517 if (!(ret = get_bits(s, 1))){
00518 j2k_flush(s);
00519 return 0;
00520 } else if (ret < 0)
00521 return ret;
00522
00523 for (bandno = 0; bandno < rlevel->nbands; bandno++){
00524 J2kBand *band = rlevel->band + bandno;
00525 J2kPrec *prec = band->prec + precno;
00526 int pos = 0;
00527
00528 if (band->coord[0][0] == band->coord[0][1]
00529 || band->coord[1][0] == band->coord[1][1])
00530 continue;
00531
00532 for (cblkny = prec->yi0; cblkny < prec->yi1; cblkny++)
00533 for(cblknx = prec->xi0, cblkno = cblkny * band->cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++){
00534 J2kCblk *cblk = band->cblk + cblkno;
00535 int incl, newpasses, llen;
00536
00537 if (cblk->npasses)
00538 incl = get_bits(s, 1);
00539 else
00540 incl = tag_tree_decode(s, prec->cblkincl + pos, layno+1) == layno;
00541 if (!incl)
00542 continue;
00543 else if (incl < 0)
00544 return incl;
00545
00546 if (!cblk->npasses)
00547 cblk->nonzerobits = expn[bandno] + numgbits - 1 - tag_tree_decode(s, prec->zerobits + pos, 100);
00548 if ((newpasses = getnpasses(s)) < 0)
00549 return newpasses;
00550 if ((llen = getlblockinc(s)) < 0)
00551 return llen;
00552 cblk->lblock += llen;
00553 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
00554 return ret;
00555 cblk->lengthinc = ret;
00556 cblk->npasses += newpasses;
00557 }
00558 }
00559 j2k_flush(s);
00560
00561 if (codsty->csty & J2K_CSTY_EPH) {
00562 if (bytestream2_peek_be16(&s->g) == J2K_EPH) {
00563 bytestream2_skip(&s->g, 2);
00564 } else {
00565 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
00566 }
00567 }
00568
00569 for (bandno = 0; bandno < rlevel->nbands; bandno++){
00570 J2kBand *band = rlevel->band + bandno;
00571 int yi, cblknw = band->prec[precno].xi1 - band->prec[precno].xi0;
00572 for (yi = band->prec[precno].yi0; yi < band->prec[precno].yi1; yi++){
00573 int xi;
00574 for (xi = band->prec[precno].xi0; xi < band->prec[precno].xi1; xi++){
00575 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
00576 if (bytestream2_get_bytes_left(&s->g) < cblk->lengthinc)
00577 return AVERROR(EINVAL);
00578 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
00579 cblk->length += cblk->lengthinc;
00580 cblk->lengthinc = 0;
00581 }
00582 }
00583 }
00584 return 0;
00585 }
00586
00587 static int decode_packets(J2kDecoderContext *s, J2kTile *tile)
00588 {
00589 int layno, reslevelno, compno, precno, ok_reslevel;
00590 s->bit_index = 8;
00591 for (layno = 0; layno < tile->codsty[0].nlayers; layno++){
00592 ok_reslevel = 1;
00593 for (reslevelno = 0; ok_reslevel; reslevelno++){
00594 ok_reslevel = 0;
00595 for (compno = 0; compno < s->ncomponents; compno++){
00596 J2kCodingStyle *codsty = tile->codsty + compno;
00597 J2kQuantStyle *qntsty = tile->qntsty + compno;
00598 if (reslevelno < codsty->nreslevels){
00599 J2kResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
00600 ok_reslevel = 1;
00601 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
00602 if (decode_packet(s, codsty, rlevel, precno, layno, qntsty->expn +
00603 (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->nguardbits))
00604 return -1;
00605 }
00606 }
00607 }
00608 }
00609 }
00610 return 0;
00611 }
00612
00613
00614 static void decode_sigpass(J2kT1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol,
00615 int vert_causal_ctx_csty_symbol)
00616 {
00617 int mask = 3 << (bpno - 1), y0, x, y;
00618
00619 for (y0 = 0; y0 < height; y0 += 4)
00620 for (x = 0; x < width; x++)
00621 for (y = y0; y < height && y < y0+4; y++){
00622 if ((t1->flags[y+1][x+1] & J2K_T1_SIG_NB)
00623 && !(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
00624 int vert_causal_ctx_csty_loc_symbol = vert_causal_ctx_csty_symbol && (x == 3 && y == 3);
00625 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno,
00626 vert_causal_ctx_csty_loc_symbol))){
00627 int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00628 if (bpass_csty_symbol)
00629 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
00630 else
00631 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
00632 -mask : mask;
00633
00634 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
00635 }
00636 t1->flags[y+1][x+1] |= J2K_T1_VIS;
00637 }
00638 }
00639 }
00640
00641 static void decode_refpass(J2kT1Context *t1, int width, int height, int bpno)
00642 {
00643 int phalf, nhalf;
00644 int y0, x, y;
00645
00646 phalf = 1 << (bpno - 1);
00647 nhalf = -phalf;
00648
00649 for (y0 = 0; y0 < height; y0 += 4)
00650 for (x = 0; x < width; x++)
00651 for (y = y0; y < height && y < y0+4; y++){
00652 if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
00653 int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
00654 int r = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? phalf : nhalf;
00655 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
00656 t1->flags[y+1][x+1] |= J2K_T1_REF;
00657 }
00658 }
00659 }
00660
00661 static void decode_clnpass(J2kDecoderContext *s, J2kT1Context *t1, int width, int height,
00662 int bpno, int bandno, int seg_symbols)
00663 {
00664 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
00665
00666 for (y0 = 0; y0 < height; y0 += 4) {
00667 for (x = 0; x < width; x++){
00668 if (y0 + 3 < height && !(
00669 (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00670 (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00671 (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00672 (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)))){
00673 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
00674 continue;
00675 runlen = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
00676 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
00677 dec = 1;
00678 } else{
00679 runlen = 0;
00680 dec = 0;
00681 }
00682
00683 for (y = y0 + runlen; y < y0 + 4 && y < height; y++){
00684 if (!dec){
00685 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)))
00686 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1],
00687 bandno, 0));
00688 }
00689 if (dec){
00690 int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00691 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ? -mask : mask;
00692 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
00693 }
00694 dec = 0;
00695 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
00696 }
00697 }
00698 }
00699 if (seg_symbols) {
00700 int val;
00701 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
00702 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
00703 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
00704 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
00705 if (val != 0xa) {
00706 av_log(s->avctx, AV_LOG_ERROR,"Segmentation symbol value incorrect\n");
00707 }
00708 }
00709 }
00710
00711 static int decode_cblk(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kT1Context *t1, J2kCblk *cblk,
00712 int width, int height, int bandpos)
00713 {
00714 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
00715 int bpass_csty_symbol = J2K_CBLK_BYPASS & codsty->cblk_style;
00716 int vert_causal_ctx_csty_symbol = J2K_CBLK_VSC & codsty->cblk_style;
00717
00718 for (y = 0; y < height+2; y++)
00719 memset(t1->flags[y], 0, (width+2)*sizeof(int));
00720
00721 for (y = 0; y < height; y++)
00722 memset(t1->data[y], 0, width*sizeof(int));
00723
00724 cblk->data[cblk->length] = 0xff;
00725 cblk->data[cblk->length+1] = 0xff;
00726 ff_mqc_initdec(&t1->mqc, cblk->data);
00727
00728 while(passno--){
00729 switch(pass_t){
00730 case 0: decode_sigpass(t1, width, height, bpno+1, bandpos,
00731 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
00732 break;
00733 case 1: decode_refpass(t1, width, height, bpno+1);
00734 if (bpass_csty_symbol && clnpass_cnt >= 4)
00735 ff_mqc_initdec(&t1->mqc, cblk->data);
00736 break;
00737 case 2: decode_clnpass(s, t1, width, height, bpno+1, bandpos,
00738 codsty->cblk_style & J2K_CBLK_SEGSYM);
00739 clnpass_cnt = clnpass_cnt + 1;
00740 if (bpass_csty_symbol && clnpass_cnt >= 4)
00741 ff_mqc_initdec(&t1->mqc, cblk->data);
00742 break;
00743 }
00744
00745 pass_t++;
00746 if (pass_t == 3){
00747 bpno--;
00748 pass_t = 0;
00749 }
00750 }
00751 return 0;
00752 }
00753
00754 static void mct_decode(J2kDecoderContext *s, J2kTile *tile)
00755 {
00756 int i, *src[3], i0, i1, i2, csize = 1;
00757
00758 for (i = 0; i < 3; i++)
00759 src[i] = tile->comp[i].data;
00760
00761 for (i = 0; i < 2; i++)
00762 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
00763
00764 if (tile->codsty[0].transform == FF_DWT97){
00765 for (i = 0; i < csize; i++){
00766 i0 = *src[0] + (*src[2] * 46802 >> 16);
00767 i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
00768 i2 = *src[0] + (116130 * *src[1] >> 16);
00769 *src[0]++ = i0;
00770 *src[1]++ = i1;
00771 *src[2]++ = i2;
00772 }
00773 } else{
00774 for (i = 0; i < csize; i++){
00775 i1 = *src[0] - (*src[2] + *src[1] >> 2);
00776 i0 = i1 + *src[2];
00777 i2 = i1 + *src[1];
00778 *src[0]++ = i0;
00779 *src[1]++ = i1;
00780 *src[2]++ = i2;
00781 }
00782 }
00783 }
00784
00785 static int decode_tile(J2kDecoderContext *s, J2kTile *tile)
00786 {
00787 int compno, reslevelno, bandno;
00788 int x, y, *src[4];
00789 uint8_t *line;
00790 J2kT1Context t1;
00791
00792 for (compno = 0; compno < s->ncomponents; compno++){
00793 J2kComponent *comp = tile->comp + compno;
00794 J2kCodingStyle *codsty = tile->codsty + compno;
00795
00796 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00797 J2kResLevel *rlevel = comp->reslevel + reslevelno;
00798 for (bandno = 0; bandno < rlevel->nbands; bandno++){
00799 J2kBand *band = rlevel->band + bandno;
00800 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
00801
00802 bandpos = bandno + (reslevelno > 0);
00803
00804 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
00805 y0 = yy0;
00806 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
00807 band->coord[1][1]) - band->coord[1][0] + yy0;
00808
00809 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
00810 continue;
00811
00812 for (cblky = 0; cblky < band->cblkny; cblky++){
00813 if (reslevelno == 0 || bandno == 1)
00814 xx0 = 0;
00815 else
00816 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
00817 x0 = xx0;
00818 xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
00819 band->coord[0][1]) - band->coord[0][0] + xx0;
00820
00821 for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
00822 int y, x;
00823 decode_cblk(s, codsty, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
00824 if (codsty->transform == FF_DWT53){
00825 for (y = yy0; y < yy1; y+=s->cdy[compno]){
00826 int *ptr = t1.data[y-yy0];
00827 for (x = xx0; x < xx1; x+=s->cdx[compno]){
00828 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
00829 }
00830 }
00831 } else{
00832 for (y = yy0; y < yy1; y+=s->cdy[compno]){
00833 int *ptr = t1.data[y-yy0];
00834 for (x = xx0; x < xx1; x+=s->cdx[compno]){
00835 int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
00836 tmp2 = FFABS(tmp>>1) + FFABS(tmp&1);
00837 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
00838 }
00839 }
00840 }
00841 xx0 = xx1;
00842 xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
00843 }
00844 yy0 = yy1;
00845 yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
00846 }
00847 }
00848 }
00849 ff_j2k_dwt_decode(&comp->dwt, comp->data);
00850 src[compno] = comp->data;
00851 }
00852 if (tile->codsty[0].mct)
00853 mct_decode(s, tile);
00854
00855 if (s->precision <= 8) {
00856 for (compno = 0; compno < s->ncomponents; compno++){
00857 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
00858 line = s->picture.data[0] + y * s->picture.linesize[0];
00859 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]){
00860 uint8_t *dst;
00861
00862 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
00863 dst = line + x * s->ncomponents + compno;
00864
00865 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
00866 *src[compno] += 1 << (s->cbps[compno]-1);
00867 if (*src[compno] < 0)
00868 *src[compno] = 0;
00869 else if (*src[compno] >= (1 << s->cbps[compno]))
00870 *src[compno] = (1 << s->cbps[compno]) - 1;
00871 *dst = *src[compno]++;
00872 dst += s->ncomponents;
00873 }
00874 line += s->picture.linesize[0];
00875 }
00876 }
00877 } else {
00878 for (compno = 0; compno < s->ncomponents; compno++) {
00879 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
00880 line = s->picture.data[0] + y * s->picture.linesize[0];
00881 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
00882 uint16_t *dst;
00883
00884 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
00885 dst = (uint16_t *)(line + (x * s->ncomponents + compno) * 2);
00886 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
00887 int32_t val;
00888
00889 val = *src[compno]++ << (16 - s->cbps[compno]);
00890 val += 1 << 15;
00891 val = av_clip(val, 0, (1 << 16) - 1);
00892 *dst = val;
00893 dst += s->ncomponents;
00894 }
00895 line += s->picture.linesize[0];
00896 }
00897 }
00898 }
00899 return 0;
00900 }
00901
00902 static void cleanup(J2kDecoderContext *s)
00903 {
00904 int tileno, compno;
00905 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00906 for (compno = 0; compno < s->ncomponents; compno++){
00907 J2kComponent *comp = s->tile[tileno].comp + compno;
00908 J2kCodingStyle *codsty = s->tile[tileno].codsty + compno;
00909
00910 ff_j2k_cleanup(comp, codsty);
00911 }
00912 av_freep(&s->tile[tileno].comp);
00913 }
00914 av_freep(&s->tile);
00915 }
00916
00917 static int decode_codestream(J2kDecoderContext *s)
00918 {
00919 J2kCodingStyle *codsty = s->codsty;
00920 J2kQuantStyle *qntsty = s->qntsty;
00921 uint8_t *properties = s->properties;
00922
00923 for (;;){
00924 int oldpos, marker, len, ret = 0;
00925
00926 if (bytestream2_get_bytes_left(&s->g) < 2){
00927 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
00928 break;
00929 }
00930
00931 marker = bytestream2_get_be16u(&s->g);
00932 av_dlog(s->avctx, "marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
00933 oldpos = bytestream2_tell(&s->g);
00934
00935 if (marker == J2K_SOD){
00936 J2kTile *tile = s->tile + s->curtileno;
00937 if (ret = init_tile(s, s->curtileno)) {
00938 av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
00939 return ret;
00940 }
00941 if (ret = decode_packets(s, tile)) {
00942 av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
00943 return ret;
00944 }
00945 continue;
00946 }
00947 if (marker == J2K_EOC)
00948 break;
00949
00950 if (bytestream2_get_bytes_left(&s->g) < 2)
00951 return AVERROR(EINVAL);
00952 len = bytestream2_get_be16u(&s->g);
00953 switch (marker){
00954 case J2K_SIZ:
00955 ret = get_siz(s);
00956 break;
00957 case J2K_COC:
00958 ret = get_coc(s, codsty, properties);
00959 break;
00960 case J2K_COD:
00961 ret = get_cod(s, codsty, properties);
00962 break;
00963 case J2K_QCC:
00964 ret = get_qcc(s, len, qntsty, properties);
00965 break;
00966 case J2K_QCD:
00967 ret = get_qcd(s, len, qntsty, properties);
00968 break;
00969 case J2K_SOT:
00970 if (!(ret = get_sot(s))){
00971 codsty = s->tile[s->curtileno].codsty;
00972 qntsty = s->tile[s->curtileno].qntsty;
00973 properties = s->tile[s->curtileno].properties;
00974 }
00975 break;
00976 case J2K_COM:
00977
00978 bytestream2_skip(&s->g, len - 2);
00979 break;
00980 default:
00981 av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
00982 bytestream2_skip(&s->g, len - 2);
00983 break;
00984 }
00985 if (bytestream2_tell(&s->g) - oldpos != len || ret){
00986 av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker);
00987 return ret ? ret : -1;
00988 }
00989 }
00990 return 0;
00991 }
00992
00993 static int jp2_find_codestream(J2kDecoderContext *s)
00994 {
00995 uint32_t atom_size, atom;
00996 int found_codestream = 0, search_range = 10;
00997
00998 while(!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
00999 atom_size = bytestream2_get_be32u(&s->g);
01000 atom = bytestream2_get_be32u(&s->g);
01001 if (atom == JP2_CODESTREAM) {
01002 found_codestream = 1;
01003 } else {
01004 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
01005 return 0;
01006 bytestream2_skipu(&s->g, atom_size - 8);
01007 search_range--;
01008 }
01009 }
01010
01011 if (found_codestream)
01012 return 1;
01013 return 0;
01014 }
01015
01016 static int decode_frame(AVCodecContext *avctx,
01017 void *data, int *data_size,
01018 AVPacket *avpkt)
01019 {
01020 J2kDecoderContext *s = avctx->priv_data;
01021 AVFrame *picture = data;
01022 int tileno, ret;
01023
01024 bytestream2_init(&s->g, avpkt->data, avpkt->size);
01025 s->curtileno = -1;
01026
01027 if (bytestream2_get_bytes_left(&s->g) < 2) {
01028 ret = AVERROR(EINVAL);
01029 goto err_out;
01030 }
01031
01032
01033 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
01034 (bytestream2_get_be32u(&s->g) == 12) &&
01035 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
01036 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
01037 if(!jp2_find_codestream(s)) {
01038 av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
01039 ret = -1;
01040 goto err_out;
01041 }
01042 } else {
01043 bytestream2_seek(&s->g, 0, SEEK_SET);
01044 }
01045
01046 if (bytestream2_get_be16u(&s->g) != J2K_SOC){
01047 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
01048 ret = -1;
01049 goto err_out;
01050 }
01051 if (ret = decode_codestream(s))
01052 goto err_out;
01053
01054 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
01055 if (ret = decode_tile(s, s->tile + tileno))
01056 goto err_out;
01057
01058 cleanup(s);
01059
01060 *data_size = sizeof(AVPicture);
01061 *picture = s->picture;
01062
01063 return bytestream2_tell(&s->g);
01064
01065 err_out:
01066 cleanup(s);
01067 return ret;
01068 }
01069
01070 static av_cold int j2kdec_init(AVCodecContext *avctx)
01071 {
01072 J2kDecoderContext *s = avctx->priv_data;
01073
01074 s->avctx = avctx;
01075 avcodec_get_frame_defaults((AVFrame*)&s->picture);
01076 avctx->coded_frame = (AVFrame*)&s->picture;
01077
01078 ff_j2k_init_tier1_luts();
01079
01080 return 0;
01081 }
01082
01083 static av_cold int decode_end(AVCodecContext *avctx)
01084 {
01085 J2kDecoderContext *s = avctx->priv_data;
01086
01087 if (s->picture.data[0])
01088 avctx->release_buffer(avctx, &s->picture);
01089
01090 return 0;
01091 }
01092
01093 AVCodec ff_jpeg2000_decoder = {
01094 .name = "j2k",
01095 .type = AVMEDIA_TYPE_VIDEO,
01096 .id = AV_CODEC_ID_JPEG2000,
01097 .priv_data_size = sizeof(J2kDecoderContext),
01098 .init = j2kdec_init,
01099 .close = decode_end,
01100 .decode = decode_frame,
01101 .capabilities = CODEC_CAP_EXPERIMENTAL,
01102 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
01103 };