/* The copyright in this software is being made available under the BSD
 * License, included below. This software may be subject to other third party
 * and contributor rights, including patent rights, and no such rights are
 * granted under this license.  
 *
 * Copyright (c) 2010-2012, ITU/ISO/IEC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "global.h"
#include "memalloc.h"
#include "3dv_acquisition.h"

#define max(a,b)    (((a) > (b)) ? (a) : (b))
#define min(a,b)    (((a) < (b)) ? (a) : (b))


int32 getVarLength(int32 exponent, int32 precision) 
{
	if (exponent==0)
		return max(0, -30+precision);
	else 
		return max(0, exponent-31+precision);
}

void get_exponent_mantissa(ThreeDVAE* threeDV_ae,int voidx)
{
	double arg=fabs(threeDV_ae->original[voidx]);
	double log_val;
	int32 expo,v=0,i;
	double mant;
	int32 exponnent=0;
	int32 mantissa=0;
	int32 bits=0;

	if(threeDV_ae->pred_mode)
	{
		int32 bit;
		int mant_prec=threeDV_ae->precision;
		/* Find expo & mant when arg=(-1)^sign*2^expo*(0.mant),*/
		if (arg==0.0)
		{
			expo=0;
			mant=0;
		} 
		else 
		{
			log_val= log(arg)/log(2.0);
			expo = (int)floor(log_val);
			mant = pow(2.0, log_val - expo);
			if (fabs(mant) > 1.0) 
			{
				mant = mant / (double)2.0;
				expo= expo + 1;
			}		
		}

		/* Convert expo & mant so that X=(-1)^s*2^expo*(1.mant) */
		if (expo == 0 && mant ==0.0 ) 
		{
			v=0;
		} 
		else if (expo> -30)
		{ 
			while ((double)fabs(mant)<(double)1.0) 
			{
				mant=mant*2.0;
				expo=expo-1;
			}
			if (mant>=0.0)
				mant = mant-1.0;
			v= expo + mant_prec; // number of necessary mantissa bits in the case of truncation
			if (v<0)
				v=0;
			expo += 31;
		} 
		else if (expo == -30) 
		{
			v= expo + mant_prec; // number of necessary mantissa bits in the case of truncation
			if (v<0) 
				v=0;
			expo=0;
		}

		exponnent = expo;
		if(v>30)
			v=30;

		/* Convert a float number mant (0<=mant<1) into a binary representation N which is a mantissa with v bits */
		mantissa=0;


		for (i=1; i<=v; i++ )
		{
			bit = (2.0*mant >= 1.0);
			mant = 2.0*mant - (double)bit;
			mantissa = (mantissa << 1 ) | bit ; // MSB of M corresponds to 1/2 and MSB-1 to 1/4 and so on.
		}
		bits=v;
	}
	else
	{
		int32 mantissa_len=threeDV_ae->mantissa_length;
		log_val= log(arg)/log(2.0);
		expo = (int)floor(log_val);
		exponnent=expo+31;

		mantissa=(int32)floor((arg/pow(2,expo)-1)*pow(2,mantissa_len)+0.5);
		bits=mantissa_len;
	}
	threeDV_ae->element[voidx].exponent=exponnent;
	threeDV_ae->element[voidx].mantissa=mantissa;
	threeDV_ae->element[voidx].mantissa_bits=bits;
}
void get_rec_double_type(ThreeDVAE* threeDV_ae, int voidx)
{
	int32 i;
	int32 sgn= (threeDV_ae->element[voidx].sign==0) ? 1 : -1;
	int32 exponent=threeDV_ae->element[voidx].exponent;
	int32 mantissa=threeDV_ae->element[voidx].mantissa;
	int32 exponent_size=(1<<threeDV_ae->exponent_size)-1;
	double *recon=&(threeDV_ae->rec[voidx]);

	assert(exponent!=exponent_size);

	if(threeDV_ae->pred_mode)
	{
		int32 length=getVarLength(exponent,threeDV_ae->precision);


		double factor=1.0;

		if(length>30)
			length=30;
		for(i=0;i<length;i++)
			factor=factor/2;


		*recon=0.0;



		for (i=0;i<(int)length;i++) 
		{
			*recon += factor*((mantissa>>i)&0x01);
			factor *= (double)2.0;
		}
		if (exponent>0 && exponent<exponent_size)
			*recon = (double)sgn*(double)pow((double)2,(double)exponent-31)*(1.0+*recon);
		if (exponent==0)
			*recon = (double)sgn*(double)pow((double)2,(double)-30)*(*recon);	
	}
	else
	{
		int32 mantissa_len=threeDV_ae->mantissa_length;
		*recon=sgn*pow(2,exponent-31)*(1+mantissa/pow(2,mantissa_len));
	}
}
int get_mem_acquisition_info(ThreeDVAcquisitionInfo** threeDV_acquisition_info)
{
	*threeDV_acquisition_info=calloc(1,sizeof(ThreeDVAcquisitionInfo));
	if(*threeDV_acquisition_info==NULL)
		no_mem_exit("get_mem_acquisition_info:*threeDV_acquisition_info");
	(*threeDV_acquisition_info)->focal_length_x_ae=calloc(1,sizeof(ThreeDVAE));
	if((*threeDV_acquisition_info)->focal_length_x_ae==NULL)
		no_mem_exit("get_mem_acquisition_info:(*threeDV_acquisition_info)->focal_length_x_ae");
	(*threeDV_acquisition_info)->focal_length_y_ae=calloc(1,sizeof(ThreeDVAE));
	if((*threeDV_acquisition_info)->focal_length_y_ae==NULL)
		no_mem_exit("get_mem_acquisition_info:(*threeDV_acquisition_info)->focal_length_y_ae");

	(*threeDV_acquisition_info)->principal_point_x_ae=calloc(1,sizeof(ThreeDVAE));
	if((*threeDV_acquisition_info)->principal_point_x_ae==NULL)
		no_mem_exit("get_mem_acquisition_info:(*threeDV_acquisition_info)->principal_point_x_ae");
	(*threeDV_acquisition_info)->principal_point_y_ae=calloc(1,sizeof(ThreeDVAE));
	if((*threeDV_acquisition_info)->principal_point_y_ae==NULL)
		no_mem_exit("get_mem_acquisition_info:(*threeDV_acquisition_info)->principal_point_y_ae");

	(*threeDV_acquisition_info)->translation_ae=calloc(1,sizeof(ThreeDVAE));
	if((*threeDV_acquisition_info)->translation_ae==NULL)
		no_mem_exit("get_mem_acquisition_infO:(*threeDV_acquisition_info)->translation_ae");

	(*threeDV_acquisition_info)->depth_near_ae=calloc(1,sizeof(ThreeDVAE));
	if((*threeDV_acquisition_info)->depth_near_ae==NULL)
		no_mem_exit("get_mem_acquisition_infO:(*threeDV_acquisition_info)->depth_near_ae");
	(*threeDV_acquisition_info)->depth_far_ae=calloc(1,sizeof(ThreeDVAE));
	if((*threeDV_acquisition_info)->depth_far_ae==NULL)
		no_mem_exit("get_mem_acquisition_infO:(*threeDV_acquisition_info)->depth_far_ae");
	return sizeof(ThreeDVAcquisitionInfo)+7*sizeof(ThreeDVAE);
}

