diff --git a/ChangeLog b/ChangeLog index 1980d550..ae8d6622 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,17 @@ What's New for OpenJPEG ! : changed + : added +March 29, 2007 +* [Parvatha] Enable accepting file names with `-´ symbol .Modification getopt.c +* [Parvatha] Rsiz profile name generation to be STD_RSIZ for profiles which are not DCI compliant.Modification in image_to_j2k.c +! [Parvatha] renamed convert_progression_order to j2k_convert_progression_order. Modification j2k.c +* [Parvatha] Calculation of number of tile part in each tile in j2k_calculate_tp. Modification j2k.c +! [Parvatha] j2k_setup_encoder to set bit rate limitation for digital cinema compliance with quality option. Modification in j2k.c +* [Parvatha] Equation to check multiple tile precincts. Modification pi.c +! [Parvatha] array size generation of pi->include in pi_initialise_encode().Modification in pi.c +* [Parvatha] modification in pi_create_encode for tile part generation.Modification in pi.c ++ [Parvatha] In tcd_rateallocate a variable stable_threshold which holds the valid threshold value. This is used to avoid error in case of a wrong threshold value in the last iteration. Modification in tcd.c. + March 28, 2007 * [FOD] Fixed an historical bug in t1.c that leaded to the inclusion of useless 0xFF in the codestream. Thanks to Sylvain, Pascal and Parvatha ! diff --git a/codec/compat/getopt.c b/codec/compat/getopt.c index 6bb31c86..3ecbde15 100644 --- a/codec/compat/getopt.c +++ b/codec/compat/getopt.c @@ -125,7 +125,7 @@ int getopt(int nargc, char *const *nargv, const char *ostr) { int getopt_long(int argc, char * const argv[], const char *optstring, -struct option *longopts, int *longindex, int totlen) { +struct option *longopts, int totlen) { static int lastidx,lastofs; char *tmp; int i,len; @@ -147,24 +147,28 @@ again: if(strlen(arg)>1){ for (i=0;iname,arg)) { /* match */ - if (longindex) *longindex=o-longopts; - if (o->has_arg>0) { - optarg=argv[optind+1]; - if(optarg){ - if (strchr(optarg,'-')){ /* Has read next input parameter: No arg for current parameter */ - if (opterr) { - fprintf(stderr,"%s: option requires an argument\n",arg); - return (BADCH); - } - } - } - if (!optarg && o->has_arg==1) { /* no argument there */ + if (o->has_arg == 0) { + if (!(argv[optind+1][0]=='-')){ + fprintf(stderr,"%s: option does not require an argument. Ignoring %s\n",arg,argv[optind+1]); + ++optind; + } + }else{ + optarg=argv[optind+1]; + if(optarg){ + if (optarg[0] == '-'){ /* Has read next input parameter: No arg for current parameter */ if (opterr) { - fprintf(stderr,"%s: option requires an argument %c\n",arg, optopt); + fprintf(stderr,"%s: option requires an argument\n",arg); return (BADCH); } } - ++optind; + } + if (!optarg && o->has_arg==1) { /* no argument there */ + if (opterr) { + fprintf(stderr,"%s: option requires an argument \n",arg); + return (BADCH); + } + } + ++optind; } ++optind; if (o->flag) @@ -195,7 +199,7 @@ again: } optarg=argv[optind+1]; if(optarg){ - if (strchr(optarg,'-')){ /* Has read next input parameter: No arg for current parameter */ + if (optarg[0] == '-'){ /* Has read next input parameter: No arg for current parameter */ if (opterr) { fprintf(stderr,"%s: option requires an argument\n",arg); return (BADCH); @@ -210,16 +214,16 @@ again: } ++optind; }else {/*Argument not expected*/ - ++lastofs; - return optopt; + ++lastofs; + return optopt; } found: - ++optind; - return optopt; + ++optind; + return optopt; } else { /* not found */ - fprintf(stderr,"Invalid option %s\n",arg); - ++optind; - return (BADCH); + fprintf(stderr,"Invalid option %s\n",arg); + ++optind; + return (BADCH); }//end of not found }// end of single character @@ -227,4 +231,4 @@ found: fprintf(stderr,"Invalid option %s\n"); ++optind; return (BADCH);; - }//end function +}//end function diff --git a/codec/compat/getopt.h b/codec/compat/getopt.h index 4492e850..3477e069 100644 --- a/codec/compat/getopt.h +++ b/codec/compat/getopt.h @@ -23,7 +23,7 @@ extern char *optarg; extern int getopt(int nargc, char *const *nargv, const char *ostr); extern int getopt_long(int argc, char * const argv[], const char *optstring, - const struct option *longopts, int *longindex,int totlen); + const struct option *longopts, int totlen); #endif /* _GETOPT_H_ */ diff --git a/codec/image_to_j2k.c b/codec/image_to_j2k.c index 3d423e77..f626833e 100644 --- a/codec/image_to_j2k.c +++ b/codec/image_to_j2k.c @@ -450,24 +450,24 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image){ switch (parameters->cp_cinema){ case CINEMA2K_24: case CINEMA2K_48: - parameters->cp_rsiz = CINEMA2K; if(parameters->numresolution > 6){ parameters->numresolution = 6; } if (!((image->comps[0].w == 2048) & (image->comps[0].h == 1080))){ - fprintf(stdout,"Image coordinates is not 2K, %d x %d\n",image->comps[0].w,image->comps[0].h); + fprintf(stdout,"Image coordinates %d x %d is not 2K compliant.\nDCI 2K compliance requires that atleast one of coordinates match 2048 x 1080\n",image->comps[0].w,image->comps[0].h); + parameters->cp_rsiz = STD_RSIZ; } break; case CINEMA4K_24: - parameters->cp_rsiz = CINEMA4K; if(parameters->numresolution < 1){ parameters->numresolution = 1; - }else if(parameters->numresolution > 7){ + }else if((parameters->numresolution < 7) || (parameters->numresolution > 7)){ parameters->numresolution = 7; } if (!((image->comps[0].w == 4096) & (image->comps[0].h == 2160))){ - fprintf(stdout,"Image coordinates is not 4K, %d x %d\n",image->comps[0].w,image->comps[0].h); + fprintf(stdout,"Image coordinates %d x %d is not 4K compliant.\nDCI 4K compliance requires that atleast one of coordinates match 4096 x 2160\n",image->comps[0].w,image->comps[0].h); + parameters->cp_rsiz = STD_RSIZ; } break; } @@ -493,22 +493,21 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image){ case CINEMA2K_48: for(i=0;itcp_numlayers;i++){ + temp_rate = 0 ; if (parameters->tcp_rates[i]== 0){ parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy); }else{ - parameters->tcp_rates[i]=((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ + temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ (parameters->tcp_rates[i] * 8 * image->comps[0].dx * image->comps[0].dy); - if (parameters->tcp_rates[i] > CINEMA_48_CS ){ + if (temp_rate > CINEMA_48_CS ){ parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy); } } } break; - -} - + } parameters->cp_disto_alloc = 1; } @@ -517,8 +516,8 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image){ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,img_fol_t *img_fol) { int i, j,totlen; option_t long_option[]={ - {"cinema2k",REQ_ARG, NULL ,'w'}, - {"cinema4k",NO_ARG, NULL ,'y'}, + {"cinema2K",REQ_ARG, NULL ,'w'}, + {"cinema4K",NO_ARG, NULL ,'y'}, {"ImgDir",REQ_ARG, NULL ,'z'}, {"TP",REQ_ARG, NULL ,'v'}, {"SOP",NO_ARG, NULL ,'S'}, @@ -538,7 +537,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,i img_fol->set_out_format=0; while (1) { - int c = getopt_long(argc, argv, optlist,long_option,NULL,totlen); + int c = getopt_long(argc, argv, optlist,long_option,totlen); /* <cp_rsiz = CINEMA2K; } break; @@ -953,6 +953,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,i { parameters->cp_cinema = CINEMA4K_24; fprintf(stdout,"CINEMA 4K compliant codestream\n"); + parameters->cp_rsiz = CINEMA4K; } break; @@ -1292,7 +1293,8 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,i /* check for possible errors */ if (parameters->cp_cinema){ - if(parameters->tcp_numlayers > 0){ + if(parameters->tcp_numlayers > 1){ + parameters->cp_rsiz = STD_RSIZ; fprintf(stdout,"Warning: DC profiles do not allow more than one quality layer. The codestream created will not be compliant with the DC profile\n"); } } @@ -1604,7 +1606,7 @@ int main(int argc, char **argv) { } fwrite(cio->buffer, 1, codestream_length, f); fclose(f); - + fprintf(stderr,"Generated outfile %s\n",parameters.outfile); /* close and free the byte stream */ opj_cio_close(cio); diff --git a/codec/j2k_to_image.c b/codec/j2k_to_image.c index ccc22471..bdf6de74 100644 --- a/codec/j2k_to_image.c +++ b/codec/j2k_to_image.c @@ -240,7 +240,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i totlen=sizeof(long_option); img_fol->set_out_format = 0; while (1) { - int c = getopt_long(argc, argv,optlist,long_option,NULL,totlen); + int c = getopt_long(argc, argv,optlist,long_option,totlen); if (c == -1) break; switch (c) { @@ -678,7 +678,7 @@ int main(int argc, char **argv) { fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); } else { - fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); + fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; @@ -687,7 +687,7 @@ int main(int argc, char **argv) { fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); } else { - fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); + fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; @@ -696,7 +696,7 @@ int main(int argc, char **argv) { fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); } else { - fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); + fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; @@ -705,7 +705,7 @@ int main(int argc, char **argv) { fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); } else { - fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); + fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; } diff --git a/libopenjpeg/j2k.c b/libopenjpeg/j2k.c index 04a3df9a..e38cb0ba 100644 --- a/libopenjpeg/j2k.c +++ b/libopenjpeg/j2k.c @@ -245,10 +245,10 @@ j2k_prog_order_t j2k_prog_order_list[] = { {PCRL, "PCRL"}, {RLCP, "RLCP"}, {RPCL, "RPCL"}, - {-1, NULL} + {-1, ""} }; -char *convert_progression_order(OPJ_PROG_ORDER prg_order){ +char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){ j2k_prog_order_t *po; for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){ if(po->enum_prog == prg_order){ @@ -332,12 +332,12 @@ void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_t * cp) { } /* ----------------------------------------------------------------------- */ -int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){ +static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){ char *prog; int i; int tpnum=1,tpend=0; opj_tcp_t *tcp = &cp->tcps[tileno]; - prog = convert_progression_order(tcp->prg); + prog = j2k_convert_progression_order(tcp->prg); if(cp->tp_on == 1){ for(i=0;i<4;i++){ @@ -368,19 +368,22 @@ int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){ } /** mem allocation for TLM marker*/ -int j2k_generate_tlm(opj_cp_t *cp,int img_numcomp,opj_image_t *image ){ +int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){ int pino,tileno,maxres=0,totnum_tp=0; + j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int)); for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { + int cur_totnum_tp = 0; opj_tcp_t *tcp = &cp->tcps[tileno]; for(pino = 0; pino <= tcp->numpocs; pino++) { int tp_num=0; - opj_pi_iterator_t *pi = (opj_pi_iterator_t *) opj_malloc(sizeof(opj_pi_iterator_t)); - pi = pi_initialise_encode(image, cp, tileno,pino); + opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,pino); if(!pi) { return -1;} tp_num = j2k_get_num_tp(cp,pino,tileno); totnum_tp = totnum_tp + tp_num; + cur_totnum_tp = cur_totnum_tp + tp_num; pi_destroy(pi, cp, tileno); } + j2k->cur_totnum_tp[tileno] = cur_totnum_tp; } return totnum_tp; } @@ -1184,7 +1187,7 @@ static void j2k_write_sot(opj_j2k_t *j2k) { cio_write(cio, j2k->curtileno, 2); /* Isot */ cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */ cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */ - cio_write(cio, j2k->cur_totnum_tp, 1); /* TNsot */ + cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */ len = cio_tell(cio) - lenp; cio_seek(cio, lenp); cio_write(cio, len, 2); /* Lsot */ @@ -1306,8 +1309,9 @@ static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) { opj_cp_t *cp = j2k->cp; opj_cio_t *cio = j2k->cio; + tcd->tp_num = j2k->tp_num ; tcd->cur_tp_num = j2k->cur_tp_num; - tcd->cur_totnum_tp = j2k->cur_totnum_tp; + tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno]; cio_write(cio, J2K_MS_SOD, 2); if (j2k->curtileno == 0) { @@ -2026,10 +2030,17 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_ opj_tcp_t *tcp = &cp->tcps[tileno]; tcp->numlayers = parameters->tcp_numlayers; for (j = 0; j < tcp->numlayers; j++) { - if (cp->fixed_quality) { /* add fixed_quality */ - tcp->distoratio[j] = parameters->tcp_distoratio[j]; - } else { + if(cp->cinema){ + if (cp->fixed_quality) { + tcp->distoratio[j] = parameters->tcp_distoratio[j]; + } tcp->rates[j] = parameters->tcp_rates[j]; + }else{ + if (cp->fixed_quality) { /* add fixed_quality */ + tcp->distoratio[j] = parameters->tcp_distoratio[j]; + } else { + tcp->rates[j] = parameters->tcp_rates[j]; + } } } tcp->csty = parameters->csty; @@ -2432,9 +2443,9 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) } /* << INDEX */ + j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k); /* TLM Marker*/ if(cp->cinema){ - j2k->totnum_tp = j2k_generate_tlm(cp,image->numcomps,image); j2k_write_tlm(j2k); } /**** Main Header ENDS here ***/ @@ -2452,6 +2463,7 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th); j2k->curtileno = tileno; + j2k->cur_tp_num = 0; /* initialisation before tile encoding */ if (tileno == 0) { @@ -2469,18 +2481,14 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) for(pino = 0; pino <= tcp->numpocs; pino++) { int tot_num_tp; - opj_pi_iterator_t *pi = NULL; tcd->cur_pino=pino; - tcd->pi = pi_initialise_encode(image, cp, tileno,pino); - pi=tcd->pi; /*Get number of tile parts*/ tot_num_tp = j2k_get_num_tp(cp,pino,tileno); tcd->tp_pos = cp->tp_pos; for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){ - j2k->cur_tp_num = tilepartno; - j2k->cur_totnum_tp = tot_num_tp; + j2k->tp_num = tilepartno; j2k_write_sot(j2k); if(j2k->cur_tp_num == 0 && cp->cinema == 0){ @@ -2494,8 +2502,9 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) } j2k_write_sod(j2k, tcd); + j2k->cur_tp_num ++; } - pi_destroy(pi, cp, tileno); + } /* INDEX >> */ if(image_info && image_info->index_on) { diff --git a/libopenjpeg/j2k.h b/libopenjpeg/j2k.h index 639fbcef..6d76ec1c 100644 --- a/libopenjpeg/j2k.h +++ b/libopenjpeg/j2k.h @@ -391,10 +391,12 @@ typedef struct opj_j2k { int state; /** number of the tile curently concern by coding/decoding */ int curtileno; + /** Tile part number*/ + int tp_num; /** Tilepart number currently coding*/ int cur_tp_num; /** Total number of tileparts of the current tile*/ - int cur_totnum_tp; + int *cur_totnum_tp; /** locate the start position of the TLM marker after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length. @@ -495,11 +497,7 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_ /** Converts an enum type progression order to string type */ -char *convert_progression_order(OPJ_PROG_ORDER prg_order); -/** -Generates the number of tile parts for the current tile -*/ -int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno); +char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order); /** Encode an image into a JPEG-2000 codestream @param j2k J2K compressor handle diff --git a/libopenjpeg/openjpeg.h b/libopenjpeg/openjpeg.h index 12349532..23f57595 100644 --- a/libopenjpeg/openjpeg.h +++ b/libopenjpeg/openjpeg.h @@ -113,21 +113,28 @@ braindamage below. enum definitions ========================================================== */ - +/** +Rsiz Capabilities +*/ typedef enum RSIZ_CAPABILITIES { - STD_RSIZ = 0, + STD_RSIZ = 0, /** Standard JPEG2000 profile*/ CINEMA2K = 3, /** Profile name for a 2K image*/ CINEMA4K = 4 /** Profile name for a 4K image*/ } OPJ_RSIZ_CAPABILITIES; +/** +Digital cinema operation mode +*/ typedef enum CINEMA_MODE { - OFF = 0, - CINEMA2K_24 = 1, - CINEMA2K_48 = 2, - CINEMA4K_24 = 3 + OFF = 0, /** Not Digital Cinema*/ + CINEMA2K_24 = 1, /** 2K Digital Cinema at 24 fps*/ + CINEMA2K_48 = 2, /** 2K Digital Cinema at 48 fps*/ + CINEMA4K_24 = 3 /** 4K Digital Cinema at 24 fps*/ }OPJ_CINEMA_MODE; -/** Progression order */ +/** +Progression order +*/ typedef enum PROG_ORDER { PROG_UNKNOWN = -1, /**< place-holder */ LRCP = 0, /**< layer-resolution-component-precinct order */ diff --git a/libopenjpeg/pi.c b/libopenjpeg/pi.c index 9cd92d89..f770b215 100644 --- a/libopenjpeg/pi.c +++ b/libopenjpeg/pi.c @@ -92,7 +92,7 @@ static bool pi_next_lrcp(opj_pi_iterator_t * pi) { pi->first = 0; } - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { @@ -133,7 +133,7 @@ static bool pi_next_rlcp(opj_pi_iterator_t * pi) { } for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { comp = &pi->comps[pi->compno]; if (pi->resno >= comp->numresolutions) { @@ -209,11 +209,11 @@ if (!pi->tp_on){ try1 = int_ceildiv(pi->ty1, comp->dy << levelno); rpx = res->pdx + levelno; rpy = res->pdy + levelno; - if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { - continue; + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){ + continue; } - if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { - continue; + if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){ + continue; } if ((res->pw==0)||(res->pw==0)) continue; @@ -225,7 +225,7 @@ if (!pi->tp_on){ prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) - int_floordivpow2(try0, res->pdy); pi->precno = prci + prcj * res->pw; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; if (!pi->include[index]) { pi->include[index] = 1; @@ -290,11 +290,11 @@ static bool pi_next_pcrl(opj_pi_iterator_t * pi) { try1 = int_ceildiv(pi->ty1, comp->dy << levelno); rpx = res->pdx + levelno; rpy = res->pdy + levelno; - if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { - continue; + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){ + continue; } - if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { - continue; + if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){ + continue; } if ((res->pw==0)||(res->pw==0)) continue; @@ -306,7 +306,7 @@ static bool pi_next_pcrl(opj_pi_iterator_t * pi) { prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) - int_floordivpow2(try0, res->pdy); pi->precno = prci + prcj * res->pw; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; if (!pi->include[index]) { pi->include[index] = 1; @@ -369,11 +369,11 @@ static bool pi_next_cprl(opj_pi_iterator_t * pi) { try1 = int_ceildiv(pi->ty1, comp->dy << levelno); rpx = res->pdx + levelno; rpy = res->pdy + levelno; - if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { - continue; + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){ + continue; } - if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { - continue; + if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){ + continue; } if ((res->pw==0)||(res->pw==0)) continue; @@ -385,7 +385,7 @@ static bool pi_next_cprl(opj_pi_iterator_t * pi) { prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) - int_floordivpow2(try0, res->pdy); pi->precno = prci + prcj * res->pw; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; if (!pi->include[index]) { pi->include[index] = 1; @@ -562,7 +562,7 @@ opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int ti pi = (opj_pi_iterator_t *) opj_malloc(array_size); pi->tp_on = cp->tp_on; if(!pi) { return NULL;} - + for(pino = 0;pino < tcp->numpocs+1 ; pino ++){ p = tileno % cp->tw; q = tileno / cp->tw; @@ -652,7 +652,7 @@ opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int ti } if (pino == 0) { - array_size = image->numcomps * maxres * tcp->numlayers * 100 * sizeof(short int); + array_size = (tcp->numpocs+1) * tcp->numlayers * pi[pino].step_l * sizeof(short int); pi[pino].include = (short int *) opj_malloc(array_size); if(!pi[pino].include) { pi_destroy(pi, cp, tileno); @@ -688,7 +688,8 @@ opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int ti tcp->pocs[pino].tyE = pi[pino].ty1; tcp->pocs[pino].dx = pi[pino].dx; tcp->pocs[pino].dy = pi[pino].dy; - return pi; + } + return pi; } @@ -822,7 +823,7 @@ void pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino, int incr_top=1,resetX=0; opj_tcp_t *tcps =&cp->tcps[tileno]; opj_poc_t *tcp= &tcps->pocs[pino]; - prog = convert_progression_order(tcp->prg); + prog = j2k_convert_progression_order(tcp->prg); pi[pino].first = 1; pi[pino].poc.prg = tcp->prg; @@ -920,6 +921,35 @@ void pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino, incr_top=1; }else{ for(i=tppos;i>=0;i--){ + switch(prog[i]){ + case 'C': + pi[pino].poc.compno0 = tcp->comp_t-1; + pi[pino].poc.compno1 = tcp->comp_t; + break; + case 'R': + pi[pino].poc.resno0 = tcp->res_t-1; + pi[pino].poc.resno1 = tcp->res_t; + break; + case 'L': + pi[pino].poc.layno0 = tcp->lay_t-1; + pi[pino].poc.layno1 = tcp->lay_t; + break; + case 'P': + switch(tcp->prg){ + case LRCP: + case RLCP: + pi[pino].poc.precno0 = tcp->prc_t-1; + pi[pino].poc.precno1 = tcp->prc_t; + break; + default: + pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx); + pi[pino].poc.tx1 = tcp->tx0_t ; + pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy); + pi[pino].poc.ty1 = tcp->ty0_t ; + break; + } + break; + } if(incr_top==1){ switch(prog[i]){ case 'R': diff --git a/libopenjpeg/t2.c b/libopenjpeg/t2.c index 03f6933d..4e770073 100644 --- a/libopenjpeg/t2.c +++ b/libopenjpeg/t2.c @@ -563,7 +563,7 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t /* ----------------------------------------------------------------------- */ -int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info,opj_pi_iterator_t *tcd_pi,int tpnum, int tppos,int pino, char final_encoding){ +int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info,int tpnum, int tppos,int pino, char final_encoding){ unsigned char *c = dest; int e = 0; opj_pi_iterator_t *pi = NULL; @@ -571,11 +571,7 @@ int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlaye opj_image_t *image = t2->image; opj_cp_t *cp = t2->cp; - if(final_encoding == 0){ - pi = pi_initialise_encode(image, cp, tileno,pino); - }else{ - pi = tcd_pi; - } + pi = pi_initialise_encode(image, cp, tileno,pino); if(!pi) { /* TODO: throw an error */ return -999; @@ -613,9 +609,9 @@ int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlaye /* << INDEX */ } } - if(final_encoding == 0){ - pi_destroy(pi, cp, tileno); - } + + pi_destroy(pi, cp, tileno); + if (e == -999) { return e; } diff --git a/libopenjpeg/t2.h b/libopenjpeg/t2.h index dfe0dda1..f2b91a7b 100644 --- a/libopenjpeg/t2.h +++ b/libopenjpeg/t2.h @@ -66,7 +66,7 @@ Encode the packets of a tile to a destination buffer @param len the length of the destination buffer @param image_info structure to create an index file */ -int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info,opj_pi_iterator_t *tcd_pi,int tpnum, int tppos,int pino,char final_encoding); +int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info,int tpnum, int tppos,int pino,char final_encoding); /** Decode the packets of a tile from a source buffer @param t2 T2 handle diff --git a/libopenjpeg/tcd.c b/libopenjpeg/tcd.c index cfd31d52..af789588 100644 --- a/libopenjpeg/tcd.c +++ b/libopenjpeg/tcd.c @@ -1024,6 +1024,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in /* TODO: remove maxlen */ int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len; double goodthresh = 0; + double stable_thresh = 0; int i; double distotarget; /* fixed_quality */ @@ -1042,7 +1043,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in if (cp->fixed_quality) { /* fixed_quality */ if(cp->cinema){ - l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->pi,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,0); + l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,0); if (l == -999) { lo = thresh; continue; @@ -1050,7 +1051,9 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in distoachieved = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; if (distoachieved < distotarget) { - hi=thresh; continue; + hi=thresh; + stable_thresh = thresh; + continue; }else{ lo=thresh; } @@ -1060,12 +1063,13 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); if (distoachieved < distotarget) { hi = thresh; + stable_thresh = thresh; continue; } lo = thresh; } } else { - l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->pi,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,0); + l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,0); /* 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) { @@ -1073,11 +1077,11 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_in continue; } hi = thresh; + stable_thresh = thresh; } - - success = 1; - goodthresh = thresh; } + success = 1; + goodthresh = stable_thresh; t2_destroy(t2); } else { success = 1; @@ -1229,19 +1233,16 @@ int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, op } t2 = t2_create(tcd->cinfo, image, cp); - l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, image_info,tcd->pi,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,1); + l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, image_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,1); t2_destroy(t2); /*---------------CLEAN-------------------*/ - encoding_time = opj_clock() - encoding_time; - if(tcd->cur_totnum_tp > 1){ - opj_event_msg(tcd->cinfo, EVT_INFO, "- Tile part num %d encoded in %f s\n", tcd->cur_tp_num,encoding_time); - }else{ - opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time); - } - + if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){ + encoding_time = opj_clock() - encoding_time; + opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time); + /* cleaning memory */ for (compno = 0; compno < tile->numcomps; compno++) { opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; diff --git a/libopenjpeg/tcd.h b/libopenjpeg/tcd.h index 9a3503f4..b81a94a6 100644 --- a/libopenjpeg/tcd.h +++ b/libopenjpeg/tcd.h @@ -160,6 +160,8 @@ Tile coder/decoder typedef struct opj_tcd { /** Position of the tilepart flag in Progression order*/ int tp_pos; + /** Tile part number*/ + int tp_num; /** Current tile part number*/ int cur_tp_num; /** Total number of tileparts of the current tile*/ @@ -181,8 +183,6 @@ typedef struct opj_tcd { opj_tcp_t *tcp; /** current encoded/decoded tile */ int tcd_tileno; - /** Packet iterator*/ - opj_pi_iterator_t *pi; } opj_tcd_t; /** @name Exported functions */