· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
Asterisk Ver0-1-0/Translate DocC

AsteriskVer0-1-0/TranslateDocC


* codec_gsm.so ¸¸ ¸¸µé¾î Áü.

* include/transrate.h
struct ast_translator {
        char name[80];
        int srcfmt;
        int dstfmt;
        struct ast_translator_pvt *(*new)();
        int (*framein)(struct ast_translator_pvt *pvt, struct ast_frame *in);
        struct ast_frame * (*frameout)(struct ast_translator_pvt *pvt);
        void (*destroy)(struct ast_translator_pvt *pvt);
        /* For performance measurements */
        /* Generate an example frame */
        struct ast_frame * (*sample)(void);
        /* Cost in milliseconds for encoding/decoding 1 second of sound */
        int cost;
        /* For linking, not to be modified by the translator */
        struct ast_translator *next;
};

* codec_gsm.c
static struct ast_translator gsmtolin =
        { "gsmtolin",
           AST_FORMAT_GSM, AST_FORMAT_SLINEAR,
           gsm_new,
           gsmtolin_framein,
           gsmtolin_frameout,
           gsm_destroy_stuff,
           gsmtolin_sample
           };

static struct ast_translator lintogsm =
        { "lintogsm",
           AST_FORMAT_SLINEAR, AST_FORMAT_GSM,
           gsm_new,
           lintogsm_framein,
           lintogsm_frameout,
           gsm_destroy_stuff,
           lintogsm_sample
           };

* include/frame.h
/* Data formats for capabilities and frames alike */
#define AST_FORMAT_G723_1       (1 << 0)        /* G.723.1 compression */
#define AST_FORMAT_GSM          (1 << 1)        /* GSM compression */
#define AST_FORMAT_ULAW         (1 << 2)        /* Raw mu-law data (G.711) */
#define AST_FORMAT_ALAW         (1 << 3)        /* Raw A-law data (G.711) */
#define AST_FORMAT_MP3          (1 << 4)        /* MPEG-2 layer 3 */
#define AST_FORMAT_ADPCM        (1 << 5)        /* ADPCM */
#define AST_FORMAT_SLINEAR      (1 << 6)        /* Raw 16-bit Signed Linear (8000 Hz) PCM */
#define AST_FORMAT_MAX_AUDIO (1 << 15)  /* Maximum audio format */
#define AST_FORMAT_JPEG         (1 << 16)       /* JPEG Images */
#define AST_FORMAT_PNG          (1 << 17)       /* PNG Images */
#define AST_FORMAT_H261         (1 << 18)       /* H.261 Video */
#define AST_FORMAT_H263         (1 << 19)       /* H.263 Video */

int load_module(void)
{
        int res;
        res=ast_register_translator(&gsmtolin);
        if (!res)
                res=ast_register_translator(&lintogsm);
        else
                ast_unregister_translator(&gsmtolin);
        return res;
}

