libavcodec/j2kenc.c
83654c7b
 /*
  * JPEG2000 image encoder
  * Copyright (c) 2007 Kamil Nowosad
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * JPEG2000 image encoder
  * @file
  * @author Kamil Nowosad
  */
 
 #include <float.h>
 #include "avcodec.h"
35dfeb87
 #include "internal.h"
83654c7b
 #include "bytestream.h"
62d00aa2
 #include "jpeg2000.h"
83654c7b
 #include "libavutil/common.h"
 
 #define NMSEDEC_BITS 7
 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
 #define WMSEDEC_SHIFT 13 ///< must be >= 13
 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
 
 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
            lut_nmsedec_ref0[1<<NMSEDEC_BITS],
            lut_nmsedec_sig [1<<NMSEDEC_BITS],
            lut_nmsedec_sig0[1<<NMSEDEC_BITS];
 
 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
     {{10000, 19650, 41770,  84030, 169000, 338400,  676900, 1353000, 2706000, 5409000},
      {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
      {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
      {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
 
     {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
      {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
      {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
      { 7186,  9218, 15860, 30430,  60190, 120100, 240000, 479700,  959300}}
 };
 
 typedef struct {
0ab0ed2b
    Jpeg2000Component *comp;
 } Jpeg2000Tile;
83654c7b
 
 typedef struct {
     AVCodecContext *avctx;
8443b270
     const AVFrame *picture;
83654c7b
 
     int width, height; ///< image width and height
     uint8_t cbps[4]; ///< bits per sample in particular components
     int chroma_shift[2];
     uint8_t planar;
     int ncomponents;
     int tile_width, tile_height; ///< tile size
     int numXtiles, numYtiles;
 
     uint8_t *buf_start;
     uint8_t *buf;
     uint8_t *buf_end;
     int bit_index;
 
     int64_t lambda;
 
0ab0ed2b
     Jpeg2000CodingStyle codsty;
     Jpeg2000QuantStyle  qntsty;
83654c7b
 
0ab0ed2b
     Jpeg2000Tile *tile;
 } Jpeg2000EncoderContext;
83654c7b
 
 
 /* debug */
 #if 0
 #undef ifprintf
 #undef printf
 
 static void nspaces(FILE *fd, int n)
 {
     while(n--) putc(' ', fd);
 }
 
0ab0ed2b
 static void printcomp(Jpeg2000Component *comp)
83654c7b
 {
     int i;
     for (i = 0; i < comp->y1 - comp->y0; i++)
62d00aa2
         ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
83654c7b
 }
 
0ab0ed2b
 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
83654c7b
 {
     int tileno, compno, reslevelno, bandno, precno;
     fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
                 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
                 "tiles:\n",
             s->width, s->height, s->tile_width, s->tile_height,
             s->numXtiles, s->numYtiles, s->ncomponents);
     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
0ab0ed2b
         Jpeg2000Tile *tile = s->tile + tileno;
83654c7b
         nspaces(fd, 2);
         fprintf(fd, "tile %d:\n", tileno);
         for(compno = 0; compno < s->ncomponents; compno++){
0ab0ed2b
             Jpeg2000Component *comp = tile->comp + compno;
83654c7b
             nspaces(fd, 4);
             fprintf(fd, "component %d:\n", compno);
             nspaces(fd, 4);
             fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
                         comp->x0, comp->x1, comp->y0, comp->y1);
             for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
0ab0ed2b
                 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
83654c7b
                 nspaces(fd, 6);
                 fprintf(fd, "reslevel %d:\n", reslevelno);
                 nspaces(fd, 6);
                 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
                         reslevel->x0, reslevel->x1, reslevel->y0,
                         reslevel->y1, reslevel->nbands);
                 for(bandno = 0; bandno < reslevel->nbands; bandno++){
0ab0ed2b
                     Jpeg2000Band *band = reslevel->band + bandno;
83654c7b
                     nspaces(fd, 8);
                     fprintf(fd, "band %d:\n", bandno);
                     nspaces(fd, 8);
                     fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
                                 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
                                 band->x0, band->x1,
                                 band->y0, band->y1,
                                 band->codeblock_width, band->codeblock_height,
                                 band->cblknx, band->cblkny);
                     for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
0ab0ed2b
                         Jpeg2000Prec *prec = band->prec + precno;
83654c7b
                         nspaces(fd, 10);
                         fprintf(fd, "prec %d:\n", precno);
                         nspaces(fd, 10);
                         fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
                                      prec->xi0, prec->xi1, prec->yi0, prec->yi1);
                     }
                 }
             }
         }
     }
 }
 #endif
 
 /* bitstream routines */
 
 /** put n times val bit */
