• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/j2kdec.c

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

Generated on Fri Feb 22 2013 07:24:27 for FFmpeg by  doxygen 1.7.1