Propagate event manager down to opj_t2_encode_packet() and use it to emit an error message when the output buffer is too small

This commit is contained in:
Even Rouault 2017-08-10 16:49:47 +02:00
parent a316f36dfc
commit 0b4fef6d19
5 changed files with 45 additions and 21 deletions

View File

@ -4659,7 +4659,8 @@ static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,
*p_data_written = 0; *p_data_written = 0;
if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data,
p_data_written, l_remaining_data, l_cstr_info)) { p_data_written, l_remaining_data, l_cstr_info,
p_manager)) {
opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n"); opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");
return OPJ_FALSE; return OPJ_FALSE;
} }

View File

@ -68,6 +68,7 @@ Encode a packet of a tile to a destination buffer
@param p_data_written FIXME DOC @param p_data_written FIXME DOC
@param len Length of the destination buffer @param len Length of the destination buffer
@param cstr_info Codestream information structure @param cstr_info Codestream information structure
@param p_manager the user event manager
@return @return
*/ */
static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno, static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
@ -77,7 +78,8 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
OPJ_BYTE *dest, OPJ_BYTE *dest,
OPJ_UINT32 * p_data_written, OPJ_UINT32 * p_data_written,
OPJ_UINT32 len, OPJ_UINT32 len,
opj_codestream_info_t *cstr_info); opj_codestream_info_t *cstr_info,
opj_event_mgr_t *p_manager);
/** /**
Decode a packet of a tile from a source buffer Decode a packet of a tile from a source buffer
@ -222,7 +224,8 @@ OPJ_BOOL opj_t2_encode_packets(opj_t2_t* p_t2,
OPJ_UINT32 p_tp_num, OPJ_UINT32 p_tp_num,
OPJ_INT32 p_tp_pos, OPJ_INT32 p_tp_pos,
OPJ_UINT32 p_pino, OPJ_UINT32 p_pino,
J2K_T2_MODE p_t2_mode) J2K_T2_MODE p_t2_mode,
opj_event_mgr_t *p_manager)
{ {
OPJ_BYTE *l_current_data = p_dest; OPJ_BYTE *l_current_data = p_dest;
OPJ_UINT32 l_nb_bytes = 0; OPJ_UINT32 l_nb_bytes = 0;
@ -268,7 +271,9 @@ OPJ_BOOL opj_t2_encode_packets(opj_t2_t* p_t2,
l_nb_bytes = 0; l_nb_bytes = 0;
if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi, if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi,
l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { l_current_data, &l_nb_bytes,
p_max_len, cstr_info,
p_manager)) {
opj_pi_destroy(l_pi, l_nb_pocs); opj_pi_destroy(l_pi, l_nb_pocs);
return OPJ_FALSE; return OPJ_FALSE;
} }
@ -306,7 +311,7 @@ OPJ_BOOL opj_t2_encode_packets(opj_t2_t* p_t2,
l_nb_bytes = 0; l_nb_bytes = 0;
if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi, if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi,
l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { l_current_data, &l_nb_bytes, p_max_len, cstr_info, p_manager)) {
opj_pi_destroy(l_pi, l_nb_pocs); opj_pi_destroy(l_pi, l_nb_pocs);
return OPJ_FALSE; return OPJ_FALSE;
} }
@ -596,7 +601,8 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
OPJ_BYTE *dest, OPJ_BYTE *dest,
OPJ_UINT32 * p_data_written, OPJ_UINT32 * p_data_written,
OPJ_UINT32 length, OPJ_UINT32 length,
opj_codestream_info_t *cstr_info) opj_codestream_info_t *cstr_info,
opj_event_mgr_t *p_manager)
{ {
OPJ_UINT32 bandno, cblkno; OPJ_UINT32 bandno, cblkno;
OPJ_BYTE* c = dest; OPJ_BYTE* c = dest;
@ -845,6 +851,10 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
} }
if (layer->len > length) { if (layer->len > length) {
opj_event_msg(p_manager, EVT_ERROR,
"opj_t2_encode_packet(): only %u bytes remaining in "
"output buffer. %u needed.\n",
layer->len, length);
return OPJ_FALSE; return OPJ_FALSE;
} }

View File

@ -76,6 +76,7 @@ Encode the packets of a tile to a destination buffer
@param tppos The position of the tile part flag in the progression order @param tppos The position of the tile part flag in the progression order
@param pino FIXME DOC @param pino FIXME DOC
@param t2_mode If == 0 In Threshold calculation ,If == 1 Final pass @param t2_mode If == 0 In Threshold calculation ,If == 1 Final pass
@param p_manager the user event manager
*/ */
OPJ_BOOL opj_t2_encode_packets(opj_t2_t* t2, OPJ_BOOL opj_t2_encode_packets(opj_t2_t* t2,
OPJ_UINT32 tileno, OPJ_UINT32 tileno,
@ -88,7 +89,8 @@ OPJ_BOOL opj_t2_encode_packets(opj_t2_t* t2,
OPJ_UINT32 tpnum, OPJ_UINT32 tpnum,
OPJ_INT32 tppos, OPJ_INT32 tppos,
OPJ_UINT32 pino, OPJ_UINT32 pino,
J2K_T2_MODE t2_mode); J2K_T2_MODE t2_mode,
opj_event_mgr_t *p_manager);
/** /**
Decode the packets of a tile from a source buffer Decode the packets of a tile from a source buffer

View File

@ -180,12 +180,14 @@ static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
OPJ_BYTE * p_dest_data, OPJ_BYTE * p_dest_data,
OPJ_UINT32 * p_data_written, OPJ_UINT32 * p_data_written,
OPJ_UINT32 p_max_dest_size, OPJ_UINT32 p_max_dest_size,
opj_codestream_info_t *p_cstr_info); opj_codestream_info_t *p_cstr_info,
opj_event_mgr_t *p_manager);
static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd, static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
OPJ_BYTE * p_dest_data, OPJ_BYTE * p_dest_data,
OPJ_UINT32 p_max_dest_size, OPJ_UINT32 p_max_dest_size,
opj_codestream_info_t *p_cstr_info); opj_codestream_info_t *p_cstr_info,
opj_event_mgr_t *p_manager);
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
@ -431,7 +433,8 @@ OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
OPJ_BYTE *dest, OPJ_BYTE *dest,
OPJ_UINT32 * p_data_written, OPJ_UINT32 * p_data_written,
OPJ_UINT32 len, OPJ_UINT32 len,
opj_codestream_info_t *cstr_info) opj_codestream_info_t *cstr_info,
opj_event_mgr_t *p_manager)
{ {
OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno; OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
OPJ_UINT32 passno; OPJ_UINT32 passno;
@ -563,7 +566,7 @@ OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
if (OPJ_IS_CINEMA(cp->rsiz)) { if (OPJ_IS_CINEMA(cp->rsiz)) {
if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino, p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
THRESH_CALC)) { THRESH_CALC, p_manager)) {
lo = thresh; lo = thresh;
continue; continue;
@ -593,7 +596,7 @@ OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
} else { } else {
if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino, p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
THRESH_CALC)) { THRESH_CALC, p_manager)) {
/* TODO: what to do with l ??? seek / tell ??? */ /* TODO: what to do with l ??? seek / tell ??? */
/* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */ /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
lo = thresh; lo = thresh;
@ -1303,7 +1306,8 @@ OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
OPJ_BYTE *p_dest, OPJ_BYTE *p_dest,
OPJ_UINT32 * p_data_written, OPJ_UINT32 * p_data_written,
OPJ_UINT32 p_max_length, OPJ_UINT32 p_max_length,
opj_codestream_info_t *p_cstr_info) opj_codestream_info_t *p_cstr_info,
opj_event_mgr_t *p_manager)
{ {
if (p_tcd->cur_tp_num == 0) { if (p_tcd->cur_tp_num == 0) {
@ -1365,7 +1369,8 @@ OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
/* FIXME _ProfStop(PGROUP_T1); */ /* FIXME _ProfStop(PGROUP_T1); */
/* FIXME _ProfStart(PGROUP_RATE); */ /* FIXME _ProfStart(PGROUP_RATE); */
if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length, p_cstr_info)) { if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
p_cstr_info, p_manager)) {
return OPJ_FALSE; return OPJ_FALSE;
} }
/* FIXME _ProfStop(PGROUP_RATE); */ /* FIXME _ProfStop(PGROUP_RATE); */
@ -1380,7 +1385,7 @@ OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
/* FIXME _ProfStart(PGROUP_T2); */ /* FIXME _ProfStart(PGROUP_T2); */
if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length, if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
p_cstr_info)) { p_cstr_info, p_manager)) {
return OPJ_FALSE; return OPJ_FALSE;
} }
/* FIXME _ProfStop(PGROUP_T2); */ /* FIXME _ProfStop(PGROUP_T2); */
@ -2196,7 +2201,8 @@ static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
OPJ_BYTE * p_dest_data, OPJ_BYTE * p_dest_data,
OPJ_UINT32 * p_data_written, OPJ_UINT32 * p_data_written,
OPJ_UINT32 p_max_dest_size, OPJ_UINT32 p_max_dest_size,
opj_codestream_info_t *p_cstr_info) opj_codestream_info_t *p_cstr_info,
opj_event_mgr_t *p_manager)
{ {
opj_t2_t * l_t2; opj_t2_t * l_t2;
@ -2217,7 +2223,8 @@ static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
p_tcd->tp_num, p_tcd->tp_num,
p_tcd->tp_pos, p_tcd->tp_pos,
p_tcd->cur_pino, p_tcd->cur_pino,
FINAL_PASS)) { FINAL_PASS,
p_manager)) {
opj_t2_destroy(l_t2); opj_t2_destroy(l_t2);
return OPJ_FALSE; return OPJ_FALSE;
} }
@ -2232,7 +2239,8 @@ static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd, static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
OPJ_BYTE * p_dest_data, OPJ_BYTE * p_dest_data,
OPJ_UINT32 p_max_dest_size, OPJ_UINT32 p_max_dest_size,
opj_codestream_info_t *p_cstr_info) opj_codestream_info_t *p_cstr_info,
opj_event_mgr_t *p_manager)
{ {
opj_cp_t * l_cp = p_tcd->cp; opj_cp_t * l_cp = p_tcd->cp;
OPJ_UINT32 l_nb_written = 0; OPJ_UINT32 l_nb_written = 0;
@ -2246,7 +2254,7 @@ static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
/* fixed_quality */ /* fixed_quality */
/* Normal Rate/distortion allocation */ /* Normal Rate/distortion allocation */
if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size, if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
p_cstr_info)) { p_cstr_info, p_manager)) {
return OPJ_FALSE; return OPJ_FALSE;
} }
} else { } else {

View File

@ -317,7 +317,8 @@ OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
OPJ_BYTE *dest, OPJ_BYTE *dest,
OPJ_UINT32 * p_data_written, OPJ_UINT32 * p_data_written,
OPJ_UINT32 len, OPJ_UINT32 len,
opj_codestream_info_t *cstr_info); opj_codestream_info_t *cstr_info,
opj_event_mgr_t *p_manager);
/** /**
* Gets the maximum tile size that will be taken by the tile once decoded. * Gets the maximum tile size that will be taken by the tile once decoded.
@ -332,6 +333,7 @@ OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd);
* @param p_data_written pointer to an int that is incremented by the number of bytes really written on p_dest * @param p_data_written pointer to an int that is incremented by the number of bytes really written on p_dest
* @param p_len Maximum length of the destination buffer * @param p_len Maximum length of the destination buffer
* @param p_cstr_info Codestream information structure * @param p_cstr_info Codestream information structure
* @param p_manager the user event manager
* @return true if the coding is successful. * @return true if the coding is successful.
*/ */
OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd, OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
@ -339,7 +341,8 @@ OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
OPJ_BYTE *p_dest, OPJ_BYTE *p_dest,
OPJ_UINT32 * p_data_written, OPJ_UINT32 * p_data_written,
OPJ_UINT32 p_len, OPJ_UINT32 p_len,
struct opj_codestream_info *p_cstr_info); struct opj_codestream_info *p_cstr_info,
opj_event_mgr_t *p_manager);
/** /**