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

libavcodec/j2kenc.c

Go to the documentation of this file.
00001 /*
00002  * JPEG2000 image encoder
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 <float.h>
00029 #include "avcodec.h"
00030 #include "bytestream.h"
00031 #include "j2k.h"
00032 #include "libavutil/common.h"
00033 
00034 #define NMSEDEC_BITS 7
00035 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
00036 #define WMSEDEC_SHIFT 13 ///< must be >= 13
00037 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
00038 
00039 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
00040            lut_nmsedec_ref0[1<<NMSEDEC_BITS],
00041            lut_nmsedec_sig [1<<NMSEDEC_BITS],
00042            lut_nmsedec_sig0[1<<NMSEDEC_BITS];
00043 
00044 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
00045     {{10000, 19650, 41770,  84030, 169000, 338400,  676900, 1353000, 2706000, 5409000},
00046      {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
00047      {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
00048      {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
00049 
00050     {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
00051      {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
00052      {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
00053      { 7186,  9218, 15860, 30430,  60190, 120100, 240000, 479700,  959300}}
00054 };
00055 
00056 typedef struct {
00057    J2kComponent *comp;
00058 } J2kTile;
00059 
00060 typedef struct {
00061     AVCodecContext *avctx;
00062     AVFrame *picture;
00063 
00064     int width, height; 
00065     uint8_t cbps[4]; 
00066     int chroma_shift[2];
00067     uint8_t planar;
00068     int ncomponents;
00069     int tile_width, tile_height; 
00070     int numXtiles, numYtiles;
00071 
00072     uint8_t *buf_start;
00073     uint8_t *buf;
00074     uint8_t *buf_end;
00075     int bit_index;
00076 
00077     int64_t lambda;
00078 
00079     J2kCodingStyle codsty;
00080     J2kQuantStyle  qntsty;
00081 
00082     J2kTile *tile;
00083 } J2kEncoderContext;
00084 
00085 
00086 /* debug */
00087 #if 0
00088 #undef ifprintf
00089 #undef printf
00090 
00091 static void nspaces(FILE *fd, int n)
00092 {
00093     while(n--) putc(' ', fd);
00094 }
00095 
00096 static void printv(int *tab, int l)
00097 {
00098     int i;
00099     for (i = 0; i < l; i++)
00100         printf("%.3d ", tab[i]);
00101     printf("\n");
00102 }
00103 
00104 static void printu(uint8_t *tab, int l)
00105 {
00106     int i;
00107     for (i = 0; i < l; i++)
00108         printf("%.3hd ", tab[i]);
00109     printf("\n");
00110 }
00111 
00112 static void printcomp(J2kComponent *comp)
00113 {
00114     int i;
00115     for (i = 0; i < comp->y1 - comp->y0; i++)
00116         printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
00117 }
00118 
00119 static void dump(J2kEncoderContext *s, FILE *fd)
00120 {
00121     int tileno, compno, reslevelno, bandno, precno;
00122     fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
00123                 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
00124                 "tiles:\n",
00125             s->width, s->height, s->tile_width, s->tile_height,
00126             s->numXtiles, s->numYtiles, s->ncomponents);
00127     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00128         J2kTile *tile = s->tile + tileno;
00129         nspaces(fd, 2);
00130         fprintf(fd, "tile %d:\n", tileno);
00131         for(compno = 0; compno < s->ncomponents; compno++){
00132             J2kComponent *comp = tile->comp + compno;
00133             nspaces(fd, 4);
00134             fprintf(fd, "component %d:\n", compno);
00135             nspaces(fd, 4);
00136             fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
00137                         comp->x0, comp->x1, comp->y0, comp->y1);
00138             for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
00139                 J2kResLevel *reslevel = comp->reslevel + reslevelno;
00140                 nspaces(fd, 6);
00141                 fprintf(fd, "reslevel %d:\n", reslevelno);
00142                 nspaces(fd, 6);
00143                 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
00144                         reslevel->x0, reslevel->x1, reslevel->y0,
00145                         reslevel->y1, reslevel->nbands);
00146                 for(bandno = 0; bandno < reslevel->nbands; bandno++){
00147                     J2kBand *band = reslevel->band + bandno;
00148                     nspaces(fd, 8);
00149                     fprintf(fd, "band %d:\n", bandno);
00150                     nspaces(fd, 8);
00151                     fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
00152                                 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
00153                                 band->x0, band->x1,
00154                                 band->y0, band->y1,
00155                                 band->codeblock_width, band->codeblock_height,
00156                                 band->cblknx, band->cblkny);
00157                     for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
00158                         J2kPrec *prec = band->prec + precno;
00159                         nspaces(fd, 10);
00160                         fprintf(fd, "prec %d:\n", precno);
00161                         nspaces(fd, 10);
00162                         fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
00163                                      prec->xi0, prec->xi1, prec->yi0, prec->yi1);
00164                     }
00165                 }
00166             }
00167         }
00168     }
00169 }
00170 #endif
00171 
00172 /* bitstream routines */
00173 
00175 static void put_bits(J2kEncoderContext *s, int val, int n) // TODO: optimize
00176 {
00177     while (n-- > 0){
00178         if (s->bit_index == 8)
00179         {
00180             s->bit_index = *s->buf == 0xff;
00181             *(++s->buf) = 0;
00182         }
00183         *s->buf |= val << (7 - s->bit_index++);
00184     }
00185 }
00186 
00188 static void put_num(J2kEncoderContext *s, int num, int n)
00189 {
00190     while(--n >= 0)
00191         put_bits(s, (num >> n) & 1, 1);
00192 }
00193 
00195 static void j2k_flush(J2kEncoderContext *s)
00196 {
00197     if (s->bit_index){
00198         s->bit_index = 0;
00199         s->buf++;
00200     }
00201 }
00202 
00203 /* tag tree routines */
00204 
00206 static void tag_tree_code(J2kEncoderContext *s, J2kTgtNode *node, int threshold)
00207 {
00208     J2kTgtNode *stack[30];
00209     int sp = 1, curval = 0;
00210     stack[0] = node;
00211 
00212     node = node->parent;
00213     while(node){
00214         if (node->vis){
00215             curval = node->val;
00216             break;
00217         }
00218         node->vis++;
00219         stack[sp++] = node;
00220         node = node->parent;
00221     }
00222     while(--sp >= 0){
00223         if (stack[sp]->val >= threshold){
00224             put_bits(s, 0, threshold - curval);
00225             break;
00226         }
00227         put_bits(s, 0, stack[sp]->val - curval);
00228         put_bits(s, 1, 1);
00229         curval = stack[sp]->val;
00230     }
00231 }
00232 
00234 static void tag_tree_update(J2kTgtNode *node)
00235 {
00236     int lev = 0;
00237     while (node->parent){
00238         if (node->parent->val <= node->val)
00239             break;
00240         node->parent->val = node->val;
00241         node = node->parent;
00242         lev++;
00243     }
00244 }
00245 
00246 static int put_siz(J2kEncoderContext *s)
00247 {
00248     int i;
00249 
00250     if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
00251         return -1;
00252 
00253     bytestream_put_be16(&s->buf, J2K_SIZ);
00254     bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
00255     bytestream_put_be16(&s->buf, 0); // Rsiz
00256     bytestream_put_be32(&s->buf, s->width); // width
00257     bytestream_put_be32(&s->buf, s->height); // height
00258     bytestream_put_be32(&s->buf, 0); // X0Siz
00259     bytestream_put_be32(&s->buf, 0); // Y0Siz
00260 
00261     bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
00262     bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
00263     bytestream_put_be32(&s->buf, 0); // XT0Siz
00264     bytestream_put_be32(&s->buf, 0); // YT0Siz
00265     bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
00266 
00267     for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
00268         bytestream_put_byte(&s->buf, 7);
00269         bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
00270         bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
00271     }
00272     return 0;
00273 }
00274 
00275 static int put_cod(J2kEncoderContext *s)
00276 {
00277     J2kCodingStyle *codsty = &s->codsty;
00278 
00279     if (s->buf_end - s->buf < 14)
00280         return -1;
00281 
00282     bytestream_put_be16(&s->buf, J2K_COD);
00283     bytestream_put_be16(&s->buf, 12); // Lcod
00284     bytestream_put_byte(&s->buf, 0);  // Scod
00285     // SGcod
00286     bytestream_put_byte(&s->buf, 0); // progression level
00287     bytestream_put_be16(&s->buf, 1); // num of layers
00288     bytestream_put_byte(&s->buf, 0); // multiple component transformation
00289     // SPcod
00290     bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
00291     bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
00292     bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
00293     bytestream_put_byte(&s->buf, 0); // cblk style
00294     bytestream_put_byte(&s->buf, codsty->transform); // transformation
00295     return 0;
00296 }
00297 
00298 static int put_qcd(J2kEncoderContext *s, int compno)
00299 {
00300     int i, size;
00301     J2kCodingStyle *codsty = &s->codsty;
00302     J2kQuantStyle  *qntsty = &s->qntsty;
00303 
00304     if (qntsty->quantsty == J2K_QSTY_NONE)
00305         size = 4 + 3 * (codsty->nreslevels-1);
00306     else // QSTY_SE
00307         size = 5 + 6 * (codsty->nreslevels-1);
00308 
00309     if (s->buf_end - s->buf < size + 2)
00310         return -1;
00311 
00312     bytestream_put_be16(&s->buf, J2K_QCD);
00313     bytestream_put_be16(&s->buf, size);  // LQcd
00314     bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty);  // Sqcd
00315     if (qntsty->quantsty == J2K_QSTY_NONE)
00316         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
00317             bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
00318     else // QSTY_SE
00319         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
00320             bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
00321     return 0;
00322 }
00323 
00324 static uint8_t *put_sot(J2kEncoderContext *s, int tileno)
00325 {
00326     uint8_t *psotptr;
00327 
00328     if (s->buf_end - s->buf < 12)
00329         return -1;
00330 
00331     bytestream_put_be16(&s->buf, J2K_SOT);
00332     bytestream_put_be16(&s->buf, 10); // Lsot
00333     bytestream_put_be16(&s->buf, tileno); // Isot
00334 
00335     psotptr = s->buf;
00336     bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
00337 
00338     bytestream_put_byte(&s->buf, 0); // TPsot
00339     bytestream_put_byte(&s->buf, 1); // TNsot
00340     return psotptr;
00341 }
00342 
00348 static int init_tiles(J2kEncoderContext *s)
00349 {
00350     int tileno, tilex, tiley, compno;
00351     J2kCodingStyle *codsty = &s->codsty;
00352     J2kQuantStyle  *qntsty = &s->qntsty;
00353 
00354     s->numXtiles = ff_j2k_ceildiv(s->width, s->tile_width);
00355     s->numYtiles = ff_j2k_ceildiv(s->height, s->tile_height);
00356 
00357     s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(J2kTile));
00358     if (!s->tile)
00359         return AVERROR(ENOMEM);
00360     for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
00361         for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
00362             J2kTile *tile = s->tile + tileno;
00363 
00364             tile->comp = av_malloc(s->ncomponents * sizeof(J2kComponent));
00365             if (!tile->comp)
00366                 return AVERROR(ENOMEM);
00367             for (compno = 0; compno < s->ncomponents; compno++){
00368                 J2kComponent *comp = tile->comp + compno;
00369                 int ret, i, j;
00370 
00371                 comp->coord[0][0] = tilex * s->tile_width;
00372                 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
00373                 comp->coord[1][0] = tiley * s->tile_height;
00374                 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
00375                 if (compno > 0)
00376                     for (i = 0; i < 2; i++)
00377                         for (j = 0; j < 2; j++)
00378                             comp->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
00379 
00380                 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno]))
00381                     return ret;
00382             }
00383         }
00384     return 0;
00385 }
00386 
00387 static void copy_frame(J2kEncoderContext *s)
00388 {
00389     int tileno, compno, i, y, x;
00390     uint8_t *line;
00391     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00392         J2kTile *tile = s->tile + tileno;
00393         if (s->planar){
00394             for (compno = 0; compno < s->ncomponents; compno++){
00395                 J2kComponent *comp = tile->comp + compno;
00396                 int *dst = comp->data;
00397                 line = s->picture->data[compno]
00398                        + comp->coord[1][0] * s->picture->linesize[compno]
00399                        + comp->coord[0][0];
00400                 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
00401                     uint8_t *ptr = line;
00402                     for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
00403                         *dst++ = *ptr++ - (1 << 7);
00404                     line += s->picture->linesize[compno];
00405                 }
00406             }
00407         } else{
00408             line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
00409                    + tile->comp[0].coord[0][0] * s->ncomponents;
00410 
00411             i = 0;
00412             for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
00413                 uint8_t *ptr = line;
00414                 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
00415                     for (compno = 0; compno < s->ncomponents; compno++){
00416                         tile->comp[compno].data[i] = *ptr++  - (1 << 7);
00417                     }
00418                 }
00419                 line += s->picture->linesize[0];
00420             }
00421         }
00422     }
00423 }
00424 
00425 static void init_quantization(J2kEncoderContext *s)
00426 {
00427     int compno, reslevelno, bandno;
00428     J2kQuantStyle  *qntsty = &s->qntsty;
00429     J2kCodingStyle *codsty = &s->codsty;
00430 
00431     for (compno = 0; compno < s->ncomponents; compno++){
00432         int gbandno = 0;
00433         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00434             int nbands, lev = codsty->nreslevels - reslevelno - 1;
00435             nbands = reslevelno ? 3 : 1;
00436             for (bandno = 0; bandno < nbands; bandno++, gbandno++){
00437                 int expn, mant;
00438 
00439                 if (codsty->transform == FF_DWT97){
00440                     int bandpos = bandno + (reslevelno>0),
00441                         ss = 81920000 / dwt_norms[0][bandpos][lev],
00442                         log = av_log2(ss);
00443                     mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
00444                     expn = s->cbps[compno] - log + 13;
00445                 } else
00446                     expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
00447 
00448                 qntsty->expn[gbandno] = expn;
00449                 qntsty->mant[gbandno] = mant;
00450             }
00451         }
00452     }
00453 }
00454 
00455 static void init_luts()
00456 {
00457     int i, a,
00458         mask = ~((1<<NMSEDEC_FRACBITS)-1);
00459 
00460     for (i = 0; i < (1 << NMSEDEC_BITS); i++){
00461         lut_nmsedec_sig[i]  = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
00462         lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
00463 
00464         a = (i >> (NMSEDEC_BITS-2)&2) + 1;
00465         lut_nmsedec_ref[i]  = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
00466                                     << 13-NMSEDEC_FRACBITS, 0);
00467         lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
00468                                     << 1, 0);
00469     }
00470 }
00471 
00472 /* tier-1 routines */
00473 static int getnmsedec_sig(int x, int bpno)
00474 {
00475     if (bpno > NMSEDEC_FRACBITS)
00476         return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
00477     return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
00478 }
00479 
00480 static int getnmsedec_ref(int x, int bpno)
00481 {
00482     if (bpno > NMSEDEC_FRACBITS)
00483         return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
00484     return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
00485 }
00486 
00487 static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
00488 {
00489     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
00490     for (y0 = 0; y0 < height; y0 += 4)
00491         for (x = 0; x < width; x++)
00492             for (y = y0; y < height && y < y0+4; y++){
00493                 if (!(t1->flags[y+1][x+1] & J2K_T1_SIG) && (t1->flags[y+1][x+1] & J2K_T1_SIG_NB)){
00494                     int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno),
00495                         bit = t1->data[y][x] & mask ? 1 : 0;
00496                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
00497                     if (bit){
00498                         int xorbit;
00499                         int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00500                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
00501                         *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00502                         ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
00503                     }
00504                     t1->flags[y+1][x+1] |= J2K_T1_VIS;
00505                 }
00506             }
00507 }
00508 
00509 static void encode_refpass(J2kT1Context *t1, int width, int height, int *nmsedec, int bpno)
00510 {
00511     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
00512     for (y0 = 0; y0 < height; y0 += 4)
00513         for (x = 0; x < width; x++)
00514             for (y = y0; y < height && y < y0+4; y++)
00515                 if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
00516                     int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
00517                     *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00518                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
00519                     t1->flags[y+1][x+1] |= J2K_T1_REF;
00520                 }
00521 }
00522 
00523 static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
00524 {
00525     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
00526     for (y0 = 0; y0 < height; y0 += 4)
00527         for (x = 0; x < width; x++){
00528             if (y0 + 3 < height && !(
00529             (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00530             (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00531             (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00532             (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG))))
00533             {
00534                 // aggregation mode
00535                 int rlen;
00536                 for (rlen = 0; rlen < 4; rlen++)
00537                     if (t1->data[y0+rlen][x] & mask)
00538                         break;
00539                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
00540                 if (rlen == 4)
00541                     continue;
00542                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
00543                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
00544                 for (y = y0 + rlen; y < y0 + 4; y++){
00545                     if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
00546                         int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno);
00547                         if (y > y0 + rlen)
00548                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
00549                         if (t1->data[y][x] & mask){ // newly significant
00550                             int xorbit;
00551                             int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00552                             *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00553                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
00554                             ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
00555                         }
00556                     }
00557                     t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
00558                 }
00559             } else{
00560                 for (y = y0; y < y0 + 4 && y < height; y++){
00561                     if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
00562                         int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno);
00563                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
00564                         if (t1->data[y][x] & mask){ // newly significant
00565                             int xorbit;
00566                             int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00567                             *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00568                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
00569                             ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
00570                         }
00571                     }
00572                     t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
00573                 }
00574             }
00575         }
00576 }
00577 
00578 static void encode_cblk(J2kEncoderContext *s, J2kT1Context *t1, J2kCblk *cblk, J2kTile *tile,
00579                         int width, int height, int bandpos, int lev)
00580 {
00581     int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
00582     int64_t wmsedec = 0;
00583 
00584     for (y = 0; y < height+2; y++)
00585         memset(t1->flags[y], 0, (width+2)*sizeof(int));
00586 
00587     for (y = 0; y < height; y++){
00588         for (x = 0; x < width; x++){
00589             if (t1->data[y][x] < 0){
00590                 t1->flags[y+1][x+1] |= J2K_T1_SGN;
00591                 t1->data[y][x] = -t1->data[y][x];
00592             }
00593             max = FFMAX(max, t1->data[y][x]);
00594         }
00595     }
00596 
00597     if (max == 0){
00598         cblk->nonzerobits = 0;
00599         bpno = 0;
00600     } else{
00601         cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
00602         bpno = cblk->nonzerobits - 1;
00603     }
00604 
00605     ff_mqc_initenc(&t1->mqc, cblk->data);
00606 
00607     for (passno = 0; bpno >= 0; passno++){
00608         nmsedec=0;
00609 
00610         switch(pass_t){
00611             case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
00612                     break;
00613             case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
00614                     break;
00615             case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
00616                     break;
00617         }
00618 
00619         cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
00620         wmsedec += (int64_t)nmsedec << (2*bpno);
00621         cblk->passes[passno].disto = wmsedec;
00622 
00623         if (++pass_t == 3){
00624             pass_t = 0;
00625             bpno--;
00626         }
00627     }
00628     cblk->npasses = passno;
00629     cblk->ninclpasses = passno;
00630 
00631     // TODO: optional flush on each pass
00632     cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
00633 }
00634 
00635 /* tier-2 routines: */
00636 
00637 static void putnumpasses(J2kEncoderContext *s, int n)
00638 {
00639     if (n == 1)
00640         put_num(s, 0, 1);
00641     else if (n == 2)
00642         put_num(s, 2, 2);
00643     else if (n <= 5)
00644         put_num(s, 0xc | (n-3), 4);
00645     else if (n <= 36)
00646         put_num(s, 0x1e0 | (n-6), 9);
00647     else
00648         put_num(s, 0xff80 | (n-37), 16);
00649 }
00650 
00651 
00652 static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno,
00653                           uint8_t *expn, int numgbits)
00654 {
00655     int bandno, empty = 1;
00656 
00657     // init bitstream
00658     *s->buf = 0;
00659     s->bit_index = 0;
00660 
00661     // header
00662 
00663     // is the packet empty?
00664     for (bandno = 0; bandno < rlevel->nbands; bandno++){
00665         if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
00666         &&  rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
00667             empty = 0;
00668             break;
00669         }
00670     }
00671 
00672     put_bits(s, !empty, 1);
00673     if (empty){
00674         j2k_flush(s);
00675         return 0;
00676     }
00677 
00678     for (bandno = 0; bandno < rlevel->nbands; bandno++){
00679         J2kBand *band = rlevel->band + bandno;
00680         J2kPrec *prec = band->prec + precno;
00681         int yi, xi, pos;
00682         int cblknw = prec->xi1 - prec->xi0;
00683 
00684         if (band->coord[0][0] == band->coord[0][1]
00685         ||  band->coord[1][0] == band->coord[1][1])
00686             continue;
00687 
00688         for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
00689             for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
00690                 prec->cblkincl[pos].val = band->cblk[yi * cblknw + xi].ninclpasses == 0;
00691                 tag_tree_update(prec->cblkincl + pos);
00692                 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - band->cblk[yi * cblknw + xi].nonzerobits;
00693                 tag_tree_update(prec->zerobits + pos);
00694             }
00695         }
00696 
00697         for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
00698             for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
00699                 int pad = 0, llen, length;
00700                 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
00701 
00702                 if (s->buf_end - s->buf < 20) // approximately
00703                     return -1;
00704 
00705                 // inclusion information
00706                 tag_tree_code(s, prec->cblkincl + pos, 1);
00707                 if (!cblk->ninclpasses)
00708                     continue;
00709                 // zerobits information
00710                 tag_tree_code(s, prec->zerobits + pos, 100);
00711                 // number of passes
00712                 putnumpasses(s, cblk->ninclpasses);
00713 
00714                 length = cblk->passes[cblk->ninclpasses-1].rate;
00715                 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
00716                 if (llen < 0){
00717                     pad = -llen;
00718                     llen = 0;
00719                 }
00720                 // length of code block
00721                 put_bits(s, 1, llen);
00722                 put_bits(s, 0, 1);
00723                 put_num(s, length, av_log2(length)+1+pad);
00724             }
00725         }
00726     }
00727     j2k_flush(s);
00728     for (bandno = 0; bandno < rlevel->nbands; bandno++){
00729         J2kBand *band = rlevel->band + bandno;
00730         J2kPrec *prec = band->prec + precno;
00731         int yi, cblknw = prec->xi1 - prec->xi0;
00732         for (yi = prec->yi0; yi < prec->yi1; yi++){
00733             int xi;
00734             for (xi = prec->xi0; xi < prec->xi1; xi++){
00735                 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
00736                 if (cblk->ninclpasses){
00737                     if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
00738                         return -1;
00739                     bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
00740                 }
00741             }
00742         }
00743     }
00744     return 0;
00745 }
00746 
00747 static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
00748 {
00749     int compno, reslevelno, ret;
00750     J2kCodingStyle *codsty = &s->codsty;
00751     J2kQuantStyle  *qntsty = &s->qntsty;
00752 
00753     av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
00754     // lay-rlevel-comp-pos progression
00755     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00756         for (compno = 0; compno < s->ncomponents; compno++){
00757             int precno;
00758             J2kResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
00759             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
00760                 if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
00761                               qntsty->nguardbits))
00762                     return ret;
00763             }
00764         }
00765     }
00766     av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
00767     return 0;
00768 }
00769 
00770 static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
00771 {
00772     int passno, res = 0;
00773     for (passno = 0; passno < cblk->npasses; passno++){
00774         int dr;
00775         int64_t dd;
00776 
00777         dr = cblk->passes[passno].rate
00778            - (res ? cblk->passes[res-1].rate:0);
00779         dd = cblk->passes[passno].disto
00780            - (res ? cblk->passes[res-1].disto:0);
00781 
00782         if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
00783             res = passno+1;
00784     }
00785     return res;
00786 }
00787 
00788 static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
00789 {
00790     int compno, reslevelno, bandno, cblkno, lev;
00791     J2kCodingStyle *codsty = &s->codsty;
00792 
00793     for (compno = 0; compno < s->ncomponents; compno++){
00794         J2kComponent *comp = tile->comp + compno;
00795 
00796         for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
00797             J2kResLevel *reslevel = comp->reslevel + reslevelno;
00798 
00799             for (bandno = 0; bandno < reslevel->nbands ; bandno++){
00800                 int bandpos = bandno + (reslevelno > 0);
00801                 J2kBand *band = reslevel->band + bandno;
00802 
00803                 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
00804                     J2kCblk *cblk = band->cblk + cblkno;
00805 
00806                     cblk->ninclpasses = getcut(cblk, s->lambda,
00807                             (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 13);
00808                 }
00809             }
00810         }
00811     }
00812 }
00813 
00814 static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
00815 {
00816     int compno, reslevelno, bandno, ret;
00817     J2kT1Context t1;
00818     J2kCodingStyle *codsty = &s->codsty;
00819     for (compno = 0; compno < s->ncomponents; compno++){
00820         J2kComponent *comp = s->tile[tileno].comp + compno;
00821 
00822         av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
00823         if (ret = ff_dwt_encode(&comp->dwt, comp->data))
00824             return ret;
00825         av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
00826 
00827         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00828             J2kResLevel *reslevel = comp->reslevel + reslevelno;
00829 
00830             for (bandno = 0; bandno < reslevel->nbands ; bandno++){
00831                 J2kBand *band = reslevel->band + bandno;
00832                 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
00833                 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
00834                 y0 = yy0;
00835                 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
00836                             band->coord[1][1]) - band->coord[1][0] + yy0;
00837 
00838                 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
00839                     continue;
00840 
00841                 bandpos = bandno + (reslevelno > 0);
00842 
00843                 for (cblky = 0; cblky < band->cblkny; cblky++){
00844                     if (reslevelno == 0 || bandno == 1)
00845                         xx0 = 0;
00846                     else
00847                         xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
00848                     x0 = xx0;
00849                     xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
00850                                 band->coord[0][1]) - band->coord[0][0] + xx0;
00851 
00852                     for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
00853                         int y, x;
00854                         if (codsty->transform == FF_DWT53){
00855                             for (y = yy0; y < yy1; y++){
00856                                 int *ptr = t1.data[y-yy0];
00857                                 for (x = xx0; x < xx1; x++){
00858                                     *ptr++ = comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
00859                                 }
00860                             }
00861                         } else{
00862                             for (y = yy0; y < yy1; y++){
00863                                 int *ptr = t1.data[y-yy0];
00864                                 for (x = xx0; x < xx1; x++){
00865                                     *ptr = (comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
00866                                     *ptr++ = (int64_t)*ptr * (int64_t)(8192 * 8192 / band->stepsize) >> 13 - NMSEDEC_FRACBITS;
00867                                 }
00868                             }
00869                         }
00870                         encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
00871                                     bandpos, codsty->nreslevels - reslevelno - 1);
00872                         xx0 = xx1;
00873                         xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
00874                     }
00875                     yy0 = yy1;
00876                     yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
00877                 }
00878             }
00879         }
00880         av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
00881     }
00882 
00883     av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
00884     truncpasses(s, tile);
00885     if (ret = encode_packets(s, tile, tileno))
00886         return ret;
00887     av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
00888     return 0;
00889 }
00890 
00891 void cleanup(J2kEncoderContext *s)
00892 {
00893     int tileno, compno;
00894     J2kCodingStyle *codsty = &s->codsty;
00895 
00896     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00897         for (compno = 0; compno < s->ncomponents; compno++){
00898             J2kComponent *comp = s->tile[tileno].comp + compno;
00899             ff_j2k_cleanup(comp, codsty);
00900         }
00901         av_freep(&s->tile[tileno].comp);
00902     }
00903     av_freep(&s->tile);
00904 }
00905 
00906 static void reinit(J2kEncoderContext *s)
00907 {
00908     int tileno, compno;
00909     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00910         J2kTile *tile = s->tile + tileno;
00911         for (compno = 0; compno < s->ncomponents; compno++)
00912             ff_j2k_reinit(tile->comp + compno, &s->codsty);
00913     }
00914 }
00915 
00916 static int encode_frame(AVCodecContext *avctx,
00917                         uint8_t *buf, int buf_size,
00918                         void *data)
00919 {
00920     int tileno, ret;
00921     J2kEncoderContext *s = avctx->priv_data;
00922 
00923     // init:
00924     s->buf = s->buf_start = buf;
00925     s->buf_end = buf + buf_size;
00926 
00927     s->picture = data;
00928 
00929     s->lambda = s->picture->quality * LAMBDA_SCALE;
00930 
00931     copy_frame(s);
00932     reinit(s);
00933 
00934     if (s->buf_end - s->buf < 2)
00935         return -1;
00936     bytestream_put_be16(&s->buf, J2K_SOC);
00937     if (ret = put_siz(s))
00938         return ret;
00939     if (ret = put_cod(s))
00940         return ret;
00941     if (ret = put_qcd(s, 0))
00942         return ret;
00943 
00944     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00945         uint8_t *psotptr;
00946         if ((psotptr = put_sot(s, tileno)) < 0)
00947             return psotptr;
00948         if (s->buf_end - s->buf < 2)
00949             return -1;
00950         bytestream_put_be16(&s->buf, J2K_SOD);
00951         if (ret = encode_tile(s, s->tile + tileno, tileno))
00952             return ret;
00953         bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
00954     }
00955     if (s->buf_end - s->buf < 2)
00956         return -1;
00957     bytestream_put_be16(&s->buf, J2K_EOC);
00958 
00959     av_log(s->avctx, AV_LOG_DEBUG, "end\n");
00960     return s->buf - s->buf_start;
00961 }
00962 
00963 static av_cold int j2kenc_init(AVCodecContext *avctx)
00964 {
00965     int i, ret;
00966     J2kEncoderContext *s = avctx->priv_data;
00967     J2kCodingStyle *codsty = &s->codsty;
00968     J2kQuantStyle  *qntsty = &s->qntsty;
00969 
00970     s->avctx = avctx;
00971     av_log(s->avctx, AV_LOG_DEBUG, "init\n");
00972 
00973     // defaults:
00974     // TODO: implement setting non-standard precinct size
00975     codsty->log2_prec_width  = 15;
00976     codsty->log2_prec_height = 15;
00977     codsty->nreslevels       = 7;
00978     codsty->log2_cblk_width  = 4;
00979     codsty->log2_cblk_height = 4;
00980     codsty->transform        = 1;
00981 
00982     qntsty->nguardbits       = 1;
00983 
00984     s->tile_width            = 256;
00985     s->tile_height           = 256;
00986 
00987     if (codsty->transform == FF_DWT53)
00988         qntsty->quantsty = J2K_QSTY_NONE;
00989     else
00990         qntsty->quantsty = J2K_QSTY_SE;
00991 
00992     s->width = avctx->width;
00993     s->height = avctx->height;
00994 
00995     for (i = 0; i < 3; i++)
00996         s->cbps[i] = 8;
00997 
00998     if (avctx->pix_fmt == PIX_FMT_RGB24){
00999         s->ncomponents = 3;
01000     } else if (avctx->pix_fmt == PIX_FMT_GRAY8){
01001         s->ncomponents = 1;
01002     } else{ // planar YUV
01003         s->planar = 1;
01004         s->ncomponents = 3;
01005         avcodec_get_chroma_sub_sample(avctx->pix_fmt,
01006                 s->chroma_shift, s->chroma_shift + 1);
01007     }
01008 
01009     ff_j2k_init_tier1_luts();
01010 
01011     init_luts();
01012 
01013     init_quantization(s);
01014     if (ret=init_tiles(s))
01015         return ret;
01016 
01017     av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
01018 
01019     return 0;
01020 }
01021 
01022 static int j2kenc_destroy(AVCodecContext *avctx)
01023 {
01024     J2kEncoderContext *s = avctx->priv_data;
01025 
01026     cleanup(s);
01027     return 0;
01028 }
01029 
01030 AVCodec jpeg2000_encoder = {
01031     "j2k",
01032     CODEC_TYPE_VIDEO,
01033     CODEC_ID_JPEG2000,
01034     sizeof(J2kEncoderContext),
01035     j2kenc_init,
01036     encode_frame,
01037     j2kenc_destroy,
01038     NULL,
01039     0,
01040     .pix_fmts =
01041         (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_RGB24,
01042                               PIX_FMT_YUV422P, PIX_FMT_YUV444P,
01043                               PIX_FMT_YUV410P, PIX_FMT_YUV411P,
01044                               -1}
01045 };

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