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;
134 } Jpeg2000EncoderContext;
142 static void nspaces(FILE *fd, int n)
144 while(n--) putc(' ', fd);
147 static void printcomp(Jpeg2000Component *comp)
150 for (i = 0; i < comp->y1 - comp->y0; i++)
151 ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
154 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
156 int tileno, compno, reslevelno, bandno, precno;
157 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
158 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
160 s->width, s->height, s->tile_width, s->tile_height,
161 s->numXtiles, s->numYtiles, s->ncomponents);
162 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
163 Jpeg2000Tile *tile = s->tile + tileno;
165 fprintf(fd, "tile %d:\n", tileno);
166 for(compno = 0; compno < s->ncomponents; compno++){
167 Jpeg2000Component *comp = tile->comp + compno;
169 fprintf(fd, "component %d:\n", compno);
171 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
172 comp->x0, comp->x1, comp->y0, comp->y1);
173 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
174 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
176 fprintf(fd, "reslevel %d:\n", reslevelno);
178 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
179 reslevel->x0, reslevel->x1, reslevel->y0,
180 reslevel->y1, reslevel->nbands);
181 for(bandno = 0; bandno < reslevel->nbands; bandno++){
182 Jpeg2000Band *band = reslevel->band + bandno;
184 fprintf(fd, "band %d:\n", bandno);
186 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
187 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
190 band->codeblock_width, band->codeblock_height,
191 band->cblknx, band->cblkny);
192 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
193 Jpeg2000Prec *prec = band->prec + precno;
195 fprintf(fd, "prec %d:\n", precno);
197 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
198 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
207 /* bitstream routines */
209 /** put n times val bit */
210 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
213 if (s->bit_index == 8)
215 s->bit_index = *s->buf == 0xff;
218 *s->buf |= val << (7 - s->bit_index++);
222 /** put n least significant bits of a number num */
223 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
226 put_bits(s, (num >> n) & 1, 1);
229 /** flush the bitstream */
230 static void j2k_flush(Jpeg2000EncoderContext *s)
238 /* tag tree routines */
240 /** code the value stored in node */
241 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
243 Jpeg2000TgtNode *stack[30];
244 int sp = 1, curval = 0;
258 if (stack[sp]->val >= threshold){
259 put_bits(s, 0, threshold - curval);
262 put_bits(s, 0, stack[sp]->val - curval);
264 curval = stack[sp]->val;
268 /** update the value in node */
269 static void tag_tree_update(Jpeg2000TgtNode *node)
272 while (node->parent){
273 if (node->parent->val <= node->val)
275 node->parent->val = node->val;
281 static int put_siz(Jpeg2000EncoderContext *s)
285 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
288 bytestream_put_be16(&s->buf, JPEG2000_SIZ);
289 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
290 bytestream_put_be16(&s->buf, 0); // Rsiz
291 bytestream_put_be32(&s->buf, s->width); // width
292 bytestream_put_be32(&s->buf, s->height); // height
293 bytestream_put_be32(&s->buf, 0); // X0Siz
294 bytestream_put_be32(&s->buf, 0); // Y0Siz
296 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
297 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
298 bytestream_put_be32(&s->buf, 0); // XT0Siz
299 bytestream_put_be32(&s->buf, 0); // YT0Siz
300 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
302 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
303 bytestream_put_byte(&s->buf, s->cbps[i] - 1);
304 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
305 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
310 static int put_cod(Jpeg2000EncoderContext *s)
312 Jpeg2000CodingStyle *codsty = &s->codsty;
315 if (s->buf_end - s->buf < 14)
318 bytestream_put_be16(&s->buf, JPEG2000_COD);
319 bytestream_put_be16(&s->buf, 12); // Lcod
321 scod |= JPEG2000_CSTY_SOP;
323 scod |= JPEG2000_CSTY_EPH;
324 bytestream_put_byte(&s->buf, scod); // Scod
326 bytestream_put_byte(&s->buf, 0); // progression level
327 bytestream_put_be16(&s->buf, 1); // num of layers
328 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
329 bytestream_put_byte(&s->buf, 0); // unspecified
331 bytestream_put_byte(&s->buf, 0); // unspecified
334 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
335 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
336 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
337 bytestream_put_byte(&s->buf, 0); // cblk style
338 bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
342 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
345 Jpeg2000CodingStyle *codsty = &s->codsty;
346 Jpeg2000QuantStyle *qntsty = &s->qntsty;
348 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
349 size = 4 + 3 * (codsty->nreslevels-1);
351 size = 5 + 6 * (codsty->nreslevels-1);
353 if (s->buf_end - s->buf < size + 2)
356 bytestream_put_be16(&s->buf, JPEG2000_QCD);
357 bytestream_put_be16(&s->buf, size); // LQcd
358 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
359 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
360 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
361 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
363 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
364 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
368 static int put_com(Jpeg2000EncoderContext *s, int compno)
370 int size = 4 + strlen(LIBAVCODEC_IDENT);
372 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
375 if (s->buf_end - s->buf < size + 2)
378 bytestream_put_be16(&s->buf, JPEG2000_COM);
379 bytestream_put_be16(&s->buf, size);
380 bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
382 bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
387 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
391 if (s->buf_end - s->buf < 12)
394 bytestream_put_be16(&s->buf, JPEG2000_SOT);
395 bytestream_put_be16(&s->buf, 10); // Lsot
396 bytestream_put_be16(&s->buf, tileno); // Isot
399 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
401 bytestream_put_byte(&s->buf, 0); // TPsot
402 bytestream_put_byte(&s->buf, 1); // TNsot
407 * compute the sizes of tiles, resolution levels, bands, etc.
408 * allocate memory for them
409 * divide the input image into tile-components
411 static int init_tiles(Jpeg2000EncoderContext *s)
413 int tileno, tilex, tiley, compno;
414 Jpeg2000CodingStyle *codsty = &s->codsty;
415 Jpeg2000QuantStyle *qntsty = &s->qntsty;
417 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
418 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
420 s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
422 return AVERROR(ENOMEM);
423 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
424 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
425 Jpeg2000Tile *tile = s->tile + tileno;
427 tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
429 return AVERROR(ENOMEM);
430 for (compno = 0; compno < s->ncomponents; compno++){
431 Jpeg2000Component *comp = tile->comp + compno;
434 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
435 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
436 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
437 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
439 for (i = 0; i < 2; i++)
440 for (j = 0; j < 2; j++)
441 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
443 if ((ret = ff_jpeg2000_init_component(comp,
447 compno?1<<s->chroma_shift[0]:1,
448 compno?1<<s->chroma_shift[1]:1,
457 #define COPY_FRAME(D, PIXEL) \
458 static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
460 int tileno, compno, i, y, x; \
462 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
463 Jpeg2000Tile *tile = s->tile + tileno; \
465 for (compno = 0; compno < s->ncomponents; compno++){ \
466 Jpeg2000Component *comp = tile->comp + compno; \
467 int *dst = comp->i_data; \
468 int cbps = s->cbps[compno]; \
469 line = (PIXEL*)s->picture->data[compno] \
470 + comp->coord[1][0] * (s->picture->linesize[compno] / sizeof(PIXEL)) \
471 + comp->coord[0][0]; \
472 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
474 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
475 *dst++ = *ptr++ - (1 << (cbps - 1)); \
476 line += s->picture->linesize[compno] / sizeof(PIXEL); \
480 line = (PIXEL*)s->picture->data[0] + tile->comp[0].coord[1][0] * (s->picture->linesize[0] / sizeof(PIXEL)) \
481 + tile->comp[0].coord[0][0] * s->ncomponents; \
484 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
486 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
487 for (compno = 0; compno < s->ncomponents; compno++){ \
488 int cbps = s->cbps[compno]; \
489 tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
492 line += s->picture->linesize[0] / sizeof(PIXEL); \
498 COPY_FRAME(8, uint8_t)
499 COPY_FRAME(16, uint16_t)
501 static void init_quantization(Jpeg2000EncoderContext *s)
503 int compno, reslevelno, bandno;
504 Jpeg2000QuantStyle *qntsty = &s->qntsty;
505 Jpeg2000CodingStyle *codsty = &s->codsty;
507 for (compno = 0; compno < s->ncomponents; compno++){
509 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
510 int nbands, lev = codsty->nreslevels - reslevelno - 1;
511 nbands = reslevelno ? 3 : 1;
512 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
515 if (codsty->transform == FF_DWT97_INT){
516 int bandpos = bandno + (reslevelno>0),
517 ss = 81920000 / dwt_norms[0][bandpos][lev],
519 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
520 expn = s->cbps[compno] - log + 13;
522 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
524 qntsty->expn[gbandno] = expn;
525 qntsty->mant[gbandno] = mant;
531 static void init_luts(void)
534 mask = ~((1<<NMSEDEC_FRACBITS)-1);
536 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
537 lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
538 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
540 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
541 lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
542 (1 << 13) - (a * a << 11), 0);
543 lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
548 /* tier-1 routines */
549 static int getnmsedec_sig(int x, int bpno)
551 if (bpno > NMSEDEC_FRACBITS)
552 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
553 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
556 static int getnmsedec_ref(int x, int bpno)
558 if (bpno > NMSEDEC_FRACBITS)
559 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
560 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
563 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
565 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
566 for (y0 = 0; y0 < height; y0 += 4)
567 for (x = 0; x < width; x++)
568 for (y = y0; y < height && y < y0+4; y++){
569 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
570 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
571 bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
572 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
575 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
576 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
577 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
578 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
580 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
585 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, 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 for (y = y0; y < height && y < y0+4; y++)
591 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
592 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
593 *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
594 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
595 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
599 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
601 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
602 for (y0 = 0; y0 < height; y0 += 4)
603 for (x = 0; x < width; x++){
604 if (y0 + 3 < height && !(
605 (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
606 (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
607 (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
608 (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
612 for (rlen = 0; rlen < 4; rlen++)
613 if (t1->data[(y0+rlen) * t1->stride + x] & mask)
615 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
618 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
619 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
620 for (y = y0 + rlen; y < y0 + 4; y++){
621 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
622 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
624 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
625 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
627 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
628 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
629 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
630 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
633 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
636 for (y = y0; y < y0 + 4 && y < height; y++){
637 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
638 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
639 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
640 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
642 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
643 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
644 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
645 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
648 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
654 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
655 int width, int height, int bandpos, int lev)
657 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
660 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
662 for (y = 0; y < height; y++){
663 for (x = 0; x < width; x++){
664 if (t1->data[(y) * t1->stride + x] < 0){
665 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
666 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
668 max = FFMAX(max, t1->data[(y) * t1->stride + x]);
673 cblk->nonzerobits = 0;
676 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
677 bpno = cblk->nonzerobits - 1;
681 ff_mqc_initenc(&t1->mqc, cblk->data + 1);
683 for (passno = 0; bpno >= 0; passno++){
687 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
689 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
691 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
695 cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
696 wmsedec += (int64_t)nmsedec << (2*bpno);
697 cblk->passes[passno].disto = wmsedec;
704 cblk->npasses = passno;
705 cblk->ninclpasses = passno;
708 cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
711 /* tier-2 routines: */
713 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
720 put_num(s, 0xc | (n-3), 4);
722 put_num(s, 0x1e0 | (n-6), 9);
724 put_num(s, 0xff80 | (n-37), 16);
728 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
729 uint8_t *expn, int numgbits, int packetno)
731 int bandno, empty = 1;
737 bytestream_put_be16(&s->buf, JPEG2000_SOP);
738 bytestream_put_be16(&s->buf, 4);
739 bytestream_put_be16(&s->buf, packetno);
743 // is the packet empty?
744 for (bandno = 0; bandno < rlevel->nbands; bandno++){
745 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
746 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
752 put_bits(s, !empty, 1);
758 for (bandno = 0; bandno < rlevel->nbands; bandno++){
759 Jpeg2000Band *band = rlevel->band + bandno;
760 Jpeg2000Prec *prec = band->prec + precno;
762 int cblknw = prec->nb_codeblocks_width;
764 if (band->coord[0][0] == band->coord[0][1]
765 || band->coord[1][0] == band->coord[1][1])
768 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
769 for (xi = 0; xi < cblknw; xi++, pos++){
770 prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
771 tag_tree_update(prec->cblkincl + pos);
772 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
773 tag_tree_update(prec->zerobits + pos);
777 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
778 for (xi = 0; xi < cblknw; xi++, pos++){
779 int pad = 0, llen, length;
780 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
782 if (s->buf_end - s->buf < 20) // approximately
785 // inclusion information
786 tag_tree_code(s, prec->cblkincl + pos, 1);
787 if (!cblk->ninclpasses)
789 // zerobits information
790 tag_tree_code(s, prec->zerobits + pos, 100);
792 putnumpasses(s, cblk->ninclpasses);
794 length = cblk->passes[cblk->ninclpasses-1].rate;
795 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
800 // length of code block
801 put_bits(s, 1, llen);
803 put_num(s, length, av_log2(length)+1+pad);
809 bytestream_put_be16(&s->buf, JPEG2000_EPH);
812 for (bandno = 0; bandno < rlevel->nbands; bandno++){
813 Jpeg2000Band *band = rlevel->band + bandno;
814 Jpeg2000Prec *prec = band->prec + precno;
815 int yi, cblknw = prec->nb_codeblocks_width;
816 for (yi =0; yi < prec->nb_codeblocks_height; yi++){
818 for (xi = 0; xi < cblknw; xi++){
819 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
820 if (cblk->ninclpasses){
821 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
823 bytestream_put_buffer(&s->buf, cblk->data + 1, cblk->passes[cblk->ninclpasses-1].rate
824 - cblk->passes[cblk->ninclpasses-1].flushed_len);
825 bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
826 cblk->passes[cblk->ninclpasses-1].flushed_len);
834 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
836 int compno, reslevelno, ret;
837 Jpeg2000CodingStyle *codsty = &s->codsty;
838 Jpeg2000QuantStyle *qntsty = &s->qntsty;
840 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
841 // lay-rlevel-comp-pos progression
842 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
843 for (compno = 0; compno < s->ncomponents; compno++){
845 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
846 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
847 if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
848 qntsty->nguardbits, packetno++)) < 0)
853 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
857 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
860 for (passno = 0; passno < cblk->npasses; passno++){
864 dr = cblk->passes[passno].rate
865 - (res ? cblk->passes[res-1].rate:0);
866 dd = cblk->passes[passno].disto
867 - (res ? cblk->passes[res-1].disto:0);
869 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
875 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
877 int precno, compno, reslevelno, bandno, cblkno, lev;
878 Jpeg2000CodingStyle *codsty = &s->codsty;
880 for (compno = 0; compno < s->ncomponents; compno++){
881 Jpeg2000Component *comp = tile->comp + compno;
883 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
884 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
886 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
887 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
888 int bandpos = bandno + (reslevelno > 0);
889 Jpeg2000Band *band = reslevel->band + bandno;
890 Jpeg2000Prec *prec = band->prec + precno;
892 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
893 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
895 cblk->ninclpasses = getcut(cblk, s->lambda,
896 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
904 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
906 int compno, reslevelno, bandno, ret;
907 Jpeg2000T1Context t1;
908 Jpeg2000CodingStyle *codsty = &s->codsty;
909 for (compno = 0; compno < s->ncomponents; compno++){
910 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
912 t1.stride = (1<<codsty->log2_cblk_width) + 2;
914 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
915 if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
917 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
919 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
920 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
922 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
923 Jpeg2000Band *band = reslevel->band + bandno;
924 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
925 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
926 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
928 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
929 band->coord[1][1]) - band->coord[1][0] + yy0;
931 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
934 bandpos = bandno + (reslevelno > 0);
936 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
937 if (reslevelno == 0 || bandno == 1)
940 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
942 xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
943 band->coord[0][1]) - band->coord[0][0] + xx0;
945 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
947 if (codsty->transform == FF_DWT53){
948 for (y = yy0; y < yy1; y++){
949 int *ptr = t1.data + (y-yy0)*t1.stride;
950 for (x = xx0; x < xx1; x++){
951 *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
955 for (y = yy0; y < yy1; y++){
956 int *ptr = t1.data + (y-yy0)*t1.stride;
957 for (x = xx0; x < xx1; x++){
958 *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
959 *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
964 if (!prec->cblk[cblkno].data)
965 prec->cblk[cblkno].data = av_malloc(1 + 8192);
966 if (!prec->cblk[cblkno].passes)
967 prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
968 if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
969 return AVERROR(ENOMEM);
970 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
971 bandpos, codsty->nreslevels - reslevelno - 1);
973 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
976 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
980 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
983 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
984 truncpasses(s, tile);
985 if ((ret = encode_packets(s, tile, tileno)) < 0)
987 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
991 static void cleanup(Jpeg2000EncoderContext *s)
994 Jpeg2000CodingStyle *codsty = &s->codsty;
996 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
997 for (compno = 0; compno < s->ncomponents; compno++){
998 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
999 ff_jpeg2000_cleanup(comp, codsty);
1001 av_freep(&s->tile[tileno].comp);
1006 static void reinit(Jpeg2000EncoderContext *s)
1009 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1010 Jpeg2000Tile *tile = s->tile + tileno;
1011 for (compno = 0; compno < s->ncomponents; compno++)
1012 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1016 static void update_size(uint8_t *size, const uint8_t *end)
1018 AV_WB32(size, end-size);
1021 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1022 const AVFrame *pict, int *got_packet)
1025 Jpeg2000EncoderContext *s = avctx->priv_data;
1026 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1028 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1032 s->buf = s->buf_start = pkt->data;
1033 s->buf_end = pkt->data + pkt->size;
1037 s->lambda = s->picture->quality * LAMBDA_SCALE;
1039 if (avctx->pix_fmt == AV_PIX_FMT_BGR48 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
1046 if (s->format == CODEC_JP2) {
1047 av_assert0(s->buf == pkt->data);
1049 bytestream_put_be32(&s->buf, 0x0000000C);
1050 bytestream_put_be32(&s->buf, 0x6A502020);
1051 bytestream_put_be32(&s->buf, 0x0D0A870A);
1053 chunkstart = s->buf;
1054 bytestream_put_be32(&s->buf, 0);
1055 bytestream_put_buffer(&s->buf, "ftyp", 4);
1056 bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1057 bytestream_put_be32(&s->buf, 0);
1058 bytestream_put_buffer(&s->buf, "jp2\040", 4);
1059 update_size(chunkstart, s->buf);
1062 bytestream_put_be32(&s->buf, 0);
1063 bytestream_put_buffer(&s->buf, "jp2h", 4);
1065 chunkstart = s->buf;
1066 bytestream_put_be32(&s->buf, 0);
1067 bytestream_put_buffer(&s->buf, "ihdr", 4);
1068 bytestream_put_be32(&s->buf, avctx->height);
1069 bytestream_put_be32(&s->buf, avctx->width);
1070 bytestream_put_be16(&s->buf, s->ncomponents);
1071 bytestream_put_byte(&s->buf, s->cbps[0]);
1072 bytestream_put_byte(&s->buf, 7);
1073 bytestream_put_byte(&s->buf, 0);
1074 bytestream_put_byte(&s->buf, 0);
1075 update_size(chunkstart, s->buf);
1077 chunkstart = s->buf;
1078 bytestream_put_be32(&s->buf, 0);
1079 bytestream_put_buffer(&s->buf, "colr", 4);
1080 bytestream_put_byte(&s->buf, 1);
1081 bytestream_put_byte(&s->buf, 0);
1082 bytestream_put_byte(&s->buf, 0);
1083 if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1084 bytestream_put_be32(&s->buf, 16);
1085 } else if (s->ncomponents == 1) {
1086 bytestream_put_be32(&s->buf, 17);
1088 bytestream_put_be32(&s->buf, 18);
1090 update_size(chunkstart, s->buf);
1091 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1093 uint8_t *palette = pict->data[1];
1094 chunkstart = s->buf;
1095 bytestream_put_be32(&s->buf, 0);
1096 bytestream_put_buffer(&s->buf, "pclr", 4);
1097 bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1098 bytestream_put_byte(&s->buf, 3); // colour channels
1099 bytestream_put_be24(&s->buf, 0x070707); //colour depths
1100 for (i = 0; i < AVPALETTE_COUNT; i++) {
1101 bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1104 update_size(chunkstart, s->buf);
1105 chunkstart = s->buf;
1106 bytestream_put_be32(&s->buf, 0);
1107 bytestream_put_buffer(&s->buf, "cmap", 4);
1108 for (i = 0; i < 3; i++) {
1109 bytestream_put_be16(&s->buf, 0); // component
1110 bytestream_put_byte(&s->buf, 1); // palette mapping
1111 bytestream_put_byte(&s->buf, i); // index
1113 update_size(chunkstart, s->buf);
1115 update_size(jp2hstart, s->buf);
1118 bytestream_put_be32(&s->buf, 0);
1119 bytestream_put_buffer(&s->buf, "jp2c", 4);
1122 if (s->buf_end - s->buf < 2)
1124 bytestream_put_be16(&s->buf, JPEG2000_SOC);
1125 if ((ret = put_siz(s)) < 0)
1127 if ((ret = put_cod(s)) < 0)
1129 if ((ret = put_qcd(s, 0)) < 0)
1131 if ((ret = put_com(s, 0)) < 0)
1134 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1136 if (!(psotptr = put_sot(s, tileno)))
1138 if (s->buf_end - s->buf < 2)
1140 bytestream_put_be16(&s->buf, JPEG2000_SOD);
1141 if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1143 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1145 if (s->buf_end - s->buf < 2)
1147 bytestream_put_be16(&s->buf, JPEG2000_EOC);
1149 if (s->format == CODEC_JP2)
1150 update_size(jp2cstart, s->buf);
1152 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1153 pkt->size = s->buf - s->buf_start;
1154 pkt->flags |= AV_PKT_FLAG_KEY;
1160 static av_cold int j2kenc_init(AVCodecContext *avctx)
1163 Jpeg2000EncoderContext *s = avctx->priv_data;
1164 Jpeg2000CodingStyle *codsty = &s->codsty;
1165 Jpeg2000QuantStyle *qntsty = &s->qntsty;
1168 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1170 #if FF_API_PRIVATE_OPT
1171 FF_DISABLE_DEPRECATION_WARNINGS
1172 if (avctx->prediction_method)
1173 s->pred = avctx->prediction_method;
1174 FF_ENABLE_DEPRECATION_WARNINGS
1177 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1178 av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1179 s->pred = FF_DWT97_INT;
1180 s->format = CODEC_JP2;
1184 // TODO: implement setting non-standard precinct size
1185 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1186 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1187 codsty->nreslevels2decode=
1188 codsty->nreslevels = 7;
1189 codsty->log2_cblk_width = 4;
1190 codsty->log2_cblk_height = 4;
1191 codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1193 qntsty->nguardbits = 1;
1195 if ((s->tile_width & (s->tile_width -1)) ||
1196 (s->tile_height & (s->tile_height-1))) {
1197 av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1200 if (codsty->transform == FF_DWT53)
1201 qntsty->quantsty = JPEG2000_QSTY_NONE;
1203 qntsty->quantsty = JPEG2000_QSTY_SE;
1205 s->width = avctx->width;
1206 s->height = avctx->height;
1208 for (i = 0; i < 3; i++) {
1209 if (avctx->pix_fmt == AV_PIX_FMT_GRAY16 || avctx->pix_fmt == AV_PIX_FMT_RGB48)
1215 if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_RGB48){
1217 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16){
1219 } else{ // planar YUV
1222 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
1223 s->chroma_shift, s->chroma_shift + 1);
1228 ff_jpeg2000_init_tier1_luts();
1229 ff_mqc_init_context_tables();
1232 init_quantization(s);
1233 if ((ret=init_tiles(s)) < 0)
1236 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1241 static int j2kenc_destroy(AVCodecContext *avctx)
1243 Jpeg2000EncoderContext *s = avctx->priv_data;
1249 // taken from the libopenjpeg wraper so it matches
1251 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1252 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1253 static const AVOption options[] = {
1254 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1255 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1256 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1257 { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1258 { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1259 { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1260 { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1261 { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1262 { "sop", "SOP marker", OFFSET(sop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1263 { "eph", "EPH marker", OFFSET(eph), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1267 static const AVClass j2k_class = {
1268 .class_name = "jpeg 2000 encoder",
1269 .item_name = av_default_item_name,
1271 .version = LIBAVUTIL_VERSION_INT,
1274 AVCodec ff_jpeg2000_encoder = {
1276 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1277 .type = AVMEDIA_TYPE_VIDEO,
1278 .id = AV_CODEC_ID_JPEG2000,
1279 .priv_data_size = sizeof(Jpeg2000EncoderContext),
1280 .init = j2kenc_init,
1281 .encode2 = encode_frame,
1282 .close = j2kenc_destroy,
1283 .pix_fmts = (const enum AVPixelFormat[]) {
1284 AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1285 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1286 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
1288 AV_PIX_FMT_RGB48, AV_PIX_FMT_GRAY16,
1291 .priv_class = &j2k_class,