0ab0ed2b
 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
83654c7b
 {
     while (n-- > 0){
         if (s->bit_index == 8)
         {
             s->bit_index = *s->buf == 0xff;
             *(++s->buf) = 0;
         }
         *s->buf |= val << (7 - s->bit_index++);
     }
 }
 
 /** put n least significant bits of a number num */
0ab0ed2b
 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
83654c7b
 {
     while(--n >= 0)
         put_bits(s, (num >> n) & 1, 1);
 }
 
 /** flush the bitstream */
0ab0ed2b
 static void j2k_flush(Jpeg2000EncoderContext *s)
83654c7b
 {
     if (s->bit_index){
         s->bit_index = 0;
         s->buf++;
     }
 }
 
 /* tag tree routines */
 
 /** code the value stored in node */
0ab0ed2b
 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
83654c7b
 {
0ab0ed2b
     Jpeg2000TgtNode *stack[30];
83654c7b
     int sp = 1, curval = 0;
     stack[0] = node;
 
     node = node->parent;
     while(node){
         if (node->vis){
             curval = node->val;
             break;
         }
         node->vis++;
         stack[sp++] = node;
         node = node->parent;
     }
     while(--sp >= 0){
         if (stack[sp]->val >= threshold){
             put_bits(s, 0, threshold - curval);
             break;
         }
         put_bits(s, 0, stack[sp]->val - curval);
         put_bits(s, 1, 1);
         curval = stack[sp]->val;
     }
 }
 
 /** update the value in node */
0ab0ed2b
 static void tag_tree_update(Jpeg2000TgtNode *node)
83654c7b
 {
     int lev = 0;
     while (node->parent){
         if (node->parent->val <= node->val)
             break;
         node->parent->val = node->val;
         node = node->parent;
         lev++;
     }
 }
 
0ab0ed2b
 static int put_siz(Jpeg2000EncoderContext *s)
83654c7b
 {
     int i;
 
     if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
         return -1;
 
78c7bff0
     bytestream_put_be16(&s->buf, JPEG2000_SIZ);
83654c7b
     bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
     bytestream_put_be16(&s->buf, 0); // Rsiz
     bytestream_put_be32(&s->buf, s->width); // width
     bytestream_put_be32(&s->buf, s->height); // height
     bytestream_put_be32(&s->buf, 0); // X0Siz
     bytestream_put_be32(&s->buf, 0); // Y0Siz
 
     bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
     bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
     bytestream_put_be32(&s->buf, 0); // XT0Siz
     bytestream_put_be32(&s->buf, 0); // YT0Siz
     bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
 
     for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
         bytestream_put_byte(&s->buf, 7);
         bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
         bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
     }
     return 0;
 }
 
0ab0ed2b
 static int put_cod(Jpeg2000EncoderContext *s)
83654c7b
 {
0ab0ed2b
     Jpeg2000CodingStyle *codsty = &s->codsty;
83654c7b
 
     if (s->buf_end - s->buf < 14)
         return -1;
 
78c7bff0
     bytestream_put_be16(&s->buf, JPEG2000_COD);
83654c7b
     bytestream_put_be16(&s->buf, 12); // Lcod
     bytestream_put_byte(&s->buf, 0);  // Scod
     // SGcod
     bytestream_put_byte(&s->buf, 0); // progression level
     bytestream_put_be16(&s->buf, 1); // num of layers
ac627b3d
     if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
2214a67d
         bytestream_put_byte(&s->buf, 2); // ICT
     }else{
         bytestream_put_byte(&s->buf, 0); // unspecified
     }
83654c7b
     // SPcod
     bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
     bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
     bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
     bytestream_put_byte(&s->buf, 0); // cblk style
83fd377c
     bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
83654c7b
     return 0;
 }
 
0ab0ed2b
 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
