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;
92 } Jpeg2000EncoderContext;
100 static void nspaces(FILE *fd, int n)
102 while(n--) putc(' ', fd);
105 static void printcomp(Jpeg2000Component *comp)
108 for (i = 0; i < comp->y1 - comp->y0; i++)
109 ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
112 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
114 int tileno, compno, reslevelno, bandno, precno;
115 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
116 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
118 s->width, s->height, s->tile_width, s->tile_height,
119 s->numXtiles, s->numYtiles, s->ncomponents);
120 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
121 Jpeg2000Tile *tile = s->tile + tileno;
123 fprintf(fd, "tile %d:\n", tileno);
124 for(compno = 0; compno < s->ncomponents; compno++){
125 Jpeg2000Component *comp = tile->comp + compno;
127 fprintf(fd, "component %d:\n", compno);
129 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
130 comp->x0, comp->x1, comp->y0, comp->y1);
131 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
132 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
134 fprintf(fd, "reslevel %d:\n", reslevelno);
136 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
137 reslevel->x0, reslevel->x1, reslevel->y0,
138 reslevel->y1, reslevel->nbands);
139 for(bandno = 0; bandno < reslevel->nbands; bandno++){
140 Jpeg2000Band *band = reslevel->band + bandno;
142 fprintf(fd, "band %d:\n", bandno);
144 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
145 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
148 band->codeblock_width, band->codeblock_height,
149 band->cblknx, band->cblkny);
150 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
151 Jpeg2000Prec *prec = band->prec + precno;
153 fprintf(fd, "prec %d:\n", precno);
155 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
156 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
165 /* bitstream routines */
167 /** put n times val bit */
168 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
171 if (s->bit_index == 8)
173 s->bit_index = *s->buf == 0xff;
176 *s->buf |= val << (7 - s->bit_index++);
180 /** put n least significant bits of a number num */
181 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
184 put_bits(s, (num >> n) & 1, 1);
187 /** flush the bitstream */
188 static void j2k_flush(Jpeg2000EncoderContext *s)
196 /* tag tree routines */
198 /** code the value stored in node */
199 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
201 Jpeg2000TgtNode *stack[30];
202 int sp = 1, curval = 0;
216 if (stack[sp]->val >= threshold){
217 put_bits(s, 0, threshold - curval);
220 put_bits(s, 0, stack[sp]->val - curval);
222 curval = stack[sp]->val;
226 /** update the value in node */
227 static void tag_tree_update(Jpeg2000TgtNode *node)
230 while (node->parent){
231 if (node->parent->val <= node->val)
233 node->parent->val = node->val;
239 static int put_siz(Jpeg2000EncoderContext *s)
243 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
246 bytestream_put_be16(&s->buf, JPEG2000_SIZ);
247 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
248 bytestream_put_be16(&s->buf, 0); // Rsiz
249 bytestream_put_be32(&s->buf, s->width); // width
250 bytestream_put_be32(&s->buf, s->height); // height
251 bytestream_put_be32(&s->buf, 0); // X0Siz
252 bytestream_put_be32(&s->buf, 0); // Y0Siz
254 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
255 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
256 bytestream_put_be32(&s->buf, 0); // XT0Siz
257 bytestream_put_be32(&s->buf, 0); // YT0Siz
258 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
260 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
261 bytestream_put_byte(&s->buf, 7);
262 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
263 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
268 static int put_cod(Jpeg2000EncoderContext *s)
270 Jpeg2000CodingStyle *codsty = &s->codsty;
272 if (s->buf_end - s->buf < 14)
275 bytestream_put_be16(&s->buf, JPEG2000_COD);
276 bytestream_put_be16(&s->buf, 12); // Lcod
277 bytestream_put_byte(&s->buf, 0); // Scod
279 bytestream_put_byte(&s->buf, 0); // progression level
280 bytestream_put_be16(&s->buf, 1); // num of layers
281 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
282 bytestream_put_byte(&s->buf, 0); // unspecified
284 bytestream_put_byte(&s->buf, 0); // unspecified
287 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
288 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
289 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
290 bytestream_put_byte(&s->buf, 0); // cblk style
291 bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
295 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
298 Jpeg2000CodingStyle *codsty = &s->codsty;
299 Jpeg2000QuantStyle *qntsty = &s->qntsty;
301 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
302 size = 4 + 3 * (codsty->nreslevels-1);
304 size = 5 + 6 * (codsty->nreslevels-1);
306 if (s->buf_end - s->buf < size + 2)
309 bytestream_put_be16(&s->buf, JPEG2000_QCD);
310 bytestream_put_be16(&s->buf, size); // LQcd
311 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
312 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
313 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
314 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
316 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
317 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
321 static int put_com(Jpeg2000EncoderContext *s, int compno)
323 int size = 4 + strlen(LIBAVCODEC_IDENT);
325 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
328 if (s->buf_end - s->buf < size + 2)
331 bytestream_put_be16(&s->buf, JPEG2000_COM);
332 bytestream_put_be16(&s->buf, size);
333 bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
335 bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
340 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
344 if (s->buf_end - s->buf < 12)
347 bytestream_put_be16(&s->buf, JPEG2000_SOT);
348 bytestream_put_be16(&s->buf, 10); // Lsot
349 bytestream_put_be16(&s->buf, tileno); // Isot
352 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
354 bytestream_put_byte(&s->buf, 0); // TPsot
355 bytestream_put_byte(&s->buf, 1); // TNsot
360 * compute the sizes of tiles, resolution levels, bands, etc.
361 * allocate memory for them
362 * divide the input image into tile-components
364 static int init_tiles(Jpeg2000EncoderContext *s)
366 int tileno, tilex, tiley, compno;
367 Jpeg2000CodingStyle *codsty = &s->codsty;
368 Jpeg2000QuantStyle *qntsty = &s->qntsty;
370 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
371 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
373 s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
375 return AVERROR(ENOMEM);
376 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
377 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
378 Jpeg2000Tile *tile = s->tile + tileno;
380 tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
382 return AVERROR(ENOMEM);
383 for (compno = 0; compno < s->ncomponents; compno++){
384 Jpeg2000Component *comp = tile->comp + compno;
387 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
388 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
389 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
390 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
392 for (i = 0; i < 2; i++)
393 for (j = 0; j < 2; j++)
394 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
396 if ((ret = ff_jpeg2000_init_component(comp,
400 compno?1<<s->chroma_shift[0]:1,
401 compno?1<<s->chroma_shift[1]:1,
410 static void copy_frame(Jpeg2000EncoderContext *s)
412 int tileno, compno, i, y, x;
414 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
415 Jpeg2000Tile *tile = s->tile + tileno;
417 for (compno = 0; compno < s->ncomponents; compno++){
418 Jpeg2000Component *comp = tile->comp + compno;
419 int *dst = comp->i_data;
420 line = s->picture->data[compno]
421 + comp->coord[1][0] * s->picture->linesize[compno]
423 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
425 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
426 *dst++ = *ptr++ - (1 << 7);
427 line += s->picture->linesize[compno];
431 line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
432 + tile->comp[0].coord[0][0] * s->ncomponents;
435 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
437 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
438 for (compno = 0; compno < s->ncomponents; compno++){
439 tile->comp[compno].i_data[i] = *ptr++ - (1 << 7);
442 line += s->picture->linesize[0];
448 static void init_quantization(Jpeg2000EncoderContext *s)
450 int compno, reslevelno, bandno;
451 Jpeg2000QuantStyle *qntsty = &s->qntsty;
452 Jpeg2000CodingStyle *codsty = &s->codsty;
454 for (compno = 0; compno < s->ncomponents; compno++){
456 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
457 int nbands, lev = codsty->nreslevels - reslevelno - 1;
458 nbands = reslevelno ? 3 : 1;
459 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
462 if (codsty->transform == FF_DWT97_INT){
463 int bandpos = bandno + (reslevelno>0),
464 ss = 81920000 / dwt_norms[0][bandpos][lev],
466 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
467 expn = s->cbps[compno] - log + 13;
469 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
471 qntsty->expn[gbandno] = expn;
472 qntsty->mant[gbandno] = mant;
478 static void init_luts(void)
481 mask = ~((1<<NMSEDEC_FRACBITS)-1);
483 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
484 lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
485 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
487 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
488 lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
489 << 13-NMSEDEC_FRACBITS, 0);
490 lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
495 /* tier-1 routines */
496 static int getnmsedec_sig(int x, int bpno)
498 if (bpno > NMSEDEC_FRACBITS)
499 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
500 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
503 static int getnmsedec_ref(int x, int bpno)
505 if (bpno > NMSEDEC_FRACBITS)
506 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
507 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
510 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
512 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
513 for (y0 = 0; y0 < height; y0 += 4)
514 for (x = 0; x < width; x++)
515 for (y = y0; y < height && y < y0+4; y++){
516 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
517 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
518 bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
519 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
522 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
523 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
524 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
525 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
527 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
532 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
534 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
535 for (y0 = 0; y0 < height; y0 += 4)
536 for (x = 0; x < width; x++)
537 for (y = y0; y < height && y < y0+4; y++)
538 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
539 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
540 *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
541 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
542 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
546 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
548 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
549 for (y0 = 0; y0 < height; y0 += 4)
550 for (x = 0; x < width; x++){
551 if (y0 + 3 < height && !(
552 (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
553 (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
554 (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
555 (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
559 for (rlen = 0; rlen < 4; rlen++)
560 if (t1->data[(y0+rlen) * t1->stride + x] & mask)
562 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
565 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
566 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
567 for (y = y0 + rlen; y < y0 + 4; y++){
568 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
569 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
571 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
572 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
574 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
575 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
576 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
577 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;
583 for (y = y0; y < y0 + 4 && y < height; y++){
584 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
585 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
586 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
587 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
589 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
590 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
591 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
592 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
595 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
601 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
602 int width, int height, int bandpos, int lev)
604 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
607 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
609 for (y = 0; y < height; y++){
610 for (x = 0; x < width; x++){
611 if (t1->data[(y) * t1->stride + x] < 0){
612 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
613 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
615 max = FFMAX(max, t1->data[(y) * t1->stride + x]);
620 cblk->nonzerobits = 0;
623 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
624 bpno = cblk->nonzerobits - 1;
627 ff_mqc_initenc(&t1->mqc, cblk->data);
629 for (passno = 0; bpno >= 0; passno++){
633 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
635 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
637 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
641 cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
642 wmsedec += (int64_t)nmsedec << (2*bpno);
643 cblk->passes[passno].disto = wmsedec;
650 cblk->npasses = passno;
651 cblk->ninclpasses = passno;
653 cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
656 /* tier-2 routines: */
658 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
665 put_num(s, 0xc | (n-3), 4);
667 put_num(s, 0x1e0 | (n-6), 9);
669 put_num(s, 0xff80 | (n-37), 16);
673 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
674 uint8_t *expn, int numgbits)
676 int bandno, empty = 1;
684 // is the packet empty?
685 for (bandno = 0; bandno < rlevel->nbands; bandno++){
686 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
687 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
693 put_bits(s, !empty, 1);
699 for (bandno = 0; bandno < rlevel->nbands; bandno++){
700 Jpeg2000Band *band = rlevel->band + bandno;
701 Jpeg2000Prec *prec = band->prec + precno;
703 int cblknw = prec->nb_codeblocks_width;
705 if (band->coord[0][0] == band->coord[0][1]
706 || band->coord[1][0] == band->coord[1][1])
709 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
710 for (xi = 0; xi < cblknw; xi++, pos++){
711 prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
712 tag_tree_update(prec->cblkincl + pos);
713 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
714 tag_tree_update(prec->zerobits + pos);
718 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
719 for (xi = 0; xi < cblknw; xi++, pos++){
720 int pad = 0, llen, length;
721 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
723 if (s->buf_end - s->buf < 20) // approximately
726 // inclusion information
727 tag_tree_code(s, prec->cblkincl + pos, 1);
728 if (!cblk->ninclpasses)
730 // zerobits information
731 tag_tree_code(s, prec->zerobits + pos, 100);
733 putnumpasses(s, cblk->ninclpasses);
735 length = cblk->passes[cblk->ninclpasses-1].rate;
736 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
741 // length of code block
742 put_bits(s, 1, llen);
744 put_num(s, length, av_log2(length)+1+pad);
749 for (bandno = 0; bandno < rlevel->nbands; bandno++){
750 Jpeg2000Band *band = rlevel->band + bandno;
751 Jpeg2000Prec *prec = band->prec + precno;
752 int yi, cblknw = prec->nb_codeblocks_width;
753 for (yi =0; yi < prec->nb_codeblocks_height; yi++){
755 for (xi = 0; xi < cblknw; xi++){
756 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
757 if (cblk->ninclpasses){
758 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
760 bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate
761 - cblk->passes[cblk->ninclpasses-1].flushed_len);
762 bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
763 cblk->passes[cblk->ninclpasses-1].flushed_len);
771 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
773 int compno, reslevelno, ret;
774 Jpeg2000CodingStyle *codsty = &s->codsty;
775 Jpeg2000QuantStyle *qntsty = &s->qntsty;
777 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
778 // lay-rlevel-comp-pos progression
779 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
780 for (compno = 0; compno < s->ncomponents; compno++){
782 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
783 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
784 if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
785 qntsty->nguardbits)) < 0)
790 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
794 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
797 for (passno = 0; passno < cblk->npasses; passno++){
801 dr = cblk->passes[passno].rate
802 - (res ? cblk->passes[res-1].rate:0);
803 dd = cblk->passes[passno].disto
804 - (res ? cblk->passes[res-1].disto:0);
806 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
812 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
814 int precno, compno, reslevelno, bandno, cblkno, lev;
815 Jpeg2000CodingStyle *codsty = &s->codsty;
817 for (compno = 0; compno < s->ncomponents; compno++){
818 Jpeg2000Component *comp = tile->comp + compno;
820 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
821 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
823 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
824 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
825 int bandpos = bandno + (reslevelno > 0);
826 Jpeg2000Band *band = reslevel->band + bandno;
827 Jpeg2000Prec *prec = band->prec + precno;
829 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
830 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
832 cblk->ninclpasses = getcut(cblk, s->lambda,
833 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
841 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
843 int compno, reslevelno, bandno, ret;
844 Jpeg2000T1Context t1;
845 Jpeg2000CodingStyle *codsty = &s->codsty;
846 for (compno = 0; compno < s->ncomponents; compno++){
847 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
849 t1.stride = (1<<codsty->log2_cblk_width) + 2;
851 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
852 if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
854 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
856 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
857 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
859 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
860 Jpeg2000Band *band = reslevel->band + bandno;
861 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
862 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
863 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
865 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
866 band->coord[1][1]) - band->coord[1][0] + yy0;
868 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
871 bandpos = bandno + (reslevelno > 0);
873 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
874 if (reslevelno == 0 || bandno == 1)
877 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
879 xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
880 band->coord[0][1]) - band->coord[0][0] + xx0;
882 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
884 if (codsty->transform == FF_DWT53){
885 for (y = yy0; y < yy1; y++){
886 int *ptr = t1.data + (y-yy0)*t1.stride;
887 for (x = xx0; x < xx1; x++){
888 *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
892 for (y = yy0; y < yy1; y++){
893 int *ptr = t1.data + (y-yy0)*t1.stride;
894 for (x = xx0; x < xx1; x++){
895 *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
896 *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
901 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
902 bandpos, codsty->nreslevels - reslevelno - 1);
904 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
907 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
911 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
914 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
915 truncpasses(s, tile);
916 if ((ret = encode_packets(s, tile, tileno)) < 0)
918 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
922 static void cleanup(Jpeg2000EncoderContext *s)
925 Jpeg2000CodingStyle *codsty = &s->codsty;
927 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
928 for (compno = 0; compno < s->ncomponents; compno++){
929 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
930 ff_jpeg2000_cleanup(comp, codsty);
932 av_freep(&s->tile[tileno].comp);
937 static void reinit(Jpeg2000EncoderContext *s)
940 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
941 Jpeg2000Tile *tile = s->tile + tileno;
942 for (compno = 0; compno < s->ncomponents; compno++)
943 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
947 static void update_size(uint8_t *size, const uint8_t *end)
949 AV_WB32(size, end-size);
952 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
953 const AVFrame *pict, int *got_packet)
956 Jpeg2000EncoderContext *s = avctx->priv_data;
957 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
959 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
963 s->buf = s->buf_start = pkt->data;
964 s->buf_end = pkt->data + pkt->size;
968 s->lambda = s->picture->quality * LAMBDA_SCALE;
973 if (s->format == CODEC_JP2) {
974 av_assert0(s->buf == pkt->data);
976 bytestream_put_be32(&s->buf, 0x0000000C);
977 bytestream_put_be32(&s->buf, 0x6A502020);
978 bytestream_put_be32(&s->buf, 0x0D0A870A);
981 bytestream_put_be32(&s->buf, 0);
982 bytestream_put_buffer(&s->buf, "ftyp", 4);
983 bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
984 bytestream_put_be32(&s->buf, 0);
985 bytestream_put_buffer(&s->buf, "jp2\040", 4);
986 update_size(chunkstart, s->buf);
989 bytestream_put_be32(&s->buf, 0);
990 bytestream_put_buffer(&s->buf, "jp2h", 4);
993 bytestream_put_be32(&s->buf, 0);
994 bytestream_put_buffer(&s->buf, "ihdr", 4);
995 bytestream_put_be32(&s->buf, avctx->height);
996 bytestream_put_be32(&s->buf, avctx->width);
997 bytestream_put_be16(&s->buf, s->ncomponents);
998 bytestream_put_byte(&s->buf, s->cbps[0]);
999 bytestream_put_byte(&s->buf, 7);
1000 bytestream_put_byte(&s->buf, 0);
1001 bytestream_put_byte(&s->buf, 0);
1002 update_size(chunkstart, s->buf);
1004 chunkstart = s->buf;
1005 bytestream_put_be32(&s->buf, 0);
1006 bytestream_put_buffer(&s->buf, "colr", 4);
1007 bytestream_put_byte(&s->buf, 1);
1008 bytestream_put_byte(&s->buf, 0);
1009 bytestream_put_byte(&s->buf, 0);
1010 if (s->ncomponents == 1) {
1011 bytestream_put_be32(&s->buf, 17);
1012 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
1013 bytestream_put_be32(&s->buf, 16);
1015 bytestream_put_be32(&s->buf, 18);
1017 update_size(chunkstart, s->buf);
1018 update_size(jp2hstart, s->buf);
1021 bytestream_put_be32(&s->buf, 0);
1022 bytestream_put_buffer(&s->buf, "jp2c", 4);
1025 if (s->buf_end - s->buf < 2)
1027 bytestream_put_be16(&s->buf, JPEG2000_SOC);
1028 if ((ret = put_siz(s)) < 0)
1030 if ((ret = put_cod(s)) < 0)
1032 if ((ret = put_qcd(s, 0)) < 0)
1034 if ((ret = put_com(s, 0)) < 0)
1037 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1039 if (!(psotptr = put_sot(s, tileno)))
1041 if (s->buf_end - s->buf < 2)
1043 bytestream_put_be16(&s->buf, JPEG2000_SOD);
1044 if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1046 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1048 if (s->buf_end - s->buf < 2)
1050 bytestream_put_be16(&s->buf, JPEG2000_EOC);
1052 if (s->format == CODEC_JP2)
1053 update_size(jp2cstart, s->buf);
1055 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1056 pkt->size = s->buf - s->buf_start;
1057 pkt->flags |= AV_PKT_FLAG_KEY;
1063 static av_cold int j2kenc_init(AVCodecContext *avctx)
1066 Jpeg2000EncoderContext *s = avctx->priv_data;
1067 Jpeg2000CodingStyle *codsty = &s->codsty;
1068 Jpeg2000QuantStyle *qntsty = &s->qntsty;
1071 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1073 #if FF_API_PRIVATE_OPT
1074 FF_DISABLE_DEPRECATION_WARNINGS
1075 if (avctx->prediction_method)
1076 s->pred = avctx->prediction_method;
1077 FF_ENABLE_DEPRECATION_WARNINGS
1081 // TODO: implement setting non-standard precinct size
1082 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1083 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1084 codsty->nreslevels2decode=
1085 codsty->nreslevels = 7;
1086 codsty->log2_cblk_width = 4;
1087 codsty->log2_cblk_height = 4;
1088 codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1090 qntsty->nguardbits = 1;
1092 if ((s->tile_width & (s->tile_width -1)) ||
1093 (s->tile_height & (s->tile_height-1))) {
1094 av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1097 if (codsty->transform == FF_DWT53)
1098 qntsty->quantsty = JPEG2000_QSTY_NONE;
1100 qntsty->quantsty = JPEG2000_QSTY_SE;
1102 s->width = avctx->width;
1103 s->height = avctx->height;
1105 for (i = 0; i < 3; i++)
1108 if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
1110 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
1112 } else{ // planar YUV
1115 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
1116 s->chroma_shift, s->chroma_shift + 1);
1119 ff_jpeg2000_init_tier1_luts();
1120 ff_mqc_init_context_tables();
1123 init_quantization(s);
1124 if ((ret=init_tiles(s)) < 0)
1127 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1132 static int j2kenc_destroy(AVCodecContext *avctx)
1134 Jpeg2000EncoderContext *s = avctx->priv_data;
1140 // taken from the libopenjpeg wraper so it matches
1142 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1143 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1144 static const AVOption options[] = {
1145 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1146 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1147 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1148 { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1149 { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1150 { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1151 { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1152 { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1157 static const AVClass j2k_class = {
1158 .class_name = "jpeg 2000 encoder",
1159 .item_name = av_default_item_name,
1161 .version = LIBAVUTIL_VERSION_INT,
1164 AVCodec ff_jpeg2000_encoder = {
1166 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1167 .type = AVMEDIA_TYPE_VIDEO,
1168 .id = AV_CODEC_ID_JPEG2000,
1169 .priv_data_size = sizeof(Jpeg2000EncoderContext),
1170 .init = j2kenc_init,
1171 .encode2 = encode_frame,
1172 .close = j2kenc_destroy,
1173 .pix_fmts = (const enum AVPixelFormat[]) {
1174 AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1175 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1176 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
1179 .priv_class = &j2k_class,