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     if(s->avctx->pix_fmt == PIX_FMT_YUV444P){
00289         bytestream_put_byte(&s->buf, 2); // ICT
00290     }else{
00291         bytestream_put_byte(&s->buf, 0); // unspecified
00292     }
00293     // SPcod
00294     bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
00295     bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
00296     bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
00297     bytestream_put_byte(&s->buf, 0); // cblk style
00298     bytestream_put_byte(&s->buf, codsty->transform); // transformation
00299     return 0;
00300 }
00301 
00302 static int put_qcd(J2kEncoderContext *s, int compno)
00303 {
00304     int i, size;
00305     J2kCodingStyle *codsty = &s->codsty;
00306     J2kQuantStyle  *qntsty = &s->qntsty;
00307 
00308     if (qntsty->quantsty == J2K_QSTY_NONE)
00309         size = 4 + 3 * (codsty->nreslevels-1);
00310     else // QSTY_SE
00311         size = 5 + 6 * (codsty->nreslevels-1);
00312 
00313     if (s->buf_end - s->buf < size + 2)
00314         return -1;
00315 
00316     bytestream_put_be16(&s->buf, J2K_QCD);
00317     bytestream_put_be16(&s->buf, size);  // LQcd
00318     bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty);  // Sqcd
00319     if (qntsty->quantsty == J2K_QSTY_NONE)
00320         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
00321             bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
00322     else // QSTY_SE
00323         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
00324             bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
00325     return 0;
00326 }
00327 
00328 static uint8_t *put_sot(J2kEncoderContext *s, int tileno)
00329 {
00330     uint8_t *psotptr;
00331 
00332     if (s->buf_end - s->buf < 12)
00333         return NULL;
00334 
00335     bytestream_put_be16(&s->buf, J2K_SOT);
00336     bytestream_put_be16(&s->buf, 10); // Lsot
00337     bytestream_put_be16(&s->buf, tileno); // Isot
00338 
00339     psotptr = s->buf;
00340     bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
00341 
00342     bytestream_put_byte(&s->buf, 0); // TPsot
00343     bytestream_put_byte(&s->buf, 1); // TNsot
00344     return psotptr;
00345 }
00346 
00352 static int init_tiles(J2kEncoderContext *s)
00353 {
00354     int tileno, tilex, tiley, compno;
00355     J2kCodingStyle *codsty = &s->codsty;
00356     J2kQuantStyle  *qntsty = &s->qntsty;
00357 
00358     s->numXtiles = ff_j2k_ceildiv(s->width, s->tile_width);
00359     s->numYtiles = ff_j2k_ceildiv(s->height, s->tile_height);
00360 
00361     s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(J2kTile));
00362     if (!s->tile)
00363         return AVERROR(ENOMEM);
00364     for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
00365         for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
00366             J2kTile *tile = s->tile + tileno;
00367 
00368             tile->comp = av_malloc(s->ncomponents * sizeof(J2kComponent));
00369             if (!tile->comp)
00370                 return AVERROR(ENOMEM);
00371             for (compno = 0; compno < s->ncomponents; compno++){
00372                 J2kComponent *comp = tile->comp + compno;
00373                 int ret, i, j;
00374 
00375                 comp->coord[0][0] = tilex * s->tile_width;
00376                 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
00377                 comp->coord[1][0] = tiley * s->tile_height;
00378                 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
00379                 if (compno > 0)
00380                     for (i = 0; i < 2; i++)
00381                         for (j = 0; j < 2; j++)
00382                             comp->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
00383 
00384                 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], compno?1<<s->chroma_shift[0]:1, compno?1<<s->chroma_shift[1]:1))
00385                     return ret;
00386             }
00387         }
00388     return 0;
00389 }
00390 
00391 static void copy_frame(J2kEncoderContext *s)
00392 {
00393     int tileno, compno, i, y, x;
00394     uint8_t *line;
00395     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00396         J2kTile *tile = s->tile + tileno;
00397         if (s->planar){
00398             for (compno = 0; compno < s->ncomponents; compno++){
00399                 J2kComponent *comp = tile->comp + compno;
00400                 int *dst = comp->data;
00401                 line = s->picture.data[compno]
00402                        + comp->coord[1][0] * s->picture.linesize[compno]
00403                        + comp->coord[0][0];
00404                 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
00405                     uint8_t *ptr = line;
00406                     for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
00407                         *dst++ = *ptr++ - (1 << 7);
00408                     line += s->picture.linesize[compno];
00409                 }
00410             }
00411         } else{
00412             line = s->picture.data[0] + tile->comp[0].coord[1][0] * s->picture.linesize[0]
00413                    + tile->comp[0].coord[0][0] * s->ncomponents;
00414 
00415             i = 0;
00416             for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
00417                 uint8_t *ptr = line;
00418                 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
00419                     for (compno = 0; compno < s->ncomponents; compno++){
00420                         tile->comp[compno].data[i] = *ptr++  - (1 << 7);
00421                     }
00422                 }
00423                 line += s->picture.linesize[0];
00424             }
00425         }
00426     }
00427 }
00428 
00429 static void init_quantization(J2kEncoderContext *s)
00430 {
00431     int compno, reslevelno, bandno;
00432     J2kQuantStyle  *qntsty = &s->qntsty;
00433     J2kCodingStyle *codsty = &s->codsty;
00434 
00435     for (compno = 0; compno < s->ncomponents; compno++){
00436         int gbandno = 0;
00437         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00438             int nbands, lev = codsty->nreslevels - reslevelno - 1;
00439             nbands = reslevelno ? 3 : 1;
00440             for (bandno = 0; bandno < nbands; bandno++, gbandno++){
00441                 int expn, mant;
00442 
00443                 if (codsty->transform == FF_DWT97){
00444                     int bandpos = bandno + (reslevelno>0),
00445                         ss = 81920000 / dwt_norms[0][bandpos][lev],
00446                         log = av_log2(ss);
00447                     mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
00448                     expn = s->cbps[compno] - log + 13;
00449                 } else
00450                     expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
00451 
00452                 qntsty->expn[gbandno] = expn;
00453                 qntsty->mant[gbandno] = mant;
00454             }
00455         }
00456     }
00457 }
00458 
00459 static void init_luts()
00460 {
00461     int i, a,
00462         mask = ~((1<<NMSEDEC_FRACBITS)-1);
00463 
00464     for (i = 0; i < (1 << NMSEDEC_BITS); i++){
00465         lut_nmsedec_sig[i]  = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
00466         lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
00467 
00468         a = (i >> (NMSEDEC_BITS-2)&2) + 1;
00469         lut_nmsedec_ref[i]  = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
00470                                     << 13-NMSEDEC_FRACBITS, 0);
00471         lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
00472                                     << 1, 0);
00473     }
00474 }
00475 
00476 /* tier-1 routines */
00477 static int getnmsedec_sig(int x, int bpno)
00478 {
00479     if (bpno > NMSEDEC_FRACBITS)
00480         return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
00481     return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
00482 }
00483 
00484 static int getnmsedec_ref(int x, int bpno)
00485 {
00486     if (bpno > NMSEDEC_FRACBITS)
00487         return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
00488     return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
00489 }
00490 
00491 static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
00492 {
00493     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
00494     int vert_causal_ctx_csty_loc_symbol;
00495     for (y0 = 0; y0 < height; y0 += 4)
00496         for (x = 0; x < width; x++)
00497             for (y = y0; y < height && y < y0+4; y++){
00498                 if (!(t1->flags[y+1][x+1] & J2K_T1_SIG) && (t1->flags[y+1][x+1] & J2K_T1_SIG_NB)){
00499                     int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol),
00500                         bit = t1->data[y][x] & mask ? 1 : 0;
00501                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
00502                     if (bit){
00503                         int xorbit;
00504                         int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00505                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
00506                         *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00507                         ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
00508                     }
00509                     t1->flags[y+1][x+1] |= J2K_T1_VIS;
00510                 }
00511             }
00512 }
00513 
00514 static void encode_refpass(J2kT1Context *t1, int width, int height, int *nmsedec, int bpno)
00515 {
00516     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
00517     for (y0 = 0; y0 < height; y0 += 4)
00518         for (x = 0; x < width; x++)
00519             for (y = y0; y < height && y < y0+4; y++)
00520                 if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
00521                     int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
00522                     *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00523                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
00524                     t1->flags[y+1][x+1] |= J2K_T1_REF;
00525                 }
00526 }
00527 
00528 static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
00529 {
00530     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
00531     int vert_causal_ctx_csty_loc_symbol;
00532     for (y0 = 0; y0 < height; y0 += 4)
00533         for (x = 0; x < width; x++){
00534             if (y0 + 3 < height && !(
00535             (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00536             (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00537             (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00538             (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG))))
00539             {
00540                 // aggregation mode
00541                 int rlen;
00542                 for (rlen = 0; rlen < 4; rlen++)
00543                     if (t1->data[y0+rlen][x] & mask)
00544                         break;
00545                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
00546                 if (rlen == 4)
00547                     continue;
00548                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
00549                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
00550                 for (y = y0 + rlen; y < y0 + 4; y++){
00551                     if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
00552                         int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
00553                         if (y > y0 + rlen)
00554                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
00555                         if (t1->data[y][x] & mask){ // newly significant
00556                             int xorbit;
00557                             int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00558                             *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00559                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
00560                             ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
00561                         }
00562                     }
00563                     t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
00564                 }
00565             } else{
00566                 for (y = y0; y < y0 + 4 && y < height; y++){
00567                     if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
00568                         int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
00569                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
00570                         if (t1->data[y][x] & mask){ // newly significant
00571                             int xorbit;
00572                             int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00573                             *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00574                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
00575                             ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
00576                         }
00577                     }
00578                     t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
00579                 }
00580             }
00581         }
00582 }
00583 
00584 static void encode_cblk(J2kEncoderContext *s, J2kT1Context *t1, J2kCblk *cblk, J2kTile *tile,
00585                         int width, int height, int bandpos, int lev)
00586 {
00587     int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
00588     int64_t wmsedec = 0;
00589 
00590     for (y = 0; y < height+2; y++)
00591         memset(t1->flags[y], 0, (width+2)*sizeof(int));
00592 
00593     for (y = 0; y < height; y++){
00594         for (x = 0; x < width; x++){
00595             if (t1->data[y][x] < 0){
00596                 t1->flags[y+1][x+1] |= J2K_T1_SGN;
00597                 t1->data[y][x] = -t1->data[y][x];
00598             }
00599             max = FFMAX(max, t1->data[y][x]);
00600         }
00601     }
00602 
00603     if (max == 0){
00604         cblk->nonzerobits = 0;
00605         bpno = 0;
00606     } else{
00607         cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
00608         bpno = cblk->nonzerobits - 1;
00609     }
00610 
00611     ff_mqc_initenc(&t1->mqc, cblk->data);
00612 
00613     for (passno = 0; bpno >= 0; passno++){
00614         nmsedec=0;
00615 
00616         switch(pass_t){
00617             case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
00618                     break;
00619             case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
00620                     break;
00621             case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
00622                     break;
00623         }
00624 
00625         cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
00626         wmsedec += (int64_t)nmsedec << (2*bpno);
00627         cblk->passes[passno].disto = wmsedec;
00628 
00629         if (++pass_t == 3){
00630             pass_t = 0;
00631             bpno--;
00632         }
00633     }
00634     cblk->npasses = passno;
00635     cblk->ninclpasses = passno;
00636 
00637     // TODO: optional flush on each pass
00638     cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
00639 }
00640 
00641 /* tier-2 routines: */
00642 
00643 static void putnumpasses(J2kEncoderContext *s, int n)
00644 {
00645     if (n == 1)
00646         put_num(s, 0, 1);
00647     else if (n == 2)
00648         put_num(s, 2, 2);
00649     else if (n <= 5)
00650         put_num(s, 0xc | (n-3), 4);
00651     else if (n <= 36)
00652         put_num(s, 0x1e0 | (n-6), 9);
00653     else
00654         put_num(s, 0xff80 | (n-37), 16);
00655 }
00656 
00657 
00658 static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno,
00659                           uint8_t *expn, int numgbits)
00660 {
00661     int bandno, empty = 1;
00662 
00663     // init bitstream
00664     *s->buf = 0;
00665     s->bit_index = 0;
00666 
00667     // header
00668 
00669     // is the packet empty?
00670     for (bandno = 0; bandno < rlevel->nbands; bandno++){
00671         if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
00672         &&  rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
00673             empty = 0;
00674             break;
00675         }
00676     }
00677 
00678     put_bits(s, !empty, 1);
00679     if (empty){
00680         j2k_flush(s);
00681         return 0;
00682     }
00683 
00684     for (bandno = 0; bandno < rlevel->nbands; bandno++){
00685         J2kBand *band = rlevel->band + bandno;
00686         J2kPrec *prec = band->prec + precno;
00687         int yi, xi, pos;
00688         int cblknw = prec->xi1 - prec->xi0;
00689 
00690         if (band->coord[0][0] == band->coord[0][1]
00691         ||  band->coord[1][0] == band->coord[1][1])
00692             continue;
00693 
00694         for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
00695             for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
00696                 prec->cblkincl[pos].val = band->cblk[yi * cblknw + xi].ninclpasses == 0;
00697                 tag_tree_update(prec->cblkincl + pos);
00698                 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - band->cblk[yi * cblknw + xi].nonzerobits;
00699                 tag_tree_update(prec->zerobits + pos);
00700             }
00701         }
00702 
00703         for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
00704             for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
00705                 int pad = 0, llen, length;
00706                 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
00707 
00708                 if (s->buf_end - s->buf < 20) // approximately
00709                     return -1;
00710 
00711                 // inclusion information
00712                 tag_tree_code(s, prec->cblkincl + pos, 1);
00713                 if (!cblk->ninclpasses)
00714                     continue;
00715                 // zerobits information
00716                 tag_tree_code(s, prec->zerobits + pos, 100);
00717                 // number of passes
00718                 putnumpasses(s, cblk->ninclpasses);
00719 
00720                 length = cblk->passes[cblk->ninclpasses-1].rate;
00721                 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
00722                 if (llen < 0){
00723                     pad = -llen;
00724                     llen = 0;
00725                 }
00726                 // length of code block
00727                 put_bits(s, 1, llen);
00728                 put_bits(s, 0, 1);
00729                 put_num(s, length, av_log2(length)+1+pad);
00730             }
00731         }
00732     }
00733     j2k_flush(s);
00734     for (bandno = 0; bandno < rlevel->nbands; bandno++){
00735         J2kBand *band = rlevel->band + bandno;
00736         J2kPrec *prec = band->prec + precno;
00737         int yi, cblknw = prec->xi1 - prec->xi0;
00738         for (yi = prec->yi0; yi < prec->yi1; yi++){
00739             int xi;
00740             for (xi = prec->xi0; xi < prec->xi1; xi++){
00741                 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
00742                 if (cblk->ninclpasses){
00743                     if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
00744                         return -1;
00745                     bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
00746                 }
00747             }
00748         }
00749     }
00750     return 0;
00751 }
00752 
00753 static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
00754 {
00755     int compno, reslevelno, ret;
00756     J2kCodingStyle *codsty = &s->codsty;
00757     J2kQuantStyle  *qntsty = &s->qntsty;
00758 
00759     av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
00760     // lay-rlevel-comp-pos progression
00761     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00762         for (compno = 0; compno < s->ncomponents; compno++){
00763             int precno;
00764             J2kResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
00765             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
00766                 if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
00767                               qntsty->nguardbits))
00768                     return ret;
00769             }
00770         }
00771     }
00772     av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
00773     return 0;
00774 }
00775 
00776 static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
00777 {
00778     int passno, res = 0;
00779     for (passno = 0; passno < cblk->npasses; passno++){
00780         int dr;
00781         int64_t dd;
00782 
00783         dr = cblk->passes[passno].rate
00784            - (res ? cblk->passes[res-1].rate:0);
00785         dd = cblk->passes[passno].disto
00786            - (res ? cblk->passes[res-1].disto:0);
00787 
00788         if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
00789             res = passno+1;
00790     }
00791     return res;
00792 }
00793 
00794 static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
00795 {
00796     int compno, reslevelno, bandno, cblkno, lev;
00797     J2kCodingStyle *codsty = &s->codsty;
00798 
00799     for (compno = 0; compno < s->ncomponents; compno++){
00800         J2kComponent *comp = tile->comp + compno;
00801 
00802         for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
00803             J2kResLevel *reslevel = comp->reslevel + reslevelno;
00804 
00805             for (bandno = 0; bandno < reslevel->nbands ; bandno++){
00806                 int bandpos = bandno + (reslevelno > 0);
00807                 J2kBand *band = reslevel->band + bandno;
00808 
00809                 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
00810                     J2kCblk *cblk = band->cblk + cblkno;
00811 
00812                     cblk->ninclpasses = getcut(cblk, s->lambda,
00813                             (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 13);
00814                 }
00815             }
00816         }
00817     }
00818 }
00819 
00820 static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
00821 {
00822     int compno, reslevelno, bandno, ret;
00823     J2kT1Context t1;
00824     J2kCodingStyle *codsty = &s->codsty;
00825     for (compno = 0; compno < s->ncomponents; compno++){
00826         J2kComponent *comp = s->tile[tileno].comp + compno;
00827 
00828         av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
00829         if (ret = ff_j2k_dwt_encode(&comp->dwt, comp->data))
00830             return ret;
00831         av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
00832 
00833         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00834             J2kResLevel *reslevel = comp->reslevel + reslevelno;
00835 
00836             for (bandno = 0; bandno < reslevel->nbands ; bandno++){
00837                 J2kBand *band = reslevel->band + bandno;
00838                 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
00839                 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
00840                 y0 = yy0;
00841                 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
00842                             band->coord[1][1]) - band->coord[1][0] + yy0;
00843 
00844                 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
00845                     continue;
00846 
00847                 bandpos = bandno + (reslevelno > 0);
00848 
00849                 for (cblky = 0; cblky < band->cblkny; cblky++){
00850                     if (reslevelno == 0 || bandno == 1)
00851                         xx0 = 0;
00852                     else
00853                         xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
00854                     x0 = xx0;
00855                     xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
00856                                 band->coord[0][1]) - band->coord[0][0] + xx0;
00857 
00858                     for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
00859                         int y, x;
00860                         if (codsty->transform == FF_DWT53){
00861                             for (y = yy0; y < yy1; y++){
00862                                 int *ptr = t1.data[y-yy0];
00863                                 for (x = xx0; x < xx1; x++){
00864                                     *ptr++ = comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
00865                                 }
00866                             }
00867                         } else{
00868                             for (y = yy0; y < yy1; y++){
00869                                 int *ptr = t1.data[y-yy0];
00870                                 for (x = xx0; x < xx1; x++){
00871                                     *ptr = (comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
00872                                     *ptr = (int64_t)*ptr * (int64_t)(8192 * 8192 / band->stepsize) >> 13 - NMSEDEC_FRACBITS;
00873                                     *ptr++;
00874                                 }
00875                             }
00876                         }
00877                         encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
00878                                     bandpos, codsty->nreslevels - reslevelno - 1);
00879                         xx0 = xx1;
00880                         xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
00881                     }
00882                     yy0 = yy1;
00883                     yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
00884                 }
00885             }
00886         }
00887         av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
00888     }
00889 
00890     av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
00891     truncpasses(s, tile);
00892     if (ret = encode_packets(s, tile, tileno))
00893         return ret;
00894     av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
00895     return 0;
00896 }
00897 
00898 static void cleanup(J2kEncoderContext *s)
00899 {
00900     int tileno, compno;
00901     J2kCodingStyle *codsty = &s->codsty;
00902 
00903     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00904         for (compno = 0; compno < s->ncomponents; compno++){
00905             J2kComponent *comp = s->tile[tileno].comp + compno;
00906             ff_j2k_cleanup(comp, codsty);
00907         }
00908         av_freep(&s->tile[tileno].comp);
00909     }
00910     av_freep(&s->tile);
00911 }
00912 
00913 static void reinit(J2kEncoderContext *s)
00914 {
00915     int tileno, compno;
00916     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00917         J2kTile *tile = s->tile + tileno;
00918         for (compno = 0; compno < s->ncomponents; compno++)
00919             ff_j2k_reinit(tile->comp + compno, &s->codsty);
00920     }
00921 }
00922 
00923 static int encode_frame(AVCodecContext *avctx,
00924                         uint8_t *buf, int buf_size,
00925                         void *data)
00926 {
00927     int tileno, ret;
00928     J2kEncoderContext *s = avctx->priv_data;
00929 
00930     // init:
00931     s->buf = s->buf_start = buf;
00932     s->buf_end = buf + buf_size;
00933 
00934     s->picture = *(AVFrame*)data;
00935     avctx->coded_frame= &s->picture;
00936 
00937     s->lambda = s->picture.quality * LAMBDA_SCALE;
00938 
00939     copy_frame(s);
00940     reinit(s);
00941 
00942     if (s->buf_end - s->buf < 2)
00943         return -1;
00944     bytestream_put_be16(&s->buf, J2K_SOC);
00945     if (ret = put_siz(s))
00946         return ret;
00947     if (ret = put_cod(s))
00948         return ret;
00949     if (ret = put_qcd(s, 0))
00950         return ret;
00951 
00952     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00953         uint8_t *psotptr;
00954         if (!(psotptr = put_sot(s, tileno)))
00955             return -1;
00956         if (s->buf_end - s->buf < 2)
00957             return -1;
00958         bytestream_put_be16(&s->buf, J2K_SOD);
00959         if (ret = encode_tile(s, s->tile + tileno, tileno))
00960             return ret;
00961         bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
00962     }
00963     if (s->buf_end - s->buf < 2)
00964         return -1;
00965     bytestream_put_be16(&s->buf, J2K_EOC);
00966 
00967     av_log(s->avctx, AV_LOG_DEBUG, "end\n");
00968     return s->buf - s->buf_start;
00969 }
00970 
00971 static av_cold int j2kenc_init(AVCodecContext *avctx)
00972 {
00973     int i, ret;
00974     J2kEncoderContext *s = avctx->priv_data;
00975     J2kCodingStyle *codsty = &s->codsty;
00976     J2kQuantStyle  *qntsty = &s->qntsty;
00977 
00978     s->avctx = avctx;
00979     av_log(s->avctx, AV_LOG_DEBUG, "init\n");
00980 
00981     // defaults:
00982     // TODO: implement setting non-standard precinct size
00983     codsty->log2_prec_width  = 15;
00984     codsty->log2_prec_height = 15;
00985     codsty->nreslevels       = 7;
00986     codsty->log2_cblk_width  = 4;
00987     codsty->log2_cblk_height = 4;
00988     codsty->transform        = 1;
00989 
00990     qntsty->nguardbits       = 1;
00991 
00992     s->tile_width            = 256;
00993     s->tile_height           = 256;
00994 
00995     if (codsty->transform == FF_DWT53)
00996         qntsty->quantsty = J2K_QSTY_NONE;
00997     else
00998         qntsty->quantsty = J2K_QSTY_SE;
00999 
01000     s->width = avctx->width;
01001     s->height = avctx->height;
01002 
01003     for (i = 0; i < 3; i++)
01004         s->cbps[i] = 8;
01005 
01006     if (avctx->pix_fmt == PIX_FMT_RGB24){
01007         s->ncomponents = 3;
01008     } else if (avctx->pix_fmt == PIX_FMT_GRAY8){
01009         s->ncomponents = 1;
01010     } else{ // planar YUV
01011         s->planar = 1;
01012         s->ncomponents = 3;
01013         avcodec_get_chroma_sub_sample(avctx->pix_fmt,
01014                 s->chroma_shift, s->chroma_shift + 1);
01015     }
01016 
01017     ff_j2k_init_tier1_luts();
01018 
01019     init_luts();
01020 
01021     init_quantization(s);
01022     if (ret=init_tiles(s))
01023         return ret;
01024 
01025     av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
01026 
01027     return 0;
01028 }
01029 
01030 static int j2kenc_destroy(AVCodecContext *avctx)
01031 {
01032     J2kEncoderContext *s = avctx->priv_data;
01033 
01034     cleanup(s);
01035     return 0;
01036 }
01037 
01038 AVCodec ff_jpeg2000_encoder = {
01039     .name           = "j2k",
01040     .type           = AVMEDIA_TYPE_VIDEO,
01041     .id             = CODEC_ID_JPEG2000,
01042     .priv_data_size = sizeof(J2kEncoderContext),
01043     .init           = j2kenc_init,
01044     .encode         = encode_frame,
01045     .close          = j2kenc_destroy,
01046     .capabilities= CODEC_CAP_EXPERIMENTAL,
01047     .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
01048     .pix_fmts =
01049         (const enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_YUV444P, PIX_FMT_GRAY8,
01050 /*                              PIX_FMT_YUV420P,
01051                               PIX_FMT_YUV422P, PIX_FMT_YUV444P,
01052                               PIX_FMT_YUV410P, PIX_FMT_YUV411P,*/
01053                               -1}
01054 };