void init_acquisition_info(ThreeDVAcquisitionInfo* ThreeDV_acquisition_info)
{
	memset(ThreeDV_acquisition_info->focal_length_x_ae,0x00,sizeof(ThreeDVAE));
	memset(ThreeDV_acquisition_info->focal_length_y_ae,0x00,sizeof(ThreeDVAE));
	memset(ThreeDV_acquisition_info->principal_point_x_ae,0x00,sizeof(ThreeDVAE));
	memset(ThreeDV_acquisition_info->principal_point_y_ae,0x00,sizeof(ThreeDVAE));
	memset(ThreeDV_acquisition_info->translation_ae,0x00,sizeof(ThreeDVAE));
	memset(ThreeDV_acquisition_info->depth_near_ae,0x00,sizeof(ThreeDVAE));
	memset(ThreeDV_acquisition_info->depth_far_ae,0x00,sizeof(ThreeDVAE));
	ThreeDV_acquisition_info->focal_length_x_ae->pred_mode=1;
	ThreeDV_acquisition_info->focal_length_x_ae->delta_flag=0;
	ThreeDV_acquisition_info->focal_length_x_ae->exponent_size=6;

	ThreeDV_acquisition_info->focal_length_y_ae->pred_mode=1;
	ThreeDV_acquisition_info->focal_length_y_ae->delta_flag=0;
	ThreeDV_acquisition_info->focal_length_y_ae->exponent_size=6;

	ThreeDV_acquisition_info->principal_point_x_ae->pred_mode=1;
	ThreeDV_acquisition_info->principal_point_x_ae->delta_flag=0;
	ThreeDV_acquisition_info->principal_point_x_ae->exponent_size=6;

	ThreeDV_acquisition_info->principal_point_y_ae->pred_mode=1;
	ThreeDV_acquisition_info->principal_point_y_ae->delta_flag=0;
	ThreeDV_acquisition_info->principal_point_y_ae->exponent_size=6;

	ThreeDV_acquisition_info->translation_ae->pred_mode=1;
	ThreeDV_acquisition_info->translation_ae->delta_flag=1;
	ThreeDV_acquisition_info->translation_ae->exponent_size=6;

	ThreeDV_acquisition_info->depth_near_ae->pred_mode=0;
	ThreeDV_acquisition_info->depth_near_ae->delta_flag=0;
	ThreeDV_acquisition_info->depth_near_ae->exponent_size=7;

	ThreeDV_acquisition_info->depth_far_ae->pred_mode=0;
	ThreeDV_acquisition_info->depth_far_ae->delta_flag=0;
	ThreeDV_acquisition_info->depth_far_ae->exponent_size=7;

#if NOKIA_DISP_CALC_B0150
	ThreeDV_acquisition_info->disp_param_flag=0;
	memset(ThreeDV_acquisition_info->d_disparity_scale[0],0x00,MAX_CODEVIEW*MAX_CODEVIEW*sizeof(double));
	memset(ThreeDV_acquisition_info->d_disparity_offset[0],0x00,MAX_CODEVIEW*MAX_CODEVIEW*sizeof(double));
	memset(ThreeDV_acquisition_info->i_disparity_scale[0],0x00,MAX_CODEVIEW*MAX_CODEVIEW*sizeof(int));
	memset(ThreeDV_acquisition_info->i_disparity_offset[0],0x00,MAX_CODEVIEW*MAX_CODEVIEW*sizeof(int));
	memset(ThreeDV_acquisition_info->i_disparity_scale_diff[0],0x00,MAX_CODEVIEW*MAX_CODEVIEW*sizeof(int));
	memset(ThreeDV_acquisition_info->i_disparity_offset_diff[0],0x00,MAX_CODEVIEW*MAX_CODEVIEW*sizeof(int));
#endif

}

