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