Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/bin/jp2/opj_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ static int parse_cmdline_encoder(int argc, char **argv,

/* ----------------------------------------------------- */

case 'q': { /* add fixed_quality */
case 'q': { /* layer allocation by distortion ratio (PSNR) */
char *s = opj_optarg;
while (sscanf(s, "%f", &parameters->tcp_distoratio[parameters->tcp_numlayers])
== 1) {
Expand All @@ -866,7 +866,7 @@ static int parse_cmdline_encoder(int argc, char **argv,
/* dda */
/* ----------------------------------------------------- */

case 'f': { /* mod fixed_quality (before : -q) */
case 'f': { /* layer allocation by fixed layer */
int *row = NULL, *col = NULL;
OPJ_UINT32 numlayers = 0, numresolution = 0, matrix_width = 0;

Expand Down Expand Up @@ -1812,7 +1812,7 @@ static int parse_cmdline_encoder(int argc, char **argv,
parameters->cp_fixed_quality))) {
fprintf(stderr, "[ERROR] options -r -q and -f cannot be used together !!\n");
return 1;
} /* mod fixed_quality */
}


/* if no rate entered, lossless by default */
Expand Down
26 changes: 10 additions & 16 deletions src/lib/openjp2/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@
/** @name Local static functions */
/*@{*/

/**
Write a bit
@param bio BIO handle
@param b Bit to write (0 or 1)
*/
static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b);
/**
Read a bit
@param bio BIO handle
Expand Down Expand Up @@ -100,16 +94,6 @@ static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio)
return OPJ_TRUE;
}

static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b)
{
if (bio->ct == 0) {
opj_bio_byteout(
bio); /* MSD: why not check the return value of this function ? */
}
bio->ct--;
bio->buf |= b << bio->ct;
}

static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio)
{
if (bio->ct == 0) {
Expand Down Expand Up @@ -162,6 +146,16 @@ void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len)
bio->ct = 0;
}

void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b)
{
if (bio->ct == 0) {
opj_bio_byteout(
bio); /* MSD: why not check the return value of this function ? */
}
bio->ct--;
bio->buf |= b << bio->ct;
}

void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n)
{
OPJ_INT32 i;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/openjp2/bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ Write bits
@param n Number of bits to write
*/
void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n);

/**
Write a bit
@param bio BIO handle
@param b Bit to write (0 or 1)
*/
void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b);

