// ****************** Copyright (c) 2013 by Gigaset Communications GmbH ******
// Filename : tdav_codec_fsl_amr.c
// Owner    : Souparna Lodh [STC]
//-------------------------------------------------------------------------
// Defintion : FSL AMR-NB and AMR-WB codecs.
// RTP payloader/depayloader are based on RFC 4867
//-------------------------------------------------------------------------
// 30-07-2013, Gigaset Communications GmbH
// Added FSL AMR-NB and AMR-WB codec plugin to Doubango Framework
//*************************************************************************

/*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

#include "tinydav/codecs/amr/tdav_codec_fsl_amr.h"
#include "tdav_codec_fsl_amr_wrapper.h"

#include "tsk_params.h"
#include "tsk_string.h"
#include "tsk_memory.h"
#include "tsk_debug.h"

#include <stdlib.h> /* atoi() */

#define NO_DATA 15
#define DEFAULT_ENC_MODE ((enum Mode)MR122) /* Higher, could be changed by remote party by using CMR */

#define TDAV_CODEC_FSL_AMR(self)	((tdav_codec_fsl_amr_t*)(self))

typedef enum tdav_codec_fsl_amr_type_e
{
	tdav_codec_fsl_amr_type_nb,
	tdav_codec_fsl_amr_type_wb,
}
tdav_codec_fsl_amr_type_t;

typedef enum tdav_codec_fsl_amr_mode_e
{
	tdav_codec_fsl_amr_mode_oa,
	tdav_codec_fsl_amr_mode_be,
}
tdav_codec_fsl_amr_mode_t;

/** Base class for all AMR codecs */
typedef struct tdav_codec_fsl_amr_s
{
	TMEDIA_DECLARE_CODEC_AUDIO;

	tdav_codec_fsl_amr_type_t type;
	tdav_codec_fsl_amr_mode_t mode;

	enum Mode encoder_mode;
	AMREncWrapper_t *pEncWrapper;
    AMRDecWrapper_t *pDecWrapper;
    
	unsigned modes:16; /**< 0..7 for NB and 0..8 for WB plus SID, SPEECH_LOST, NO_DATA etc etc */
	unsigned mcp:2; /**< mode-change-periode (1 or 2) */
	unsigned mcc:2; /**< mode-change-capability (1 or 2) */
	unsigned mcn:1; /**< mode-change-neighnor (0 or 1) */
	unsigned crc:1; /**< 0 or 1 */
	unsigned robust_sorting:1; /**< robust-sorting (0 or 1) */
}
tdav_codec_fsl_amr_t;

#define TDAV_DECLARE_CODEC_FSL_AMR tdav_codec_fsl_amr_t __codec_amr__

/* From WmfDecBytesPerFrame in dec_input_format_tab.cpp */
static const int tdav_codec_fsl_amr_nb_sizes[] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 6, 5, 5, 0, 0, 0, 0 };
/* From pvamrwbdecoder_api.h, by dividing by 8 and rounding up */
static const int tdav_codec_fsl_amr_wb_sizes[] = { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, -1, -1 };

/* ============ Common ================= */
static int tdav_codec_fsl_amr_init(tdav_codec_fsl_amr_t* self, tdav_codec_fsl_amr_type_t type, tdav_codec_fsl_amr_mode_t mode);
static int tdav_codec_fsl_amr_deinit(tdav_codec_fsl_amr_t* self);
static tdav_codec_fsl_amr_mode_t tdav_codec_fsl_amr_get_mode(const char* fmtp);
static int tdav_codec_fsl_amr_parse_fmtp(tdav_codec_fsl_amr_t* self, const char* fmtp);
static tsk_size_t tdav_codec_fsl_amr_oa_decode(tdav_codec_fsl_amr_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr);
static tsk_size_t tdav_codec_fsl_amr_be_decode(tdav_codec_fsl_amr_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr);
static tsk_size_t tdav_codec_fsl_amr_be_encode(tdav_codec_fsl_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size);
static tsk_size_t tdav_codec_fsl_amr_oa_encode(tdav_codec_fsl_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size);
static uint8_t tdav_codec_fsl_amr_bitbuffer_read(const void* bits, tsk_size_t size, tsk_size_t start, tsk_size_t count);

