[asterisk-scf-commits] asterisk-scf/integration/media_operations_core.git branch "pjlib-thread-hook" created.
Commits to the Asterisk SCF project code repositories
asterisk-scf-commits at lists.digium.com
Thu Sep 22 16:13:04 CDT 2011
branch "pjlib-thread-hook" has been created
at 8f7afe7a822e203a6c0a0e043b7eabcdb7bc2b4a (commit)
- Log -----------------------------------------------------------------
commit 8f7afe7a822e203a6c0a0e043b7eabcdb7bc2b4a
Author: Kevin P. Fleming <kpfleming at digium.com>
Date: Thu Sep 22 16:12:06 2011 -0500
Use the new, common, AsteriskSCF::PJLib::ThreadHook instead of a local version.
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8fb6d1f..d4b15f4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -31,7 +31,7 @@ astscf_component_add_slices(MediaOperationsCore PROJECT AsteriskSCF/Replication/
astscf_component_add_slice_collection_libraries(MediaOperationsCore ASTSCF)
astscf_component_add_boost_libraries(MediaOperationsCore core thread date_time)
astscf_component_build_icebox(MediaOperationsCore)
-target_link_libraries(MediaOperationsCore logging-client astscf-ice-util-cpp)
+target_link_libraries(MediaOperationsCore logging-client astscf-ice-util-cpp astscf-ice-util-cpp-pjlib)
pjproject_link(MediaOperationsCore pjlib)
pjproject_link(MediaOperationsCore pjlib-util)
diff --git a/src/MediaOperationsCore.cpp b/src/MediaOperationsCore.cpp
index 7c80d67..ed19c44 100644
--- a/src/MediaOperationsCore.cpp
+++ b/src/MediaOperationsCore.cpp
@@ -19,6 +19,7 @@
#include <AsteriskSCF/Component/Component.h>
#include <AsteriskSCF/Media/MediaOperationIf.h>
+#include <AsteriskSCF/PJLib/ThreadHook.h>
#include <AsteriskSCF/logger.h>
#include "MediaOperationReplicationContext.h"
@@ -96,83 +97,6 @@ private:
typedef IceUtil::Handle<MediaOperationsCompare> MediaOperationsComparePtr;
-/**
- * Wrapper class around pj_thread_desc.
- */
-class ThreadDescWrapper
-{
-public:
- ThreadDescWrapper()
- {
- memset(mDesc, 0, sizeof(mDesc));
- }
-
- /**
- * pjthread thread description information, must persist for the life of the thread
- */
- pj_thread_desc mDesc;
-};
-
-/**
- * Type definition used to create a smart pointer for the above.
- */
-typedef boost::shared_ptr<ThreadDescWrapper> ThreadDescWrapperPtr;
-
-//XXX Seriously how many places is this repeated?
-/**
- * Implementation of the Ice::ThreadNotification class.
- */
-class pjlibHook : public Ice::ThreadNotification
-{
-public:
- pjlibHook()
- {
- pj_init();
- }
-
- /**
- * Implementation of the start function which is called when a thread starts.
- */
- void start()
- {
- ThreadDescWrapperPtr wrapper(new ThreadDescWrapper());
- pj_thread_t *thread;
- pj_status_t win = pj_thread_register("ICE Thread", wrapper->mDesc, &thread);
- if (win != PJ_SUCCESS)
- {
- lg(Critical) << "Unable to register thread with PJLIB.";
- return;
- }
- else
- {
- boost::lock_guard<boost::mutex> lock(mLock);
- mpjThreads.insert(std::make_pair(thread, wrapper));
- }
- }
-
- /**
- * Implementation of the stop function which is called when a thread stops.
- */
- void stop()
- {
- if (pj_thread_is_registered())
- {
- boost::lock_guard<boost::mutex> lock(mLock);
- mpjThreads.erase(pj_thread_this());
- }
- }
-
-private:
- /**
- * A map containing thread lifetime persistent data.
- */
- std::map<pj_thread_t*, ThreadDescWrapperPtr> mpjThreads;
- /**
- * Mutex to protect the map
- */
- boost::mutex mLock;
-};
-
class MediaOperationsComponent : public AsteriskSCF::Component::Component
{
public:
@@ -269,7 +193,7 @@ private:
try
{
Ice::InitializationData id;
- id.threadHook = new pjlibHook();
+ id.threadHook = new AsteriskSCF::PJLib::ThreadHook();
id.properties = getCommunicator()->getProperties();
setCommunicator(Ice::initialize(id));
commit 4a225125a0701d5446c0e27c93c8610ba18f01c2
Merge: d4088b2 52a1c39
Author: Kevin P. Fleming <kpfleming at digium.com>
Date: Thu Sep 22 10:43:55 2011 -0500
Merge branch 'master' of git.asterisk.org:asterisk-scf/release/media_operations_core
commit d4088b2c7514a2dcfbb7036fc4d0cfee9a046626
Author: Kevin P. Fleming <kpfleming at digium.com>
Date: Thu Sep 22 10:40:49 2011 -0500
Various PJLIB-related fixes to resolve a crash problem.
After starting this component, and leaving it idle, the Ice runtime will decide
to terminate a number of the threads it started initially. At this point, the
component crashed due a double-free being attempted. The underlying cause for
this was not calling pj_init() before calling pj_thread_register(), so this
patch corrects that and also changes a few other things.
* Add a constructor to ThreadDescWrapper to ensure that mDesc is zeroed.
* Add a constructor to pjLibHook to call pj_init().
* Rename pjLibHook::pjThreads to mpjThreads to conform to coding guidelines.
* Test for a non-success result from pj_thread_register() (although unfortunately
it does not return a failure in the situation described above).
diff --git a/src/MediaOperationsCore.cpp b/src/MediaOperationsCore.cpp
index 296d41b..7c80d67 100644
--- a/src/MediaOperationsCore.cpp
+++ b/src/MediaOperationsCore.cpp
@@ -102,6 +102,11 @@ typedef IceUtil::Handle<MediaOperationsCompare> MediaOperationsComparePtr;
class ThreadDescWrapper
{
public:
+ ThreadDescWrapper()
+ {
+ memset(mDesc, 0, sizeof(mDesc));
+ }
+
/**
* pjthread thread description information, must persist for the life of the thread
*/
@@ -120,17 +125,28 @@ typedef boost::shared_ptr<ThreadDescWrapper> ThreadDescWrapperPtr;
class pjlibHook : public Ice::ThreadNotification
{
public:
+ pjlibHook()
+ {
+ pj_init();
+ }
+
/**
* Implementation of the start function which is called when a thread starts.
*/
void start()
{
- ThreadDescWrapperPtr wrapper = ThreadDescWrapperPtr(new ThreadDescWrapper());
+ ThreadDescWrapperPtr wrapper(new ThreadDescWrapper());
pj_thread_t *thread;
- pj_thread_register("ICE Thread", wrapper->mDesc, &thread);
+ pj_status_t win = pj_thread_register("ICE Thread", wrapper->mDesc, &thread);
+ if (win != PJ_SUCCESS)
+ {
+ lg(Critical) << "Unable to register thread with PJLIB.";
+ return;
+ }
+ else
{
boost::lock_guard<boost::mutex> lock(mLock);
- pjThreads.insert(std::make_pair(thread, wrapper));
+ mpjThreads.insert(std::make_pair(thread, wrapper));
}
}
@@ -142,7 +158,7 @@ public:
if (pj_thread_is_registered())
{
boost::lock_guard<boost::mutex> lock(mLock);
- pjThreads.erase(pj_thread_this());
+ mpjThreads.erase(pj_thread_this());
}
}
@@ -150,7 +166,7 @@ private:
/**
* A map containing thread lifetime persistent data.
*/
- std::map<pj_thread_t*, ThreadDescWrapperPtr> pjThreads;
+ std::map<pj_thread_t*, ThreadDescWrapperPtr> mpjThreads;
/**
* Mutex to protect the map
*/
commit 52a1c399215cca3fd4c3627818639113e30d1148
Author: Brent Eagles <beagles at digium.com>
Date: Wed Sep 21 20:43:00 2011 -0230
Fix what would probably cause a problem if pj lib's function doesn't check for
null.
diff --git a/src/resample.cpp b/src/resample.cpp
index 5d986d1..754d48f 100644
--- a/src/resample.cpp
+++ b/src/resample.cpp
@@ -63,7 +63,10 @@ private:
~Resampler()
{
- pj_pool_release(mPool);
+ if (mPool)
+ {
+ pj_pool_release(mPool);
+ }
}
FramePtr translate(const FramePtr inFrame)
commit e1cb6f9361e048b2a93abdd58f7050ef4e81a9c2
Author: Brent Eagles <beagles at digium.com>
Date: Wed Sep 21 11:39:45 2011 -0230
Fix a problem where pjX methods were being called from the IceBox start thread
which does not have the pjthread hook installed. I altered the initialization
to be "lazy" so the pjlib methods are only called through the Ice communicator
that has the appropriate hook installed.
diff --git a/src/resample.cpp b/src/resample.cpp
index 572bc1a..5d986d1 100644
--- a/src/resample.cpp
+++ b/src/resample.cpp
@@ -48,24 +48,17 @@ private:
const FormatPtr& outputFormat,
const Logger& logger,
pj_caching_pool *caching_pool)
- : Translator(source, inputFormat, outputFormat, logger)
+ : Translator(source, inputFormat, outputFormat, logger),
+ mParentPool(caching_pool),
+ mPool(0),
+ mResample(0)
{
- mPool = pj_pool_create(&caching_pool->factory, "Who cares", 256, 256, NULL);
-
AudioFormatPtr inputAudio = AudioFormatPtr::dynamicCast(inputFormat);
AudioFormatPtr outputAudio = AudioFormatPtr::dynamicCast(outputFormat);
mOutputFrameSize = (outputAudio->sampleRate * inputAudio->frameSize) / inputAudio->sampleRate;
mLogger(Debug) << "Output frames for resampler will contain " << mOutputFrameSize << " samples";
- pjmedia_resample_create(mPool,
- true,
- true,
- 1,
- inputAudio->sampleRate,
- outputAudio->sampleRate,
- inputAudio->frameSize,
- &mResample);
}
~Resampler()
@@ -75,6 +68,23 @@ private:
FramePtr translate(const FramePtr inFrame)
{
+ //
+ // Lazy initialization to avoid issues with when the pjlib thread hook is installed.
+ //
+ if (!mPool)
+ {
+ mPool = pj_pool_create(&mParentPool->factory, "Who cares", 256, 256, NULL);
+ AudioFormatPtr inputAudio = AudioFormatPtr::dynamicCast(mInputFormat);
+ AudioFormatPtr outputAudio = AudioFormatPtr::dynamicCast(mOutputFormat);
+ pjmedia_resample_create(mPool,
+ true,
+ true,
+ 1,
+ inputAudio->sampleRate,
+ outputAudio->sampleRate,
+ inputAudio->frameSize,
+ &mResample);
+ }
if (!TranslatorOperationFactory::formatsEqual(inFrame->mediaFormat, mInputFormat))
{
mLogger(Error) << "Cannot resample frame because the format is not what was expected";
@@ -106,6 +116,7 @@ private:
return new Frame(outFormat, outPayload);
}
private:
+ pj_caching_pool* mParentPool;
pj_pool_t *mPool;
pjmedia_resample *mResample;
int mOutputFrameSize;
@@ -137,12 +148,10 @@ typedef IceUtil::Handle<ResampleOperation> ResampleOperationPtr;
ResampleFactory::ResampleFactory(const Ice::ObjectAdapterPtr& adapter,
const AsteriskSCF::System::Logging::Logger& logger,
const MediaOperationReplicationContextPtr& replicationContext)
- : TranslatorOperationFactory(adapter, logger, replicationContext, "ResampleFactory")
+ : TranslatorOperationFactory(adapter, logger, replicationContext, "ResampleFactory"),
+ mInitialized(false)
{
buildTranslations();
-
- pj_memset(&mCachingPool, 0, sizeof(mCachingPool));
- pj_caching_pool_init(&mCachingPool, NULL, 1024 * 1024);
}
MediaOperationPrx ResampleFactory::createMediaOperation(
@@ -150,6 +159,19 @@ MediaOperationPrx ResampleFactory::createMediaOperation(
const FormatPtr& sinkFormat,
const std::string& operationId)
{
+ {
+ //
+ // Lazy initialization to avoid calling pjlib functions in a thread before the
+ // required hooks can be called.
+ //
+ boost::mutex::scoped_lock lock(mLock);
+ if (!mInitialized)
+ {
+ pj_memset(&mCachingPool, 0, sizeof(mCachingPool));
+ pj_caching_pool_init(&mCachingPool, NULL, 1024 * 1024);
+ mInitialized = true;
+ }
+ }
ResampleOperationPtr operation(
new ResampleOperation(
mAdapter,
diff --git a/src/resample.h b/src/resample.h
index b3ae897..11719c0 100644
--- a/src/resample.h
+++ b/src/resample.h
@@ -19,6 +19,7 @@
#include <pjmedia.h>
#include "TranslatorOperationFactory.h"
+#include <boost/thread/locks.hpp>
namespace AsteriskSCF
{
@@ -40,6 +41,8 @@ public:
private:
void buildTranslations();
+ boost::mutex mLock;
+ bool mInitialized;
pj_caching_pool mCachingPool;
};
commit 2f1f5f2b6a16000e46a51bc6b83f99e8b2c40dda
Author: Joshua Colp <jcolp at digium.com>
Date: Mon Sep 19 16:25:22 2011 -0300
Make the media operations core component conform to the configuration file in the examples repo.
diff --git a/config/test_component.config b/config/test_component.conf
similarity index 100%
rename from config/test_component.config
rename to config/test_component.conf
diff --git a/src/MediaOperationStateReplicatorApp.cpp b/src/MediaOperationStateReplicatorApp.cpp
index ed01019..ed61e9e 100644
--- a/src/MediaOperationStateReplicatorApp.cpp
+++ b/src/MediaOperationStateReplicatorApp.cpp
@@ -52,7 +52,8 @@ public:
virtual void stop();
void createStateReplicator(const Ice::CommunicatorPtr& communicator);
- void registerWithServiceLocator(const Ice::CommunicatorPtr& communicator);
+ void registerWithServiceLocator(const Ice::CommunicatorPtr& communicator,
+ const std::string& appName);
ServiceLocatorManagementPrx mManagement;
Ice::ObjectAdapterPtr mAdapter;
@@ -65,7 +66,8 @@ void MediaOperationStateReplicatorService::createStateReplicator(const Ice::Comm
mAdapter->add(mReplicator, communicator->stringToIdentity(ReplicatorId));
}
-void MediaOperationStateReplicatorService::registerWithServiceLocator(const Ice::CommunicatorPtr& communicator)
+void MediaOperationStateReplicatorService::registerWithServiceLocator(const Ice::CommunicatorPtr& communicator,
+ const std::string& appName)
{
MediaOperationStateReplicatorPrx stateReplicatorProxy =
MediaOperationStateReplicatorPrx::uncheckedCast(mAdapter->createDirectProxy(communicator->stringToIdentity(ReplicatorId)));
@@ -74,21 +76,21 @@ void MediaOperationStateReplicatorService::registerWithServiceLocator(const Ice:
ServiceLocatorParamsPtr params = new ServiceLocatorParams();
params->category = StateReplicatorDiscoveryCategory;
- params->service = communicator->getProperties()->getPropertyWithDefault("MediaOperationStateReplicator.Service", "default");
- params->id = communicator->getProperties()->getPropertyWithDefault("MediaOperationStateReplicator.Name", "default");
+ params->service = communicator->getProperties()->getPropertyWithDefault(appName + ".ServiceName", "default");
+ params->id = appName;
serviceManagement->addLocatorParams(params, "");
}
void MediaOperationStateReplicatorService::start(
- const std::string&,
+ const std::string& appName,
const Ice::CommunicatorPtr& communicator,
const Ice::StringSeq&)
{
- mManagement = ServiceLocatorManagementPrx::checkedCast(communicator->propertyToProxy("ServiceLocatorManagementProxy"));
- mAdapter = communicator->createObjectAdapter("MediaOperationStateReplicatorAdapter");
+ mManagement = ServiceLocatorManagementPrx::checkedCast(communicator->propertyToProxy("LocatorServiceManagement.Proxy"));
+ mAdapter = communicator->createObjectAdapter(appName + ".Adapter");
createStateReplicator(communicator);
- registerWithServiceLocator(communicator);
+ registerWithServiceLocator(communicator, appName);
mAdapter->activate();
}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 82b0a08..88140cb 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -10,7 +10,7 @@ astscf_component_add_boost_libraries(MediaOperationsCoreTest unit_test_framework
astscf_component_add_slice_collection_libraries(MediaOperationsCoreTest ASTSCF)
astscf_component_build_icebox(MediaOperationsCoreTest)
-astscf_test_icebox(MediaOperationsCoreTest config/test_component.config)
+astscf_test_icebox(MediaOperationsCoreTest config/test_component.conf)
target_link_libraries(MediaOperationsCoreTest astscf-ice-util-cpp)
pjproject_link(MediaOperationsCoreTest pjsip)
commit ccb310e61f696d54ec43df4b074c1cf35ac1e148
Author: Brent Eagles <beagles at digium.com>
Date: Fri Sep 16 14:24:30 2011 -0230
Some fixes for building on Windows
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b7aee9c..8fb6d1f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -29,6 +29,7 @@ astscf_component_add_files(MediaOperationsCore MediaOperationReplicationContext.
astscf_component_add_files(MediaOperationsCore MediaOperationStateReplicator.h)
astscf_component_add_slices(MediaOperationsCore PROJECT AsteriskSCF/Replication/MediaOperationsCore/MediaOperationsCoreIf.ice)
astscf_component_add_slice_collection_libraries(MediaOperationsCore ASTSCF)
+astscf_component_add_boost_libraries(MediaOperationsCore core thread date_time)
astscf_component_build_icebox(MediaOperationsCore)
target_link_libraries(MediaOperationsCore logging-client astscf-ice-util-cpp)
@@ -36,7 +37,6 @@ pjproject_link(MediaOperationsCore pjlib)
pjproject_link(MediaOperationsCore pjlib-util)
pjproject_link(MediaOperationsCore pjmedia)
pjproject_link(MediaOperationsCore pjnath)
-pjproject_link(MediaOperationsCore resample)
astscf_component_install(MediaOperationsCore)
astscf_component_init(MediaOperationStateReplicator)
diff --git a/src/g722/g722_decode.c b/src/g722/g722_decode.c
index 233bee5..0f33daf 100644
--- a/src/g722/g722_decode.c
+++ b/src/g722/g722_decode.c
@@ -28,7 +28,16 @@
#endif
#include <stdio.h>
+
+#ifndef _WIN32
#include <inttypes.h>
+#define INLINE __inline__
+#else
+#ifndef INLINE
+#define INLINE /**/
+#endif
+#endif
+
#include <memory.h>
#include <stdlib.h>
#if 0
@@ -44,7 +53,7 @@
#define TRUE (!FALSE)
#endif
-static __inline__ int16_t saturate(int32_t amp)
+static INLINE int16_t saturate(int32_t amp)
{
int16_t amp16;
diff --git a/src/g722/g722_encode.c b/src/g722/g722_encode.c
index db563a7..ed2d165 100644
--- a/src/g722/g722_encode.c
+++ b/src/g722/g722_encode.c
@@ -30,7 +30,16 @@
#endif
#include <stdio.h>
+
+#ifndef _WIN32
#include <inttypes.h>
+#define INLINE __inline__
+#else
+#ifndef INLINE
+#define INLINE /**/
+#endif
+#endif
+
#include <memory.h>
#include <stdlib.h>
#if 0
@@ -46,7 +55,7 @@
#define TRUE (!FALSE)
#endif
-static __inline__ int16_t saturate(int32_t amp)
+static INLINE int16_t saturate(int32_t amp)
{
int16_t amp16;
commit 4c8110ac58ac8632e5021ece81d5235965f98eca
Author: Mark Michelson <mmichelson at digium.com>
Date: Wed Sep 14 17:40:53 2011 -0500
Add casts to Ice::Int to make GCC 4.5 shut up.
diff --git a/test/TestMediaOperations.cpp b/test/TestMediaOperations.cpp
index f4bec35..7d3d79a 100644
--- a/test/TestMediaOperations.cpp
+++ b/test/TestMediaOperations.cpp
@@ -612,7 +612,7 @@ BOOST_FIXTURE_TEST_CASE(resample8To16, PerTestFixture)
//For this test, we have to base the resultant frame on the size of the input frame.
- Testbed.slin8->frameSize = Testbed.sampleSlin8FrameSize;
+ Testbed.slin8->frameSize = (Ice::Int) Testbed.sampleSlin8FrameSize;
MediaOperationServiceLocatorParamsPtr eightToSixteenParams = createLocatorParams(Testbed.slin8, Testbed.slin16);
@@ -671,7 +671,7 @@ BOOST_FIXTURE_TEST_CASE(resample16To8, PerTestFixture)
//For this test, we have to base the resultant frame on the size of the input frame.
- Testbed.slin16->frameSize = Testbed.sampleSlin16FrameSize;
+ Testbed.slin16->frameSize = (Ice::Int) Testbed.sampleSlin16FrameSize;
MediaOperationServiceLocatorParamsPtr sixteenToEightParams = createLocatorParams(Testbed.slin16, Testbed.slin8);
@@ -730,7 +730,7 @@ BOOST_FIXTURE_TEST_CASE(slin8toG722, PerTestFixture)
//For this test, we have to base the resultant frame on the size of the input frame.
- Testbed.g722->frameSize = Testbed.sampleG722FrameSize;
+ Testbed.g722->frameSize = (Ice::Int) Testbed.sampleG722FrameSize;
MediaOperationServiceLocatorParamsPtr slinToG722Params = createLocatorParams(Testbed.slin8, Testbed.g722);
@@ -789,7 +789,7 @@ BOOST_FIXTURE_TEST_CASE(slin16toG722, PerTestFixture)
//For this test, we have to base the resultant frame on the size of the input frame.
- Testbed.g722->frameSize = Testbed.sampleG722FrameSize;
+ Testbed.g722->frameSize = (Ice::Int) Testbed.sampleG722FrameSize;
MediaOperationServiceLocatorParamsPtr slinToG722Params = createLocatorParams(Testbed.slin16, Testbed.g722);
@@ -848,7 +848,7 @@ BOOST_FIXTURE_TEST_CASE(g722ToSlin8, PerTestFixture)
//For this test, we have to base the resultant frame on the size of the input frame.
- Testbed.slin8->frameSize = Testbed.sampleSlin8FrameSize;
+ Testbed.slin8->frameSize = (Ice::Int) Testbed.sampleSlin8FrameSize;
MediaOperationServiceLocatorParamsPtr g722ToSlinParams = createLocatorParams(Testbed.g722, Testbed.slin8);
@@ -907,7 +907,7 @@ BOOST_FIXTURE_TEST_CASE(g722ToSlin16, PerTestFixture)
//For this test, we have to base the resultant frame on the size of the input frame.
- Testbed.slin16->frameSize = Testbed.sampleSlin16FrameSize;
+ Testbed.slin16->frameSize = (Ice::Int) Testbed.sampleSlin16FrameSize;
MediaOperationServiceLocatorParamsPtr g722ToSlinParams = createLocatorParams(Testbed.g722, Testbed.slin16);
commit 4bbbb74072c578c15475f874ff4678e853b6e40f
Author: Mark Michelson <mmichelson at digium.com>
Date: Wed Sep 14 16:01:17 2011 -0500
Add missing G.722 files.
Thanks to ben2011 on IRC for pointing this out.
diff --git a/src/g722/g722.h b/src/g722/g722.h
new file mode 100644
index 0000000..78c8a18
--- /dev/null
+++ b/src/g722/g722.h
@@ -0,0 +1,151 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g722.h - The ITU G.722 codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * Despite my general liking of the GPL, I place my own contributions
+ * to this code in the public domain for the benefit of all mankind -
+ * even the slimy ones who might try to proprietize my work and use it
+ * to my detriment.
+ *
+ * Based on a single channel G.722 codec which is:
+ *
+ ***** Copyright (c) CMU 1993 *****
+ * Computer Science, Speech Group
+ * Chengxiang Lu and Alex Hauptmann
+ *
+ * $Id: g722.h 48959 2006-12-25 06:42:15Z rizzo $
+ */
+
+
+/*! \file */
+
+#if !defined(_G722_H_)
+#define _G722_H_
+
+/* XXX Is this portable? */
+#include <stdint.h>
+
+/*! \page g722_page G.722 encoding and decoding
+\section g722_page_sec_1 What does it do?
+The G.722 module is a bit exact implementation of the ITU G.722 specification for all three
+specified bit rates - 64000bps, 56000bps and 48000bps. It passes the ITU tests.
+
+To allow fast and flexible interworking with narrow band telephony, the encoder and decoder
+support an option for the linear audio to be an 8k samples/second stream. In this mode the
+codec is considerably faster, and still fully compatible with wideband terminals using G.722.
+
+\section g722_page_sec_2 How does it work?
+???.
+*/
+
+enum
+{
+ G722_SAMPLE_RATE_8000 = 0x0001,
+ G722_PACKED = 0x0002
+};
+
+#ifndef INT16_MAX
+#define INT16_MAX 32767
+#endif
+#ifndef INT16_MIN
+#define INT16_MIN (-32768)
+#endif
+
+typedef struct
+{
+ /*! TRUE if the operating in the special ITU test mode, with the band split filters
+ disabled. */
+ int itu_test_mode;
+ /*! TRUE if the G.722 data is packed */
+ int packed;
+ /*! TRUE if encode from 8k samples/second */
+ int eight_k;
+ /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
+ int bits_per_sample;
+
+ /*! Signal history for the QMF */
+ int x[24];
+
+ struct
+ {
+ int s;
+ int sp;
+ int sz;
+ int r[3];
+ int a[3];
+ int ap[3];
+ int p[3];
+ int d[7];
+ int b[7];
+ int bp[7];
+ int sg[7];
+ int nb;
+ int det;
+ } band[2];
+
+ unsigned int in_buffer;
+ int in_bits;
+ unsigned int out_buffer;
+ int out_bits;
+} g722_encode_state_t;
+
+typedef struct
+{
+ /*! TRUE if the operating in the special ITU test mode, with the band split filters
+ disabled. */
+ int itu_test_mode;
+ /*! TRUE if the G.722 data is packed */
+ int packed;
+ /*! TRUE if decode to 8k samples/second */
+ int eight_k;
+ /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
+ int bits_per_sample;
+
+ /*! Signal history for the QMF */
+ int x[24];
+
+ struct
+ {
+ int s;
+ int sp;
+ int sz;
+ int r[3];
+ int a[3];
+ int ap[3];
+ int p[3];
+ int d[7];
+ int b[7];
+ int bp[7];
+ int sg[7];
+ int nb;
+ int det;
+ } band[2];
+
+ unsigned int in_buffer;
+ int in_bits;
+ unsigned int out_buffer;
+ int out_bits;
+} g722_decode_state_t;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+g722_encode_state_t *g722_encode_init(g722_encode_state_t *s, int rate, int options);
+int g722_encode_release(g722_encode_state_t *s);
+int g722_encode(g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len);
+
+g722_decode_state_t *g722_decode_init(g722_decode_state_t *s, int rate, int options);
+int g722_decode_release(g722_decode_state_t *s);
+int g722_decode(g722_decode_state_t *s, int16_t amp[], const uint8_t g722_data[], int len);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/g722/g722_decode.c b/src/g722/g722_decode.c
new file mode 100644
index 0000000..233bee5
--- /dev/null
+++ b/src/g722/g722_decode.c
@@ -0,0 +1,398 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g722_decode.c - The ITU G.722 codec, decode part.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * Despite my general liking of the GPL, I place my own contributions
+ * to this code in the public domain for the benefit of all mankind -
+ * even the slimy ones who might try to proprietize my work and use it
+ * to my detriment.
+ *
+ * Based in part on a single channel G.722 codec which is:
+ *
+ * Copyright (c) CMU 1993
+ * Computer Science, Speech Group
+ * Chengxiang Lu and Alex Hauptmann
+ *
+ * $Id: g722_decode.c 194722 2009-05-15 17:59:08Z russell $
+ */
+
+/*! \file */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <memory.h>
+#include <stdlib.h>
+#if 0
+#include <tgmath.h>
+#endif
+
+#include "g722.h"
+
+#if !defined(FALSE)
+#define FALSE 0
+#endif
+#if !defined(TRUE)
+#define TRUE (!FALSE)
+#endif
+
+static __inline__ int16_t saturate(int32_t amp)
+{
+ int16_t amp16;
+
+ /* Hopefully this is optimised for the common case - not clipping */
+ amp16 = (int16_t) amp;
+ if (amp == amp16)
+ return amp16;
+ if (amp > INT16_MAX)
+ return INT16_MAX;
+ return INT16_MIN;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void block4(g722_decode_state_t *s, int band, int d);
+
+static void block4(g722_decode_state_t *s, int band, int d)
+{
+ int wd1;
+ int wd2;
+ int wd3;
+ int i;
+
+ /* Block 4, RECONS */
+ s->band[band].d[0] = d;
+ s->band[band].r[0] = saturate(s->band[band].s + d);
+
+ /* Block 4, PARREC */
+ s->band[band].p[0] = saturate(s->band[band].sz + d);
+
+ /* Block 4, UPPOL2 */
+ for (i = 0; i < 3; i++)
+ s->band[band].sg[i] = s->band[band].p[i] >> 15;
+ wd1 = saturate(s->band[band].a[1] << 2);
+
+ wd2 = (s->band[band].sg[0] == s->band[band].sg[1]) ? -wd1 : wd1;
+ if (wd2 > 32767)
+ wd2 = 32767;
+ wd3 = (s->band[band].sg[0] == s->band[band].sg[2]) ? 128 : -128;
+ wd3 += (wd2 >> 7);
+ wd3 += (s->band[band].a[2]*32512) >> 15;
+ if (wd3 > 12288)
+ wd3 = 12288;
+ else if (wd3 < -12288)
+ wd3 = -12288;
+ s->band[band].ap[2] = wd3;
+
+ /* Block 4, UPPOL1 */
+ s->band[band].sg[0] = s->band[band].p[0] >> 15;
+ s->band[band].sg[1] = s->band[band].p[1] >> 15;
+ wd1 = (s->band[band].sg[0] == s->band[band].sg[1]) ? 192 : -192;
+ wd2 = (s->band[band].a[1]*32640) >> 15;
+
+ s->band[band].ap[1] = saturate(wd1 + wd2);
+ wd3 = saturate(15360 - s->band[band].ap[2]);
+ if (s->band[band].ap[1] > wd3)
+ s->band[band].ap[1] = wd3;
+ else if (s->band[band].ap[1] < -wd3)
+ s->band[band].ap[1] = -wd3;
+
+ /* Block 4, UPZERO */
+ wd1 = (d == 0) ? 0 : 128;
+ s->band[band].sg[0] = d >> 15;
+ for (i = 1; i < 7; i++)
+ {
+ s->band[band].sg[i] = s->band[band].d[i] >> 15;
+ wd2 = (s->band[band].sg[i] == s->band[band].sg[0]) ? wd1 : -wd1;
+ wd3 = (s->band[band].b[i]*32640) >> 15;
+ s->band[band].bp[i] = saturate(wd2 + wd3);
+ }
+
+ /* Block 4, DELAYA */
+ for (i = 6; i > 0; i--)
+ {
+ s->band[band].d[i] = s->band[band].d[i - 1];
+ s->band[band].b[i] = s->band[band].bp[i];
+ }
+
+ for (i = 2; i > 0; i--)
+ {
+ s->band[band].r[i] = s->band[band].r[i - 1];
+ s->band[band].p[i] = s->band[band].p[i - 1];
+ s->band[band].a[i] = s->band[band].ap[i];
+ }
+
+ /* Block 4, FILTEP */
+ wd1 = saturate(s->band[band].r[1] + s->band[band].r[1]);
+ wd1 = (s->band[band].a[1]*wd1) >> 15;
+ wd2 = saturate(s->band[band].r[2] + s->band[band].r[2]);
+ wd2 = (s->band[band].a[2]*wd2) >> 15;
+ s->band[band].sp = saturate(wd1 + wd2);
+
+ /* Block 4, FILTEZ */
+ s->band[band].sz = 0;
+ for (i = 6; i > 0; i--)
+ {
+ wd1 = saturate(s->band[band].d[i] + s->band[band].d[i]);
+ s->band[band].sz += (s->band[band].b[i]*wd1) >> 15;
+ }
+ s->band[band].sz = saturate(s->band[band].sz);
+
+ /* Block 4, PREDIC */
+ s->band[band].s = saturate(s->band[band].sp + s->band[band].sz);
+}
+/*- End of function --------------------------------------------------------*/
+
+g722_decode_state_t *g722_decode_init(g722_decode_state_t *s, int rate, int options)
+{
+ if (s == NULL)
+ {
+ if ((s = (g722_decode_state_t *) malloc(sizeof(*s))) == NULL)
+ return NULL;
+ }
+ memset(s, 0, sizeof(*s));
+ if (rate == 48000)
+ s->bits_per_sample = 6;
+ else if (rate == 56000)
+ s->bits_per_sample = 7;
+ else
+ s->bits_per_sample = 8;
+ if ((options & G722_SAMPLE_RATE_8000))
+ s->eight_k = TRUE;
+ if ((options & G722_PACKED) && s->bits_per_sample != 8)
+ s->packed = TRUE;
+ else
+ s->packed = FALSE;
+ s->band[0].det = 32;
+ s->band[1].det = 8;
+ return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g722_decode_release(g722_decode_state_t *s)
+{
+ free(s);
+ return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g722_decode(g722_decode_state_t *s, int16_t amp[], const uint8_t g722_data[], int len)
+{
+ static const int wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042 };
+ static const int rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0 };
+ static const int ilb[32] =
+ {
+ 2048, 2093, 2139, 2186, 2233, 2282, 2332,
+ 2383, 2435, 2489, 2543, 2599, 2656, 2714,
+ 2774, 2834, 2896, 2960, 3025, 3091, 3158,
+ 3228, 3298, 3371, 3444, 3520, 3597, 3676,
+ 3756, 3838, 3922, 4008
+ };
+ static const int wh[3] = {0, -214, 798};
+ static const int rh2[4] = {2, 1, 2, 1};
+ static const int qm2[4] = {-7408, -1616, 7408, 1616};
+ static const int qm4[16] =
+ {
+ 0, -20456, -12896, -8968,
+ -6288, -4240, -2584, -1200,
+ 20456, 12896, 8968, 6288,
+ 4240, 2584, 1200, 0
+ };
+ static const int qm5[32] =
+ {
+ -280, -280, -23352, -17560,
+ -14120, -11664, -9752, -8184,
+ -6864, -5712, -4696, -3784,
+ -2960, -2208, -1520, -880,
+ 23352, 17560, 14120, 11664,
+ 9752, 8184, 6864, 5712,
+ 4696, 3784, 2960, 2208,
+ 1520, 880, 280, -280
+ };
+ static const int qm6[64] =
+ {
+ -136, -136, -136, -136,
+ -24808, -21904, -19008, -16704,
+ -14984, -13512, -12280, -11192,
+ -10232, -9360, -8576, -7856,
+ -7192, -6576, -6000, -5456,
+ -4944, -4464, -4008, -3576,
+ -3168, -2776, -2400, -2032,
+ -1688, -1360, -1040, -728,
+ 24808, 21904, 19008, 16704,
+ 14984, 13512, 12280, 11192,
+ 10232, 9360, 8576, 7856,
+ 7192, 6576, 6000, 5456,
+ 4944, 4464, 4008, 3576,
+ 3168, 2776, 2400, 2032,
+ 1688, 1360, 1040, 728,
+ 432, 136, -432, -136
+ };
+ static const int qmf_coeffs[12] =
+ {
+ 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
+ };
+
+ int dlowt;
+ int rlow;
+ int ihigh;
+ int dhigh;
+ int rhigh;
+ int xout1;
+ int xout2;
+ int wd1;
+ int wd2;
+ int wd3;
+ int code;
+ int outlen;
+ int i;
+ int j;
+
+ outlen = 0;
+ rhigh = 0;
+ for (j = 0; j < len; )
+ {
+ if (s->packed)
+ {
+ /* Unpack the code bits */
+ if (s->in_bits < s->bits_per_sample)
+ {
+ s->in_buffer |= (g722_data[j++] << s->in_bits);
+ s->in_bits += 8;
+ }
+ code = s->in_buffer & ((1 << s->bits_per_sample) - 1);
+ s->in_buffer >>= s->bits_per_sample;
+ s->in_bits -= s->bits_per_sample;
+ }
+ else
+ {
+ code = g722_data[j++];
+ }
+
+ switch (s->bits_per_sample)
+ {
+ default:
+ case 8:
+ wd1 = code & 0x3F;
+ ihigh = (code >> 6) & 0x03;
+ wd2 = qm6[wd1];
+ wd1 >>= 2;
+ break;
+ case 7:
+ wd1 = code & 0x1F;
+ ihigh = (code >> 5) & 0x03;
+ wd2 = qm5[wd1];
+ wd1 >>= 1;
+ break;
+ case 6:
+ wd1 = code & 0x0F;
+ ihigh = (code >> 4) & 0x03;
+ wd2 = qm4[wd1];
+ break;
+ }
+ /* Block 5L, LOW BAND INVQBL */
+ wd2 = (s->band[0].det*wd2) >> 15;
+ /* Block 5L, RECONS */
+ rlow = s->band[0].s + wd2;
+ /* Block 6L, LIMIT */
+ if (rlow > 16383)
+ rlow = 16383;
+ else if (rlow < -16384)
+ rlow = -16384;
+
+ /* Block 2L, INVQAL */
+ wd2 = qm4[wd1];
+ dlowt = (s->band[0].det*wd2) >> 15;
+
+ /* Block 3L, LOGSCL */
+ wd2 = rl42[wd1];
+ wd1 = (s->band[0].nb*127) >> 7;
+ wd1 += wl[wd2];
+ if (wd1 < 0)
+ wd1 = 0;
+ else if (wd1 > 18432)
+ wd1 = 18432;
+ s->band[0].nb = wd1;
+
+ /* Block 3L, SCALEL */
+ wd1 = (s->band[0].nb >> 6) & 31;
+ wd2 = 8 - (s->band[0].nb >> 11);
+ wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
+ s->band[0].det = wd3 << 2;
+
+ block4(s, 0, dlowt);
+
+ if (!s->eight_k)
+ {
+ /* Block 2H, INVQAH */
+ wd2 = qm2[ihigh];
+ dhigh = (s->band[1].det*wd2) >> 15;
+ /* Block 5H, RECONS */
+ rhigh = dhigh + s->band[1].s;
+ /* Block 6H, LIMIT */
+ if (rhigh > 16383)
+ rhigh = 16383;
+ else if (rhigh < -16384)
+ rhigh = -16384;
+
+ /* Block 2H, INVQAH */
+ wd2 = rh2[ihigh];
+ wd1 = (s->band[1].nb*127) >> 7;
+ wd1 += wh[wd2];
+ if (wd1 < 0)
+ wd1 = 0;
+ else if (wd1 > 22528)
+ wd1 = 22528;
+ s->band[1].nb = wd1;
+
+ /* Block 3H, SCALEH */
+ wd1 = (s->band[1].nb >> 6) & 31;
+ wd2 = 10 - (s->band[1].nb >> 11);
+ wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
+ s->band[1].det = wd3 << 2;
+
+ block4(s, 1, dhigh);
+ }
+
+ if (s->itu_test_mode)
+ {
+ amp[outlen++] = (int16_t) (rlow << 1);
+ amp[outlen++] = (int16_t) (rhigh << 1);
+ }
+ else
+ {
+ if (s->eight_k)
+ {
+ amp[outlen++] = (int16_t) (rlow << 1);
+ }
+ else
+ {
+ /* Apply the receive QMF */
+ for (i = 0; i < 22; i++)
+ s->x[i] = s->x[i + 2];
+ s->x[22] = rlow + rhigh;
+ s->x[23] = rlow - rhigh;
+
+ xout1 = 0;
+ xout2 = 0;
+ for (i = 0; i < 12; i++)
+ {
+ xout2 += s->x[2*i]*qmf_coeffs[i];
+ xout1 += s->x[2*i + 1]*qmf_coeffs[11 - i];
+ }
+ amp[outlen++] = (int16_t) (xout1 >> 11);
+ amp[outlen++] = (int16_t) (xout2 >> 11);
+ }
+ }
+ }
+ return outlen;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/
diff --git a/src/g722/g722_decode.o b/src/g722/g722_decode.o
new file mode 100644
index 0000000..b051185
Binary files /dev/null and b/src/g722/g722_decode.o differ
diff --git a/src/g722/g722_encode.c b/src/g722/g722_encode.c
new file mode 100644
index 0000000..db563a7
--- /dev/null
+++ b/src/g722/g722_encode.c
@@ -0,0 +1,400 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g722_encode.c - The ITU G.722 codec, encode part.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * Despite my general liking of the GPL, I place my own contributions
+ * to this code in the public domain for the benefit of all mankind -
+ * even the slimy ones who might try to proprietize my work and use it
+ * to my detriment.
+ *
+ * Based on a single channel 64kbps only G.722 codec which is:
+ *
+ ***** Copyright (c) CMU 1993 *****
+ * Computer Science, Speech Group
+ * Chengxiang Lu and Alex Hauptmann
+ *
+ * $Id: g722_encode.c 194722 2009-05-15 17:59:08Z russell $
+ */
+
+/*! \file */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <memory.h>
+#include <stdlib.h>
+#if 0
+#include <tgmath.h>
+#endif
+
+#include "g722.h"
+
+#if !defined(FALSE)
+#define FALSE 0
+#endif
+#if !defined(TRUE)
+#define TRUE (!FALSE)
+#endif
+
+static __inline__ int16_t saturate(int32_t amp)
+{
+ int16_t amp16;
+
+ /* Hopefully this is optimised for the common case - not clipping */
+ amp16 = (int16_t) amp;
+ if (amp == amp16)
+ return amp16;
+ if (amp > INT16_MAX)
+ return INT16_MAX;
+ return INT16_MIN;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void block4(g722_encode_state_t *s, int band, int d)
+{
+ int wd1;
+ int wd2;
+ int wd3;
+ int i;
+
+ /* Block 4, RECONS */
+ s->band[band].d[0] = d;
+ s->band[band].r[0] = saturate(s->band[band].s + d);
+
+ /* Block 4, PARREC */
+ s->band[band].p[0] = saturate(s->band[band].sz + d);
+
+ /* Block 4, UPPOL2 */
+ for (i = 0; i < 3; i++)
+ s->band[band].sg[i] = s->band[band].p[i] >> 15;
+ wd1 = saturate(s->band[band].a[1] << 2);
+
+ wd2 = (s->band[band].sg[0] == s->band[band].sg[1]) ? -wd1 : wd1;
+ if (wd2 > 32767)
+ wd2 = 32767;
+ wd3 = (wd2 >> 7) + ((s->band[band].sg[0] == s->band[band].sg[2]) ? 128 : -128);
+ wd3 += (s->band[band].a[2]*32512) >> 15;
+ if (wd3 > 12288)
+ wd3 = 12288;
+ else if (wd3 < -12288)
+ wd3 = -12288;
+ s->band[band].ap[2] = wd3;
+
+ /* Block 4, UPPOL1 */
+ s->band[band].sg[0] = s->band[band].p[0] >> 15;
+ s->band[band].sg[1] = s->band[band].p[1] >> 15;
+ wd1 = (s->band[band].sg[0] == s->band[band].sg[1]) ? 192 : -192;
+ wd2 = (s->band[band].a[1]*32640) >> 15;
+
+ s->band[band].ap[1] = saturate(wd1 + wd2);
+ wd3 = saturate(15360 - s->band[band].ap[2]);
+ if (s->band[band].ap[1] > wd3)
+ s->band[band].ap[1] = wd3;
+ else if (s->band[band].ap[1] < -wd3)
+ s->band[band].ap[1] = -wd3;
+
+ /* Block 4, UPZERO */
+ wd1 = (d == 0) ? 0 : 128;
+ s->band[band].sg[0] = d >> 15;
+ for (i = 1; i < 7; i++)
+ {
+ s->band[band].sg[i] = s->band[band].d[i] >> 15;
+ wd2 = (s->band[band].sg[i] == s->band[band].sg[0]) ? wd1 : -wd1;
+ wd3 = (s->band[band].b[i]*32640) >> 15;
+ s->band[band].bp[i] = saturate(wd2 + wd3);
+ }
+
+ /* Block 4, DELAYA */
+ for (i = 6; i > 0; i--)
+ {
+ s->band[band].d[i] = s->band[band].d[i - 1];
+ s->band[band].b[i] = s->band[band].bp[i];
+ }
+
+ for (i = 2; i > 0; i--)
+ {
+ s->band[band].r[i] = s->band[band].r[i - 1];
+ s->band[band].p[i] = s->band[band].p[i - 1];
+ s->band[band].a[i] = s->band[band].ap[i];
+ }
+
+ /* Block 4, FILTEP */
+ wd1 = saturate(s->band[band].r[1] + s->band[band].r[1]);
+ wd1 = (s->band[band].a[1]*wd1) >> 15;
+ wd2 = saturate(s->band[band].r[2] + s->band[band].r[2]);
+ wd2 = (s->band[band].a[2]*wd2) >> 15;
+ s->band[band].sp = saturate(wd1 + wd2);
+
+ /* Block 4, FILTEZ */
+ s->band[band].sz = 0;
+ for (i = 6; i > 0; i--)
+ {
+ wd1 = saturate(s->band[band].d[i] + s->band[band].d[i]);
+ s->band[band].sz += (s->band[band].b[i]*wd1) >> 15;
+ }
+ s->band[band].sz = saturate(s->band[band].sz);
+
+ /* Block 4, PREDIC */
+ s->band[band].s = saturate(s->band[band].sp + s->band[band].sz);
+}
+/*- End of function --------------------------------------------------------*/
+
+g722_encode_state_t *g722_encode_init(g722_encode_state_t *s, int rate, int options)
+{
+ if (s == NULL)
+ {
+ if ((s = (g722_encode_state_t *) malloc(sizeof(*s))) == NULL)
+ return NULL;
+ }
+ memset(s, 0, sizeof(*s));
+ if (rate == 48000)
+ s->bits_per_sample = 6;
+ else if (rate == 56000)
+ s->bits_per_sample = 7;
+ else
+ s->bits_per_sample = 8;
+ if ((options & G722_SAMPLE_RATE_8000))
+ s->eight_k = TRUE;
+ if ((options & G722_PACKED) && s->bits_per_sample != 8)
+ s->packed = TRUE;
+ else
+ s->packed = FALSE;
+ s->band[0].det = 32;
+ s->band[1].det = 8;
+ return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g722_encode_release(g722_encode_state_t *s)
+{
+ free(s);
+ return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g722_encode(g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len)
+{
+ static const int q6[32] =
+ {
+ 0, 35, 72, 110, 150, 190, 233, 276,
+ 323, 370, 422, 473, 530, 587, 650, 714,
+ 786, 858, 940, 1023, 1121, 1219, 1339, 1458,
+ 1612, 1765, 1980, 2195, 2557, 2919, 0, 0
+ };
+ static const int iln[32] =
+ {
+ 0, 63, 62, 31, 30, 29, 28, 27,
+ 26, 25, 24, 23, 22, 21, 20, 19,
+ 18, 17, 16, 15, 14, 13, 12, 11,
+ 10, 9, 8, 7, 6, 5, 4, 0
+ };
+ static const int ilp[32] =
+ {
+ 0, 61, 60, 59, 58, 57, 56, 55,
+ 54, 53, 52, 51, 50, 49, 48, 47,
+ 46, 45, 44, 43, 42, 41, 40, 39,
+ 38, 37, 36, 35, 34, 33, 32, 0
+ };
+ static const int wl[8] =
+ {
+ -60, -30, 58, 172, 334, 538, 1198, 3042
+ };
+ static const int rl42[16] =
+ {
+ 0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
+ };
+ static const int ilb[32] =
+ {
+ 2048, 2093, 2139, 2186, 2233, 2282, 2332,
+ 2383, 2435, 2489, 2543, 2599, 2656, 2714,
+ 2774, 2834, 2896, 2960, 3025, 3091, 3158,
+ 3228, 3298, 3371, 3444, 3520, 3597, 3676,
+ 3756, 3838, 3922, 4008
+ };
+ static const int qm4[16] =
+ {
+ 0, -20456, -12896, -8968,
+ -6288, -4240, -2584, -1200,
+ 20456, 12896, 8968, 6288,
+ 4240, 2584, 1200, 0
+ };
+ static const int qm2[4] =
+ {
+ -7408, -1616, 7408, 1616
+ };
+ static const int qmf_coeffs[12] =
+ {
+ 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
+ };
+ static const int ihn[3] = {0, 1, 0};
+ static const int ihp[3] = {0, 3, 2};
+ static const int wh[3] = {0, -214, 798};
+ static const int rh2[4] = {2, 1, 2, 1};
+
+ int dlow;
+ int dhigh;
+ int el;
+ int wd;
+ int wd1;
+ int ril;
+ int wd2;
+ int il4;
+ int ih2;
+ int wd3;
+ int eh;
+ int mih;
+ int i;
+ int j;
+ /* Low and high band PCM from the QMF */
+ int xlow;
+ int xhigh;
+ int g722_bytes;
+ /* Even and odd tap accumulators */
+ int sumeven;
+ int sumodd;
+ int ihigh;
+ int ilow;
+ int code;
+
+ g722_bytes = 0;
+ xhigh = 0;
+ for (j = 0; j < len; )
+ {
+ if (s->itu_test_mode)
+ {
+ xlow =
+ xhigh = amp[j++] >> 1;
+ }
+ else
+ {
+ if (s->eight_k)
+ {
+ xlow = amp[j++] >> 1;
+ }
+ else
+ {
+ /* Apply the transmit QMF */
+ /* Shuffle the buffer down */
+ for (i = 0; i < 22; i++)
+ s->x[i] = s->x[i + 2];
+ s->x[22] = amp[j++];
+ s->x[23] = amp[j++];
+
+ /* Discard every other QMF output */
+ sumeven = 0;
+ sumodd = 0;
+ for (i = 0; i < 12; i++)
+ {
+ sumodd += s->x[2*i]*qmf_coeffs[i];
+ sumeven += s->x[2*i + 1]*qmf_coeffs[11 - i];
+ }
+ xlow = (sumeven + sumodd) >> 14;
+ xhigh = (sumeven - sumodd) >> 14;
+ }
+ }
+ /* Block 1L, SUBTRA */
+ el = saturate(xlow - s->band[0].s);
+
+ /* Block 1L, QUANTL */
+ wd = (el >= 0) ? el : -(el + 1);
+
+ for (i = 1; i < 30; i++)
+ {
+ wd1 = (q6[i]*s->band[0].det) >> 12;
+ if (wd < wd1)
+ break;
+ }
+ ilow = (el < 0) ? iln[i] : ilp[i];
+
+ /* Block 2L, INVQAL */
+ ril = ilow >> 2;
+ wd2 = qm4[ril];
+ dlow = (s->band[0].det*wd2) >> 15;
+
+ /* Block 3L, LOGSCL */
+ il4 = rl42[ril];
+ wd = (s->band[0].nb*127) >> 7;
+ s->band[0].nb = wd + wl[il4];
+ if (s->band[0].nb < 0)
+ s->band[0].nb = 0;
+ else if (s->band[0].nb > 18432)
+ s->band[0].nb = 18432;
+
+ /* Block 3L, SCALEL */
+ wd1 = (s->band[0].nb >> 6) & 31;
+ wd2 = 8 - (s->band[0].nb >> 11);
+ wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
+ s->band[0].det = wd3 << 2;
+
+ block4(s, 0, dlow);
+
+ if (s->eight_k)
+ {
+ /* Just leave the high bits as zero */
+ code = (0xC0 | ilow) >> (8 - s->bits_per_sample);
+ }
+ else
+ {
+ /* Block 1H, SUBTRA */
+ eh = saturate(xhigh - s->band[1].s);
+
+ /* Block 1H, QUANTH */
+ wd = (eh >= 0) ? eh : -(eh + 1);
+ wd1 = (564*s->band[1].det) >> 12;
+ mih = (wd >= wd1) ? 2 : 1;
+ ihigh = (eh < 0) ? ihn[mih] : ihp[mih];
+
+ /* Block 2H, INVQAH */
+ wd2 = qm2[ihigh];
+ dhigh = (s->band[1].det*wd2) >> 15;
+
+ /* Block 3H, LOGSCH */
+ ih2 = rh2[ihigh];
+ wd = (s->band[1].nb*127) >> 7;
+ s->band[1].nb = wd + wh[ih2];
+ if (s->band[1].nb < 0)
+ s->band[1].nb = 0;
+ else if (s->band[1].nb > 22528)
+ s->band[1].nb = 22528;
+
+ /* Block 3H, SCALEH */
+ wd1 = (s->band[1].nb >> 6) & 31;
+ wd2 = 10 - (s->band[1].nb >> 11);
+ wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
+ s->band[1].det = wd3 << 2;
+
+ block4(s, 1, dhigh);
+ code = ((ihigh << 6) | ilow) >> (8 - s->bits_per_sample);
+ }
+
+ if (s->packed)
+ {
+ /* Pack the code bits */
+ s->out_buffer |= (code << s->out_bits);
+ s->out_bits += s->bits_per_sample;
+ if (s->out_bits >= 8)
+ {
+ g722_data[g722_bytes++] = (uint8_t) (s->out_buffer & 0xFF);
+ s->out_bits -= 8;
+ s->out_buffer >>= 8;
+ }
+ }
+ else
+ {
+ g722_data[g722_bytes++] = (uint8_t) code;
+ }
+ }
+ return g722_bytes;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/
diff --git a/src/g722/g722_encode.o b/src/g722/g722_encode.o
new file mode 100644
index 0000000..93a9dea
Binary files /dev/null and b/src/g722/g722_encode.o differ
commit e9a724da2ab813c7605606531d5a554bdbc3cac2
Author: Mark Michelson <mmichelson at digium.com>
Date: Wed Sep 14 15:22:34 2011 -0500
Make adjustments from CR-ASTSCF-156
diff --git a/src/TranslatorOperation.cpp b/src/TranslatorOperation.cpp
index 078016a..ea25865 100644
--- a/src/TranslatorOperation.cpp
+++ b/src/TranslatorOperation.cpp
@@ -47,7 +47,7 @@ TranslatorOperation::TranslatorOperation(const Ice::ObjectAdapterPtr& adapter,
TranslatorOperation::~TranslatorOperation()
{
- mLogger(Debug) << "UlawAlawOperation destructor called";
+ mLogger(Debug) << "TranslatorOperation destructor called";
try
{
@@ -58,6 +58,10 @@ TranslatorOperation::~TranslatorOperation()
{
mLogger(Error) << "Exception caught while trying to remove source and sink from object adapter";
}
+ catch (...)
+ {
+ mLogger(Error) << "Exception caught while trying to remove source and sink from object adapter";
+ }
}
void TranslatorOperation::setState()
commit 4456735a649c3828f7f228f1f77a1dc06ad06079
Merge: 057918e e0cd8fa
Author: Mark Michelson <mmichelson at digium.com>
Date: Wed Sep 14 10:21:53 2011 -0500
Merge branch 'resample'
Conflicts:
src/ulaw_alaw.cpp
commit e0cd8fa5dd63fcbf8c291f66356ab565cd057cc9
Author: Mark Michelson <mmichelson at digium.com>
Date: Thu Sep 1 11:29:07 2011 -0500
Place common code into common functions.
diff --git a/src/MediaOperationStateReplicatorListener.cpp b/src/MediaOperationStateReplicatorListener.cpp
index 7cfb0f0..2943d21 100644
--- a/src/MediaOperationStateReplicatorListener.cpp
+++ b/src/MediaOperationStateReplicatorListener.cpp
@@ -15,7 +15,10 @@
*/
#include "MediaOperationStateReplicator.h"
+#include "TranslatorOperationFactory.h"
#include "ulaw_alaw.h"
+#include "resample.h"
+#include "g722.h"
namespace AsteriskSCF
{
@@ -51,6 +54,14 @@ void MediaOperationStateReplicatorListenerImpl::stateRemovedForItems(
{
mAdapter->remove(mAdapter->getCommunicator()->stringToIdentity(ulawAlaw->operationId));
}
+ void visitResamplerMediaOperationStateItem(const ResamplerMediaOperationStateItemPtr& resample)
+ {
+ mAdapter->remove(mAdapter->getCommunicator()->stringToIdentity(resample->operationId));
+ }
+ void visitG722MediaOperationStateItem(const G722MediaOperationStateItemPtr& g722)
+ {
+ mAdapter->remove(mAdapter->getCommunicator()->stringToIdentity(g722->operationId));
+ }
Ice::ObjectAdapterPtr mAdapter;
};
@@ -77,15 +88,35 @@ void MediaOperationStateReplicatorListenerImpl::stateSet(
{
UlawAlawFactoryPtr factory = UlawAlawFactoryPtr::dynamicCast(mAdapter->find(ulawAlaw->factoryId));
+ createTranslationMediaOperation(factory, ulawAlaw);
+ }
+
+ void visitResamplerMediaOperationStateItem(const ResamplerMediaOperationStateItemPtr& resample)
+ {
+ ResampleFactoryPtr factory = ResampleFactoryPtr::dynamicCast(mAdapter->find(resample->factoryId));
+
+ createTranslationMediaOperation(factory, resample);
+ }
+
+ void visitG722MediaOperationStateItem(const G722MediaOperationStateItemPtr& g722)
+ {
+ G722FactoryPtr factory = G722FactoryPtr::dynamicCast(mAdapter->find(g722->factoryId));
+
+ createTranslationMediaOperation(factory, g722);
+ }
+
+ void createTranslationMediaOperation(const TranslatorOperationFactoryPtr& factory,
+ const TranslatorMediaOperationStateItemPtr& item)
+ {
if (!factory)
{
return;
}
factory->createMediaOperation(
- ulawAlaw->sourceFormat,
- ulawAlaw->sinkFormat,
- ulawAlaw->operationId);
+ item->sourceFormat,
+ item->sinkFormat,
+ item->operationId);
}
Ice::ObjectAdapterPtr mAdapter;
};
commit dc1598b75cca6711f9bd233ac477031fb39dbca1
Author: Mark Michelson <mmichelson at digium.com>
Date: Thu Sep 1 11:03:06 2011 -0500
Add documentation and change some bad function names.
diff --git a/src/TranslatorOperation.h b/src/TranslatorOperation.h
index bb9f4c7..152a469 100644
--- a/src/TranslatorOperation.h
+++ b/src/TranslatorOperation.h
@@ -30,6 +30,9 @@ namespace AsteriskSCF
namespace MediaOperationsCore
{
+/**
+ * Base class for Media operations that translate between formats
+ */
class TranslatorOperation : public AsteriskSCF::Media::V1::MediaOperation
{
public:
@@ -43,16 +46,59 @@ public:
virtual ~TranslatorOperation();
+ /**
+ * Set state in the state replicator.
+ *
+ * The only reason that subclasses should override this is if they have
+ * any specific state they need to replicate in addition to the common
+ * traits all translators replicate.
+ *
+ * If this is overridden, the best approach to take is to set fields within
+ * your state item and then call TranslatorOperation::setState().
+ */
virtual void setState();
+ /**
+ * Remove state in the state replicator.
+ *
+ * The only reason that subclasses should override this is if they have
+ * specific state they need to replicate in addition to the common
+ * traits all translators replicate.
+ *
+ * If this is overridden, the best approach to take is to set fields within
+ * your state item and then call TranslatorOperation::removeState().
+ */
virtual void removeState();
+ /**
+ * Add objects to the object adapter.
+ *
+ * The only reason to override this would be if additional servants should
+ * be added to the object adapter. Be sure to call TranslatorOperation::activate(id)
+ * in your override.
+ *
+ * This will add the operation, along with its source and sink to the object adapter.
+ */
virtual AsteriskSCF::Media::V1::MediaOperationPrx activate(const std::string& id);
- virtual void destroy(const Ice::Current&);
+ /**
+ * Remove items from the object adapter.
+ *
+ * NOTE: It may seem like this should be virtual, so that custom servants added
+ * during activate() could be removed here. However, this should be done in an
+ * overridden destructor instead. Otherwise, replicated TranslatorOperations
+ * would not properly remove servants from their object adapter.
+ */
+ void destroy(const Ice::Current&);
+ /**
+ * Get the stream source for this operation.
+ */
AsteriskSCF::Media::V1::StreamSourcePrx getSource(const Ice::Current&);
+ /**
+ * Get the stream sink for this operation
+ */
AsteriskSCF::Media::V1::StreamSinkPrx getSink(const Ice::Current&);
protected:
diff --git a/src/TranslatorOperationFactory.cpp b/src/TranslatorOperationFactory.cpp
index bce48d7..82137fb 100644
--- a/src/TranslatorOperationFactory.cpp
+++ b/src/TranslatorOperationFactory.cpp
@@ -38,6 +38,8 @@ TranslatorOperationFactory::TranslatorOperationFactory(
mLocatorParams->service = MediaOperationDiscoveryTranslatorService;
}
+TranslatorOperationFactory::~TranslatorOperationFactory() { }
+
MediaOperationPrx TranslatorOperationFactory::createMediaOperation(
const StreamSourcePrx& source,
const StreamSinkPrx& sink,
@@ -56,7 +58,7 @@ MediaOperationPrx TranslatorOperationFactory::createMediaOperation(
sourceFormats.end(),
mTranslations.begin(),
mTranslations.end(),
- TranslatorOperationFactory::formatsEqual2);
+ TranslatorOperationFactory::formatsEqualMap);
if (supportedInput == sourceFormats.end())
{
@@ -76,7 +78,7 @@ MediaOperationPrx TranslatorOperationFactory::createMediaOperation(
sinkFormats.end(),
mTranslations[(*supportedInput)->name].begin(),
mTranslations[(*supportedInput)->name].end(),
- TranslatorOperationFactory::formatsEqual1);
+ TranslatorOperationFactory::formatsEqual);
if (supportedOutput == sinkFormats.end())
{
@@ -108,14 +110,14 @@ void TranslatorOperationFactory::addTranslation(
mLocatorParams->attributes.push_back(attrs);
}
-bool TranslatorOperationFactory::formatsEqual1(
+bool TranslatorOperationFactory::formatsEqual(
const FormatPtr& lhs,
const FormatPtr& rhs)
{
return lhs->name == rhs->name;
}
-bool TranslatorOperationFactory::formatsEqual2(
+bool TranslatorOperationFactory::formatsEqualMap(
const FormatPtr& lhs,
const std::pair<std::string, FormatSeq>& rhs)
{
diff --git a/src/TranslatorOperationFactory.h b/src/TranslatorOperationFactory.h
index d95389f..ee2a279 100644
--- a/src/TranslatorOperationFactory.h
+++ b/src/TranslatorOperationFactory.h
@@ -24,6 +24,10 @@ namespace AsteriskSCF
namespace MediaOperationsCore
{
+/**
+ * Base class for Media Operation Factories that
+ * produce translators.
+ */
class TranslatorOperationFactory : public MediaOperationFactoryImpl
{
public:
@@ -34,32 +38,66 @@ public:
const MediaOperationReplicationContextPtr& replicationContext,
const std::string& name);
+ virtual ~TranslatorOperationFactory();
+
+ /**
+ * Overload of the slice MediaOperationFactory::createMediaOperation
+ * method.
+ *
+ * This will use the translation map in order to determine if the source
+ * and sink parameters support formats that this factory knows about.
+ * It will then call the below createMediaOperation() method once
+ * the formats have been determined.
+ */
AsteriskSCF::Media::V1::MediaOperationPrx createMediaOperation(
const AsteriskSCF::Media::V1::StreamSourcePrx& source,
const AsteriskSCF::Media::V1::StreamSinkPrx& sink,
const Ice::Current&);
+ /**
+ * Pure virtual function to create a media operation.
+ *
+ * Create a media operation with the given formats.
+ *
+ * @param sourceFormat The format for the media operation's source to use.
+ * @param sinkFormat The format for the media operation's sink to use.
+ * @param operationId The Ice identification string to use when adding the operation
+ * to the object adapter.
+ * @return A proxy to the newly created media operation
+ */
virtual AsteriskSCF::Media::V1::MediaOperationPrx createMediaOperation(
const AsteriskSCF::Media::V1::FormatPtr& sourceFormat,
const AsteriskSCF::Media::V1::FormatPtr& sinkFormat,
const std::string& operationId) = 0;
+ /**
+ * Add a translation supported by the factory.
+ *
+ * @param inFormat The input format for translation
+ * @param outFormat The output format for translation
+ * @param cost The cost of the translation. Units for cost are
+ * undetermined at this time. A higher cost implies a
+ */
void addTranslation(
- const AsteriskSCF::Media::V1::FormatPtr&,
- const AsteriskSCF::Media::V1::FormatPtr&,
+ const AsteriskSCF::Media::V1::FormatPtr& inFormat,
+ const AsteriskSCF::Media::V1::FormatPtr& outFormat,
int cost);
- static bool formatsEqual1(
+ /**
+ * Handy function to compare two formats. Useful for predicates
+ * in STL algorithms. Returns true if the formats are the same.
+ */
+ static bool formatsEqual(
const AsteriskSCF::Media::V1::FormatPtr& lhs,
const AsteriskSCF::Media::V1::FormatPtr& rhs);
- static bool formatsEqual2(
- const AsteriskSCF::Media::V1::FormatPtr& lhs,
- const std::pair<std::string, AsteriskSCF::Media::V1::FormatSeq>& rhs);
-
private:
typedef std::map<std::string, AsteriskSCF::Media::V1::FormatSeq> TranslationMap;
+ static bool formatsEqualMap(
+ const AsteriskSCF::Media::V1::FormatPtr& lhs,
+ const std::pair<std::string, AsteriskSCF::Media::V1::FormatSeq>& rhs);
+
TranslationMap mTranslations;
};
diff --git a/src/g722.cpp b/src/g722.cpp
index 112bbec..038110d 100644
--- a/src/g722.cpp
+++ b/src/g722.cpp
@@ -112,7 +112,7 @@ private:
FramePtr translate(const FramePtr inFrame)
{
- if (!TranslatorOperationFactory::formatsEqual1(inFrame->mediaFormat, mInputFormat))
+ if (!TranslatorOperationFactory::formatsEqual(inFrame->mediaFormat, mInputFormat))
{
mLogger(Error) << "Cannot translate frame because the format is not what we expect.";
throw UnsupportedMediaFormatException();
diff --git a/src/resample.cpp b/src/resample.cpp
index de4cb64..572bc1a 100644
--- a/src/resample.cpp
+++ b/src/resample.cpp
@@ -26,16 +26,6 @@
using namespace AsteriskSCF::Media::V1;
-namespace
-{
-
-bool formatCompare(const FormatPtr& format1, const FormatPtr& format2)
-{
- return format1->name == format2->name;
-}
-
-};
-
namespace AsteriskSCF
{
@@ -85,7 +75,7 @@ private:
FramePtr translate(const FramePtr inFrame)
{
- if (!formatCompare(inFrame->mediaFormat, mInputFormat))
+ if (!TranslatorOperationFactory::formatsEqual(inFrame->mediaFormat, mInputFormat))
{
mLogger(Error) << "Cannot resample frame because the format is not what was expected";
throw UnsupportedMediaFormatException();
commit 6a39d3d26db6669fee7ea19d9aa9dbc9310923fb
Author: Mark Michelson <mmichelson at digium.com>
Date: Wed Aug 31 19:59:08 2011 -0500
Add G.722 tests. They pass.
diff --git a/src/g722.cpp b/src/g722.cpp
index 7d60640..112bbec 100644
--- a/src/g722.cpp
+++ b/src/g722.cpp
@@ -76,7 +76,7 @@ private:
outsamples = g722_decode(&mDecodeToSlin8, outbuffer16, &inPayload->payload.front(), inFormat->frameSize);
}
- ShortSeqPayloadPtr outPayload(new ShortSeqPayload(Ice::ShortSeq(outsamples)));
+ ShortSeqPayloadPtr outPayload(new ShortSeqPayload(Ice::ShortSeq(outbuffer16, outbuffer16 + outsamples)));
outFormat->frameSize = outsamples;
return new Frame(outFormat, outPayload);
}
@@ -105,7 +105,7 @@ private:
outlen = g722_encode(&mEncodeFromSlin8, outbuffer8, &inPayload->payload.front(), inFormat->frameSize);
}
- ByteSeqPayloadPtr outPayload(new ByteSeqPayload(Ice::ByteSeq(outlen)));
+ ByteSeqPayloadPtr outPayload(new ByteSeqPayload(Ice::ByteSeq(outbuffer8, outbuffer8 + outlen)));
outFormat->frameSize = outlen;
return new Frame(outFormat, outPayload);
}
diff --git a/test/TestMediaOperations.cpp b/test/TestMediaOperations.cpp
index 2df6731..f4bec35 100644
--- a/test/TestMediaOperations.cpp
+++ b/test/TestMediaOperations.cpp
... 11617 lines suppressed ...
--
asterisk-scf/integration/media_operations_core.git
More information about the asterisk-scf-commits
mailing list