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