Newer
Older
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;
}
}
if(ret & AST_FORMAT_AUDIO_MASK)
return ret;
if (option_debug > 3)
ast_log(LOG_DEBUG, "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;
}
void ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing)
{
char *parse;
char *this;
int format;
parse = ast_strdupa(list);
while ((this = strsep(&parse, ","))) {
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)
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);
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
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
}
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;
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
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
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:
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;
}
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
int ast_codec_get_len(int format, int samples)
{
int len = 0;
/* XXX Still need speex, g723, and lpc10 XXX */
switch(format) {
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:
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;
}
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
struct ast_frame *ast_frame_enqueue(struct ast_frame *head, struct ast_frame *f, int maxlen, int dupe)
{
struct ast_frame *cur, *oldhead;
int len=0;
if (f && dupe)
f = ast_frdup(f);
if (!f)
return head;
f->next = NULL;
if (!head)
return f;
cur = head;
while(cur->next) {
cur = cur->next;
len++;
if (len >= maxlen) {
oldhead = head;
head = head->next;
ast_frfree(oldhead);
}
}
return head;
}