void copy_acquisition_info(ThreeDVAcquisitionInfo* dest_acquisition_infor, ThreeDVAcquisitionInfo* src_acquisition_info)
{
	memcpy(dest_acquisition_infor->focal_length_x_ae,src_acquisition_info->focal_length_x_ae,sizeof(ThreeDVAE));
	memcpy(dest_acquisition_infor->focal_length_y_ae,src_acquisition_info->focal_length_y_ae,sizeof(ThreeDVAE));
	memcpy(dest_acquisition_infor->principal_point_x_ae,src_acquisition_info->principal_point_x_ae,sizeof(ThreeDVAE));
	memcpy(dest_acquisition_infor->principal_point_y_ae,src_acquisition_info->principal_point_y_ae,sizeof(ThreeDVAE));
	memcpy(dest_acquisition_infor->translation_ae,src_acquisition_info->translation_ae,sizeof(ThreeDVAE));
	memcpy(dest_acquisition_infor->depth_near_ae,src_acquisition_info->depth_near_ae,sizeof(ThreeDVAE));
	memcpy(dest_acquisition_infor->depth_far_ae,src_acquisition_info->depth_far_ae,sizeof(ThreeDVAE));
#if NOKIA_DISP_CALC_B0150
	dest_acquisition_infor->disp_param_flag=src_acquisition_info->disp_param_flag;
	memcpy(dest_acquisition_infor->d_disparity_scale[0],src_acquisition_info->d_disparity_scale[0],MAX_CODEVIEW*MAX_CODEVIEW*sizeof(double));
	memcpy(dest_acquisition_infor->d_disparity_offset[0],src_acquisition_info->d_disparity_offset[0],MAX_CODEVIEW*MAX_CODEVIEW*sizeof(double));
	memcpy(dest_acquisition_infor->i_disparity_scale[0],src_acquisition_info->i_disparity_scale[0],MAX_CODEVIEW*MAX_CODEVIEW*sizeof(int));
	memcpy(dest_acquisition_infor->i_disparity_offset[0],src_acquisition_info->i_disparity_offset[0],MAX_CODEVIEW*MAX_CODEVIEW*sizeof(int));
	memcpy(dest_acquisition_infor->i_disparity_scale_diff[0],src_acquisition_info->i_disparity_scale_diff[0],MAX_CODEVIEW*MAX_CODEVIEW*sizeof(int));
	memcpy(dest_acquisition_infor->i_disparity_offset_diff[0],src_acquisition_info->i_disparity_offset_diff[0],MAX_CODEVIEW*MAX_CODEVIEW*sizeof(int));
#endif
}

