]> git.sesse.net Git - ffmpeg/blob - libavcodec/j2kenc.c
libavcodec/j2kenc: Allow Encoder to use SOP and EPH markers
[ffmpeg] / libavcodec / j2kenc.c
1 /*
2  * JPEG2000 image encoder
3  * Copyright (c) 2007 Kamil Nowosad
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * **********************************************************************************************************************
22  *
23  *
24  *
25  * This source code incorporates work covered by the following copyright and
26  * permission notice:
27  *
28  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
29  * Copyright (c) 2002-2007, Professor Benoit Macq
30  * Copyright (c) 2001-2003, David Janssens
31  * Copyright (c) 2002-2003, Yannick Verschueren
32  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
33  * Copyright (c) 2005, Herve Drolon, FreeImage Team
34  * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
47  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56  * POSSIBILITY OF SUCH DAMAGE.
57  */
58
59
60 /**
61  * JPEG2000 image encoder
62  * @file
63  * @author Kamil Nowosad
64  */
65
66 #include <float.h>
67 #include "avcodec.h"
68 #include "internal.h"
69 #include "bytestream.h"
70 #include "jpeg2000.h"
71 #include "libavutil/common.h"
72 #include "libavutil/pixdesc.h"
73 #include "libavutil/opt.h"
74 #include "libavutil/intreadwrite.h"
75
76 #define NMSEDEC_BITS 7
77 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
78 #define WMSEDEC_SHIFT 13 ///< must be >= 13
79 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
80
81 #define CODEC_JP2 1
82 #define CODEC_J2K 0
83
84 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
85            lut_nmsedec_ref0[1<<NMSEDEC_BITS],
86            lut_nmsedec_sig [1<<NMSEDEC_BITS],
87            lut_nmsedec_sig0[1<<NMSEDEC_BITS];
88
89 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
90     {{10000, 19650, 41770,  84030, 169000, 338400,  676900, 1353000, 2706000, 5409000},
91      {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
92      {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
93      {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
94
95     {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
96      {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
97      {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
98      { 7186,  9218, 15860, 30430,  60190, 120100, 240000, 479700,  959300}}
99 };
100
101 typedef struct {
102    Jpeg2000Component *comp;
103 } Jpeg2000Tile;
104
105 typedef struct {
106     AVClass *class;
107     AVCodecContext *avctx;
108     const AVFrame *picture;
109
110     int width, height; ///< image width and height
111     uint8_t cbps[4]; ///< bits per sample in particular components
112     int chroma_shift[2];
113     uint8_t planar;
114     int ncomponents;
115     int tile_width, tile_height; ///< tile size
116     int numXtiles, numYtiles;
117
118     uint8_t *buf_start;
119     uint8_t *buf;
120     uint8_t *buf_end;
121     int bit_index;
122
123     int64_t lambda;
124
125     Jpeg2000CodingStyle codsty;
126     Jpeg2000QuantStyle  qntsty;
127
128     Jpeg2000Tile *tile;
129
130     int format;
131     int pred;
132     int sop;
133     int eph;
134 } Jpeg2000EncoderContext;
135
136
137 /* debug */
138 #if 0
139 #undef ifprintf
140 #undef printf
141
142 static void nspaces(FILE *fd, int n)
143 {
144     while(n--) putc(' ', fd);
145 }
146
147 static void printcomp(Jpeg2000Component *comp)
148 {
149     int i;
150     for (i = 0; i < comp->y1 - comp->y0; i++)
151         ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
152 }
153
154 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
155 {
156     int tileno, compno, reslevelno, bandno, precno;
157     fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
158                 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
159                 "tiles:\n",
160             s->width, s->height, s->tile_width, s->tile_height,
161             s->numXtiles, s->numYtiles, s->ncomponents);
162     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
163         Jpeg2000Tile *tile = s->tile + tileno;
164         nspaces(fd, 2);
165         fprintf(fd, "tile %d:\n", tileno);
166         for(compno = 0; compno < s->ncomponents; compno++){
167             Jpeg2000Component *comp = tile->comp + compno;
168             nspaces(fd, 4);
169             fprintf(fd, "component %d:\n", compno);
170             nspaces(fd, 4);
171             fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
172                         comp->x0, comp->x1, comp->y0, comp->y1);
173             for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
174                 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
175                 nspaces(fd, 6);
176                 fprintf(fd, "reslevel %d:\n", reslevelno);
177                 nspaces(fd, 6);
178                 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
179                         reslevel->x0, reslevel->x1, reslevel->y0,
180                         reslevel->y1, reslevel->nbands);
181                 for(bandno = 0; bandno < reslevel->nbands; bandno++){
182                     Jpeg2000Band *band = reslevel->band + bandno;
183                     nspaces(fd, 8);
184                     fprintf(fd, "band %d:\n", bandno);
185                     nspaces(fd, 8);
186                     fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
187                                 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
188                                 band->x0, band->x1,
189                                 band->y0, band->y1,
190                                 band->codeblock_width, band->codeblock_height,
191                                 band->cblknx, band->cblkny);
192                     for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
193                         Jpeg2000Prec *prec = band->prec + precno;
194                         nspaces(fd, 10);
195                         fprintf(fd, "prec %d:\n", precno);
196                         nspaces(fd, 10);
197                         fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
198                                      prec->xi0, prec->xi1, prec->yi0, prec->yi1);
199                     }
200                 }
201             }
202         }
203     }
204 }
205 #endif
206
207 /* bitstream routines */
208
209 /** put n times val bit */
210 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
211 {
212     while (n-- > 0){
213         if (s->bit_index == 8)
214         {
215             s->bit_index = *s->buf == 0xff;
216             *(++s->buf) = 0;
217         }
218         *s->buf |= val << (7 - s->bit_index++);
219     }
220 }
221
222 /** put n least significant bits of a number num */
223 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
224 {
225     while(--n >= 0)
226         put_bits(s, (num >> n) & 1, 1);
227 }
228
229 /** flush the bitstream */
230 static void j2k_flush(Jpeg2000EncoderContext *s)
231 {
232     if (s->bit_index){
233         s->bit_index = 0;
234         s->buf++;
235     }
236 }
237
238 /* tag tree routines */
239
240 /** code the value stored in node */
241 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
242 {
243     Jpeg2000TgtNode *stack[30];
244     int sp = 1, curval = 0;
245     stack[0] = node;
246
247     node = node->parent;
248     while(node){
249         if (node->vis){
250             curval = node->val;
251             break;
252         }
253         node->vis++;
254         stack[sp++] = node;
255         node = node->parent;
256     }
257     while(--sp >= 0){
258         if (stack[sp]->val >= threshold){
259             put_bits(s, 0, threshold - curval);
260             break;
261         }
262         put_bits(s, 0, stack[sp]->val - curval);
263         put_bits(s, 1, 1);
264         curval = stack[sp]->val;
265     }
266 }
267
268 /** update the value in node */
269 static void tag_tree_update(Jpeg2000TgtNode *node)
270 {
271     int lev = 0;
272     while (node->parent){
273         if (node->parent->val <= node->val)
274             break;
275         node->parent->val = node->val;
276         node = node->parent;
277         lev++;
278     }
279 }
280
281 static int put_siz(Jpeg2000EncoderContext *s)
282 {
283     int i;
284
285     if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
286         return -1;
287
288     bytestream_put_be16(&s->buf, JPEG2000_SIZ);
289     bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
290     bytestream_put_be16(&s->buf, 0); // Rsiz
291     bytestream_put_be32(&s->buf, s->width); // width
292     bytestream_put_be32(&s->buf, s->height); // height
293     bytestream_put_be32(&s->buf, 0); // X0Siz
294     bytestream_put_be32(&s->buf, 0); // Y0Siz
295
296     bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
297     bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
298     bytestream_put_be32(&s->buf, 0); // XT0Siz
299     bytestream_put_be32(&s->buf, 0); // YT0Siz
300     bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
301
302     for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
303         bytestream_put_byte(&s->buf, s->cbps[i] - 1);
304         bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
305         bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
306     }
307     return 0;
308 }
309
310 static int put_cod(Jpeg2000EncoderContext *s)
311 {
312     Jpeg2000CodingStyle *codsty = &s->codsty;
313     uint8_t scod = 0;
314
315     if (s->buf_end - s->buf < 14)
316         return -1;
317
318     bytestream_put_be16(&s->buf, JPEG2000_COD);
319     bytestream_put_be16(&s->buf, 12); // Lcod
320     if (s->sop)
321         scod |= JPEG2000_CSTY_SOP;
322     if (s->eph)
323         scod |= JPEG2000_CSTY_EPH;
324     bytestream_put_byte(&s->buf, scod);  // Scod
325     // SGcod
326     bytestream_put_byte(&s->buf, 0); // progression level
327     bytestream_put_be16(&s->buf, 1); // num of layers
328     if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
329         bytestream_put_byte(&s->buf, 0); // unspecified
330     }else{
331         bytestream_put_byte(&s->buf, 0); // unspecified
332     }
333     // SPcod
334     bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
335     bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
336     bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
337     bytestream_put_byte(&s->buf, 0); // cblk style
338     bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
339     return 0;
340 }
341
342 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
343 {
344     int i, size;
345     Jpeg2000CodingStyle *codsty = &s->codsty;
346     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
347
348     if (qntsty->quantsty == JPEG2000_QSTY_NONE)
349         size = 4 + 3 * (codsty->nreslevels-1);
350     else // QSTY_SE
351         size = 5 + 6 * (codsty->nreslevels-1);
352
353     if (s->buf_end - s->buf < size + 2)
354         return -1;
355
356     bytestream_put_be16(&s->buf, JPEG2000_QCD);
357     bytestream_put_be16(&s->buf, size);  // LQcd
358     bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty);  // Sqcd
359     if (qntsty->quantsty == JPEG2000_QSTY_NONE)
360         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
361             bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
362     else // QSTY_SE
363         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
364             bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
365     return 0;
366 }
367
368 static int put_com(Jpeg2000EncoderContext *s, int compno)
369 {
370     int size = 4 + strlen(LIBAVCODEC_IDENT);
371
372     if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
373         return 0;
374
375     if (s->buf_end - s->buf < size + 2)
376         return -1;
377
378     bytestream_put_be16(&s->buf, JPEG2000_COM);
379     bytestream_put_be16(&s->buf, size);
380     bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
381
382     bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
383
384     return 0;
385 }
386
387 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
388 {
389     uint8_t *psotptr;
390
391     if (s->buf_end - s->buf < 12)
392         return NULL;
393
394     bytestream_put_be16(&s->buf, JPEG2000_SOT);
395     bytestream_put_be16(&s->buf, 10); // Lsot
396     bytestream_put_be16(&s->buf, tileno); // Isot
397
398     psotptr = s->buf;
399     bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
400
401     bytestream_put_byte(&s->buf, 0); // TPsot
402     bytestream_put_byte(&s->buf, 1); // TNsot
403     return psotptr;
404 }
405
406 /**
407  * compute the sizes of tiles, resolution levels, bands, etc.
408  * allocate memory for them
409  * divide the input image into tile-components
410  */
411 static int init_tiles(Jpeg2000EncoderContext *s)
412 {
413     int tileno, tilex, tiley, compno;
414     Jpeg2000CodingStyle *codsty = &s->codsty;
415     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
416
417     s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
418     s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
419
420     s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
421     if (!s->tile)
422         return AVERROR(ENOMEM);
423     for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
424         for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
425             Jpeg2000Tile *tile = s->tile + tileno;
426
427             tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
428             if (!tile->comp)
429                 return AVERROR(ENOMEM);
430             for (compno = 0; compno < s->ncomponents; compno++){
431                 Jpeg2000Component *comp = tile->comp + compno;
432                 int ret, i, j;
433
434                 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
435                 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
436                 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
437                 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
438                 if (compno > 0)
439                     for (i = 0; i < 2; i++)
440                         for (j = 0; j < 2; j++)
441                             comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
442
443                 if ((ret = ff_jpeg2000_init_component(comp,
444                                                 codsty,
445                                                 qntsty,
446                                                 s->cbps[compno],
447                                                 compno?1<<s->chroma_shift[0]:1,
448                                                 compno?1<<s->chroma_shift[1]:1,
449                                                 s->avctx
450                                                )) < 0)
451                     return ret;
452             }
453         }
454     return 0;
455 }
456
457 #define COPY_FRAME(D, PIXEL)                                                                                                \
458     static void copy_frame_ ##D(Jpeg2000EncoderContext *s)                                                                  \
459     {                                                                                                                       \
460         int tileno, compno, i, y, x;                                                                                        \
461         PIXEL *line;                                                                                                        \
462         for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){                                                   \
463             Jpeg2000Tile *tile = s->tile + tileno;                                                                          \
464             if (s->planar){                                                                                                 \
465                 for (compno = 0; compno < s->ncomponents; compno++){                                                        \
466                     Jpeg2000Component *comp = tile->comp + compno;                                                          \
467                     int *dst = comp->i_data;                                                                                \
468                     int cbps = s->cbps[compno];                                                                             \
469                     line = (PIXEL*)s->picture->data[compno]                                                                 \
470                            + comp->coord[1][0] * (s->picture->linesize[compno] / sizeof(PIXEL))                             \
471                            + comp->coord[0][0];                                                                             \
472                     for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){                                                \
473                         PIXEL *ptr = line;                                                                                  \
474                         for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)                                             \
475                             *dst++ = *ptr++ - (1 << (cbps - 1));                                                            \
476                         line += s->picture->linesize[compno] / sizeof(PIXEL);                                               \
477                     }                                                                                                       \
478                 }                                                                                                           \
479             } else{                                                                                                         \
480                 line = (PIXEL*)s->picture->data[0] + tile->comp[0].coord[1][0] * (s->picture->linesize[0] / sizeof(PIXEL))  \
481                        + tile->comp[0].coord[0][0] * s->ncomponents;                                                        \
482                                                                                                                             \
483                 i = 0;                                                                                                      \
484                 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){                                    \
485                     PIXEL *ptr = line;                                                                                      \
486                     for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){                           \
487                         for (compno = 0; compno < s->ncomponents; compno++){                                                \
488                             int cbps = s->cbps[compno];                                                                     \
489                             tile->comp[compno].i_data[i] = *ptr++  - (1 << (cbps - 1));                                     \
490                         }                                                                                                   \
491                     }                                                                                                       \
492                     line += s->picture->linesize[0] / sizeof(PIXEL);                                                        \
493                 }                                                                                                           \
494             }                                                                                                               \
495         }                                                                                                                   \
496     }
497
498 COPY_FRAME(8, uint8_t)
499 COPY_FRAME(16, uint16_t)
500
501 static void init_quantization(Jpeg2000EncoderContext *s)
502 {
503     int compno, reslevelno, bandno;
504     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
505     Jpeg2000CodingStyle *codsty = &s->codsty;
506
507     for (compno = 0; compno < s->ncomponents; compno++){
508         int gbandno = 0;
509         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
510             int nbands, lev = codsty->nreslevels - reslevelno - 1;
511             nbands = reslevelno ? 3 : 1;
512             for (bandno = 0; bandno < nbands; bandno++, gbandno++){
513                 int expn, mant = 0;
514
515                 if (codsty->transform == FF_DWT97_INT){
516                     int bandpos = bandno + (reslevelno>0),
517                         ss = 81920000 / dwt_norms[0][bandpos][lev],
518                         log = av_log2(ss);
519                     mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
520                     expn = s->cbps[compno] - log + 13;
521                 } else
522                     expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
523
524                 qntsty->expn[gbandno] = expn;
525                 qntsty->mant[gbandno] = mant;
526             }
527         }
528     }
529 }
530
531 static void init_luts(void)
532 {
533     int i, a,
534         mask = ~((1<<NMSEDEC_FRACBITS)-1);
535
536     for (i = 0; i < (1 << NMSEDEC_BITS); i++){
537         lut_nmsedec_sig[i]  = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
538         lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
539
540         a = (i >> (NMSEDEC_BITS-2)&2) + 1;
541         lut_nmsedec_ref[i]  = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
542                                     (1 << 13) - (a * a << 11), 0);
543         lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
544                                     << 1, 0);
545     }
546 }
547
548 /* tier-1 routines */
549 static int getnmsedec_sig(int x, int bpno)
550 {
551     if (bpno > NMSEDEC_FRACBITS)
552         return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
553     return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
554 }
555
556 static int getnmsedec_ref(int x, int bpno)
557 {
558     if (bpno > NMSEDEC_FRACBITS)
559         return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
560     return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
561 }
562
563 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
564 {
565     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
566     for (y0 = 0; y0 < height; y0 += 4)
567         for (x = 0; x < width; x++)
568             for (y = y0; y < height && y < y0+4; y++){
569                 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
570                     int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
571                         bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
572                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
573                     if (bit){
574                         int xorbit;
575                         int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
576                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
577                         *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
578                         ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
579                     }
580                     t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
581                 }
582             }
583 }
584
585 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
586 {
587     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
588     for (y0 = 0; y0 < height; y0 += 4)
589         for (x = 0; x < width; x++)
590             for (y = y0; y < height && y < y0+4; y++)
591                 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
592                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
593                     *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
594                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
595                     t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
596                 }
597 }
598
599 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
600 {
601     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
602     for (y0 = 0; y0 < height; y0 += 4)
603         for (x = 0; x < width; x++){
604             if (y0 + 3 < height && !(
605             (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
606             (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
607             (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
608             (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
609             {
610                 // aggregation mode
611                 int rlen;
612                 for (rlen = 0; rlen < 4; rlen++)
613                     if (t1->data[(y0+rlen) * t1->stride + x] & mask)
614                         break;
615                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
616                 if (rlen == 4)
617                     continue;
618                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
619                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
620                 for (y = y0 + rlen; y < y0 + 4; y++){
621                     if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
622                         int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
623                         if (y > y0 + rlen)
624                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
625                         if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
626                             int xorbit;
627                             int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
628                             *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
629                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
630                             ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
631                         }
632                     }
633                     t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
634                 }
635             } else{
636                 for (y = y0; y < y0 + 4 && y < height; y++){
637                     if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
638                         int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
639                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
640                         if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
641                             int xorbit;
642                             int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
643                             *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
644                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
645                             ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
646                         }
647                     }
648                     t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
649                 }
650             }
651         }
652 }
653
654 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
655                         int width, int height, int bandpos, int lev)
656 {
657     int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
658     int64_t wmsedec = 0;
659
660     memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
661
662     for (y = 0; y < height; y++){
663         for (x = 0; x < width; x++){
664             if (t1->data[(y) * t1->stride + x] < 0){
665                 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
666                 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
667             }
668             max = FFMAX(max, t1->data[(y) * t1->stride + x]);
669         }
670     }
671
672     if (max == 0){
673         cblk->nonzerobits = 0;
674         bpno = 0;
675     } else{
676         cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
677         bpno = cblk->nonzerobits - 1;
678     }
679
680     cblk->data[0] = 0;
681     ff_mqc_initenc(&t1->mqc, cblk->data + 1);
682
683     for (passno = 0; bpno >= 0; passno++){
684         nmsedec=0;
685
686         switch(pass_t){
687             case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
688                     break;
689             case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
690                     break;
691             case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
692                     break;
693         }
694
695         cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
696         wmsedec += (int64_t)nmsedec << (2*bpno);
697         cblk->passes[passno].disto = wmsedec;
698
699         if (++pass_t == 3){
700             pass_t = 0;
701             bpno--;
702         }
703     }
704     cblk->npasses = passno;
705     cblk->ninclpasses = passno;
706
707     if (passno)
708         cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
709 }
710
711 /* tier-2 routines: */
712
713 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
714 {
715     if (n == 1)
716         put_num(s, 0, 1);
717     else if (n == 2)
718         put_num(s, 2, 2);
719     else if (n <= 5)
720         put_num(s, 0xc | (n-3), 4);
721     else if (n <= 36)
722         put_num(s, 0x1e0 | (n-6), 9);
723     else
724         put_num(s, 0xff80 | (n-37), 16);
725 }
726
727
728 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
729                           uint8_t *expn, int numgbits, int packetno)
730 {
731     int bandno, empty = 1;
732     // init bitstream
733     *s->buf = 0;
734     s->bit_index = 0;
735
736     if (s->sop) {
737         bytestream_put_be16(&s->buf, JPEG2000_SOP);
738         bytestream_put_be16(&s->buf, 4);
739         bytestream_put_be16(&s->buf, packetno);
740     }
741     // header
742
743     // is the packet empty?
744     for (bandno = 0; bandno < rlevel->nbands; bandno++){
745         if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
746         &&  rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
747             empty = 0;
748             break;
749         }
750     }
751
752     put_bits(s, !empty, 1);
753     if (empty){
754         j2k_flush(s);
755         return 0;
756     }
757
758     for (bandno = 0; bandno < rlevel->nbands; bandno++){
759         Jpeg2000Band *band = rlevel->band + bandno;
760         Jpeg2000Prec *prec = band->prec + precno;
761         int yi, xi, pos;
762         int cblknw = prec->nb_codeblocks_width;
763
764         if (band->coord[0][0] == band->coord[0][1]
765         ||  band->coord[1][0] == band->coord[1][1])
766             continue;
767
768         for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
769             for (xi = 0; xi < cblknw; xi++, pos++){
770                 prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
771                 tag_tree_update(prec->cblkincl + pos);
772                 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
773                 tag_tree_update(prec->zerobits + pos);
774             }
775         }
776
777         for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
778             for (xi = 0; xi < cblknw; xi++, pos++){
779                 int pad = 0, llen, length;
780                 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
781
782                 if (s->buf_end - s->buf < 20) // approximately
783                     return -1;
784
785                 // inclusion information
786                 tag_tree_code(s, prec->cblkincl + pos, 1);
787                 if (!cblk->ninclpasses)
788                     continue;
789                 // zerobits information
790                 tag_tree_code(s, prec->zerobits + pos, 100);
791                 // number of passes
792                 putnumpasses(s, cblk->ninclpasses);
793
794                 length = cblk->passes[cblk->ninclpasses-1].rate;
795                 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
796                 if (llen < 0){
797                     pad = -llen;
798                     llen = 0;
799                 }
800                 // length of code block
801                 put_bits(s, 1, llen);
802                 put_bits(s, 0, 1);
803                 put_num(s, length, av_log2(length)+1+pad);
804             }
805         }
806     }
807     j2k_flush(s);
808     if (s->eph) {
809         bytestream_put_be16(&s->buf, JPEG2000_EPH);
810     }
811
812     for (bandno = 0; bandno < rlevel->nbands; bandno++){
813         Jpeg2000Band *band = rlevel->band + bandno;
814         Jpeg2000Prec *prec = band->prec + precno;
815         int yi, cblknw = prec->nb_codeblocks_width;
816         for (yi =0; yi < prec->nb_codeblocks_height; yi++){
817             int xi;
818             for (xi = 0; xi < cblknw; xi++){
819                 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
820                 if (cblk->ninclpasses){
821                     if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
822                         return -1;
823                     bytestream_put_buffer(&s->buf, cblk->data + 1,   cblk->passes[cblk->ninclpasses-1].rate
824                                                                - cblk->passes[cblk->ninclpasses-1].flushed_len);
825                     bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
826                                                    cblk->passes[cblk->ninclpasses-1].flushed_len);
827                 }
828             }
829         }
830     }
831     return 0;
832 }
833
834 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
835 {
836     int compno, reslevelno, ret;
837     Jpeg2000CodingStyle *codsty = &s->codsty;
838     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
839     int packetno = 0;
840     av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
841     // lay-rlevel-comp-pos progression
842     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
843         for (compno = 0; compno < s->ncomponents; compno++){
844             int precno;
845             Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
846             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
847                 if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
848                               qntsty->nguardbits, packetno++)) < 0)
849                     return ret;
850             }
851         }
852     }
853     av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
854     return 0;
855 }
856
857 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
858 {
859     int passno, res = 0;
860     for (passno = 0; passno < cblk->npasses; passno++){
861         int dr;
862         int64_t dd;
863
864         dr = cblk->passes[passno].rate
865            - (res ? cblk->passes[res-1].rate:0);
866         dd = cblk->passes[passno].disto
867            - (res ? cblk->passes[res-1].disto:0);
868
869         if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
870             res = passno+1;
871     }
872     return res;
873 }
874
875 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
876 {
877     int precno, compno, reslevelno, bandno, cblkno, lev;
878     Jpeg2000CodingStyle *codsty = &s->codsty;
879
880     for (compno = 0; compno < s->ncomponents; compno++){
881         Jpeg2000Component *comp = tile->comp + compno;
882
883         for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
884             Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
885
886             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
887                 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
888                     int bandpos = bandno + (reslevelno > 0);
889                     Jpeg2000Band *band = reslevel->band + bandno;
890                     Jpeg2000Prec *prec = band->prec + precno;
891
892                     for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
893                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
894
895                         cblk->ninclpasses = getcut(cblk, s->lambda,
896                                 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
897                     }
898                 }
899             }
900         }
901     }
902 }
903
904 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
905 {
906     int compno, reslevelno, bandno, ret;
907     Jpeg2000T1Context t1;
908     Jpeg2000CodingStyle *codsty = &s->codsty;
909     for (compno = 0; compno < s->ncomponents; compno++){
910         Jpeg2000Component *comp = s->tile[tileno].comp + compno;
911
912         t1.stride = (1<<codsty->log2_cblk_width) + 2;
913
914         av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
915         if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
916             return ret;
917         av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
918
919         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
920             Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
921
922             for (bandno = 0; bandno < reslevel->nbands ; bandno++){
923                 Jpeg2000Band *band = reslevel->band + bandno;
924                 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
925                 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
926                 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
927                 y0 = yy0;
928                 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
929                             band->coord[1][1]) - band->coord[1][0] + yy0;
930
931                 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
932                     continue;
933
934                 bandpos = bandno + (reslevelno > 0);
935
936                 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
937                     if (reslevelno == 0 || bandno == 1)
938                         xx0 = 0;
939                     else
940                         xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
941                     x0 = xx0;
942                     xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
943                                 band->coord[0][1]) - band->coord[0][0] + xx0;
944
945                     for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
946                         int y, x;
947                         if (codsty->transform == FF_DWT53){
948                             for (y = yy0; y < yy1; y++){
949                                 int *ptr = t1.data + (y-yy0)*t1.stride;
950                                 for (x = xx0; x < xx1; x++){
951                                     *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
952                                 }
953                             }
954                         } else{
955                             for (y = yy0; y < yy1; y++){
956                                 int *ptr = t1.data + (y-yy0)*t1.stride;
957                                 for (x = xx0; x < xx1; x++){
958                                     *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
959                                     *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
960                                     ptr++;
961                                 }
962                             }
963                         }
964                         if (!prec->cblk[cblkno].data)
965                             prec->cblk[cblkno].data = av_malloc(1 + 8192);
966                         if (!prec->cblk[cblkno].passes)
967                             prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
968                         if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
969                             return AVERROR(ENOMEM);
970                         encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
971                                     bandpos, codsty->nreslevels - reslevelno - 1);
972                         xx0 = xx1;
973                         xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
974                     }
975                     yy0 = yy1;
976                     yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
977                 }
978             }
979         }
980         av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
981     }
982
983     av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
984     truncpasses(s, tile);
985     if ((ret = encode_packets(s, tile, tileno)) < 0)
986         return ret;
987     av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
988     return 0;
989 }
990
991 static void cleanup(Jpeg2000EncoderContext *s)
992 {
993     int tileno, compno;
994     Jpeg2000CodingStyle *codsty = &s->codsty;
995
996     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
997         for (compno = 0; compno < s->ncomponents; compno++){
998             Jpeg2000Component *comp = s->tile[tileno].comp + compno;
999             ff_jpeg2000_cleanup(comp, codsty);
1000         }
1001         av_freep(&s->tile[tileno].comp);
1002     }
1003     av_freep(&s->tile);
1004 }
1005
1006 static void reinit(Jpeg2000EncoderContext *s)
1007 {
1008     int tileno, compno;
1009     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1010         Jpeg2000Tile *tile = s->tile + tileno;
1011         for (compno = 0; compno < s->ncomponents; compno++)
1012             ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1013     }
1014 }
1015
1016 static void update_size(uint8_t *size, const uint8_t *end)
1017 {
1018     AV_WB32(size, end-size);
1019 }
1020
1021 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1022                         const AVFrame *pict, int *got_packet)
1023 {
1024     int tileno, ret;
1025     Jpeg2000EncoderContext *s = avctx->priv_data;
1026     uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1027
1028     if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1029         return ret;
1030
1031     // init:
1032     s->buf = s->buf_start = pkt->data;
1033     s->buf_end = pkt->data + pkt->size;
1034
1035     s->picture = pict;
1036
1037     s->lambda = s->picture->quality * LAMBDA_SCALE;
1038
1039     if (avctx->pix_fmt == AV_PIX_FMT_BGR48 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
1040         copy_frame_16(s);
1041     else
1042         copy_frame_8(s);
1043
1044     reinit(s);
1045
1046     if (s->format == CODEC_JP2) {
1047         av_assert0(s->buf == pkt->data);
1048
1049         bytestream_put_be32(&s->buf, 0x0000000C);
1050         bytestream_put_be32(&s->buf, 0x6A502020);
1051         bytestream_put_be32(&s->buf, 0x0D0A870A);
1052
1053         chunkstart = s->buf;
1054         bytestream_put_be32(&s->buf, 0);
1055         bytestream_put_buffer(&s->buf, "ftyp", 4);
1056         bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1057         bytestream_put_be32(&s->buf, 0);
1058         bytestream_put_buffer(&s->buf, "jp2\040", 4);
1059         update_size(chunkstart, s->buf);
1060
1061         jp2hstart = s->buf;
1062         bytestream_put_be32(&s->buf, 0);
1063         bytestream_put_buffer(&s->buf, "jp2h", 4);
1064
1065         chunkstart = s->buf;
1066         bytestream_put_be32(&s->buf, 0);
1067         bytestream_put_buffer(&s->buf, "ihdr", 4);
1068         bytestream_put_be32(&s->buf, avctx->height);
1069         bytestream_put_be32(&s->buf, avctx->width);
1070         bytestream_put_be16(&s->buf, s->ncomponents);
1071         bytestream_put_byte(&s->buf, s->cbps[0]);
1072         bytestream_put_byte(&s->buf, 7);
1073         bytestream_put_byte(&s->buf, 0);
1074         bytestream_put_byte(&s->buf, 0);
1075         update_size(chunkstart, s->buf);
1076
1077         chunkstart = s->buf;
1078         bytestream_put_be32(&s->buf, 0);
1079         bytestream_put_buffer(&s->buf, "colr", 4);
1080         bytestream_put_byte(&s->buf, 1);
1081         bytestream_put_byte(&s->buf, 0);
1082         bytestream_put_byte(&s->buf, 0);
1083         if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1084             bytestream_put_be32(&s->buf, 16);
1085         } else if (s->ncomponents == 1) {
1086             bytestream_put_be32(&s->buf, 17);
1087         } else {
1088             bytestream_put_be32(&s->buf, 18);
1089         }
1090         update_size(chunkstart, s->buf);
1091         if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1092             int i;
1093             uint8_t *palette = pict->data[1];
1094             chunkstart = s->buf;
1095             bytestream_put_be32(&s->buf, 0);
1096             bytestream_put_buffer(&s->buf, "pclr", 4);
1097             bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1098             bytestream_put_byte(&s->buf, 3); // colour channels
1099             bytestream_put_be24(&s->buf, 0x070707); //colour depths
1100             for (i = 0; i < AVPALETTE_COUNT; i++) {
1101                 bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1102                 palette += 4;
1103             }
1104             update_size(chunkstart, s->buf);
1105             chunkstart = s->buf;
1106             bytestream_put_be32(&s->buf, 0);
1107             bytestream_put_buffer(&s->buf, "cmap", 4);
1108             for (i = 0; i < 3; i++) {
1109                 bytestream_put_be16(&s->buf, 0); // component
1110                 bytestream_put_byte(&s->buf, 1); // palette mapping
1111                 bytestream_put_byte(&s->buf, i); // index
1112             }
1113             update_size(chunkstart, s->buf);
1114         }
1115         update_size(jp2hstart, s->buf);
1116
1117         jp2cstart = s->buf;
1118         bytestream_put_be32(&s->buf, 0);
1119         bytestream_put_buffer(&s->buf, "jp2c", 4);
1120     }
1121
1122     if (s->buf_end - s->buf < 2)
1123         return -1;
1124     bytestream_put_be16(&s->buf, JPEG2000_SOC);
1125     if ((ret = put_siz(s)) < 0)
1126         return ret;
1127     if ((ret = put_cod(s)) < 0)
1128         return ret;
1129     if ((ret = put_qcd(s, 0)) < 0)
1130         return ret;
1131     if ((ret = put_com(s, 0)) < 0)
1132         return ret;
1133
1134     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1135         uint8_t *psotptr;
1136         if (!(psotptr = put_sot(s, tileno)))
1137             return -1;
1138         if (s->buf_end - s->buf < 2)
1139             return -1;
1140         bytestream_put_be16(&s->buf, JPEG2000_SOD);
1141         if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1142             return ret;
1143         bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1144     }
1145     if (s->buf_end - s->buf < 2)
1146         return -1;
1147     bytestream_put_be16(&s->buf, JPEG2000_EOC);
1148
1149     if (s->format == CODEC_JP2)
1150         update_size(jp2cstart, s->buf);
1151
1152     av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1153     pkt->size = s->buf - s->buf_start;
1154     pkt->flags |= AV_PKT_FLAG_KEY;
1155     *got_packet = 1;
1156
1157     return 0;
1158 }
1159
1160 static av_cold int j2kenc_init(AVCodecContext *avctx)
1161 {
1162     int i, ret;
1163     Jpeg2000EncoderContext *s = avctx->priv_data;
1164     Jpeg2000CodingStyle *codsty = &s->codsty;
1165     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
1166
1167     s->avctx = avctx;
1168     av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1169
1170 #if FF_API_PRIVATE_OPT
1171 FF_DISABLE_DEPRECATION_WARNINGS
1172     if (avctx->prediction_method)
1173         s->pred = avctx->prediction_method;
1174 FF_ENABLE_DEPRECATION_WARNINGS
1175 #endif
1176
1177     if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1178         av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1179         s->pred = FF_DWT97_INT;
1180         s->format = CODEC_JP2;
1181     }
1182
1183     // defaults:
1184     // TODO: implement setting non-standard precinct size
1185     memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1186     memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1187     codsty->nreslevels2decode=
1188     codsty->nreslevels       = 7;
1189     codsty->log2_cblk_width  = 4;
1190     codsty->log2_cblk_height = 4;
1191     codsty->transform        = s->pred ? FF_DWT53 : FF_DWT97_INT;
1192
1193     qntsty->nguardbits       = 1;
1194
1195     if ((s->tile_width  & (s->tile_width -1)) ||
1196         (s->tile_height & (s->tile_height-1))) {
1197         av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1198     }
1199
1200     if (codsty->transform == FF_DWT53)
1201         qntsty->quantsty = JPEG2000_QSTY_NONE;
1202     else
1203         qntsty->quantsty = JPEG2000_QSTY_SE;
1204
1205     s->width = avctx->width;
1206     s->height = avctx->height;
1207
1208     for (i = 0; i < 3; i++) {
1209         if (avctx->pix_fmt == AV_PIX_FMT_GRAY16 || avctx->pix_fmt == AV_PIX_FMT_RGB48)
1210             s->cbps[i] = 16;
1211         else
1212             s->cbps[i] = 8;
1213     }
1214
1215     if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_RGB48){
1216         s->ncomponents = 3;
1217     } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16){
1218         s->ncomponents = 1;
1219     } else{ // planar YUV
1220         s->planar = 1;
1221         s->ncomponents = 3;
1222         ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
1223                                                s->chroma_shift, s->chroma_shift + 1);
1224         if (ret)
1225             return ret;
1226     }
1227
1228     ff_jpeg2000_init_tier1_luts();
1229     ff_mqc_init_context_tables();
1230     init_luts();
1231
1232     init_quantization(s);
1233     if ((ret=init_tiles(s)) < 0)
1234         return ret;
1235
1236     av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1237
1238     return 0;
1239 }
1240
1241 static int j2kenc_destroy(AVCodecContext *avctx)
1242 {
1243     Jpeg2000EncoderContext *s = avctx->priv_data;
1244
1245     cleanup(s);
1246     return 0;
1247 }
1248
1249 // taken from the libopenjpeg wraper so it matches
1250
1251 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1252 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1253 static const AVOption options[] = {
1254     { "format",        "Codec Format",      OFFSET(format),        AV_OPT_TYPE_INT,   { .i64 = CODEC_JP2   }, CODEC_J2K, CODEC_JP2,   VE, "format"      },
1255     { "j2k",           NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K   }, 0,         0,           VE, "format"      },
1256     { "jp2",           NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2   }, 0,         0,           VE, "format"      },
1257     { "tile_width",    "Tile Width",        OFFSET(tile_width),    AV_OPT_TYPE_INT,   { .i64 = 256         }, 1,     1<<30,           VE, },
1258     { "tile_height",   "Tile Height",       OFFSET(tile_height),   AV_OPT_TYPE_INT,   { .i64 = 256         }, 1,     1<<30,           VE, },
1259     { "pred",          "DWT Type",          OFFSET(pred),          AV_OPT_TYPE_INT,   { .i64 = 0           }, 0,         1,           VE, "pred"        },
1260     { "dwt97int",      NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = 0           }, INT_MIN, INT_MAX,       VE, "pred"        },
1261     { "dwt53",         NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = 0           }, INT_MIN, INT_MAX,       VE, "pred"        },
1262     { "sop",           "SOP marker",        OFFSET(sop),           AV_OPT_TYPE_INT,   { .i64 = 0           }, 0,         1,           VE, },
1263     { "eph",           "EPH marker",        OFFSET(eph),           AV_OPT_TYPE_INT,   { .i64 = 0           }, 0,         1,           VE, },
1264     { NULL }
1265 };
1266
1267 static const AVClass j2k_class = {
1268     .class_name = "jpeg 2000 encoder",
1269     .item_name  = av_default_item_name,
1270     .option     = options,
1271     .version    = LIBAVUTIL_VERSION_INT,
1272 };
1273
1274 AVCodec ff_jpeg2000_encoder = {
1275     .name           = "jpeg2000",
1276     .long_name      = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1277     .type           = AVMEDIA_TYPE_VIDEO,
1278     .id             = AV_CODEC_ID_JPEG2000,
1279     .priv_data_size = sizeof(Jpeg2000EncoderContext),
1280     .init           = j2kenc_init,
1281     .encode2        = encode_frame,
1282     .close          = j2kenc_destroy,
1283     .pix_fmts       = (const enum AVPixelFormat[]) {
1284         AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1285         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1286         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
1287         AV_PIX_FMT_PAL8,
1288         AV_PIX_FMT_RGB48, AV_PIX_FMT_GRAY16,
1289         AV_PIX_FMT_NONE
1290     },
1291     .priv_class     = &j2k_class,
1292 };