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
23 * JPEG2000 image encoder
25 * @author Kamil Nowosad
31 #include "bytestream.h"
33 #include "libavutil/common.h"
34 #include "libavutil/opt.h"
36 #define NMSEDEC_BITS 7
37 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
38 #define WMSEDEC_SHIFT 13 ///< must be >= 13
39 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
44 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
45 lut_nmsedec_ref0[1<<NMSEDEC_BITS],
46 lut_nmsedec_sig [1<<NMSEDEC_BITS],
47 lut_nmsedec_sig0[1<<NMSEDEC_BITS];
49 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
50 {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
51 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
52 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
53 {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
55 {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
56 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
57 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
58 { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
62 Jpeg2000Component *comp;
67 AVCodecContext *avctx;
68 const AVFrame *picture;
70 int width, height; ///< image width and height
71 uint8_t cbps[4]; ///< bits per sample in particular components
75 int tile_width, tile_height; ///< tile size
76 int numXtiles, numYtiles;
85 Jpeg2000CodingStyle codsty;
86 Jpeg2000QuantStyle qntsty;
91 } Jpeg2000EncoderContext;
99 static void nspaces(FILE *fd, int n)
101 while(n--) putc(' ', fd);
104 static void printcomp(Jpeg2000Component *comp)
107 for (i = 0; i < comp->y1 - comp->y0; i++)
108 ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
111 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
113 int tileno, compno, reslevelno, bandno, precno;
114 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
115 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
117 s->width, s->height, s->tile_width, s->tile_height,
118 s->numXtiles, s->numYtiles, s->ncomponents);
119 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
120 Jpeg2000Tile *tile = s->tile + tileno;
122 fprintf(fd, "tile %d:\n", tileno);
123 for(compno = 0; compno < s->ncomponents; compno++){
124 Jpeg2000Component *comp = tile->comp + compno;
126 fprintf(fd, "component %d:\n", compno);
128 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
129 comp->x0, comp->x1, comp->y0, comp->y1);
130 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
131 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
133 fprintf(fd, "reslevel %d:\n", reslevelno);
135 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
136 reslevel->x0, reslevel->x1, reslevel->y0,
137 reslevel->y1, reslevel->nbands);
138 for(bandno = 0; bandno < reslevel->nbands; bandno++){
139 Jpeg2000Band *band = reslevel->band + bandno;
141 fprintf(fd, "band %d:\n", bandno);
143 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
144 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
147 band->codeblock_width, band->codeblock_height,
148 band->cblknx, band->cblkny);
149 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
150 Jpeg2000Prec *prec = band->prec + precno;
152 fprintf(fd, "prec %d:\n", precno);
154 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
155 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
164 /* bitstream routines */
166 /** put n times val bit */
167 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
170 if (s->bit_index == 8)
172 s->bit_index = *s->buf == 0xff;
175 *s->buf |= val << (7 - s->bit_index++);
179 /** put n least significant bits of a number num */
180 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
183 put_bits(s, (num >> n) & 1, 1);
186 /** flush the bitstream */
187 static void j2k_flush(Jpeg2000EncoderContext *s)
195 /* tag tree routines */
197 /** code the value stored in node */
198 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
200 Jpeg2000TgtNode *stack[30];
201 int sp = 1, curval = 0;
215 if (stack[sp]->val >= threshold){
216 put_bits(s, 0, threshold - curval);
219 put_bits(s, 0, stack[sp]->val - curval);
221 curval = stack[sp]->val;
225 /** update the value in node */
226 static void tag_tree_update(Jpeg2000TgtNode *node)
229 while (node->parent){
230 if (node->parent->val <= node->val)
232 node->parent->val = node->val;
238 static int put_siz(Jpeg2000EncoderContext *s)
242 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
245 bytestream_put_be16(&s->buf, JPEG2000_SIZ);
246 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
247 bytestream_put_be16(&s->buf, 0); // Rsiz
248 bytestream_put_be32(&s->buf, s->width); // width
249 bytestream_put_be32(&s->buf, s->height); // height
250 bytestream_put_be32(&s->buf, 0); // X0Siz
251 bytestream_put_be32(&s->buf, 0); // Y0Siz
253 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
254 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
255 bytestream_put_be32(&s->buf, 0); // XT0Siz
256 bytestream_put_be32(&s->buf, 0); // YT0Siz
257 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
259 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
260 bytestream_put_byte(&s->buf, 7);
261 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
262 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
267 static int put_cod(Jpeg2000EncoderContext *s)
269 Jpeg2000CodingStyle *codsty = &s->codsty;
271 if (s->buf_end - s->buf < 14)
274 bytestream_put_be16(&s->buf, JPEG2000_COD);
275 bytestream_put_be16(&s->buf, 12); // Lcod
276 bytestream_put_byte(&s->buf, 0); // Scod
278 bytestream_put_byte(&s->buf, 0); // progression level
279 bytestream_put_be16(&s->buf, 1); // num of layers
280 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
281 bytestream_put_byte(&s->buf, 0); // unspecified
283 bytestream_put_byte(&s->buf, 0); // unspecified
286 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
287 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
288 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
289 bytestream_put_byte(&s->buf, 0); // cblk style
290 bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
294 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
297 Jpeg2000CodingStyle *codsty = &s->codsty;
298 Jpeg2000QuantStyle *qntsty = &s->qntsty;
300 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
301 size = 4 + 3 * (codsty->nreslevels-1);
303 size = 5 + 6 * (codsty->nreslevels-1);
305 if (s->buf_end - s->buf < size + 2)
308 bytestream_put_be16(&s->buf, JPEG2000_QCD);
309 bytestream_put_be16(&s->buf, size); // LQcd
310 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
311 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
312 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
313 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
315 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
316 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
320 static int put_com(Jpeg2000EncoderContext *s, int compno)
322 int size = 4 + strlen(LIBAVCODEC_IDENT);
324 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
327 if (s->buf_end - s->buf < size + 2)
330 bytestream_put_be16(&s->buf, JPEG2000_COM);
331 bytestream_put_be16(&s->buf, size);
332 bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
334 bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
339 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
343 if (s->buf_end - s->buf < 12)
346 bytestream_put_be16(&s->buf, JPEG2000_SOT);
347 bytestream_put_be16(&s->buf, 10); // Lsot
348 bytestream_put_be16(&s->buf, tileno); // Isot
351 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
353 bytestream_put_byte(&s->buf, 0); // TPsot
354 bytestream_put_byte(&s->buf, 1); // TNsot
359 * compute the sizes of tiles, resolution levels, bands, etc.
360 * allocate memory for them
361 * divide the input image into tile-components
363 static int init_tiles(Jpeg2000EncoderContext *s)
365 int tileno, tilex, tiley, compno;
366 Jpeg2000CodingStyle *codsty = &s->codsty;
367 Jpeg2000QuantStyle *qntsty = &s->qntsty;
369 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
370 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
372 s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
374 return AVERROR(ENOMEM);
375 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
376 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
377 Jpeg2000Tile *tile = s->tile + tileno;
379 tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
381 return AVERROR(ENOMEM);
382 for (compno = 0; compno < s->ncomponents; compno++){
383 Jpeg2000Component *comp = tile->comp + compno;
386 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
387 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
388 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
389 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
391 for (i = 0; i < 2; i++)
392 for (j = 0; j < 2; j++)
393 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
395 if ((ret = ff_jpeg2000_init_component(comp,
399 compno?1<<s->chroma_shift[0]:1,
400 compno?1<<s->chroma_shift[1]:1,
409 static void copy_frame(Jpeg2000EncoderContext *s)
411 int tileno, compno, i, y, x;
413 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
414 Jpeg2000Tile *tile = s->tile + tileno;
416 for (compno = 0; compno < s->ncomponents; compno++){
417 Jpeg2000Component *comp = tile->comp + compno;
418 int *dst = comp->i_data;
419 line = s->picture->data[compno]
420 + comp->coord[1][0] * s->picture->linesize[compno]
422 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
424 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
425 *dst++ = *ptr++ - (1 << 7);
426 line += s->picture->linesize[compno];
430 line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
431 + tile->comp[0].coord[0][0] * s->ncomponents;
434 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
436 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
437 for (compno = 0; compno < s->ncomponents; compno++){
438 tile->comp[compno].i_data[i] = *ptr++ - (1 << 7);
441 line += s->picture->linesize[0];
447 static void init_quantization(Jpeg2000EncoderContext *s)
449 int compno, reslevelno, bandno;
450 Jpeg2000QuantStyle *qntsty = &s->qntsty;
451 Jpeg2000CodingStyle *codsty = &s->codsty;
453 for (compno = 0; compno < s->ncomponents; compno++){
455 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
456 int nbands, lev = codsty->nreslevels - reslevelno - 1;
457 nbands = reslevelno ? 3 : 1;
458 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
461 if (codsty->transform == FF_DWT97_INT){
462 int bandpos = bandno + (reslevelno>0),
463 ss = 81920000 / dwt_norms[0][bandpos][lev],
465 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
466 expn = s->cbps[compno] - log + 13;
468 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
470 qntsty->expn[gbandno] = expn;
471 qntsty->mant[gbandno] = mant;
477 static void init_luts(void)
480 mask = ~((1<<NMSEDEC_FRACBITS)-1);
482 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
483 lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
484 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
486 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
487 lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
488 << 13-NMSEDEC_FRACBITS, 0);
489 lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
494 /* tier-1 routines */
495 static int getnmsedec_sig(int x, int bpno)
497 if (bpno > NMSEDEC_FRACBITS)
498 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
499 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
502 static int getnmsedec_ref(int x, int bpno)
504 if (bpno > NMSEDEC_FRACBITS)
505 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
506 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
509 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
511 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
512 for (y0 = 0; y0 < height; y0 += 4)
513 for (x = 0; x < width; x++)
514 for (y = y0; y < height && y < y0+4; y++){
515 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
516 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
517 bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
518 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
521 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
522 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
523 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
524 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
526 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
531 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
533 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
534 for (y0 = 0; y0 < height; y0 += 4)
535 for (x = 0; x < width; x++)
536 for (y = y0; y < height && y < y0+4; y++)
537 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
538 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
539 *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
540 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
541 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
545 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
547 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
548 for (y0 = 0; y0 < height; y0 += 4)
549 for (x = 0; x < width; x++){
550 if (y0 + 3 < height && !(
551 (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
552 (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
553 (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
554 (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
558 for (rlen = 0; rlen < 4; rlen++)
559 if (t1->data[(y0+rlen) * t1->stride + x] & mask)
561 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
564 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
565 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
566 for (y = y0 + rlen; y < y0 + 4; y++){
567 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
568 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
570 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
571 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
573 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
574 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
575 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
576 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
579 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
582 for (y = y0; y < y0 + 4 && y < height; y++){
583 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
584 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
585 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
586 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
588 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
589 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
590 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
591 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
594 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
600 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
601 int width, int height, int bandpos, int lev)
603 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
606 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
608 for (y = 0; y < height; y++){
609 for (x = 0; x < width; x++){
610 if (t1->data[(y) * t1->stride + x] < 0){
611 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
612 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
614 max = FFMAX(max, t1->data[(y) * t1->stride + x]);
619 cblk->nonzerobits = 0;
622 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
623 bpno = cblk->nonzerobits - 1;
626 ff_mqc_initenc(&t1->mqc, cblk->data);
628 for (passno = 0; bpno >= 0; passno++){
632 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
634 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
636 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
640 cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
641 wmsedec += (int64_t)nmsedec << (2*bpno);
642 cblk->passes[passno].disto = wmsedec;
649 cblk->npasses = passno;
650 cblk->ninclpasses = passno;
652 cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
655 /* tier-2 routines: */
657 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
664 put_num(s, 0xc | (n-3), 4);
666 put_num(s, 0x1e0 | (n-6), 9);
668 put_num(s, 0xff80 | (n-37), 16);
672 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
673 uint8_t *expn, int numgbits)
675 int bandno, empty = 1;
683 // is the packet empty?
684 for (bandno = 0; bandno < rlevel->nbands; bandno++){
685 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
686 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
692 put_bits(s, !empty, 1);
698 for (bandno = 0; bandno < rlevel->nbands; bandno++){
699 Jpeg2000Band *band = rlevel->band + bandno;
700 Jpeg2000Prec *prec = band->prec + precno;
702 int cblknw = prec->nb_codeblocks_width;
704 if (band->coord[0][0] == band->coord[0][1]
705 || band->coord[1][0] == band->coord[1][1])
708 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
709 for (xi = 0; xi < cblknw; xi++, pos++){
710 prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
711 tag_tree_update(prec->cblkincl + pos);
712 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
713 tag_tree_update(prec->zerobits + pos);
717 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
718 for (xi = 0; xi < cblknw; xi++, pos++){
719 int pad = 0, llen, length;
720 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
722 if (s->buf_end - s->buf < 20) // approximately
725 // inclusion information
726 tag_tree_code(s, prec->cblkincl + pos, 1);
727 if (!cblk->ninclpasses)
729 // zerobits information
730 tag_tree_code(s, prec->zerobits + pos, 100);
732 putnumpasses(s, cblk->ninclpasses);
734 length = cblk->passes[cblk->ninclpasses-1].rate;
735 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
740 // length of code block
741 put_bits(s, 1, llen);
743 put_num(s, length, av_log2(length)+1+pad);
748 for (bandno = 0; bandno < rlevel->nbands; bandno++){
749 Jpeg2000Band *band = rlevel->band + bandno;
750 Jpeg2000Prec *prec = band->prec + precno;
751 int yi, cblknw = prec->nb_codeblocks_width;
752 for (yi =0; yi < prec->nb_codeblocks_height; yi++){
754 for (xi = 0; xi < cblknw; xi++){
755 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
756 if (cblk->ninclpasses){
757 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
759 bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate
760 - cblk->passes[cblk->ninclpasses-1].flushed_len);
761 bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
762 cblk->passes[cblk->ninclpasses-1].flushed_len);
770 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
772 int compno, reslevelno, ret;
773 Jpeg2000CodingStyle *codsty = &s->codsty;
774 Jpeg2000QuantStyle *qntsty = &s->qntsty;
776 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
777 // lay-rlevel-comp-pos progression
778 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
779 for (compno = 0; compno < s->ncomponents; compno++){
781 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
782 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
783 if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
784 qntsty->nguardbits)) < 0)
789 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
793 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
796 for (passno = 0; passno < cblk->npasses; passno++){
800 dr = cblk->passes[passno].rate
801 - (res ? cblk->passes[res-1].rate:0);
802 dd = cblk->passes[passno].disto
803 - (res ? cblk->passes[res-1].disto:0);
805 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
811 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
813 int precno, compno, reslevelno, bandno, cblkno, lev;
814 Jpeg2000CodingStyle *codsty = &s->codsty;
816 for (compno = 0; compno < s->ncomponents; compno++){
817 Jpeg2000Component *comp = tile->comp + compno;
819 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
820 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
822 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
823 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
824 int bandpos = bandno + (reslevelno > 0);
825 Jpeg2000Band *band = reslevel->band + bandno;
826 Jpeg2000Prec *prec = band->prec + precno;
828 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
829 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
831 cblk->ninclpasses = getcut(cblk, s->lambda,
832 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
840 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
842 int compno, reslevelno, bandno, ret;
843 Jpeg2000T1Context t1;
844 Jpeg2000CodingStyle *codsty = &s->codsty;
845 for (compno = 0; compno < s->ncomponents; compno++){
846 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
848 t1.stride = (1<<codsty->log2_cblk_width) + 2;
850 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
851 if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
853 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
855 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
856 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
858 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
859 Jpeg2000Band *band = reslevel->band + bandno;
860 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
861 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
862 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
864 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
865 band->coord[1][1]) - band->coord[1][0] + yy0;
867 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
870 bandpos = bandno + (reslevelno > 0);
872 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
873 if (reslevelno == 0 || bandno == 1)
876 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
878 xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
879 band->coord[0][1]) - band->coord[0][0] + xx0;
881 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
883 if (codsty->transform == FF_DWT53){
884 for (y = yy0; y < yy1; y++){
885 int *ptr = t1.data + (y-yy0)*t1.stride;
886 for (x = xx0; x < xx1; x++){
887 *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
891 for (y = yy0; y < yy1; y++){
892 int *ptr = t1.data + (y-yy0)*t1.stride;
893 for (x = xx0; x < xx1; x++){
894 *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
895 *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
900 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
901 bandpos, codsty->nreslevels - reslevelno - 1);
903 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
906 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
910 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
913 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
914 truncpasses(s, tile);
915 if ((ret = encode_packets(s, tile, tileno)) < 0)
917 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
921 static void cleanup(Jpeg2000EncoderContext *s)
924 Jpeg2000CodingStyle *codsty = &s->codsty;
926 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
927 for (compno = 0; compno < s->ncomponents; compno++){
928 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
929 ff_jpeg2000_cleanup(comp, codsty);
931 av_freep(&s->tile[tileno].comp);
936 static void reinit(Jpeg2000EncoderContext *s)
939 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
940 Jpeg2000Tile *tile = s->tile + tileno;
941 for (compno = 0; compno < s->ncomponents; compno++)
942 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
946 static void update_size(uint8_t *size, const uint8_t *end)
948 AV_WB32(size, end-size);
951 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
952 const AVFrame *pict, int *got_packet)
955 Jpeg2000EncoderContext *s = avctx->priv_data;
956 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
958 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
962 s->buf = s->buf_start = pkt->data;
963 s->buf_end = pkt->data + pkt->size;
967 s->lambda = s->picture->quality * LAMBDA_SCALE;
972 if (s->format == CODEC_JP2) {
973 av_assert0(s->buf == pkt->data);
975 bytestream_put_be32(&s->buf, 0x0000000C);
976 bytestream_put_be32(&s->buf, 0x6A502020);
977 bytestream_put_be32(&s->buf, 0x0D0A870A);
980 bytestream_put_be32(&s->buf, 0);
981 bytestream_put_buffer(&s->buf, "ftyp", 4);
982 bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
983 bytestream_put_be32(&s->buf, 0);
984 bytestream_put_buffer(&s->buf, "jp2\040", 4);
985 update_size(chunkstart, s->buf);
988 bytestream_put_be32(&s->buf, 0);
989 bytestream_put_buffer(&s->buf, "jp2h", 4);
992 bytestream_put_be32(&s->buf, 0);
993 bytestream_put_buffer(&s->buf, "ihdr", 4);
994 bytestream_put_be32(&s->buf, avctx->height);
995 bytestream_put_be32(&s->buf, avctx->width);
996 bytestream_put_be16(&s->buf, s->ncomponents);
997 bytestream_put_byte(&s->buf, s->cbps[0]);
998 bytestream_put_byte(&s->buf, 7);
999 bytestream_put_byte(&s->buf, 0);
1000 bytestream_put_byte(&s->buf, 0);
1001 update_size(chunkstart, s->buf);
1003 chunkstart = s->buf;
1004 bytestream_put_be32(&s->buf, 0);
1005 bytestream_put_buffer(&s->buf, "colr", 4);
1006 bytestream_put_byte(&s->buf, 1);
1007 bytestream_put_byte(&s->buf, 0);
1008 bytestream_put_byte(&s->buf, 0);
1009 if (s->ncomponents == 1) {
1010 bytestream_put_be32(&s->buf, 17);
1011 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
1012 bytestream_put_be32(&s->buf, 16);
1014 bytestream_put_be32(&s->buf, 18);
1016 update_size(chunkstart, s->buf);
1017 update_size(jp2hstart, s->buf);
1020 bytestream_put_be32(&s->buf, 0);
1021 bytestream_put_buffer(&s->buf, "jp2c", 4);
1024 if (s->buf_end - s->buf < 2)
1026 bytestream_put_be16(&s->buf, JPEG2000_SOC);
1027 if ((ret = put_siz(s)) < 0)
1029 if ((ret = put_cod(s)) < 0)
1031 if ((ret = put_qcd(s, 0)) < 0)
1033 if ((ret = put_com(s, 0)) < 0)
1036 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1038 if (!(psotptr = put_sot(s, tileno)))
1040 if (s->buf_end - s->buf < 2)
1042 bytestream_put_be16(&s->buf, JPEG2000_SOD);
1043 if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1045 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1047 if (s->buf_end - s->buf < 2)
1049 bytestream_put_be16(&s->buf, JPEG2000_EOC);
1051 if (s->format == CODEC_JP2)
1052 update_size(jp2cstart, s->buf);
1054 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1055 pkt->size = s->buf - s->buf_start;
1056 pkt->flags |= AV_PKT_FLAG_KEY;
1062 static av_cold int j2kenc_init(AVCodecContext *avctx)
1065 Jpeg2000EncoderContext *s = avctx->priv_data;
1066 Jpeg2000CodingStyle *codsty = &s->codsty;
1067 Jpeg2000QuantStyle *qntsty = &s->qntsty;
1070 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1073 // TODO: implement setting non-standard precinct size
1074 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1075 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1076 codsty->nreslevels2decode=
1077 codsty->nreslevels = 7;
1078 codsty->log2_cblk_width = 4;
1079 codsty->log2_cblk_height = 4;
1080 codsty->transform = avctx->prediction_method ? FF_DWT53 : FF_DWT97_INT;
1082 qntsty->nguardbits = 1;
1084 if ((s->tile_width & (s->tile_width -1)) ||
1085 (s->tile_height & (s->tile_height-1))) {
1086 av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1089 if (codsty->transform == FF_DWT53)
1090 qntsty->quantsty = JPEG2000_QSTY_NONE;
1092 qntsty->quantsty = JPEG2000_QSTY_SE;
1094 s->width = avctx->width;
1095 s->height = avctx->height;
1097 for (i = 0; i < 3; i++)
1100 if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
1102 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
1104 } else{ // planar YUV
1107 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
1108 s->chroma_shift, s->chroma_shift + 1);
1111 ff_jpeg2000_init_tier1_luts();
1112 ff_mqc_init_context_tables();
1115 init_quantization(s);
1116 if ((ret=init_tiles(s)) < 0)
1119 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1124 static int j2kenc_destroy(AVCodecContext *avctx)
1126 Jpeg2000EncoderContext *s = avctx->priv_data;
1132 // taken from the libopenjpeg wraper so it matches
1134 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1135 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1136 static const AVOption options[] = {
1137 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1138 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1139 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1140 { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1141 { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1146 static const AVClass j2k_class = {
1147 .class_name = "jpeg 2000 encoder",
1148 .item_name = av_default_item_name,
1150 .version = LIBAVUTIL_VERSION_INT,
1153 AVCodec ff_jpeg2000_encoder = {
1155 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1156 .type = AVMEDIA_TYPE_VIDEO,
1157 .id = AV_CODEC_ID_JPEG2000,
1158 .priv_data_size = sizeof(Jpeg2000EncoderContext),
1159 .init = j2kenc_init,
1160 .encode2 = encode_frame,
1161 .close = j2kenc_destroy,
1162 .pix_fmts = (const enum AVPixelFormat[]) {
1163 AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1164 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1165 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
1168 .priv_class = &j2k_class,