83654c7b
 {
     int i, size;
0ab0ed2b
     Jpeg2000CodingStyle *codsty = &s->codsty;
     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
83654c7b
 
78c7bff0
     if (qntsty->quantsty == JPEG2000_QSTY_NONE)
83654c7b
         size = 4 + 3 * (codsty->nreslevels-1);
     else // QSTY_SE
         size = 5 + 6 * (codsty->nreslevels-1);
 
     if (s->buf_end - s->buf < size + 2)
         return -1;
 
78c7bff0
     bytestream_put_be16(&s->buf, JPEG2000_QCD);
83654c7b
     bytestream_put_be16(&s->buf, size);  // LQcd
     bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty);  // Sqcd
78c7bff0
     if (qntsty->quantsty == JPEG2000_QSTY_NONE)
83654c7b
         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
             bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
     else // QSTY_SE
         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
             bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
     return 0;
 }
 
0ab0ed2b
 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
83654c7b
 {
     uint8_t *psotptr;
 
     if (s->buf_end - s->buf < 12)
7393b417
         return NULL;
83654c7b
 
78c7bff0
     bytestream_put_be16(&s->buf, JPEG2000_SOT);
83654c7b
     bytestream_put_be16(&s->buf, 10); // Lsot
     bytestream_put_be16(&s->buf, tileno); // Isot
 
     psotptr = s->buf;
     bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
 
     bytestream_put_byte(&s->buf, 0); // TPsot
     bytestream_put_byte(&s->buf, 1); // TNsot
     return psotptr;
 }
 
 /**
  * compute the sizes of tiles, resolution levels, bands, etc.
  * allocate memory for them
  * divide the input image into tile-components
  */
0ab0ed2b
 static int init_tiles(Jpeg2000EncoderContext *s)
83654c7b
 {
     int tileno, tilex, tiley, compno;
0ab0ed2b
     Jpeg2000CodingStyle *codsty = &s->codsty;
     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
83654c7b
 
d42106c3
     s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
     s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
83654c7b
 
0ab0ed2b
     s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(Jpeg2000Tile));
83654c7b
     if (!s->tile)
         return AVERROR(ENOMEM);
     for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
         for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
0ab0ed2b
             Jpeg2000Tile *tile = s->tile + tileno;
83654c7b
 
9c66da51
             tile->comp = av_mallocz(s->ncomponents * sizeof(Jpeg2000Component));
83654c7b
             if (!tile->comp)
                 return AVERROR(ENOMEM);
             for (compno = 0; compno < s->ncomponents; compno++){
0ab0ed2b
                 Jpeg2000Component *comp = tile->comp + compno;
83654c7b
                 int ret, i, j;
 
8cf57efd
                 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
                 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
                 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
                 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
83654c7b
                 if (compno > 0)
                     for (i = 0; i < 2; i++)
                         for (j = 0; j < 2; j++)
8cf57efd
                             comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
83654c7b
 
62d00aa2
                 if (ret = ff_jpeg2000_init_component(comp,
1b5cb6c0
                                                 codsty,
                                                 qntsty,
                                                 s->cbps[compno],
                                                 compno?1<<s->chroma_shift[0]:1,
                                                 compno?1<<s->chroma_shift[1]:1,
                                                 s->avctx
                                                ))
83654c7b
                     return ret;
             }
         }
     return 0;
 }
 
0ab0ed2b
 static void copy_frame(Jpeg2000EncoderContext *s)
83654c7b
 {
     int tileno, compno, i, y, x;
     uint8_t *line;
     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
0ab0ed2b
         Jpeg2000Tile *tile = s->tile + tileno;
83654c7b
         if (s->planar){
             for (compno = 0; compno < s->ncomponents; compno++){
0ab0ed2b
                 Jpeg2000Component *comp = tile->comp + compno;
8bedbb82
                 int *dst = comp->i_data;
8443b270
                 line = s->picture->data[compno]
                        + comp->coord[1][0] * s->picture->linesize[compno]
83654c7b
                        + comp->coord[0][0];
                 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
                     uint8_t *ptr = line;
                     for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
                         *dst++ = *ptr++ - (1 << 7);
8443b270
                     line += s->picture->linesize[compno];
83654c7b
                 }
             }
         } else{
8443b270
             line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
83654c7b
                    + tile->comp[0].coord[0][0] * s->ncomponents;
 
             i = 0;
             for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
                 uint8_t *ptr = line;
                 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
                     for (compno = 0; compno < s->ncomponents; compno++){
8bedbb82
                         tile->comp[compno].i_data[i] = *ptr++  - (1 << 7);
83654c7b
                     }
                 }
