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