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 * Copyright (c) 2020, Gautam Ramakrishnan <gautamramk@gmail.com>
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
62 * JPEG2000 image encoder
64 * @author Kamil Nowosad
70 #include "bytestream.h"
72 #include "libavutil/common.h"
73 #include "libavutil/pixdesc.h"
74 #include "libavutil/opt.h"
75 #include "libavutil/intreadwrite.h"
76 #include "libavutil/avstring.h"
78 #define NMSEDEC_BITS 7
79 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
80 #define WMSEDEC_SHIFT 13 ///< must be >= 13
81 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
86 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
87 lut_nmsedec_ref0[1<<NMSEDEC_BITS],
88 lut_nmsedec_sig [1<<NMSEDEC_BITS],
89 lut_nmsedec_sig0[1<<NMSEDEC_BITS];
91 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
92 {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
93 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
94 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
95 {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
97 {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
98 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
99 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
100 { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
104 Jpeg2000Component *comp;
110 AVCodecContext *avctx;
111 const AVFrame *picture;
113 int width, height; ///< image width and height
114 uint8_t cbps[4]; ///< bits per sample in particular components
118 int tile_width, tile_height; ///< tile size
119 int numXtiles, numYtiles;
128 Jpeg2000CodingStyle codsty;
129 Jpeg2000QuantStyle qntsty;
132 int layer_rates[100];
133 uint8_t compression_rate_enc; ///< Is compression done using compression ratio?
142 } Jpeg2000EncoderContext;
150 static void nspaces(FILE *fd, int n)
152 while(n--) putc(' ', fd);
155 static void printcomp(Jpeg2000Component *comp)
158 for (i = 0; i < comp->y1 - comp->y0; i++)
159 ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
162 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
164 int tileno, compno, reslevelno, bandno, precno;
165 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
166 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
168 s->width, s->height, s->tile_width, s->tile_height,
169 s->numXtiles, s->numYtiles, s->ncomponents);
170 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
171 Jpeg2000Tile *tile = s->tile + tileno;
173 fprintf(fd, "tile %d:\n", tileno);
174 for(compno = 0; compno < s->ncomponents; compno++){
175 Jpeg2000Component *comp = tile->comp + compno;
177 fprintf(fd, "component %d:\n", compno);
179 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
180 comp->x0, comp->x1, comp->y0, comp->y1);
181 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
182 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
184 fprintf(fd, "reslevel %d:\n", reslevelno);
186 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
187 reslevel->x0, reslevel->x1, reslevel->y0,
188 reslevel->y1, reslevel->nbands);
189 for(bandno = 0; bandno < reslevel->nbands; bandno++){
190 Jpeg2000Band *band = reslevel->band + bandno;
192 fprintf(fd, "band %d:\n", bandno);
194 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
195 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
198 band->codeblock_width, band->codeblock_height,
199 band->cblknx, band->cblkny);
200 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
201 Jpeg2000Prec *prec = band->prec + precno;
203 fprintf(fd, "prec %d:\n", precno);
205 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
206 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
215 /* bitstream routines */
217 /** put n times val bit */
218 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
221 if (s->bit_index == 8)
223 s->bit_index = *s->buf == 0xff;
226 *s->buf |= val << (7 - s->bit_index++);
230 /** put n least significant bits of a number num */
231 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
234 put_bits(s, (num >> n) & 1, 1);
237 /** flush the bitstream */
238 static void j2k_flush(Jpeg2000EncoderContext *s)
246 /* tag tree routines */
248 /** code the value stored in node */
249 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
251 Jpeg2000TgtNode *stack[30];
252 int sp = -1, curval = 0;
260 if (curval > node->temp_val)
261 node->temp_val = curval;
263 curval = node->temp_val;
266 if (node->val >= threshold) {
267 put_bits(s, 0, threshold - curval);
270 put_bits(s, 0, node->val - curval);
278 node->temp_val = curval;
285 /** update the value in node */
286 static void tag_tree_update(Jpeg2000TgtNode *node)
289 while (node->parent){
290 if (node->parent->val <= node->val)
292 node->parent->val = node->val;
298 static int put_siz(Jpeg2000EncoderContext *s)
302 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
305 bytestream_put_be16(&s->buf, JPEG2000_SIZ);
306 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
307 bytestream_put_be16(&s->buf, 0); // Rsiz
308 bytestream_put_be32(&s->buf, s->width); // width
309 bytestream_put_be32(&s->buf, s->height); // height
310 bytestream_put_be32(&s->buf, 0); // X0Siz
311 bytestream_put_be32(&s->buf, 0); // Y0Siz
313 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
314 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
315 bytestream_put_be32(&s->buf, 0); // XT0Siz
316 bytestream_put_be32(&s->buf, 0); // YT0Siz
317 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
319 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
320 bytestream_put_byte(&s->buf, s->cbps[i] - 1);
321 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
322 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
327 static int put_cod(Jpeg2000EncoderContext *s)
329 Jpeg2000CodingStyle *codsty = &s->codsty;
332 if (s->buf_end - s->buf < 14)
335 bytestream_put_be16(&s->buf, JPEG2000_COD);
336 bytestream_put_be16(&s->buf, 12); // Lcod
338 scod |= JPEG2000_CSTY_SOP;
340 scod |= JPEG2000_CSTY_EPH;
341 bytestream_put_byte(&s->buf, scod); // Scod
343 bytestream_put_byte(&s->buf, s->prog); // progression level
344 bytestream_put_be16(&s->buf, s->nlayers); // num of layers
345 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
346 bytestream_put_byte(&s->buf, 0); // unspecified
348 bytestream_put_byte(&s->buf, 0); // unspecified
351 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
352 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
353 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
354 bytestream_put_byte(&s->buf, 0); // cblk style
355 bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
359 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
362 Jpeg2000CodingStyle *codsty = &s->codsty;
363 Jpeg2000QuantStyle *qntsty = &s->qntsty;
365 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
366 size = 4 + 3 * (codsty->nreslevels-1);
368 size = 5 + 6 * (codsty->nreslevels-1);
370 if (s->buf_end - s->buf < size + 2)
373 bytestream_put_be16(&s->buf, JPEG2000_QCD);
374 bytestream_put_be16(&s->buf, size); // LQcd
375 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
376 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
377 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
378 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
380 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
381 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
385 static int put_com(Jpeg2000EncoderContext *s, int compno)
387 int size = 4 + strlen(LIBAVCODEC_IDENT);
389 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
392 if (s->buf_end - s->buf < size + 2)
395 bytestream_put_be16(&s->buf, JPEG2000_COM);
396 bytestream_put_be16(&s->buf, size);
397 bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
399 bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
404 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
408 if (s->buf_end - s->buf < 12)
411 bytestream_put_be16(&s->buf, JPEG2000_SOT);
412 bytestream_put_be16(&s->buf, 10); // Lsot
413 bytestream_put_be16(&s->buf, tileno); // Isot
416 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
418 bytestream_put_byte(&s->buf, 0); // TPsot
419 bytestream_put_byte(&s->buf, 1); // TNsot
423 static void compute_rates(Jpeg2000EncoderContext* s)
427 for (i = 0; i < s->numYtiles; i++) {
428 for (j = 0; j < s->numXtiles; j++) {
429 Jpeg2000Tile *tile = &s->tile[s->numXtiles * i + j];
430 for (compno = 0; compno < s->ncomponents; compno++) {
431 int tilew = tile->comp[compno].coord[0][1] - tile->comp[compno].coord[0][0];
432 int tileh = tile->comp[compno].coord[1][1] - tile->comp[compno].coord[1][0];
433 int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << s->chroma_shift[1]:1);
434 for (layno = 0; layno < s->nlayers; layno++) {
435 if (s->layer_rates[layno] > 0) {
436 tile->layer_rates[layno] += (double)(tilew * tileh) * s->ncomponents * s->cbps[compno] /
437 (double)(s->layer_rates[layno] * 8 * scale);
439 tile->layer_rates[layno] = 0.0;
449 * compute the sizes of tiles, resolution levels, bands, etc.
450 * allocate memory for them
451 * divide the input image into tile-components
453 static int init_tiles(Jpeg2000EncoderContext *s)
455 int tileno, tilex, tiley, compno;
456 Jpeg2000CodingStyle *codsty = &s->codsty;
457 Jpeg2000QuantStyle *qntsty = &s->qntsty;
459 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
460 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
462 s->tile = av_calloc(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
464 return AVERROR(ENOMEM);
465 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
466 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
467 Jpeg2000Tile *tile = s->tile + tileno;
469 tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
471 return AVERROR(ENOMEM);
473 tile->layer_rates = av_mallocz_array(s->nlayers, sizeof(*tile->layer_rates));
474 if (!tile->layer_rates)
475 return AVERROR(ENOMEM);
477 for (compno = 0; compno < s->ncomponents; compno++){
478 Jpeg2000Component *comp = tile->comp + compno;
481 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
482 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
483 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
484 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
486 for (i = 0; i < 2; i++)
487 for (j = 0; j < 2; j++)
488 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
490 if ((ret = ff_jpeg2000_init_component(comp,
494 compno?1<<s->chroma_shift[0]:1,
495 compno?1<<s->chroma_shift[1]:1,
505 #define COPY_FRAME(D, PIXEL) \
506 static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
508 int tileno, compno, i, y, x; \
510 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
511 Jpeg2000Tile *tile = s->tile + tileno; \
513 for (compno = 0; compno < s->ncomponents; compno++){ \
514 Jpeg2000Component *comp = tile->comp + compno; \
515 int *dst = comp->i_data; \
516 int cbps = s->cbps[compno]; \
517 line = (PIXEL*)s->picture->data[compno] \
518 + comp->coord[1][0] * (s->picture->linesize[compno] / sizeof(PIXEL)) \
519 + comp->coord[0][0]; \
520 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
522 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
523 *dst++ = *ptr++ - (1 << (cbps - 1)); \
524 line += s->picture->linesize[compno] / sizeof(PIXEL); \
528 line = (PIXEL*)s->picture->data[0] + tile->comp[0].coord[1][0] * (s->picture->linesize[0] / sizeof(PIXEL)) \
529 + tile->comp[0].coord[0][0] * s->ncomponents; \
532 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
534 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
535 for (compno = 0; compno < s->ncomponents; compno++){ \
536 int cbps = s->cbps[compno]; \
537 tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
540 line += s->picture->linesize[0] / sizeof(PIXEL); \
546 COPY_FRAME(8, uint8_t)
547 COPY_FRAME(16, uint16_t)
549 static void init_quantization(Jpeg2000EncoderContext *s)
551 int compno, reslevelno, bandno;
552 Jpeg2000QuantStyle *qntsty = &s->qntsty;
553 Jpeg2000CodingStyle *codsty = &s->codsty;
555 for (compno = 0; compno < s->ncomponents; compno++){
557 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
558 int nbands, lev = codsty->nreslevels - reslevelno - 1;
559 nbands = reslevelno ? 3 : 1;
560 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
563 if (codsty->transform == FF_DWT97_INT){
564 int bandpos = bandno + (reslevelno>0),
565 ss = 81920000 / dwt_norms[0][bandpos][lev],
567 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
568 expn = s->cbps[compno] - log + 13;
570 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
572 qntsty->expn[gbandno] = expn;
573 qntsty->mant[gbandno] = mant;
579 static void init_luts(void)
582 mask = ~((1<<NMSEDEC_FRACBITS)-1);
584 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
585 lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
586 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
588 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
589 lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
590 (1 << 13) - (a * a << 11), 0);
591 lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
596 /* tier-1 routines */
597 static int getnmsedec_sig(int x, int bpno)
599 if (bpno > NMSEDEC_FRACBITS)
600 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
601 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
604 static int getnmsedec_ref(int x, int bpno)
606 if (bpno > NMSEDEC_FRACBITS)
607 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
608 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
611 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
613 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
614 for (y0 = 0; y0 < height; y0 += 4)
615 for (x = 0; x < width; x++)
616 for (y = y0; y < height && y < y0+4; y++){
617 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
618 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
619 bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
620 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
623 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
624 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
625 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
626 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
628 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
633 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
635 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
636 for (y0 = 0; y0 < height; y0 += 4)
637 for (x = 0; x < width; x++)
638 for (y = y0; y < height && y < y0+4; y++)
639 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
640 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
641 *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
642 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
643 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
647 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
649 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
650 for (y0 = 0; y0 < height; y0 += 4)
651 for (x = 0; x < width; x++){
652 if (y0 + 3 < height && !(
653 (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
654 (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
655 (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
656 (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
660 for (rlen = 0; rlen < 4; rlen++)
661 if (t1->data[(y0+rlen) * t1->stride + x] & mask)
663 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
666 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
667 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
668 for (y = y0 + rlen; y < y0 + 4; y++){
669 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
670 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
672 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
673 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
675 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
676 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
677 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
678 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
681 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
684 for (y = y0; y < y0 + 4 && y < height; y++){
685 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
686 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
687 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
688 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
690 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
691 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
692 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
693 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
696 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
702 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
703 int width, int height, int bandpos, int lev)
705 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
708 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
710 for (y = 0; y < height; y++){
711 for (x = 0; x < width; x++){
712 if (t1->data[(y) * t1->stride + x] < 0){
713 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
714 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
716 max = FFMAX(max, t1->data[(y) * t1->stride + x]);
721 cblk->nonzerobits = 0;
724 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
725 bpno = cblk->nonzerobits - 1;
729 ff_mqc_initenc(&t1->mqc, cblk->data + 1);
731 for (passno = 0; bpno >= 0; passno++){
735 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
737 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
739 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
743 cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
744 cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
746 wmsedec += (int64_t)nmsedec << (2*bpno);
747 cblk->passes[passno].disto = wmsedec;
754 cblk->npasses = passno;
755 cblk->ninclpasses = passno;
758 cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
759 cblk->passes[passno-1].rate -= cblk->passes[passno-1].flushed_len;
763 /* tier-2 routines: */
765 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
772 put_num(s, 0xc | (n-3), 4);
774 put_num(s, 0x1e0 | (n-6), 9);
776 put_num(s, 0xff80 | (n-37), 16);
780 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno,
781 int precno, uint8_t *expn, int numgbits, int packetno,
784 int bandno, empty = 1;
791 bytestream_put_be16(&s->buf, JPEG2000_SOP);
792 bytestream_put_be16(&s->buf, 4);
793 bytestream_put_be16(&s->buf, packetno);
798 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
799 Jpeg2000Band *band = rlevel->band + bandno;
800 if (band->coord[0][0] < band->coord[0][1]
801 && band->coord[1][0] < band->coord[1][1]) {
802 Jpeg2000Prec *prec = band->prec + precno;
803 int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
805 ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 99);
806 ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 99);
807 for (pos = 0; pos < nb_cblks; pos++) {
808 Jpeg2000Cblk *cblk = &prec->cblk[pos];
809 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits;
812 tag_tree_update(prec->zerobits + pos);
813 for (i = 0; i < nlayers; i++) {
814 if (cblk->layers[i].npasses > 0) {
815 prec->cblkincl[pos].val = i;
820 prec->cblkincl[pos].val = i;
821 tag_tree_update(prec->cblkincl + pos);
827 // is the packet empty?
828 for (bandno = 0; bandno < rlevel->nbands; bandno++){
829 Jpeg2000Band *band = rlevel->band + bandno;
830 if (band->coord[0][0] < band->coord[0][1]
831 && band->coord[1][0] < band->coord[1][1]) {
832 Jpeg2000Prec *prec = band->prec + precno;
833 int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
835 for (pos = 0; pos < nb_cblks; pos++) {
836 Jpeg2000Cblk *cblk = &prec->cblk[pos];
837 if (cblk->layers[layno].npasses) {
847 put_bits(s, !empty, 1);
851 bytestream_put_be16(&s->buf, JPEG2000_EPH);
855 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
856 Jpeg2000Band *band = rlevel->band + bandno;
857 Jpeg2000Prec *prec = band->prec + precno;
859 int cblknw = prec->nb_codeblocks_width;
861 if (band->coord[0][0] == band->coord[0][1]
862 || band->coord[1][0] == band->coord[1][1])
865 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++) {
866 for (xi = 0; xi < cblknw; xi++, pos++){
867 int llen = 0, length;
868 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
870 if (s->buf_end - s->buf < 20) // approximately
873 // inclusion information
875 tag_tree_code(s, prec->cblkincl + pos, layno + 1);
877 put_bits(s, cblk->layers[layno].npasses > 0, 1);
880 if (!cblk->layers[layno].npasses)
883 // zerobits information
885 tag_tree_code(s, prec->zerobits + pos, 100);
890 putnumpasses(s, cblk->layers[layno].npasses);
892 length = cblk->layers[layno].data_len;
893 if (layno == nlayers - 1 && cblk->layers[layno].cum_passes){
894 length += cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len;
896 if (cblk->lblock + av_log2(cblk->layers[layno].npasses) < av_log2(length) + 1) {
897 llen = av_log2(length) + 1 - cblk->lblock - av_log2(cblk->layers[layno].npasses);
900 // length of code block
901 cblk->lblock += llen;
902 put_bits(s, 1, llen);
904 put_num(s, length, cblk->lblock + av_log2(cblk->layers[layno].npasses));
910 bytestream_put_be16(&s->buf, JPEG2000_EPH);
913 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
914 Jpeg2000Band *band = rlevel->band + bandno;
915 Jpeg2000Prec *prec = band->prec + precno;
916 int yi, cblknw = prec->nb_codeblocks_width;
917 for (yi =0; yi < prec->nb_codeblocks_height; yi++) {
919 for (xi = 0; xi < cblknw; xi++){
920 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
921 if (cblk->layers[layno].npasses) {
922 if (s->buf_end - s->buf < cblk->layers[layno].data_len + 2)
924 bytestream_put_buffer(&s->buf, cblk->layers[layno].data_start + 1, cblk->layers[layno].data_len);
925 if (layno == nlayers - 1 && cblk->layers[layno].cum_passes) {
926 bytestream_put_buffer(&s->buf, cblk->passes[cblk->layers[layno].cum_passes-1].flushed,
927 cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len);
936 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
938 int compno, reslevelno, layno, ret;
939 Jpeg2000CodingStyle *codsty = &s->codsty;
940 Jpeg2000QuantStyle *qntsty = &s->qntsty;
944 int tile_coord[2][2];
945 int col = tileno % s->numXtiles;
946 int row = tileno / s->numXtiles;
948 tile_coord[0][0] = col * s->tile_width;
949 tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
950 tile_coord[1][0] = row * s->tile_height;
951 tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
953 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
954 // lay-rlevel-comp-pos progression
956 case JPEG2000_PGOD_LRCP:
957 for (layno = 0; layno < nlayers; layno++) {
958 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
959 for (compno = 0; compno < s->ncomponents; compno++){
961 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
962 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
963 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
964 qntsty->nguardbits, packetno++, nlayers)) < 0)
971 case JPEG2000_PGOD_RLCP:
972 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
973 for (layno = 0; layno < nlayers; layno++) {
974 for (compno = 0; compno < s->ncomponents; compno++){
976 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
977 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
978 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
979 qntsty->nguardbits, packetno++, nlayers)) < 0)
986 case JPEG2000_PGOD_RPCL:
987 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
991 for (compno = 0; compno < s->ncomponents; compno++) {
992 Jpeg2000Component *comp = tile->comp + compno;
993 if (reslevelno < codsty->nreslevels) {
994 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
995 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
996 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
997 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1003 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1004 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1005 for (compno = 0; compno < s->ncomponents; compno++) {
1006 Jpeg2000Component *comp = tile->comp + compno;
1007 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1008 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1009 int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1010 unsigned prcx, prcy;
1013 trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1014 try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1016 if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1017 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1020 if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1021 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1024 // check if a precinct exists
1025 prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1026 prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1027 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1028 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1029 precno = prcx + reslevel->num_precincts_x * prcy;
1031 if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1032 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1033 prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1036 for (layno = 0; layno < nlayers; layno++) {
1037 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1038 qntsty->nguardbits, packetno++, nlayers)) < 0)
1046 case JPEG2000_PGOD_PCRL:
1049 for (compno = 0; compno < s->ncomponents; compno++) {
1050 Jpeg2000Component *comp = tile->comp + compno;
1052 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1053 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1054 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1055 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1056 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1059 if (step_x >= 31 || step_y >= 31){
1060 avpriv_request_sample(s->avctx, "PCRL with large step");
1061 return AVERROR_PATCHWELCOME;
1066 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1067 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1068 for (compno = 0; compno < s->ncomponents; compno++) {
1069 Jpeg2000Component *comp = tile->comp + compno;
1070 int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1072 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1073 unsigned prcx, prcy;
1075 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1076 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1079 trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1080 try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1082 if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1083 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1086 if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1087 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1090 // check if a precinct exists
1091 prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1092 prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1093 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1094 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1096 precno = prcx + reslevel->num_precincts_x * prcy;
1098 if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1099 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1100 prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1103 for (layno = 0; layno < nlayers; layno++) {
1104 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1105 qntsty->nguardbits, packetno++, nlayers)) < 0)
1113 case JPEG2000_PGOD_CPRL:
1114 for (compno = 0; compno < s->ncomponents; compno++) {
1115 Jpeg2000Component *comp = tile->comp + compno;
1116 int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1120 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1121 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1122 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1123 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1124 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1126 if (step_x >= 31 || step_y >= 31){
1127 avpriv_request_sample(s->avctx, "CPRL with large step");
1128 return AVERROR_PATCHWELCOME;
1133 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1134 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1135 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1136 unsigned prcx, prcy;
1139 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1140 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1142 trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1143 try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1145 if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1146 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1149 if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1150 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1153 // check if a precinct exists
1154 prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1155 prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1156 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1157 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1159 precno = prcx + reslevel->num_precincts_x * prcy;
1161 if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1162 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1163 prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1166 for (layno = 0; layno < nlayers; layno++) {
1167 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1168 qntsty->nguardbits, packetno++, nlayers)) < 0)
1178 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
1182 static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final)
1184 int compno, resno, bandno, precno, cblkno;
1187 for (compno = 0; compno < s->ncomponents; compno++) {
1188 Jpeg2000Component *comp = &tile->comp[compno];
1190 for (resno = 0; resno < s->codsty.nreslevels; resno++) {
1191 Jpeg2000ResLevel *reslevel = comp->reslevel + resno;
1193 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1194 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1195 Jpeg2000Band *band = reslevel->band + bandno;
1196 Jpeg2000Prec *prec = band->prec + precno;
1198 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1199 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1200 Jpeg2000Layer *layer = &cblk->layers[layno];
1204 cblk->ninclpasses = 0;
1207 n = cblk->ninclpasses;
1212 for (passno = cblk->ninclpasses; passno < cblk->npasses; passno++) {
1215 Jpeg2000Pass *pass = &cblk->passes[passno];
1221 dr = pass->rate - cblk->passes[n - 1].rate;
1222 dd = pass->disto - cblk->passes[n-1].disto;
1232 if (thresh - (dd / dr) < DBL_EPSILON)
1236 layer->npasses = n - cblk->ninclpasses;
1237 layer->cum_passes = n;
1239 if (layer->npasses == 0) {
1241 layer->data_len = 0;
1245 if (cblk->ninclpasses == 0) {
1246 layer->data_len = cblk->passes[n - 1].rate;
1247 layer->data_start = cblk->data;
1248 layer->disto = cblk->passes[n - 1].disto;
1250 layer->data_len = cblk->passes[n - 1].rate - cblk->passes[cblk->ninclpasses - 1].rate;
1251 layer->data_start = cblk->data + cblk->passes[cblk->ninclpasses - 1].rate;
1252 layer->disto = cblk->passes[n - 1].disto -
1253 cblk->passes[cblk->ninclpasses - 1].disto;
1256 cblk->ninclpasses = n;
1265 static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
1267 int precno, compno, reslevelno, bandno, cblkno, lev, passno, layno;
1269 double min = DBL_MAX;
1274 Jpeg2000CodingStyle *codsty = &s->codsty;
1276 for (compno = 0; compno < s->ncomponents; compno++){
1277 Jpeg2000Component *comp = tile->comp + compno;
1279 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1280 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1282 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1283 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1284 Jpeg2000Band *band = reslevel->band + bandno;
1285 Jpeg2000Prec *prec = band->prec + precno;
1287 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1288 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1289 for (passno = 0; passno < cblk->npasses; passno++) {
1290 Jpeg2000Pass *pass = &cblk->passes[passno];
1294 tile_disto += pass->disto;
1296 dr = (int32_t)pass->rate;
1299 dr = (int32_t)(pass->rate - cblk->passes[passno - 1].rate);
1300 dd = pass->disto - cblk->passes[passno - 1].disto;
1319 for (layno = 0; layno < s->nlayers; layno++) {
1322 double stable_thresh = 0.0;
1323 double good_thresh = 0.0;
1324 if (!s->layer_rates[layno]) {
1327 for (i = 0; i < 128; i++) {
1328 uint8_t *stream_pos = s->buf;
1330 thresh = (lo + hi) / 2;
1331 makelayer(s, layno, thresh, tile, 0);
1332 ret = encode_packets(s, tile, (int)(tile - s->tile), layno + 1);
1333 memset(stream_pos, 0, s->buf - stream_pos);
1334 if ((s->buf - stream_pos > ceil(tile->layer_rates[layno])) || ret < 0) {
1336 s->buf = stream_pos;
1340 stable_thresh = thresh;
1341 s->buf = stream_pos;
1344 if (good_thresh >= 0.0)
1345 good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
1346 makelayer(s, layno, good_thresh, tile, 1);
1350 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
1352 int passno, res = 0;
1353 for (passno = 0; passno < cblk->npasses; passno++){
1357 dr = cblk->passes[passno].rate
1358 - (res ? cblk->passes[res-1].rate : 0);
1359 dd = cblk->passes[passno].disto
1360 - (res ? cblk->passes[res-1].disto : 0);
1362 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
1368 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
1370 int precno, compno, reslevelno, bandno, cblkno, lev;
1371 Jpeg2000CodingStyle *codsty = &s->codsty;
1373 for (compno = 0; compno < s->ncomponents; compno++){
1374 Jpeg2000Component *comp = tile->comp + compno;
1376 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1377 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1379 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1380 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1381 int bandpos = bandno + (reslevelno > 0);
1382 Jpeg2000Band *band = reslevel->band + bandno;
1383 Jpeg2000Prec *prec = band->prec + precno;
1385 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1386 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1388 cblk->ninclpasses = getcut(cblk, s->lambda,
1389 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
1390 cblk->layers[0].data_start = cblk->data;
1391 cblk->layers[0].cum_passes = cblk->ninclpasses;
1392 cblk->layers[0].npasses = cblk->ninclpasses;
1393 if (cblk->ninclpasses)
1394 cblk->layers[0].data_len = cblk->passes[cblk->ninclpasses - 1].rate;
1402 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
1404 int compno, reslevelno, bandno, ret;
1405 Jpeg2000T1Context t1;
1406 Jpeg2000CodingStyle *codsty = &s->codsty;
1407 for (compno = 0; compno < s->ncomponents; compno++){
1408 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1410 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1412 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
1413 if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
1415 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
1417 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
1418 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1420 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1421 Jpeg2000Band *band = reslevel->band + bandno;
1422 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
1423 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
1424 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
1426 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
1427 band->coord[1][1]) - band->coord[1][0] + yy0;
1429 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
1432 bandpos = bandno + (reslevelno > 0);
1434 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
1435 if (reslevelno == 0 || bandno == 1)
1438 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
1440 xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
1441 band->coord[0][1]) - band->coord[0][0] + xx0;
1443 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
1445 if (codsty->transform == FF_DWT53){
1446 for (y = yy0; y < yy1; y++){
1447 int *ptr = t1.data + (y-yy0)*t1.stride;
1448 for (x = xx0; x < xx1; x++){
1449 *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
1453 for (y = yy0; y < yy1; y++){
1454 int *ptr = t1.data + (y-yy0)*t1.stride;
1455 for (x = xx0; x < xx1; x++){
1456 *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
1457 *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
1462 if (!prec->cblk[cblkno].data)
1463 prec->cblk[cblkno].data = av_malloc(1 + 8192);
1464 if (!prec->cblk[cblkno].passes)
1465 prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
1466 if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
1467 return AVERROR(ENOMEM);
1468 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
1469 bandpos, codsty->nreslevels - reslevelno - 1);
1471 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
1474 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
1478 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
1481 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
1482 if (s->compression_rate_enc)
1483 makelayers(s, tile);
1485 truncpasses(s, tile);
1487 if ((ret = encode_packets(s, tile, tileno, s->nlayers)) < 0)
1489 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
1493 static void cleanup(Jpeg2000EncoderContext *s)
1496 Jpeg2000CodingStyle *codsty = &s->codsty;
1500 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1501 if (s->tile[tileno].comp) {
1502 for (compno = 0; compno < s->ncomponents; compno++){
1503 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1504 ff_jpeg2000_cleanup(comp, codsty);
1506 av_freep(&s->tile[tileno].comp);
1508 av_freep(&s->tile[tileno].layer_rates);
1513 static void reinit(Jpeg2000EncoderContext *s)
1516 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1517 Jpeg2000Tile *tile = s->tile + tileno;
1518 for (compno = 0; compno < s->ncomponents; compno++)
1519 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1523 static void update_size(uint8_t *size, const uint8_t *end)
1525 AV_WB32(size, end-size);
1528 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1529 const AVFrame *pict, int *got_packet)
1532 Jpeg2000EncoderContext *s = avctx->priv_data;
1533 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1535 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1539 s->buf = s->buf_start = pkt->data;
1540 s->buf_end = pkt->data + pkt->size;
1544 s->lambda = s->picture->quality * LAMBDA_SCALE;
1546 if (avctx->pix_fmt == AV_PIX_FMT_BGR48 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
1553 if (s->format == CODEC_JP2) {
1554 av_assert0(s->buf == pkt->data);
1556 bytestream_put_be32(&s->buf, 0x0000000C);
1557 bytestream_put_be32(&s->buf, 0x6A502020);
1558 bytestream_put_be32(&s->buf, 0x0D0A870A);
1560 chunkstart = s->buf;
1561 bytestream_put_be32(&s->buf, 0);
1562 bytestream_put_buffer(&s->buf, "ftyp", 4);
1563 bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1564 bytestream_put_be32(&s->buf, 0);
1565 bytestream_put_buffer(&s->buf, "jp2\040", 4);
1566 update_size(chunkstart, s->buf);
1569 bytestream_put_be32(&s->buf, 0);
1570 bytestream_put_buffer(&s->buf, "jp2h", 4);
1572 chunkstart = s->buf;
1573 bytestream_put_be32(&s->buf, 0);
1574 bytestream_put_buffer(&s->buf, "ihdr", 4);
1575 bytestream_put_be32(&s->buf, avctx->height);
1576 bytestream_put_be32(&s->buf, avctx->width);
1577 bytestream_put_be16(&s->buf, s->ncomponents);
1578 bytestream_put_byte(&s->buf, s->cbps[0]);
1579 bytestream_put_byte(&s->buf, 7);
1580 bytestream_put_byte(&s->buf, 0);
1581 bytestream_put_byte(&s->buf, 0);
1582 update_size(chunkstart, s->buf);
1584 chunkstart = s->buf;
1585 bytestream_put_be32(&s->buf, 0);
1586 bytestream_put_buffer(&s->buf, "colr", 4);
1587 bytestream_put_byte(&s->buf, 1);
1588 bytestream_put_byte(&s->buf, 0);
1589 bytestream_put_byte(&s->buf, 0);
1590 if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1591 bytestream_put_be32(&s->buf, 16);
1592 } else if (s->ncomponents == 1) {
1593 bytestream_put_be32(&s->buf, 17);
1595 bytestream_put_be32(&s->buf, 18);
1597 update_size(chunkstart, s->buf);
1598 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1600 uint8_t *palette = pict->data[1];
1601 chunkstart = s->buf;
1602 bytestream_put_be32(&s->buf, 0);
1603 bytestream_put_buffer(&s->buf, "pclr", 4);
1604 bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1605 bytestream_put_byte(&s->buf, 3); // colour channels
1606 bytestream_put_be24(&s->buf, 0x070707); //colour depths
1607 for (i = 0; i < AVPALETTE_COUNT; i++) {
1608 bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1611 update_size(chunkstart, s->buf);
1612 chunkstart = s->buf;
1613 bytestream_put_be32(&s->buf, 0);
1614 bytestream_put_buffer(&s->buf, "cmap", 4);
1615 for (i = 0; i < 3; i++) {
1616 bytestream_put_be16(&s->buf, 0); // component
1617 bytestream_put_byte(&s->buf, 1); // palette mapping
1618 bytestream_put_byte(&s->buf, i); // index
1620 update_size(chunkstart, s->buf);
1622 update_size(jp2hstart, s->buf);
1625 bytestream_put_be32(&s->buf, 0);
1626 bytestream_put_buffer(&s->buf, "jp2c", 4);
1629 if (s->buf_end - s->buf < 2)
1631 bytestream_put_be16(&s->buf, JPEG2000_SOC);
1632 if ((ret = put_siz(s)) < 0)
1634 if ((ret = put_cod(s)) < 0)
1636 if ((ret = put_qcd(s, 0)) < 0)
1638 if ((ret = put_com(s, 0)) < 0)
1641 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1643 if (!(psotptr = put_sot(s, tileno)))
1645 if (s->buf_end - s->buf < 2)
1647 bytestream_put_be16(&s->buf, JPEG2000_SOD);
1648 if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1650 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1652 if (s->buf_end - s->buf < 2)
1654 bytestream_put_be16(&s->buf, JPEG2000_EOC);
1656 if (s->format == CODEC_JP2)
1657 update_size(jp2cstart, s->buf);
1659 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1660 pkt->size = s->buf - s->buf_start;
1661 pkt->flags |= AV_PKT_FLAG_KEY;
1667 static int parse_layer_rates(Jpeg2000EncoderContext *s)
1671 char *saveptr = NULL;
1676 s->layer_rates[0] = 0;
1677 s->compression_rate_enc = 0;
1681 token = av_strtok(s->lr_str, ",", &saveptr);
1682 if (rate = strtol(token, NULL, 10)) {
1683 s->layer_rates[0] = rate <= 1 ? 0:rate;
1686 return AVERROR_INVALIDDATA;
1690 token = av_strtok(NULL, ",", &saveptr);
1693 if (rate = strtol(token, NULL, 10)) {
1694 if (nlayers >= 100) {
1695 return AVERROR_INVALIDDATA;
1697 s->layer_rates[nlayers] = rate <= 1 ? 0:rate;
1700 return AVERROR_INVALIDDATA;
1704 for (i = 1; i < nlayers; i++) {
1705 if (s->layer_rates[i] >= s->layer_rates[i-1]) {
1706 return AVERROR_INVALIDDATA;
1709 s->nlayers = nlayers;
1710 s->compression_rate_enc = 1;
1714 static av_cold int j2kenc_init(AVCodecContext *avctx)
1717 Jpeg2000EncoderContext *s = avctx->priv_data;
1718 Jpeg2000CodingStyle *codsty = &s->codsty;
1719 Jpeg2000QuantStyle *qntsty = &s->qntsty;
1722 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1723 if (parse_layer_rates(s)) {
1724 av_log(s, AV_LOG_WARNING, "Layer rates invalid. Encoding with 1 layer based on quality metric.\n");
1726 s->layer_rates[0] = 0;
1727 s->compression_rate_enc = 0;
1730 #if FF_API_PRIVATE_OPT
1731 FF_DISABLE_DEPRECATION_WARNINGS
1732 if (avctx->prediction_method)
1733 s->pred = avctx->prediction_method;
1734 FF_ENABLE_DEPRECATION_WARNINGS
1737 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1738 av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1739 s->pred = FF_DWT97_INT;
1740 s->format = CODEC_JP2;
1744 // TODO: implement setting non-standard precinct size
1745 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1746 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1747 codsty->nreslevels2decode=
1748 codsty->nreslevels = 7;
1749 codsty->nlayers = s->nlayers;
1750 codsty->log2_cblk_width = 4;
1751 codsty->log2_cblk_height = 4;
1752 codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1754 qntsty->nguardbits = 1;
1756 if ((s->tile_width & (s->tile_width -1)) ||
1757 (s->tile_height & (s->tile_height-1))) {
1758 av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1761 if (codsty->transform == FF_DWT53)
1762 qntsty->quantsty = JPEG2000_QSTY_NONE;
1764 qntsty->quantsty = JPEG2000_QSTY_SE;
1766 s->width = avctx->width;
1767 s->height = avctx->height;
1769 for (i = 0; i < 3; i++) {
1770 if (avctx->pix_fmt == AV_PIX_FMT_GRAY16 || avctx->pix_fmt == AV_PIX_FMT_RGB48)
1776 if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_RGB48){
1778 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16){
1780 } else{ // planar YUV
1783 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
1784 s->chroma_shift, s->chroma_shift + 1);
1789 ff_jpeg2000_init_tier1_luts();
1790 ff_mqc_init_context_tables();
1793 init_quantization(s);
1794 if ((ret=init_tiles(s)) < 0)
1797 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1802 static int j2kenc_destroy(AVCodecContext *avctx)
1804 Jpeg2000EncoderContext *s = avctx->priv_data;
1810 // taken from the libopenjpeg wraper so it matches
1812 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1813 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1814 static const AVOption options[] = {
1815 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1816 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1817 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1818 { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1819 { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1820 { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1821 { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1822 { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1823 { "sop", "SOP marker", OFFSET(sop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1824 { "eph", "EPH marker", OFFSET(eph), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1825 { "prog", "Progression Order", OFFSET(prog), AV_OPT_TYPE_INT, { .i64 = 0 }, JPEG2000_PGOD_LRCP, JPEG2000_PGOD_CPRL, VE, "prog" },
1826 { "lrcp", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_LRCP }, 0, 0, VE, "prog" },
1827 { "rlcp", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RLCP }, 0, 0, VE, "prog" },
1828 { "rpcl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RPCL }, 0, 0, VE, "prog" },
1829 { "pcrl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_PCRL }, 0, 0, VE, "prog" },
1830 { "cprl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_CPRL }, 0, 0, VE, "prog" },
1831 { "layer_rates", "Layer Rates", OFFSET(lr_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
1835 static const AVClass j2k_class = {
1836 .class_name = "jpeg 2000 encoder",
1837 .item_name = av_default_item_name,
1839 .version = LIBAVUTIL_VERSION_INT,
1842 AVCodec ff_jpeg2000_encoder = {
1844 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1845 .type = AVMEDIA_TYPE_VIDEO,
1846 .id = AV_CODEC_ID_JPEG2000,
1847 .priv_data_size = sizeof(Jpeg2000EncoderContext),
1848 .init = j2kenc_init,
1849 .encode2 = encode_frame,
1850 .close = j2kenc_destroy,
1851 .pix_fmts = (const enum AVPixelFormat[]) {
1852 AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1853 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1854 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
1856 AV_PIX_FMT_RGB48, AV_PIX_FMT_GRAY16,
1859 .priv_class = &j2k_class,
1860 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,