8443b270
                 line += s->picture->linesize[0];
83654c7b
             }
         }
     }
 }
 
0ab0ed2b
 static void init_quantization(Jpeg2000EncoderContext *s)
83654c7b
 {
     int compno, reslevelno, bandno;
0ab0ed2b
     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
     Jpeg2000CodingStyle *codsty = &s->codsty;
83654c7b
 
     for (compno = 0; compno < s->ncomponents; compno++){
         int gbandno = 0;
         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
             int nbands, lev = codsty->nreslevels - reslevelno - 1;
             nbands = reslevelno ? 3 : 1;
             for (bandno = 0; bandno < nbands; bandno++, gbandno++){
                 int expn, mant;
 
83fd377c
                 if (codsty->transform == FF_DWT97_INT){
83654c7b
                     int bandpos = bandno + (reslevelno>0),
                         ss = 81920000 / dwt_norms[0][bandpos][lev],
                         log = av_log2(ss);
                     mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
                     expn = s->cbps[compno] - log + 13;
                 } else
                     expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
 
                 qntsty->expn[gbandno] = expn;
                 qntsty->mant[gbandno] = mant;
             }
         }
     }
 }
 
03757d48
 static void init_luts(void)
83654c7b
 {
     int i, a,
         mask = ~((1<<NMSEDEC_FRACBITS)-1);
 
     for (i = 0; i < (1 << NMSEDEC_BITS); i++){
         lut_nmsedec_sig[i]  = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
         lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
 
         a = (i >> (NMSEDEC_BITS-2)&2) + 1;
         lut_nmsedec_ref[i]  = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
                                     << 13-NMSEDEC_FRACBITS, 0);
         lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
                                     << 1, 0);
     }
 }
 
 /* tier-1 routines */
 static int getnmsedec_sig(int x, int bpno)
 {
     if (bpno > NMSEDEC_FRACBITS)
         return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
     return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
 }
 
 static int getnmsedec_ref(int x, int bpno)
 {
     if (bpno > NMSEDEC_FRACBITS)
         return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
     return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
 }
 
0ab0ed2b
 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
83654c7b
 {
     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
     for (y0 = 0; y0 < height; y0 += 4)
         for (x = 0; x < width; x++)
             for (y = y0; y < height && y < y0+4; y++){
78c7bff0
                 if (!(t1->flags[y+1][x+1] & JPEG2000_T1_SIG) && (t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)){
b4013899
                     int ctxno = ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1], bandno),
83654c7b
                         bit = t1->data[y][x] & mask ? 1 : 0;
                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
                     if (bit){
                         int xorbit;
d42106c3
                         int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
83654c7b
                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
                         *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
62d00aa2
                         ff_jpeg2000_set_significance(t1, x, y, t1->flags[y+1][x+1] >> 15);
83654c7b
                     }
78c7bff0
                     t1->flags[y+1][x+1] |= JPEG2000_T1_VIS;
83654c7b
                 }
             }
 }
 
0ab0ed2b
 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
83654c7b
 {
     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
     for (y0 = 0; y0 < height; y0 += 4)
         for (x = 0; x < width; x++)
             for (y = y0; y < height && y < y0+4; y++)
78c7bff0
                 if ((t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
d42106c3
                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y+1][x+1]);
83654c7b
                     *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
78c7bff0
                     t1->flags[y+1][x+1] |= JPEG2000_T1_REF;
83654c7b
                 }
 }
 
0ab0ed2b
 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
