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 if(s->avctx->pix_fmt == PIX_FMT_YUV444P){
289 bytestream_put_byte(&s->buf, 2); // ICT
291 bytestream_put_byte(&s->buf, 0); // unspecified
294 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
295 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
296 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
297 bytestream_put_byte(&s->buf, 0); // cblk style
298 bytestream_put_byte(&s->buf, codsty->transform); // transformation
302 static int put_qcd(J2kEncoderContext *s, int compno)
305 J2kCodingStyle *codsty = &s->codsty;
306 J2kQuantStyle *qntsty = &s->qntsty;
308 if (qntsty->quantsty == J2K_QSTY_NONE)
309 size = 4 + 3 * (codsty->nreslevels-1);
311 size = 5 + 6 * (codsty->nreslevels-1);
313 if (s->buf_end - s->buf < size + 2)
316 bytestream_put_be16(&s->buf, J2K_QCD);
317 bytestream_put_be16(&s->buf, size); // LQcd
318 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
319 if (qntsty->quantsty == J2K_QSTY_NONE)
320 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
321 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
323 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
324 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
328 static uint8_t *put_sot(J2kEncoderContext *s, int tileno)
332 if (s->buf_end - s->buf < 12)
335 bytestream_put_be16(&s->buf, J2K_SOT);
336 bytestream_put_be16(&s->buf, 10); // Lsot
337 bytestream_put_be16(&s->buf, tileno); // Isot
340 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
342 bytestream_put_byte(&s->buf, 0); // TPsot
343 bytestream_put_byte(&s->buf, 1); // TNsot
348 * compute the sizes of tiles, resolution levels, bands, etc.
349 * allocate memory for them
350 * divide the input image into tile-components
352 static int init_tiles(J2kEncoderContext *s)
354 int tileno, tilex, tiley, compno;
355 J2kCodingStyle *codsty = &s->codsty;
356 J2kQuantStyle *qntsty = &s->qntsty;
358 s->numXtiles = ff_j2k_ceildiv(s->width, s->tile_width);
359 s->numYtiles = ff_j2k_ceildiv(s->height, s->tile_height);
361 s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(J2kTile));
363 return AVERROR(ENOMEM);
364 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
365 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
366 J2kTile *tile = s->tile + tileno;
368 tile->comp = av_malloc(s->ncomponents * sizeof(J2kComponent));
370 return AVERROR(ENOMEM);
371 for (compno = 0; compno < s->ncomponents; compno++){
372 J2kComponent *comp = tile->comp + compno;
375 comp->coord[0][0] = tilex * s->tile_width;
376 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
377 comp->coord[1][0] = tiley * s->tile_height;
378 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
380 for (i = 0; i < 2; i++)
381 for (j = 0; j < 2; j++)
382 comp->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
384 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], compno?1<<s->chroma_shift[0]:1, compno?1<<s->chroma_shift[1]:1))
391 static void copy_frame(J2kEncoderContext *s)
393 int tileno, compno, i, y, x;
395 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
396 J2kTile *tile = s->tile + tileno;
398 for (compno = 0; compno < s->ncomponents; compno++){
399 J2kComponent *comp = tile->comp + compno;
400 int *dst = comp->data;
401 line = s->picture.data[compno]
402 + comp->coord[1][0] * s->picture.linesize[compno]
404 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
406 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
407 *dst++ = *ptr++ - (1 << 7);
408 line += s->picture.linesize[compno];
412 line = s->picture.data[0] + tile->comp[0].coord[1][0] * s->picture.linesize[0]
413 + tile->comp[0].coord[0][0] * s->ncomponents;
416 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
418 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
419 for (compno = 0; compno < s->ncomponents; compno++){
420 tile->comp[compno].data[i] = *ptr++ - (1 << 7);
423 line += s->picture.linesize[0];
429 static void init_quantization(J2kEncoderContext *s)
431 int compno, reslevelno, bandno;
432 J2kQuantStyle *qntsty = &s->qntsty;
433 J2kCodingStyle *codsty = &s->codsty;
435 for (compno = 0; compno < s->ncomponents; compno++){
437 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
438 int nbands, lev = codsty->nreslevels - reslevelno - 1;
439 nbands = reslevelno ? 3 : 1;
440 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
443 if (codsty->transform == FF_DWT97){
444 int bandpos = bandno + (reslevelno>0),
445 ss = 81920000 / dwt_norms[0][bandpos][lev],
447 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
448 expn = s->cbps[compno] - log + 13;
450 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
452 qntsty->expn[gbandno] = expn;
453 qntsty->mant[gbandno] = mant;
459 static void init_luts()
462 mask = ~((1<<NMSEDEC_FRACBITS)-1);
464 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
465 lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
466 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
468 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
469 lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
470 << 13-NMSEDEC_FRACBITS, 0);
471 lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
476 /* tier-1 routines */
477 static int getnmsedec_sig(int x, int bpno)
479 if (bpno > NMSEDEC_FRACBITS)
480 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
481 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
484 static int getnmsedec_ref(int x, int bpno)
486 if (bpno > NMSEDEC_FRACBITS)
487 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
488 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
491 static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
493 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
494 int vert_causal_ctx_csty_loc_symbol;
495 for (y0 = 0; y0 < height; y0 += 4)
496 for (x = 0; x < width; x++)
497 for (y = y0; y < height && y < y0+4; y++){
498 if (!(t1->flags[y+1][x+1] & J2K_T1_SIG) && (t1->flags[y+1][x+1] & J2K_T1_SIG_NB)){
499 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol),
500 bit = t1->data[y][x] & mask ? 1 : 0;
501 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
504 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
505 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
506 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
507 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
509 t1->flags[y+1][x+1] |= J2K_T1_VIS;
514 static void encode_refpass(J2kT1Context *t1, int width, int height, int *nmsedec, int bpno)
516 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
517 for (y0 = 0; y0 < height; y0 += 4)
518 for (x = 0; x < width; x++)
519 for (y = y0; y < height && y < y0+4; y++)
520 if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
521 int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
522 *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
523 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
524 t1->flags[y+1][x+1] |= J2K_T1_REF;
528 static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
530 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
531 int vert_causal_ctx_csty_loc_symbol;
532 for (y0 = 0; y0 < height; y0 += 4)
533 for (x = 0; x < width; x++){
534 if (y0 + 3 < height && !(
535 (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
536 (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
537 (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
538 (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG))))
542 for (rlen = 0; rlen < 4; rlen++)
543 if (t1->data[y0+rlen][x] & mask)
545 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
548 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
549 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
550 for (y = y0 + rlen; y < y0 + 4; y++){
551 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
552 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
554 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
555 if (t1->data[y][x] & mask){ // newly significant
557 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
558 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
559 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
560 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
563 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
566 for (y = y0; y < y0 + 4 && y < height; y++){
567 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
568 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
569 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
570 if (t1->data[y][x] & mask){ // newly significant
572 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
573 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
574 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
575 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
578 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
584 static void encode_cblk(J2kEncoderContext *s, J2kT1Context *t1, J2kCblk *cblk, J2kTile *tile,
585 int width, int height, int bandpos, int lev)
587 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
590 for (y = 0; y < height+2; y++)
591 memset(t1->flags[y], 0, (width+2)*sizeof(int));
593 for (y = 0; y < height; y++){
594 for (x = 0; x < width; x++){
595 if (t1->data[y][x] < 0){
596 t1->flags[y+1][x+1] |= J2K_T1_SGN;
597 t1->data[y][x] = -t1->data[y][x];
599 max = FFMAX(max, t1->data[y][x]);
604 cblk->nonzerobits = 0;
607 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
608 bpno = cblk->nonzerobits - 1;
611 ff_mqc_initenc(&t1->mqc, cblk->data);
613 for (passno = 0; bpno >= 0; passno++){
617 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
619 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
621 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
625 cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
626 wmsedec += (int64_t)nmsedec << (2*bpno);
627 cblk->passes[passno].disto = wmsedec;
634 cblk->npasses = passno;
635 cblk->ninclpasses = passno;
637 // TODO: optional flush on each pass
638 cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
641 /* tier-2 routines: */
643 static void putnumpasses(J2kEncoderContext *s, int n)
650 put_num(s, 0xc | (n-3), 4);
652 put_num(s, 0x1e0 | (n-6), 9);
654 put_num(s, 0xff80 | (n-37), 16);
658 static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno,
659 uint8_t *expn, int numgbits)
661 int bandno, empty = 1;
669 // is the packet empty?
670 for (bandno = 0; bandno < rlevel->nbands; bandno++){
671 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
672 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
678 put_bits(s, !empty, 1);
684 for (bandno = 0; bandno < rlevel->nbands; bandno++){
685 J2kBand *band = rlevel->band + bandno;
686 J2kPrec *prec = band->prec + precno;
688 int cblknw = prec->xi1 - prec->xi0;
690 if (band->coord[0][0] == band->coord[0][1]
691 || band->coord[1][0] == band->coord[1][1])
694 for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
695 for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
696 prec->cblkincl[pos].val = band->cblk[yi * cblknw + xi].ninclpasses == 0;
697 tag_tree_update(prec->cblkincl + pos);
698 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - band->cblk[yi * cblknw + xi].nonzerobits;
699 tag_tree_update(prec->zerobits + pos);
703 for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
704 for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
705 int pad = 0, llen, length;
706 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
708 if (s->buf_end - s->buf < 20) // approximately
711 // inclusion information
712 tag_tree_code(s, prec->cblkincl + pos, 1);
713 if (!cblk->ninclpasses)
715 // zerobits information
716 tag_tree_code(s, prec->zerobits + pos, 100);
718 putnumpasses(s, cblk->ninclpasses);
720 length = cblk->passes[cblk->ninclpasses-1].rate;
721 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
726 // length of code block
727 put_bits(s, 1, llen);
729 put_num(s, length, av_log2(length)+1+pad);
734 for (bandno = 0; bandno < rlevel->nbands; bandno++){
735 J2kBand *band = rlevel->band + bandno;
736 J2kPrec *prec = band->prec + precno;
737 int yi, cblknw = prec->xi1 - prec->xi0;
738 for (yi = prec->yi0; yi < prec->yi1; yi++){
740 for (xi = prec->xi0; xi < prec->xi1; xi++){
741 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
742 if (cblk->ninclpasses){
743 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
745 bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
753 static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
755 int compno, reslevelno, ret;
756 J2kCodingStyle *codsty = &s->codsty;
757 J2kQuantStyle *qntsty = &s->qntsty;
759 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
760 // lay-rlevel-comp-pos progression
761 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
762 for (compno = 0; compno < s->ncomponents; compno++){
764 J2kResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
765 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
766 if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
772 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
776 static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
779 for (passno = 0; passno < cblk->npasses; passno++){
783 dr = cblk->passes[passno].rate
784 - (res ? cblk->passes[res-1].rate:0);
785 dd = cblk->passes[passno].disto
786 - (res ? cblk->passes[res-1].disto:0);
788 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
794 static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
796 int compno, reslevelno, bandno, cblkno, lev;
797 J2kCodingStyle *codsty = &s->codsty;
799 for (compno = 0; compno < s->ncomponents; compno++){
800 J2kComponent *comp = tile->comp + compno;
802 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
803 J2kResLevel *reslevel = comp->reslevel + reslevelno;
805 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
806 int bandpos = bandno + (reslevelno > 0);
807 J2kBand *band = reslevel->band + bandno;
809 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
810 J2kCblk *cblk = band->cblk + cblkno;
812 cblk->ninclpasses = getcut(cblk, s->lambda,
813 (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 13);
820 static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
822 int compno, reslevelno, bandno, ret;
824 J2kCodingStyle *codsty = &s->codsty;
825 for (compno = 0; compno < s->ncomponents; compno++){
826 J2kComponent *comp = s->tile[tileno].comp + compno;
828 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
829 if (ret = ff_j2k_dwt_encode(&comp->dwt, comp->data))
831 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
833 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
834 J2kResLevel *reslevel = comp->reslevel + reslevelno;
836 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
837 J2kBand *band = reslevel->band + bandno;
838 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
839 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
841 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
842 band->coord[1][1]) - band->coord[1][0] + yy0;
844 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
847 bandpos = bandno + (reslevelno > 0);
849 for (cblky = 0; cblky < band->cblkny; cblky++){
850 if (reslevelno == 0 || bandno == 1)
853 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
855 xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
856 band->coord[0][1]) - band->coord[0][0] + xx0;
858 for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
860 if (codsty->transform == FF_DWT53){
861 for (y = yy0; y < yy1; y++){
862 int *ptr = t1.data[y-yy0];
863 for (x = xx0; x < xx1; x++){
864 *ptr++ = comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
868 for (y = yy0; y < yy1; y++){
869 int *ptr = t1.data[y-yy0];
870 for (x = xx0; x < xx1; x++){
871 *ptr = (comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
872 *ptr = (int64_t)*ptr * (int64_t)(8192 * 8192 / band->stepsize) >> 13 - NMSEDEC_FRACBITS;
877 encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
878 bandpos, codsty->nreslevels - reslevelno - 1);
880 xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
883 yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
887 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
890 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
891 truncpasses(s, tile);
892 if (ret = encode_packets(s, tile, tileno))
894 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
898 static void cleanup(J2kEncoderContext *s)
901 J2kCodingStyle *codsty = &s->codsty;
903 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
904 for (compno = 0; compno < s->ncomponents; compno++){
905 J2kComponent *comp = s->tile[tileno].comp + compno;
906 ff_j2k_cleanup(comp, codsty);
908 av_freep(&s->tile[tileno].comp);
913 static void reinit(J2kEncoderContext *s)
916 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
917 J2kTile *tile = s->tile + tileno;
918 for (compno = 0; compno < s->ncomponents; compno++)
919 ff_j2k_reinit(tile->comp + compno, &s->codsty);
923 static int encode_frame(AVCodecContext *avctx,
924 uint8_t *buf, int buf_size,
928 J2kEncoderContext *s = avctx->priv_data;
931 s->buf = s->buf_start = buf;
932 s->buf_end = buf + buf_size;
934 s->picture = *(AVFrame*)data;
935 avctx->coded_frame= &s->picture;
937 s->lambda = s->picture.quality * LAMBDA_SCALE;
942 if (s->buf_end - s->buf < 2)
944 bytestream_put_be16(&s->buf, J2K_SOC);
945 if (ret = put_siz(s))
947 if (ret = put_cod(s))
949 if (ret = put_qcd(s, 0))
952 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
954 if (!(psotptr = put_sot(s, tileno)))
956 if (s->buf_end - s->buf < 2)
958 bytestream_put_be16(&s->buf, J2K_SOD);
959 if (ret = encode_tile(s, s->tile + tileno, tileno))
961 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
963 if (s->buf_end - s->buf < 2)
965 bytestream_put_be16(&s->buf, J2K_EOC);
967 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
968 return s->buf - s->buf_start;
971 static av_cold int j2kenc_init(AVCodecContext *avctx)
974 J2kEncoderContext *s = avctx->priv_data;
975 J2kCodingStyle *codsty = &s->codsty;
976 J2kQuantStyle *qntsty = &s->qntsty;
979 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
982 // TODO: implement setting non-standard precinct size
983 codsty->log2_prec_width = 15;
984 codsty->log2_prec_height = 15;
985 codsty->nreslevels = 7;
986 codsty->log2_cblk_width = 4;
987 codsty->log2_cblk_height = 4;
988 codsty->transform = 1;
990 qntsty->nguardbits = 1;
993 s->tile_height = 256;
995 if (codsty->transform == FF_DWT53)
996 qntsty->quantsty = J2K_QSTY_NONE;
998 qntsty->quantsty = J2K_QSTY_SE;
1000 s->width = avctx->width;
1001 s->height = avctx->height;
1003 for (i = 0; i < 3; i++)
1006 if (avctx->pix_fmt == PIX_FMT_RGB24){
1008 } else if (avctx->pix_fmt == PIX_FMT_GRAY8){
1010 } else{ // planar YUV
1013 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
1014 s->chroma_shift, s->chroma_shift + 1);
1017 ff_j2k_init_tier1_luts();
1021 init_quantization(s);
1022 if (ret=init_tiles(s))
1025 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1030 static int j2kenc_destroy(AVCodecContext *avctx)
1032 J2kEncoderContext *s = avctx->priv_data;
1038 AVCodec ff_jpeg2000_encoder = {
1040 .type = AVMEDIA_TYPE_VIDEO,
1041 .id = CODEC_ID_JPEG2000,
1042 .priv_data_size = sizeof(J2kEncoderContext),
1043 .init = j2kenc_init,
1044 .encode = encode_frame,
1045 .close = j2kenc_destroy,
1046 .capabilities= CODEC_CAP_EXPERIMENTAL,
1047 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1049 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_YUV444P, PIX_FMT_GRAY8,
1051 PIX_FMT_YUV422P, PIX_FMT_YUV444P,
1052 PIX_FMT_YUV410P, PIX_FMT_YUV411P,*/