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
30 #include "bytestream.h"
32 #include "libavutil/common.h"
34 #define NMSEDEC_BITS 7
35 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
36 #define WMSEDEC_SHIFT 13 ///< must be >= 13
37 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
39 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
40 lut_nmsedec_ref0[1<<NMSEDEC_BITS],
41 lut_nmsedec_sig [1<<NMSEDEC_BITS],
42 lut_nmsedec_sig0[1<<NMSEDEC_BITS];
44 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
45 {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
46 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
47 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
48 {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
50 {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
51 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
52 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
53 { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
61 AVCodecContext *avctx;
64 int width, height; ///< image width and height
65 uint8_t cbps[4]; ///< bits per sample in particular components
69 int tile_width, tile_height; ///< tile size
70 int numXtiles, numYtiles;
79 J2kCodingStyle codsty;
91 static void nspaces(FILE *fd, int n)
93 while(n--) putc(' ', fd);
96 static void printv(int *tab, int l)
99 for (i = 0; i < l; i++)
100 printf("%.3d ", tab[i]);
104 static void printu(uint8_t *tab, int l)
107 for (i = 0; i < l; i++)
108 printf("%.3hd ", tab[i]);
112 static void printcomp(J2kComponent *comp)
115 for (i = 0; i < comp->y1 - comp->y0; i++)
116 printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
119 static void dump(J2kEncoderContext *s, FILE *fd)
121 int tileno, compno, reslevelno, bandno, precno;
122 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
123 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
125 s->width, s->height, s->tile_width, s->tile_height,
126 s->numXtiles, s->numYtiles, s->ncomponents);
127 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
128 J2kTile *tile = s->tile + tileno;
130 fprintf(fd, "tile %d:\n", tileno);
131 for(compno = 0; compno < s->ncomponents; compno++){
132 J2kComponent *comp = tile->comp + compno;
134 fprintf(fd, "component %d:\n", compno);
136 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
137 comp->x0, comp->x1, comp->y0, comp->y1);
138 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
139 J2kResLevel *reslevel = comp->reslevel + reslevelno;
141 fprintf(fd, "reslevel %d:\n", reslevelno);
143 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
144 reslevel->x0, reslevel->x1, reslevel->y0,
145 reslevel->y1, reslevel->nbands);
146 for(bandno = 0; bandno < reslevel->nbands; bandno++){
147 J2kBand *band = reslevel->band + bandno;
149 fprintf(fd, "band %d:\n", bandno);
151 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
152 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
155 band->codeblock_width, band->codeblock_height,
156 band->cblknx, band->cblkny);
157 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
158 J2kPrec *prec = band->prec + precno;
160 fprintf(fd, "prec %d:\n", precno);
162 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
163 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
172 /* bitstream routines */
174 /** put n times val bit */
175 static void put_bits(J2kEncoderContext *s, int val, int n) // TODO: optimize
178 if (s->bit_index == 8)
180 s->bit_index = *s->buf == 0xff;
183 *s->buf |= val << (7 - s->bit_index++);
187 /** put n least significant bits of a number num */
188 static void put_num(J2kEncoderContext *s, int num, int n)
191 put_bits(s, (num >> n) & 1, 1);
194 /** flush the bitstream */
195 static void j2k_flush(J2kEncoderContext *s)
203 /* tag tree routines */
205 /** code the value stored in node */
206 static void tag_tree_code(J2kEncoderContext *s, J2kTgtNode *node, int threshold)
208 J2kTgtNode *stack[30];
209 int sp = 1, curval = 0;
223 if (stack[sp]->val >= threshold){
224 put_bits(s, 0, threshold - curval);
227 put_bits(s, 0, stack[sp]->val - curval);
229 curval = stack[sp]->val;
233 /** update the value in node */
234 static void tag_tree_update(J2kTgtNode *node)
237 while (node->parent){
238 if (node->parent->val <= node->val)
240 node->parent->val = node->val;
246 static int put_siz(J2kEncoderContext *s)
250 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
253 bytestream_put_be16(&s->buf, J2K_SIZ);
254 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
255 bytestream_put_be16(&s->buf, 0); // Rsiz
256 bytestream_put_be32(&s->buf, s->width); // width
257 bytestream_put_be32(&s->buf, s->height); // height
258 bytestream_put_be32(&s->buf, 0); // X0Siz
259 bytestream_put_be32(&s->buf, 0); // Y0Siz
261 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
262 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
263 bytestream_put_be32(&s->buf, 0); // XT0Siz
264 bytestream_put_be32(&s->buf, 0); // YT0Siz
265 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
267 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
268 bytestream_put_byte(&s->buf, 7);
269 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
270 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
275 static int put_cod(J2kEncoderContext *s)
277 J2kCodingStyle *codsty = &s->codsty;
279 if (s->buf_end - s->buf < 14)
282 bytestream_put_be16(&s->buf, J2K_COD);
283 bytestream_put_be16(&s->buf, 12); // Lcod
284 bytestream_put_byte(&s->buf, 0); // Scod
286 bytestream_put_byte(&s->buf, 0); // progression level
287 bytestream_put_be16(&s->buf, 1); // num of layers
288 bytestream_put_byte(&s->buf, 0); // multiple component transformation
290 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
291 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
292 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
293 bytestream_put_byte(&s->buf, 0); // cblk style
294 bytestream_put_byte(&s->buf, codsty->transform); // transformation
298 static int put_qcd(J2kEncoderContext *s, int compno)
301 J2kCodingStyle *codsty = &s->codsty;
302 J2kQuantStyle *qntsty = &s->qntsty;
304 if (qntsty->quantsty == J2K_QSTY_NONE)
305 size = 4 + 3 * (codsty->nreslevels-1);
307 size = 5 + 6 * (codsty->nreslevels-1);
309 if (s->buf_end - s->buf < size + 2)
312 bytestream_put_be16(&s->buf, J2K_QCD);
313 bytestream_put_be16(&s->buf, size); // LQcd
314 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
315 if (qntsty->quantsty == J2K_QSTY_NONE)
316 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
317 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
319 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
320 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
324 static uint8_t *put_sot(J2kEncoderContext *s, int tileno)
328 if (s->buf_end - s->buf < 12)
331 bytestream_put_be16(&s->buf, J2K_SOT);
332 bytestream_put_be16(&s->buf, 10); // Lsot
333 bytestream_put_be16(&s->buf, tileno); // Isot
336 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
338 bytestream_put_byte(&s->buf, 0); // TPsot
339 bytestream_put_byte(&s->buf, 1); // TNsot
344 * compute the sizes of tiles, resolution levels, bands, etc.
345 * allocate memory for them
346 * divide the input image into tile-components
348 static int init_tiles(J2kEncoderContext *s)
350 int tileno, tilex, tiley, compno;
351 J2kCodingStyle *codsty = &s->codsty;
352 J2kQuantStyle *qntsty = &s->qntsty;
354 s->numXtiles = ff_j2k_ceildiv(s->width, s->tile_width);
355 s->numYtiles = ff_j2k_ceildiv(s->height, s->tile_height);
357 s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(J2kTile));
359 return AVERROR(ENOMEM);
360 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
361 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
362 J2kTile *tile = s->tile + tileno;
364 tile->comp = av_malloc(s->ncomponents * sizeof(J2kComponent));
366 return AVERROR(ENOMEM);
367 for (compno = 0; compno < s->ncomponents; compno++){
368 J2kComponent *comp = tile->comp + compno;
371 comp->coord[0][0] = tilex * s->tile_width;
372 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
373 comp->coord[1][0] = tiley * s->tile_height;
374 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
376 for (i = 0; i < 2; i++)
377 for (j = 0; j < 2; j++)
378 comp->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
380 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno]))
387 static void copy_frame(J2kEncoderContext *s)
389 int tileno, compno, i, y, x;
391 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
392 J2kTile *tile = s->tile + tileno;
394 for (compno = 0; compno < s->ncomponents; compno++){
395 J2kComponent *comp = tile->comp + compno;
396 int *dst = comp->data;
397 line = s->picture->data[compno]
398 + comp->coord[1][0] * s->picture->linesize[compno]
400 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
402 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
403 *dst++ = *ptr++ - (1 << 7);
404 line += s->picture->linesize[compno];
408 line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
409 + tile->comp[0].coord[0][0] * s->ncomponents;
412 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
414 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
415 for (compno = 0; compno < s->ncomponents; compno++){
416 tile->comp[compno].data[i] = *ptr++ - (1 << 7);
419 line += s->picture->linesize[0];
425 static void init_quantization(J2kEncoderContext *s)
427 int compno, reslevelno, bandno;
428 J2kQuantStyle *qntsty = &s->qntsty;
429 J2kCodingStyle *codsty = &s->codsty;
431 for (compno = 0; compno < s->ncomponents; compno++){
433 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
434 int nbands, lev = codsty->nreslevels - reslevelno - 1;
435 nbands = reslevelno ? 3 : 1;
436 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
439 if (codsty->transform == FF_DWT97){
440 int bandpos = bandno + (reslevelno>0),
441 ss = 81920000 / dwt_norms[0][bandpos][lev],
443 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
444 expn = s->cbps[compno] - log + 13;
446 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
448 qntsty->expn[gbandno] = expn;
449 qntsty->mant[gbandno] = mant;
455 static void init_luts()
458 mask = ~((1<<NMSEDEC_FRACBITS)-1);
460 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
461 lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
462 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
464 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
465 lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
466 << 13-NMSEDEC_FRACBITS, 0);
467 lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
472 /* tier-1 routines */
473 static int getnmsedec_sig(int x, int bpno)
475 if (bpno > NMSEDEC_FRACBITS)
476 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
477 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
480 static int getnmsedec_ref(int x, int bpno)
482 if (bpno > NMSEDEC_FRACBITS)
483 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
484 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
487 static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
489 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
490 for (y0 = 0; y0 < height; y0 += 4)
491 for (x = 0; x < width; x++)
492 for (y = y0; y < height && y < y0+4; y++){
493 if (!(t1->flags[y+1][x+1] & J2K_T1_SIG) && (t1->flags[y+1][x+1] & J2K_T1_SIG_NB)){
494 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno),
495 bit = t1->data[y][x] & mask ? 1 : 0;
496 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
499 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
500 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
501 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
502 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
504 t1->flags[y+1][x+1] |= J2K_T1_VIS;
509 static void encode_refpass(J2kT1Context *t1, int width, int height, 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][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
516 int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
517 *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
518 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
519 t1->flags[y+1][x+1] |= J2K_T1_REF;
523 static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
525 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
526 for (y0 = 0; y0 < height; y0 += 4)
527 for (x = 0; x < width; x++){
528 if (y0 + 3 < height && !(
529 (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
530 (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
531 (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
532 (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG))))
536 for (rlen = 0; rlen < 4; rlen++)
537 if (t1->data[y0+rlen][x] & mask)
539 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
542 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
543 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
544 for (y = y0 + rlen; y < y0 + 4; y++){
545 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
546 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno);
548 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
549 if (t1->data[y][x] & mask){ // newly significant
551 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
552 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
553 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
554 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
557 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
560 for (y = y0; y < y0 + 4 && y < height; y++){
561 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
562 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno);
563 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
564 if (t1->data[y][x] & mask){ // newly significant
566 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
567 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
568 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
569 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
572 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
578 static void encode_cblk(J2kEncoderContext *s, J2kT1Context *t1, J2kCblk *cblk, J2kTile *tile,
579 int width, int height, int bandpos, int lev)
581 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
584 for (y = 0; y < height+2; y++)
585 memset(t1->flags[y], 0, (width+2)*sizeof(int));
587 for (y = 0; y < height; y++){
588 for (x = 0; x < width; x++){
589 if (t1->data[y][x] < 0){
590 t1->flags[y+1][x+1] |= J2K_T1_SGN;
591 t1->data[y][x] = -t1->data[y][x];
593 max = FFMAX(max, t1->data[y][x]);
598 cblk->nonzerobits = 0;
601 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
602 bpno = cblk->nonzerobits - 1;
605 ff_mqc_initenc(&t1->mqc, cblk->data);
607 for (passno = 0; bpno >= 0; passno++){
611 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
613 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
615 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
619 cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
620 wmsedec += (int64_t)nmsedec << (2*bpno);
621 cblk->passes[passno].disto = wmsedec;
628 cblk->npasses = passno;
629 cblk->ninclpasses = passno;
631 // TODO: optional flush on each pass
632 cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
635 /* tier-2 routines: */
637 static void putnumpasses(J2kEncoderContext *s, int n)
644 put_num(s, 0xc | (n-3), 4);
646 put_num(s, 0x1e0 | (n-6), 9);
648 put_num(s, 0xff80 | (n-37), 16);
652 static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno,
653 uint8_t *expn, int numgbits)
655 int bandno, empty = 1;
663 // is the packet empty?
664 for (bandno = 0; bandno < rlevel->nbands; bandno++){
665 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
666 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
672 put_bits(s, !empty, 1);
678 for (bandno = 0; bandno < rlevel->nbands; bandno++){
679 J2kBand *band = rlevel->band + bandno;
680 J2kPrec *prec = band->prec + precno;
682 int cblknw = prec->xi1 - prec->xi0;
684 if (band->coord[0][0] == band->coord[0][1]
685 || band->coord[1][0] == band->coord[1][1])
688 for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
689 for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
690 prec->cblkincl[pos].val = band->cblk[yi * cblknw + xi].ninclpasses == 0;
691 tag_tree_update(prec->cblkincl + pos);
692 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - band->cblk[yi * cblknw + xi].nonzerobits;
693 tag_tree_update(prec->zerobits + pos);
697 for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
698 for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
699 int pad = 0, llen, length;
700 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
702 if (s->buf_end - s->buf < 20) // approximately
705 // inclusion information
706 tag_tree_code(s, prec->cblkincl + pos, 1);
707 if (!cblk->ninclpasses)
709 // zerobits information
710 tag_tree_code(s, prec->zerobits + pos, 100);
712 putnumpasses(s, cblk->ninclpasses);
714 length = cblk->passes[cblk->ninclpasses-1].rate;
715 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
720 // length of code block
721 put_bits(s, 1, llen);
723 put_num(s, length, av_log2(length)+1+pad);
728 for (bandno = 0; bandno < rlevel->nbands; bandno++){
729 J2kBand *band = rlevel->band + bandno;
730 J2kPrec *prec = band->prec + precno;
731 int yi, cblknw = prec->xi1 - prec->xi0;
732 for (yi = prec->yi0; yi < prec->yi1; yi++){
734 for (xi = prec->xi0; xi < prec->xi1; xi++){
735 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
736 if (cblk->ninclpasses){
737 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
739 bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
747 static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
749 int compno, reslevelno, ret;
750 J2kCodingStyle *codsty = &s->codsty;
751 J2kQuantStyle *qntsty = &s->qntsty;
753 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
754 // lay-rlevel-comp-pos progression
755 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
756 for (compno = 0; compno < s->ncomponents; compno++){
758 J2kResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
759 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
760 if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
766 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
770 static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
773 for (passno = 0; passno < cblk->npasses; passno++){
777 dr = cblk->passes[passno].rate
778 - (res ? cblk->passes[res-1].rate:0);
779 dd = cblk->passes[passno].disto
780 - (res ? cblk->passes[res-1].disto:0);
782 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
788 static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
790 int compno, reslevelno, bandno, cblkno, lev;
791 J2kCodingStyle *codsty = &s->codsty;
793 for (compno = 0; compno < s->ncomponents; compno++){
794 J2kComponent *comp = tile->comp + compno;
796 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
797 J2kResLevel *reslevel = comp->reslevel + reslevelno;
799 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
800 int bandpos = bandno + (reslevelno > 0);
801 J2kBand *band = reslevel->band + bandno;
803 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
804 J2kCblk *cblk = band->cblk + cblkno;
806 cblk->ninclpasses = getcut(cblk, s->lambda,
807 (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 13);
814 static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
816 int compno, reslevelno, bandno, ret;
818 J2kCodingStyle *codsty = &s->codsty;
819 for (compno = 0; compno < s->ncomponents; compno++){
820 J2kComponent *comp = s->tile[tileno].comp + compno;
822 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
823 if (ret = ff_dwt_encode(&comp->dwt, comp->data))
825 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
827 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
828 J2kResLevel *reslevel = comp->reslevel + reslevelno;
830 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
831 J2kBand *band = reslevel->band + bandno;
832 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
833 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
835 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
836 band->coord[1][1]) - band->coord[1][0] + yy0;
838 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
841 bandpos = bandno + (reslevelno > 0);
843 for (cblky = 0; cblky < band->cblkny; cblky++){
844 if (reslevelno == 0 || bandno == 1)
847 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
849 xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
850 band->coord[0][1]) - band->coord[0][0] + xx0;
852 for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
854 if (codsty->transform == FF_DWT53){
855 for (y = yy0; y < yy1; y++){
856 int *ptr = t1.data[y-yy0];
857 for (x = xx0; x < xx1; x++){
858 *ptr++ = comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
862 for (y = yy0; y < yy1; y++){
863 int *ptr = t1.data[y-yy0];
864 for (x = xx0; x < xx1; x++){
865 *ptr = (comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
866 *ptr++ = (int64_t)*ptr * (int64_t)(8192 * 8192 / band->stepsize) >> 13 - NMSEDEC_FRACBITS;
870 encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
871 bandpos, codsty->nreslevels - reslevelno - 1);
873 xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
876 yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
880 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
883 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
884 truncpasses(s, tile);
885 if (ret = encode_packets(s, tile, tileno))
887 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
891 void cleanup(J2kEncoderContext *s)
894 J2kCodingStyle *codsty = &s->codsty;
896 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
897 for (compno = 0; compno < s->ncomponents; compno++){
898 J2kComponent *comp = s->tile[tileno].comp + compno;
899 ff_j2k_cleanup(comp, codsty);
901 av_freep(&s->tile[tileno].comp);
906 static void reinit(J2kEncoderContext *s)
909 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
910 J2kTile *tile = s->tile + tileno;
911 for (compno = 0; compno < s->ncomponents; compno++)
912 ff_j2k_reinit(tile->comp + compno, &s->codsty);
916 static int encode_frame(AVCodecContext *avctx,
917 uint8_t *buf, int buf_size,
921 J2kEncoderContext *s = avctx->priv_data;
924 s->buf = s->buf_start = buf;
925 s->buf_end = buf + buf_size;
929 s->lambda = s->picture->quality * LAMBDA_SCALE;
934 if (s->buf_end - s->buf < 2)
936 bytestream_put_be16(&s->buf, J2K_SOC);
937 if (ret = put_siz(s))
939 if (ret = put_cod(s))
941 if (ret = put_qcd(s, 0))
944 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
946 if ((psotptr = put_sot(s, tileno)) < 0)
948 if (s->buf_end - s->buf < 2)
950 bytestream_put_be16(&s->buf, J2K_SOD);
951 if (ret = encode_tile(s, s->tile + tileno, tileno))
953 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
955 if (s->buf_end - s->buf < 2)
957 bytestream_put_be16(&s->buf, J2K_EOC);
959 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
960 return s->buf - s->buf_start;
963 static av_cold int j2kenc_init(AVCodecContext *avctx)
966 J2kEncoderContext *s = avctx->priv_data;
967 J2kCodingStyle *codsty = &s->codsty;
968 J2kQuantStyle *qntsty = &s->qntsty;
971 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
974 // TODO: implement setting non-standard precinct size
975 codsty->log2_prec_width = 15;
976 codsty->log2_prec_height = 15;
977 codsty->nreslevels = 7;
978 codsty->log2_cblk_width = 4;
979 codsty->log2_cblk_height = 4;
980 codsty->transform = 1;
982 qntsty->nguardbits = 1;
985 s->tile_height = 256;
987 if (codsty->transform == FF_DWT53)
988 qntsty->quantsty = J2K_QSTY_NONE;
990 qntsty->quantsty = J2K_QSTY_SE;
992 s->width = avctx->width;
993 s->height = avctx->height;
995 for (i = 0; i < 3; i++)
998 if (avctx->pix_fmt == PIX_FMT_RGB24){
1000 } else if (avctx->pix_fmt == PIX_FMT_GRAY8){
1002 } else{ // planar YUV
1005 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
1006 s->chroma_shift, s->chroma_shift + 1);
1009 ff_j2k_init_tier1_luts();
1013 init_quantization(s);
1014 if (ret=init_tiles(s))
1017 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1022 static int j2kenc_destroy(AVCodecContext *avctx)
1024 J2kEncoderContext *s = avctx->priv_data;
1030 AVCodec jpeg2000_encoder = {
1034 sizeof(J2kEncoderContext),
1041 (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_RGB24,
1042 PIX_FMT_YUV422P, PIX_FMT_YUV444P,
1043 PIX_FMT_YUV410P, PIX_FMT_YUV411P,