2 * JPEG2000 image encoder
3 * Copyright (c) 2007 Kamil Nowosad
5 * This file is part of FFmpeg.
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.
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.
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
21 * **********************************************************************************************************************
25 * This source code incorporates work covered by the following copyright and
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.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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.
61 * JPEG2000 image encoder
63 * @author Kamil Nowosad
69 #include "bytestream.h"
71 #include "libavutil/common.h"
72 #include "libavutil/pixdesc.h"
73 #include "libavutil/opt.h"
75 #define NMSEDEC_BITS 7
76 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
77 #define WMSEDEC_SHIFT 13 ///< must be >= 13
78 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
83 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
84 lut_nmsedec_ref0[1<<NMSEDEC_BITS],
85 lut_nmsedec_sig [1<<NMSEDEC_BITS],
86 lut_nmsedec_sig0[1<<NMSEDEC_BITS];
88 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
89 {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
90 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
91 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
92 {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
94 {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
95 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
96 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
97 { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
101 Jpeg2000Component *comp;
106 AVCodecContext *avctx;
107 const AVFrame *picture;
109 int width, height; ///< image width and height
110 uint8_t cbps[4]; ///< bits per sample in particular components
114 int tile_width, tile_height; ///< tile size
115 int numXtiles, numYtiles;
124 Jpeg2000CodingStyle codsty;
125 Jpeg2000QuantStyle qntsty;
131 } Jpeg2000EncoderContext;
139 static void nspaces(FILE *fd, int n)
141 while(n--) putc(' ', fd);
144 static void printcomp(Jpeg2000Component *comp)
147 for (i = 0; i < comp->y1 - comp->y0; i++)
148 ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
151 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
153 int tileno, compno, reslevelno, bandno, precno;
154 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
155 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
157 s->width, s->height, s->tile_width, s->tile_height,
158 s->numXtiles, s->numYtiles, s->ncomponents);
159 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
160 Jpeg2000Tile *tile = s->tile + tileno;
162 fprintf(fd, "tile %d:\n", tileno);
163 for(compno = 0; compno < s->ncomponents; compno++){
164 Jpeg2000Component *comp = tile->comp + compno;
166 fprintf(fd, "component %d:\n", compno);
168 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
169 comp->x0, comp->x1, comp->y0, comp->y1);
170 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
171 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
173 fprintf(fd, "reslevel %d:\n", reslevelno);
175 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
176 reslevel->x0, reslevel->x1, reslevel->y0,
177 reslevel->y1, reslevel->nbands);
178 for(bandno = 0; bandno < reslevel->nbands; bandno++){
179 Jpeg2000Band *band = reslevel->band + bandno;
181 fprintf(fd, "band %d:\n", bandno);
183 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
184 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
187 band->codeblock_width, band->codeblock_height,
188 band->cblknx, band->cblkny);
189 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
190 Jpeg2000Prec *prec = band->prec + precno;
192 fprintf(fd, "prec %d:\n", precno);
194 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
195 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
204 /* bitstream routines */
206 /** put n times val bit */
207 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
210 if (s->bit_index == 8)
212 s->bit_index = *s->buf == 0xff;
215 *s->buf |= val << (7 - s->bit_index++);
219 /** put n least significant bits of a number num */
220 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
223 put_bits(s, (num >> n) & 1, 1);
226 /** flush the bitstream */
227 static void j2k_flush(Jpeg2000EncoderContext *s)
235 /* tag tree routines */
237 /** code the value stored in node */
238 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
240 Jpeg2000TgtNode *stack[30];
241 int sp = 1, curval = 0;
255 if (stack[sp]->val >= threshold){
256 put_bits(s, 0, threshold - curval);
259 put_bits(s, 0, stack[sp]->val - curval);
261 curval = stack[sp]->val;
265 /** update the value in node */
266 static void tag_tree_update(Jpeg2000TgtNode *node)
269 while (node->parent){
270 if (node->parent->val <= node->val)
272 node->parent->val = node->val;
278 static int put_siz(Jpeg2000EncoderContext *s)
282 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
285 bytestream_put_be16(&s->buf, JPEG2000_SIZ);
286 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
287 bytestream_put_be16(&s->buf, 0); // Rsiz
288 bytestream_put_be32(&s->buf, s->width); // width
289 bytestream_put_be32(&s->buf, s->height); // height
290 bytestream_put_be32(&s->buf, 0); // X0Siz
291 bytestream_put_be32(&s->buf, 0); // Y0Siz
293 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
294 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
295 bytestream_put_be32(&s->buf, 0); // XT0Siz
296 bytestream_put_be32(&s->buf, 0); // YT0Siz
297 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
299 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
300 bytestream_put_byte(&s->buf, 7);
301 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
302 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
307 static int put_cod(Jpeg2000EncoderContext *s)
309 Jpeg2000CodingStyle *codsty = &s->codsty;
311 if (s->buf_end - s->buf < 14)
314 bytestream_put_be16(&s->buf, JPEG2000_COD);
315 bytestream_put_be16(&s->buf, 12); // Lcod
316 bytestream_put_byte(&s->buf, 0); // Scod
318 bytestream_put_byte(&s->buf, 0); // progression level
319 bytestream_put_be16(&s->buf, 1); // num of layers
320 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
321 bytestream_put_byte(&s->buf, 0); // unspecified
323 bytestream_put_byte(&s->buf, 0); // unspecified
326 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
327 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
328 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
329 bytestream_put_byte(&s->buf, 0); // cblk style
330 bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
334 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
337 Jpeg2000CodingStyle *codsty = &s->codsty;
338 Jpeg2000QuantStyle *qntsty = &s->qntsty;
340 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
341 size = 4 + 3 * (codsty->nreslevels-1);
343 size = 5 + 6 * (codsty->nreslevels-1);
345 if (s->buf_end - s->buf < size + 2)
348 bytestream_put_be16(&s->buf, JPEG2000_QCD);
349 bytestream_put_be16(&s->buf, size); // LQcd
350 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
351 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
352 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
353 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
355 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
356 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
360 static int put_com(Jpeg2000EncoderContext *s, int compno)
362 int size = 4 + strlen(LIBAVCODEC_IDENT);
364 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
367 if (s->buf_end - s->buf < size + 2)
370 bytestream_put_be16(&s->buf, JPEG2000_COM);
371 bytestream_put_be16(&s->buf, size);
372 bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
374 bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
379 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
383 if (s->buf_end - s->buf < 12)
386 bytestream_put_be16(&s->buf, JPEG2000_SOT);
387 bytestream_put_be16(&s->buf, 10); // Lsot
388 bytestream_put_be16(&s->buf, tileno); // Isot
391 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
393 bytestream_put_byte(&s->buf, 0); // TPsot
394 bytestream_put_byte(&s->buf, 1); // TNsot
399 * compute the sizes of tiles, resolution levels, bands, etc.
400 * allocate memory for them
401 * divide the input image into tile-components
403 static int init_tiles(Jpeg2000EncoderContext *s)
405 int tileno, tilex, tiley, compno;
406 Jpeg2000CodingStyle *codsty = &s->codsty;
407 Jpeg2000QuantStyle *qntsty = &s->qntsty;
409 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
410 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
412 s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
414 return AVERROR(ENOMEM);
415 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
416 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
417 Jpeg2000Tile *tile = s->tile + tileno;
419 tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
421 return AVERROR(ENOMEM);
422 for (compno = 0; compno < s->ncomponents; compno++){
423 Jpeg2000Component *comp = tile->comp + compno;
426 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
427 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
428 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
429 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
431 for (i = 0; i < 2; i++)
432 for (j = 0; j < 2; j++)
433 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
435 if ((ret = ff_jpeg2000_init_component(comp,
439 compno?1<<s->chroma_shift[0]:1,
440 compno?1<<s->chroma_shift[1]:1,
449 static void copy_frame(Jpeg2000EncoderContext *s)
451 int tileno, compno, i, y, x;
453 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
454 Jpeg2000Tile *tile = s->tile + tileno;
456 for (compno = 0; compno < s->ncomponents; compno++){
457 Jpeg2000Component *comp = tile->comp + compno;
458 int *dst = comp->i_data;
459 line = s->picture->data[compno]
460 + comp->coord[1][0] * s->picture->linesize[compno]
462 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
464 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
465 *dst++ = *ptr++ - (1 << 7);
466 line += s->picture->linesize[compno];
470 line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
471 + tile->comp[0].coord[0][0] * s->ncomponents;
474 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
476 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
477 for (compno = 0; compno < s->ncomponents; compno++){
478 tile->comp[compno].i_data[i] = *ptr++ - (1 << 7);
481 line += s->picture->linesize[0];
487 static void init_quantization(Jpeg2000EncoderContext *s)
489 int compno, reslevelno, bandno;
490 Jpeg2000QuantStyle *qntsty = &s->qntsty;
491 Jpeg2000CodingStyle *codsty = &s->codsty;
493 for (compno = 0; compno < s->ncomponents; compno++){
495 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
496 int nbands, lev = codsty->nreslevels - reslevelno - 1;
497 nbands = reslevelno ? 3 : 1;
498 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
501 if (codsty->transform == FF_DWT97_INT){
502 int bandpos = bandno + (reslevelno>0),
503 ss = 81920000 / dwt_norms[0][bandpos][lev],
505 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
506 expn = s->cbps[compno] - log + 13;
508 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
510 qntsty->expn[gbandno] = expn;
511 qntsty->mant[gbandno] = mant;
517 static void init_luts(void)
520 mask = ~((1<<NMSEDEC_FRACBITS)-1);
522 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
523 lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
524 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
526 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
527 lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
528 << 13-NMSEDEC_FRACBITS, 0);
529 lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
534 /* tier-1 routines */
535 static int getnmsedec_sig(int x, int bpno)
537 if (bpno > NMSEDEC_FRACBITS)
538 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
539 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
542 static int getnmsedec_ref(int x, int bpno)
544 if (bpno > NMSEDEC_FRACBITS)
545 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
546 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
549 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
551 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
552 for (y0 = 0; y0 < height; y0 += 4)
553 for (x = 0; x < width; x++)
554 for (y = y0; y < height && y < y0+4; y++){
555 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
556 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
557 bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
558 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
561 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
562 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
563 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
564 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
566 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
571 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
573 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
574 for (y0 = 0; y0 < height; y0 += 4)
575 for (x = 0; x < width; x++)
576 for (y = y0; y < height && y < y0+4; y++)
577 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
578 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
579 *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
580 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
581 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
585 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
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 if (y0 + 3 < height && !(
591 (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
592 (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
593 (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
594 (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
598 for (rlen = 0; rlen < 4; rlen++)
599 if (t1->data[(y0+rlen) * t1->stride + x] & mask)
601 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
604 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
605 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
606 for (y = y0 + rlen; y < y0 + 4; y++){
607 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
608 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
610 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
611 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
613 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
614 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
615 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
616 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
619 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
622 for (y = y0; y < y0 + 4 && y < height; y++){
623 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
624 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
625 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
626 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
628 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
629 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
630 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
631 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
634 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
640 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
641 int width, int height, int bandpos, int lev)
643 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
646 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
648 for (y = 0; y < height; y++){
649 for (x = 0; x < width; x++){
650 if (t1->data[(y) * t1->stride + x] < 0){
651 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
652 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
654 max = FFMAX(max, t1->data[(y) * t1->stride + x]);
659 cblk->nonzerobits = 0;
662 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
663 bpno = cblk->nonzerobits - 1;
667 ff_mqc_initenc(&t1->mqc, cblk->data + 1);
669 for (passno = 0; bpno >= 0; passno++){
673 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
675 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
677 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
681 cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
682 wmsedec += (int64_t)nmsedec << (2*bpno);
683 cblk->passes[passno].disto = wmsedec;
690 cblk->npasses = passno;
691 cblk->ninclpasses = passno;
694 cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
697 /* tier-2 routines: */
699 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
706 put_num(s, 0xc | (n-3), 4);
708 put_num(s, 0x1e0 | (n-6), 9);
710 put_num(s, 0xff80 | (n-37), 16);
714 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
715 uint8_t *expn, int numgbits)
717 int bandno, empty = 1;
725 // is the packet empty?
726 for (bandno = 0; bandno < rlevel->nbands; bandno++){
727 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
728 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
734 put_bits(s, !empty, 1);
740 for (bandno = 0; bandno < rlevel->nbands; bandno++){
741 Jpeg2000Band *band = rlevel->band + bandno;
742 Jpeg2000Prec *prec = band->prec + precno;
744 int cblknw = prec->nb_codeblocks_width;
746 if (band->coord[0][0] == band->coord[0][1]
747 || band->coord[1][0] == band->coord[1][1])
750 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
751 for (xi = 0; xi < cblknw; xi++, pos++){
752 prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
753 tag_tree_update(prec->cblkincl + pos);
754 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
755 tag_tree_update(prec->zerobits + pos);
759 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
760 for (xi = 0; xi < cblknw; xi++, pos++){
761 int pad = 0, llen, length;
762 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
764 if (s->buf_end - s->buf < 20) // approximately
767 // inclusion information
768 tag_tree_code(s, prec->cblkincl + pos, 1);
769 if (!cblk->ninclpasses)
771 // zerobits information
772 tag_tree_code(s, prec->zerobits + pos, 100);
774 putnumpasses(s, cblk->ninclpasses);
776 length = cblk->passes[cblk->ninclpasses-1].rate;
777 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
782 // length of code block
783 put_bits(s, 1, llen);
785 put_num(s, length, av_log2(length)+1+pad);
790 for (bandno = 0; bandno < rlevel->nbands; bandno++){
791 Jpeg2000Band *band = rlevel->band + bandno;
792 Jpeg2000Prec *prec = band->prec + precno;
793 int yi, cblknw = prec->nb_codeblocks_width;
794 for (yi =0; yi < prec->nb_codeblocks_height; yi++){
796 for (xi = 0; xi < cblknw; xi++){
797 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
798 if (cblk->ninclpasses){
799 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
801 bytestream_put_buffer(&s->buf, cblk->data + 1, cblk->passes[cblk->ninclpasses-1].rate
802 - cblk->passes[cblk->ninclpasses-1].flushed_len);
803 bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
804 cblk->passes[cblk->ninclpasses-1].flushed_len);
812 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
814 int compno, reslevelno, ret;
815 Jpeg2000CodingStyle *codsty = &s->codsty;
816 Jpeg2000QuantStyle *qntsty = &s->qntsty;
818 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
819 // lay-rlevel-comp-pos progression
820 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
821 for (compno = 0; compno < s->ncomponents; compno++){
823 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
824 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
825 if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
826 qntsty->nguardbits)) < 0)
831 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
835 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
838 for (passno = 0; passno < cblk->npasses; passno++){
842 dr = cblk->passes[passno].rate
843 - (res ? cblk->passes[res-1].rate:0);
844 dd = cblk->passes[passno].disto
845 - (res ? cblk->passes[res-1].disto:0);
847 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
853 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
855 int precno, compno, reslevelno, bandno, cblkno, lev;
856 Jpeg2000CodingStyle *codsty = &s->codsty;
858 for (compno = 0; compno < s->ncomponents; compno++){
859 Jpeg2000Component *comp = tile->comp + compno;
861 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
862 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
864 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
865 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
866 int bandpos = bandno + (reslevelno > 0);
867 Jpeg2000Band *band = reslevel->band + bandno;
868 Jpeg2000Prec *prec = band->prec + precno;
870 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
871 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
873 cblk->ninclpasses = getcut(cblk, s->lambda,
874 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
882 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
884 int compno, reslevelno, bandno, ret;
885 Jpeg2000T1Context t1;
886 Jpeg2000CodingStyle *codsty = &s->codsty;
887 for (compno = 0; compno < s->ncomponents; compno++){
888 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
890 t1.stride = (1<<codsty->log2_cblk_width) + 2;
892 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
893 if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
895 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
897 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
898 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
900 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
901 Jpeg2000Band *band = reslevel->band + bandno;
902 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
903 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
904 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
906 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
907 band->coord[1][1]) - band->coord[1][0] + yy0;
909 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
912 bandpos = bandno + (reslevelno > 0);
914 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
915 if (reslevelno == 0 || bandno == 1)
918 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
920 xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
921 band->coord[0][1]) - band->coord[0][0] + xx0;
923 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
925 if (codsty->transform == FF_DWT53){
926 for (y = yy0; y < yy1; y++){
927 int *ptr = t1.data + (y-yy0)*t1.stride;
928 for (x = xx0; x < xx1; x++){
929 *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
933 for (y = yy0; y < yy1; y++){
934 int *ptr = t1.data + (y-yy0)*t1.stride;
935 for (x = xx0; x < xx1; x++){
936 *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
937 *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
942 if (!prec->cblk[cblkno].data)
943 prec->cblk[cblkno].data = av_malloc(1 + 8192);
944 if (!prec->cblk[cblkno].passes)
945 prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
946 if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
947 return AVERROR(ENOMEM);
948 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
949 bandpos, codsty->nreslevels - reslevelno - 1);
951 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
954 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
958 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
961 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
962 truncpasses(s, tile);
963 if ((ret = encode_packets(s, tile, tileno)) < 0)
965 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
969 static void cleanup(Jpeg2000EncoderContext *s)
972 Jpeg2000CodingStyle *codsty = &s->codsty;
974 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
975 for (compno = 0; compno < s->ncomponents; compno++){
976 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
977 ff_jpeg2000_cleanup(comp, codsty);
979 av_freep(&s->tile[tileno].comp);
984 static void reinit(Jpeg2000EncoderContext *s)
987 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
988 Jpeg2000Tile *tile = s->tile + tileno;
989 for (compno = 0; compno < s->ncomponents; compno++)
990 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
994 static void update_size(uint8_t *size, const uint8_t *end)
996 AV_WB32(size, end-size);
999 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1000 const AVFrame *pict, int *got_packet)
1003 Jpeg2000EncoderContext *s = avctx->priv_data;
1004 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1006 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1010 s->buf = s->buf_start = pkt->data;
1011 s->buf_end = pkt->data + pkt->size;
1015 s->lambda = s->picture->quality * LAMBDA_SCALE;
1020 if (s->format == CODEC_JP2) {
1021 av_assert0(s->buf == pkt->data);
1023 bytestream_put_be32(&s->buf, 0x0000000C);
1024 bytestream_put_be32(&s->buf, 0x6A502020);
1025 bytestream_put_be32(&s->buf, 0x0D0A870A);
1027 chunkstart = s->buf;
1028 bytestream_put_be32(&s->buf, 0);
1029 bytestream_put_buffer(&s->buf, "ftyp", 4);
1030 bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1031 bytestream_put_be32(&s->buf, 0);
1032 bytestream_put_buffer(&s->buf, "jp2\040", 4);
1033 update_size(chunkstart, s->buf);
1036 bytestream_put_be32(&s->buf, 0);
1037 bytestream_put_buffer(&s->buf, "jp2h", 4);
1039 chunkstart = s->buf;
1040 bytestream_put_be32(&s->buf, 0);
1041 bytestream_put_buffer(&s->buf, "ihdr", 4);
1042 bytestream_put_be32(&s->buf, avctx->height);
1043 bytestream_put_be32(&s->buf, avctx->width);
1044 bytestream_put_be16(&s->buf, s->ncomponents);
1045 bytestream_put_byte(&s->buf, s->cbps[0]);
1046 bytestream_put_byte(&s->buf, 7);
1047 bytestream_put_byte(&s->buf, 0);
1048 bytestream_put_byte(&s->buf, 0);
1049 update_size(chunkstart, s->buf);
1051 chunkstart = s->buf;
1052 bytestream_put_be32(&s->buf, 0);
1053 bytestream_put_buffer(&s->buf, "colr", 4);
1054 bytestream_put_byte(&s->buf, 1);
1055 bytestream_put_byte(&s->buf, 0);
1056 bytestream_put_byte(&s->buf, 0);
1057 if (s->ncomponents == 1) {
1058 bytestream_put_be32(&s->buf, 17);
1059 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
1060 bytestream_put_be32(&s->buf, 16);
1062 bytestream_put_be32(&s->buf, 18);
1064 update_size(chunkstart, s->buf);
1065 update_size(jp2hstart, s->buf);
1068 bytestream_put_be32(&s->buf, 0);
1069 bytestream_put_buffer(&s->buf, "jp2c", 4);
1072 if (s->buf_end - s->buf < 2)
1074 bytestream_put_be16(&s->buf, JPEG2000_SOC);
1075 if ((ret = put_siz(s)) < 0)
1077 if ((ret = put_cod(s)) < 0)
1079 if ((ret = put_qcd(s, 0)) < 0)
1081 if ((ret = put_com(s, 0)) < 0)
1084 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1086 if (!(psotptr = put_sot(s, tileno)))
1088 if (s->buf_end - s->buf < 2)
1090 bytestream_put_be16(&s->buf, JPEG2000_SOD);
1091 if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1093 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1095 if (s->buf_end - s->buf < 2)
1097 bytestream_put_be16(&s->buf, JPEG2000_EOC);
1099 if (s->format == CODEC_JP2)
1100 update_size(jp2cstart, s->buf);
1102 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1103 pkt->size = s->buf - s->buf_start;
1104 pkt->flags |= AV_PKT_FLAG_KEY;
1110 static av_cold int j2kenc_init(AVCodecContext *avctx)
1113 Jpeg2000EncoderContext *s = avctx->priv_data;
1114 Jpeg2000CodingStyle *codsty = &s->codsty;
1115 Jpeg2000QuantStyle *qntsty = &s->qntsty;
1118 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1120 #if FF_API_PRIVATE_OPT
1121 FF_DISABLE_DEPRECATION_WARNINGS
1122 if (avctx->prediction_method)
1123 s->pred = avctx->prediction_method;
1124 FF_ENABLE_DEPRECATION_WARNINGS
1128 // TODO: implement setting non-standard precinct size
1129 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1130 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1131 codsty->nreslevels2decode=
1132 codsty->nreslevels = 7;
1133 codsty->log2_cblk_width = 4;
1134 codsty->log2_cblk_height = 4;
1135 codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1137 qntsty->nguardbits = 1;
1139 if ((s->tile_width & (s->tile_width -1)) ||
1140 (s->tile_height & (s->tile_height-1))) {
1141 av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1144 if (codsty->transform == FF_DWT53)
1145 qntsty->quantsty = JPEG2000_QSTY_NONE;
1147 qntsty->quantsty = JPEG2000_QSTY_SE;
1149 s->width = avctx->width;
1150 s->height = avctx->height;
1152 for (i = 0; i < 3; i++)
1155 if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
1157 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
1159 } else{ // planar YUV
1162 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
1163 s->chroma_shift, s->chroma_shift + 1);
1168 ff_jpeg2000_init_tier1_luts();
1169 ff_mqc_init_context_tables();
1172 init_quantization(s);
1173 if ((ret=init_tiles(s)) < 0)
1176 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1181 static int j2kenc_destroy(AVCodecContext *avctx)
1183 Jpeg2000EncoderContext *s = avctx->priv_data;
1189 // taken from the libopenjpeg wraper so it matches
1191 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1192 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1193 static const AVOption options[] = {
1194 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1195 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1196 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1197 { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1198 { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1199 { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1200 { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1201 { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1206 static const AVClass j2k_class = {
1207 .class_name = "jpeg 2000 encoder",
1208 .item_name = av_default_item_name,
1210 .version = LIBAVUTIL_VERSION_INT,
1213 AVCodec ff_jpeg2000_encoder = {
1215 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1216 .type = AVMEDIA_TYPE_VIDEO,
1217 .id = AV_CODEC_ID_JPEG2000,
1218 .priv_data_size = sizeof(Jpeg2000EncoderContext),
1219 .init = j2kenc_init,
1220 .encode2 = encode_frame,
1221 .close = j2kenc_destroy,
1222 .pix_fmts = (const enum AVPixelFormat[]) {
1223 AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1224 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1225 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
1228 .priv_class = &j2k_class,