| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <pjmedia-codec/speex.h> |
| 21 |
#include <pjmedia/codec.h> |
| 22 |
#include <pjmedia/errno.h> |
| 23 |
#include <pjmedia/endpoint.h> |
| 24 |
#include <pjmedia/port.h> |
| 25 |
#include <pj/assert.h> |
| 26 |
#include <pj/log.h> |
| 27 |
#include <pj/pool.h> |
| 28 |
#include <pj/string.h> |
| 29 |
#include <pj/os.h> |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#if defined(PJMEDIA_HAS_SPEEX_CODEC) && PJMEDIA_HAS_SPEEX_CODEC!=0 |
| 35 |
|
| 36 |
#include <speex/speex.h> |
| 37 |
|
| 38 |
#define THIS_FILE "speex_codec.c" |
| 39 |
|
| 40 |
|
| 41 |
static pj_status_t spx_test_alloc( pjmedia_codec_factory *factory, |
| 42 |
const pjmedia_codec_info *id ); |
| 43 |
static pj_status_t spx_default_attr( pjmedia_codec_factory *factory, |
| 44 |
const pjmedia_codec_info *id, |
| 45 |
pjmedia_codec_param *attr ); |
| 46 |
static pj_status_t spx_enum_codecs( pjmedia_codec_factory *factory, |
| 47 |
unsigned *count, |
| 48 |
pjmedia_codec_info codecs[]); |
| 49 |
static pj_status_t spx_alloc_codec( pjmedia_codec_factory *factory, |
| 50 |
const pjmedia_codec_info *id, |
| 51 |
pjmedia_codec **p_codec); |
| 52 |
static pj_status_t spx_dealloc_codec( pjmedia_codec_factory *factory, |
| 53 |
pjmedia_codec *codec ); |
| 54 |
|
| 55 |
|
| 56 |
static pj_status_t spx_codec_init( pjmedia_codec *codec, |
| 57 |
pj_pool_t *pool ); |
| 58 |
static pj_status_t spx_codec_open( pjmedia_codec *codec, |
| 59 |
pjmedia_codec_param *attr ); |
| 60 |
static pj_status_t spx_codec_close( pjmedia_codec *codec ); |
| 61 |
static pj_status_t spx_codec_modify(pjmedia_codec *codec, |
| 62 |
const pjmedia_codec_param *attr ); |
| 63 |
static pj_status_t spx_codec_parse( pjmedia_codec *codec, |
| 64 |
void *pkt, |
| 65 |
pj_size_t pkt_size, |
| 66 |
const pj_timestamp *ts, |
| 67 |
unsigned *frame_cnt, |
| 68 |
pjmedia_frame frames[]); |
| 69 |
static pj_status_t spx_codec_encode( pjmedia_codec *codec, |
| 70 |
const struct pjmedia_frame *input, |
| 71 |
unsigned output_buf_len, |
| 72 |
struct pjmedia_frame *output); |
| 73 |
static pj_status_t spx_codec_decode( pjmedia_codec *codec, |
| 74 |
const struct pjmedia_frame *input, |
| 75 |
unsigned output_buf_len, |
| 76 |
struct pjmedia_frame *output); |
| 77 |
static pj_status_t spx_codec_recover(pjmedia_codec *codec, |
| 78 |
unsigned output_buf_len, |
| 79 |
struct pjmedia_frame *output); |
| 80 |
|
| 81 |
|
| 82 |
static pjmedia_codec_op spx_op = |
| 83 |
{ |
| 84 |
&spx_codec_init, |
| 85 |
&spx_codec_open, |
| 86 |
&spx_codec_close, |
| 87 |
&spx_codec_modify, |
| 88 |
&spx_codec_parse, |
| 89 |
&spx_codec_encode, |
| 90 |
&spx_codec_decode, |
| 91 |
&spx_codec_recover |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
static pjmedia_codec_factory_op spx_factory_op = |
| 96 |
{ |
| 97 |
&spx_test_alloc, |
| 98 |
&spx_default_attr, |
| 99 |
&spx_enum_codecs, |
| 100 |
&spx_alloc_codec, |
| 101 |
&spx_dealloc_codec, |
| 102 |
&pjmedia_codec_speex_deinit |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
enum |
| 107 |
{ |
| 108 |
PARAM_NB, |
| 109 |
PARAM_WB, |
| 110 |
PARAM_UWB, |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
struct speex_param |
| 115 |
{ |
| 116 |
int enabled; |
| 117 |
const SpeexMode *mode; |
| 118 |
int pt; |
| 119 |
unsigned clock_rate; |
| 120 |
int quality; |
| 121 |
int complexity; |
| 122 |
int samples_per_frame; |
| 123 |
int framesize; |
| 124 |
int bitrate; |
| 125 |
int max_bitrate; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
static struct spx_factory |
| 130 |
{ |
| 131 |
pjmedia_codec_factory base; |
| 132 |
pjmedia_endpt *endpt; |
| 133 |
pj_pool_t *pool; |
| 134 |
pj_mutex_t *mutex; |
| 135 |
pjmedia_codec codec_list; |
| 136 |
struct speex_param speex_param[3]; |
| 137 |
|
| 138 |
} spx_factory; |
| 139 |
|
| 140 |
|
| 141 |
struct spx_private |
| 142 |
{ |
| 143 |
int param_id; |
| 144 |
|
| 145 |
void *enc; |
| 146 |
SpeexBits enc_bits; |
| 147 |
void *dec; |
| 148 |
SpeexBits dec_bits; |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
static pj_status_t get_speex_info( struct speex_param *p ) |
| 156 |
{ |
| 157 |
void *state; |
| 158 |
int tmp; |
| 159 |
|
| 160 |
|
| 161 |
state = speex_encoder_init(p->mode); |
| 162 |
if (!state) |
| 163 |
return PJMEDIA_CODEC_EFAILED; |
| 164 |
|
| 165 |
|
| 166 |
if (p->quality != -1) |
| 167 |
speex_encoder_ctl(state, SPEEX_SET_QUALITY, &p->quality); |
| 168 |
|
| 169 |
|
| 170 |
speex_encoder_ctl(state, SPEEX_SET_SAMPLING_RATE, &p->clock_rate); |
| 171 |
|
| 172 |
|
| 173 |
tmp = 0; |
| 174 |
speex_encoder_ctl(state, SPEEX_SET_VAD, &tmp); |
| 175 |
|
| 176 |
|
| 177 |
if (p->complexity != -1) |
| 178 |
speex_encoder_ctl(state, SPEEX_SET_COMPLEXITY, &p->complexity); |
| 179 |
|
| 180 |
|
| 181 |
speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &p->samples_per_frame); |
| 182 |
|
| 183 |
|
| 184 |
speex_encoder_ctl(state, SPEEX_GET_BITRATE, &p->bitrate); |
| 185 |
|
| 186 |
|
| 187 |
p->framesize = p->bitrate * 20 / 1000; |
| 188 |
|
| 189 |
|
| 190 |
tmp = 10; |
| 191 |
speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp); |
| 192 |
speex_encoder_ctl(state, SPEEX_GET_BITRATE, &p->max_bitrate); |
| 193 |
|
| 194 |
|
| 195 |
speex_encoder_destroy(state); |
| 196 |
|
| 197 |
return PJ_SUCCESS; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
PJ_DEF(pj_status_t) pjmedia_codec_speex_init( pjmedia_endpt *endpt, |
| 204 |
unsigned options, |
| 205 |
int quality, |
| 206 |
int complexity ) |
| 207 |
{ |
| 208 |
pjmedia_codec_mgr *codec_mgr; |
| 209 |
unsigned i; |
| 210 |
pj_status_t status; |
| 211 |
|
| 212 |
if (spx_factory.pool != NULL) { |
| 213 |
|
| 214 |
return PJ_SUCCESS; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
if (quality < 0) quality = PJMEDIA_CODEC_SPEEX_DEFAULT_QUALITY; |
| 219 |
if (complexity < 0) complexity = PJMEDIA_CODEC_SPEEX_DEFAULT_COMPLEXITY; |
| 220 |
|
| 221 |
|
| 222 |
PJ_ASSERT_RETURN(quality >= 0 && quality <= 10, PJ_EINVAL); |
| 223 |
PJ_ASSERT_RETURN(complexity >= 1 && complexity <= 10, PJ_EINVAL); |
| 224 |
|
| 225 |
|
| 226 |
spx_factory.base.op = &spx_factory_op; |
| 227 |
spx_factory.base.factory_data = NULL; |
| 228 |
spx_factory.endpt = endpt; |
| 229 |
|
| 230 |
spx_factory.pool = pjmedia_endpt_create_pool(endpt, "speex", |
| 231 |
1000, 4000); |
| 232 |
if (!spx_factory.pool) |
| 233 |
return PJ_ENOMEM; |
| 234 |
|
| 235 |
pj_list_init(&spx_factory.codec_list); |
| 236 |
|
| 237 |
|
| 238 |
status = pj_mutex_create_simple(spx_factory.pool, "speex", |
| 239 |
&spx_factory.mutex); |
| 240 |
if (status != PJ_SUCCESS) |
| 241 |
goto on_error; |
| 242 |
|
| 243 |
|
| 244 |
spx_factory.speex_param[PARAM_NB].enabled = |
| 245 |
((options & PJMEDIA_SPEEX_NO_NB) == 0); |
| 246 |
spx_factory.speex_param[PARAM_NB].pt = PJMEDIA_RTP_PT_SPEEX_NB; |
| 247 |
spx_factory.speex_param[PARAM_NB].mode = speex_lib_get_mode(SPEEX_MODEID_NB); |
| 248 |
spx_factory.speex_param[PARAM_NB].clock_rate = 8000; |
| 249 |
spx_factory.speex_param[PARAM_NB].quality = quality; |
| 250 |
spx_factory.speex_param[PARAM_NB].complexity = complexity; |
| 251 |
|
| 252 |
spx_factory.speex_param[PARAM_WB].enabled = |
| 253 |
((options & PJMEDIA_SPEEX_NO_WB) == 0); |
| 254 |
spx_factory.speex_param[PARAM_WB].pt = PJMEDIA_RTP_PT_SPEEX_WB; |
| 255 |
spx_factory.speex_param[PARAM_WB].mode = speex_lib_get_mode(SPEEX_MODEID_WB); |
| 256 |
spx_factory.speex_param[PARAM_WB].clock_rate = 16000; |
| 257 |
spx_factory.speex_param[PARAM_WB].quality = quality; |
| 258 |
spx_factory.speex_param[PARAM_WB].complexity = complexity; |
| 259 |
|
| 260 |
spx_factory.speex_param[PARAM_UWB].enabled = |
| 261 |
((options & PJMEDIA_SPEEX_NO_UWB) == 0); |
| 262 |
spx_factory.speex_param[PARAM_UWB].pt = PJMEDIA_RTP_PT_SPEEX_UWB; |
| 263 |
spx_factory.speex_param[PARAM_UWB].mode = speex_lib_get_mode(SPEEX_MODEID_UWB); |
| 264 |
spx_factory.speex_param[PARAM_UWB].clock_rate = 32000; |
| 265 |
spx_factory.speex_param[PARAM_UWB].quality = quality; |
| 266 |
spx_factory.speex_param[PARAM_UWB].complexity = complexity; |
| 267 |
|
| 268 |
|
| 269 |
if (quality <= 4 && quality >= 0) { |
| 270 |
PJ_LOG(5,(THIS_FILE, "Adjusting quality to 5 for uwb")); |
| 271 |
spx_factory.speex_param[PARAM_UWB].quality = 5; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
for (i=0; i<PJ_ARRAY_SIZE(spx_factory.speex_param); ++i) { |
| 276 |
status = get_speex_info(&spx_factory.speex_param[i]); |
| 277 |
PJ_ASSERT_ON_FAIL(status == PJ_SUCCESS, goto on_error); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
codec_mgr = pjmedia_endpt_get_codec_mgr(endpt); |
| 282 |
if (!codec_mgr) { |
| 283 |
status = PJ_EINVALIDOP; |
| 284 |
goto on_error; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
status = pjmedia_codec_mgr_register_factory(codec_mgr, |
| 289 |
&spx_factory.base); |
| 290 |
if (status != PJ_SUCCESS) |
| 291 |
goto on_error; |
| 292 |
|
| 293 |
|
| 294 |
return PJ_SUCCESS; |
| 295 |
|
| 296 |
on_error: |
| 297 |
pj_pool_release(spx_factory.pool); |
| 298 |
spx_factory.pool = NULL; |
| 299 |
return status; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
PJ_DEF(pj_status_t) pjmedia_codec_speex_init_default(pjmedia_endpt *endpt) |
| 307 |
{ |
| 308 |
return pjmedia_codec_speex_init(endpt, 0, -1, -1); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
PJ_DEF(pj_status_t) pjmedia_codec_speex_set_param(unsigned clock_rate, |
| 315 |
int quality, |
| 316 |
int complexity) |
| 317 |
{ |
| 318 |
unsigned i; |
| 319 |
|
| 320 |
|
| 321 |
if (quality < 0) quality = PJMEDIA_CODEC_SPEEX_DEFAULT_QUALITY; |
| 322 |
if (complexity < 0) complexity = PJMEDIA_CODEC_SPEEX_DEFAULT_COMPLEXITY; |
| 323 |
|
| 324 |
|
| 325 |
PJ_ASSERT_RETURN(quality >= 0 && quality <= 10, PJ_EINVAL); |
| 326 |
PJ_ASSERT_RETURN(complexity >= 1 && complexity <= 10, PJ_EINVAL); |
| 327 |
|
| 328 |
|
| 329 |
for (i=0; i<PJ_ARRAY_SIZE(spx_factory.speex_param); ++i) { |
| 330 |
if (spx_factory.speex_param[i].clock_rate == clock_rate) { |
| 331 |
pj_status_t status; |
| 332 |
|
| 333 |
spx_factory.speex_param[i].quality = quality; |
| 334 |
spx_factory.speex_param[i].complexity = complexity; |
| 335 |
|
| 336 |
|
| 337 |
if (i == PARAM_UWB && quality <= 4 && quality >= 0) { |
| 338 |
PJ_LOG(5,(THIS_FILE, "Adjusting quality to 5 for uwb")); |
| 339 |
spx_factory.speex_param[PARAM_UWB].quality = 5; |
| 340 |
} |
| 341 |
|
| 342 |
status = get_speex_info(&spx_factory.speex_param[i]); |
| 343 |
|
| 344 |
return status; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
return PJ_EINVAL; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
PJ_DEF(pj_status_t) pjmedia_codec_speex_deinit(void) |
| 356 |
{ |
| 357 |
pjmedia_codec_mgr *codec_mgr; |
| 358 |
pj_status_t status; |
| 359 |
|
| 360 |
if (spx_factory.pool == NULL) { |
| 361 |
|
| 362 |
return PJ_SUCCESS; |
| 363 |
} |
| 364 |
|
| 365 |
pj_mutex_lock(spx_factory.mutex); |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
codec_mgr = pjmedia_endpt_get_codec_mgr(spx_factory.endpt); |
| 379 |
if (!codec_mgr) { |
| 380 |
pj_pool_release(spx_factory.pool); |
| 381 |
spx_factory.pool = NULL; |
| 382 |
pj_mutex_unlock(spx_factory.mutex); |
| 383 |
return PJ_EINVALIDOP; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
pj_mutex_unlock(spx_factory.mutex); |
| 390 |
|
| 391 |
|
| 392 |
status = pjmedia_codec_mgr_unregister_factory(codec_mgr, |
| 393 |
&spx_factory.base); |
| 394 |
|
| 395 |
|
| 396 |
pj_mutex_destroy(spx_factory.mutex); |
| 397 |
spx_factory.mutex = NULL; |
| 398 |
|
| 399 |
|
| 400 |
pj_pool_release(spx_factory.pool); |
| 401 |
spx_factory.pool = NULL; |
| 402 |
|
| 403 |
return status; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
static pj_status_t spx_test_alloc( pjmedia_codec_factory *factory, |
| 410 |
const pjmedia_codec_info *info ) |
| 411 |
{ |
| 412 |
const pj_str_t speex_tag = { "speex", 5}; |
| 413 |
unsigned i; |
| 414 |
|
| 415 |
PJ_UNUSED_ARG(factory); |
| 416 |
|
| 417 |
|
| 418 |
if (info->type != PJMEDIA_TYPE_AUDIO) |
| 419 |
return PJMEDIA_CODEC_EUNSUP; |
| 420 |
|
| 421 |
|
| 422 |
if (pj_stricmp(&info->encoding_name, &speex_tag) != 0) |
| 423 |
return PJMEDIA_CODEC_EUNSUP; |
| 424 |
|
| 425 |
|
| 426 |
for (i=0; i<PJ_ARRAY_SIZE(spx_factory.speex_param); ++i) { |
| 427 |
if (info->clock_rate == spx_factory.speex_param[i].clock_rate) { |
| 428 |
|
| 429 |
return PJ_SUCCESS; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
return PJMEDIA_CODEC_EUNSUP; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
static pj_status_t spx_default_attr (pjmedia_codec_factory *factory, |
| 442 |
const pjmedia_codec_info *id, |
| 443 |
pjmedia_codec_param *attr ) |
| 444 |
{ |
| 445 |
|
| 446 |
PJ_ASSERT_RETURN(factory==&spx_factory.base, PJ_EINVAL); |
| 447 |
|
| 448 |
pj_bzero(attr, sizeof(pjmedia_codec_param)); |
| 449 |
attr->info.pt = (pj_uint8_t)id->pt; |
| 450 |
attr->info.channel_cnt = 1; |
| 451 |
|
| 452 |
if (id->clock_rate <= 8000) { |
| 453 |
attr->info.clock_rate = spx_factory.speex_param[PARAM_NB].clock_rate; |
| 454 |
attr->info.avg_bps = spx_factory.speex_param[PARAM_NB].bitrate; |
| 455 |
attr->info.max_bps = spx_factory.speex_param[PARAM_NB].max_bitrate; |
| 456 |
|
| 457 |
} else if (id->clock_rate <= 16000) { |
| 458 |
attr->info.clock_rate = spx_factory.speex_param[PARAM_WB].clock_rate; |
| 459 |
attr->info.avg_bps = spx_factory.speex_param[PARAM_WB].bitrate; |
| 460 |
attr->info.max_bps = spx_factory.speex_param[PARAM_WB].max_bitrate; |
| 461 |
|
| 462 |
} else { |
| 463 |
|
| 464 |
attr->info.clock_rate = spx_factory.speex_param[PARAM_UWB].clock_rate; |
| 465 |
attr->info.avg_bps = spx_factory.speex_param[PARAM_UWB].bitrate; |
| 466 |
attr->info.max_bps = spx_factory.speex_param[PARAM_UWB].max_bitrate; |
| 467 |
} |
| 468 |
|
| 469 |
attr->info.pcm_bits_per_sample = 16; |
| 470 |
attr->info.frm_ptime = 20; |
| 471 |
attr->info.pt = (pj_uint8_t)id->pt; |
| 472 |
|
| 473 |
attr->setting.frm_per_pkt = 1; |
| 474 |
|
| 475 |
|
| 476 |
attr->setting.cng = 1; |
| 477 |
attr->setting.plc = 1; |
| 478 |
attr->setting.penh =1 ; |
| 479 |
attr->setting.vad = 1; |
| 480 |
|
| 481 |
return PJ_SUCCESS; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
static pj_status_t spx_enum_codecs(pjmedia_codec_factory *factory, |
| 488 |
unsigned *count, |
| 489 |
pjmedia_codec_info codecs[]) |
| 490 |
{ |
| 491 |
unsigned max; |
| 492 |
int i; |
| 493 |
|
| 494 |
PJ_UNUSED_ARG(factory); |
| 495 |
PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL); |
| 496 |
|
| 497 |
max = *count; |
| 498 |
*count = 0; |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
for (i=PJ_ARRAY_SIZE(spx_factory.speex_param)-1; i>=0 && *count<max; --i) { |
| 505 |
|
| 506 |
if (!spx_factory.speex_param[i].enabled) |
| 507 |
continue; |
| 508 |
|
| 509 |
pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info)); |
| 510 |
codecs[*count].encoding_name = pj_str("speex"); |
| 511 |
codecs[*count].pt = spx_factory.speex_param[i].pt; |
| 512 |
codecs[*count].type = PJMEDIA_TYPE_AUDIO; |
| 513 |
codecs[*count].clock_rate = spx_factory.speex_param[i].clock_rate; |
| 514 |
codecs[*count].channel_cnt = 1; |
| 515 |
|
| 516 |
++*count; |
| 517 |
} |
| 518 |
|
| 519 |
return PJ_SUCCESS; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
static pj_status_t spx_alloc_codec( pjmedia_codec_factory *factory, |
| 526 |
const pjmedia_codec_info *id, |
| 527 |
pjmedia_codec **p_codec) |
| 528 |
{ |
| 529 |
pjmedia_codec *codec; |
| 530 |
struct spx_private *spx; |
| 531 |
|
| 532 |
PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL); |
| 533 |
PJ_ASSERT_RETURN(factory == &spx_factory.base, PJ_EINVAL); |
| 534 |
|
| 535 |
|
| 536 |
pj_mutex_lock(spx_factory.mutex); |
| 537 |
|
| 538 |
|
| 539 |
if (!pj_list_empty(&spx_factory.codec_list)) { |
| 540 |
codec = spx_factory.codec_list.next; |
| 541 |
pj_list_erase(codec); |
| 542 |
} else { |
| 543 |
codec = PJ_POOL_ZALLOC_T(spx_factory.pool, pjmedia_codec); |
| 544 |
PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM); |
| 545 |
codec->op = &spx_op; |
| 546 |
codec->factory = factory; |
| 547 |
codec->codec_data = pj_pool_alloc(spx_factory.pool, |
| 548 |
sizeof(struct spx_private)); |
| 549 |
} |
| 550 |
|
| 551 |
pj_mutex_unlock(spx_factory.mutex); |
| 552 |
|
| 553 |
spx = (struct spx_private*) codec->codec_data; |
| 554 |
spx->enc = NULL; |
| 555 |
spx->dec = NULL; |
| 556 |
|
| 557 |
if (id->clock_rate <= 8000) |
| 558 |
spx->param_id = PARAM_NB; |
| 559 |
else if (id->clock_rate <= 16000) |
| 560 |
spx->param_id = PARAM_WB; |
| 561 |
else |
| 562 |
spx->param_id = PARAM_UWB; |
| 563 |
|
| 564 |
*p_codec = codec; |
| 565 |
return PJ_SUCCESS; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
static pj_status_t spx_dealloc_codec( pjmedia_codec_factory *factory, |
| 572 |
pjmedia_codec *codec ) |
| 573 |
{ |
| 574 |
struct spx_private *spx; |
| 575 |
|
| 576 |
PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL); |
| 577 |
PJ_ASSERT_RETURN(factory == &spx_factory.base, PJ_EINVAL); |
| 578 |
|
| 579 |
|
| 580 |
spx = (struct spx_private*) codec->codec_data; |
| 581 |
if (spx->enc != NULL || spx->dec != NULL) { |
| 582 |
spx_codec_close(codec); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
pj_mutex_lock(spx_factory.mutex); |
| 587 |
pj_list_push_front(&spx_factory.codec_list, codec); |
| 588 |
pj_mutex_unlock(spx_factory.mutex); |
| 589 |
|
| 590 |
return PJ_SUCCESS; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
static pj_status_t spx_codec_init( pjmedia_codec *codec, |
| 597 |
pj_pool_t *pool ) |
| 598 |
{ |
| 599 |
PJ_UNUSED_ARG(codec); |
| 600 |
PJ_UNUSED_ARG(pool); |
| 601 |
return PJ_SUCCESS; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
static pj_status_t spx_codec_open( pjmedia_codec *codec, |
| 608 |
pjmedia_codec_param *attr ) |
| 609 |
{ |
| 610 |
struct spx_private *spx; |
| 611 |
int id, tmp; |
| 612 |
|
| 613 |
spx = (struct spx_private*) codec->codec_data; |
| 614 |
id = spx->param_id; |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
spx->enc = speex_encoder_init(spx_factory.speex_param[id].mode); |
| 620 |
if (!spx->enc) |
| 621 |
return PJMEDIA_CODEC_EFAILED; |
| 622 |
speex_bits_init(&spx->enc_bits); |
| 623 |
|
| 624 |
|
| 625 |
if (spx_factory.speex_param[id].quality != -1) { |
| 626 |
speex_encoder_ctl(spx->enc, SPEEX_SET_QUALITY, |
| 627 |
&spx_factory.speex_param[id].quality); |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
tmp = attr->info.clock_rate; |
| 632 |
speex_encoder_ctl(spx->enc, SPEEX_SET_SAMPLING_RATE, |
| 633 |
&spx_factory.speex_param[id].clock_rate); |
| 634 |
|
| 635 |
|
| 636 |
tmp = (attr->setting.vad != 0); |
| 637 |
speex_encoder_ctl(spx->enc, SPEEX_SET_VAD, &tmp); |
| 638 |
speex_encoder_ctl(spx->enc, SPEEX_SET_DTX, &tmp); |
| 639 |
|
| 640 |
|
| 641 |
if (spx_factory.speex_param[id].complexity != -1) { |
| 642 |
speex_encoder_ctl(spx->enc, SPEEX_SET_COMPLEXITY, |
| 643 |
&spx_factory.speex_param[id].complexity); |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
spx->dec = speex_decoder_init(spx_factory.speex_param[id].mode); |
| 650 |
if (!spx->dec) { |
| 651 |
spx_codec_close(codec); |
| 652 |
return PJMEDIA_CODEC_EFAILED; |
| 653 |
} |
| 654 |
speex_bits_init(&spx->dec_bits); |
| 655 |
|
| 656 |
|
| 657 |
speex_decoder_ctl(spx->dec, SPEEX_SET_SAMPLING_RATE, |
| 658 |
&spx_factory.speex_param[id].clock_rate); |
| 659 |
|
| 660 |
|
| 661 |
tmp = attr->setting.penh; |
| 662 |
speex_decoder_ctl(spx->dec, SPEEX_SET_ENH, &tmp); |
| 663 |
|
| 664 |
return PJ_SUCCESS; |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
static pj_status_t spx_codec_close( pjmedia_codec *codec ) |
| 671 |
{ |
| 672 |
struct spx_private *spx; |
| 673 |
|
| 674 |
spx = (struct spx_private*) codec->codec_data; |
| 675 |
|
| 676 |
|
| 677 |
if (spx->enc) { |
| 678 |
speex_encoder_destroy( spx->enc ); |
| 679 |
spx->enc = NULL; |
| 680 |
speex_bits_destroy( &spx->enc_bits ); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
if (spx->dec) { |
| 685 |
speex_decoder_destroy( spx->dec); |
| 686 |
spx->dec = NULL; |
| 687 |
speex_bits_destroy( &spx->dec_bits ); |
| 688 |
} |
| 689 |
|
| 690 |
return PJ_SUCCESS; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
static pj_status_t spx_codec_modify(pjmedia_codec *codec, |
| 698 |
const pjmedia_codec_param *attr ) |
| 699 |
{ |
| 700 |
struct spx_private *spx; |
| 701 |
int tmp; |
| 702 |
|
| 703 |
spx = (struct spx_private*) codec->codec_data; |
| 704 |
|
| 705 |
|
| 706 |
tmp = (attr->setting.vad != 0); |
| 707 |
speex_encoder_ctl(spx->enc, SPEEX_SET_VAD, &tmp); |
| 708 |
speex_encoder_ctl(spx->enc, SPEEX_SET_DTX, &tmp); |
| 709 |
|
| 710 |
|
| 711 |
tmp = attr->setting.penh; |
| 712 |
speex_decoder_ctl(spx->dec, SPEEX_SET_ENH, &tmp); |
| 713 |
|
| 714 |
return PJ_SUCCESS; |
| 715 |
} |
| 716 |
|
| 717 |
#if 0 |
| 718 |
# define TRACE__(args) PJ_LOG(5,args) |
| 719 |
#else |
| 720 |
# define TRACE__(args) |
| 721 |
#endif |
| 722 |
|
| 723 |
#undef THIS_FUNC |
| 724 |
#define THIS_FUNC "speex_get_next_frame" |
| 725 |
|
| 726 |
#define NB_SUBMODES 16 |
| 727 |
#define NB_SUBMODE_BITS 4 |
| 728 |
|
| 729 |
#define SB_SUBMODES 8 |
| 730 |
#define SB_SUBMODE_BITS 3 |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
static int speex_get_next_frame(SpeexBits *bits) |
| 736 |
{ |
| 737 |
static const int inband_skip_table[NB_SUBMODES] = |
| 738 |
{1, 1, 4, 4, 4, 4, 4, 4, 8, 8, 16, 16, 32, 32, 64, 64 }; |
| 739 |
static const int wb_skip_table[SB_SUBMODES] = |
| 740 |
{SB_SUBMODE_BITS+1, 36, 112, 192, 352, -1, -1, -1}; |
| 741 |
|
| 742 |
unsigned submode; |
| 743 |
unsigned nb_count = 0; |
| 744 |
|
| 745 |
while (speex_bits_remaining(bits) >= 5) { |
| 746 |
unsigned wb_count = 0; |
| 747 |
unsigned bit_ptr = bits->bitPtr; |
| 748 |
unsigned char_ptr = bits->charPtr; |
| 749 |
|
| 750 |
|
| 751 |
while ((speex_bits_remaining(bits) >= 4) |
| 752 |
&& speex_bits_unpack_unsigned(bits, 1)) |
| 753 |
{ |
| 754 |
int advance; |
| 755 |
|
| 756 |
submode = speex_bits_unpack_unsigned(bits, 3); |
| 757 |
advance = wb_skip_table[submode]; |
| 758 |
if (advance < 0) { |
| 759 |
TRACE__((THIS_FUNC, "Invalid mode encountered. " |
| 760 |
"The stream is corrupted.")); |
| 761 |
return -1; |
| 762 |
} |
| 763 |
TRACE__((THIS_FUNC, "WB layer skipped: %d bits", advance)); |
| 764 |
advance -= (SB_SUBMODE_BITS+1); |
| 765 |
speex_bits_advance(bits, advance); |
| 766 |
|
| 767 |
bit_ptr = bits->bitPtr; |
| 768 |
char_ptr = bits->charPtr; |
| 769 |
|
| 770 |
|
| 771 |
if (++wb_count > 2) |
| 772 |
return -1; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
if (speex_bits_remaining(bits) < 4) { |
| 777 |
TRACE__((THIS_FUNC, "End of stream")); |
| 778 |
return 0; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
if (nb_count > 0) { |
| 783 |
bits->bitPtr = bit_ptr; |
| 784 |
bits->charPtr = char_ptr; |
| 785 |
return 0; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
submode = speex_bits_unpack_unsigned(bits, 4); |
| 790 |
TRACE__((THIS_FUNC, "Control bits: %d at %d", |
| 791 |
submode, bits->charPtr*8+bits->bitPtr)); |
| 792 |
|
| 793 |
if (submode == 15) { |
| 794 |
TRACE__((THIS_FUNC, "Found submode: terminator")); |
| 795 |
return -1; |
| 796 |
} else if (submode == 14) { |
| 797 |
|
| 798 |
submode = speex_bits_unpack_unsigned(bits, 4); |
| 799 |
TRACE__((THIS_FUNC, "Found submode: in-band %d bits", |
| 800 |
inband_skip_table[submode])); |
| 801 |
speex_bits_advance(bits, inband_skip_table[submode]); |
| 802 |
} else if (submode == 13) { |
| 803 |
|
| 804 |
submode = speex_bits_unpack_unsigned(bits, 5); |
| 805 |
TRACE__((THIS_FUNC, "Found submode: user-band %d bytes", submode)); |
| 806 |
speex_bits_advance(bits, submode * 8); |
| 807 |
} else if (submode > 8) { |
| 808 |
TRACE__((THIS_FUNC, "Unknown sub-mode %d", submode)); |
| 809 |
return -1; |
| 810 |
} else { |
| 811 |
|
| 812 |
int advance = submode; |
| 813 |
speex_mode_query(&speex_nb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance); |
| 814 |
if (advance < 0) { |
| 815 |
TRACE__((THIS_FUNC, "Invalid mode encountered. " |
| 816 |
"The stream is corrupted.")); |
| 817 |
return -1; |
| 818 |
} |
| 819 |
TRACE__((THIS_FUNC, "Submode %d: %d bits", submode, advance)); |
| 820 |
advance -= (NB_SUBMODE_BITS+1); |
| 821 |
speex_bits_advance(bits, advance); |
| 822 |
|
| 823 |
++nb_count; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
return 0; |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
static pj_status_t spx_codec_parse( pjmedia_codec *codec, |
| 835 |
void *pkt, |
| 836 |
pj_size_t pkt_size, |
| 837 |
const pj_timestamp *ts, |
| 838 |
unsigned *frame_cnt, |
| 839 |
pjmedia_frame frames[]) |
| 840 |
{ |
| 841 |
struct spx_private *spx = (struct spx_private*) codec->codec_data; |
| 842 |
unsigned samples_per_frame; |
| 843 |
unsigned count = 0; |
| 844 |
int char_ptr = 0; |
| 845 |
int bit_ptr = 0; |
| 846 |
|
| 847 |
PJ_ASSERT_RETURN(codec && ts && frames && frame_cnt, PJ_EINVAL); |
| 848 |
|
| 849 |
samples_per_frame=spx_factory.speex_param[spx->param_id].samples_per_frame; |
| 850 |
|
| 851 |
|
| 852 |
speex_bits_read_from(&spx->dec_bits, (char*)pkt, (int)pkt_size); |
| 853 |
|
| 854 |
while (speex_get_next_frame(&spx->dec_bits) == 0 && |
| 855 |
spx->dec_bits.charPtr != char_ptr && |
| 856 |
count < *frame_cnt) |
| 857 |
{ |
| 858 |
frames[count].buf = (char*)pkt + char_ptr; |
| 859 |
|
| 860 |
frames[count].bit_info = bit_ptr; |
| 861 |
frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 862 |
frames[count].timestamp.u64 = ts->u64 + |
| 863 |
(pj_uint64_t)count * samples_per_frame; |
| 864 |
frames[count].size = spx->dec_bits.charPtr - char_ptr; |
| 865 |
if (spx->dec_bits.bitPtr) |
| 866 |
++frames[count].size; |
| 867 |
|
| 868 |
bit_ptr = spx->dec_bits.bitPtr; |
| 869 |
char_ptr = spx->dec_bits.charPtr; |
| 870 |
|
| 871 |
++count; |
| 872 |
} |
| 873 |
|
| 874 |
*frame_cnt = count; |
| 875 |
|
| 876 |
return PJ_SUCCESS; |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
static pj_status_t spx_codec_encode( pjmedia_codec *codec, |
| 883 |
const struct pjmedia_frame *input, |
| 884 |
unsigned output_buf_len, |
| 885 |
struct pjmedia_frame *output) |
| 886 |
{ |
| 887 |
struct spx_private *spx; |
| 888 |
unsigned samples_per_frame; |
| 889 |
int tx = 0; |
| 890 |
spx_int16_t *pcm_in = (spx_int16_t*)input->buf; |
| 891 |
pj_size_t nsamples; |
| 892 |
|
| 893 |
spx = (struct spx_private*) codec->codec_data; |
| 894 |
|
| 895 |
if (input->type != PJMEDIA_FRAME_TYPE_AUDIO) { |
| 896 |
output->size = 0; |
| 897 |
output->buf = NULL; |
| 898 |
output->timestamp = input->timestamp; |
| 899 |
output->type = input->type; |
| 900 |
return PJ_SUCCESS; |
| 901 |
} |
| 902 |
|
| 903 |
nsamples = input->size >> 1; |
| 904 |
samples_per_frame=spx_factory.speex_param[spx->param_id].samples_per_frame; |
| 905 |
|
| 906 |
PJ_ASSERT_RETURN(nsamples % samples_per_frame == 0, |
| 907 |
PJMEDIA_CODEC_EPCMFRMINLEN); |
| 908 |
|
| 909 |
|
| 910 |
speex_bits_reset(&spx->enc_bits); |
| 911 |
|
| 912 |
|
| 913 |
while (nsamples >= samples_per_frame) { |
| 914 |
tx += speex_encode_int(spx->enc, pcm_in, &spx->enc_bits); |
| 915 |
pcm_in += samples_per_frame; |
| 916 |
nsamples -= samples_per_frame; |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
if (tx == 0) { |
| 921 |
output->buf = NULL; |
| 922 |
output->size = 0; |
| 923 |
output->timestamp.u64 = input->timestamp.u64; |
| 924 |
output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 925 |
return PJ_SUCCESS; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
pj_assert(speex_bits_nbytes(&spx->enc_bits) <= (int)output_buf_len); |
| 930 |
|
| 931 |
|
| 932 |
output->size = speex_bits_write(&spx->enc_bits, |
| 933 |
(char*)output->buf, output_buf_len); |
| 934 |
output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 935 |
output->timestamp = input->timestamp; |
| 936 |
|
| 937 |
return PJ_SUCCESS; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
static pj_status_t spx_codec_decode( pjmedia_codec *codec, |
| 944 |
const struct pjmedia_frame *input, |
| 945 |
unsigned output_buf_len, |
| 946 |
struct pjmedia_frame *output) |
| 947 |
{ |
| 948 |
struct spx_private *spx; |
| 949 |
unsigned samples_per_frame; |
| 950 |
|
| 951 |
spx = (struct spx_private*) codec->codec_data; |
| 952 |
samples_per_frame=spx_factory.speex_param[spx->param_id].samples_per_frame; |
| 953 |
|
| 954 |
PJ_ASSERT_RETURN(output_buf_len >= samples_per_frame << 1, |
| 955 |
PJMEDIA_CODEC_EPCMTOOSHORT); |
| 956 |
|
| 957 |
if (input->type != PJMEDIA_FRAME_TYPE_AUDIO) { |
| 958 |
pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame); |
| 959 |
output->size = samples_per_frame << 1; |
| 960 |
output->timestamp.u64 = input->timestamp.u64; |
| 961 |
output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 962 |
return PJ_SUCCESS; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
speex_bits_read_from(&spx->dec_bits, (char*)input->buf, (int)input->size); |
| 967 |
|
| 968 |
|
| 969 |
speex_bits_advance(&spx->dec_bits, input->bit_info); |
| 970 |
|
| 971 |
|
| 972 |
speex_decode_int(spx->dec, &spx->dec_bits, (spx_int16_t*)output->buf); |
| 973 |
|
| 974 |
output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 975 |
output->size = samples_per_frame << 1; |
| 976 |
output->timestamp.u64 = input->timestamp.u64; |
| 977 |
|
| 978 |
return PJ_SUCCESS; |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
static pj_status_t spx_codec_recover(pjmedia_codec *codec, |
| 985 |
unsigned output_buf_len, |
| 986 |
struct pjmedia_frame *output) |
| 987 |
{ |
| 988 |
struct spx_private *spx; |
| 989 |
unsigned count; |
| 990 |
|
| 991 |
|
| 992 |
PJ_UNUSED_ARG(output_buf_len); |
| 993 |
|
| 994 |
spx = (struct spx_private*) codec->codec_data; |
| 995 |
|
| 996 |
count = spx_factory.speex_param[spx->param_id].clock_rate * 20 / 1000; |
| 997 |
pj_assert(count <= output_buf_len/2); |
| 998 |
|
| 999 |
|
| 1000 |
speex_decode_int(spx->dec, NULL, (spx_int16_t*) output->buf); |
| 1001 |
|
| 1002 |
output->size = count * 2; |
| 1003 |
|
| 1004 |
return PJ_SUCCESS; |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
#endif |