From 1e3a57563defb6aa7cf24ffd2394d4a820e13bda Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 25 Apr 2019 00:17:13 +0200 Subject: [PATCH 1/7] compression: emit POC marker when only one single POC is requested (fixes #1191) --- src/lib/openjp2/j2k.c | 9 ++++++++- src/lib/openjp2/t2.c | 8 ++++++++ tests/nonregression/test_suite.ctest.in | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index aa0f43f9..6e000d54 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -1702,6 +1702,13 @@ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, for (resno = 0; resno < p_nb_resolutions; ++resno) { for (compno = 0; compno < p_num_comps; ++compno) { loss |= (packet_array[index] != 1); +#ifdef DEBUG_VERBOSE + if (packet_array[index] != 1) { + fprintf(stderr, + "Missing packet in POC: layno=%d resno=%d compno=%d\n", + layno, resno, compno); + } +#endif /*index = step_r * resno + step_c * compno + step_l * layno;*/ index += step_c; } @@ -11759,7 +11766,7 @@ static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, total_data_size -= l_current_nb_bytes_written; } #endif - if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) { + if (l_cp->tcps[p_j2k->m_current_tile_number].POC) { l_current_nb_bytes_written = 0; opj_j2k_write_poc_in_memory(p_j2k, p_data, &l_current_nb_bytes_written, p_manager); diff --git a/src/lib/openjp2/t2.c b/src/lib/openjp2/t2.c index 9825118c..0887b9f5 100644 --- a/src/lib/openjp2/t2.c +++ b/src/lib/openjp2/t2.c @@ -673,6 +673,14 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno, OPJ_BOOL packet_empty = OPJ_FALSE; #endif +#ifdef DEBUG_VERBOSE + if (p_t2_mode == FINAL_PASS) { + fprintf(stderr, + "encode packet compono=%d, resno=%d, precno=%d, layno=%d\n", + compno, resno, precno, layno); + } +#endif + /* */ if (tcp->csty & J2K_CP_CSTY_SOP) { if (length < 6) { diff --git a/tests/nonregression/test_suite.ctest.in b/tests/nonregression/test_suite.ctest.in index 4048a410..86ebf979 100644 --- a/tests/nonregression/test_suite.ctest.in +++ b/tests/nonregression/test_suite.ctest.in @@ -171,6 +171,9 @@ opj_compress -i @INPUT_NR_PATH@/Bretagne2.ppm -o @TEMP_PATH@/Bretagne2_empty_ban opj_compress -i @INPUT_NR_PATH@/issue982.bmp -o @TEMP_PATH@/issue982.j2k -n 1 +# Test generating only one POC +opj_compress -i @INPUT_NR_PATH@/byte.tif -o @TEMP_PATH@/byte_one_poc.j2k -n 2 -r 20,1 -POC T1=0,0,2,2,1,CPRL -b 4,4 + # DECODER TEST SUITE opj_decompress -i @INPUT_NR_PATH@/Bretagne2.j2k -o @TEMP_PATH@/Bretagne2.j2k.pgx opj_decompress -i @INPUT_NR_PATH@/_00042.j2k -o @TEMP_PATH@/_00042.j2k.pgx From 6589c609f6d6b3743715fceefbdac6e4ecb76aee Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 25 Apr 2019 00:28:05 +0200 Subject: [PATCH 2/7] opj_j2k_check_poc_val(): fix starting index for checking layer dimension The standard mandates that the layer index always starts at zero for every progression. --- src/lib/openjp2/j2k.c | 42 +++++++----------------------------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index 6e000d54..be7f1812 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -1628,7 +1628,10 @@ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, OPJ_UINT32 step_r = p_num_comps * step_c; OPJ_UINT32 step_l = p_nb_resolutions * step_r; OPJ_BOOL loss = OPJ_FALSE; - OPJ_UINT32 layno0 = 0; + + if (p_nb_pocs == 0) { + return OPJ_TRUE; + } packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32)); @@ -1638,39 +1641,8 @@ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, return OPJ_FALSE; } - if (p_nb_pocs == 0) { - opj_free(packet_array); - return OPJ_TRUE; - } - - index = step_r * p_pocs->resno0; - /* take each resolution for each poc */ - for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) { - OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; - - /* take each comp of each resolution for each poc */ - for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { - OPJ_UINT32 comp_index = res_index + layno0 * step_l; - - /* and finally take each layer of each res of ... */ - for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { - /*index = step_r * resno + step_c * compno + step_l * layno;*/ - packet_array[comp_index] = 1; - comp_index += step_l; - } - - res_index += step_c; - } - - index += step_r; - } - ++p_pocs; - /* iterate through all the pocs */ - for (i = 1; i < p_nb_pocs ; ++i) { - OPJ_UINT32 l_last_layno1 = (p_pocs - 1)->layno1 ; - - layno0 = (p_pocs->layno1 > l_last_layno1) ? l_last_layno1 : 0; + for (i = 0; i < p_nb_pocs ; ++i) { index = step_r * p_pocs->resno0; /* take each resolution for each poc */ @@ -1679,11 +1651,12 @@ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, /* take each comp of each resolution for each poc */ for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { + /* The layer index always starts at zero for every progression. */ + const OPJ_UINT32 layno0 = 0; OPJ_UINT32 comp_index = res_index + layno0 * step_l; /* and finally take each layer of each res of ... */ for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { - /*index = step_r * resno + step_c * compno + step_l * layno;*/ packet_array[comp_index] = 1; comp_index += step_l; } @@ -1709,7 +1682,6 @@ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, layno, resno, compno); } #endif - /*index = step_r * resno + step_c * compno + step_l * layno;*/ index += step_c; } } From 23883458b9de2c57fc1890b42efbd0832c8fbe3b Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 25 Apr 2019 00:34:44 +0200 Subject: [PATCH 3/7] opj_j2k_check_poc_val(): prevent potential write outside of allocated array --- src/lib/openjp2/j2k.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index be7f1812..fcf62653 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -1646,17 +1646,20 @@ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, index = step_r * p_pocs->resno0; /* take each resolution for each poc */ - for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) { + for (resno = p_pocs->resno0 ; + resno < opj_uint_min(p_pocs->resno1, p_nb_resolutions); ++resno) { OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; /* take each comp of each resolution for each poc */ - for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { + for (compno = p_pocs->compno0 ; + compno < opj_uint_min(p_pocs->compno1, p_num_comps); ++compno) { /* The layer index always starts at zero for every progression. */ const OPJ_UINT32 layno0 = 0; OPJ_UINT32 comp_index = res_index + layno0 * step_l; /* and finally take each layer of each res of ... */ - for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { + for (layno = layno0; layno < opj_uint_min(p_pocs->layno1, p_num_layers); + ++layno) { packet_array[comp_index] = 1; comp_index += step_l; } From b86717fdd36b628ea7ecb5c24f7a086bf5bcd3a7 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 25 Apr 2019 00:40:04 +0200 Subject: [PATCH 4/7] Add test for previous commit --- tests/nonregression/test_suite.ctest.in | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/nonregression/test_suite.ctest.in b/tests/nonregression/test_suite.ctest.in index 86ebf979..65f0ebe7 100644 --- a/tests/nonregression/test_suite.ctest.in +++ b/tests/nonregression/test_suite.ctest.in @@ -173,6 +173,7 @@ opj_compress -i @INPUT_NR_PATH@/issue982.bmp -o @TEMP_PATH@/issue982.j2k -n 1 # Test generating only one POC opj_compress -i @INPUT_NR_PATH@/byte.tif -o @TEMP_PATH@/byte_one_poc.j2k -n 2 -r 20,1 -POC T1=0,0,2,2,1,CPRL -b 4,4 +opj_compress -i @INPUT_NR_PATH@/byte.tif -o @TEMP_PATH@/byte_one_poc_funky_bounds.j2k -n 2 -r 20,1 -POC T1=0,0,65535,33,255,CPRL -b 4,4 # DECODER TEST SUITE opj_decompress -i @INPUT_NR_PATH@/Bretagne2.j2k -o @TEMP_PATH@/Bretagne2.j2k.pgx From 6423163141412cb93364de4e33d90bcffefa0885 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 25 Apr 2019 01:27:02 +0200 Subject: [PATCH 5/7] Fix POC in multi-tile scenarios: avoid almost endless loop when a tile has no POC settings --- src/lib/openjp2/j2k.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index fcf62653..a40ea05a 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -1669,7 +1669,6 @@ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, index += step_r; } - ++p_pocs; } @@ -7179,7 +7178,6 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, if (parameters->numpocs) { /* initialisation of POC */ - tcp->POC = 1; for (i = 0; i < parameters->numpocs; i++) { if (tileno + 1 == parameters->POC[i].tile) { opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; @@ -7196,7 +7194,10 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, } } - tcp->numpocs = numpocs_tile - 1 ; + if (numpocs_tile) { + tcp->POC = 1; + tcp->numpocs = numpocs_tile - 1 ; + } } else { tcp->numpocs = 0; } From bdec5ae2723369be5abba7aaae398aa4ae3225cc Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 25 Apr 2019 01:29:38 +0200 Subject: [PATCH 6/7] Add test for previous commit --- tests/nonregression/test_suite.ctest.in | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/nonregression/test_suite.ctest.in b/tests/nonregression/test_suite.ctest.in index 65f0ebe7..c42bad16 100644 --- a/tests/nonregression/test_suite.ctest.in +++ b/tests/nonregression/test_suite.ctest.in @@ -174,6 +174,7 @@ opj_compress -i @INPUT_NR_PATH@/issue982.bmp -o @TEMP_PATH@/issue982.j2k -n 1 # Test generating only one POC opj_compress -i @INPUT_NR_PATH@/byte.tif -o @TEMP_PATH@/byte_one_poc.j2k -n 2 -r 20,1 -POC T1=0,0,2,2,1,CPRL -b 4,4 opj_compress -i @INPUT_NR_PATH@/byte.tif -o @TEMP_PATH@/byte_one_poc_funky_bounds.j2k -n 2 -r 20,1 -POC T1=0,0,65535,33,255,CPRL -b 4,4 +opj_compress -i @INPUT_NR_PATH@/byte.tif -o @TEMP_PATH@/byte_one_poc_unrelated_tile.j2k -n 1 -POC T1000=0,0,1,1,1,CPRL # DECODER TEST SUITE opj_decompress -i @INPUT_NR_PATH@/Bretagne2.j2k -o @TEMP_PATH@/Bretagne2.j2k.pgx From a94cfbd5334922ca5b63cfac9d2e5e0ec98155be Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 25 Apr 2019 14:07:46 +0200 Subject: [PATCH 7/7] Change opj_j2k_check_poc_val() to take into account tile number --- src/lib/openjp2/j2k.c | 66 ++++++++++++++++++++------------------ src/lib/openjp2/openjpeg.h | 2 +- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index a40ea05a..3d4aa0fc 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -1219,6 +1219,7 @@ static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, * A nice message is outputted at errors. * * @param p_pocs the progression order changes. + * @param tileno the tile number of interest * @param p_nb_pocs the number of progression order changes. * @param p_nb_resolutions the number of resolutions. * @param numcomps the number of components @@ -1228,6 +1229,7 @@ static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, * @return true if the pocs are valid. */ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, + OPJ_UINT32 tileno, OPJ_UINT32 p_nb_pocs, OPJ_UINT32 p_nb_resolutions, OPJ_UINT32 numcomps, @@ -1615,6 +1617,7 @@ const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order) } static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, + OPJ_UINT32 tileno, OPJ_UINT32 p_nb_pocs, OPJ_UINT32 p_nb_resolutions, OPJ_UINT32 p_num_comps, @@ -1629,9 +1632,7 @@ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, OPJ_UINT32 step_l = p_nb_resolutions * step_r; OPJ_BOOL loss = OPJ_FALSE; - if (p_nb_pocs == 0) { - return OPJ_TRUE; - } + assert(p_nb_pocs > 0); packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32)); @@ -1641,35 +1642,37 @@ static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, return OPJ_FALSE; } - /* iterate through all the pocs */ - for (i = 0; i < p_nb_pocs ; ++i) { - index = step_r * p_pocs->resno0; + /* iterate through all the pocs that match our tile of interest. */ + for (i = 0; i < p_nb_pocs; ++i) { + const opj_poc_t *poc = &p_pocs[i]; + if (tileno + 1 == poc->tile) { + index = step_r * poc->resno0; - /* take each resolution for each poc */ - for (resno = p_pocs->resno0 ; - resno < opj_uint_min(p_pocs->resno1, p_nb_resolutions); ++resno) { - OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; + /* take each resolution for each poc */ + for (resno = poc->resno0 ; + resno < opj_uint_min(poc->resno1, p_nb_resolutions); ++resno) { + OPJ_UINT32 res_index = index + poc->compno0 * step_c; - /* take each comp of each resolution for each poc */ - for (compno = p_pocs->compno0 ; - compno < opj_uint_min(p_pocs->compno1, p_num_comps); ++compno) { - /* The layer index always starts at zero for every progression. */ - const OPJ_UINT32 layno0 = 0; - OPJ_UINT32 comp_index = res_index + layno0 * step_l; + /* take each comp of each resolution for each poc */ + for (compno = poc->compno0 ; + compno < opj_uint_min(poc->compno1, p_num_comps); ++compno) { + /* The layer index always starts at zero for every progression. */ + const OPJ_UINT32 layno0 = 0; + OPJ_UINT32 comp_index = res_index + layno0 * step_l; - /* and finally take each layer of each res of ... */ - for (layno = layno0; layno < opj_uint_min(p_pocs->layno1, p_num_layers); - ++layno) { - packet_array[comp_index] = 1; - comp_index += step_l; + /* and finally take each layer of each res of ... */ + for (layno = layno0; layno < opj_uint_min(poc->layno1, p_num_layers); + ++layno) { + packet_array[comp_index] = 1; + comp_index += step_l; + } + + res_index += step_c; } - res_index += step_c; + index += step_r; } - - index += step_r; } - ++p_pocs; } index = 0; @@ -7138,13 +7141,6 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, "Not enough memory to allocate tile coding parameters\n"); return OPJ_FALSE; } - if (parameters->numpocs) { - /* initialisation of POC */ - opj_j2k_check_poc_val(parameters->POC, parameters->numpocs, - (OPJ_UINT32)parameters->numresolution, image->numcomps, - (OPJ_UINT32)parameters->tcp_numlayers, p_manager); - /* TODO MSD use the return value*/ - } for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { opj_tcp_t *tcp = &cp->tcps[tileno]; @@ -7195,6 +7191,12 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, } if (numpocs_tile) { + + /* TODO MSD use the return value*/ + opj_j2k_check_poc_val(parameters->POC, tileno, parameters->numpocs, + (OPJ_UINT32)parameters->numresolution, image->numcomps, + (OPJ_UINT32)parameters->tcp_numlayers, p_manager); + tcp->POC = 1; tcp->numpocs = numpocs_tile - 1 ; } diff --git a/src/lib/openjp2/openjpeg.h b/src/lib/openjp2/openjpeg.h index 53a0e10c..dc1e206f 100644 --- a/src/lib/openjp2/openjpeg.h +++ b/src/lib/openjp2/openjpeg.h @@ -333,7 +333,7 @@ typedef struct opj_poc { OPJ_PROG_ORDER prg1, prg; /** Progression order string*/ OPJ_CHAR progorder[5]; - /** Tile number */ + /** Tile number (starting at 1) */ OPJ_UINT32 tile; /** Start and end values for Tile width and height*/ OPJ_INT32 tx0, tx1, ty0, ty1;