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"
35 #define NMSEDEC_BITS 7
36 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
37 #define WMSEDEC_SHIFT 13 ///< must be >= 13
38 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
40 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
41 lut_nmsedec_ref0[1<<NMSEDEC_BITS],
42 lut_nmsedec_sig [1<<NMSEDEC_BITS],
43 lut_nmsedec_sig0[1<<NMSEDEC_BITS];
45 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
46 {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
47 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
48 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
49 {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
51 {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
52 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
53 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
54 { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
62 AVCodecContext *avctx;
65 int width, height; ///< image width and height
66 uint8_t cbps[4]; ///< bits per sample in particular components
70 int tile_width, tile_height; ///< tile size
71 int numXtiles, numYtiles;
80 J2kCodingStyle codsty;
92 static void nspaces(FILE *fd, int n)
94 while(n--) putc(' ', fd);
97 static void printv(int *tab, int l)
100 for (i = 0; i < l; i++)
101 printf("%.3d ", tab[i]);
105 static void printu(uint8_t *tab, int l)
108 for (i = 0; i < l; i++)
109 printf("%.3hd ", tab[i]);
113 static void printcomp(J2kComponent *comp)
116 for (i = 0; i < comp->y1 - comp->y0; i++)
117 printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
120 static void dump(J2kEncoderContext *s, FILE *fd)
122 int tileno, compno, reslevelno, bandno, precno;
123 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
124 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
126 s->width, s->height, s->tile_width, s->tile_height,
127 s->numXtiles, s->numYtiles, s->ncomponents);
128 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
129 J2kTile *tile = s->tile + tileno;
131 fprintf(fd, "tile %d:\n", tileno);
132 for(compno = 0; compno < s->ncomponents; compno++){
133 J2kComponent *comp = tile->comp + compno;
135 fprintf(fd, "component %d:\n", compno);
137 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
138 comp->x0, comp->x1, comp->y0, comp->y1);
139 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
140 J2kResLevel *reslevel = comp->reslevel + reslevelno;
142 fprintf(fd, "reslevel %d:\n", reslevelno);
144 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
145 reslevel->x0, reslevel->x1, reslevel->y0,
146 reslevel->y1, reslevel->nbands);
147 for(bandno = 0; bandno < reslevel->nbands; bandno++){
148 J2kBand *band = reslevel->band + bandno;
150 fprintf(fd, "band %d:\n", bandno);
152 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
153 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
156 band->codeblock_width, band->codeblock_height,
157 band->cblknx, band->cblkny);
158 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
159 J2kPrec *prec = band->prec + precno;
161 fprintf(fd, "prec %d:\n", precno);
163 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
164 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
173 /* bitstream routines */
175 /** put n times val bit */
176 static void put_bits(J2kEncoderContext *s, int val, int n) // TODO: optimize
179 if (s->bit_index == 8)
181 s->bit_index = *s->buf == 0xff;
184 *s->buf |= val << (7 - s->bit_index++);
188 /** put n least significant bits of a number num */
189 static void put_num(J2kEncoderContext *s, int num, int n)
192 put_bits(s, (num >> n) & 1, 1);
195 /** flush the bitstream */
196 static void j2k_flush(J2kEncoderContext *s)
204 /* tag tree routines */
206 /** code the value stored in node */
207 static void tag_tree_code(J2kEncoderContext *s, J2kTgtNode *node, int threshold)
209 J2kTgtNode *stack[30];
210 int sp = 1, curval = 0;
224 if (stack[sp]->val >= threshold){
225 put_bits(s, 0, threshold - curval);
228 put_bits(s, 0, stack[sp]->val - curval);
230 curval = stack[sp]->val;
234 /** update the value in node */
235 static void tag_tree_update(J2kTgtNode *node)
238 while (node->parent){
239 if (node->parent->val <= node->val)
241 node->parent->val = node->val;
247 static int put_siz(J2kEncoderContext *s)
251 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
254 bytestream_put_be16(&s->buf, J2K_SIZ);
255 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
256 bytestream_put_be16(&s->buf, 0); // Rsiz
257 bytestream_put_be32(&s->buf, s->width); // width
258 bytestream_put_be32(&s->buf, s->height); // height
259 bytestream_put_be32(&s->buf, 0); // X0Siz
260 bytestream_put_be32(&s->buf, 0); // Y0Siz
262 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
263 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
264 bytestream_put_be32(&s->buf, 0); // XT0Siz
265 bytestream_put_be32(&s->buf, 0); // YT0Siz
266 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
268 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
269 bytestream_put_byte(&s->buf, 7);
270 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
271 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
276 static int put_cod(J2kEncoderContext *s)
278 J2kCodingStyle *codsty = &s->codsty;
280 if (s->buf_end - s->buf < 14)
283 bytestream_put_be16(&s->buf, J2K_COD);
284 bytestream_put_be16(&s->buf, 12); // Lcod
285 bytestream_put_byte(&s->buf, 0); // Scod
287 bytestream_put_byte(&s->buf, 0); // progression level
288 bytestream_put_be16(&s->buf, 1); // num of layers
289 if(s->avctx->pix_fmt == PIX_FMT_YUV444P){
290 bytestream_put_byte(&s->buf, 2); // ICT
292 bytestream_put_byte(&s->buf, 0); // unspecified
295 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
296 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
297 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
298 bytestream_put_byte(&s->buf, 0); // cblk style
299 bytestream_put_byte(&s->buf, codsty->transform); // transformation
303 static int put_qcd(J2kEncoderContext *s, int compno)
306 J2kCodingStyle *codsty = &s->codsty;
307 J2kQuantStyle *qntsty = &s->qntsty;
309 if (qntsty->quantsty == J2K_QSTY_NONE)
310 size = 4 + 3 * (codsty->nreslevels-1);
312 size = 5 + 6 * (codsty->nreslevels-1);
314 if (s->buf_end - s->buf < size + 2)
317 bytestream_put_be16(&s->buf, J2K_QCD);
318 bytestream_put_be16(&s->buf, size); // LQcd
319 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
320 if (qntsty->quantsty == J2K_QSTY_NONE)
321 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
322 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
324 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
325 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
329 static uint8_t *put_sot(J2kEncoderContext *s, int tileno)
333 if (s->buf_end - s->buf < 12)
336 bytestream_put_be16(&s->buf, J2K_SOT);
337 bytestream_put_be16(&s->buf, 10); // Lsot
338 bytestream_put_be16(&s->buf, tileno); // Isot
341 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
343 bytestream_put_byte(&s->buf, 0); // TPsot
344 bytestream_put_byte(&s->buf, 1); // TNsot
349 * compute the sizes of tiles, resolution levels, bands, etc.
350 * allocate memory for them
351 * divide the input image into tile-components
353 static int init_tiles(J2kEncoderContext *s)
355 int tileno, tilex, tiley, compno;
356 J2kCodingStyle *codsty = &s->codsty;
357 J2kQuantStyle *qntsty = &s->qntsty;
359 s->numXtiles = ff_j2k_ceildiv(s->width, s->tile_width);
360 s->numYtiles = ff_j2k_ceildiv(s->height, s->tile_height);
362 s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(J2kTile));
364 return AVERROR(ENOMEM);
365 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
366 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
367 J2kTile *tile = s->tile + tileno;
369 tile->comp = av_malloc(s->ncomponents * sizeof(J2kComponent));
371 return AVERROR(ENOMEM);
372 for (compno = 0; compno < s->ncomponents; compno++){
373 J2kComponent *comp = tile->comp + compno;
376 comp->coord[0][0] = tilex * s->tile_width;
377 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
378 comp->coord[1][0] = tiley * s->tile_height;
379 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
381 for (i = 0; i < 2; i++)
382 for (j = 0; j < 2; j++)
383 comp->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
385 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))
392 static void copy_frame(J2kEncoderContext *s)
394 int tileno, compno, i, y, x;
396 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
397 J2kTile *tile = s->tile + tileno;
399 for (compno = 0; compno < s->ncomponents; compno++){
400 J2kComponent *comp = tile->comp + compno;
401 int *dst = comp->data;
402 line = s->picture.data[compno]
403 + comp->coord[1][0] * s->picture.linesize[compno]
405 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
407 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
408 *dst++ = *ptr++ - (1 << 7);
409 line += s->picture.linesize[compno];
413 line = s->picture.data[0] + tile->comp[0].coord[1][0] * s->picture.linesize[0]
414 + tile->comp[0].coord[0][0] * s->ncomponents;
417 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
419 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
420 for (compno = 0; compno < s->ncomponents; compno++){
421 tile->comp[compno].data[i] = *ptr++ - (1 << 7);
424 line += s->picture.linesize[0];
430 static void init_quantization(J2kEncoderContext *s)
432 int compno, reslevelno, bandno;
433 J2kQuantStyle *qntsty = &s->qntsty;
434 J2kCodingStyle *codsty = &s->codsty;
436 for (compno = 0; compno < s->ncomponents; compno++){
438 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
439 int nbands, lev = codsty->nreslevels - reslevelno - 1;
440 nbands = reslevelno ? 3 : 1;
441 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
444 if (codsty->transform == FF_DWT97){
445 int bandpos = bandno + (reslevelno>0),
446 ss = 81920000 / dwt_norms[0][bandpos][lev],
448 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
449 expn = s->cbps[compno] - log + 13;
451 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
453 qntsty->expn[gbandno] = expn;
454 qntsty->mant[gbandno] = mant;
460 static void init_luts(void)
463 mask = ~((1<<NMSEDEC_FRACBITS)-1);
465 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
466 lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
467 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
469 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
470 lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
471 << 13-NMSEDEC_FRACBITS, 0);
472 lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
477 /* tier-1 routines */
478 static int getnmsedec_sig(int x, int bpno)
480 if (bpno > NMSEDEC_FRACBITS)
481 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
482 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
485 static int getnmsedec_ref(int x, int bpno)
487 if (bpno > NMSEDEC_FRACBITS)
488 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
489 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
492 static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
494 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
495 int vert_causal_ctx_csty_loc_symbol;
496 for (y0 = 0; y0 < height; y0 += 4)
497 for (x = 0; x < width; x++)
498 for (y = y0; y < height && y < y0+4; y++){
499 if (!(t1->flags[y+1][x+1] & J2K_T1_SIG) && (t1->flags[y+1][x+1] & J2K_T1_SIG_NB)){
500 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol),
501 bit = t1->data[y][x] & mask ? 1 : 0;
502 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
505 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
506 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
507 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
508 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
510 t1->flags[y+1][x+1] |= J2K_T1_VIS;
515 static void encode_refpass(J2kT1Context *t1, int width, int height, int *nmsedec, int bpno)
517 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
518 for (y0 = 0; y0 < height; y0 += 4)
519 for (x = 0; x < width; x++)
520 for (y = y0; y < height && y < y0+4; y++)
521 if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
522 int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
523 *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
524 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
525 t1->flags[y+1][x+1] |= J2K_T1_REF;
529 static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
531 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
532 int vert_causal_ctx_csty_loc_symbol;
533 for (y0 = 0; y0 < height; y0 += 4)
534 for (x = 0; x < width; x++){
535 if (y0 + 3 < height && !(
536 (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
537 (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
538 (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
539 (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG))))
543 for (rlen = 0; rlen < 4; rlen++)
544 if (t1->data[y0+rlen][x] & mask)
546 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
549 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
550 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
551 for (y = y0 + rlen; y < y0 + 4; y++){
552 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
553 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
555 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
556 if (t1->data[y][x] & mask){ // newly significant
558 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
559 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
560 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
561 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
564 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
567 for (y = y0; y < y0 + 4 && y < height; y++){
568 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
569 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
570 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
571 if (t1->data[y][x] & mask){ // newly significant
573 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
574 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
575 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
576 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
579 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
585 static void encode_cblk(J2kEncoderContext *s, J2kT1Context *t1, J2kCblk *cblk, J2kTile *tile,
586 int width, int height, int bandpos, int lev)
588 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
591 for (y = 0; y < height+2; y++)
592 memset(t1->flags[y], 0, (width+2)*sizeof(int));
594 for (y = 0; y < height; y++){
595 for (x = 0; x < width; x++){
596 if (t1->data[y][x] < 0){
597 t1->flags[y+1][x+1] |= J2K_T1_SGN;
598 t1->data[y][x] = -t1->data[y][x];
600 max = FFMAX(max, t1->data[y][x]);
605 cblk->nonzerobits = 0;
608 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
609 bpno = cblk->nonzerobits - 1;
612 ff_mqc_initenc(&t1->mqc, cblk->data);
614 for (passno = 0; bpno >= 0; passno++){
618 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
620 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
622 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
626 cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
627 wmsedec += (int64_t)nmsedec << (2*bpno);
628 cblk->passes[passno].disto = wmsedec;
635 cblk->npasses = passno;
636 cblk->ninclpasses = passno;
638 // TODO: optional flush on each pass
639 cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
642 /* tier-2 routines: */
644 static void putnumpasses(J2kEncoderContext *s, int n)
651 put_num(s, 0xc | (n-3), 4);
653 put_num(s, 0x1e0 | (n-6), 9);
655 put_num(s, 0xff80 | (n-37), 16);
659 static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno,
660 uint8_t *expn, int numgbits)
662 int bandno, empty = 1;
670 // is the packet empty?
671 for (bandno = 0; bandno < rlevel->nbands; bandno++){
672 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
673 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
679 put_bits(s, !empty, 1);
685 for (bandno = 0; bandno < rlevel->nbands; bandno++){
686 J2kBand *band = rlevel->band + bandno;
687 J2kPrec *prec = band->prec + precno;
689 int cblknw = prec->xi1 - prec->xi0;
691 if (band->coord[0][0] == band->coord[0][1]
692 || band->coord[1][0] == band->coord[1][1])
695 for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
696 for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
697 prec->cblkincl[pos].val = band->cblk[yi * cblknw + xi].ninclpasses == 0;
698 tag_tree_update(prec->cblkincl + pos);
699 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - band->cblk[yi * cblknw + xi].nonzerobits;
700 tag_tree_update(prec->zerobits + pos);
704 for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
705 for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
706 int pad = 0, llen, length;
707 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
709 if (s->buf_end - s->buf < 20) // approximately
712 // inclusion information
713 tag_tree_code(s, prec->cblkincl + pos, 1);
714 if (!cblk->ninclpasses)
716 // zerobits information
717 tag_tree_code(s, prec->zerobits + pos, 100);
719 putnumpasses(s, cblk->ninclpasses);
721 length = cblk->passes[cblk->ninclpasses-1].rate;
722 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
727 // length of code block
728 put_bits(s, 1, llen);
730 put_num(s, length, av_log2(length)+1+pad);
735 for (bandno = 0; bandno < rlevel->nbands; bandno++){
736 J2kBand *band = rlevel->band + bandno;
737 J2kPrec *prec = band->prec + precno;
738 int yi, cblknw = prec->xi1 - prec->xi0;
739 for (yi = prec->yi0; yi < prec->yi1; yi++){
741 for (xi = prec->xi0; xi < prec->xi1; xi++){
742 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
743 if (cblk->ninclpasses){
744 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
746 bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
754 static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
756 int compno, reslevelno, ret;
757 J2kCodingStyle *codsty = &s->codsty;
758 J2kQuantStyle *qntsty = &s->qntsty;
760 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
761 // lay-rlevel-comp-pos progression
762 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
763 for (compno = 0; compno < s->ncomponents; compno++){
765 J2kResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
766 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
767 if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
773 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
777 static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
780 for (passno = 0; passno < cblk->npasses; passno++){
784 dr = cblk->passes[passno].rate
785 - (res ? cblk->passes[res-1].rate:0);
786 dd = cblk->passes[passno].disto
787 - (res ? cblk->passes[res-1].disto:0);
789 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
795 static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
797 int compno, reslevelno, bandno, cblkno, lev;
798 J2kCodingStyle *codsty = &s->codsty;
800 for (compno = 0; compno < s->ncomponents; compno++){
801 J2kComponent *comp = tile->comp + compno;
803 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
804 J2kResLevel *reslevel = comp->reslevel + reslevelno;
806 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
807 int bandpos = bandno + (reslevelno > 0);
808 J2kBand *band = reslevel->band + bandno;
810 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
811 J2kCblk *cblk = band->cblk + cblkno;
813 cblk->ninclpasses = getcut(cblk, s->lambda,
814 (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 13);
821 static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
823 int compno, reslevelno, bandno, ret;
825 J2kCodingStyle *codsty = &s->codsty;
826 for (compno = 0; compno < s->ncomponents; compno++){
827 J2kComponent *comp = s->tile[tileno].comp + compno;
829 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
830 if (ret = ff_j2k_dwt_encode(&comp->dwt, comp->data))
832 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
834 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
835 J2kResLevel *reslevel = comp->reslevel + reslevelno;
837 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
838 J2kBand *band = reslevel->band + bandno;
839 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
840 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
842 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
843 band->coord[1][1]) - band->coord[1][0] + yy0;
845 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
848 bandpos = bandno + (reslevelno > 0);
850 for (cblky = 0; cblky < band->cblkny; cblky++){
851 if (reslevelno == 0 || bandno == 1)
854 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
856 xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
857 band->coord[0][1]) - band->coord[0][0] + xx0;
859 for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
861 if (codsty->transform == FF_DWT53){
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] << NMSEDEC_FRACBITS;
869 for (y = yy0; y < yy1; y++){
870 int *ptr = t1.data[y-yy0];
871 for (x = xx0; x < xx1; x++){
872 *ptr = (comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
873 *ptr = (int64_t)*ptr * (int64_t)(8192 * 8192 / band->stepsize) >> 13 - NMSEDEC_FRACBITS;
878 encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
879 bandpos, codsty->nreslevels - reslevelno - 1);
881 xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
884 yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
888 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
891 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
892 truncpasses(s, tile);
893 if (ret = encode_packets(s, tile, tileno))
895 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
899 static void cleanup(J2kEncoderContext *s)
902 J2kCodingStyle *codsty = &s->codsty;
904 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
905 for (compno = 0; compno < s->ncomponents; compno++){
906 J2kComponent *comp = s->tile[tileno].comp + compno;
907 ff_j2k_cleanup(comp, codsty);
909 av_freep(&s->tile[tileno].comp);
914 static void reinit(J2kEncoderContext *s)
917 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
918 J2kTile *tile = s->tile + tileno;
919 for (compno = 0; compno < s->ncomponents; compno++)
920 ff_j2k_reinit(tile->comp + compno, &s->codsty);
924 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
925 const AVFrame *pict, int *got_packet)
928 J2kEncoderContext *s = avctx->priv_data;
930 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
934 s->buf = s->buf_start = pkt->data;
935 s->buf_end = pkt->data + pkt->size;
938 avctx->coded_frame= &s->picture;
940 s->lambda = s->picture.quality * LAMBDA_SCALE;
945 if (s->buf_end - s->buf < 2)
947 bytestream_put_be16(&s->buf, J2K_SOC);
948 if (ret = put_siz(s))
950 if (ret = put_cod(s))
952 if (ret = put_qcd(s, 0))
955 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
957 if (!(psotptr = put_sot(s, tileno)))
959 if (s->buf_end - s->buf < 2)
961 bytestream_put_be16(&s->buf, J2K_SOD);
962 if (ret = encode_tile(s, s->tile + tileno, tileno))
964 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
966 if (s->buf_end - s->buf < 2)
968 bytestream_put_be16(&s->buf, J2K_EOC);
970 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
971 pkt->size = s->buf - s->buf_start;
972 pkt->flags |= AV_PKT_FLAG_KEY;
978 static av_cold int j2kenc_init(AVCodecContext *avctx)
981 J2kEncoderContext *s = avctx->priv_data;
982 J2kCodingStyle *codsty = &s->codsty;
983 J2kQuantStyle *qntsty = &s->qntsty;
986 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
989 // TODO: implement setting non-standard precinct size
990 codsty->log2_prec_width = 15;
991 codsty->log2_prec_height = 15;
992 codsty->nreslevels = 7;
993 codsty->log2_cblk_width = 4;
994 codsty->log2_cblk_height = 4;
995 codsty->transform = 1;
997 qntsty->nguardbits = 1;
1000 s->tile_height = 256;
1002 if (codsty->transform == FF_DWT53)
1003 qntsty->quantsty = J2K_QSTY_NONE;
1005 qntsty->quantsty = J2K_QSTY_SE;
1007 s->width = avctx->width;
1008 s->height = avctx->height;
1010 for (i = 0; i < 3; i++)
1013 if (avctx->pix_fmt == PIX_FMT_RGB24){
1015 } else if (avctx->pix_fmt == PIX_FMT_GRAY8){
1017 } else{ // planar YUV
1020 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
1021 s->chroma_shift, s->chroma_shift + 1);
1024 ff_j2k_init_tier1_luts();
1028 init_quantization(s);
1029 if (ret=init_tiles(s))
1032 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1037 static int j2kenc_destroy(AVCodecContext *avctx)
1039 J2kEncoderContext *s = avctx->priv_data;
1045 AVCodec ff_jpeg2000_encoder = {
1047 .type = AVMEDIA_TYPE_VIDEO,
1048 .id = CODEC_ID_JPEG2000,
1049 .priv_data_size = sizeof(J2kEncoderContext),
1050 .init = j2kenc_init,
1051 .encode2 = encode_frame,
1052 .close = j2kenc_destroy,
1053 .capabilities = CODEC_CAP_EXPERIMENTAL,
1054 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1055 .pix_fmts = (const enum PixelFormat[]) {
1056 PIX_FMT_RGB24, PIX_FMT_YUV444P, PIX_FMT_GRAY8,
1058 PIX_FMT_YUV422P, PIX_FMT_YUV444P,
1059 PIX_FMT_YUV410P, PIX_FMT_YUV411P,*/