Ejemplo n.º 1
0
int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_codestream_info_t *cstr_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode, int cur_totnum_tp){
	unsigned char *c = dest;
	int e = 0;
	int compno;
	opj_pi_iterator_t *pi = NULL;
	int poc;
	opj_image_t *image = t2->image;
	opj_cp_t *cp = t2->cp;
	opj_tcp_t *tcp = &cp->tcps[tileno];
	int pocno = cp->cinema == CINEMA4K_24? 2: 1;
	int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
	
	pi = pi_initialise_encode(image, cp, tileno, t2_mode);
	if(!pi) {
		/* TODO: throw an error */
		return -999;
	}
	
	if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
		for(compno = 0; compno < maxcomp; compno++ ){
			for(poc = 0; poc < pocno ; poc++){
				int comp_len = 0;
				int tpnum = compno;
				if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
					opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
					pi_destroy(pi, cp, tileno);
					return -999;
				}
				while (pi_next(&pi[poc])) {
					if (pi[poc].layno < maxlayers) {
						e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
						comp_len = comp_len + e;
						if (e == -999) {
							break;
						} else {
							c += e;
						}
					}
				}
				if (e == -999) break;
				if (cp->max_comp_size){
					if (comp_len > cp->max_comp_size){
						e = -999;
						break;
					}
				}
			}
			if (e == -999)  break;
		}
	}else{  /* t2_mode == FINAL_PASS  */
		pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
		while (pi_next(&pi[pino])) {
			if (pi[pino].layno < maxlayers) {
				e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
				if (e == -999) {
					break;
				} else {
					c += e;
				}
				/* INDEX >> */
				if(cstr_info) {
					if(cstr_info->index_write) {
						opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
						opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
						if (!cstr_info->packno) {
							info_PK->start_pos = info_TL->end_header + 1;
						} else {
							info_PK->start_pos = ((cp->tp_on | tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
						}
						info_PK->end_pos = info_PK->start_pos + e - 1;
						info_PK->end_ph_pos += info_PK->start_pos - 1;	/* End of packet header which now only represents the distance 
																														// to start of packet is incremented by value of start of packet*/
					}
					
					cstr_info->packno++;
				}
				/* << INDEX */
				tile->packno++;
			}
		}
	}
	
	pi_destroy(pi, cp, tileno);
	
	if (e == -999) {
		return e;
	}
	
  return (c - dest);
}
Ejemplo n.º 2
0
bool t2_encode_packets(
             opj_t2_t* p_t2,
             OPJ_UINT32 p_tile_no,
             opj_tcd_tile_t *p_tile,
             OPJ_UINT32 p_maxlayers,
             OPJ_BYTE *p_dest,
             OPJ_UINT32 * p_data_written,
             OPJ_UINT32 p_max_len,
             opj_codestream_info_t *cstr_info,
             OPJ_UINT32 p_tp_num,
             OPJ_INT32 p_tp_pos,
             OPJ_UINT32 p_pino,
             J2K_T2_MODE p_t2_mode)
{
  OPJ_BYTE *l_current_data = p_dest;
  OPJ_UINT32 l_nb_bytes = 0;
  OPJ_UINT32 compno;
  OPJ_UINT32 poc;
  opj_pi_iterator_t *l_pi = 00;
  opj_pi_iterator_t *l_current_pi = 00;
  opj_image_t *l_image = p_t2->image;
  opj_cp_t *l_cp = p_t2->cp;
  opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
  OPJ_UINT32 pocno = l_cp->m_specific_param.m_enc.m_cinema == CINEMA4K_24? 2: 1;
  OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1;
  OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;

  l_pi = pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode);
  if
    (!l_pi)
  {
    return false;
  }
  * p_data_written = 0;
  if
    (p_t2_mode == THRESH_CALC )
  { /* Calculating threshold */
    l_current_pi = l_pi;
    for
      (compno = 0; compno < l_max_comp; ++compno)
    {
      OPJ_UINT32 l_comp_len = 0;
      l_current_pi = l_pi;

      for
        (poc = 0; poc < pocno ; ++poc)
      {
        OPJ_UINT32 l_tp_num = compno;
        pi_create_encode(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode);
        while
          (pi_next(l_current_pi))
        {
          if
            (l_current_pi->layno < p_maxlayers)
          {
            l_nb_bytes = 0;
            if
              (! t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info))
            {
              pi_destroy(l_pi, l_nb_pocs);
              return false;
            }
            l_comp_len += l_nb_bytes;
            l_current_data += l_nb_bytes;
            p_max_len -= l_nb_bytes;
            * p_data_written += l_nb_bytes;
          }
        }
        if
          (l_cp->m_specific_param.m_enc.m_max_comp_size)
        {
          if
            (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size)
          {
            pi_destroy(l_pi, l_nb_pocs);
            return false;
          }
        }
        ++l_current_pi;
      }
    }
  }
  else
  {  /* t2_mode == FINAL_PASS  */
    pi_create_encode(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
    l_current_pi = &l_pi[p_pino];
    while
      (pi_next(l_current_pi))
    {
      if
        (l_current_pi->layno < p_maxlayers)
      {
        l_nb_bytes=0;
        if
          (! t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info))
        {
          pi_destroy(l_pi, l_nb_pocs);
          return false;
        }
        l_current_data += l_nb_bytes;
        p_max_len -= l_nb_bytes;
        * p_data_written += l_nb_bytes;

        /* INDEX >> */
        if(cstr_info) {
          if(cstr_info->index_write) {
            opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
            opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
            if (!cstr_info->packno) {
              info_PK->start_pos = info_TL->end_header + 1;
            } else {
              info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
            }
            info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
            info_PK->end_ph_pos += info_PK->start_pos - 1;  // End of packet header which now only represents the distance
                                                            // to start of packet is incremented by value of start of packet
          }

          cstr_info->packno++;
        }
        /* << INDEX */
        ++p_tile->packno;
      }
    }
  }
  pi_destroy(l_pi, l_nb_pocs);
  return true;
}