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 uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
324 if (s->buf_end - s->buf < 12)
327 bytestream_put_be16(&s->buf, JPEG2000_SOT);
328 bytestream_put_be16(&s->buf, 10); // Lsot
329 bytestream_put_be16(&s->buf, tileno); // Isot
332 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
334 bytestream_put_byte(&s->buf, 0); // TPsot
335 bytestream_put_byte(&s->buf, 1); // TNsot
340 * compute the sizes of tiles, resolution levels, bands, etc.
341 * allocate memory for them
342 * divide the input image into tile-components
344 static int init_tiles(Jpeg2000EncoderContext *s)
346 int tileno, tilex, tiley, compno;
347 Jpeg2000CodingStyle *codsty = &s->codsty;
348 Jpeg2000QuantStyle *qntsty = &s->qntsty;
350 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
351 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
353 s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
355 return AVERROR(ENOMEM);
356 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
357 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
358 Jpeg2000Tile *tile = s->tile + tileno;
360 tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
362 return AVERROR(ENOMEM);
363 for (compno = 0; compno < s->ncomponents; compno++){
364 Jpeg2000Component *comp = tile->comp + compno;
367 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
368 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
369 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
370 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
372 for (i = 0; i < 2; i++)
373 for (j = 0; j < 2; j++)
374 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
376 if ((ret = ff_jpeg2000_init_component(comp,
380 compno?1<<s->chroma_shift[0]:1,
381 compno?1<<s->chroma_shift[1]:1,
390 static void copy_frame(Jpeg2000EncoderContext *s)
392 int tileno, compno, i, y, x;
394 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
395 Jpeg2000Tile *tile = s->tile + tileno;
397 for (compno = 0; compno < s->ncomponents; compno++){
398 Jpeg2000Component *comp = tile->comp + compno;
399 int *dst = comp->i_data;
400 line = s->picture->data[compno]
401 + comp->coord[1][0] * s->picture->linesize[compno]
403 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
405 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
406 *dst++ = *ptr++ - (1 << 7);
407 line += s->picture->linesize[compno];
411 line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
412 + tile->comp[0].coord[0][0] * s->ncomponents;
415 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
417 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
418 for (compno = 0; compno < s->ncomponents; compno++){
419 tile->comp[compno].i_data[i] = *ptr++ - (1 << 7);
422 line += s->picture->linesize[0];
428 static void init_quantization(Jpeg2000EncoderContext *s)
430 int compno, reslevelno, bandno;
431 Jpeg2000QuantStyle *qntsty = &s->qntsty;
432 Jpeg2000CodingStyle *codsty = &s->codsty;
434 for (compno = 0; compno < s->ncomponents; compno++){
436 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
437 int nbands, lev = codsty->nreslevels - reslevelno - 1;
438 nbands = reslevelno ? 3 : 1;
439 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
442 if (codsty->transform == FF_DWT97_INT){
443 int bandpos = bandno + (reslevelno>0),
444 ss = 81920000 / dwt_norms[0][bandpos][lev],
446 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
447 expn = s->cbps[compno] - log + 13;
449 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
451 qntsty->expn[gbandno] = expn;
452 qntsty->mant[gbandno] = mant;
458 static void init_luts(void)
461 mask = ~((1<<NMSEDEC_FRACBITS)-1);
463 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
464 lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
465 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
467 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
468 lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
469 << 13-NMSEDEC_FRACBITS, 0);
470 lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
475 /* tier-1 routines */
476 static int getnmsedec_sig(int x, int bpno)
478 if (bpno > NMSEDEC_FRACBITS)
479 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
480 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
483 static int getnmsedec_ref(int x, int bpno)
485 if (bpno > NMSEDEC_FRACBITS)
486 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
487 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
490 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
492 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
493 for (y0 = 0; y0 < height; y0 += 4)
494 for (x = 0; x < width; x++)
495 for (y = y0; y < height && y < y0+4; y++){
496 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
497 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
498 bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
499 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
502 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
503 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
504 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
505 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
507 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
512 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
514 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
515 for (y0 = 0; y0 < height; y0 += 4)
516 for (x = 0; x < width; x++)
517 for (y = y0; y < height && y < y0+4; y++)
518 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
519 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
520 *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
521 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
522 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
526 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
528 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
529 for (y0 = 0; y0 < height; y0 += 4)
530 for (x = 0; x < width; x++){
531 if (y0 + 3 < height && !(
532 (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
533 (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
534 (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
535 (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
539 for (rlen = 0; rlen < 4; rlen++)
540 if (t1->data[(y0+rlen) * t1->stride + x] & mask)
542 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
545 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
546 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
547 for (y = y0 + rlen; y < y0 + 4; y++){
548 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
549 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
551 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
552 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
554 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
555 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
556 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
557 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
560 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
563 for (y = y0; y < y0 + 4 && y < height; y++){
564 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
565 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
566 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
567 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
569 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
570 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
571 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
572 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
575 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
581 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
582 int width, int height, int bandpos, int lev)
584 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
587 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
589 for (y = 0; y < height; y++){
590 for (x = 0; x < width; x++){
591 if (t1->data[(y) * t1->stride + x] < 0){
592 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
593 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
595 max = FFMAX(max, t1->data[(y) * t1->stride + x]);
600 cblk->nonzerobits = 0;
603 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
604 bpno = cblk->nonzerobits - 1;
607 ff_mqc_initenc(&t1->mqc, cblk->data);
609 for (passno = 0; bpno >= 0; passno++){
613 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
615 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
617 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
621 cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
622 wmsedec += (int64_t)nmsedec << (2*bpno);
623 cblk->passes[passno].disto = wmsedec;
630 cblk->npasses = passno;
631 cblk->ninclpasses = passno;
633 cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
636 /* tier-2 routines: */
638 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
645 put_num(s, 0xc | (n-3), 4);
647 put_num(s, 0x1e0 | (n-6), 9);
649 put_num(s, 0xff80 | (n-37), 16);
653 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
654 uint8_t *expn, int numgbits)
656 int bandno, empty = 1;
664 // is the packet empty?
665 for (bandno = 0; bandno < rlevel->nbands; bandno++){
666 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
667 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
673 put_bits(s, !empty, 1);
679 for (bandno = 0; bandno < rlevel->nbands; bandno++){
680 Jpeg2000Band *band = rlevel->band + bandno;
681 Jpeg2000Prec *prec = band->prec + precno;
683 int cblknw = prec->nb_codeblocks_width;
685 if (band->coord[0][0] == band->coord[0][1]
686 || band->coord[1][0] == band->coord[1][1])
689 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
690 for (xi = 0; xi < cblknw; xi++, pos++){
691 prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
692 tag_tree_update(prec->cblkincl + pos);
693 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
694 tag_tree_update(prec->zerobits + pos);
698 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
699 for (xi = 0; xi < cblknw; xi++, pos++){
700 int pad = 0, llen, length;
701 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
703 if (s->buf_end - s->buf < 20) // approximately
706 // inclusion information
707 tag_tree_code(s, prec->cblkincl + pos, 1);
708 if (!cblk->ninclpasses)
710 // zerobits information
711 tag_tree_code(s, prec->zerobits + pos, 100);
713 putnumpasses(s, cblk->ninclpasses);
715 length = cblk->passes[cblk->ninclpasses-1].rate;
716 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
721 // length of code block
722 put_bits(s, 1, llen);
724 put_num(s, length, av_log2(length)+1+pad);
729 for (bandno = 0; bandno < rlevel->nbands; bandno++){
730 Jpeg2000Band *band = rlevel->band + bandno;
731 Jpeg2000Prec *prec = band->prec + precno;
732 int yi, cblknw = prec->nb_codeblocks_width;
733 for (yi =0; yi < prec->nb_codeblocks_height; yi++){
735 for (xi = 0; xi < cblknw; xi++){
736 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
737 if (cblk->ninclpasses){
738 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
740 bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate
741 - cblk->passes[cblk->ninclpasses-1].flushed_len);
742 bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
743 cblk->passes[cblk->ninclpasses-1].flushed_len);
751 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
753 int compno, reslevelno, ret;
754 Jpeg2000CodingStyle *codsty = &s->codsty;
755 Jpeg2000QuantStyle *qntsty = &s->qntsty;
757 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
758 // lay-rlevel-comp-pos progression
759 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
760 for (compno = 0; compno < s->ncomponents; compno++){
762 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
763 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
764 if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
765 qntsty->nguardbits)) < 0)
770 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
774 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
777 for (passno = 0; passno < cblk->npasses; passno++){
781 dr = cblk->passes[passno].rate
782 - (res ? cblk->passes[res-1].rate:0);
783 dd = cblk->passes[passno].disto
784 - (res ? cblk->passes[res-1].disto:0);
786 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
792 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
794 int precno, compno, reslevelno, bandno, cblkno, lev;
795 Jpeg2000CodingStyle *codsty = &s->codsty;
797 for (compno = 0; compno < s->ncomponents; compno++){
798 Jpeg2000Component *comp = tile->comp + compno;
800 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
801 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
803 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
804 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
805 int bandpos = bandno + (reslevelno > 0);
806 Jpeg2000Band *band = reslevel->band + bandno;
807 Jpeg2000Prec *prec = band->prec + precno;
809 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
810 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
812 cblk->ninclpasses = getcut(cblk, s->lambda,
813 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
821 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
823 int compno, reslevelno, bandno, ret;
824 Jpeg2000T1Context t1;
825 Jpeg2000CodingStyle *codsty = &s->codsty;
826 for (compno = 0; compno < s->ncomponents; compno++){
827 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
829 t1.stride = (1<<codsty->log2_cblk_width) + 2;
831 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
832 if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
834 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
836 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
837 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
839 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
840 Jpeg2000Band *band = reslevel->band + bandno;
841 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
842 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
843 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
845 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
846 band->coord[1][1]) - band->coord[1][0] + yy0;
848 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
851 bandpos = bandno + (reslevelno > 0);
853 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
854 if (reslevelno == 0 || bandno == 1)
857 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
859 xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
860 band->coord[0][1]) - band->coord[0][0] + xx0;
862 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
864 if (codsty->transform == FF_DWT53){
865 for (y = yy0; y < yy1; y++){
866 int *ptr = t1.data + (y-yy0)*t1.stride;
867 for (x = xx0; x < xx1; x++){
868 *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
872 for (y = yy0; y < yy1; y++){
873 int *ptr = t1.data + (y-yy0)*t1.stride;
874 for (x = xx0; x < xx1; x++){
875 *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
876 *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
881 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
882 bandpos, codsty->nreslevels - reslevelno - 1);
884 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
887 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
891 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
894 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
895 truncpasses(s, tile);
896 if ((ret = encode_packets(s, tile, tileno)) < 0)
898 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
902 static void cleanup(Jpeg2000EncoderContext *s)
905 Jpeg2000CodingStyle *codsty = &s->codsty;
907 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
908 for (compno = 0; compno < s->ncomponents; compno++){
909 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
910 ff_jpeg2000_cleanup(comp, codsty);
912 av_freep(&s->tile[tileno].comp);
917 static void reinit(Jpeg2000EncoderContext *s)
920 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
921 Jpeg2000Tile *tile = s->tile + tileno;
922 for (compno = 0; compno < s->ncomponents; compno++)
923 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
927 static void update_size(uint8_t *size, const uint8_t *end)
929 AV_WB32(size, end-size);
932 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
933 const AVFrame *pict, int *got_packet)
936 Jpeg2000EncoderContext *s = avctx->priv_data;
937 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
939 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
943 s->buf = s->buf_start = pkt->data;
944 s->buf_end = pkt->data + pkt->size;
948 s->lambda = s->picture->quality * LAMBDA_SCALE;
953 if (s->format == CODEC_JP2) {
954 av_assert0(s->buf == pkt->data);
956 bytestream_put_be32(&s->buf, 0x0000000C);
957 bytestream_put_be32(&s->buf, 0x6A502020);
958 bytestream_put_be32(&s->buf, 0x0D0A870A);
961 bytestream_put_be32(&s->buf, 0);
962 bytestream_put_buffer(&s->buf, "ftyp", 4);
963 bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
964 bytestream_put_be32(&s->buf, 0);
965 update_size(chunkstart, s->buf);
968 bytestream_put_be32(&s->buf, 0);
969 bytestream_put_buffer(&s->buf, "jp2h", 4);
972 bytestream_put_be32(&s->buf, 0);
973 bytestream_put_buffer(&s->buf, "ihdr", 4);
974 bytestream_put_be32(&s->buf, avctx->height);
975 bytestream_put_be32(&s->buf, avctx->width);
976 bytestream_put_be16(&s->buf, s->ncomponents);
977 bytestream_put_byte(&s->buf, s->cbps[0]);
978 bytestream_put_byte(&s->buf, 7);
979 bytestream_put_byte(&s->buf, 0);
980 bytestream_put_byte(&s->buf, 0);
981 update_size(chunkstart, s->buf);
984 bytestream_put_be32(&s->buf, 0);
985 bytestream_put_buffer(&s->buf, "colr", 4);
986 bytestream_put_byte(&s->buf, 1);
987 bytestream_put_byte(&s->buf, 0);
988 bytestream_put_byte(&s->buf, 0);
989 if (s->ncomponents == 1) {
990 bytestream_put_be32(&s->buf, 17);
991 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
992 bytestream_put_be32(&s->buf, 16);
994 bytestream_put_be32(&s->buf, 18);
996 update_size(chunkstart, s->buf);
997 update_size(jp2hstart, s->buf);
1000 bytestream_put_be32(&s->buf, 0);
1001 bytestream_put_buffer(&s->buf, "jp2c", 4);
1004 if (s->buf_end - s->buf < 2)
1006 bytestream_put_be16(&s->buf, JPEG2000_SOC);
1007 if ((ret = put_siz(s)) < 0)
1009 if ((ret = put_cod(s)) < 0)
1011 if ((ret = put_qcd(s, 0)) < 0)
1014 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1016 if (!(psotptr = put_sot(s, tileno)))
1018 if (s->buf_end - s->buf < 2)
1020 bytestream_put_be16(&s->buf, JPEG2000_SOD);
1021 if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1023 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1025 if (s->buf_end - s->buf < 2)
1027 bytestream_put_be16(&s->buf, JPEG2000_EOC);
1029 if (s->format == CODEC_JP2)
1030 update_size(jp2cstart, s->buf);
1032 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1033 pkt->size = s->buf - s->buf_start;
1034 pkt->flags |= AV_PKT_FLAG_KEY;
1040 static av_cold int j2kenc_init(AVCodecContext *avctx)
1043 Jpeg2000EncoderContext *s = avctx->priv_data;
1044 Jpeg2000CodingStyle *codsty = &s->codsty;
1045 Jpeg2000QuantStyle *qntsty = &s->qntsty;
1048 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1051 // TODO: implement setting non-standard precinct size
1052 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1053 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1054 codsty->nreslevels2decode=
1055 codsty->nreslevels = 7;
1056 codsty->log2_cblk_width = 4;
1057 codsty->log2_cblk_height = 4;
1058 codsty->transform = avctx->prediction_method ? FF_DWT53 : FF_DWT97_INT;
1060 qntsty->nguardbits = 1;
1062 s->tile_width = 256;
1063 s->tile_height = 256;
1065 if (codsty->transform == FF_DWT53)
1066 qntsty->quantsty = JPEG2000_QSTY_NONE;
1068 qntsty->quantsty = JPEG2000_QSTY_SE;
1070 s->width = avctx->width;
1071 s->height = avctx->height;
1073 for (i = 0; i < 3; i++)
1076 if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
1078 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
1080 } else{ // planar YUV
1083 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
1084 s->chroma_shift, s->chroma_shift + 1);
1087 ff_jpeg2000_init_tier1_luts();
1088 ff_mqc_init_context_tables();
1091 init_quantization(s);
1092 if ((ret=init_tiles(s)) < 0)
1095 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1100 static int j2kenc_destroy(AVCodecContext *avctx)
1102 Jpeg2000EncoderContext *s = avctx->priv_data;
1108 // taken from the libopenjpeg wraper so it matches
1110 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1111 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1112 static const AVOption options[] = {
1113 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1114 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1115 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1119 static const AVClass j2k_class = {
1120 .class_name = "jpeg 2000 encoder",
1121 .item_name = av_default_item_name,
1123 .version = LIBAVUTIL_VERSION_INT,
1126 AVCodec ff_jpeg2000_encoder = {
1128 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1129 .type = AVMEDIA_TYPE_VIDEO,
1130 .id = AV_CODEC_ID_JPEG2000,
1131 .priv_data_size = sizeof(Jpeg2000EncoderContext),
1132 .init = j2kenc_init,
1133 .encode2 = encode_frame,
1134 .close = j2kenc_destroy,
1135 .capabilities = CODEC_CAP_EXPERIMENTAL,
1136 .pix_fmts = (const enum AVPixelFormat[]) {
1137 AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1138 /* AV_PIX_FMT_YUV420P,
1139 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1140 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,*/
1143 .priv_class = &j2k_class,