Merge pull request #1403 from rouault/configure_guard_bits

opj_encoder_set_extra_options(): add a GUARD_BITS=value option
This commit is contained in:
Even Rouault 2022-01-24 12:00:46 +01:00 committed by GitHub
commit a5c95cfe26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 9 deletions

View File

@ -304,6 +304,9 @@ static void encode_help_display(void)
fprintf(stdout, " Y >= 0 and Y <= 9.\n"); fprintf(stdout, " Y >= 0 and Y <= 9.\n");
fprintf(stdout, fprintf(stdout,
" framerate > 0 may be specified to enhance checks and set maximum bit rate when Y > 0.\n"); " framerate > 0 may be specified to enhance checks and set maximum bit rate when Y > 0.\n");
fprintf(stdout, "-GuardBits value\n");
fprintf(stdout,
" Number of guard bits in [0,7] range. Usually 1 or 2 (default value).\n");
fprintf(stdout, "-jpip\n"); fprintf(stdout, "-jpip\n");
fprintf(stdout, " Write jpip codestream index box in JP2 output file.\n"); fprintf(stdout, " Write jpip codestream index box in JP2 output file.\n");
fprintf(stdout, " Currently supports only RPCL order.\n"); fprintf(stdout, " Currently supports only RPCL order.\n");
@ -612,6 +615,7 @@ static int parse_cmdline_encoder(int argc, char **argv,
int* pOutFramerate, int* pOutFramerate,
OPJ_BOOL* pOutPLT, OPJ_BOOL* pOutPLT,
OPJ_BOOL* pOutTLM, OPJ_BOOL* pOutTLM,
int* pOutGuardBits,
int* pOutNumThreads, int* pOutNumThreads,
unsigned int* pTarget_bitdepth) unsigned int* pTarget_bitdepth)
{ {
@ -634,10 +638,11 @@ static int parse_cmdline_encoder(int argc, char **argv,
{"threads", REQ_ARG, NULL, 'B'}, {"threads", REQ_ARG, NULL, 'B'},
{"TLM", NO_ARG, NULL, 'D'}, {"TLM", NO_ARG, NULL, 'D'},
{"TargetBitDepth", REQ_ARG, NULL, 'X'}, {"TargetBitDepth", REQ_ARG, NULL, 'X'},
{"GuardBits", REQ_ARG, NULL, 'G'}
}; };
/* parse the command line */ /* parse the command line */
const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:X:" const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:X:G:"
#ifdef USE_JPWL #ifdef USE_JPWL
"W:" "W:"
#endif /* USE_JPWL */ #endif /* USE_JPWL */
@ -933,6 +938,13 @@ static int parse_cmdline_encoder(int argc, char **argv,
} }
break; break;
/* ----------------------------------------------------- */
case 'G': { /* guard bits */
char *s = opj_optarg;
sscanf(s, "%d", pOutGuardBits);
}
break;
/* ----------------------------------------------------- */ /* ----------------------------------------------------- */
case 'n': { /* resolution */ case 'n': { /* resolution */
@ -1932,6 +1944,7 @@ int main(int argc, char **argv)
OPJ_BOOL PLT = OPJ_FALSE; OPJ_BOOL PLT = OPJ_FALSE;
OPJ_BOOL TLM = OPJ_FALSE; OPJ_BOOL TLM = OPJ_FALSE;
int num_threads = 0; int num_threads = 0;
int guard_bits = -1;
/** desired bitdepth from input file */ /** desired bitdepth from input file */
unsigned int target_bitdepth = 0; unsigned int target_bitdepth = 0;
@ -1956,7 +1969,7 @@ int main(int argc, char **argv)
255; /* This will be set later according to the input image or the provided option */ 255; /* This will be set later according to the input image or the provided option */
if (parse_cmdline_encoder(argc, argv, &parameters, &img_fol, &raw_cp, if (parse_cmdline_encoder(argc, argv, &parameters, &img_fol, &raw_cp,
indexfilename, sizeof(indexfilename), &framerate, &PLT, &TLM, indexfilename, sizeof(indexfilename), &framerate, &PLT, &TLM,
&num_threads, &target_bitdepth) == 1) { &guard_bits, &num_threads, &target_bitdepth) == 1) {
ret = 1; ret = 1;
goto fin; goto fin;
} }
@ -2201,17 +2214,21 @@ int main(int argc, char **argv)
goto fin; goto fin;
} }
if (PLT || TLM) { {
const char* options[3] = { NULL, NULL, NULL }; const char* options[4] = { NULL, NULL, NULL, NULL };
int iOpt = 0; int iOpt = 0;
char szGuardBits[32];
if (PLT) { if (PLT) {
options[iOpt++] = "PLT=YES"; options[iOpt++] = "PLT=YES";
} }
if (TLM) { if (TLM) {
options[iOpt++] = "TLM=YES"; options[iOpt++] = "TLM=YES";
} }
(void)iOpt; if (guard_bits >= 0) {
if (!opj_encoder_set_extra_options(l_codec, options)) { sprintf(szGuardBits, "GUARD_BITS=%d", guard_bits);
options[iOpt++] = szGuardBits;
}
if (iOpt > 0 && !opj_encoder_set_extra_options(l_codec, options)) {
fprintf(stderr, "failed to encode image: opj_encoder_set_extra_options\n"); fprintf(stderr, "failed to encode image: opj_encoder_set_extra_options\n");
opj_destroy_codec(l_codec); opj_destroy_codec(l_codec);
opj_image_destroy(image); opj_image_destroy(image);

View File

@ -7654,6 +7654,8 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
return OPJ_FALSE; 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 */ /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
cp = &(p_j2k->m_cp); cp = &(p_j2k->m_cp);
@ -12180,6 +12182,25 @@ OPJ_BOOL opj_j2k_encoder_set_extra_options(
"Invalid value for option: %s.\n", *p_option_iter); "Invalid value for option: %s.\n", *p_option_iter);
return OPJ_FALSE; return OPJ_FALSE;
} }
} else if (strncmp(*p_option_iter, "GUARD_BITS=", strlen("GUARD_BITS=")) == 0) {
OPJ_UINT32 tileno;
opj_cp_t *cp = cp = &(p_j2k->m_cp);
int numgbits = atoi(*p_option_iter + strlen("GUARD_BITS="));
if (numgbits < 0 || numgbits > 7) {
opj_event_msg(p_manager, EVT_ERROR,
"Invalid value for option: %s. Should be in [0,7]\n", *p_option_iter);
return OPJ_FALSE;
}
for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
OPJ_UINT32 i;
opj_tcp_t *tcp = &cp->tcps[tileno];
for (i = 0; i < p_j2k->m_specific_param.m_encoder.m_nb_comps; i++) {
opj_tccp_t *tccp = &tcp->tccps[i];
tccp->numgbits = (OPJ_UINT32)numgbits;
}
}
} else { } else {
opj_event_msg(p_manager, EVT_ERROR, opj_event_msg(p_manager, EVT_ERROR,
"Invalid option: %s.\n", *p_option_iter); "Invalid option: %s.\n", *p_option_iter);

View File

@ -550,6 +550,9 @@ typedef struct opj_j2k_enc {
/* reserved bytes in m_encoded_tile_size for PLT markers */ /* reserved bytes in m_encoded_tile_size for PLT markers */
OPJ_UINT32 m_reserved_bytes_for_PLT; OPJ_UINT32 m_reserved_bytes_for_PLT;
/** Number of components */
OPJ_UINT32 m_nb_comps;
} opj_j2k_enc_t; } opj_j2k_enc_t;

View File

@ -1599,9 +1599,12 @@ OPJ_API OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
* <li>PLT=YES/NO. Defaults to NO. If set to YES, PLT marker segments, * <li>PLT=YES/NO. Defaults to NO. If set to YES, PLT marker segments,
* indicating the length of each packet in the tile-part header, will be * indicating the length of each packet in the tile-part header, will be
* written. Since 2.4.0</li> * written. Since 2.4.0</li>
* <li>TLM=YES/NO. Defaults to NO (except for Cinema and IMF profiles). * <li>TLM=YES/NO. Defaults to NO (except for Cinema and IMF profiles).
* If set to YES, TLM marker segments, indicating the length of each * If set to YES, TLM marker segments, indicating the length of each
* tile-part part will be written. Since 2.4.0</li> * tile-part part will be written. Since 2.4.0</li>
* <li>GUARD_BITS=value. Number of guard bits in [0,7] range. Default value is 2.
* 1 may be used sometimes (like in SMPTE DCP Bv2.1 Application Profile for 2K images).
* Since 2.5.0</li>
* </ul> * </ul>
* *
* @param p_codec Compressor handle * @param p_codec Compressor handle