/**
Read bits
@param bio BIO handle
Expand Down
50 changes: 38 additions & 12 deletions src/lib/openjp2/j2k.c
Original file line number Diff line number Diff line change
Expand Up @@ -7666,6 +7666,27 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
return OPJ_FALSE;
}

if (parameters->cp_fixed_alloc) {
if (parameters->cp_matrice == NULL) {
opj_event_msg(p_manager, EVT_ERROR,
"cp_fixed_alloc set, but cp_matrice missing\n");
return OPJ_FALSE;
}

if (parameters->tcp_numlayers > J2K_TCD_MATRIX_MAX_LAYER_COUNT) {
opj_event_msg(p_manager, EVT_ERROR,
"tcp_numlayers when cp_fixed_alloc set should not exceed %d\n",
J2K_TCD_MATRIX_MAX_LAYER_COUNT);
return OPJ_FALSE;
}
if (parameters->numresolution > J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT) {
opj_event_msg(p_manager, EVT_ERROR,
"numresolution when cp_fixed_alloc set should not exceed %d\n",
J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT);
return OPJ_FALSE;
}
}

p_j2k->m_specific_param.m_encoder.m_nb_comps = image->numcomps;

/* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
Expand Down Expand Up @@ -7885,15 +7906,17 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)
parameters->max_comp_size;
cp->rsiz = parameters->rsiz;
cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32)
parameters->cp_disto_alloc & 1u;
cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32)
parameters->cp_fixed_alloc & 1u;
cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32)
parameters->cp_fixed_quality & 1u;

/* mod fixed_quality */
if (parameters->cp_fixed_alloc && parameters->cp_matrice) {
if (parameters->cp_fixed_alloc) {
cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = FIXED_LAYER;
} else if (parameters->cp_fixed_quality) {
cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy =
FIXED_DISTORTION_RATIO;
} else {
cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy =
RATE_DISTORTION_RATIO;
}

if (parameters->cp_fixed_alloc) {
size_t array_size = (size_t)parameters->tcp_numlayers *
(size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32);
cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
Expand Down Expand Up @@ -8051,22 +8074,25 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,

for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
opj_tcp_t *tcp = &cp->tcps[tileno];
const OPJ_BOOL fixed_distoratio =
cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
FIXED_DISTORTION_RATIO;
tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;

for (j = 0; j < tcp->numlayers; j++) {
if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) {
if (cp->m_specific_param.m_enc.m_fixed_quality) {
if (fixed_distoratio) {
tcp->distoratio[j] = parameters->tcp_distoratio[j];
}
tcp->rates[j] = parameters->tcp_rates[j];
} else {
if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
if (fixed_distoratio) {
tcp->distoratio[j] = parameters->tcp_distoratio[j];
} else {
tcp->rates[j] = parameters->tcp_rates[j];
}
}
if (!cp->m_specific_param.m_enc.m_fixed_quality &&
if (!fixed_distoratio &&
tcp->rates[j] <= 1.0) {
tcp->rates[j] = 0.0; /* force lossless */
}
Expand Down
21 changes: 14 additions & 7 deletions src/lib/openjp2/j2k.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ The functions in J2K.C have for goal to read/write the several parts of the code

#define J2K_MAX_POCS 32 /**< Maximum number of POCs */

#define J2K_TCD_MATRIX_MAX_LAYER_COUNT 10
#define J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT 10

/* ----------------------------------------------------------------------- */

/**
Expand Down Expand Up @@ -272,7 +275,7 @@ typedef struct opj_tcp {
OPJ_UINT32 ppt_data_size;
/** size of ppt_data*/
OPJ_UINT32 ppt_len;
/** add fixed_quality */
/** PSNR values */
OPJ_FLOAT32 distoratio[100];
/** tile-component coding parameters */
opj_tccp_t *tccps;
Expand Down Expand Up @@ -314,6 +317,14 @@ typedef struct opj_tcp {
} opj_tcp_t;


/**
Rate allocation strategy
*/
typedef enum {
RATE_DISTORTION_RATIO = 0, /** allocation by rate/distortion */
FIXED_DISTORTION_RATIO = 1, /** allocation by fixed distortion ratio (PSNR) (fixed quality) */
FIXED_LAYER = 2, /** allocation by fixed layer (number of passes per layer / resolution / subband) */
} J2K_QUALITY_LAYER_ALLOCATION_STRATEGY;


typedef struct opj_encoding_param {
Expand All @@ -325,12 +336,8 @@ typedef struct opj_encoding_param {
OPJ_INT32 *m_matrice;
/** Flag determining tile part generation*/
OPJ_BYTE m_tp_flag;
/** allocation by rate/distortion */
OPJ_BITFIELD m_disto_alloc : 1;
/** allocation by fixed layer */
OPJ_BITFIELD m_fixed_alloc : 1;
/** add fixed_quality */
OPJ_BITFIELD m_fixed_quality : 1;
/** Quality layer allocation strategy */
J2K_QUALITY_LAYER_ALLOCATION_STRATEGY m_quality_layer_alloc_strategy;
/** Enabling Tile part generation*/
OPJ_BITFIELD m_tp_on : 1;
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/openjp2/openjpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ typedef struct opj_cparameters {
int cp_disto_alloc;
/** allocation by fixed layer */
int cp_fixed_alloc;
/** add fixed_quality */
/** allocation by fixed quality (PSNR) */
int cp_fixed_quality;
/** fixed layer */
int *cp_matrice;
Expand Down Expand Up @@ -829,9 +829,9 @@ typedef struct opj_tile_info {
int pdy[33];
/** information concerning packets inside tile */
opj_packet_info_t *packet;
/** add fixed_quality */
/** number of pixels of the tile */
int numpix;
/** add fixed_quality */
/** distortion of the tile */
double distotile;
/** number of markers */
int marknum;
Expand Down
5 changes: 1 addition & 4 deletions src/lib/openjp2/t1.c
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,6 @@ static void opj_t1_dec_clnpass(
}


/** mod fixed_quality */
static OPJ_FLOAT64 opj_t1_getwmsedec(
OPJ_INT32 nmsedec,
OPJ_UINT32 compno,
Expand Down Expand Up @@ -2313,7 +2312,7 @@ OPJ_BOOL opj_t1_encode_cblks(opj_tcd_t* tcd,
OPJ_UINT32 compno, resno, bandno, precno, cblkno;
opj_mutex_t* mutex = opj_mutex_create();

tile->distotile = 0; /* fixed_quality */
tile->distotile = 0;

for (compno = 0; compno < tile->numcomps; ++compno) {
opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
Expand Down Expand Up @@ -2401,7 +2400,6 @@ static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk,
}


/** mod fixed_quality */
static OPJ_FLOAT64 opj_t1_encode_cblk(opj_t1_t *t1,
opj_tcd_cblk_enc_t* cblk,
OPJ_UINT32 orient,
Expand Down Expand Up @@ -2505,7 +2503,6 @@ static OPJ_FLOAT64 opj_t1_encode_cblk(opj_t1_t *t1,
break;
}

/* fixed_quality */
tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
stepsize, numcomps, mct_norms, mct_numcomps) ;
cumwmsedec += tempwmsedec;
Expand Down
14 changes: 8 additions & 6 deletions src/lib/openjp2/t2.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n)
{
while (--n >= 0) {
opj_bio_write(bio, 1, 1);
opj_bio_putbit(bio, 1);
}
opj_bio_write(bio, 0, 1);
opj_bio_putbit(bio, 0);
}

static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
Expand All @@ -184,7 +184,7 @@ static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n)
{
if (n == 1) {
opj_bio_write(bio, 0, 1);
opj_bio_putbit(bio, 0);
} else if (n == 2) {
opj_bio_write(bio, 2, 2);
} else if (n <= 5) {
Expand Down Expand Up @@ -801,7 +801,7 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
}
}
#endif
opj_bio_write(bio, packet_empty ? 0 : 1, 1); /* Empty header bit */
opj_bio_putbit(bio, packet_empty ? 0 : 1); /* Empty header bit */

/* Writing Packet header */
band = res->bands;
Expand Down Expand Up @@ -849,7 +849,7 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
if (!cblk->numpasses) {
opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
} else {
opj_bio_write(bio, layer->numpasses != 0, 1);
opj_bio_putbit(bio, layer->numpasses != 0);
}

/* if cblk not included, go to the next cblk */
Expand Down Expand Up @@ -978,7 +978,9 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
return OPJ_FALSE;
}

memcpy(c, layer->data, layer->len);
if (p_t2_mode == FINAL_PASS) {
memcpy(c, layer->data, layer->len);
}
cblk->numpasses += layer->numpasses;
c += layer->len;
length -= layer->len;
Expand Down
Loading