summaryrefslogtreecommitdiffstats
path: root/src/modules/dcc/gsmcodec.cpp
blob: 19f9f575ce07af1a3ae667cccaaf96972e037ccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
//   File : gsmcodec.cpp
//   Creation date : Wed Aug 22 19:12:55 2001 GMT by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2001 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program 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 2
//   of the License, or (at your opinion) any later version.
//
//   This program 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 this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//

#define _GSMCODEC_CPP_

#include "gsmcodec.h"

#ifdef COMPILE_USE_GSM

#include <gsm/gsm.h>

#define GSM_PACKED_FRAME_SIZE_IN_BYTES 33
#define GSM_UNPACKED_FRAME_SIZE_IN_BYTES 320
#define GSM_UNPACKED_FRAME_SIZE_IN_SHORTS 160

KviDccVoiceGsmCodec::KviDccVoiceGsmCodec()
: KviDccVoiceCodec()
{
	m_pEncodeState = gsm_create();
	m_pDecodeState = gsm_create();
	m_szName = "gsm (compression 33:320)";
}

KviDccVoiceGsmCodec::~KviDccVoiceGsmCodec()
{
	gsm_destroy(m_pEncodeState);
	gsm_destroy(m_pDecodeState);
}

void KviDccVoiceGsmCodec::encode(KviDataBuffer * signal,KviDataBuffer * stream)
{
	if(signal->size() < GSM_UNPACKED_FRAME_SIZE_IN_BYTES)return; // nothing to encode

	unsigned char * ptr = signal->data();

	int uFrames = signal->size() / GSM_UNPACKED_FRAME_SIZE_IN_BYTES;
	int uTotalDataCompressed = uFrames * GSM_UNPACKED_FRAME_SIZE_IN_BYTES;
	int uFrameOffset = stream->size();
	unsigned char * endPtr = ptr + uTotalDataCompressed;

	stream->addSize(GSM_PACKED_FRAME_SIZE_IN_BYTES * uFrames);

	while(ptr != endPtr)
	{
		gsm_encode(m_pEncodeState,(short *)ptr,stream->data() + uFrameOffset);
		ptr += GSM_UNPACKED_FRAME_SIZE_IN_BYTES;
		uFrameOffset += GSM_PACKED_FRAME_SIZE_IN_BYTES;
	}
	signal->remove(uTotalDataCompressed);
}

void KviDccVoiceGsmCodec::decode(KviDataBuffer * stream,KviDataBuffer * signal)
{
	if(stream->size() < GSM_PACKED_FRAME_SIZE_IN_BYTES)return; // nothing to decode

	unsigned char * ptr = stream->data();

	// Gsm codec
	int uFrames = stream->size() / GSM_PACKED_FRAME_SIZE_IN_BYTES;
	int uTotalDataDecompressed = uFrames * GSM_PACKED_FRAME_SIZE_IN_BYTES;
	int uSignalOffset = signal->size();
	unsigned char * endPtr = ptr + (uTotalDataDecompressed);

	signal->addSize(GSM_UNPACKED_FRAME_SIZE_IN_BYTES * uFrames);

	while(ptr != endPtr)
	{
		// We don't check the return value here
		// Well..it is either an unrecoverable internal error
		// or a broken frame...
		// but if we receive broken frames over DCC...well....better
		// check the hardware...or the remote codec as well...
		gsm_decode(m_pDecodeState,ptr,(short *)(signal->data() + uSignalOffset));
		ptr += GSM_PACKED_FRAME_SIZE_IN_BYTES;
		uSignalOffset += GSM_UNPACKED_FRAME_SIZE_IN_BYTES;
	}
	stream->remove(uTotalDataDecompressed);
}

int KviDccVoiceGsmCodec::encodedFrameSize()
{
	return GSM_PACKED_FRAME_SIZE_IN_BYTES;
}

int KviDccVoiceGsmCodec::decodedFrameSize()
{
	return GSM_UNPACKED_FRAME_SIZE_IN_BYTES;
}

#endif // COMPILE_USE_GSM