83654c7b
 {
     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
     for (y0 = 0; y0 < height; y0 += 4)
         for (x = 0; x < width; x++){
             if (y0 + 3 < height && !(
78c7bff0
             (t1->flags[y0+1][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
             (t1->flags[y0+2][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
             (t1->flags[y0+3][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
             (t1->flags[y0+4][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
83654c7b
             {
                 // aggregation mode
                 int rlen;
                 for (rlen = 0; rlen < 4; rlen++)
                     if (t1->data[y0+rlen][x] & mask)
                         break;
                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
                 if (rlen == 4)
                     continue;
                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
                 for (y = y0 + rlen; y < y0 + 4; y++){
78c7bff0
                     if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
b4013899
                         int ctxno = ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1], bandno);
83654c7b
                         if (y > y0 + rlen)
                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
                         if (t1->data[y][x] & mask){ // newly significant
                             int xorbit;
d42106c3
                             int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
83654c7b
                             *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
62d00aa2
                             ff_jpeg2000_set_significance(t1, x, y, t1->flags[y+1][x+1] >> 15);
83654c7b
                         }
                     }
78c7bff0
                     t1->flags[y+1][x+1] &= ~JPEG2000_T1_VIS;
83654c7b
                 }
             } else{
                 for (y = y0; y < y0 + 4 && y < height; y++){
78c7bff0
                     if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
b4013899
                         int ctxno = ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1], bandno);
83654c7b
                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
                         if (t1->data[y][x] & mask){ // newly significant
                             int xorbit;
d42106c3
                             int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
83654c7b
                             *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
62d00aa2
                             ff_jpeg2000_set_significance(t1, x, y, t1->flags[y+1][x+1] >> 15);
83654c7b
                         }
                     }
78c7bff0
                     t1->flags[y+1][x+1] &= ~JPEG2000_T1_VIS;
83654c7b
                 }
             }
         }
 }
 
0ab0ed2b
 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
83654c7b
                         int width, int height, int bandpos, int lev)
 {
     int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
     int64_t wmsedec = 0;
 
     for (y = 0; y < height+2; y++)
         memset(t1->flags[y], 0, (width+2)*sizeof(int));
 
     for (y = 0; y < height; y++){
         for (x = 0; x < width; x++){
             if (t1->data[y][x] < 0){
78c7bff0
                 t1->flags[y+1][x+1] |= JPEG2000_T1_SGN;
83654c7b
                 t1->data[y][x] = -t1->data[y][x];
             }
             max = FFMAX(max, t1->data[y][x]);
         }
     }
 
     if (max == 0){
         cblk->nonzerobits = 0;
         bpno = 0;
     } else{
         cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
         bpno = cblk->nonzerobits - 1;
     }
 
     ff_mqc_initenc(&t1->mqc, cblk->data);
 
     for (passno = 0; bpno >= 0; passno++){
         nmsedec=0;
 
         switch(pass_t){
             case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
                     break;
             case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
                     break;
             case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
                     break;
         }
 
         cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
         wmsedec += (int64_t)nmsedec << (2*bpno);
         cblk->passes[passno].disto = wmsedec;
 
         if (++pass_t == 3){
             pass_t = 0;
             bpno--;
         }
     }
     cblk->npasses = passno;
     cblk->ninclpasses = passno;
 
     // TODO: optional flush on each pass
     cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
 }
 
 /* tier-2 routines: */
 
0ab0ed2b
 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
83654c7b
 {
     if (n == 1)
         put_num(s, 0, 1);
     else if (n == 2)
         put_num(s, 2, 2);
     else if (n <= 5)
         put_num(s, 0xc | (n-3), 4);
     else if (n <= 36)
         put_num(s, 0x1e0 | (n-6), 9);
     else
         put_num(s, 0xff80 | (n-37), 16);
 }
 
 
0ab0ed2b
 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
83654c7b
                           uint8_t *expn, int numgbits)
 {
     int bandno, empty = 1;
 
     // init bitstream
     *s->buf = 0;
     s->bit_index = 0;
 
     // header
 
     // is the packet empty?
     for (bandno = 0; bandno < rlevel->nbands; bandno++){
         if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
         &&  rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
             empty = 0;
             break;
         }
     }
 
     put_bits(s, !empty, 1);
     if (empty){
         j2k_flush(s);
         return 0;
     }
 
     for (bandno = 0; bandno < rlevel->nbands; bandno++){
0ab0ed2b
         Jpeg2000Band *band = rlevel->band + bandno;
         Jpeg2000Prec *prec = band->prec + precno;
83654c7b
         int yi, xi, pos;
8c2e201c
         int cblknw = prec->nb_codeblocks_width;
83654c7b
 
         if (band->coord[0][0] == band->coord[0][1]
         ||  band->coord[1][0] == band->coord[1][1])
             continue;
 
8c2e201c
         for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
             for (xi = 0; xi < cblknw; xi++, pos++){
cbaa0871
                 prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
83654c7b
                 tag_tree_update(prec->cblkincl + pos);
cbaa0871
                 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
83654c7b
                 tag_tree_update(prec->zerobits + pos);
             }
         }
 
8c2e201c
         for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
             for (xi = 0; xi < cblknw; xi++, pos++){
83654c7b
                 int pad = 0, llen, length;
cbaa0871
                 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
83654c7b
 
                 if (s->buf_end - s->buf < 20) // approximately
                     return -1;
 
                 // inclusion information
                 tag_tree_code(s, prec->cblkincl + pos, 1);
                 if (!cblk->ninclpasses)
                     continue;
                 // zerobits information
                 tag_tree_code(s, prec->zerobits + pos, 100);
                 // number of passes
                 putnumpasses(s, cblk->ninclpasses);
 
                 length = cblk->passes[cblk->ninclpasses-1].rate;
                 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
                 if (llen < 0){
                     pad = -llen;
                     llen = 0;
                 }
                 // length of code block
                 put_bits(s, 1, llen);
                 put_bits(s, 0, 1);
                 put_num(s, length, av_log2(length)+1+pad);
             }
         }
     }
     j2k_flush(s);
     for (bandno = 0; bandno < rlevel->nbands; bandno++){
0ab0ed2b
         Jpeg2000Band *band = rlevel->band + bandno;
         Jpeg2000Prec *prec = band->prec + precno;
8c2e201c
         int yi, cblknw = prec->nb_codeblocks_width;
         for (yi =0; yi < prec->nb_codeblocks_height; yi++){
83654c7b
             int xi;
8c2e201c
             for (xi = 0; xi < cblknw; xi++){
cbaa0871
                 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
83654c7b
                 if (cblk->ninclpasses){
                     if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
                         return -1;
                     bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
                 }
             }
         }
     }
     return 0;
 }
 