/* ============ AMR-NB Plugin interface ================= 
	The AMR codec was originally developed and standardized by the
	European Telecommunications Standards Institute (ETSI) for GSM
	cellular systems.  It is now chosen by the Third Generation
	Partnership Project (3GPP) as the mandatory codec for third
	generation (3G) cellular systems [1].

	The AMR codec is a multi-mode codec that supports eight narrow band
	speech encoding modes with bit rates between 4.75 and 12.2 kbps.  The
	sampling frequency used in AMR is 8000 Hz and the speech encoding is
	performed on 20 ms speech frames.  Therefore, each encoded AMR speech
	frame represents 160 samples of the original speech.

	Among the eight AMR encoding modes, three are already separately
	adopted as standards of their own.  Particularly, the 6.7 kbps mode
	is adopted as PDC-EFR [18], the 7.4 kbps mode as IS-641 codec in TDMA
	[17], and the 12.2 kbps mode as GSM-EFR [16].
*/

int tdav_codec_fsl_amrnb_open(tmedia_codec_t* self)
{
    __android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amrnb_open in\n");
	tdav_codec_fsl_amr_t* amrnb = (tdav_codec_fsl_amr_t*)self;

	switch(amrnb->type){
		case tdav_codec_fsl_amr_type_nb: {
            if(!amrnb->pEncWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init attempt CreateAMREncWrapper nb\n");
                amrnb->pEncWrapper = CreateAMREncWrapper(tsk_true);
            }
            if(!amrnb->pEncWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init out CreateAMREncWrapper nb NULL\n");
                return -1;
            }

            if(!amrnb->pDecWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init attempt CreateAMRDecWrapper nb\n");
                amrnb->pDecWrapper = CreateAMRDecWrapper(tsk_true);
            }
            if(!amrnb->pDecWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init out CreateAMRDecWrapper nb NULL\n");
                return -1;
            }
            break;
        }
        case tdav_codec_fsl_amr_type_wb: {
            if(!amrnb->pEncWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init attempt CreateAMREncWrapper wb\n");
                amrnb->pEncWrapper = CreateAMREncWrapper(tsk_false);
            }
            if(!amrnb->pEncWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init out CreateAMREncWrapper wb NULL\n");
                return -1;
            }

            if(!amrnb->pDecWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init attempt CreateAMRDecWrapper wb\n");
                amrnb->pDecWrapper = CreateAMRDecWrapper(tsk_false);
            }
            if(!amrnb->pDecWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init out CreateAMRDecWrapper wb NULL\n");
                return -1;
            }
            break;
        }
    }

    if(amrnb->pEncWrapper) {
        if(amrnb->pEncWrapper->InstanceInit(amrnb->pEncWrapper)) {
            TSK_DEBUG_ERROR("Failed to initialize AMR-NB encoder instance");
            //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amrnb_open out InstanceInit failed\n");
            return -1;
        }
        if(amrnb->pEncWrapper->CodecInit(amrnb->pEncWrapper)) {
            TSK_DEBUG_ERROR("Failed to initialize AMR-NB encoder");
            //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amrnb_open out CodecInit failed\n");
            return -1;
        }
    }

    if(amrnb->pDecWrapper) {
        if(amrnb->pDecWrapper->InstanceInit(amrnb->pDecWrapper)) {
            TSK_DEBUG_ERROR("Failed to initialize AMR-NB decoder instance");
            //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amrnb_open out InstanceInit failed\n");
            return -1;
        }
        if(amrnb->pDecWrapper->CodecInit(amrnb->pDecWrapper)) {
            TSK_DEBUG_ERROR("Failed to initialize AMR-NB decoder");
            //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amrnb_open out CodecInit failed\n");
            return -1;
        }
    }

    __android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amrnb_open out\n");
	return 0;
}

int tdav_codec_fsl_amrnb_close(tmedia_codec_t* self)
{
	tdav_codec_fsl_amr_t* amrnb = (tdav_codec_fsl_amr_t*)self;

	if(amrnb->pEncWrapper) {
        amrnb->pEncWrapper->InstanceDeInit(amrnb->pEncWrapper);
        DeleteAMREncWrapper(amrnb->pEncWrapper);
		amrnb->pEncWrapper = tsk_null;
    }

    if(amrnb->pDecWrapper) {
        amrnb->pDecWrapper->InstanceDeInit(amrnb->pDecWrapper);
        DeleteAMRDecWrapper(amrnb->pDecWrapper);
		amrnb->pDecWrapper = tsk_null;
    }

	return 0;
}

tsk_size_t tdav_codec_fsl_amrnb_encode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size)
{	
	tdav_codec_fsl_amr_t* amr = (tdav_codec_fsl_amr_t*)self;
	
	switch(amr->mode){
		case tdav_codec_fsl_amr_mode_be:
			return tdav_codec_fsl_amr_be_encode(amr, in_data, in_size, out_data, out_max_size);
		default:
			return tdav_codec_fsl_amr_oa_encode(amr, in_data, in_size, out_data, out_max_size); 
	}
	
	return 0;
}

