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}}
58 Jpeg2000Component *comp;
62 AVCodecContext *avctx;
63 const AVFrame *picture;
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 Jpeg2000CodingStyle codsty;
81 Jpeg2000QuantStyle qntsty;
84 } Jpeg2000EncoderContext;
92 static void nspaces(FILE *fd, int n)
94 while(n--) putc(' ', fd);
97 static void printcomp(Jpeg2000Component *comp)
100 for (i = 0; i < comp->y1 - comp->y0; i++)
101 ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
104 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
106 int tileno, compno, reslevelno, bandno, precno;
107 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
108 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
110 s->width, s->height, s->tile_width, s->tile_height,
111 s->numXtiles, s->numYtiles, s->ncomponents);
112 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
113 Jpeg2000Tile *tile = s->tile + tileno;
115 fprintf(fd, "tile %d:\n", tileno);
116 for(compno = 0; compno < s->ncomponents; compno++){
117 Jpeg2000Component *comp = tile->comp + compno;
119 fprintf(fd, "component %d:\n", compno);
121 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
122 comp->x0, comp->x1, comp->y0, comp->y1);
123 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
124 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
126 fprintf(fd, "reslevel %d:\n", reslevelno);
128 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
129 reslevel->x0, reslevel->x1, reslevel->y0,
130 reslevel->y1, reslevel->nbands);
131 for(bandno = 0; bandno < reslevel->nbands; bandno++){
132 Jpeg2000Band *band = reslevel->band + bandno;
134 fprintf(fd, "band %d:\n", bandno);
136 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
137 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
140 band->codeblock_width, band->codeblock_height,
141 band->cblknx, band->cblkny);
142 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
143 Jpeg2000Prec *prec = band->prec + precno;
145 fprintf(fd, "prec %d:\n", precno);
147 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
148 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
157 /* bitstream routines */
159 /** put n times val bit */
160 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
163 if (s->bit_index == 8)
165 s->bit_index = *s->buf == 0xff;
168 *s->buf |= val << (7 - s->bit_index++);
172 /** put n least significant bits of a number num */
173 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
176 put_bits(s, (num >> n) & 1, 1);
179 /** flush the bitstream */
180 static void j2k_flush(Jpeg2000EncoderContext *s)
188 /* tag tree routines */
190 /** code the value stored in node */
191 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
193 Jpeg2000TgtNode *stack[30];
194 int sp = 1, curval = 0;
208 if (stack[sp]->val >= threshold){
209 put_bits(s, 0, threshold - curval);
212 put_bits(s, 0, stack[sp]->val - curval);
214 curval = stack[sp]->val;
218 /** update the value in node */
219 static void tag_tree_update(Jpeg2000TgtNode *node)
222 while (node->parent){
223 if (node->parent->val <= node->val)
225 node->parent->val = node->val;
231 static int put_siz(Jpeg2000EncoderContext *s)
235 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
238 bytestream_put_be16(&s->buf, JPEG2000_SIZ);
239 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
240 bytestream_put_be16(&s->buf, 0); // Rsiz
241 bytestream_put_be32(&s->buf, s->width); // width
242 bytestream_put_be32(&s->buf, s->height); // height
243 bytestream_put_be32(&s->buf, 0); // X0Siz
244 bytestream_put_be32(&s->buf, 0); // Y0Siz
246 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
247 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
248 bytestream_put_be32(&s->buf, 0); // XT0Siz
249 bytestream_put_be32(&s->buf, 0); // YT0Siz
250 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
252 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
253 bytestream_put_byte(&s->buf, 7);
254 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
255 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
260 static int put_cod(Jpeg2000EncoderContext *s)
262 Jpeg2000CodingStyle *codsty = &s->codsty;
264 if (s->buf_end - s->buf < 14)
267 bytestream_put_be16(&s->buf, JPEG2000_COD);
268 bytestream_put_be16(&s->buf, 12); // Lcod
269 bytestream_put_byte(&s->buf, 0); // Scod
271 bytestream_put_byte(&s->buf, 0); // progression level
272 bytestream_put_be16(&s->buf, 1); // num of layers
273 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
274 bytestream_put_byte(&s->buf, 2); // ICT
276 bytestream_put_byte(&s->buf, 0); // unspecified
279 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
280 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
281 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
282 bytestream_put_byte(&s->buf, 0); // cblk style
283 bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
287 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
290 Jpeg2000CodingStyle *codsty = &s->codsty;
291 Jpeg2000QuantStyle *qntsty = &s->qntsty;
293 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
294 size = 4 + 3 * (codsty->nreslevels-1);
296 size = 5 + 6 * (codsty->nreslevels-1);
298 if (s->buf_end - s->buf < size + 2)
301 bytestream_put_be16(&s->buf, JPEG2000_QCD);
302 bytestream_put_be16(&s->buf, size); // LQcd
303 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
304 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
305 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
306 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
308 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
309 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
313 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
317 if (s->buf_end - s->buf < 12)
320 bytestream_put_be16(&s->buf, JPEG2000_SOT);
321 bytestream_put_be16(&s->buf, 10); // Lsot
322 bytestream_put_be16(&s->buf, tileno); // Isot
325 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
327 bytestream_put_byte(&s->buf, 0); // TPsot
328 bytestream_put_byte(&s->buf, 1); // TNsot
333 * compute the sizes of tiles, resolution levels, bands, etc.
334 * allocate memory for them
335 * divide the input image into tile-components
337 static int init_tiles(Jpeg2000EncoderContext *s)
339 int tileno, tilex, tiley, compno;
340 Jpeg2000CodingStyle *codsty = &s->codsty;
341 Jpeg2000QuantStyle *qntsty = &s->qntsty;
343 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
344 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
346 s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
348 return AVERROR(ENOMEM);
349 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
350 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
351 Jpeg2000Tile *tile = s->tile + tileno;
353 tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
355 return AVERROR(ENOMEM);
356 for (compno = 0; compno < s->ncomponents; compno++){
357 Jpeg2000Component *comp = tile->comp + compno;
360 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
361 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
362 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
363 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
365 for (i = 0; i < 2; i++)
366 for (j = 0; j < 2; j++)
367 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
369 if (ret = ff_jpeg2000_init_component(comp,
373 compno?1<<s->chroma_shift[0]:1,
374 compno?1<<s->chroma_shift[1]:1,
383 static void copy_frame(Jpeg2000EncoderContext *s)
385 int tileno, compno, i, y, x;
387 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
388 Jpeg2000Tile *tile = s->tile + tileno;
390 for (compno = 0; compno < s->ncomponents; compno++){
391 Jpeg2000Component *comp = tile->comp + compno;
392 int *dst = comp->i_data;
393 line = s->picture->data[compno]
394 + comp->coord[1][0] * s->picture->linesize[compno]
396 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
398 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
399 *dst++ = *ptr++ - (1 << 7);
400 line += s->picture->linesize[compno];
404 line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
405 + tile->comp[0].coord[0][0] * s->ncomponents;
408 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
410 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
411 for (compno = 0; compno < s->ncomponents; compno++){
412 tile->comp[compno].i_data[i] = *ptr++ - (1 << 7);
415 line += s->picture->linesize[0];
421 static void init_quantization(Jpeg2000EncoderContext *s)
423 int compno, reslevelno, bandno;
424 Jpeg2000QuantStyle *qntsty = &s->qntsty;
425 Jpeg2000CodingStyle *codsty = &s->codsty;
427 for (compno = 0; compno < s->ncomponents; compno++){
429 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
430 int nbands, lev = codsty->nreslevels - reslevelno - 1;
431 nbands = reslevelno ? 3 : 1;
432 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
435 if (codsty->transform == FF_DWT97_INT){
436 int bandpos = bandno + (reslevelno>0),
437 ss = 81920000 / dwt_norms[0][bandpos][lev],
439 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
440 expn = s->cbps[compno] - log + 13;
442 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
444 qntsty->expn[gbandno] = expn;
445 qntsty->mant[gbandno] = mant;
451 static void init_luts(void)
454 mask = ~((1<<NMSEDEC_FRACBITS)-1);
456 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
457 lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
458 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
460 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
461 lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
462 << 13-NMSEDEC_FRACBITS, 0);
463 lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
468 /* tier-1 routines */
469 static int getnmsedec_sig(int x, int bpno)
471 if (bpno > NMSEDEC_FRACBITS)
472 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
473 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
476 static int getnmsedec_ref(int x, int bpno)
478 if (bpno > NMSEDEC_FRACBITS)
479 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
480 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
483 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
485 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
486 for (y0 = 0; y0 < height; y0 += 4)
487 for (x = 0; x < width; x++)
488 for (y = y0; y < height && y < y0+4; y++){
489 if (!(t1->flags[y+1][x+1] & JPEG2000_T1_SIG) && (t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)){
490 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1], bandno),
491 bit = t1->data[y][x] & mask ? 1 : 0;
492 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
495 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
496 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
497 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
498 ff_jpeg2000_set_significance(t1, x, y, t1->flags[y+1][x+1] >> 15);
500 t1->flags[y+1][x+1] |= JPEG2000_T1_VIS;
505 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
507 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
508 for (y0 = 0; y0 < height; y0 += 4)
509 for (x = 0; x < width; x++)
510 for (y = y0; y < height && y < y0+4; y++)
511 if ((t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
512 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y+1][x+1]);
513 *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
514 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
515 t1->flags[y+1][x+1] |= JPEG2000_T1_REF;
519 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
521 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
522 for (y0 = 0; y0 < height; y0 += 4)
523 for (x = 0; x < width; x++){
524 if (y0 + 3 < height && !(
525 (t1->flags[y0+1][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
526 (t1->flags[y0+2][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
527 (t1->flags[y0+3][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
528 (t1->flags[y0+4][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
532 for (rlen = 0; rlen < 4; rlen++)
533 if (t1->data[y0+rlen][x] & mask)
535 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
538 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
539 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
540 for (y = y0 + rlen; y < y0 + 4; y++){
541 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
542 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1], bandno);
544 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
545 if (t1->data[y][x] & mask){ // newly significant
547 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
548 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
549 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
550 ff_jpeg2000_set_significance(t1, x, y, t1->flags[y+1][x+1] >> 15);
553 t1->flags[y+1][x+1] &= ~JPEG2000_T1_VIS;
556 for (y = y0; y < y0 + 4 && y < height; y++){
557 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
558 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1], bandno);
559 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
560 if (t1->data[y][x] & mask){ // newly significant
562 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
563 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
564 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
565 ff_jpeg2000_set_significance(t1, x, y, t1->flags[y+1][x+1] >> 15);
568 t1->flags[y+1][x+1] &= ~JPEG2000_T1_VIS;
574 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
575 int width, int height, int bandpos, int lev)
577 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
580 for (y = 0; y < height+2; y++)
581 memset(t1->flags[y], 0, (width+2)*sizeof(int));
583 for (y = 0; y < height; y++){
584 for (x = 0; x < width; x++){
585 if (t1->data[y][x] < 0){
586 t1->flags[y+1][x+1] |= JPEG2000_T1_SGN;
587 t1->data[y][x] = -t1->data[y][x];
589 max = FFMAX(max, t1->data[y][x]);
594 cblk->nonzerobits = 0;
597 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
598 bpno = cblk->nonzerobits - 1;
601 ff_mqc_initenc(&t1->mqc, cblk->data);
603 for (passno = 0; bpno >= 0; passno++){
607 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
609 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
611 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
615 cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
616 wmsedec += (int64_t)nmsedec << (2*bpno);
617 cblk->passes[passno].disto = wmsedec;
624 cblk->npasses = passno;
625 cblk->ninclpasses = passno;
627 // TODO: optional flush on each pass
628 cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
631 /* tier-2 routines: */
633 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
640 put_num(s, 0xc | (n-3), 4);
642 put_num(s, 0x1e0 | (n-6), 9);
644 put_num(s, 0xff80 | (n-37), 16);
648 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
649 uint8_t *expn, int numgbits)
651 int bandno, empty = 1;
659 // is the packet empty?
660 for (bandno = 0; bandno < rlevel->nbands; bandno++){
661 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
662 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
668 put_bits(s, !empty, 1);
674 for (bandno = 0; bandno < rlevel->nbands; bandno++){
675 Jpeg2000Band *band = rlevel->band + bandno;
676 Jpeg2000Prec *prec = band->prec + precno;
678 int cblknw = prec->nb_codeblocks_width;
680 if (band->coord[0][0] == band->coord[0][1]
681 || band->coord[1][0] == band->coord[1][1])
684 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
685 for (xi = 0; xi < cblknw; xi++, pos++){
686 prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
687 tag_tree_update(prec->cblkincl + pos);
688 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
689 tag_tree_update(prec->zerobits + pos);
693 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
694 for (xi = 0; xi < cblknw; xi++, pos++){
695 int pad = 0, llen, length;
696 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
698 if (s->buf_end - s->buf < 20) // approximately
701 // inclusion information
702 tag_tree_code(s, prec->cblkincl + pos, 1);
703 if (!cblk->ninclpasses)
705 // zerobits information
706 tag_tree_code(s, prec->zerobits + pos, 100);
708 putnumpasses(s, cblk->ninclpasses);
710 length = cblk->passes[cblk->ninclpasses-1].rate;
711 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
716 // length of code block
717 put_bits(s, 1, llen);
719 put_num(s, length, av_log2(length)+1+pad);
724 for (bandno = 0; bandno < rlevel->nbands; bandno++){
725 Jpeg2000Band *band = rlevel->band + bandno;
726 Jpeg2000Prec *prec = band->prec + precno;
727 int yi, cblknw = prec->nb_codeblocks_width;
728 for (yi =0; yi < prec->nb_codeblocks_height; yi++){
730 for (xi = 0; xi < cblknw; xi++){
731 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
732 if (cblk->ninclpasses){
733 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
735 bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
743 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
745 int compno, reslevelno, ret;
746 Jpeg2000CodingStyle *codsty = &s->codsty;
747 Jpeg2000QuantStyle *qntsty = &s->qntsty;
749 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
750 // lay-rlevel-comp-pos progression
751 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
752 for (compno = 0; compno < s->ncomponents; compno++){
754 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
755 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
756 if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
762 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
766 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
769 for (passno = 0; passno < cblk->npasses; passno++){
773 dr = cblk->passes[passno].rate
774 - (res ? cblk->passes[res-1].rate:0);
775 dd = cblk->passes[passno].disto
776 - (res ? cblk->passes[res-1].disto:0);
778 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
784 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
786 int precno, compno, reslevelno, bandno, cblkno, lev;
787 Jpeg2000CodingStyle *codsty = &s->codsty;
789 for (compno = 0; compno < s->ncomponents; compno++){
790 Jpeg2000Component *comp = tile->comp + compno;
792 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
793 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
795 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
796 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
797 int bandpos = bandno + (reslevelno > 0);
798 Jpeg2000Band *band = reslevel->band + bandno;
799 Jpeg2000Prec *prec = band->prec + precno;
801 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
802 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
804 cblk->ninclpasses = getcut(cblk, s->lambda,
805 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
813 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
815 int compno, reslevelno, bandno, ret;
816 Jpeg2000T1Context t1;
817 Jpeg2000CodingStyle *codsty = &s->codsty;
818 for (compno = 0; compno < s->ncomponents; compno++){
819 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
821 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
822 if (ret = ff_dwt_encode(&comp->dwt, comp->i_data))
824 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
826 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
827 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
829 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
830 Jpeg2000Band *band = reslevel->band + bandno;
831 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
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_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_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 < prec->nb_codeblocks_height; 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_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
850 band->coord[0][1]) - band->coord[0][0] + xx0;
852 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; 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->i_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->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
866 *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
871 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
872 bandpos, codsty->nreslevels - reslevelno - 1);
874 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
877 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
881 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
884 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
885 truncpasses(s, tile);
886 if (ret = encode_packets(s, tile, tileno))
888 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
892 static void cleanup(Jpeg2000EncoderContext *s)
895 Jpeg2000CodingStyle *codsty = &s->codsty;
897 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
898 for (compno = 0; compno < s->ncomponents; compno++){
899 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
900 ff_jpeg2000_cleanup(comp, codsty);
902 av_freep(&s->tile[tileno].comp);
907 static void reinit(Jpeg2000EncoderContext *s)
910 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
911 Jpeg2000Tile *tile = s->tile + tileno;
912 for (compno = 0; compno < s->ncomponents; compno++)
913 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
917 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
918 const AVFrame *pict, int *got_packet)
921 Jpeg2000EncoderContext *s = avctx->priv_data;
923 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
927 s->buf = s->buf_start = pkt->data;
928 s->buf_end = pkt->data + pkt->size;
932 s->lambda = s->picture->quality * LAMBDA_SCALE;
937 if (s->buf_end - s->buf < 2)
939 bytestream_put_be16(&s->buf, JPEG2000_SOC);
940 if (ret = put_siz(s))
942 if (ret = put_cod(s))
944 if (ret = put_qcd(s, 0))
947 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
949 if (!(psotptr = put_sot(s, tileno)))
951 if (s->buf_end - s->buf < 2)
953 bytestream_put_be16(&s->buf, JPEG2000_SOD);
954 if (ret = encode_tile(s, s->tile + tileno, tileno))
956 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
958 if (s->buf_end - s->buf < 2)
960 bytestream_put_be16(&s->buf, JPEG2000_EOC);
962 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
963 pkt->size = s->buf - s->buf_start;
964 pkt->flags |= AV_PKT_FLAG_KEY;
970 static av_cold int j2kenc_init(AVCodecContext *avctx)
973 Jpeg2000EncoderContext *s = avctx->priv_data;
974 Jpeg2000CodingStyle *codsty = &s->codsty;
975 Jpeg2000QuantStyle *qntsty = &s->qntsty;
978 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
981 // TODO: implement setting non-standard precinct size
982 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
983 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
984 codsty->nreslevels2decode=
985 codsty->nreslevels = 7;
986 codsty->log2_cblk_width = 4;
987 codsty->log2_cblk_height = 4;
988 codsty->transform = avctx->prediction_method ? FF_DWT53 : FF_DWT97_INT;
990 qntsty->nguardbits = 1;
993 s->tile_height = 256;
995 if (codsty->transform == FF_DWT53)
996 qntsty->quantsty = JPEG2000_QSTY_NONE;
998 qntsty->quantsty = JPEG2000_QSTY_SE;
1000 s->width = avctx->width;
1001 s->height = avctx->height;
1003 for (i = 0; i < 3; i++)
1006 if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
1008 } else if (avctx->pix_fmt == AV_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_jpeg2000_init_tier1_luts();
1018 ff_mqc_init_context_tables();
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 Jpeg2000EncoderContext *s = avctx->priv_data;
1038 AVCodec ff_jpeg2000_encoder = {
1040 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1041 .type = AVMEDIA_TYPE_VIDEO,
1042 .id = AV_CODEC_ID_JPEG2000,
1043 .priv_data_size = sizeof(Jpeg2000EncoderContext),
1044 .init = j2kenc_init,
1045 .encode2 = encode_frame,
1046 .close = j2kenc_destroy,
1047 .capabilities = CODEC_CAP_EXPERIMENTAL,
1048 .pix_fmts = (const enum AVPixelFormat[]) {
1049 AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1050 /* AV_PIX_FMT_YUV420P,
1051 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1052 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,*/