00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include <float.h>
00029 #include "avcodec.h"
00030 #include "internal.h"
00031 #include "bytestream.h"
00032 #include "j2k.h"
00033 #include "libavutil/common.h"
00034
00035 #define NMSEDEC_BITS 7
00036 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
00037 #define WMSEDEC_SHIFT 13
00038 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
00039
00040 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
00041 lut_nmsedec_ref0[1<<NMSEDEC_BITS],
00042 lut_nmsedec_sig [1<<NMSEDEC_BITS],
00043 lut_nmsedec_sig0[1<<NMSEDEC_BITS];
00044
00045 static const int dwt_norms[2][4][10] = {
00046 {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
00047 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
00048 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
00049 {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
00050
00051 {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
00052 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
00053 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
00054 { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
00055 };
00056
00057 typedef struct {
00058 J2kComponent *comp;
00059 } J2kTile;
00060
00061 typedef struct {
00062 AVCodecContext *avctx;
00063 AVFrame picture;
00064
00065 int width, height;
00066 uint8_t cbps[4];
00067 int chroma_shift[2];
00068 uint8_t planar;
00069 int ncomponents;
00070 int tile_width, tile_height;
00071 int numXtiles, numYtiles;
00072
00073 uint8_t *buf_start;
00074 uint8_t *buf;
00075 uint8_t *buf_end;
00076 int bit_index;
00077
00078 int64_t lambda;
00079
00080 J2kCodingStyle codsty;
00081 J2kQuantStyle qntsty;
00082
00083 J2kTile *tile;
00084 } J2kEncoderContext;
00085
00086
00087
00088 #if 0
00089 #undef ifprintf
00090 #undef printf
00091
00092 static void nspaces(FILE *fd, int n)
00093 {
00094 while(n--) putc(' ', fd);
00095 }
00096
00097 static void printv(int *tab, int l)
00098 {
00099 int i;
00100 for (i = 0; i < l; i++)
00101 printf("%.3d ", tab[i]);
00102 printf("\n");
00103 }
00104
00105 static void printu(uint8_t *tab, int l)
00106 {
00107 int i;
00108 for (i = 0; i < l; i++)
00109 printf("%.3hd ", tab[i]);
00110 printf("\n");
00111 }
00112
00113 static void printcomp(J2kComponent *comp)
00114 {
00115 int i;
00116 for (i = 0; i < comp->y1 - comp->y0; i++)
00117 printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
00118 }
00119
00120 static void dump(J2kEncoderContext *s, FILE *fd)
00121 {
00122 int tileno, compno, reslevelno, bandno, precno;
00123 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
00124 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
00125 "tiles:\n",
00126 s->width, s->height, s->tile_width, s->tile_height,
00127 s->numXtiles, s->numYtiles, s->ncomponents);
00128 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00129 J2kTile *tile = s->tile + tileno;
00130 nspaces(fd, 2);
00131 fprintf(fd, "tile %d:\n", tileno);
00132 for(compno = 0; compno < s->ncomponents; compno++){
00133 J2kComponent *comp = tile->comp + compno;
00134 nspaces(fd, 4);
00135 fprintf(fd, "component %d:\n", compno);
00136 nspaces(fd, 4);
00137 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
00138 comp->x0, comp->x1, comp->y0, comp->y1);
00139 for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
00140 J2kResLevel *reslevel = comp->reslevel + reslevelno;
00141 nspaces(fd, 6);
00142 fprintf(fd, "reslevel %d:\n", reslevelno);
00143 nspaces(fd, 6);
00144 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
00145 reslevel->x0, reslevel->x1, reslevel->y0,
00146 reslevel->y1, reslevel->nbands);
00147 for(bandno = 0; bandno < reslevel->nbands; bandno++){
00148 J2kBand *band = reslevel->band + bandno;
00149 nspaces(fd, 8);
00150 fprintf(fd, "band %d:\n", bandno);
00151 nspaces(fd, 8);
00152 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
00153 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
00154 band->x0, band->x1,
00155 band->y0, band->y1,
00156 band->codeblock_width, band->codeblock_height,
00157 band->cblknx, band->cblkny);
00158 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
00159 J2kPrec *prec = band->prec + precno;
00160 nspaces(fd, 10);
00161 fprintf(fd, "prec %d:\n", precno);
00162 nspaces(fd, 10);
00163 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
00164 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
00165 }
00166 }
00167 }
00168 }
00169 }
00170 }
00171 #endif
00172
00173
00174
00176 static void put_bits(J2kEncoderContext *s, int val, int n)
00177 {
00178 while (n-- > 0){
00179 if (s->bit_index == 8)
00180 {
00181 s->bit_index = *s->buf == 0xff;
00182 *(++s->buf) = 0;
00183 }
00184 *s->buf |= val << (7 - s->bit_index++);
00185 }
00186 }
00187
00189 static void put_num(J2kEncoderContext *s, int num, int n)
00190 {
00191 while(--n >= 0)
00192 put_bits(s, (num >> n) & 1, 1);
00193 }
00194
00196 static void j2k_flush(J2kEncoderContext *s)
00197 {
00198 if (s->bit_index){
00199 s->bit_index = 0;
00200 s->buf++;
00201 }
00202 }
00203
00204
00205
00207 static void tag_tree_code(J2kEncoderContext *s, J2kTgtNode *node, int threshold)
00208 {
00209 J2kTgtNode *stack[30];
00210 int sp = 1, curval = 0;
00211 stack[0] = node;
00212
00213 node = node->parent;
00214 while(node){
00215 if (node->vis){
00216 curval = node->val;
00217 break;
00218 }
00219 node->vis++;
00220 stack[sp++] = node;
00221 node = node->parent;
00222 }
00223 while(--sp >= 0){
00224 if (stack[sp]->val >= threshold){
00225 put_bits(s, 0, threshold - curval);
00226 break;
00227 }
00228 put_bits(s, 0, stack[sp]->val - curval);
00229 put_bits(s, 1, 1);
00230 curval = stack[sp]->val;
00231 }
00232 }
00233
00235 static void tag_tree_update(J2kTgtNode *node)
00236 {
00237 int lev = 0;
00238 while (node->parent){
00239 if (node->parent->val <= node->val)
00240 break;
00241 node->parent->val = node->val;
00242 node = node->parent;
00243 lev++;
00244 }
00245 }
00246
00247 static int put_siz(J2kEncoderContext *s)
00248 {
00249 int i;
00250
00251 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
00252 return -1;
00253
00254 bytestream_put_be16(&s->buf, J2K_SIZ);
00255 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents);
00256 bytestream_put_be16(&s->buf, 0);
00257 bytestream_put_be32(&s->buf, s->width);
00258 bytestream_put_be32(&s->buf, s->height);
00259 bytestream_put_be32(&s->buf, 0);
00260 bytestream_put_be32(&s->buf, 0);
00261
00262 bytestream_put_be32(&s->buf, s->tile_width);
00263 bytestream_put_be32(&s->buf, s->tile_height);
00264 bytestream_put_be32(&s->buf, 0);
00265 bytestream_put_be32(&s->buf, 0);
00266 bytestream_put_be16(&s->buf, s->ncomponents);
00267
00268 for (i = 0; i < s->ncomponents; i++){
00269 bytestream_put_byte(&s->buf, 7);
00270 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
00271 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
00272 }
00273 return 0;
00274 }
00275
00276 static int put_cod(J2kEncoderContext *s)
00277 {
00278 J2kCodingStyle *codsty = &s->codsty;
00279
00280 if (s->buf_end - s->buf < 14)
00281 return -1;
00282
00283 bytestream_put_be16(&s->buf, J2K_COD);
00284 bytestream_put_be16(&s->buf, 12);
00285 bytestream_put_byte(&s->buf, 0);
00286
00287 bytestream_put_byte(&s->buf, 0);
00288 bytestream_put_be16(&s->buf, 1);
00289 if(s->avctx->pix_fmt == PIX_FMT_YUV444P){
00290 bytestream_put_byte(&s->buf, 2);
00291 }else{
00292 bytestream_put_byte(&s->buf, 0);
00293 }
00294
00295 bytestream_put_byte(&s->buf, codsty->nreslevels - 1);
00296 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2);
00297 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2);
00298 bytestream_put_byte(&s->buf, 0);
00299 bytestream_put_byte(&s->buf, codsty->transform);
00300 return 0;
00301 }
00302
00303 static int put_qcd(J2kEncoderContext *s, int compno)
00304 {
00305 int i, size;
00306 J2kCodingStyle *codsty = &s->codsty;
00307 J2kQuantStyle *qntsty = &s->qntsty;
00308
00309 if (qntsty->quantsty == J2K_QSTY_NONE)
00310 size = 4 + 3 * (codsty->nreslevels-1);
00311 else
00312 size = 5 + 6 * (codsty->nreslevels-1);
00313
00314 if (s->buf_end - s->buf < size + 2)
00315 return -1;
00316
00317 bytestream_put_be16(&s->buf, J2K_QCD);
00318 bytestream_put_be16(&s->buf, size);
00319 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty);
00320 if (qntsty->quantsty == J2K_QSTY_NONE)
00321 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
00322 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
00323 else
00324 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
00325 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
00326 return 0;
00327 }
00328
00329 static uint8_t *put_sot(J2kEncoderContext *s, int tileno)
00330 {
00331 uint8_t *psotptr;
00332
00333 if (s->buf_end - s->buf < 12)
00334 return NULL;
00335
00336 bytestream_put_be16(&s->buf, J2K_SOT);
00337 bytestream_put_be16(&s->buf, 10);
00338 bytestream_put_be16(&s->buf, tileno);
00339
00340 psotptr = s->buf;
00341 bytestream_put_be32(&s->buf, 0);
00342
00343 bytestream_put_byte(&s->buf, 0);
00344 bytestream_put_byte(&s->buf, 1);
00345 return psotptr;
00346 }
00347
00353 static int init_tiles(J2kEncoderContext *s)
00354 {
00355 int tileno, tilex, tiley, compno;
00356 J2kCodingStyle *codsty = &s->codsty;
00357 J2kQuantStyle *qntsty = &s->qntsty;
00358
00359 s->numXtiles = ff_j2k_ceildiv(s->width, s->tile_width);
00360 s->numYtiles = ff_j2k_ceildiv(s->height, s->tile_height);
00361
00362 s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(J2kTile));
00363 if (!s->tile)
00364 return AVERROR(ENOMEM);
00365 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
00366 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
00367 J2kTile *tile = s->tile + tileno;
00368
00369 tile->comp = av_malloc(s->ncomponents * sizeof(J2kComponent));
00370 if (!tile->comp)
00371 return AVERROR(ENOMEM);
00372 for (compno = 0; compno < s->ncomponents; compno++){
00373 J2kComponent *comp = tile->comp + compno;
00374 int ret, i, j;
00375
00376 comp->coord[0][0] = tilex * s->tile_width;
00377 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
00378 comp->coord[1][0] = tiley * s->tile_height;
00379 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
00380 if (compno > 0)
00381 for (i = 0; i < 2; i++)
00382 for (j = 0; j < 2; j++)
00383 comp->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
00384
00385 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))
00386 return ret;
00387 }
00388 }
00389 return 0;
00390 }
00391
00392 static void copy_frame(J2kEncoderContext *s)
00393 {
00394 int tileno, compno, i, y, x;
00395 uint8_t *line;
00396 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00397 J2kTile *tile = s->tile + tileno;
00398 if (s->planar){
00399 for (compno = 0; compno < s->ncomponents; compno++){
00400 J2kComponent *comp = tile->comp + compno;
00401 int *dst = comp->data;
00402 line = s->picture.data[compno]
00403 + comp->coord[1][0] * s->picture.linesize[compno]
00404 + comp->coord[0][0];
00405 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
00406 uint8_t *ptr = line;
00407 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
00408 *dst++ = *ptr++ - (1 << 7);
00409 line += s->picture.linesize[compno];
00410 }
00411 }
00412 } else{
00413 line = s->picture.data[0] + tile->comp[0].coord[1][0] * s->picture.linesize[0]
00414 + tile->comp[0].coord[0][0] * s->ncomponents;
00415
00416 i = 0;
00417 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
00418 uint8_t *ptr = line;
00419 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
00420 for (compno = 0; compno < s->ncomponents; compno++){
00421 tile->comp[compno].data[i] = *ptr++ - (1 << 7);
00422 }
00423 }
00424 line += s->picture.linesize[0];
00425 }
00426 }
00427 }
00428 }
00429
00430 static void init_quantization(J2kEncoderContext *s)
00431 {
00432 int compno, reslevelno, bandno;
00433 J2kQuantStyle *qntsty = &s->qntsty;
00434 J2kCodingStyle *codsty = &s->codsty;
00435
00436 for (compno = 0; compno < s->ncomponents; compno++){
00437 int gbandno = 0;
00438 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00439 int nbands, lev = codsty->nreslevels - reslevelno - 1;
00440 nbands = reslevelno ? 3 : 1;
00441 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
00442 int expn, mant;
00443
00444 if (codsty->transform == FF_DWT97){
00445 int bandpos = bandno + (reslevelno>0),
00446 ss = 81920000 / dwt_norms[0][bandpos][lev],
00447 log = av_log2(ss);
00448 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
00449 expn = s->cbps[compno] - log + 13;
00450 } else
00451 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
00452
00453 qntsty->expn[gbandno] = expn;
00454 qntsty->mant[gbandno] = mant;
00455 }
00456 }
00457 }
00458 }
00459
00460 static void init_luts(void)
00461 {
00462 int i, a,
00463 mask = ~((1<<NMSEDEC_FRACBITS)-1);
00464
00465 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
00466 lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
00467 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
00468
00469 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
00470 lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
00471 << 13-NMSEDEC_FRACBITS, 0);
00472 lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
00473 << 1, 0);
00474 }
00475 }
00476
00477
00478 static int getnmsedec_sig(int x, int bpno)
00479 {
00480 if (bpno > NMSEDEC_FRACBITS)
00481 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
00482 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
00483 }
00484
00485 static int getnmsedec_ref(int x, int bpno)
00486 {
00487 if (bpno > NMSEDEC_FRACBITS)
00488 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
00489 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
00490 }
00491
00492 static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
00493 {
00494 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
00495 int vert_causal_ctx_csty_loc_symbol;
00496 for (y0 = 0; y0 < height; y0 += 4)
00497 for (x = 0; x < width; x++)
00498 for (y = y0; y < height && y < y0+4; y++){
00499 if (!(t1->flags[y+1][x+1] & J2K_T1_SIG) && (t1->flags[y+1][x+1] & J2K_T1_SIG_NB)){
00500 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol),
00501 bit = t1->data[y][x] & mask ? 1 : 0;
00502 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
00503 if (bit){
00504 int xorbit;
00505 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00506 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
00507 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00508 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
00509 }
00510 t1->flags[y+1][x+1] |= J2K_T1_VIS;
00511 }
00512 }
00513 }
00514
00515 static void encode_refpass(J2kT1Context *t1, int width, int height, int *nmsedec, int bpno)
00516 {
00517 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
00518 for (y0 = 0; y0 < height; y0 += 4)
00519 for (x = 0; x < width; x++)
00520 for (y = y0; y < height && y < y0+4; y++)
00521 if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
00522 int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
00523 *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00524 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
00525 t1->flags[y+1][x+1] |= J2K_T1_REF;
00526 }
00527 }
00528
00529 static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
00530 {
00531 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
00532 int vert_causal_ctx_csty_loc_symbol;
00533 for (y0 = 0; y0 < height; y0 += 4)
00534 for (x = 0; x < width; x++){
00535 if (y0 + 3 < height && !(
00536 (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00537 (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00538 (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
00539 (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG))))
00540 {
00541
00542 int rlen;
00543 for (rlen = 0; rlen < 4; rlen++)
00544 if (t1->data[y0+rlen][x] & mask)
00545 break;
00546 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
00547 if (rlen == 4)
00548 continue;
00549 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
00550 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
00551 for (y = y0 + rlen; y < y0 + 4; y++){
00552 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
00553 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
00554 if (y > y0 + rlen)
00555 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
00556 if (t1->data[y][x] & mask){
00557 int xorbit;
00558 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00559 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00560 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
00561 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
00562 }
00563 }
00564 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
00565 }
00566 } else{
00567 for (y = y0; y < y0 + 4 && y < height; y++){
00568 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
00569 int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
00570 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
00571 if (t1->data[y][x] & mask){
00572 int xorbit;
00573 int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
00574 *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
00575 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
00576 ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
00577 }
00578 }
00579 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
00580 }
00581 }
00582 }
00583 }
00584
00585 static void encode_cblk(J2kEncoderContext *s, J2kT1Context *t1, J2kCblk *cblk, J2kTile *tile,
00586 int width, int height, int bandpos, int lev)
00587 {
00588 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
00589 int64_t wmsedec = 0;
00590
00591 for (y = 0; y < height+2; y++)
00592 memset(t1->flags[y], 0, (width+2)*sizeof(int));
00593
00594 for (y = 0; y < height; y++){
00595 for (x = 0; x < width; x++){
00596 if (t1->data[y][x] < 0){
00597 t1->flags[y+1][x+1] |= J2K_T1_SGN;
00598 t1->data[y][x] = -t1->data[y][x];
00599 }
00600 max = FFMAX(max, t1->data[y][x]);
00601 }
00602 }
00603
00604 if (max == 0){
00605 cblk->nonzerobits = 0;
00606 bpno = 0;
00607 } else{
00608 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
00609 bpno = cblk->nonzerobits - 1;
00610 }
00611
00612 ff_mqc_initenc(&t1->mqc, cblk->data);
00613
00614 for (passno = 0; bpno >= 0; passno++){
00615 nmsedec=0;
00616
00617 switch(pass_t){
00618 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
00619 break;
00620 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
00621 break;
00622 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
00623 break;
00624 }
00625
00626 cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
00627 wmsedec += (int64_t)nmsedec << (2*bpno);
00628 cblk->passes[passno].disto = wmsedec;
00629
00630 if (++pass_t == 3){
00631 pass_t = 0;
00632 bpno--;
00633 }
00634 }
00635 cblk->npasses = passno;
00636 cblk->ninclpasses = passno;
00637
00638
00639 cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
00640 }
00641
00642
00643
00644 static void putnumpasses(J2kEncoderContext *s, int n)
00645 {
00646 if (n == 1)
00647 put_num(s, 0, 1);
00648 else if (n == 2)
00649 put_num(s, 2, 2);
00650 else if (n <= 5)
00651 put_num(s, 0xc | (n-3), 4);
00652 else if (n <= 36)
00653 put_num(s, 0x1e0 | (n-6), 9);
00654 else
00655 put_num(s, 0xff80 | (n-37), 16);
00656 }
00657
00658
00659 static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno,
00660 uint8_t *expn, int numgbits)
00661 {
00662 int bandno, empty = 1;
00663
00664
00665 *s->buf = 0;
00666 s->bit_index = 0;
00667
00668
00669
00670
00671 for (bandno = 0; bandno < rlevel->nbands; bandno++){
00672 if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
00673 && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
00674 empty = 0;
00675 break;
00676 }
00677 }
00678
00679 put_bits(s, !empty, 1);
00680 if (empty){
00681 j2k_flush(s);
00682 return 0;
00683 }
00684
00685 for (bandno = 0; bandno < rlevel->nbands; bandno++){
00686 J2kBand *band = rlevel->band + bandno;
00687 J2kPrec *prec = band->prec + precno;
00688 int yi, xi, pos;
00689 int cblknw = prec->xi1 - prec->xi0;
00690
00691 if (band->coord[0][0] == band->coord[0][1]
00692 || band->coord[1][0] == band->coord[1][1])
00693 continue;
00694
00695 for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
00696 for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
00697 prec->cblkincl[pos].val = band->cblk[yi * cblknw + xi].ninclpasses == 0;
00698 tag_tree_update(prec->cblkincl + pos);
00699 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - band->cblk[yi * cblknw + xi].nonzerobits;
00700 tag_tree_update(prec->zerobits + pos);
00701 }
00702 }
00703
00704 for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
00705 for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
00706 int pad = 0, llen, length;
00707 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
00708
00709 if (s->buf_end - s->buf < 20)
00710 return -1;
00711
00712
00713 tag_tree_code(s, prec->cblkincl + pos, 1);
00714 if (!cblk->ninclpasses)
00715 continue;
00716
00717 tag_tree_code(s, prec->zerobits + pos, 100);
00718
00719 putnumpasses(s, cblk->ninclpasses);
00720
00721 length = cblk->passes[cblk->ninclpasses-1].rate;
00722 llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
00723 if (llen < 0){
00724 pad = -llen;
00725 llen = 0;
00726 }
00727
00728 put_bits(s, 1, llen);
00729 put_bits(s, 0, 1);
00730 put_num(s, length, av_log2(length)+1+pad);
00731 }
00732 }
00733 }
00734 j2k_flush(s);
00735 for (bandno = 0; bandno < rlevel->nbands; bandno++){
00736 J2kBand *band = rlevel->band + bandno;
00737 J2kPrec *prec = band->prec + precno;
00738 int yi, cblknw = prec->xi1 - prec->xi0;
00739 for (yi = prec->yi0; yi < prec->yi1; yi++){
00740 int xi;
00741 for (xi = prec->xi0; xi < prec->xi1; xi++){
00742 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
00743 if (cblk->ninclpasses){
00744 if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
00745 return -1;
00746 bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
00747 }
00748 }
00749 }
00750 }
00751 return 0;
00752 }
00753
00754 static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
00755 {
00756 int compno, reslevelno, ret;
00757 J2kCodingStyle *codsty = &s->codsty;
00758 J2kQuantStyle *qntsty = &s->qntsty;
00759
00760 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
00761
00762 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00763 for (compno = 0; compno < s->ncomponents; compno++){
00764 int precno;
00765 J2kResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
00766 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
00767 if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
00768 qntsty->nguardbits))
00769 return ret;
00770 }
00771 }
00772 }
00773 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
00774 return 0;
00775 }
00776
00777 static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
00778 {
00779 int passno, res = 0;
00780 for (passno = 0; passno < cblk->npasses; passno++){
00781 int dr;
00782 int64_t dd;
00783
00784 dr = cblk->passes[passno].rate
00785 - (res ? cblk->passes[res-1].rate:0);
00786 dd = cblk->passes[passno].disto
00787 - (res ? cblk->passes[res-1].disto:0);
00788
00789 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
00790 res = passno+1;
00791 }
00792 return res;
00793 }
00794
00795 static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
00796 {
00797 int compno, reslevelno, bandno, cblkno, lev;
00798 J2kCodingStyle *codsty = &s->codsty;
00799
00800 for (compno = 0; compno < s->ncomponents; compno++){
00801 J2kComponent *comp = tile->comp + compno;
00802
00803 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
00804 J2kResLevel *reslevel = comp->reslevel + reslevelno;
00805
00806 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
00807 int bandpos = bandno + (reslevelno > 0);
00808 J2kBand *band = reslevel->band + bandno;
00809
00810 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
00811 J2kCblk *cblk = band->cblk + cblkno;
00812
00813 cblk->ninclpasses = getcut(cblk, s->lambda,
00814 (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 13);
00815 }
00816 }
00817 }
00818 }
00819 }
00820
00821 static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
00822 {
00823 int compno, reslevelno, bandno, ret;
00824 J2kT1Context t1;
00825 J2kCodingStyle *codsty = &s->codsty;
00826 for (compno = 0; compno < s->ncomponents; compno++){
00827 J2kComponent *comp = s->tile[tileno].comp + compno;
00828
00829 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
00830 if (ret = ff_j2k_dwt_encode(&comp->dwt, comp->data))
00831 return ret;
00832 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
00833
00834 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
00835 J2kResLevel *reslevel = comp->reslevel + reslevelno;
00836
00837 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
00838 J2kBand *band = reslevel->band + bandno;
00839 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
00840 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
00841 y0 = yy0;
00842 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
00843 band->coord[1][1]) - band->coord[1][0] + yy0;
00844
00845 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
00846 continue;
00847
00848 bandpos = bandno + (reslevelno > 0);
00849
00850 for (cblky = 0; cblky < band->cblkny; cblky++){
00851 if (reslevelno == 0 || bandno == 1)
00852 xx0 = 0;
00853 else
00854 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
00855 x0 = xx0;
00856 xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
00857 band->coord[0][1]) - band->coord[0][0] + xx0;
00858
00859 for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
00860 int y, x;
00861 if (codsty->transform == FF_DWT53){
00862 for (y = yy0; y < yy1; y++){
00863 int *ptr = t1.data[y-yy0];
00864 for (x = xx0; x < xx1; x++){
00865 *ptr++ = comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
00866 }
00867 }
00868 } else{
00869 for (y = yy0; y < yy1; y++){
00870 int *ptr = t1.data[y-yy0];
00871 for (x = xx0; x < xx1; x++){
00872 *ptr = (comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
00873 *ptr = (int64_t)*ptr * (int64_t)(8192 * 8192 / band->stepsize) >> 13 - NMSEDEC_FRACBITS;
00874 *ptr++;
00875 }
00876 }
00877 }
00878 encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
00879 bandpos, codsty->nreslevels - reslevelno - 1);
00880 xx0 = xx1;
00881 xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
00882 }
00883 yy0 = yy1;
00884 yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
00885 }
00886 }
00887 }
00888 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
00889 }
00890
00891 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
00892 truncpasses(s, tile);
00893 if (ret = encode_packets(s, tile, tileno))
00894 return ret;
00895 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
00896 return 0;
00897 }
00898
00899 static void cleanup(J2kEncoderContext *s)
00900 {
00901 int tileno, compno;
00902 J2kCodingStyle *codsty = &s->codsty;
00903
00904 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00905 for (compno = 0; compno < s->ncomponents; compno++){
00906 J2kComponent *comp = s->tile[tileno].comp + compno;
00907 ff_j2k_cleanup(comp, codsty);
00908 }
00909 av_freep(&s->tile[tileno].comp);
00910 }
00911 av_freep(&s->tile);
00912 }
00913
00914 static void reinit(J2kEncoderContext *s)
00915 {
00916 int tileno, compno;
00917 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00918 J2kTile *tile = s->tile + tileno;
00919 for (compno = 0; compno < s->ncomponents; compno++)
00920 ff_j2k_reinit(tile->comp + compno, &s->codsty);
00921 }
00922 }
00923
00924 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00925 const AVFrame *pict, int *got_packet)
00926 {
00927 int tileno, ret;
00928 J2kEncoderContext *s = avctx->priv_data;
00929
00930 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
00931 return ret;
00932
00933
00934 s->buf = s->buf_start = pkt->data;
00935 s->buf_end = pkt->data + pkt->size;
00936
00937 s->picture = *pict;
00938 avctx->coded_frame= &s->picture;
00939
00940 s->lambda = s->picture.quality * LAMBDA_SCALE;
00941
00942 copy_frame(s);
00943 reinit(s);
00944
00945 if (s->buf_end - s->buf < 2)
00946 return -1;
00947 bytestream_put_be16(&s->buf, J2K_SOC);
00948 if (ret = put_siz(s))
00949 return ret;
00950 if (ret = put_cod(s))
00951 return ret;
00952 if (ret = put_qcd(s, 0))
00953 return ret;
00954
00955 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
00956 uint8_t *psotptr;
00957 if (!(psotptr = put_sot(s, tileno)))
00958 return -1;
00959 if (s->buf_end - s->buf < 2)
00960 return -1;
00961 bytestream_put_be16(&s->buf, J2K_SOD);
00962 if (ret = encode_tile(s, s->tile + tileno, tileno))
00963 return ret;
00964 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
00965 }
00966 if (s->buf_end - s->buf < 2)
00967 return -1;
00968 bytestream_put_be16(&s->buf, J2K_EOC);
00969
00970 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
00971 pkt->size = s->buf - s->buf_start;
00972 pkt->flags |= AV_PKT_FLAG_KEY;
00973 *got_packet = 1;
00974
00975 return 0;
00976 }
00977
00978 static av_cold int j2kenc_init(AVCodecContext *avctx)
00979 {
00980 int i, ret;
00981 J2kEncoderContext *s = avctx->priv_data;
00982 J2kCodingStyle *codsty = &s->codsty;
00983 J2kQuantStyle *qntsty = &s->qntsty;
00984
00985 s->avctx = avctx;
00986 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
00987
00988
00989
00990 codsty->log2_prec_width = 15;
00991 codsty->log2_prec_height = 15;
00992 codsty->nreslevels = 7;
00993 codsty->log2_cblk_width = 4;
00994 codsty->log2_cblk_height = 4;
00995 codsty->transform = 1;
00996
00997 qntsty->nguardbits = 1;
00998
00999 s->tile_width = 256;
01000 s->tile_height = 256;
01001
01002 if (codsty->transform == FF_DWT53)
01003 qntsty->quantsty = J2K_QSTY_NONE;
01004 else
01005 qntsty->quantsty = J2K_QSTY_SE;
01006
01007 s->width = avctx->width;
01008 s->height = avctx->height;
01009
01010 for (i = 0; i < 3; i++)
01011 s->cbps[i] = 8;
01012
01013 if (avctx->pix_fmt == PIX_FMT_RGB24){
01014 s->ncomponents = 3;
01015 } else if (avctx->pix_fmt == PIX_FMT_GRAY8){
01016 s->ncomponents = 1;
01017 } else{
01018 s->planar = 1;
01019 s->ncomponents = 3;
01020 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
01021 s->chroma_shift, s->chroma_shift + 1);
01022 }
01023
01024 ff_j2k_init_tier1_luts();
01025
01026 init_luts();
01027
01028 init_quantization(s);
01029 if (ret=init_tiles(s))
01030 return ret;
01031
01032 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
01033
01034 return 0;
01035 }
01036
01037 static int j2kenc_destroy(AVCodecContext *avctx)
01038 {
01039 J2kEncoderContext *s = avctx->priv_data;
01040
01041 cleanup(s);
01042 return 0;
01043 }
01044
01045 AVCodec ff_jpeg2000_encoder = {
01046 .name = "j2k",
01047 .type = AVMEDIA_TYPE_VIDEO,
01048 .id = AV_CODEC_ID_JPEG2000,
01049 .priv_data_size = sizeof(J2kEncoderContext),
01050 .init = j2kenc_init,
01051 .encode2 = encode_frame,
01052 .close = j2kenc_destroy,
01053 .capabilities = CODEC_CAP_EXPERIMENTAL,
01054 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
01055 .pix_fmts = (const enum PixelFormat[]) {
01056 PIX_FMT_RGB24, PIX_FMT_YUV444P, PIX_FMT_GRAY8,
01057
01058
01059
01060 PIX_FMT_NONE
01061 }
01062 };