tsk_size_t tdav_codec_fsl_amrnb_decode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr)
{
	tdav_codec_fsl_amr_t* amr = (tdav_codec_fsl_amr_t*)self;

	switch(amr->mode){
		case tdav_codec_fsl_amr_mode_be:
			return tdav_codec_fsl_amr_be_decode(amr, in_data, in_size, out_data, out_max_size, proto_hdr);
		default:
			return tdav_codec_fsl_amr_oa_decode(amr, in_data, in_size, out_data, out_max_size, proto_hdr); 
	}
}

char* tdav_codec_fsl_amrnb_sdp_att_get(const tmedia_codec_t* codec, const char* att_name)
{
	const tdav_codec_fsl_amr_t* amr = (const tdav_codec_fsl_amr_t*)codec;

	/* We support all modes, all ... */
	if(amr){
		switch(amr->mode){
			case tdav_codec_fsl_amr_mode_be:
				return tsk_strdup("octet-align=0");
			default:
				return tsk_strdup("octet-align=1");
		}
	}
	return tsk_null;
}

tsk_bool_t tdav_codec_fsl_amrnb_sdp_att_match(const tmedia_codec_t* codec, const char* att_name, const char* att_value)
{	
	tdav_codec_fsl_amr_t* amr;
	if(!(amr = (tdav_codec_fsl_amr_t*)codec) || !att_name){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_false;
	}

	if(amr && tsk_striequals(att_name, "fmtp")){
		/* Match mode */
		if(tdav_codec_fsl_amr_get_mode(att_value) != amr->mode){
			TSK_DEBUG_INFO("Failed to match [%s]", att_value);
			return tsk_false;
		}
		/* check parameters validity */
		if(tdav_codec_fsl_amr_parse_fmtp(amr, att_value)){
			TSK_DEBUG_INFO("Failed to match [%s]", att_value);
			return tsk_false;
		}

		return tsk_true;
	}
	return tsk_false;
}


//
//	AMR-NB OA Plugin definition
//