* codec_g723_1.c
static struct ast_translator g723tolin =
#ifdef ANNEX_B
        { "g723tolinb",
#else
        { "g723tolin",
#endif
           AST_FORMAT_G723_1, AST_FORMAT_SLINEAR,
           g723tolin_new,
           g723tolin_framein,
           g723tolin_frameout,
           g723_destroy,
           g723tolin_sample
           };

static struct ast_translator lintog723 =
#ifdef ANNEX_B
        { "lintog723b",
#else
        { "lintog723",
#endif
           AST_FORMAT_SLINEAR, AST_FORMAT_G723_1,
           lintog723_new,
           lintog723_framein,
           lintog723_frameout,
           g723_destroy,
           lintog723_sample
           };

int load_module(void)
{
        int res;
        res=ast_register_translator(&g723tolin);
        if (!res)
                res=ast_register_translator(&lintog723);
        else
                ast_unregister_translator(&g723tolin);
        return res;
}

static struct ast_translator *list = NULL;

* int ast_register_translator(struct ast_translator *t)
  • t->srcfmt = powerof(t->srcfmt);
  • t->dstfmt = powerof(t->dstfmt);
  • scrfmt ³ª dstfmt ÀÇ º¯È¯µÈ °ªÀÌ MAX_FORMAT Áï 32 º¸´Ù Å©°Å³ª °°À¸¸é ¿À·ù
    • return -1
  • calc_cost(t); ast_transrator structure ÀÇ cost °ª ¼³Á¤
  • pthread_mutex_lock(&list_lock);
  • t->next = list;
  • list = t;
  • rebuild_matrix();
  • pthread_mutex_unlock(&list_lock);
  • return 0;

* static int powerof(int d)
  • interger d ¸¦ bit ·Î Ç¥½ÃÇÏ¿© 1ÀÎ bit Áß °¡Àå ¿À¸¥ÂÊ bit ÀÌ ¿À¸¥ÂÊ ³¡¿¡¼­ ºÎÅÍ ¸î¹ø°ÀÎÁö¸¦ ³ªÅ¸³½´Ù.
  • d=1 ÀÎ °æ¿ì 0 ; ¿À¸¥ÂÊ ³¡ bit ¸¦ 0 ¹ø° bit À¸·Î Ç¥½ÃÇÔ.
  • d=2 ÀÎ °æ¿ì 1
  • d=3 ÀÎ °æ¿ì 0
  • d=4 ÀÎ °æ¿ì 2
  • d=10 ÀÎ °æ¿ì 1
  • d=16 ÀÎ °æ¿ì 4

calc_cost


* static void calc_cost(struct ast_translator *t)
  • int sofar=0;
  • structure t ¿¡ sample ÀÌ ¼³Á¤µÇ¾î ÀÖÁö ¾ÊÀ¸¸é t->cost ¸¦ 99999 À¸·Î ¼³Á¤ÇÏ°í return
  • pvt=t->new() transrate module ÀÇ new ÇÔ¼ö¸¦ ¼öÇà
    • ÇÔ¼ö ¼öÇà¿¡ ¿À·ù°¡ ÀÖÀ¸¸é t->cost ¸¦ 99999 À¸·Î ¼³Á¤ÇÏ°í return
  • gettimeofday(&start, NULL);
  • while(sofar < 1000) {
    • f = t->sample();
    • t->framein(pvt, f);
    • ast_frfree(f);
    • while((out = t->frameout(pvt))) {
      • sofar += out->timelen;
      • ast_frfree(out);
    • }
  • }
  • gettimeofday(&finish, NULL);
  • t->destroy(pvt);
  • cost = (finish.tv_sec - start.tv_sec) * 1000 + (finish.tv_usec - start.tv_usec) / 1000;
  • t->cost = cost;

  • framein °ú frameout À» 50¹ø ¼öÇàÇϴµ¥ °É¸®´Â ½Ã°£À» ÃøÁ¤ÇÏ¿© ast_transrator structure ÀÇ cost °ªÀ¸·Î ¼³Á¤.

GSM-FR is specified in ETSI 06.10 (ETS 300 961) and is based on RPE-LTP (Regular Pulse Excitation - Long Term Prediction) speech coding paradigm. Like many other speech codecs, linear prediction is used in the synthesis filter. However, unlike most modern speech codecs, the order of the linear prediction is only 8. In modern narrowband speech codecs the order is usually 10 and in wideband speech codecs the order is usually 16.

The speech encoder accepts 13 bit linear PCM at a 8 kHz sample rate, e.g., directly from an A/D converter in a phone or computer. G.711 8-bit nonlinear A-law or ¥ì-law PCM from the PSTN can be converted to 13 bit linear PCM with a lookup table. In GSM, the encoded speech is passed to the channel encoder specified in GSM 05.03. In the receive direction, the inverse operations take place.

The codec operates on 160 sample frames that span 20 ms, so this is the minimum transcoder delay possible even with infinitely fast CPUs and zero network latency. The operational requirement is that the transcoder delay should be less than 30 ms. The transcoder delay is defined as the time interval between the instant a speech frame of 160 samples has been received at the encoder input and the instant the corresponding 160 reconstructed speech samples have been out-put by the speech decoder at an 8 kHz sample rate.[1]

* example gsm codec

struct ast_translator_pvt {
        gsm gsm;
        struct ast_frame f;
        /* Space to build offset */
        char offset[AST_FRIENDLY_OFFSET];
        /* Buffer for our outgoing frame */
        gsm_frame outbuf;
        /* Enough to store a full second */
        short buf[8000];
        int tail;
};

#define gsm_coder_pvt ast_translator_pvt

* static struct ast_translator_pvt *gsm_new()
  • gsm_coder_pvt structure ¸¦ malloc ÇÏ¿© pointer tmp ¸¦ »ý¼ºÇÑ´Ù.
  • tmp->gsm ¿¡ gsm_create() ¿¡ ÀÇÇØ »ý¼ºµÇ´Â gsm À» ¼³Á¤ÇÑ´Ù.
  • tmp->tail = 0;
  • return tmp;
include/asterisk/frame.h
/* A frame of data read used to communicate between
   between channels and applications */
struct ast_frame {
        int frametype;                          /* Kind of frame */
        int subclass;                           /* Subclass, frame dependent */
        int datalen;                            /* Length of data */
        int timelen;                            /* Amount of time associated with this frame */
        int mallocd;                            /* Was the data malloc'd?  i.e. should we
                                                                   free it when we discard the frame? */
        int offset;                                     /* How far into "data" the data really starts */
        char *src;                                      /* Optional source of frame for debugging */
        void *data;                                     /* Pointer to actual data */
};

* static struct ast_frame *gsmtolin_sample()
  • ast_frame structure ¸¦ ¼³Á¤ÇÏ°í ±× structure ÀÇ pointer ¸¦ return
  • static struct ast_frame f;
  • f.frametype = AST_FRAME_VOICE;
  • f.subclass = AST_FORMAT_GSM;
  • f.datalen = sizeof(gsm_slin_ex);
  • /* All frames are 30 ms long */
  • f.timelen = 30;
  • f.mallocd = 0;
  • f.offset = 0;
  • f.src = PRETTY_FUNCTION;
  • f.data = gsm_slin_ex; signed short integer 160 °³ÀÇ array
  • return &f;

* static int gsmtolin_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
  • if (tmp->tail + 160 < sizeof(tmp->buf)/2) {
    • if (gsm_decode(tmp->gsm, f->data, tmp->buf + tmp->tail)) {
      • return -1;
    • }
    • tmp->tail+=160;
  • } else {
    • return -1;
  • }
  • return 0;
* static struct ast_frame *gsmtolin_frameout(struct ast_translator_pvt *tmp)
  • if (!tmp->tail) return NULL;
  • /* Signed linear is no particular frame size, so just send whatever we have in the buffer in one lump sum */
  • tmp->f.frametype = AST_FRAME_VOICE;
  • tmp->f.subclass = AST_FORMAT_SLINEAR;
  • tmp->f.datalen = tmp->tail * 2;
  • /* Assume 8000 Hz */
  • tmp->f.timelen = tmp->tail / 8;
  • tmp->f.mallocd = 0;
  • tmp->f.offset = AST_FRIENDLY_OFFSET;
  • tmp->f.src = PRETTY_FUNCTION;
  • tmp->f.data = tmp->buf;
  • /* Reset tail pointer */
  • tmp->tail = 0;
  • return &tmp->f;


* gsm_destroy_stuff,


gsm_create, gsm_destroy, gsm_encode, gsm_decode - GSM 06.10 lossy sound compression
Synopsis

#include "gsm.h"

gsm gsm_create();

void gsm_encode(handle, src, dst)
gsm handle;
gsm_signal src[160];
gsm_frame dst;

int gsm_decode(handle, src, dst)
gsm handle;
gsm_frame src;
gsm_signal dst[160];

void gsm_destroy(handle)
gsm handle;
Description

Gsm is an implementation of the final draft GSM 06.10 standard for full-rate speech transcoding.

gsm_create() initializes a gsm pass and returns a 'gsm' object which can be used as a handle in subsequent calls to gsm_decode(), gsm_encode() or gsm_destroy().

gsm_encode() encodes an array of 160 13-bit samples (given as gsm_signal's, signed integral values of at least 16 bits) into a gsm_frame of 33 bytes. (gsm_frame is a type defined as an array of 33 gsm_bytes in gsm.h.)

gsm_decode() decodes a gsm_frame into an array of 160 13-bit samples (given as gsm_signals), which sound rather like what you handed to gsm_encode() on the other side of the wire.

gsm_destroy() finishes a gsm pass and frees all storage associated with it.

Sample format

The following scaling is assumed for input to the algorithm:

   0  1                             11 12
   S..v..v..v..v..v..v..v..v..v..v..v..v..*..*..*
Only the top 13 bits are used as a signed input value.
The output of gsm_decode() has the three lower bits set to zero.

Return Value

gsm_create() returns an opaque handle object of type gsm, or 0 on error. gsm_decode() returns -1 if the passed frame is invalid, else 0.
Example

#include "gsm.h"

gsm handle;
gsm_frame buf;
gsm_signal sample[160];
int cc, soundfd;

play() {

/* read compressed data from standard input, write to soundfd */

if (!(handle = gsm_create())) error...

while (cc = read(0, (char *)buf, sizeof buf)) {

if (cc != sizeof buf) error...

if (gsm_decode(handle, buf, sample) < 0) error...

if (write(soundfd, sample, sizeof sample) != sizeof sample)

error...

}

gsm_destroy(handle);
}
record() {

/* read from soundfd, write compressed to standard output */

if (!(handle = gsm_create())) error...

while (cc = read(soundfd, sample, sizeof sample)) {

if (cc != sizeof sample) error...

gsm_encode(handle, sample, buf);

if (write(1, (char *)buf, sizeof buf) != sizeof sample)

error...

}

gsm_destroy(handle);
}


rebuild_matrix


struct ast_translator_dir {
        struct ast_translator *step;    /* Next step translator */
        int cost;                                               /* Complete cost to destination */
};

static struct ast_translator_dir tr_matrix[MAX_FORMAT][MAX_FORMAT];

* static void rebuild_matrix()
  • bzero(tr_matrix, sizeof(tr_matrix));
  • t = list;
  • while(t) {
  • }
  • do {
  • } while (changed);

voice codec


* speech codec À̶ó°íµµ ºÒ¸².

Standard systems to record speech record a frequency from about 500 Hz to 3400 Hz, where most of the frequencies used in speech lie, typically use a sampling rate of 8 kHz (slightly greater than the Nyquist rate.)
The sampling resolution is typically at least 12 or more bits per sample resolution (16 is standard), for a final data rate in the range of 96-128 kbps.
However a good vocoder can provide a reasonably good simulation of voice with as little as 2.4 kbps of data.
G.729 in particular has a final data rate of 8 kbps with superb voice quality. 
G.723 achieves slightly worse quality at data rates of 5.3 kbps and 6.4 kbps. 
Many voice systems use even lower data rates, but below 5 kbps voice quality begins to drop rapidly.

* sample rate ¸¦ 8 khz ·Î ÇÏ°í sampleing resolution À» 12 bits ·Î Çϸé ÇÊ¿äÇÑ data rate ´Â 96 kbps ÀÓ.

* Sampling Rate - When to Measure
The sampling rate is the number of times per second that the waveform is measured, which typically ranges from 8 to 192 thousand times per second (8 kHz to 192 kHz). The greater the rate, the higher the frequency that can be captured.

The sampling rate must be at least twice that of the analog frequency being captured. For example, the sampling rate used to create the digital data on a CD is 44.1 kHz, slightly more than double the 20kHz frequency an average person can hear. The sampling rate for digitizing voice for a toll-quality conversation is typically 8,000 times per second (8 kHz), twice the 4 kHz required for the full spectrum of the human voice.

* Sample Size - The Measurement
Also called "resolution" and "precision," the sample size is the measurement of each sample point on a numeric scale. Known as "quantizing," the sample point is turned into the closest whole number. The more granular the scale (the more increments), the more accurate the digital sample represents the original analog signal. 
sample size 8bits : 0 to 255
sample size 16bits: 0 to 65535

ID
Password
Join
He is truly wise who gains wisdom from another's mishap.


sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2010-07-11 19:03:33
Processing time 0.0015 sec