00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00029 #include "avcodec.h"
00030 #include "j2k.h"
00031 
00032 #define SHL(a, n) ((n)>=0 ? (a) << (n) : (a) >> -(n))
00033 
00034 #if 0
00035 void ff_j2k_printv(int *tab, int l)
00036 {
00037     int i;
00038     for (i = 0; i < l; i++)
00039         printf("%.3d ", tab[i]);
00040     printf("\n");
00041 }
00042 
00043 void ff_j2k_printu(uint8_t *tab, int l)
00044 {
00045     int i;
00046     for (i = 0; i < l; i++)
00047         printf("%.3hd ", tab[i]);
00048     printf("\n");
00049 }
00050 #endif
00051 
00052 
00053 
00056 static int tag_tree_size(int w, int h)
00057 {
00058     int res = 0;
00059     while (w > 1 || h > 1){
00060         res += w * h;
00061         w = (w+1) >> 1;
00062         h = (h+1) >> 1;
00063     }
00064     return res + 1;
00065 }
00066 
00067 J2kTgtNode *ff_j2k_tag_tree_init(int w, int h)
00068 {
00069     int pw = w, ph = h;
00070     J2kTgtNode *res, *t, *t2;
00071 
00072     t = res = av_mallocz(tag_tree_size(w, h)*sizeof(J2kTgtNode));
00073 
00074     if (res == NULL)
00075         return NULL;
00076 
00077     while (w > 1 || h > 1){
00078         int i, j;
00079         pw = w;
00080         ph = h;
00081 
00082         w = (w+1) >> 1;
00083         h = (h+1) >> 1;
00084         t2 = t + pw*ph;
00085 
00086         for (i = 0; i < ph; i++)
00087             for (j = 0; j < pw; j++){
00088                 t[i*pw + j].parent = &t2[(i>>1)*w + (j>>1)];
00089             }
00090         t = t2;
00091     }
00092     t[0].parent = NULL;
00093     return res;
00094 }
00095 
00096 static void tag_tree_zero(J2kTgtNode *t, int w, int h)
00097 {
00098     int i, siz = tag_tree_size(w, h);
00099 
00100     for (i = 0; i < siz; i++){
00101         t[i].val = 0;
00102         t[i].vis = 0;
00103     }
00104 }
00105 
00106 uint8_t ff_j2k_nbctxno_lut[256][4];
00107 
00108 static int getnbctxno(int flag, int bandno, int vert_causal_ctx_csty_symbol)
00109 {
00110     int h, v, d;
00111 
00112     h = ((flag & J2K_T1_SIG_E) ? 1:0)+
00113         ((flag & J2K_T1_SIG_W) ? 1:0);
00114     v = ((flag & J2K_T1_SIG_N) ? 1:0);
00115     if (!vert_causal_ctx_csty_symbol)
00116          v = v + ((flag & J2K_T1_SIG_S) ? 1:0);
00117     d = ((flag & J2K_T1_SIG_NE) ? 1:0)+
00118         ((flag & J2K_T1_SIG_NW) ? 1:0);
00119     if (!vert_causal_ctx_csty_symbol)
00120         d = d + ((flag & J2K_T1_SIG_SE) ? 1:0)+
00121                 ((flag & J2K_T1_SIG_SW) ? 1:0);
00122     if (bandno < 3){
00123             if (bandno == 1)
00124                 FFSWAP(int, h, v);
00125             if (h == 2) return 8;
00126             if (h == 1){
00127                 if (v >= 1) return 7;
00128                 if (d >= 1) return 6;
00129                 return 5;
00130             }
00131             if (v == 2) return 4;
00132             if (v == 1) return 3;
00133             if (d >= 2) return 2;
00134             if (d == 1) return 1;
00135             return 0;
00136     } else{
00137             if (d >= 3) return 8;
00138             if (d == 2){
00139                 if (h+v >= 1) return 7;
00140                 return 6;
00141             }
00142             if (d == 1){
00143                 if (h+v >= 2) return 5;
00144                 if (h+v == 1) return 4;
00145                 return 3;
00146             }
00147             if (h+v >= 2) return 2;
00148             if (h+v == 1) return 1;
00149             return 0;
00150     }
00151 }
00152 
00153 uint8_t ff_j2k_sgnctxno_lut[16][16], ff_j2k_xorbit_lut[16][16];
00154 
00155 static int getsgnctxno(int flag, uint8_t *xorbit)
00156 {
00157     int vcontrib, hcontrib;
00158     static const int contribtab[3][3] = {{0, -1, 1}, {-1, -1, 0}, {1, 0, 1}};
00159     static const int ctxlbltab[3][3] = {{13, 12, 11}, {10, 9, 10}, {11, 12, 13}};
00160     static const int xorbittab[3][3] = {{1, 1, 1,}, {1, 0, 0}, {0, 0, 0}};
00161 
00162     hcontrib = contribtab[flag & J2K_T1_SIG_E ? flag & J2K_T1_SGN_E ? 1:2:0]
00163                          [flag & J2K_T1_SIG_W ? flag & J2K_T1_SGN_W ? 1:2:0]+1;
00164     vcontrib = contribtab[flag & J2K_T1_SIG_S ? flag & J2K_T1_SGN_S ? 1:2:0]
00165                          [flag & J2K_T1_SIG_N ? flag & J2K_T1_SGN_N ? 1:2:0]+1;
00166     *xorbit = xorbittab[hcontrib][vcontrib];
00167     return ctxlbltab[hcontrib][vcontrib];
00168 }
00169 
00170 void ff_j2k_init_tier1_luts(void)
00171 {
00172     int i, j;
00173     for (i = 0; i < 256; i++)
00174         for (j = 0; j < 4; j++)
00175             ff_j2k_nbctxno_lut[i][j] = getnbctxno(i, j, 0);
00176     for (i = 0; i < 16; i++)
00177         for (j = 0; j < 16; j++)
00178             ff_j2k_sgnctxno_lut[i][j] = getsgnctxno(i + (j << 8), &ff_j2k_xorbit_lut[i][j]);
00179 }
00180 
00181 void ff_j2k_set_significant(J2kT1Context *t1, int x, int y, int negative)
00182 {
00183     x++; y++;
00184     t1->flags[y][x] |= J2K_T1_SIG;
00185     if (negative){
00186         t1->flags[y][x+1] |= J2K_T1_SIG_W | J2K_T1_SGN_W;
00187         t1->flags[y][x-1] |= J2K_T1_SIG_E | J2K_T1_SGN_E;
00188         t1->flags[y+1][x] |= J2K_T1_SIG_N | J2K_T1_SGN_N;
00189         t1->flags[y-1][x] |= J2K_T1_SIG_S | J2K_T1_SGN_S;
00190     } else{
00191         t1->flags[y][x+1] |= J2K_T1_SIG_W;
00192         t1->flags[y][x-1] |= J2K_T1_SIG_E;
00193         t1->flags[y+1][x] |= J2K_T1_SIG_N;
00194         t1->flags[y-1][x] |= J2K_T1_SIG_S;
00195     }
00196     t1->flags[y+1][x+1] |= J2K_T1_SIG_NW;
00197     t1->flags[y+1][x-1] |= J2K_T1_SIG_NE;
00198     t1->flags[y-1][x+1] |= J2K_T1_SIG_SW;
00199     t1->flags[y-1][x-1] |= J2K_T1_SIG_SE;
00200 }
00201 
00202 int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps, int dx, int dy)
00203 {
00204     int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
00205 
00206     if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
00207         return ret;
00208     for (i = 0; i < 2; i++)
00209         csize *= comp->coord[i][1] - comp->coord[i][0];
00210 
00211     comp->data = av_malloc(csize * sizeof(int));
00212     if (!comp->data)
00213         return AVERROR(ENOMEM);
00214     comp->reslevel = av_malloc(codsty->nreslevels * sizeof(J2kResLevel));
00215 
00216     if (!comp->reslevel)
00217         return AVERROR(ENOMEM);
00218     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00219         int declvl = codsty->nreslevels - reslevelno;
00220         J2kResLevel *reslevel = comp->reslevel + reslevelno;
00221 
00222         for (i = 0; i < 2; i++)
00223             for (j = 0; j < 2; j++)
00224                 reslevel->coord[i][j] =
00225                     ff_j2k_ceildivpow2(comp->coord[i][j], declvl - 1);
00226 
00227         if (reslevelno == 0)
00228             reslevel->nbands = 1;
00229         else
00230             reslevel->nbands = 3;
00231 
00232         if (reslevel->coord[0][1] == reslevel->coord[0][0])
00233             reslevel->num_precincts_x = 0;
00234         else
00235             reslevel->num_precincts_x = ff_j2k_ceildivpow2(reslevel->coord[0][1], codsty->log2_prec_width)
00236                                         - (reslevel->coord[0][0] >> codsty->log2_prec_width);
00237 
00238         if (reslevel->coord[1][1] == reslevel->coord[1][0])
00239             reslevel->num_precincts_y = 0;
00240         else
00241             reslevel->num_precincts_y = ff_j2k_ceildivpow2(reslevel->coord[1][1], codsty->log2_prec_height)
00242                                         - (reslevel->coord[1][0] >> codsty->log2_prec_height);
00243 
00244         reslevel->band = av_malloc(reslevel->nbands * sizeof(J2kBand));
00245         if (!reslevel->band)
00246             return AVERROR(ENOMEM);
00247         for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++){
00248             J2kBand *band = reslevel->band + bandno;
00249             int cblkno, precx, precy, precno;
00250             int x0, y0, x1, y1;
00251             int xi0, yi0, xi1, yi1;
00252             int cblkperprecw, cblkperprech;
00253 
00254             if (qntsty->quantsty != J2K_QSTY_NONE){
00255                 static const uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
00256                 int numbps;
00257 
00258                 numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
00259                 band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
00260             } else
00261                 band->stepsize = 1 << 13;
00262 
00263             if (reslevelno == 0){  
00264                 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width-1);
00265                 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height-1);
00266                 for (i = 0; i < 2; i++)
00267                     for (j = 0; j < 2; j++)
00268                         band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], declvl-1);
00269             } else{
00270                 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width);
00271                 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height);
00272 
00273                 for (i = 0; i < 2; i++)
00274                     for (j = 0; j < 2; j++)
00275                         band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j] - (((bandno+1>>i)&1) << declvl-1), declvl);
00276             }
00277             band->cblknx = ff_j2k_ceildiv(band->coord[0][1], band->codeblock_width)  - band->coord[0][0] / band->codeblock_width;
00278             band->cblkny = ff_j2k_ceildiv(band->coord[1][1], band->codeblock_height) - band->coord[1][0] / band->codeblock_height;
00279 
00280             for (j = 0; j < 2; j++)
00281                 band->coord[0][j] = ff_j2k_ceildiv(band->coord[0][j], dx);
00282             for (j = 0; j < 2; j++)
00283                 band->coord[1][j] = ff_j2k_ceildiv(band->coord[1][j], dy);
00284 
00285             band->cblknx = ff_j2k_ceildiv(band->cblknx, dx);
00286             band->cblkny = ff_j2k_ceildiv(band->cblkny, dy);
00287 
00288             band->cblk = av_malloc(band->cblknx * band->cblkny * sizeof(J2kCblk));
00289             if (!band->cblk)
00290                 return AVERROR(ENOMEM);
00291             band->prec = av_malloc(reslevel->num_precincts_x * reslevel->num_precincts_y * sizeof(J2kPrec));
00292             if (!band->prec)
00293                 return AVERROR(ENOMEM);
00294 
00295             for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
00296                 J2kCblk *cblk = band->cblk + cblkno;
00297                 cblk->zero = 0;
00298                 cblk->lblock = 3;
00299                 cblk->length = 0;
00300                 cblk->lengthinc = 0;
00301                 cblk->npasses = 0;
00302             }
00303 
00304             y0 = band->coord[1][0];
00305             y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
00306             yi0 = 0;
00307             yi1 = ff_j2k_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
00308             yi1 = FFMIN(yi1, band->cblkny);
00309             cblkperprech = 1<<(codsty->log2_prec_height - codsty->log2_cblk_height);
00310             for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++){
00311                 for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++){
00312                     band->prec[precno].yi0 = yi0;
00313                     band->prec[precno].yi1 = yi1;
00314                 }
00315                 yi1 += cblkperprech;
00316                 yi0 = yi1 - cblkperprech;
00317                 yi1 = FFMIN(yi1, band->cblkny);
00318             }
00319             x0 = band->coord[0][0];
00320             x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
00321             xi0 = 0;
00322             xi1 = ff_j2k_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
00323             xi1 = FFMIN(xi1, band->cblknx);
00324 
00325             cblkperprecw = 1<<(codsty->log2_prec_width - codsty->log2_cblk_width);
00326             for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++){
00327                 for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0){
00328                     J2kPrec *prec = band->prec + precno;
00329                     prec->xi0 = xi0;
00330                     prec->xi1 = xi1;
00331                     prec->cblkincl = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
00332                                                           prec->yi1 - prec->yi0);
00333                     prec->zerobits = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
00334                                                           prec->yi1 - prec->yi0);
00335                     if (!prec->cblkincl || !prec->zerobits)
00336                         return AVERROR(ENOMEM);
00337 
00338                 }
00339                 xi1 += cblkperprecw;
00340                 xi0 = xi1 - cblkperprecw;
00341                 xi1 = FFMIN(xi1, band->cblknx);
00342             }
00343         }
00344     }
00345     return 0;
00346 }
00347 
00348 void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty)
00349 {
00350     int reslevelno, bandno, cblkno, precno;
00351     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00352         J2kResLevel *rlevel = comp->reslevel + reslevelno;
00353         for (bandno = 0; bandno < rlevel->nbands; bandno++){
00354             J2kBand *band = rlevel->band + bandno;
00355             for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
00356                 J2kPrec *prec = band->prec + precno;
00357                 tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
00358                 tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
00359             }
00360             for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
00361                 J2kCblk *cblk = band->cblk + cblkno;
00362                 cblk->length = 0;
00363                 cblk->lblock = 3;
00364             }
00365         }
00366     }
00367 }
00368 
00369 void ff_j2k_cleanup(J2kComponent *comp, J2kCodingStyle *codsty)
00370 {
00371     int reslevelno, bandno, precno;
00372     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00373         J2kResLevel *reslevel = comp->reslevel + reslevelno;
00374 
00375         for (bandno = 0; bandno < reslevel->nbands ; bandno++){
00376             J2kBand *band = reslevel->band + bandno;
00377                 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
00378                     J2kPrec *prec = band->prec + precno;
00379                     av_freep(&prec->zerobits);
00380                     av_freep(&prec->cblkincl);
00381                 }
00382                 av_freep(&band->cblk);
00383                 av_freep(&band->prec);
00384             }
00385         av_freep(&reslevel->band);
00386     }
00387 
00388     ff_j2k_dwt_destroy(&comp->dwt);
00389     av_freep(&comp->reslevel);
00390     av_freep(&comp->data);
00391 }