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