0ab0ed2b
 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
83654c7b
 {
     int compno, reslevelno, ret;
0ab0ed2b
     Jpeg2000CodingStyle *codsty = &s->codsty;
     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
83654c7b
 
     av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
     // lay-rlevel-comp-pos progression
     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
         for (compno = 0; compno < s->ncomponents; compno++){
             int precno;
0ab0ed2b
             Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
83654c7b
             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
                 if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
                               qntsty->nguardbits))
                     return ret;
             }
         }
     }
     av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
     return 0;
 }
 
0ab0ed2b
 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
83654c7b
 {
     int passno, res = 0;
     for (passno = 0; passno < cblk->npasses; passno++){
         int dr;
         int64_t dd;
 
         dr = cblk->passes[passno].rate
            - (res ? cblk->passes[res-1].rate:0);
         dd = cblk->passes[passno].disto
            - (res ? cblk->passes[res-1].disto:0);
 
         if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
             res = passno+1;
     }
     return res;
 }
 
0ab0ed2b
 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
83654c7b
 {
cbaa0871
     int precno, compno, reslevelno, bandno, cblkno, lev;
0ab0ed2b
     Jpeg2000CodingStyle *codsty = &s->codsty;
83654c7b
 
     for (compno = 0; compno < s->ncomponents; compno++){
0ab0ed2b
         Jpeg2000Component *comp = tile->comp + compno;
83654c7b
 
         for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
0ab0ed2b
             Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
83654c7b
 
cbaa0871
             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
                 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
                     int bandpos = bandno + (reslevelno > 0);
                     Jpeg2000Band *band = reslevel->band + bandno;
                     Jpeg2000Prec *prec = band->prec + precno;
83654c7b
 
cbaa0871
                     for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
83654c7b
 
cbaa0871
                         cblk->ninclpasses = getcut(cblk, s->lambda,
f57119b8
                                 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
cbaa0871
                     }
83654c7b
                 }
             }
         }
     }
 }
 
0ab0ed2b
 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
