interf_enc.cpp
资源名称:111.rar [点击查看]
上传用户:mony888
上传日期:2022-07-26
资源大小:1247k
文件大小:10k
源码类别:
Windows CE
开发平台:
Visual C++
- /*************************************************************************/
- /* */
- /* Copyright (c) 2000-2004 Linuos Design */
- /* 领驰设计中心 版权所有 2000-2004 */
- /* */
- /* PROPRIETARY RIGHTS of Linuos Design are involved in the subject */
- /* matter of this material. All manufacturing, reproduction, use, and */
- /* sales rights pertaining to this subject matter are governed by the */
- /* license agreement. The recipient of this software implicitly accepts */
- /* the terms of the license. */
- /* 本软件文档资料是领驰设计中心的资产,任何人士阅读和使用本资料必须获得 */
- /* 相应的书面授权,承担保密责任和接受相应的法律约束. */
- /* */
- /*************************************************************************/
- /*
- * ===================================================================
- * TS 26.104
- * REL-5 V5.4.0 2004-03
- * REL-6 V6.1.0 2004-03
- * 3GPP AMR Floating-point Speech Codec
- * ===================================================================
- *
- */
- /*
- * interf_enc.c
- *
- *
- * Project:
- * AMR Floating-Point Codec
- *
- * Contains:
- * This module contains all the functions needed encoding 160
- * 16-bit speech samples to AMR encoder parameters.
- *
- */
- /*
- * include files
- */
- //#include <stdlib.h>
- //#include <stdio.h>
- //#include <memory.h>
- #include "stdafx.h"
- //#include "interf_enc.h"
- //#include "sp_enc.h"
- //#include "interf_rom.h"
- //
- /*
- * EncoderMMS
- *
- *
- * Parameters:
- * mode I: AMR mode
- * param I: Encoder output parameters
- * stream O: packed speech frame
- * frame_type I: frame type (DTX)
- * speech_mode I: speech mode (DTX)
- *
- * Function:
- * Pack encoder output parameters to octet structure according
- * importance table and AMR file storage format according to
- * RFC 3267.
- * Returns:
- * number of octets
- */
- static INT32 EncoderMMS( enum Mode mode, INT16 *param, UINT8 *stream,
- enum TXFrameType frame_type, enum Mode speech_mode )
- {
- INT32 j = 0, k;
- INT16 *mask;
- memset(stream, 0, block_size[mode]);
- *stream = toc_byte[mode];
- stream++;
- if ( mode == 15 )
- {
- return 1;
- }
- else if ( mode == MRDTX )
- {
- mask = order_MRDTX;
- for ( j = 1; j < 36; j++ )
- {
- if ( param[ * mask] & *( mask + 1 ) )
- *stream += 0x01;
- mask += 2;
- if ( j % 8 )
- *stream <<= 1;
- else
- stream++;
- }
- /* add SID type information */
- if ( frame_type == TX_SID_UPDATE )
- *stream += 0x01;
- *stream <<= 3;
- /* speech mode indication */
- *stream += ( UINT8 )(speech_mode & 0x0007);
- *stream <<= 1;
- /* don't shift at the end of the function */
- return 6;
- }
- else if ( mode == MR475 )
- {
- mask = order_MR475;
- for ( j = 1; j < 96; j++ )
- {
- if ( param[ * mask] & *( mask + 1 ) )
- *stream += 0x01;
- mask += 2;
- if ( j % 8 )
- *stream <<= 1;
- else
- stream++;
- }
- }
- else if ( mode == MR515 )
- {
- mask = order_MR515;
- for ( j = 1; j < 104; j++ )
- {
- if ( param[ * mask] & *( mask + 1 ) )
- *stream += 0x01;
- mask += 2;
- if ( j % 8 )
- *stream <<= 1;
- else
- stream++;
- }
- }
- else if ( mode == MR59 )
- {
- mask = order_MR59;
- for ( j = 1; j < 119; j++ )
- {
- if ( param[ * mask] & *( mask + 1 ) )
- *stream += 0x01;
- mask += 2;
- if ( j % 8 )
- *stream <<= 1;
- else
- stream++;
- }
- }
- else if ( mode == MR67 )
- {
- mask = order_MR67;
- for ( j = 1; j < 135; j++ )
- {
- if ( param[ * mask] & *( mask + 1 ) )
- *stream += 0x01;
- mask += 2;
- if ( j % 8 )
- *stream <<= 1;
- else
- stream++;
- }
- }
- else if ( mode == MR74 )
- {
- mask = order_MR74;
- for ( j = 1; j < 149; j++ )
- {
- if ( param[ * mask] & *( mask + 1 ) )
- *stream += 0x01;
- mask += 2;
- if ( j % 8 )
- *stream <<= 1;
- else
- stream++;
- }
- }
- else if ( mode == MR795 )
- {
- mask = order_MR795;
- for ( j = 1; j < 160; j++ )
- {
- if ( param[ * mask] & *( mask + 1 ) )
- *stream += 0x01;
- mask += 2;
- if ( j % 8 )
- *stream <<= 1;
- else
- stream++;
- }
- }
- else if ( mode == MR102 )
- {
- mask = order_MR102;
- for ( j = 1; j < 205; j++ )
- {
- if ( param[ * mask] & *( mask + 1 ) )
- *stream += 0x01;
- mask += 2;
- if ( j % 8 )
- *stream <<= 1;
- else
- stream++;
- }
- }
- else if ( mode == MR122 )
- {
- mask = order_MR122;
- for ( j = 1; j < 245; j++ )
- {
- if ( param[ * mask] & *( mask + 1 ) )
- *stream += 0x01;
- mask += 2;
- if ( j % 8 )
- *stream <<= 1;
- else
- stream++;
- }
- }
- /* shift remaining bits */
- k = j % 8;
- if ( k )
- {
- *stream <<= ( 8 - k );
- }
- return( (INT32)block_size[mode] );
- }
- /*
- * Sid_Sync_reset
- *
- *
- * Parameters:
- * st O: state structure
- *
- * Function:
- * Initializes state memory
- *
- * Returns:
- * void
- */
- static void Sid_Sync_reset( AMR_Encode_State *st )
- {
- st->sid_update_counter = 3;
- st->sid_handover_debt = 0;
- st->prev_ft = TX_SPEECH_GOOD;
- }
- /*
- * Encoder_Interface_Encode
- *
- *
- * Parameters:
- * st I: pointer to state structure
- * mode I: Speech Mode
- * speech I: Input speech
- * serial O: Output octet structure 3GPP or
- * ETSI serial stream
- * force_speech I: Force speech in DTX
- *
- * Function:
- * Encoding and packing one frame of speech
- *
- * Returns:
- * number of octets
- */
- INT32 Encoder_Interface_Encode( void *st, enum Mode mode, INT16 *speech, UINT8 *serial, INT32 force_speech)
- /*
- #ifndef ETSI
- UINT8 *serial,
- #else
- INT16 *serial,
- #endif
- INT32 force_speech )
- */
- {
- INT16 prm[PRMNO_MR122]; /* speech parameters, max size */
- const INT16 *homing; /* pointer to homing frame */
- INT16 homing_size; /* frame size for homing frame */
- AMR_Encode_State * s;
- enum TXFrameType txFrameType; /* frame type */
- INT32 i, noHoming = 0;
- /*
- * used encoder mode,
- * if used_mode == -1, force VAD on
- */
- enum Mode used_mode = (Mode)-force_speech;
- s = ( AMR_Encode_State * )st;
- /*
- * Checks if all samples of the input frame matches the encoder
- * homing frame pattern, which is 0x0008 for all samples.
- */
- for ( i = 0; i < 160; i++ )
- {
- noHoming = speech[i] ^ 0x0008;
- if ( noHoming )
- break;
- }
- if (noHoming)
- {
- Speech_Encode_Frame( s->encoderState, mode, speech, prm, &used_mode );
- }
- else
- {
- switch ( mode )
- {
- case MR122:
- homing = dhf_MR122;
- homing_size = 18;
- break;
- case MR102:
- homing = dhf_MR102;
- homing_size = 12;
- break;
- case MR795:
- homing = dhf_MR795;
- homing_size = 8;
- break;
- case MR74:
- homing = dhf_MR74;
- homing_size = 7;
- break;
- case MR67:
- homing = dhf_MR67;
- homing_size = 7;
- break;
- case MR59:
- homing = dhf_MR59;
- homing_size = 7;
- break;
- case MR515:
- homing = dhf_MR515;
- homing_size = 7;
- break;
- case MR475:
- homing = dhf_MR475;
- homing_size = 7;
- break;
- default:
- homing = NULL;
- homing_size = 0;
- break;
- }
- for( i = 0; i < homing_size; i++)
- {
- prm[i] = homing[i];
- }
- /* rest of the parameters are zero */
- memset(&prm[homing_size], 0, (PRMNO_MR122 - homing_size) << 1);
- used_mode = mode;
- }
- if ( used_mode == MRDTX )
- {
- s->sid_update_counter--;
- if ( s->prev_ft == TX_SPEECH_GOOD )
- {
- txFrameType = TX_SID_FIRST;
- s->sid_update_counter = 3;
- }
- else
- {
- /* TX_SID_UPDATE or TX_NO_DATA */
- if ( ( s->sid_handover_debt > 0 ) && ( s->sid_update_counter > 2 ) )
- {
- /*
- * ensure extra updates are properly delayed after
- * a possible SID_FIRST
- */
- txFrameType = TX_SID_UPDATE;
- s->sid_handover_debt--;
- }
- else
- {
- if ( s->sid_update_counter == 0 )
- {
- txFrameType = TX_SID_UPDATE;
- s->sid_update_counter = 8;
- }
- else
- {
- txFrameType = TX_NO_DATA;
- used_mode = (Mode)15;
- }
- }
- }
- }
- else
- {
- s->sid_update_counter = 8;
- txFrameType = TX_SPEECH_GOOD;
- }
- s->prev_ft = txFrameType;
- if ( noHoming == 0 )
- {
- Speech_Encode_Frame_reset( s->encoderState, s->dtx );
- Sid_Sync_reset( s );
- }
- /*
- #ifndef ETSI
- #ifdef IF2
- return Encoder3GPP( used_mode, prm, serial, txFrameType, mode );
- #else
- */
- return EncoderMMS( used_mode, prm, serial, txFrameType, mode );
- /*
- #endif
- #else
- Prm2Bits( used_mode, prm, &serial[1] );
- serial[0] = ( INT16 )txFrameType;
- serial[245] = ( INT16 )mode;
- return 500;
- #endif
- */
- }
- /*
- * Encoder_Interface_init
- *
- *
- * Parameters:
- * dtx I: DTX flag
- *
- * Function:
- * Allocates state memory and initializes state memory
- *
- * Returns:
- * pointer to encoder interface structure
- */
- void * Encoder_Interface_init( INT32 dtx )
- {
- AMR_Encode_State * s = NULL;
- /* allocate memory */
- s = ( AMR_Encode_State * ) new AMR_Encode_State;
- if ( s == NULL )
- {
- return NULL;
- }
- s->encoderState = Speech_Encode_Frame_init( dtx );
- Sid_Sync_reset( s );
- s->dtx = dtx;
- return s;
- }
- /*
- * DecoderInterfaceExit
- *
- *
- * Parameters:
- * state I: state structure
- *
- * Function:
- * The memory used for state memory is freed
- *
- * Returns:
- * Void
- */
- void Encoder_Interface_exit( void *state )
- {
- AMR_Encode_State * s;
- s = ( AMR_Encode_State * )state;
- /* mcFree memory */
- // Speech_Encode_Frame_exit( &s->encoderState );
- delete s->encoderState;
- delete s;
- state = NULL;
- }