/* constructor */
static tsk_object_t* tdav_codec_fsl_amrnb_oa_ctor(tsk_object_t * self, va_list * app)
{
	tdav_codec_fsl_amr_t *amrnb_oa = self;
	if(amrnb_oa){
		/* init base: called by tmedia_codec_create() */
		/* init self */
		tdav_codec_fsl_amr_init(TDAV_CODEC_FSL_AMR(amrnb_oa), tdav_codec_fsl_amr_type_nb, tdav_codec_fsl_amr_mode_oa);
	}
	return self;
}
/* destructor */
static tsk_object_t* tdav_codec_fsl_amrnb_oa_dtor(tsk_object_t * self)
{ 
	tdav_codec_fsl_amr_t *amrnb_oa = self;
	if(amrnb_oa){
		/* deinit base */
		tmedia_codec_audio_deinit(amrnb_oa);
		/* deinit self */
		tdav_codec_fsl_amr_deinit(TDAV_CODEC_FSL_AMR(amrnb_oa));
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tdav_codec_fsl_amrnb_oa_def_s = 
{
	sizeof(tdav_codec_fsl_amr_t),
	tdav_codec_fsl_amrnb_oa_ctor, 
	tdav_codec_fsl_amrnb_oa_dtor,
	tmedia_codec_cmp, 
};
/* plugin definition*/
static const tmedia_codec_plugin_def_t tdav_codec_fsl_amrnb_oa_plugin_def_s = 
{
	&tdav_codec_fsl_amrnb_oa_def_s,

	tmedia_audio,
	tmedia_codec_id_amr_nb_oa,
	"AMR",
	"AMR Narrow Band - Octet Aligned",
	TMEDIA_CODEC_FORMAT_AMR_NB_OA,
	tsk_true,
	8000, // rate
	
	{ /* audio */
		1, // channels
		20 // ptime
	},

	/* video */
	{0},

	tsk_null, // set()
	tdav_codec_fsl_amrnb_open,
	tdav_codec_fsl_amrnb_close,
	tdav_codec_fsl_amrnb_encode,
	tdav_codec_fsl_amrnb_decode,
	tdav_codec_fsl_amrnb_sdp_att_match,
	tdav_codec_fsl_amrnb_sdp_att_get
};
const tmedia_codec_plugin_def_t *tdav_codec_fsl_amrnb_oa_plugin_def_t = &tdav_codec_fsl_amrnb_oa_plugin_def_s;

//
//	AMR-NB BE Plugin definition
//

/* constructor */
static tsk_object_t* tdav_codec_fsl_amrnb_be_ctor(tsk_object_t * self, va_list * app)
{
	tdav_codec_fsl_amr_t *amrnb_be = self;
	if(amrnb_be){
		/* init base: called by tmedia_codec_create() */
		/* init self */
		tdav_codec_fsl_amr_init(TDAV_CODEC_FSL_AMR(amrnb_be), tdav_codec_fsl_amr_type_nb, tdav_codec_fsl_amr_mode_be);
	}
	return self;
}
/* destructor */
static tsk_object_t* tdav_codec_fsl_amrnb_be_dtor(tsk_object_t * self)
{ 
	tdav_codec_fsl_amr_t *amrnb_be = self;
	if(amrnb_be){
		/* deinit base */
		tmedia_codec_audio_deinit(amrnb_be);
		/* deinit self */
		tdav_codec_fsl_amr_deinit(TDAV_CODEC_FSL_AMR(amrnb_be));
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tdav_codec_fsl_amrnb_be_def_s = 
{
	sizeof(tdav_codec_fsl_amr_t),
	tdav_codec_fsl_amrnb_be_ctor, 
	tdav_codec_fsl_amrnb_be_dtor,
	tmedia_codec_cmp, 
};
/* plugin definition*/
static const tmedia_codec_plugin_def_t tdav_codec_fsl_amrnb_be_plugin_def_s = 
{
	&tdav_codec_fsl_amrnb_be_def_s,

	tmedia_audio,
	tmedia_codec_id_amr_nb_be,
	"AMR",
	"AMR Narrow Band - Bandwidth-Efficient",
	TMEDIA_CODEC_FORMAT_AMR_NB_BE,
	tsk_true,
	8000, // rate
	
	{ /* audio */
		1, // channels
		20 // ptime
	},

	/* video */
	{0},

	tsk_null, // set()
	tdav_codec_fsl_amrnb_open,
	tdav_codec_fsl_amrnb_close,
	tdav_codec_fsl_amrnb_encode,
	tdav_codec_fsl_amrnb_decode,
	tdav_codec_fsl_amrnb_sdp_att_match,
	tdav_codec_fsl_amrnb_sdp_att_get
};
const tmedia_codec_plugin_def_t *tdav_codec_fsl_amrnb_be_plugin_def_t = &tdav_codec_fsl_amrnb_be_plugin_def_s;









//
// Common functions
//

static int tdav_codec_fsl_amr_init(tdav_codec_fsl_amr_t* self, tdav_codec_fsl_amr_type_t type, tdav_codec_fsl_amr_mode_t mode)
{
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init in\n");
	if(self){
		self->type = type;
		self->mode = mode;
		self->encoder_mode = DEFAULT_ENC_MODE;

        //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init out\n");
		return 0;
	}
	else{
		TSK_DEBUG_ERROR("Invalid Parameter");
        //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_init out with self == NULL\n");
		return -1;
	}
}

static int tdav_codec_fsl_amr_deinit(tdav_codec_fsl_amr_t* self)
{
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_deinit in\n");
	if(self){
        //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_deinit out\n");
		return 0;
	}
	else{
		TSK_DEBUG_ERROR("Invalid Parameter");
        //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_deinit out with self == NULL\n");
		return -1;
	}
}

static tdav_codec_fsl_amr_mode_t tdav_codec_fsl_amr_get_mode(const char* fmtp)
{
	/* RFC 4867 - 8.1.  AMR Media Type Registration
		octet-align: Permissible values are 0 and 1.  If 1, octet-aligned
		operation SHALL be used.  If 0 or if not present, bandwidth-efficient operation is employed.
	*/
	tdav_codec_fsl_amr_mode_t mode = tdav_codec_fsl_amr_mode_be;
	tsk_size_t size = tsk_strlen(fmtp);
	int start, end;
	
	if((start = tsk_strindexOf(fmtp, size, "octet-align")) !=-1){
		tsk_param_t* param;
		if((end = tsk_strindexOf((fmtp+start), (size-start), ";")) == -1){
			end = size;
		}
		if((param = tsk_params_parse_param((fmtp+start), (end-start)))){
			if(param->value && tsk_strequals(param->value, "1")){
				mode = tdav_codec_fsl_amr_mode_oa;
			}
			TSK_OBJECT_SAFE_FREE(param);
		}
	}
	return mode;
}

int tdav_codec_fsl_amr_parse_fmtp(tdav_codec_fsl_amr_t* self, const char* fmtp)
{
	int ret = 0;
	int val_int;
	const char* val_str;
	//--tdav_codec_fsl_amr_mode_t mode = self->mode;
	tsk_params_L_t* params = tsk_null;

	if((params = tsk_params_fromstring(fmtp, ";", tsk_true))){
		/* Do not check "octet-align" => already done by the caller of this function */
		
		/* === mode-set ===*/
		if((val_str = tsk_params_get_param_value(params, "mode-set"))){
			char* modes = tsk_strdup(val_str);
			char* pch;
			int mode_int;
			pch = strtok(modes, ", ");
			while(pch){
				mode_int = atoi(pch);
				self->modes |= 0x0001 << mode_int;
				pch = strtok(tsk_null, ", ");
			}
			
			TSK_FREE(modes);
		}
		else{
			self->modes = 0xFFFF;
		}

		/* === interleaving ===*/
		if((val_int = tsk_params_get_param_value_as_int(params, "interleaving")) != -1){
			TSK_DEBUG_WARN("interleaving not supported");
			ret = -1; goto bail;
		}
		/* === mode-change-period ===*/
		if((val_int = tsk_params_get_param_value_as_int(params, "mode-change-period")) != -1){
			if(val_int != 1 && val_int != 2){
				TSK_DEBUG_ERROR("Invalid [mode-change-period]");
				ret = -1; goto bail;
			}
			self->mcp = (unsigned)val_int;
		}
		/* === mode-change-capability ===*/
		if((val_int = tsk_params_get_param_value_as_int(params, "mode-change-capability")) != -1){
			if(val_int != 1 && val_int != 2){
				TSK_DEBUG_ERROR("Invalid [mode-change-capability]");
				ret = -1; goto bail;
			}
			self->mcc = (unsigned)val_int;
		}
		/* === mode-change-neighbor ===*/
		if((val_int = tsk_params_get_param_value_as_int(params, "mode-change-neighbor")) != -1){
			if(val_int != 0 && val_int != 1){
				TSK_DEBUG_ERROR("Invalid [mode-change-neighbor]");
				ret = -1; goto bail;
			}
			self->mcn = (unsigned)val_int;
		}
	}

bail:
	TSK_OBJECT_SAFE_FREE(params);
	return ret;
}


/* RFC 4867 - 4.2.  Payload Structure
	+----------------+-------------------+----------------
	| payload header | table of contents | speech data ...
	+----------------+-------------------+----------------
*/
/*	RFC 4867 - 4.4.2.  The Payload Table of Contents and Frame CRCs
		The table of contents (ToC) consists of a list of ToC entries, each representing a speech frame.
		+---------------------+
		| list of ToC entries |
		+---------------------+
		| list of frame CRCs  | (optional)
		- - - - - - - - - - -
		Note, for ToC entries with FT=14 or 15, there will be no
		corresponding speech frame or frame CRC present in the payload.
*/


static tsk_size_t tdav_codec_fsl_amr_be_encode(tdav_codec_fsl_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size)
{	
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_encode in\n");
	tsk_size_t out_size = 0, i;
	//int ret_size;
	uint8_t ToC;
	static uint8_t CMR = NO_DATA /* No interleaving */;

	uint8_t outbuf[60 + 1]; /* enought for both NB and WB at ptime=20ms */
	if(!amr || !in_data || !in_size || !out_data || (amr->mode != tdav_codec_fsl_amr_mode_be)){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	/* Encode */
	
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_encode before encode call - in_size = %lu\n", in_size);

	int32_t ret = 0;
    ret = amr->pEncWrapper->encodeFrame(amr->pEncWrapper, (uint8_t *)in_data, amr->encoder_mode, outbuf, &in_size, &out_size);

    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_encode after encode call - in_size = %lu\n", in_size);

    if(!ret && out_size) {
        //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_encode Sucess - out_size = %lu\n", out_size);
    } else {
        //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_encode returned error \n");
        TSK_DEBUG_ERROR("Encoding failed");
        goto bail;
    }

	/* allocate output buffer */
	if((int)*out_max_size < out_size/*ret_size*/){
		if(!(*out_data = tsk_realloc(*out_data, out_size/*ret_size*/))){
			*out_max_size = 0;
			TSK_DEBUG_ERROR("Failed to allocate new buffer");
			goto bail;
		}
		*out_max_size = out_size/*ret_size*/;
	}

	//out_size = ret_size;

	/* CMR (4bits) */
	((uint8_t*)*out_data)[0] = (CMR<<4);
	/* ToC (Always ONE Frame, don't need to test for [F]) (6bits)*/
	ToC = outbuf[0]>>2/*2*[P]*/;
	((uint8_t*)*out_data)[0] |=  (ToC >> 2/*[Q],[1-FT]*/) & 0xF; /* 4bits */
	((uint8_t*)*out_data)[1] = (ToC & 0x3/*[1-FT],[Q]*/)<<6; /* 2bits */

	/* === THERE ARE 2 EXTRA BITS === */

	for(i=1; i<out_size-1; i++){
		((uint8_t*)*out_data)[i] |= outbuf[i]>>2;/* 6bits */
		((uint8_t*)*out_data)[i+1] = outbuf[i]<<6;/* 2bits */
	}

bail:
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_encode out - with out_siz=%lu\n", out_size);
	return out_size;
}

tsk_size_t tdav_codec_fsl_amr_be_decode(tdav_codec_fsl_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr)
{
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode in\n");
	tsk_size_t out_size = 0, pcm_frame_size = 0, index = 0;
	const uint8_t* pdata = (const uint8_t*)in_data;
	//--const uint8_t* pend = (pdata + in_size);
	uint8_t CMR;
	int toc_entries = 0, i, k; // ToC entries count

	if(!amr || !in_data || !in_size || !out_data || (amr->mode != tdav_codec_fsl_amr_mode_be)){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	/* compute PCM frame size */
	switch(TDAV_CODEC_FSL_AMR(amr)->type){
		case tdav_codec_fsl_amr_type_nb:
			pcm_frame_size = 160 * sizeof(short);
			break;
		case tdav_codec_fsl_amr_type_wb:
			pcm_frame_size = 320 * sizeof(short);
			break;
		default:
			TSK_DEBUG_ERROR("Invalid AMR type");
			return 0;
	}

    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode - 1\n");

	/* CMR (4bits) */
	CMR = tdav_codec_fsl_amr_bitbuffer_read(in_data, (in_size*8), index, 4);
	index += 4;
	if(CMR != NO_DATA){
		amr->encoder_mode = (enum Mode)CMR;
	}
	
	/* F(1bit), FT(4bits), Q(1bit) */
	/* count ToC entries */
	do{	/* At least ONE ToC */
		++toc_entries;
		++pdata;
		index += 6;
	}
	while((index < (in_size*8)) && (tdav_codec_fsl_amr_bitbuffer_read(in_data, (in_size*8), (index-6), 1)/* F */));
	
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode - 2\n");

	for(i = 0; (i<toc_entries && (in_size < (in_size*8))) ; i++){
		int size = -1;
		uint8_t* speech_data = tsk_null;
		//--int speech_data_size = 0;
		uint8_t ToC = tdav_codec_fsl_amr_bitbuffer_read(in_data, (in_size*8), 4/*CMR*/ + (i*6), 6);
		
		switch(TDAV_CODEC_FSL_AMR(amr)->type){
			case tdav_codec_fsl_amr_type_nb:
				size = tdav_codec_fsl_amr_nb_sizes[(ToC>>1)&0x0F/* FT */];
				break;
			case tdav_codec_fsl_amr_type_wb:
				size = tdav_codec_fsl_amr_wb_sizes[(ToC>>1)&0x0F/* FT */];
				break;
		}		
		
		if((speech_data = tsk_calloc((size + 2/* ToC + '\0' */), sizeof(uint8_t)))){
			/* copy ToC */
			speech_data[0] = (ToC & 0x1F)<<2/* 2*[P] */;  /* ToC as OA layout */
			/* copy speech data */
			for(k=0; k<size; k++){
				speech_data[1 + k] = tdav_codec_fsl_amr_bitbuffer_read(in_data, (in_size*8), index, 8);
				index+=8;
				if((k==size-1) && (index%8)){
					speech_data[1 + k] <<= (8-(index%8)); //clean
				}
			}
			
			/* allocate/reallocate speech data */
			if(*out_max_size <(out_size + pcm_frame_size)){
				if(!(*out_data = tsk_realloc(*out_data, (out_size + pcm_frame_size)))){
					TSK_DEBUG_ERROR("Failed to allocate new buffer");
					*out_max_size = 0;
					TSK_FREE(speech_data);
					goto bail;
				}
				*out_max_size = out_size + pcm_frame_size;
			}

            //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode - 3\n");
			/* decode speech data */
			uint32_t consumedBytes = -1;
            int32_t ret = 0;

            if(!amr || !amr->pDecWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode somethign severly wrong!!!\n");
            }

            //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode before decodeFrame call amr = %lu, amr->pDecWrapper = %lu\n", amr, amr->pDecWrapper);

            ret = amr->pDecWrapper->decodeFrame(amr->pDecWrapper, speech_data, &((uint8_t *)*out_data)[out_size/sizeof(uint8_t)], size + 2, &consumedBytes);

            //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode returned = %ld\n", ret);

            if(!ret) {
                out_size += pcm_frame_size;
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode success Frame No: %d of TOCs: %d\n", i, toc_entries);
            } else {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode returned error Frame No: %d of TOCs: %d\n", i, toc_entries);
                TSK_DEBUG_ERROR("Decoding failed");
            }

            pdata+= size;

			TSK_FREE(speech_data);
		}
	}

bail:
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_be_decode out out_size = %lu\n", out_size);
	return out_size;
}

static tsk_size_t tdav_codec_fsl_amr_oa_encode(tdav_codec_fsl_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size)
{	
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_encode in\n");
	tsk_size_t out_size = 0;
	uint32_t ret_size;
	static uint8_t CMR = NO_DATA /* No interleaving */;

	uint8_t outbuf[60 + 1]; /* enought for both NB and WB at ptime=20ms */
	if(!amr || !in_data || !in_size || !out_data || (amr->mode != tdav_codec_fsl_amr_mode_oa)){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	/* Encode */
	
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_encode before encode call - in_size = %lu\n", in_size);

    int32_t ret = 0;
    ret = amr->pEncWrapper->encodeFrame(amr->pEncWrapper, (uint8_t *)in_data, amr->encoder_mode, outbuf, &in_size, &ret_size);

    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_encode after encode call - in_size = %lu\n", in_size);

    if(!ret && ret_size) {
        //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_encode Sucess - output buff size = %lu\n", ret_size);
    } else {
        //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_encode returned error \n");
        TSK_DEBUG_ERROR("Encoding failed");
        goto bail;
    }
	
	out_size = ret_size + 1 /* CMR without interleaving */;
	/* allocate output buffer */
	if(*out_max_size <out_size){
		if(!(*out_data = tsk_realloc(*out_data, out_size))){
			TSK_DEBUG_ERROR("Failed to allocate new buffer");
			*out_max_size = out_size = 0;
			goto bail;
		}
		*out_max_size = out_size;
	}

	/* CMR */
	((uint8_t*)*out_data)[0] = (CMR << 4);
	/* Only ONE ToC --> believe me */
	memcpy(&((uint8_t*)*out_data)[1], outbuf, ret_size);

bail:
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_encode out - with out_siz=%lu\n", out_size);
	return out_size;
}

static tsk_size_t tdav_codec_fsl_amr_oa_decode(tdav_codec_fsl_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr)
{
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode in\n");
	tsk_size_t out_size = 0, pcm_frame_size = 0;
	const uint8_t* pdata = (const uint8_t*)in_data;
	const uint8_t* pend = (pdata + in_size);
	uint8_t CMR;
	int toc_entries = 0, i; // ToC entries count

	if(!amr || !in_data || !in_size || !out_data || (amr->mode != tdav_codec_fsl_amr_mode_oa)){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	/* compute PCM frame size */
	switch(TDAV_CODEC_FSL_AMR(amr)->type){
		case tdav_codec_fsl_amr_type_nb:
			pcm_frame_size = 160 * sizeof(short);
			break;
		case tdav_codec_fsl_amr_type_wb:
			pcm_frame_size = 320 * sizeof(short);
			break;
		default:
			TSK_DEBUG_ERROR("Invalid AMR type");
			return 0;
	}	

    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode - 1\n");

	/* RFC 4867 - 4.4.  Octet-Aligned Mode
		In octet-aligned mode, the payload header consists of a 4-bit CMR, 4
	   reserved bits, and optionally, an 8-bit interleaving header, as shown
	   below:

		0                   1
		0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
	   +-+-+-+-+-+-+-+-+- - - - - - - -
	   |  CMR  |R|R|R|R|  ILL  |  ILP  |
	   +-+-+-+-+-+-+-+-+- - - - - - - -

	   CMR (4 bits): same as defined in Section 4.3.1.

	   "interleaving" not supported ==> could ignore ILL and ILP (wich are optional)
	*/
	
	CMR = (*pdata++ >> 4);
	if(CMR != NO_DATA){
		/* The codec mode request received in the CMR field is valid until the
		next codec mode request is received, i.e., a newly received CMR value
		corresponding to a speech mode, or NO_DATA overrides the previously
		received CMR value corresponding to a speech mode or NO_DATA. */
		amr->encoder_mode = (enum Mode)CMR; // As we support all modes, do not check for validity
	}
	
	/*	
		A ToC entry takes the following format in octet-aligned mode:
		0 1 2 3 4 5 6 7
		+-+-+-+-+-+-+-+-+
		|F|  FT   |Q|P|P|
		+-+-+-+-+-+-+-+-+

		F (1 bit): see definition in Section 4.3.2.
		FT (4 bits, unsigned integer): see definition in Section 4.3.2.
		Q (1 bit): see definition in Section 4.3.2.
		P bits: padding bits, MUST be set to zero, and MUST be ignored on reception.
	*/	

	/* count ToC entries */
	do{	/* At least ONE ToC */
		++toc_entries;
		++pdata;
	}
	while(pdata && (pdata < pend) && (pdata[-1] >> 7/* F */));

    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode - 2\n");

	for(i = 0; (i<toc_entries && (pdata < pend)) ; i++){
		int size = -1;
		uint8_t* speech_data = tsk_null;
		//--int speech_data_size = 0;
		uint8_t ToC = ((const uint8_t*)in_data)[1/*CMR...*/ + i];
		switch(TDAV_CODEC_FSL_AMR(amr)->type){
			case tdav_codec_fsl_amr_type_nb:
				size = tdav_codec_fsl_amr_nb_sizes[(ToC>>3) & 0x0F/* FT */];
				break;
			case tdav_codec_fsl_amr_type_wb:
				size = tdav_codec_fsl_amr_wb_sizes[(ToC>>3) & 0x0F/* FT */];
				break;
		}
		
		/* check size */
		if(size <0 || ((pdata + size) > pend)){
			TSK_DEBUG_ERROR("Invalid size");
			break;
		}
		
        //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode - 3\n");

		if((speech_data = tsk_calloc((size + 2/* ToC + '\0' */), sizeof(uint8_t)))){
			/* copy ToC */
			*speech_data = ToC & 0x7F/* with 'F'=0 */;
			/* copy speech data */
			memcpy((speech_data + 1), pdata, size);
			/* allocate/reallocate speech data */
			if(*out_max_size <(out_size + pcm_frame_size)){
				if(!(*out_data = tsk_realloc(*out_data, (out_size + pcm_frame_size)))){
					TSK_DEBUG_ERROR("Failed to allocate new buffer");
					*out_max_size = 0;
					TSK_FREE(speech_data);
					goto bail;
				}
				*out_max_size = (out_size + pcm_frame_size);
			}
			/* decode speech data */
			uint32_t consumedBytes = -1;
            int32_t ret = 0;

            if(!amr || !amr->pDecWrapper) {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode somethign severly wrong!!!\n");
            }

            //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode before decodeFrame call amr = %lu, amr->pDecWrapper = %lu\n", amr, amr->pDecWrapper);

            ret = amr->pDecWrapper->decodeFrame(amr->pDecWrapper, speech_data, &((uint8_t *)*out_data)[out_size/sizeof(uint8_t)], size + 2, &consumedBytes);
			
            //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode returned = %ld\n", ret);

            if(!ret) {
                out_size += pcm_frame_size;
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode success Frame No: %d of TOCs: %d\n", i, toc_entries);
            } else {
                //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode returned error Frame No: %d of TOCs: %d\n", i, toc_entries);
                TSK_DEBUG_ERROR("Decoding failed");
            }

            pdata+= size;

			TSK_FREE(speech_data);
		}
	}

bail:
    //__android_log_print("Teleca", "FSL AMR", "tdav_codec_fsl_amr_oa_decode out out_size = %lu\n", out_size);
	return out_size;
}


static uint8_t tdav_codec_fsl_amr_bitbuffer_read(const void* bits, tsk_size_t size, tsk_size_t start, tsk_size_t count)
{
	uint8_t byte, left, right, pad;

	if(!bits || !size || count>8){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	
	if((start + count) > size){
		count = (size - start);
	}

	pad = start ? (8 - (start % 8)) : count;
	left = ((uint8_t*)bits)[start/8] << (8-pad);
	right = ((uint8_t*)bits)[((start+count)<size ? (start+count) : start)/8] >> pad;

	if((start && (start % 8) != ((start+count)%8)) || (!start && count>8)){
		/* overlap */
		byte = (left | right) >> (8-count);
	}
	else{
		byte = (left | right) & (0xFF >> (8-count));
	}

	return byte;
}