/*!
************************************************************************
* \brief
*    free the memory for 3DV acquisition information
************************************************************************
*/
void free_mem_acquisition_info(ThreeDVAcquisitionInfo* threeDV_acqusition_info)
{
	if(threeDV_acqusition_info)
	{
		free(threeDV_acqusition_info->focal_length_x_ae);
		free(threeDV_acqusition_info->focal_length_y_ae);
		free(threeDV_acqusition_info->principal_point_x_ae);
		free(threeDV_acqusition_info->principal_point_y_ae);
		free(threeDV_acqusition_info->translation_ae);
		free(threeDV_acqusition_info->depth_near_ae);
		free(threeDV_acqusition_info->depth_far_ae);
		free(threeDV_acqusition_info);
	}
}
#if SONY_MDFY_SEI_AQINF_M24730
void get_exponent_mantissa_SEI(ThreeDVAESEI* threeDV_ae)
{
	double arg=fabs(threeDV_ae->original);
	double log_val;
	int32 expo,v=0,i;
	double mant;
	int32 exponnent=0;
	int32 mantissa=0;
	int32 bits=0;

	if(threeDV_ae->pred_mode)
	{
		int32 bit;
		int mant_prec=threeDV_ae->precision;
		/* Find expo & mant when arg=(-1)^sign*2^expo*(0.mant),*/
		if (arg==0.0)
		{
			expo=0;
			mant=0;
		} 
		else 
		{
			log_val= log(arg)/log(2.0);
			expo = (int)floor(log_val);
			mant = pow(2.0, log_val - expo);
			if (fabs(mant) > 1.0) 
			{
				mant = mant / (double)2.0;
				expo= expo + 1;
			}		
		}

		/* Convert expo & mant so that X=(-1)^s*2^expo*(1.mant) */
		if (expo == 0 && mant ==0.0 ) 
		{
			v=0;
		} 
		else if (expo> -30)
		{ 
			while ((double)fabs(mant)<(double)1.0) 
			{
				mant=mant*2.0;
				expo=expo-1;
			}
			if (mant>=0.0)
				mant = mant-1.0;
			v= expo + mant_prec; // number of necessary mantissa bits in the case of truncation
			if (v<0)
				v=0;
			expo += 31;
		} 
		else if (expo == -30) 
		{
			v= expo + mant_prec; // number of necessary mantissa bits in the case of truncation
			if (v<0) 
				v=0;
			expo=0;
		}

		exponnent = expo;
		if(v>30)
			v=30;

		/* Convert a float number mant (0<=mant<1) into a binary representation N which is a mantissa with v bits */
		mantissa=0;


		for (i=1; i<=v; i++ )
		{
			bit = (2.0*mant >= 1.0);
			mant = 2.0*mant - (double)bit;
			mantissa = (mantissa << 1 ) | bit ; // MSB of M corresponds to 1/2 and MSB-1 to 1/4 and so on.
		}
		bits=v;
	}
	else
	{
		int32 mantissa_len=threeDV_ae->mantissa_length;
		log_val= log(arg)/log(2.0);
		expo = (int)floor(log_val);
		exponnent=expo+31;

		mantissa=(int32)floor((arg/pow(2,expo)-1)*pow(2,mantissa_len)+0.5);
		bits=mantissa_len;
	}
	threeDV_ae->element.exponent=exponnent;
	threeDV_ae->element.mantissa=mantissa;
	threeDV_ae->element.mantissa_bits=bits;
}
void get_rec_double_type_SEI(ThreeDVAESEI* threeDV_ae)
{
	int32 i;
	int32 sgn= (threeDV_ae->element.sign==0) ? 1 : -1;
	int32 exponent=threeDV_ae->element.exponent;
	int32 mantissa=threeDV_ae->element.mantissa;
	int32 exponent_size=(1<<threeDV_ae->exponent_size)-1;
	double *recon=&(threeDV_ae->rec);

	assert(exponent!=exponent_size);

	if(threeDV_ae->pred_mode)
	{
		int32 length=getVarLength(exponent,threeDV_ae->precision);


		double factor=1.0;

		if(length>30)
			length=30;
		for(i=0;i<length;i++)
			factor=factor/2;


		*recon=0.0;



		for (i=0;i<(int)length;i++) 
		{
			*recon += factor*((mantissa>>i)&0x01);
			factor *= (double)2.0;
		}
		if (exponent>0 && exponent<exponent_size)
			*recon = (double)sgn*(double)pow((double)2,(double)exponent-31)*(1.0+*recon);
		if (exponent==0)
			*recon = (double)sgn*(double)pow((double)2,(double)-30)*(*recon);	
	}
	else
	{
		int32 mantissa_len=threeDV_ae->mantissa_length;
		*recon=sgn*pow(2,exponent-31)*(1+mantissa/pow(2,mantissa_len));
	}
}
#if NOKIA_DEPTH_SEI_C0162
void init_depth_acquisition_info_SEI(DepthAcquisitionInfoSEI* depth_acquisition_info)
{
  depth_acquisition_info->depth_near_ae.pred_mode=0;
  depth_acquisition_info->depth_near_ae.exponent_size=7;

  depth_acquisition_info->depth_far_ae.pred_mode=0;
  depth_acquisition_info->depth_far_ae.exponent_size=7;

  depth_acquisition_info->d_max_ae.pred_mode=0;
  depth_acquisition_info->d_max_ae.exponent_size=7;

  depth_acquisition_info->d_min_ae.pred_mode=0;
  depth_acquisition_info->d_min_ae.exponent_size=7;
}

