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"
74 #include "libavutil/intreadwrite.h"
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))
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];
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}},
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}}
102 Jpeg2000Component *comp;
107 AVCodecContext *avctx;
108 const AVFrame *picture;
110 int width, height; ///< image width and height
111 uint8_t cbps[4]; ///< bits per sample in particular components
115 int tile_width, tile_height; ///< tile size
116 int numXtiles, numYtiles;
125 Jpeg2000CodingStyle codsty;
126 Jpeg2000QuantStyle qntsty;
132 } Jpeg2000EncoderContext;
140 static void nspaces(FILE *fd, int n)
142 while(n--) putc(' ', fd);
145 static void printcomp(Jpeg2000Component *comp)
148 for (i = 0; i < comp->y1 - comp->y0; i++)
149 ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
152 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
154 int tileno, compno, reslevelno, bandno, precno;
155 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
156 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
158 s->width, s->height, s->tile_width, s->tile_height,
159 s->numXtiles, s->numYtiles, s->ncomponents);
160 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
161 Jpeg2000Tile *tile = s->tile + tileno;
163 fprintf(fd, "tile %d:\n", tileno);
164 for(compno = 0; compno < s->ncomponents; compno++){
165 Jpeg2000Component *comp = tile->comp + compno;
167 fprintf(fd, "component %d:\n", compno);
169 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
170 comp->x0, comp->x1, comp->y0, comp->y1);
171 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
172 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
174 fprintf(fd, "reslevel %d:\n", reslevelno);
176 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
177 reslevel->x0, reslevel->x1, reslevel->y0,
178 reslevel->y1, reslevel->nbands);
179 for(bandno = 0; bandno < reslevel->nbands; bandno++){
180 Jpeg2000Band *band = reslevel->band + bandno;
182 fprintf(fd, "band %d:\n", bandno);
184 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
185 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
188 band->codeblock_width, band->codeblock_height,
189 band->cblknx, band->cblkny);
190 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
191 Jpeg2000Prec *prec = band->prec + precno;
193 fprintf(fd, "prec %d:\n", precno);
195 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
196 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
205 /* bitstream routines */
207 /** put n times val bit */
208 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
211 if (s->bit_index == 8)
213 s->bit_index = *s->buf == 0xff;
216 *s->buf |= val << (7 - s->bit_index++);
220 /** put n least significant bits of a number num */
221 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
224 put_bits(s, (num >> n) & 1, 1);
227 /** flush the bitstream */
228 static void j2k_flush(Jpeg2000EncoderContext *s)
236 /* tag tree routines */
238 /** code the value stored in node */
239 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
241 Jpeg2000TgtNode *stack[30];
242 int sp = 1, curval = 0;
256 if (stack[sp]->val >= threshold){
257 put_bits(s, 0, threshold - curval);
260 put_bits(s, 0, stack[sp]->val - curval);
262 curval = stack[sp]->val;
266 /** update the value in node */
267 static void tag_tree_update(Jpeg2000TgtNode *node)
270 while (node->parent){
271 if (node->parent->val <= node->val)
273 node->parent->val = node->val;
279 static int put_siz(Jpeg2000EncoderContext *s)
283 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
286 bytestream_put_be16(&s->buf, JPEG2000_SIZ);
287 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
288 bytestream_put_be16(&s->buf, 0); // Rsiz
289 bytestream_put_be32(&s->buf, s->width); // width
290 bytestream_put_be32(&s->buf, s->height); // height
291 bytestream_put_be32(&s->buf, 0); // X0Siz
292 bytestream_put_be32(&s->buf, 0); // Y0Siz
294 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
295 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
296 bytestream_put_be32(&s->buf, 0); // XT0Siz
297 bytestream_put_be32(&s->buf, 0); // YT0Siz
298 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
300 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
301 bytestream_put_byte(&s->buf, s->cbps[i] - 1);
302 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
303 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
308 static int put_cod(Jpeg2000EncoderContext *s)
310 Jpeg2000CodingStyle *codsty = &s->codsty;
312 if (s->buf_end - s->buf < 14)
315 bytestream_put_be16(&s->buf, JPEG2000_COD);
316 bytestream_put_be16(&s->buf, 12); // Lcod
317 bytestream_put_byte(&s->buf, 0); // Scod
319 bytestream_put_byte(&s->buf, 0); // progression level
320 bytestream_put_be16(&s->buf, 1); // num of layers
321 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
322 bytestream_put_byte(&s->buf, 0); // unspecified
324 bytestream_put_byte(&s->buf, 0); // unspecified
327 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
328 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
329 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
330 bytestream_put_byte(&s->buf, 0); // cblk style
331 bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
335 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
338 Jpeg2000CodingStyle *codsty = &s->codsty;
339 Jpeg2000QuantStyle *qntsty = &s->qntsty;
341 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
342 size = 4 + 3 * (codsty->nreslevels-1);
344 size = 5 + 6 * (codsty->nreslevels-1);
346 if (s->buf_end - s->buf < size + 2)
349 bytestream_put_be16(&s->buf, JPEG2000_QCD);
350 bytestream_put_be16(&s->buf, size); // LQcd
351 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
352 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
353 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
354 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
356 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
357 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
361 static int put_com(Jpeg2000EncoderContext *s, int compno)
363 int size = 4 + strlen(LIBAVCODEC_IDENT);
365 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
368 if (s->buf_end - s->buf < size + 2)
371 bytestream_put_be16(&s->buf, JPEG2000_COM);
372 bytestream_put_be16(&s->buf, size);
373 bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
375 bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
380 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
384 if (s->buf_end - s->buf < 12)
387 bytestream_put_be16(&s->buf, JPEG2000_SOT);
388 bytestream_put_be16(&s->buf, 10); // Lsot
389 bytestream_put_be16(&s->buf, tileno); // Isot
392 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
394 bytestream_put_byte(&s->buf, 0); // TPsot
395 bytestream_put_byte(&s->buf, 1); // TNsot
400 * compute the sizes of tiles, resolution levels, bands, etc.
401 * allocate memory for them
402 * divide the input image into tile-components
404 static int init_tiles(Jpeg2000EncoderContext *s)
406 int tileno, tilex, tiley, compno;
407 Jpeg2000CodingStyle *codsty = &s->codsty;
408 Jpeg2000QuantStyle *qntsty = &s->qntsty;
410 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
411 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
413 s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
415 return AVERROR(ENOMEM);
416 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
417 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
418 Jpeg2000Tile *tile = s->tile + tileno;
420 tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
422 return AVERROR(ENOMEM);
423 for (compno = 0; compno < s->ncomponents; compno++){
424 Jpeg2000Component *comp = tile->comp + compno;
427 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
428 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
429 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
430 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
432 for (i = 0; i < 2; i++)
433 for (j = 0; j < 2; j++)
434 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
436 if ((ret = ff_jpeg2000_init_component(comp,
440 compno?1<<s->chroma_shift[0]:1,
441 compno?1<<s->chroma_shift[1]:1,
450 #define COPY_FRAME(D, PIXEL) \
451 static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
453 int tileno, compno, i, y, x; \
455 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
456 Jpeg2000Tile *tile = s->tile + tileno; \
458 for (compno = 0; compno < s->ncomponents; compno++){ \
459 Jpeg2000Component *comp = tile->comp + compno; \
460 int *dst = comp->i_data; \
461 int cbps = s->cbps[compno]; \
462 line = (PIXEL*)s->picture->data[compno] \
463 + comp->coord[1][0] * (s->picture->linesize[compno] / sizeof(PIXEL)) \
464 + comp->coord[0][0]; \
465 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
467 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
468 *dst++ = *ptr++ - (1 << (cbps - 1)); \
469 line += s->picture->linesize[compno] / sizeof(PIXEL); \
473 line = (PIXEL*)s->picture->data[0] + tile->comp[0].coord[1][0] * (s->picture->linesize[0] / sizeof(PIXEL)) \
474 + tile->comp[0].coord[0][0] * s->ncomponents; \
477 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
479 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
480 for (compno = 0; compno < s->ncomponents; compno++){ \
481 int cbps = s->cbps[compno]; \
482 tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
485 line += s->picture->linesize[0] / sizeof(PIXEL); \
491 COPY_FRAME(8, uint8_t)
492 COPY_FRAME(16, uint16_t)
494 static void init_quantization(Jpeg2000EncoderContext *s)
496 int compno, reslevelno, bandno;
497 Jpeg2000QuantStyle *qntsty = &s->qntsty;
498 Jpeg2000CodingStyle *codsty = &s->codsty;
500 for (compno = 0; compno < s->ncomponents; compno++){
502 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
503 int nbands, lev = codsty->nreslevels - reslevelno - 1;
504 nbands = reslevelno ? 3 : 1;
505 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
508 if (codsty->transform == FF_DWT97_INT){
509 int bandpos = bandno + (reslevelno>0),
510 ss = 81920000 / dwt_norms[0][bandpos][lev],
512 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
513 expn = s->cbps[compno] - log + 13;
515 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
517 qntsty->expn[gbandno] = expn;
518 qntsty->mant[gbandno] = mant;
524 static void init_luts(void)
527 mask = ~((1<<NMSEDEC_FRACBITS)-1);
529 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
530 lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
531 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
533 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
534 lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
535 (1 << 13) - (a * a << 11), 0);
536 lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
541 /* tier-1 routines */
542 static int getnmsedec_sig(int x, int bpno)
544 if (bpno > NMSEDEC_FRACBITS)
545 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
546 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
549 static int getnmsedec_ref(int x, int bpno)
551 if (bpno > NMSEDEC_FRACBITS)
552 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
553 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
556 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
558 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
559 for (y0 = 0; y0 < height; y0 += 4)
560 for (x = 0; x < width; x++)
561 for (y = y0; y < height && y < y0+4; y++){
562 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
563 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
564 bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
565 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
568 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
569 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
570 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
571 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
573 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
578 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
580 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
581 for (y0 = 0; y0 < height; y0 += 4)
582 for (x = 0; x < width; x++)
583 for (y = y0; y < height && y < y0+4; y++)
584 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
585 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
586 *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
587 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
588 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
592 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
594 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
595 for (y0 = 0; y0 < height; y0 += 4)
596 for (x = 0; x < width; x++){
597 if (y0 + 3 < height && !(
598 (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
599 (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
600 (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
601 (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
605 for (rlen = 0; rlen < 4; rlen++)
606 if (t1->data[(y0+rlen) * t1->stride + x] & mask)
608 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
611 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
612 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
613 for (y = y0 + rlen; y < y0 + 4; y++){
614 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
615 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
617 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
618 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
620 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
621 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
622 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
623 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
626 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
629 for (y = y0; y < y0 + 4 && y < height; y++){
630 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
631 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
632 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
633 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
635 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
636 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
637 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
638 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
641 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
647 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
648 int width, int height, int bandpos, int lev)
650 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
653 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
655 for (y = 0; y < height; y++){
656 for (x = 0; x < width; x++){
657 if (t1->data[(y) * t1->stride + x] < 0){
658 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
659 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
661 max = FFMAX(max, t1->data[(y) * t1->stride + x]);
666 cblk->nonzerobits = 0;
669 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
670 bpno = cblk->nonzerobits - 1;
674 ff_mqc_initenc(&t1->mqc, cblk->data + 1);
676 for (passno = 0; bpno >= 0; passno++){
680 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
682 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
684 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
688 cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
689 wmsedec += (int64_t)nmsedec << (2*bpno);
690 cblk->passes[passno].disto = wmsedec;
697 cblk->npasses = passno;
698 cblk->ninclpasses = passno;
701 cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
704 /* tier-2 routines: */
706 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
713 put_num(s, 0xc | (n-3), 4);
715 put_num(s, 0x1e0 | (n-6), 9);
717 put_num(s, 0xff80 | (n-37), 16);
721 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
722 uint8_t *expn, int numgbits)
724 int bandno, empty = 1;
732 // is the packet empty?
733 for (bandno = 0; bandno < rlevel->nbands; bandno++){
734 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
735 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
741 put_bits(s, !empty, 1);
747 for (bandno = 0; bandno < rlevel->nbands; bandno++){
748 Jpeg2000Band *band = rlevel->band + bandno;
749 Jpeg2000Prec *prec = band->prec + precno;
751 int cblknw = prec->nb_codeblocks_width;
753 if (band->coord[0][0] == band->coord[0][1]
754 || band->coord[1][0] == band->coord[1][1])
757 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
758 for (xi = 0; xi < cblknw; xi++, pos++){
759 prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
760 tag_tree_update(prec->cblkincl + pos);
761 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
762 tag_tree_update(prec->zerobits + pos);
766 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
767 for (xi = 0; xi < cblknw; xi++, pos++){
768 int pad = 0, llen, length;
769 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
771 if (s->buf_end - s->buf < 20) // approximately
774 // inclusion information
775 tag_tree_code(s, prec->cblkincl + pos, 1);
776 if (!cblk->ninclpasses)
778 // zerobits information
779 tag_tree_code(s, prec->zerobits + pos, 100);
781 putnumpasses(s, cblk->ninclpasses);
783 length = cblk->passes[cblk->ninclpasses-1].rate;
784 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
789 // length of code block
790 put_bits(s, 1, llen);
792 put_num(s, length, av_log2(length)+1+pad);
797 for (bandno = 0; bandno < rlevel->nbands; bandno++){
798 Jpeg2000Band *band = rlevel->band + bandno;
799 Jpeg2000Prec *prec = band->prec + precno;
800 int yi, cblknw = prec->nb_codeblocks_width;
801 for (yi =0; yi < prec->nb_codeblocks_height; yi++){
803 for (xi = 0; xi < cblknw; xi++){
804 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
805 if (cblk->ninclpasses){
806 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
808 bytestream_put_buffer(&s->buf, cblk->data + 1, cblk->passes[cblk->ninclpasses-1].rate
809 - cblk->passes[cblk->ninclpasses-1].flushed_len);
810 bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
811 cblk->passes[cblk->ninclpasses-1].flushed_len);
819 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
821 int compno, reslevelno, ret;
822 Jpeg2000CodingStyle *codsty = &s->codsty;
823 Jpeg2000QuantStyle *qntsty = &s->qntsty;
825 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
826 // lay-rlevel-comp-pos progression
827 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
828 for (compno = 0; compno < s->ncomponents; compno++){
830 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
831 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
832 if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
833 qntsty->nguardbits)) < 0)
838 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
842 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
845 for (passno = 0; passno < cblk->npasses; passno++){
849 dr = cblk->passes[passno].rate
850 - (res ? cblk->passes[res-1].rate:0);
851 dd = cblk->passes[passno].disto
852 - (res ? cblk->passes[res-1].disto:0);
854 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
860 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
862 int precno, compno, reslevelno, bandno, cblkno, lev;
863 Jpeg2000CodingStyle *codsty = &s->codsty;
865 for (compno = 0; compno < s->ncomponents; compno++){
866 Jpeg2000Component *comp = tile->comp + compno;
868 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
869 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
871 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
872 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
873 int bandpos = bandno + (reslevelno > 0);
874 Jpeg2000Band *band = reslevel->band + bandno;
875 Jpeg2000Prec *prec = band->prec + precno;
877 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
878 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
880 cblk->ninclpasses = getcut(cblk, s->lambda,
881 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
889 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
891 int compno, reslevelno, bandno, ret;
892 Jpeg2000T1Context t1;
893 Jpeg2000CodingStyle *codsty = &s->codsty;
894 for (compno = 0; compno < s->ncomponents; compno++){
895 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
897 t1.stride = (1<<codsty->log2_cblk_width) + 2;
899 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
900 if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
902 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
904 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
905 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
907 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
908 Jpeg2000Band *band = reslevel->band + bandno;
909 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
910 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
911 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
913 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
914 band->coord[1][1]) - band->coord[1][0] + yy0;
916 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
919 bandpos = bandno + (reslevelno > 0);
921 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
922 if (reslevelno == 0 || bandno == 1)
925 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
927 xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
928 band->coord[0][1]) - band->coord[0][0] + xx0;
930 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
932 if (codsty->transform == FF_DWT53){
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] * (1 << NMSEDEC_FRACBITS);
940 for (y = yy0; y < yy1; y++){
941 int *ptr = t1.data + (y-yy0)*t1.stride;
942 for (x = xx0; x < xx1; x++){
943 *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
944 *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
949 if (!prec->cblk[cblkno].data)
950 prec->cblk[cblkno].data = av_malloc(1 + 8192);
951 if (!prec->cblk[cblkno].passes)
952 prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
953 if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
954 return AVERROR(ENOMEM);
955 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
956 bandpos, codsty->nreslevels - reslevelno - 1);
958 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
961 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
965 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
968 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
969 truncpasses(s, tile);
970 if ((ret = encode_packets(s, tile, tileno)) < 0)
972 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
976 static void cleanup(Jpeg2000EncoderContext *s)
979 Jpeg2000CodingStyle *codsty = &s->codsty;
981 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
982 for (compno = 0; compno < s->ncomponents; compno++){
983 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
984 ff_jpeg2000_cleanup(comp, codsty);
986 av_freep(&s->tile[tileno].comp);
991 static void reinit(Jpeg2000EncoderContext *s)
994 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
995 Jpeg2000Tile *tile = s->tile + tileno;
996 for (compno = 0; compno < s->ncomponents; compno++)
997 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1001 static void update_size(uint8_t *size, const uint8_t *end)
1003 AV_WB32(size, end-size);
1006 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1007 const AVFrame *pict, int *got_packet)
1010 Jpeg2000EncoderContext *s = avctx->priv_data;
1011 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1013 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1017 s->buf = s->buf_start = pkt->data;
1018 s->buf_end = pkt->data + pkt->size;
1022 s->lambda = s->picture->quality * LAMBDA_SCALE;
1024 if (avctx->pix_fmt == AV_PIX_FMT_BGR48 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
1031 if (s->format == CODEC_JP2) {
1032 av_assert0(s->buf == pkt->data);
1034 bytestream_put_be32(&s->buf, 0x0000000C);
1035 bytestream_put_be32(&s->buf, 0x6A502020);
1036 bytestream_put_be32(&s->buf, 0x0D0A870A);
1038 chunkstart = s->buf;
1039 bytestream_put_be32(&s->buf, 0);
1040 bytestream_put_buffer(&s->buf, "ftyp", 4);
1041 bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1042 bytestream_put_be32(&s->buf, 0);
1043 bytestream_put_buffer(&s->buf, "jp2\040", 4);
1044 update_size(chunkstart, s->buf);
1047 bytestream_put_be32(&s->buf, 0);
1048 bytestream_put_buffer(&s->buf, "jp2h", 4);
1050 chunkstart = s->buf;
1051 bytestream_put_be32(&s->buf, 0);
1052 bytestream_put_buffer(&s->buf, "ihdr", 4);
1053 bytestream_put_be32(&s->buf, avctx->height);
1054 bytestream_put_be32(&s->buf, avctx->width);
1055 bytestream_put_be16(&s->buf, s->ncomponents);
1056 bytestream_put_byte(&s->buf, s->cbps[0]);
1057 bytestream_put_byte(&s->buf, 7);
1058 bytestream_put_byte(&s->buf, 0);
1059 bytestream_put_byte(&s->buf, 0);
1060 update_size(chunkstart, s->buf);
1062 chunkstart = s->buf;
1063 bytestream_put_be32(&s->buf, 0);
1064 bytestream_put_buffer(&s->buf, "colr", 4);
1065 bytestream_put_byte(&s->buf, 1);
1066 bytestream_put_byte(&s->buf, 0);
1067 bytestream_put_byte(&s->buf, 0);
1068 if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1069 bytestream_put_be32(&s->buf, 16);
1070 } else if (s->ncomponents == 1) {
1071 bytestream_put_be32(&s->buf, 17);
1073 bytestream_put_be32(&s->buf, 18);
1075 update_size(chunkstart, s->buf);
1076 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1078 uint8_t *palette = pict->data[1];
1079 chunkstart = s->buf;
1080 bytestream_put_be32(&s->buf, 0);
1081 bytestream_put_buffer(&s->buf, "pclr", 4);
1082 bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1083 bytestream_put_byte(&s->buf, 3); // colour channels
1084 bytestream_put_be24(&s->buf, 0x070707); //colour depths
1085 for (i = 0; i < AVPALETTE_COUNT; i++) {
1086 bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1089 update_size(chunkstart, s->buf);
1090 chunkstart = s->buf;
1091 bytestream_put_be32(&s->buf, 0);
1092 bytestream_put_buffer(&s->buf, "cmap", 4);
1093 for (i = 0; i < 3; i++) {
1094 bytestream_put_be16(&s->buf, 0); // component
1095 bytestream_put_byte(&s->buf, 1); // palette mapping
1096 bytestream_put_byte(&s->buf, i); // index
1098 update_size(chunkstart, s->buf);
1100 update_size(jp2hstart, s->buf);
1103 bytestream_put_be32(&s->buf, 0);
1104 bytestream_put_buffer(&s->buf, "jp2c", 4);
1107 if (s->buf_end - s->buf < 2)
1109 bytestream_put_be16(&s->buf, JPEG2000_SOC);
1110 if ((ret = put_siz(s)) < 0)
1112 if ((ret = put_cod(s)) < 0)
1114 if ((ret = put_qcd(s, 0)) < 0)
1116 if ((ret = put_com(s, 0)) < 0)
1119 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1121 if (!(psotptr = put_sot(s, tileno)))
1123 if (s->buf_end - s->buf < 2)
1125 bytestream_put_be16(&s->buf, JPEG2000_SOD);
1126 if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1128 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1130 if (s->buf_end - s->buf < 2)
1132 bytestream_put_be16(&s->buf, JPEG2000_EOC);
1134 if (s->format == CODEC_JP2)
1135 update_size(jp2cstart, s->buf);
1137 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1138 pkt->size = s->buf - s->buf_start;
1139 pkt->flags |= AV_PKT_FLAG_KEY;
1145 static av_cold int j2kenc_init(AVCodecContext *avctx)
1148 Jpeg2000EncoderContext *s = avctx->priv_data;
1149 Jpeg2000CodingStyle *codsty = &s->codsty;
1150 Jpeg2000QuantStyle *qntsty = &s->qntsty;
1153 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1155 #if FF_API_PRIVATE_OPT
1156 FF_DISABLE_DEPRECATION_WARNINGS
1157 if (avctx->prediction_method)
1158 s->pred = avctx->prediction_method;
1159 FF_ENABLE_DEPRECATION_WARNINGS
1162 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1163 av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1164 s->pred = FF_DWT97_INT;
1165 s->format = CODEC_JP2;
1169 // TODO: implement setting non-standard precinct size
1170 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1171 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1172 codsty->nreslevels2decode=
1173 codsty->nreslevels = 7;
1174 codsty->log2_cblk_width = 4;
1175 codsty->log2_cblk_height = 4;
1176 codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1178 qntsty->nguardbits = 1;
1180 if ((s->tile_width & (s->tile_width -1)) ||
1181 (s->tile_height & (s->tile_height-1))) {
1182 av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1185 if (codsty->transform == FF_DWT53)
1186 qntsty->quantsty = JPEG2000_QSTY_NONE;
1188 qntsty->quantsty = JPEG2000_QSTY_SE;
1190 s->width = avctx->width;
1191 s->height = avctx->height;
1193 for (i = 0; i < 3; i++) {
1194 if (avctx->pix_fmt == AV_PIX_FMT_GRAY16 || avctx->pix_fmt == AV_PIX_FMT_RGB48)
1200 if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_RGB48){
1202 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16){
1204 } else{ // planar YUV
1207 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
1208 s->chroma_shift, s->chroma_shift + 1);
1213 ff_jpeg2000_init_tier1_luts();
1214 ff_mqc_init_context_tables();
1217 init_quantization(s);
1218 if ((ret=init_tiles(s)) < 0)
1221 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1226 static int j2kenc_destroy(AVCodecContext *avctx)
1228 Jpeg2000EncoderContext *s = avctx->priv_data;
1234 // taken from the libopenjpeg wraper so it matches
1236 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1237 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1238 static const AVOption options[] = {
1239 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1240 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1241 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1242 { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1243 { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1244 { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1245 { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1246 { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1251 static const AVClass j2k_class = {
1252 .class_name = "jpeg 2000 encoder",
1253 .item_name = av_default_item_name,
1255 .version = LIBAVUTIL_VERSION_INT,
1258 AVCodec ff_jpeg2000_encoder = {
1260 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1261 .type = AVMEDIA_TYPE_VIDEO,
1262 .id = AV_CODEC_ID_JPEG2000,
1263 .priv_data_size = sizeof(Jpeg2000EncoderContext),
1264 .init = j2kenc_init,
1265 .encode2 = encode_frame,
1266 .close = j2kenc_destroy,
1267 .pix_fmts = (const enum AVPixelFormat[]) {
1268 AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1269 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1270 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
1272 AV_PIX_FMT_RGB48, AV_PIX_FMT_GRAY16,
1275 .priv_class = &j2k_class,