Newer
Older
return;
memcpy(&oldorder, pref, sizeof(oldorder));
memset(pref, 0, sizeof(*pref));
for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); 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)
{
int x, newindex = -1;
ast_codec_pref_remove(pref, format);
for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
Joshua Colp
committed
if (AST_FORMAT_LIST[x].bits == format) {
newindex = x + 1;
break;
}
}
Joshua Colp
committed
if (newindex) {
for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
Joshua Colp
committed
if (!pref->order[x]) {
pref->order[x] = newindex;
break;
}
}
}
return x;
}
/*! \brief Set packet size for codec */
int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems)
{
int x, index = -1;
for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
Joshua Colp
committed
if (AST_FORMAT_LIST[x].bits == format) {
index = x;
break;
}
}
Joshua Colp
committed
if (index < 0)
return -1;
/* size validation */
Joshua Colp
committed
if (!framems)
framems = AST_FORMAT_LIST[index].def_ms;
Joshua Colp
committed
if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
framems -= framems % AST_FORMAT_LIST[index].inc_ms;
Joshua Colp
committed
if (framems < AST_FORMAT_LIST[index].min_ms)
framems = AST_FORMAT_LIST[index].min_ms;
Joshua Colp
committed
if (framems > AST_FORMAT_LIST[index].max_ms)
framems = AST_FORMAT_LIST[index].max_ms;
for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
Joshua Colp
committed
if (pref->order[x] == (index + 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, index = -1, framems = 0;
for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
Joshua Colp
committed
if (AST_FORMAT_LIST[x].bits == format) {
fmt = AST_FORMAT_LIST[x];
index = x;
break;
}
}
for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
Joshua Colp
committed
if (pref->order[x] == (index + 1)) {
framems = pref->framing[x];
break;
}
}
/* size validation */
Joshua Colp
committed
if (!framems)
framems = AST_FORMAT_LIST[index].def_ms;
Joshua Colp
committed
if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
framems -= framems % AST_FORMAT_LIST[index].inc_ms;
Joshua Colp
committed
if (framems < AST_FORMAT_LIST[index].min_ms)
framems = AST_FORMAT_LIST[index].min_ms;
Joshua Colp
committed
if (framems > AST_FORMAT_LIST[index].max_ms)
framems = AST_FORMAT_LIST[index].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 < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); 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);
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
}
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;
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
1351
1352
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:
samples = speex_samples(f->data, f->datalen);
break;
case AST_FORMAT_G723_1:
samples = g723_samples(f->data, 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 */
samples = 22 * 8;
samples += (((char *)(f->data))[7] & 0x1) * 8;
break;
case AST_FORMAT_ULAW:
case AST_FORMAT_ALAW:
samples = f->datalen;
break;
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;
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;
short *fdata = f->data;
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;
for (count = 0, data1 = f1->data, data2 = f2->data;
count < f1->samples;
count++, data1++, data2++)
Kevin P. Fleming
committed
ast_slinear_saturated_add(data1, data2);
return 0;
}