void init_multiview_acquisition_info_SEI(MultiviewAcquisitionInfoSEI* multiview_acquisition_info)
{
  int i,j;

  multiview_acquisition_info->focal_length_x_ae.pred_mode=1;
  multiview_acquisition_info->focal_length_x_ae.exponent_size=6;

  multiview_acquisition_info->focal_length_y_ae.pred_mode=1;
  multiview_acquisition_info->focal_length_y_ae.exponent_size=6;

  multiview_acquisition_info->principal_point_x_ae.pred_mode=1;
  multiview_acquisition_info->principal_point_x_ae.exponent_size=6;

  multiview_acquisition_info->principal_point_y_ae.pred_mode=1;
  multiview_acquisition_info->principal_point_y_ae.exponent_size=6;

  multiview_acquisition_info->skew_factor_ae.pred_mode=1;
  multiview_acquisition_info->skew_factor_ae.exponent_size=6;

  for (i=0; i<3; i++)
  {
    multiview_acquisition_info->translation_ae[i].pred_mode=1;
    multiview_acquisition_info->translation_ae[i].exponent_size=6;
  }

  for (i=0; i<3; i++)
    for (j=0; j<3; j++)
    {
      multiview_acquisition_info->rotation_ae[i][j].pred_mode=1;
      multiview_acquisition_info->rotation_ae[i][j].exponent_size=6;
    }
}
#else
int get_mem_acquisition_info_SEI(ThreeDVAcquisitionInfoSEI*** threeDV_acquisition_info, int num_frames)
{
	int i;

	*threeDV_acquisition_info=(ThreeDVAcquisitionInfoSEI**)malloc(num_frames*sizeof(ThreeDVAcquisitionInfoSEI*));
	if(*threeDV_acquisition_info==NULL)
		no_mem_exit("get_mem_acquisition_info:*threeDV_acquisition_info");
	for(i=0;i<num_frames;i++){
		(*threeDV_acquisition_info)[i]=(ThreeDVAcquisitionInfoSEI*)calloc(1,sizeof(ThreeDVAcquisitionInfoSEI));
	}

	for(i=0;i<num_frames;i++){
		(*threeDV_acquisition_info)[i]->focal_length_x_ae=calloc(1,sizeof(ThreeDVAESEI));
		if((*threeDV_acquisition_info)[i]->focal_length_x_ae==NULL)
		  no_mem_exit("get_mem_acquisition_info:(*threeDV_acquisition_info)->focal_length_x_ae");
		(*threeDV_acquisition_info)[i]->focal_length_y_ae=calloc(1,sizeof(ThreeDVAESEI));
		if((*threeDV_acquisition_info)[i]->focal_length_y_ae==NULL)
			no_mem_exit("get_mem_acquisition_info:(*threeDV_acquisition_info)->focal_length_y_ae");

		(*threeDV_acquisition_info)[i]->principal_point_x_ae=calloc(1,sizeof(ThreeDVAESEI));
		if((*threeDV_acquisition_info)[i]->principal_point_x_ae==NULL)
		  no_mem_exit("get_mem_acquisition_info:(*threeDV_acquisition_info)->principal_point_x_ae");
		(*threeDV_acquisition_info)[i]->principal_point_y_ae=calloc(1,sizeof(ThreeDVAESEI));
		if((*threeDV_acquisition_info)[i]->principal_point_y_ae==NULL)
		  no_mem_exit("get_mem_acquisition_info:(*threeDV_acquisition_info)->principal_point_y_ae");

		(*threeDV_acquisition_info)[i]->translation_ae=calloc(1,sizeof(ThreeDVAESEI));
		if((*threeDV_acquisition_info)[i]->translation_ae==NULL)
		  no_mem_exit("get_mem_acquisition_infO:(*threeDV_acquisition_info)->translation_ae");

		(*threeDV_acquisition_info)[i]->depth_near_ae=calloc(1,sizeof(ThreeDVAESEI));
		if((*threeDV_acquisition_info)[i]->depth_near_ae==NULL)
			no_mem_exit("get_mem_acquisition_infO:(*threeDV_acquisition_info)->depth_near_ae");
		(*threeDV_acquisition_info)[i]->depth_far_ae=calloc(1,sizeof(ThreeDVAESEI));
		if((*threeDV_acquisition_info)[i]->depth_far_ae==NULL)
			no_mem_exit("get_mem_acquisition_infO:(*threeDV_acquisition_info)->depth_far_ae");

		(*threeDV_acquisition_info)[i]->d_max_ae=calloc(1,sizeof(ThreeDVAESEI));
		if((*threeDV_acquisition_info)[i]->d_max_ae==NULL)
			no_mem_exit("get_mem_acquisition_infO:(*threeDV_acquisition_info)->d_max_ae");
		(*threeDV_acquisition_info)[i]->d_min_ae=calloc(1,sizeof(ThreeDVAESEI));
		if((*threeDV_acquisition_info)[i]->d_min_ae==NULL)
			no_mem_exit("get_mem_acquisition_infO:(*threeDV_acquisition_info)->d_min_ae");

#if NTT_MDFY_SEI_DAQINF_A0140
		(*threeDV_acquisition_info)[i]->rotation_ae = calloc(1, sizeof(ThreeDVAESEI) );
		if((*threeDV_acquisition_info)[i]->rotation_ae==NULL)
			no_mem_exit("get_mem_acquisition_infO:(*threeDV_acquisition_info)->rotation_ae");
#endif
	}
#if NTT_MDFY_SEI_DAQINF_A0140
	return sizeof(ThreeDVAcquisitionInfoSEI)*num_frames+sizeof(ThreeDVAcquisitionInfoSEI)+10*num_frames*sizeof(ThreeDVAESEI);
#else
	return sizeof(ThreeDVAcquisitionInfoSEI)*num_frames+sizeof(ThreeDVAcquisitionInfoSEI)+9*num_frames*sizeof(ThreeDVAESEI);
#endif
}

