#include "opj_includes.h"
void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
- int tileno, compno, resno, bandno, precno, cblkno;
+ int tileno, compno, resno, bandno, precno;//, cblkno;
fprintf(fd, "image {\n");
fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
fprintf(fd,
" x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
+ /*
for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
fprintf(fd, " cblk {\n");
cblk->x0, cblk->y0, cblk->x1, cblk->y1);
fprintf(fd, " }\n");
}
+ */
fprintf(fd, " }\n");
}
fprintf(fd, " }\n");
prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
- prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cw * prc->ch) * sizeof(opj_tcd_cblk_t));
+ prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
prc->incltree = tgt_create(prc->cw, prc->ch);
prc->imsbtree = tgt_create(prc->cw, prc->ch);
int cblkxend = cblkxstart + (1 << cblkwidthexpn);
int cblkyend = cblkystart + (1 << cblkheightexpn);
- opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+ opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
/* code-block size (global) */
cblk->x0 = int_max(cblkxstart, prc->x0);
cblk->y0 = int_max(cblkystart, prc->y0);
cblk->x1 = int_min(cblkxend, prc->x1);
cblk->y1 = int_min(cblkyend, prc->y1);
+ cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
+ /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
+ cblk->data += 2;
+ cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
+ cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
}
}
}
}
void tcd_free_encode(opj_tcd_t *tcd) {
- int tileno, compno, resno, bandno, precno;
+ int tileno, compno, resno, bandno, precno, cblkno;
for (tileno = 0; tileno < 1; tileno++) {
opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
tgt_destroy(prc->imsbtree);
prc->imsbtree = NULL;
}
- opj_free(prc->cblks);
- prc->cblks = NULL;
+ for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
+ opj_free(prc->cblks.enc[cblkno].data - 2);
+ opj_free(prc->cblks.enc[cblkno].layers);
+ opj_free(prc->cblks.enc[cblkno].passes);
+ }
+ opj_free(prc->cblks.enc);
} /* for (precno */
opj_free(band->precincts);
band->precincts = NULL;
/* Modification of the RATE >> */
for (j = 0; j < tcp->numlayers; j++) {
tcp->rates[j] = tcp->rates[j] ?
- ((float) (tile->numcomps
- * (tile->x1 - tile->x0)
- * (tile->y1 - tile->y0)
- * image->comps[0].prec))/
- (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
- : 0;
+ cp->tp_on ?
+ (((float) (tile->numcomps
+ * (tile->x1 - tile->x0)
+ * (tile->y1 - tile->y0)
+ * image->comps[0].prec))
+ /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
+ :
+ ((float) (tile->numcomps
+ * (tile->x1 - tile->x0)
+ * (tile->y1 - tile->y0)
+ * image->comps[0].prec))/
+ (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
+ : 0;
if (tcp->rates[j]) {
if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
- opj_free(prc->cblks);
- prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
+ opj_free(prc->cblks.enc);
+ prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
if (prc->incltree != NULL) {
tgt_destroy(prc->incltree);
int cblkxend = cblkxstart + (1 << cblkwidthexpn);
int cblkyend = cblkystart + (1 << cblkheightexpn);
- opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
-
+ opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
+
/* code-block size (global) */
cblk->x0 = int_max(cblkxstart, prc->x0);
cblk->y0 = int_max(cblkystart, prc->y0);
cblk->x1 = int_min(cblkxend, prc->x1);
cblk->y1 = int_min(cblkyend, prc->y1);
+ cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
+ /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
+ cblk->data += 2;
+ cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
+ cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
}
} /* precno */
} /* bandno */
tileno = cp->tileno[j];
tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
tile->numcomps = image->numcomps;
- tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
+ tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
}
for (i = 0; i < image->numcomps; i++) {
x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
}
-
- w = x1 - x0;
- h = y1 - y0;
-
- image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
+
+ w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
+ h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
+
image->comps[i].w = w;
image->comps[i].h = h;
image->comps[i].x0 = x0;
void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info) {
int compno, resno, bandno, precno, cblkno;
- unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
opj_tcp_t *tcp;
opj_tcd_tile_t *tile;
tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
-
- tilec->data = (int*) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
+
tilec->numresolutions = tccp->numresolutions;
tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
numbps = image->comps[compno].prec + gain;
- band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
+ band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
-
- prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
-
+
+ prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
+
prc->incltree = tgt_create(prc->cw, prc->ch);
prc->imsbtree = tgt_create(prc->cw, prc->ch);
int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
int cblkxend = cblkxstart + (1 << cblkwidthexpn);
int cblkyend = cblkystart + (1 << cblkheightexpn);
-
+
+ opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
+ cblk->data = NULL;
+ cblk->segs = NULL;
/* code-block size (global) */
- opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
cblk->x0 = int_max(cblkxstart, prc->x0);
cblk->y0 = int_max(cblkystart, prc->y0);
cblk->x1 = int_min(cblkxend, prc->x1);
cblk->y1 = int_min(cblkyend, prc->y1);
+ cblk->numsegs = 0;
}
} /* precno */
} /* bandno */
for (precno = 0; precno < res->pw * res->ph; precno++) {
opj_tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
- opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+ opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
opj_tcd_layer_t *layer = &cblk->layers[layno];
int n;
int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
for (precno = 0; precno < res->pw * res->ph; precno++) {
opj_tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
- opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+ opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
opj_tcd_layer_t *layer = &cblk->layers[layno];
int n;
opj_tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
- opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+ opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
for (passno = 0; passno < cblk->totalpasses; passno++) {
opj_tcd_pass_t *pass = &cblk->passes[passno];
==> possible to have some lossy layers and the last layer for sure lossless */
if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
+ double thresh = 0;
- for (i = 0; i < 32; i++) {
- double thresh = (lo + hi) / 2;
+ for (i = 0; i < 128; i++) {
int l = 0;
double distoachieved = 0; /* fixed_quality */
+ thresh = (lo + hi) / 2;
tcd_makelayer(tcd, layno, thresh, 0);
if (cp->fixed_quality) { /* fixed_quality */
if(cp->cinema){
- l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
+ l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
if (l == -999) {
lo = thresh;
continue;
lo = thresh;
}
} else {
- l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
+ l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
/* TODO: what to do with l ??? seek / tell ??? */
/* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
if (l == -999) {
}
}
success = 1;
- goodthresh = stable_thresh;
+ goodthresh = stable_thresh == 0? thresh : stable_thresh;
t2_destroy(t2);
} else {
success = 1;
cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
}
- cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numcomps * cstr_info->numlayers * numpacks * sizeof(opj_packet_info_t));
+ cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
}
/* << INDEX */
}
t2 = t2_create(tcd->cinfo, image, cp);
- l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS);
+ l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS,tcd->cur_totnum_tp);
t2_destroy(t2);
/*---------------CLEAN-------------------*/
t1_time = opj_clock(); /* time needed to decode a tile */
t1 = t1_create(tcd->cinfo);
- t1_decode_cblks(t1, tile, tcd->tcp);
+ for (compno = 0; compno < tile->numcomps; ++compno) {
+ opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
+ /* The +3 is headroom required by the vectorized DWT */
+ tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
+ t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
+ }
t1_destroy(t1);
t1_time = opj_clock() - t1_time;
opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
dwt_time = opj_clock(); /* time needed to decode a tile */
for (compno = 0; compno < tile->numcomps; compno++) {
opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+ int numres2decode;
+
if (tcd->cp->reduce != 0) {
tcd->image->comps[compno].resno_decoded =
tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
- }
-
- if (tcd->tcp->tccps[compno].qmfbid == 1) {
- dwt_decode(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
- } else {
- dwt_decode_real(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
+ if (tcd->image->comps[compno].resno_decoded < 0) {
+ opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
+ " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
+ return false;
+ }
}
- if (tile->comps[compno].numresolutions > 0) {
- tcd->image->comps[compno].factor = tile->comps[compno].numresolutions - (tcd->image->comps[compno].resno_decoded + 1);
+ numres2decode = tcd->image->comps[compno].resno_decoded + 1;
+ if(numres2decode > 0){
+ if (tcd->tcp->tccps[compno].qmfbid == 1) {
+ dwt_decode(tilec, numres2decode);
+ } else {
+ dwt_decode_real(tilec, numres2decode);
+ }
}
}
dwt_time = opj_clock() - dwt_time;
opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
-
+
/*----------------MCT-------------------*/
-
+
if (tcd->tcp->mct) {
+ int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
if (tcd->tcp->tccps[0].qmfbid == 1) {
- mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
- (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
+ mct_decode(
+ tile->comps[0].data,
+ tile->comps[1].data,
+ tile->comps[2].data,
+ n);
} else {
- mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
- (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
+ mct_decode_real(
+ (float*)tile->comps[0].data,
+ (float*)tile->comps[1].data,
+ (float*)tile->comps[2].data,
+ n);
}
}
-
+
/*---------------TILE-------------------*/
-
- for (compno = 0; compno < tile->numcomps; compno++) {
- opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
- opj_tcd_resolution_t *res = &tilec->resolutions[tcd->image->comps[compno].resno_decoded];
- int adjust = tcd->image->comps[compno].sgnd ? 0 : 1 << (tcd->image->comps[compno].prec - 1);
- int min = tcd->image->comps[compno].sgnd ?
- -(1 << (tcd->image->comps[compno].prec - 1)) : 0;
- int max = tcd->image->comps[compno].sgnd ?
- (1 << (tcd->image->comps[compno].prec - 1)) - 1 : (1 << tcd->image->comps[compno].prec) - 1;
-
+
+ for (compno = 0; compno < tile->numcomps; ++compno) {
+ opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
+ opj_image_comp_t* imagec = &tcd->image->comps[compno];
+ opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
+ int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
+ int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
+ int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
+
int tw = tilec->x1 - tilec->x0;
- int w = tcd->image->comps[compno].w;
-
- int i, j;
- int offset_x = int_ceildivpow2(tcd->image->comps[compno].x0, tcd->image->comps[compno].factor);
- int offset_y = int_ceildivpow2(tcd->image->comps[compno].y0, tcd->image->comps[compno].factor);
-
- for (j = res->y0; j < res->y1; j++) {
- for (i = res->x0; i < res->x1; i++) {
- int v;
- float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw]) / 8192.0);
+ int w = imagec->w;
- if (tcd->tcp->tccps[compno].qmfbid == 1) {
- v = tilec->data[i - res->x0 + (j - res->y0) * tw];
- } else {
- int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
- v = ((tmp < 0) ? -tmp2:tmp2);
+ int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
+ int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
+
+ int i, j;
+ if(!imagec->data){
+ imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
+ }
+ if(tcd->tcp->tccps[compno].qmfbid == 1) {
+ for(j = res->y0; j < res->y1; ++j) {
+ for(i = res->x0; i < res->x1; ++i) {
+ int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
+ v += adjust;
+ imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
+ }
+ }
+ }else{
+ for(j = res->y0; j < res->y1; ++j) {
+ for(i = res->x0; i < res->x1; ++i) {
+ float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
+ int v = lrintf(tmp);
+ v += adjust;
+ imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
}
- v += adjust;
-
- tcd->image->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
}
}
+ opj_aligned_free(tilec->data);
}
-
+
tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
- for (compno = 0; compno < tile->numcomps; compno++) {
- opj_aligned_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
- tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
- }
-
if (eof) {
return false;
}
void tcd_free_decode(opj_tcd_t *tcd) {
opj_tcd_image_t *tcd_image = tcd->tcd_image;
- if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles);
+ opj_free(tcd_image->tiles);
}
void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
opj_tcd_band_t *band = &res->bands[bandno];
for (precno = 0; precno < res->ph * res->pw; precno++) {
opj_tcd_precinct_t *prec = &band->precincts[precno];
- if (prec->cblks != NULL) opj_free(prec->cblks);
if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
if (prec->incltree != NULL) tgt_destroy(prec->incltree);
}
- if (band->precincts != NULL) opj_free(band->precincts);
+ opj_free(band->precincts);
}
}
- if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
+ opj_free(tilec->resolutions);
}
- if (tile->comps != NULL) opj_free(tile->comps);
+ opj_free(tile->comps);
}
-
-
-