83654c7b
 {
     int compno, reslevelno, bandno, ret;
0ab0ed2b
     Jpeg2000T1Context t1;
     Jpeg2000CodingStyle *codsty = &s->codsty;
83654c7b
     for (compno = 0; compno < s->ncomponents; compno++){
0ab0ed2b
         Jpeg2000Component *comp = s->tile[tileno].comp + compno;
83654c7b
 
         av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
8bedbb82
         if (ret = ff_dwt_encode(&comp->dwt, comp->i_data))
83654c7b
             return ret;
         av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
 
         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
0ab0ed2b
             Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
83654c7b
 
             for (bandno = 0; bandno < reslevel->nbands ; bandno++){
0ab0ed2b
                 Jpeg2000Band *band = reslevel->band + bandno;
c2e94209
                 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
83654c7b
                 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
                 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
                 y0 = yy0;
bd89b2b2
                 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
83654c7b
                             band->coord[1][1]) - band->coord[1][0] + yy0;
 
                 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
                     continue;
 
                 bandpos = bandno + (reslevelno > 0);
 
c2e94209
                 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
83654c7b
                     if (reslevelno == 0 || bandno == 1)
                         xx0 = 0;
                     else
                         xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
                     x0 = xx0;
bd89b2b2
                     xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
83654c7b
                                 band->coord[0][1]) - band->coord[0][0] + xx0;
 
c2e94209
                     for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
83654c7b
                         int y, x;
                         if (codsty->transform == FF_DWT53){
                             for (y = yy0; y < yy1; y++){
                                 int *ptr = t1.data[y-yy0];
                                 for (x = xx0; x < xx1; x++){
8bedbb82
                                     *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
83654c7b
                                 }
                             }
                         } else{
                             for (y = yy0; y < yy1; y++){
                                 int *ptr = t1.data[y-yy0];
                                 for (x = xx0; x < xx1; x++){
8bedbb82
                                     *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
f57119b8
                                     *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
151469db
                                     ptr++;
83654c7b
                                 }
                             }
                         }
c2e94209
                         encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
83654c7b
                                     bandpos, codsty->nreslevels - reslevelno - 1);
                         xx0 = xx1;
bd89b2b2
                         xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
83654c7b
                     }
                     yy0 = yy1;
bd89b2b2
                     yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
83654c7b
                 }
             }
         }
         av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
     }
 
     av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
     truncpasses(s, tile);
     if (ret = encode_packets(s, tile, tileno))
         return ret;
     av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
     return 0;
 }
 
0ab0ed2b
 static void cleanup(Jpeg2000EncoderContext *s)
83654c7b
 {
     int tileno, compno;
0ab0ed2b
     Jpeg2000CodingStyle *codsty = &s->codsty;
83654c7b
 
     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
         for (compno = 0; compno < s->ncomponents; compno++){
0ab0ed2b
             Jpeg2000Component *comp = s->tile[tileno].comp + compno;
62d00aa2
             ff_jpeg2000_cleanup(comp, codsty);
83654c7b
         }
         av_freep(&s->tile[tileno].comp);
     }
     av_freep(&s->tile);
 }
 
0ab0ed2b
 static void reinit(Jpeg2000EncoderContext *s)
83654c7b
 {
     int tileno, compno;
     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
0ab0ed2b
         Jpeg2000Tile *tile = s->tile + tileno;
83654c7b
         for (compno = 0; compno < s->ncomponents; compno++)
62d00aa2
             ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
83654c7b
     }
 }
 
80604030
 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                         const AVFrame *pict, int *got_packet)
83654c7b
 {
     int tileno, ret;
0ab0ed2b
     Jpeg2000EncoderContext *s = avctx->priv_data;
83654c7b
 
ae2c33b0
     if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
80604030
         return ret;
 
83654c7b
     // init:
80604030
     s->buf = s->buf_start = pkt->data;
     s->buf_end = pkt->data + pkt->size;
83654c7b
 
8443b270
     s->picture = pict;
83654c7b
 
8443b270
     s->lambda = s->picture->quality * LAMBDA_SCALE;
83654c7b
 
     copy_frame(s);
     reinit(s);
 
     if (s->buf_end - s->buf < 2)
         return -1;
78c7bff0
     bytestream_put_be16(&s->buf, JPEG2000_SOC);
83654c7b
     if (ret = put_siz(s))
         return ret;
     if (ret = put_cod(s))
         return ret;
     if (ret = put_qcd(s, 0))
         return ret;
 
     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
         uint8_t *psotptr;
7393b417
         if (!(psotptr = put_sot(s, tileno)))
             return -1;
83654c7b
         if (s->buf_end - s->buf < 2)
             return -1;
78c7bff0
         bytestream_put_be16(&s->buf, JPEG2000_SOD);
83654c7b
         if (ret = encode_tile(s, s->tile + tileno, tileno))
             return ret;
         bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
     }
     if (s->buf_end - s->buf < 2)
         return -1;