void init_acquisition_info_SEI(ThreeDVAcquisitionInfoSEI* ThreeDV_acquisition_info)
{
	memset(ThreeDV_acquisition_info->focal_length_x_ae,0x00,sizeof(ThreeDVAESEI));
	memset(ThreeDV_acquisition_info->focal_length_y_ae,0x00,sizeof(ThreeDVAESEI));
	memset(ThreeDV_acquisition_info->principal_point_x_ae,0x00,sizeof(ThreeDVAESEI));
	memset(ThreeDV_acquisition_info->principal_point_y_ae,0x00,sizeof(ThreeDVAESEI));
	memset(ThreeDV_acquisition_info->translation_ae,0x00,sizeof(ThreeDVAESEI));
	memset(ThreeDV_acquisition_info->depth_near_ae,0x00,sizeof(ThreeDVAESEI));
	memset(ThreeDV_acquisition_info->depth_far_ae,0x00,sizeof(ThreeDVAESEI));
	memset(ThreeDV_acquisition_info->d_max_ae,0x00,sizeof(ThreeDVAESEI));
	memset(ThreeDV_acquisition_info->d_min_ae,0x00,sizeof(ThreeDVAESEI));
#if NTT_MDFY_SEI_DAQINF_A0140
	memset(ThreeDV_acquisition_info->rotation_ae,0x00,sizeof(ThreeDVAESEI));
#endif

	ThreeDV_acquisition_info->focal_length_x_ae->pred_mode=1;
	ThreeDV_acquisition_info->focal_length_x_ae->exponent_size=6;

	ThreeDV_acquisition_info->focal_length_y_ae->pred_mode=1;
	ThreeDV_acquisition_info->focal_length_y_ae->exponent_size=6;

	ThreeDV_acquisition_info->principal_point_x_ae->pred_mode=1;
	ThreeDV_acquisition_info->principal_point_x_ae->exponent_size=6;

	ThreeDV_acquisition_info->principal_point_y_ae->pred_mode=1;
	ThreeDV_acquisition_info->principal_point_y_ae->exponent_size=6;

	ThreeDV_acquisition_info->translation_ae->pred_mode=1;
	ThreeDV_acquisition_info->translation_ae->exponent_size=6;

	ThreeDV_acquisition_info->depth_near_ae->pred_mode=0;
	ThreeDV_acquisition_info->depth_near_ae->exponent_size=7;

	ThreeDV_acquisition_info->depth_far_ae->pred_mode=0;
	ThreeDV_acquisition_info->depth_far_ae->exponent_size=7;

	ThreeDV_acquisition_info->d_max_ae->pred_mode=0;
	ThreeDV_acquisition_info->d_max_ae->exponent_size=7;

	ThreeDV_acquisition_info->d_min_ae->pred_mode=0;
	ThreeDV_acquisition_info->d_min_ae->exponent_size=7;

#if NTT_MDFY_SEI_DAQINF_A0140
	ThreeDV_acquisition_info->rotation_ae->pred_mode=1;
	ThreeDV_acquisition_info->rotation_ae->exponent_size=6;
#endif
}


/*!
************************************************************************
* \brief
*    free the memory for 3DV acquisition information
************************************************************************
*/
void free_mem_acquisition_info_SEI(ThreeDVAcquisitionInfoSEI* threeDV_acqusition_info)
{
	if(threeDV_acqusition_info)
	{
		free(threeDV_acqusition_info->focal_length_x_ae);
		free(threeDV_acqusition_info->focal_length_y_ae);
		free(threeDV_acqusition_info->principal_point_x_ae);
		free(threeDV_acqusition_info->principal_point_y_ae);
		free(threeDV_acqusition_info->translation_ae);
		free(threeDV_acqusition_info->depth_near_ae);
		free(threeDV_acqusition_info->depth_far_ae);
		free(threeDV_acqusition_info->d_min_ae);
		free(threeDV_acqusition_info->d_max_ae);
#if NTT_MDFY_SEI_DAQINF_A0140
		free(threeDV_acqusition_info->rotation_ae);
#endif
		free(threeDV_acqusition_info);
	}
}
#endif
#endif

