Newer
Older
Joshua Colp
committed
if (total_len) {
strncat(buf, ")", total_len - 1); /* safe */
total_len--;
}
return size - total_len;
}
int ast_codec_pref_index(struct ast_codec_pref *pref, int idx)
{
int slot = 0;
if ((idx >= 0) && (idx < sizeof(pref->order))) {
slot = pref->order[idx];
}
return slot ? AST_FORMAT_LIST[slot - 1].bits : 0;
}
void ast_codec_pref_remove(struct ast_codec_pref *pref, int format)
{
struct ast_codec_pref oldorder;
int x, y = 0;
int slot;
Joshua Colp
committed
if (!pref->order[0])
return;
memcpy(&oldorder, pref, sizeof(oldorder));
memset(pref, 0, sizeof(*pref));
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
slot = oldorder.order[x];
size = oldorder.framing[x];
Joshua Colp
committed
if (! slot)
break;
Joshua Colp
committed
if (AST_FORMAT_LIST[slot-1].bits != format) {
pref->order[y] = slot;
pref->framing[y++] = size;
}
}
}
int ast_codec_pref_append(struct ast_codec_pref *pref, int format)
{
ast_codec_pref_remove(pref, format);
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
Joshua Colp
committed
if (AST_FORMAT_LIST[x].bits == format) {
newindex = x + 1;
break;
}
}
Joshua Colp
committed
if (newindex) {
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
Joshua Colp
committed
if (!pref->order[x]) {
pref->order[x] = newindex;
break;
}
}
}
return x;
}
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
/*! \brief Prepend codec to list */
void ast_codec_pref_prepend(struct ast_codec_pref *pref, int format, int only_if_existing)
{
int x, newindex = 0;
/* First step is to get the codecs "index number" */
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
if (AST_FORMAT_LIST[x].bits == format) {
newindex = x + 1;
break;
}
}
/* Done if its unknown */
if (!newindex)
return;
/* Now find any existing occurrence, or the end */
for (x = 0; x < 32; x++) {
if (!pref->order[x] || pref->order[x] == newindex)
break;
}
if (only_if_existing && !pref->order[x])
return;
/* Move down to make space to insert - either all the way to the end,
or as far as the existing location (which will be overwritten) */
for (; x > 0; x--) {
pref->order[x] = pref->order[x - 1];
pref->framing[x] = pref->framing[x - 1];
}
/* And insert the new entry */
pref->order[0] = newindex;
pref->framing[0] = 0; /* ? */
}
/*! \brief Set packet size for codec */
int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems)
{
int x, idx = -1;
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
Joshua Colp
committed
if (AST_FORMAT_LIST[x].bits == format) {
idx = x;
break;
}
}
if (idx < 0)
return -1;
/* size validation */
Joshua Colp
committed
if (!framems)
framems = AST_FORMAT_LIST[idx].def_ms;
if (AST_FORMAT_LIST[idx].inc_ms && framems % AST_FORMAT_LIST[idx].inc_ms) /* avoid division by zero */
framems -= framems % AST_FORMAT_LIST[idx].inc_ms;
if (framems < AST_FORMAT_LIST[idx].min_ms)
framems = AST_FORMAT_LIST[idx].min_ms;
if (framems > AST_FORMAT_LIST[idx].max_ms)
framems = AST_FORMAT_LIST[idx].max_ms;
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
if (pref->order[x] == (idx + 1)) {
pref->framing[x] = framems;
break;
}
}
return x;
}
/*! \brief Get packet size for codec */
struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, int format)
{
int x, idx = -1, framems = 0;
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
Joshua Colp
committed
if (AST_FORMAT_LIST[x].bits == format) {
fmt = AST_FORMAT_LIST[x];
idx = x;
break;
}
}
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
if (pref->order[x] == (idx + 1)) {
framems = pref->framing[x];
break;
}
}
/* size validation */
Joshua Colp
committed
if (!framems)
framems = AST_FORMAT_LIST[idx].def_ms;
if (AST_FORMAT_LIST[idx].inc_ms && framems % AST_FORMAT_LIST[idx].inc_ms) /* avoid division by zero */
framems -= framems % AST_FORMAT_LIST[idx].inc_ms;
if (framems < AST_FORMAT_LIST[idx].min_ms)
framems = AST_FORMAT_LIST[idx].min_ms;
if (framems > AST_FORMAT_LIST[idx].max_ms)
framems = AST_FORMAT_LIST[idx].max_ms;
fmt.cur_ms = framems;
return fmt;
}
int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best)
{
int x, ret = 0, slot;
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
slot = pref->order[x];
break;
ret = AST_FORMAT_LIST[slot-1].bits;
break;
}
}
Joshua Colp
committed
if (ret & AST_FORMAT_AUDIO_MASK)
return ret;
ast_debug(4, "Could not find preferred codec - %s\n", find_best ? "Going for the best codec" : "Returning zero codec");
return find_best ? ast_best_codec(formats) : 0;
}
int ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing)
{
char *parse = NULL, *this = NULL, *psize = NULL;
int format = 0, framems = 0;
parse = ast_strdupa(list);
while ((this = strsep(&parse, ","))) {
framems = 0;
if ((psize = strrchr(this, ':'))) {
ast_debug(1, "Packetization for codec: %s is %s\n", this, psize);
framems = 0;
errors++;
ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this);
}
if (!(format = ast_getformatbyname(this))) {
ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", this);
continue;
}
if (mask) {
if (allowing)
*mask |= format;
else
*mask &= ~format;
}
/* Set up a preference list for audio. Do not include video in preferences
since we can not transcode video and have to use whatever is offered
*/
if (pref && (format & AST_FORMAT_AUDIO_MASK)) {
if (allowing) {
ast_codec_pref_setsize(pref, format, framems);
}
else
ast_codec_pref_remove(pref, format);
} else if (!allowing) {
memset(pref, 0, sizeof(*pref));
}
}
}
static int g723_len(unsigned char buf)
{
enum frame_type type = buf & TYPE_MASK;
switch(type) {
case TYPE_DONTSEND:
return 0;
break;
case TYPE_SILENCE:
return 4;
break;
case TYPE_HIGH:
return 24;
break;
case TYPE_LOW:
return 20;
break;
default:
ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", type);
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
}
return -1;
}
static int g723_samples(unsigned char *buf, int maxlen)
{
int pos = 0;
int samples = 0;
int res;
while(pos < maxlen) {
res = g723_len(buf[pos]);
if (res <= 0)
break;
samples += 240;
pos += res;
}
return samples;
}
static unsigned char get_n_bits_at(unsigned char *data, int n, int bit)
{
int byte = bit / 8; /* byte containing first bit */
int rem = 8 - (bit % 8); /* remaining bits in first byte */
unsigned char ret = 0;
if (n <= 0 || n > 8)
return 0;
if (rem < n) {
ret = (data[byte] << (n - rem));
ret |= (data[byte + 1] >> (8 - n + rem));
} else {
ret = (data[byte] >> (rem - n));
}
return (ret & (0xff >> (8 - n)));
}
static int speex_get_wb_sz_at(unsigned char *data, int len, int bit)
{
static int SpeexWBSubModeSz[] = {
0, 36, 112, 192,
352, 0, 0, 0 };
int off = bit;
unsigned char c;
/* skip up to two wideband frames */
if (((len * 8 - off) >= 5) &&
get_n_bits_at(data, 1, off)) {
c = get_n_bits_at(data, 3, off + 1);
off += SpeexWBSubModeSz[c];
if (((len * 8 - off) >= 5) &&
get_n_bits_at(data, 1, off)) {
c = get_n_bits_at(data, 3, off + 1);
off += SpeexWBSubModeSz[c];
if (((len * 8 - off) >= 5) &&
get_n_bits_at(data, 1, off)) {
ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
return -1;
}
}
}
return off - bit;
}
static int speex_samples(unsigned char *data, int len)
{
static int SpeexSubModeSz[] = {
220, 300, 364, 492,
79, 0, 0, 0,
0, 0, 0, 0 };
static int SpeexInBandSz[] = {
1, 1, 4, 4,
4, 4, 4, 4,
8, 8, 16, 16,
32, 32, 64, 64 };
int bit = 0;
int cnt = 0;
int off;
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
unsigned char c;
while ((len * 8 - bit) >= 5) {
/* skip wideband frames */
off = speex_get_wb_sz_at(data, len, bit);
if (off < 0) {
ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n");
break;
}
bit += off;
if ((len * 8 - bit) < 5) {
ast_log(LOG_WARNING, "Not enough bits remaining after wide band for speex samples.\n");
break;
}
/* get control bits */
c = get_n_bits_at(data, 5, bit);
bit += 5;
if (c == 15) {
/* terminator */
break;
} else if (c == 14) {
/* in-band signal; next 4 bits contain signal id */
c = get_n_bits_at(data, 4, bit);
bit += 4;
bit += SpeexInBandSz[c];
} else if (c == 13) {
/* user in-band; next 5 bits contain msg len */
c = get_n_bits_at(data, 5, bit);
bit += 5;
bit += c * 8;
} else if (c > 8) {
/* unknown */
break;
} else {
/* skip number bits for submode (less the 5 control bits) */
bit += SpeexSubModeSz[c] - 5;
cnt += 160; /* new frame */
}
}
return cnt;
}
int ast_codec_get_samples(struct ast_frame *f)
{
int samples=0;
switch(f->subclass) {
case AST_FORMAT_SPEEX:
Michiel van Baak
committed
samples = speex_samples(f->data.ptr, f->datalen);
break;
case AST_FORMAT_G723_1:
Michiel van Baak
committed
samples = g723_samples(f->data.ptr, f->datalen);
break;
case AST_FORMAT_ILBC:
samples = 240 * (f->datalen / 50);
break;
case AST_FORMAT_GSM:
samples = 160 * (f->datalen / 33);
break;
case AST_FORMAT_G729A:
samples = f->datalen * 8;
break;
case AST_FORMAT_SLINEAR:
case AST_FORMAT_SLINEAR16:
samples = f->datalen / 2;
break;
case AST_FORMAT_LPC10:
/* assumes that the RTP packet contains one LPC10 frame */
Michiel van Baak
committed
samples += (((char *)(f->data.ptr))[7] & 0x1) * 8;
break;
case AST_FORMAT_ULAW:
case AST_FORMAT_ALAW:
samples = f->datalen;
break;
Russell Bryant
committed
case AST_FORMAT_G722:
case AST_FORMAT_ADPCM:
case AST_FORMAT_G726:
samples = f->datalen * 2;
break;
default:
ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(f->subclass));
}
return samples;
}
int ast_codec_get_len(int format, int samples)
{
int len = 0;
/* XXX Still need speex, g723, and lpc10 XXX */
switch(format) {
Matt O'Gorman
committed
case AST_FORMAT_G723_1:
len = (samples / 240) * 20;
break;
case AST_FORMAT_ILBC:
len = (samples / 240) * 50;
break;
case AST_FORMAT_GSM:
len = (samples / 160) * 33;
break;
case AST_FORMAT_G729A:
len = samples / 8;
break;
case AST_FORMAT_SLINEAR:
case AST_FORMAT_SLINEAR16:
len = samples * 2;
break;
case AST_FORMAT_ULAW:
case AST_FORMAT_ALAW:
len = samples;
break;
Russell Bryant
committed
case AST_FORMAT_G722:
case AST_FORMAT_ADPCM:
case AST_FORMAT_G726:
len = samples / 2;
break;
default:
ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format));
}
return len;
}
int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
{
int count;
Michiel van Baak
committed
short *fdata = f->data.ptr;
Kevin P. Fleming
committed
short adjust_value = abs(adjustment);
if ((f->frametype != AST_FRAME_VOICE) || (f->subclass != AST_FORMAT_SLINEAR))
return -1;
Kevin P. Fleming
committed
if (!adjustment)
return 0;
for (count = 0; count < f->samples; count++) {
if (adjustment > 0) {
Kevin P. Fleming
committed
ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
} else if (adjustment < 0) {
Kevin P. Fleming
committed
ast_slinear_saturated_divide(&fdata[count], &adjust_value);
}
}
return 0;
}
int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
{
int count;
short *data1, *data2;
if ((f1->frametype != AST_FRAME_VOICE) || (f1->subclass != AST_FORMAT_SLINEAR))
return -1;
if ((f2->frametype != AST_FRAME_VOICE) || (f2->subclass != AST_FORMAT_SLINEAR))
return -1;
if (f1->samples != f2->samples)
return -1;
Michiel van Baak
committed
for (count = 0, data1 = f1->data.ptr, data2 = f2->data.ptr;
count < f1->samples;
count++, data1++, data2++)
Kevin P. Fleming
committed
ast_slinear_saturated_add(data1, data2);
return 0;
}