78c7bff0
     bytestream_put_be16(&s->buf, JPEG2000_EOC);
83654c7b
 
     av_log(s->avctx, AV_LOG_DEBUG, "end\n");
80604030
     pkt->size = s->buf - s->buf_start;
     pkt->flags |= AV_PKT_FLAG_KEY;
     *got_packet = 1;
 
     return 0;
83654c7b
 }
 
 static av_cold int j2kenc_init(AVCodecContext *avctx)
 {
     int i, ret;
0ab0ed2b
     Jpeg2000EncoderContext *s = avctx->priv_data;
     Jpeg2000CodingStyle *codsty = &s->codsty;
     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
83654c7b
 
     s->avctx = avctx;
     av_log(s->avctx, AV_LOG_DEBUG, "init\n");
 
     // defaults:
     // TODO: implement setting non-standard precinct size
bd89b2b2
     memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
     memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
8cf57efd
     codsty->nreslevels2decode=
83654c7b
     codsty->nreslevels       = 7;
     codsty->log2_cblk_width  = 4;
     codsty->log2_cblk_height = 4;
83fd377c
     codsty->transform        = avctx->prediction_method ? FF_DWT53 : FF_DWT97_INT;
83654c7b
 
     qntsty->nguardbits       = 1;
 
     s->tile_width            = 256;
     s->tile_height           = 256;
 
     if (codsty->transform == FF_DWT53)
78c7bff0
         qntsty->quantsty = JPEG2000_QSTY_NONE;
83654c7b
     else
78c7bff0
         qntsty->quantsty = JPEG2000_QSTY_SE;
83654c7b
 
     s->width = avctx->width;
     s->height = avctx->height;
 
     for (i = 0; i < 3; i++)
         s->cbps[i] = 8;
 
ac627b3d
     if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
83654c7b
         s->ncomponents = 3;
ac627b3d
     } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
83654c7b
         s->ncomponents = 1;
     } else{ // planar YUV
         s->planar = 1;
         s->ncomponents = 3;
         avcodec_get_chroma_sub_sample(avctx->pix_fmt,
                 s->chroma_shift, s->chroma_shift + 1);
     }
 
e66faf71
     ff_jpeg2000_init_tier1_luts();
dd1382ac
     ff_mqc_init_context_tables();
83654c7b
     init_luts();
 
     init_quantization(s);
     if (ret=init_tiles(s))
         return ret;
 
     av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
 
     return 0;
 }
 
 static int j2kenc_destroy(AVCodecContext *avctx)
 {
0ab0ed2b
     Jpeg2000EncoderContext *s = avctx->priv_data;
83654c7b
 
     cleanup(s);
     return 0;
 }
 
9d56ccf5
 AVCodec ff_jpeg2000_encoder = {
     .name           = "jpeg2000",
b46f1910
     .long_name      = NULL_IF_CONFIG_SMALL("JPEG 2000"),
ba10207b
     .type           = AVMEDIA_TYPE_VIDEO,
7a72695c
     .id             = AV_CODEC_ID_JPEG2000,
0ab0ed2b
     .priv_data_size = sizeof(Jpeg2000EncoderContext),
ba10207b
     .init           = j2kenc_init,
80604030
     .encode2        = encode_frame,
ba10207b
     .close          = j2kenc_destroy,
7f9b42ff
     .capabilities   = CODEC_CAP_EXPERIMENTAL,
ac627b3d
     .pix_fmts       = (const enum AVPixelFormat[]) {
         AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
 /*      AV_PIX_FMT_YUV420P,
         AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,*/
         AV_PIX_FMT_NONE
f06269dd
     }
83654c7b
 };