Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | /* This file contains file from Sun Microsystems, Inc, with the complete |
| 21 | * notice in the second half of this file. |
| 22 | */ |
| 23 | #include <pjmedia/g711.h> |
| 24 | #include <pjmedia/codec.h> |
| 25 | #include <pjmedia/alaw_ulaw.h> |
| 26 | #include <pjmedia/endpoint.h> |
| 27 | #include <pjmedia/errno.h> |
| 28 | #include <pjmedia/port.h> |
| 29 | #include <pjmedia/plc.h> |
| 30 | #include <pjmedia/silencedet.h> |
| 31 | #include <pj/pool.h> |
| 32 | #include <pj/string.h> |
| 33 | #include <pj/assert.h> |
| 34 | |
| 35 | #if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0 |
| 36 | |
| 37 | /* We removed PLC in 0.6 (and re-enabled it again in 0.9!) */ |
| 38 | #define PLC_DISABLED 0 |
| 39 | |
| 40 | |
| 41 | #define G711_BPS 64000 |
| 42 | #define G711_CODEC_CNT 0 /* number of codec to preallocate in memory */ |
| 43 | #define PTIME 10 /* basic frame size is 10 msec */ |
| 44 | #define FRAME_SIZE (8000 * PTIME / 1000) /* 80 bytes */ |
| 45 | #define SAMPLES_PER_FRAME (8000 * PTIME / 1000) /* 80 samples */ |
| 46 | |
| 47 | /* Prototypes for G711 factory */ |
| 48 | static pj_status_t g711_test_alloc( pjmedia_codec_factory *factory, |
| 49 | const pjmedia_codec_info *id ); |
| 50 | static pj_status_t g711_default_attr( pjmedia_codec_factory *factory, |
| 51 | const pjmedia_codec_info *id, |
| 52 | pjmedia_codec_param *attr ); |
| 53 | static pj_status_t g711_enum_codecs (pjmedia_codec_factory *factory, |
| 54 | unsigned *count, |
| 55 | pjmedia_codec_info codecs[]); |
| 56 | static pj_status_t g711_alloc_codec( pjmedia_codec_factory *factory, |
| 57 | const pjmedia_codec_info *id, |
| 58 | pjmedia_codec **p_codec); |
| 59 | static pj_status_t g711_dealloc_codec( pjmedia_codec_factory *factory, |
| 60 | pjmedia_codec *codec ); |
| 61 | |
| 62 | /* Prototypes for G711 implementation. */ |
| 63 | static pj_status_t g711_init( pjmedia_codec *codec, |
| 64 | pj_pool_t *pool ); |
| 65 | static pj_status_t g711_open( pjmedia_codec *codec, |
| 66 | pjmedia_codec_param *attr ); |
| 67 | static pj_status_t g711_close( pjmedia_codec *codec ); |
| 68 | static pj_status_t g711_modify(pjmedia_codec *codec, |
| 69 | const pjmedia_codec_param *attr ); |
| 70 | static pj_status_t g711_parse(pjmedia_codec *codec, |
| 71 | void *pkt, |
| 72 | pj_size_t pkt_size, |
| 73 | const pj_timestamp *timestamp, |
| 74 | unsigned *frame_cnt, |
| 75 | pjmedia_frame frames[]); |
| 76 | static pj_status_t g711_encode( pjmedia_codec *codec, |
| 77 | const struct pjmedia_frame *input, |
| 78 | unsigned output_buf_len, |
| 79 | struct pjmedia_frame *output); |
| 80 | static pj_status_t g711_decode( pjmedia_codec *codec, |
| 81 | const struct pjmedia_frame *input, |
| 82 | unsigned output_buf_len, |
| 83 | struct pjmedia_frame *output); |
| 84 | #if !PLC_DISABLED |
| 85 | static pj_status_t g711_recover( pjmedia_codec *codec, |
| 86 | unsigned output_buf_len, |
| 87 | struct pjmedia_frame *output); |
| 88 | #endif |
| 89 | |
| 90 | /* Definition for G711 codec operations. */ |
| 91 | static pjmedia_codec_op g711_op = |
| 92 | { |
| 93 | &g711_init, |
| 94 | &g711_open, |
| 95 | &g711_close, |
| 96 | &g711_modify, |
| 97 | &g711_parse, |
| 98 | &g711_encode, |
| 99 | &g711_decode, |
| 100 | #if !PLC_DISABLED |
| 101 | &g711_recover |
| 102 | #else |
| 103 | NULL |
| 104 | #endif |
| 105 | }; |
| 106 | |
| 107 | /* Definition for G711 codec factory operations. */ |
| 108 | static pjmedia_codec_factory_op g711_factory_op = |
| 109 | { |
| 110 | &g711_test_alloc, |
| 111 | &g711_default_attr, |
| 112 | &g711_enum_codecs, |
| 113 | &g711_alloc_codec, |
| 114 | &g711_dealloc_codec, |
| 115 | &pjmedia_codec_g711_deinit |
| 116 | }; |
| 117 | |
| 118 | /* G711 factory private data */ |
| 119 | static struct g711_factory |
| 120 | { |
| 121 | pjmedia_codec_factory base; |
| 122 | pjmedia_endpt *endpt; |
| 123 | pj_pool_t *pool; |
| 124 | pj_mutex_t *mutex; |
| 125 | pjmedia_codec codec_list; |
| 126 | } g711_factory; |
| 127 | |
| 128 | /* G711 codec private data. */ |
| 129 | struct g711_private |
| 130 | { |
| 131 | unsigned pt; |
| 132 | #if !PLC_DISABLED |
| 133 | pj_bool_t plc_enabled; |
| 134 | pjmedia_plc *plc; |
| 135 | #endif |
| 136 | pj_bool_t vad_enabled; |
| 137 | pjmedia_silence_det *vad; |
| 138 | pj_timestamp last_tx; |
| 139 | }; |
| 140 | |
| 141 | |
| 142 | PJ_DEF(pj_status_t) pjmedia_codec_g711_init(pjmedia_endpt *endpt) |
| 143 | { |
| 144 | pjmedia_codec_mgr *codec_mgr; |
| 145 | pj_status_t status; |
| 146 | |
| 147 | if (g711_factory.endpt != NULL) { |
| 148 | /* Already initialized. */ |
| 149 | return PJ_SUCCESS; |
| 150 | } |
| 151 | |
| 152 | /* Init factory */ |
| 153 | g711_factory.base.op = &g711_factory_op; |
| 154 | g711_factory.base.factory_data = NULL; |
| 155 | g711_factory.endpt = endpt; |
| 156 | |
| 157 | pj_list_init(&g711_factory.codec_list); |
| 158 | |
| 159 | /* Create pool */ |
| 160 | g711_factory.pool = pjmedia_endpt_create_pool(endpt, "g711", 4000, 4000); |
| 161 | if (!g711_factory.pool) |
| 162 | return PJ_ENOMEM; |
| 163 | |
| 164 | /* Create mutex. */ |
| 165 | status = pj_mutex_create_simple(g711_factory.pool, "g611", |
| 166 | &g711_factory.mutex); |
| 167 | if (status != PJ_SUCCESS) |
| 168 | goto on_error; |
| 169 | |
| 170 | /* Get the codec manager. */ |
| 171 | codec_mgr = pjmedia_endpt_get_codec_mgr(endpt); |
| 172 | if (!codec_mgr) { |
| 173 | return PJ_EINVALIDOP; |
| 174 | } |
| 175 | |
| 176 | /* Register codec factory to endpoint. */ |
| 177 | status = pjmedia_codec_mgr_register_factory(codec_mgr, |
| 178 | &g711_factory.base); |
| 179 | if (status != PJ_SUCCESS) |
| 180 | return status; |
| 181 | |
| 182 | |
| 183 | return PJ_SUCCESS; |
| 184 | |
| 185 | on_error: |
| 186 | if (g711_factory.mutex) { |
| 187 | pj_mutex_destroy(g711_factory.mutex); |
| 188 | g711_factory.mutex = NULL; |
| 189 | } |
| 190 | if (g711_factory.pool) { |
| 191 | pj_pool_release(g711_factory.pool); |
| 192 | g711_factory.pool = NULL; |
| 193 | } |
| 194 | return status; |
| 195 | } |
| 196 | |
| 197 | PJ_DEF(pj_status_t) pjmedia_codec_g711_deinit(void) |
| 198 | { |
| 199 | pjmedia_codec_mgr *codec_mgr; |
| 200 | pj_status_t status; |
| 201 | |
| 202 | if (g711_factory.endpt == NULL) { |
| 203 | /* Not registered. */ |
| 204 | return PJ_SUCCESS; |
| 205 | } |
| 206 | |
| 207 | /* Lock mutex. */ |
| 208 | pj_mutex_lock(g711_factory.mutex); |
| 209 | |
| 210 | /* Get the codec manager. */ |
| 211 | codec_mgr = pjmedia_endpt_get_codec_mgr(g711_factory.endpt); |
| 212 | if (!codec_mgr) { |
| 213 | g711_factory.endpt = NULL; |
| 214 | pj_mutex_unlock(g711_factory.mutex); |
| 215 | return PJ_EINVALIDOP; |
| 216 | } |
| 217 | |
| 218 | /* Unregister G711 codec factory. */ |
| 219 | status = pjmedia_codec_mgr_unregister_factory(codec_mgr, |
| 220 | &g711_factory.base); |
| 221 | g711_factory.endpt = NULL; |
| 222 | |
| 223 | /* Destroy mutex. */ |
| 224 | pj_mutex_destroy(g711_factory.mutex); |
| 225 | g711_factory.mutex = NULL; |
| 226 | |
| 227 | |
| 228 | /* Release pool. */ |
| 229 | pj_pool_release(g711_factory.pool); |
| 230 | g711_factory.pool = NULL; |
| 231 | |
| 232 | |
| 233 | return status; |
| 234 | } |
| 235 | |
| 236 | static pj_status_t g711_test_alloc(pjmedia_codec_factory *factory, |
| 237 | const pjmedia_codec_info *id ) |
| 238 | { |
| 239 | PJ_UNUSED_ARG(factory); |
| 240 | |
| 241 | /* It's sufficient to check payload type only. */ |
| 242 | return (id->pt==PJMEDIA_RTP_PT_PCMU || id->pt==PJMEDIA_RTP_PT_PCMA)? 0:-1; |
| 243 | } |
| 244 | |
| 245 | static pj_status_t g711_default_attr (pjmedia_codec_factory *factory, |
| 246 | const pjmedia_codec_info *id, |
| 247 | pjmedia_codec_param *attr ) |
| 248 | { |
| 249 | PJ_UNUSED_ARG(factory); |
| 250 | |
| 251 | pj_bzero(attr, sizeof(pjmedia_codec_param)); |
| 252 | attr->info.clock_rate = 8000; |
| 253 | attr->info.channel_cnt = 1; |
| 254 | attr->info.avg_bps = G711_BPS; |
| 255 | attr->info.max_bps = G711_BPS; |
| 256 | attr->info.pcm_bits_per_sample = 16; |
| 257 | attr->info.frm_ptime = PTIME; |
| 258 | attr->info.pt = (pj_uint8_t)id->pt; |
| 259 | |
| 260 | /* Set default frames per packet to 2 (or 20ms) */ |
| 261 | attr->setting.frm_per_pkt = 2; |
| 262 | |
| 263 | #if !PLC_DISABLED |
| 264 | /* Enable plc by default. */ |
| 265 | attr->setting.plc = 1; |
| 266 | #endif |
| 267 | |
| 268 | /* Enable VAD by default. */ |
| 269 | attr->setting.vad = 1; |
| 270 | |
| 271 | /* Default all other flag bits disabled. */ |
| 272 | |
| 273 | return PJ_SUCCESS; |
| 274 | } |
| 275 | |
| 276 | static pj_status_t g711_enum_codecs(pjmedia_codec_factory *factory, |
| 277 | unsigned *max_count, |
| 278 | pjmedia_codec_info codecs[]) |
| 279 | { |
| 280 | unsigned count = 0; |
| 281 | |
| 282 | PJ_UNUSED_ARG(factory); |
| 283 | |
| 284 | if (count < *max_count) { |
| 285 | codecs[count].type = PJMEDIA_TYPE_AUDIO; |
| 286 | codecs[count].pt = PJMEDIA_RTP_PT_PCMU; |
| 287 | codecs[count].encoding_name = pj_str("PCMU"); |
| 288 | codecs[count].clock_rate = 8000; |
| 289 | codecs[count].channel_cnt = 1; |
| 290 | ++count; |
| 291 | } |
| 292 | if (count < *max_count) { |
| 293 | codecs[count].type = PJMEDIA_TYPE_AUDIO; |
| 294 | codecs[count].pt = PJMEDIA_RTP_PT_PCMA; |
| 295 | codecs[count].encoding_name = pj_str("PCMA"); |
| 296 | codecs[count].clock_rate = 8000; |
| 297 | codecs[count].channel_cnt = 1; |
| 298 | ++count; |
| 299 | } |
| 300 | |
| 301 | *max_count = count; |
| 302 | |
| 303 | return PJ_SUCCESS; |
| 304 | } |
| 305 | |
| 306 | static pj_status_t g711_alloc_codec( pjmedia_codec_factory *factory, |
| 307 | const pjmedia_codec_info *id, |
| 308 | pjmedia_codec **p_codec) |
| 309 | { |
| 310 | pjmedia_codec *codec = NULL; |
| 311 | pj_status_t status; |
| 312 | |
| 313 | PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL); |
| 314 | |
| 315 | /* Lock mutex. */ |
| 316 | pj_mutex_lock(g711_factory.mutex); |
| 317 | |
| 318 | /* Allocate new codec if no more is available */ |
| 319 | if (pj_list_empty(&g711_factory.codec_list)) { |
| 320 | struct g711_private *codec_priv; |
| 321 | |
| 322 | codec = PJ_POOL_ALLOC_T(g711_factory.pool, pjmedia_codec); |
| 323 | codec_priv = PJ_POOL_ZALLOC_T(g711_factory.pool, struct g711_private); |
| 324 | if (!codec || !codec_priv) { |
| 325 | pj_mutex_unlock(g711_factory.mutex); |
| 326 | return PJ_ENOMEM; |
| 327 | } |
| 328 | |
| 329 | /* Set the payload type */ |
| 330 | codec_priv->pt = id->pt; |
| 331 | |
| 332 | #if !PLC_DISABLED |
| 333 | /* Create PLC, always with 10ms ptime */ |
| 334 | status = pjmedia_plc_create(g711_factory.pool, 8000, |
| 335 | SAMPLES_PER_FRAME, |
| 336 | 0, &codec_priv->plc); |
| 337 | if (status != PJ_SUCCESS) { |
| 338 | pj_mutex_unlock(g711_factory.mutex); |
| 339 | return status; |
| 340 | } |
| 341 | #endif |
| 342 | |
| 343 | /* Create VAD */ |
| 344 | status = pjmedia_silence_det_create(g711_factory.pool, |
| 345 | 8000, SAMPLES_PER_FRAME, |
| 346 | &codec_priv->vad); |
| 347 | if (status != PJ_SUCCESS) { |
| 348 | pj_mutex_unlock(g711_factory.mutex); |
| 349 | return status; |
| 350 | } |
| 351 | |
| 352 | codec->factory = factory; |
| 353 | codec->op = &g711_op; |
| 354 | codec->codec_data = codec_priv; |
| 355 | } else { |
| 356 | codec = g711_factory.codec_list.next; |
| 357 | pj_list_erase(codec); |
| 358 | } |
| 359 | |
| 360 | /* Zero the list, for error detection in g711_dealloc_codec */ |
| 361 | codec->next = codec->prev = NULL; |
| 362 | |
| 363 | *p_codec = codec; |
| 364 | |
| 365 | /* Unlock mutex. */ |
| 366 | pj_mutex_unlock(g711_factory.mutex); |
| 367 | |
| 368 | return PJ_SUCCESS; |
| 369 | } |
| 370 | |
| 371 | static pj_status_t g711_dealloc_codec(pjmedia_codec_factory *factory, |
| 372 | pjmedia_codec *codec ) |
| 373 | { |
| 374 | struct g711_private *priv = (struct g711_private*) codec->codec_data; |
| 375 | int i = 0; |
| 376 | |
| 377 | PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL); |
| 378 | |
| 379 | /* Check that this node has not been deallocated before */ |
| 380 | pj_assert (codec->next==NULL && codec->prev==NULL); |
| 381 | if (codec->next!=NULL || codec->prev!=NULL) { |
| 382 | return PJ_EINVALIDOP; |
| 383 | } |
| 384 | |
| 385 | #if !PLC_DISABLED |
| 386 | /* Clear left samples in the PLC, since codec+plc will be reused |
| 387 | * next time. |
| 388 | */ |
| 389 | for (i=0; i<2; ++i) { |
| 390 | pj_int16_t frame[SAMPLES_PER_FRAME]; |
| 391 | pjmedia_zero_samples(frame, PJ_ARRAY_SIZE(frame)); |
| 392 | pjmedia_plc_save(priv->plc, frame); |
| 393 | } |
| 394 | #else |
| 395 | PJ_UNUSED_ARG(i); |
| 396 | PJ_UNUSED_ARG(priv); |
| 397 | #endif |
| 398 | |
| 399 | /* Lock mutex. */ |
| 400 | pj_mutex_lock(g711_factory.mutex); |
| 401 | |
| 402 | /* Insert at the back of the list */ |
| 403 | pj_list_insert_before(&g711_factory.codec_list, codec); |
| 404 | |
| 405 | /* Unlock mutex. */ |
| 406 | pj_mutex_unlock(g711_factory.mutex); |
| 407 | |
| 408 | return PJ_SUCCESS; |
| 409 | } |
| 410 | |
| 411 | static pj_status_t g711_init( pjmedia_codec *codec, pj_pool_t *pool ) |
| 412 | { |
| 413 | /* There's nothing to do here really */ |
| 414 | PJ_UNUSED_ARG(codec); |
| 415 | PJ_UNUSED_ARG(pool); |
| 416 | |
| 417 | return PJ_SUCCESS; |
| 418 | } |
| 419 | |
| 420 | static pj_status_t g711_open(pjmedia_codec *codec, |
| 421 | pjmedia_codec_param *attr ) |
| 422 | { |
| 423 | struct g711_private *priv = (struct g711_private*) codec->codec_data; |
| 424 | priv->pt = attr->info.pt; |
| 425 | #if !PLC_DISABLED |
| 426 | priv->plc_enabled = (attr->setting.plc != 0); |
| 427 | #endif |
| 428 | priv->vad_enabled = (attr->setting.vad != 0); |
| 429 | return PJ_SUCCESS; |
| 430 | } |
| 431 | |
| 432 | static pj_status_t g711_close( pjmedia_codec *codec ) |
| 433 | { |
| 434 | PJ_UNUSED_ARG(codec); |
| 435 | /* Nothing to do */ |
| 436 | return PJ_SUCCESS; |
| 437 | } |
| 438 | |
| 439 | static pj_status_t g711_modify(pjmedia_codec *codec, |
| 440 | const pjmedia_codec_param *attr ) |
| 441 | { |
| 442 | struct g711_private *priv = (struct g711_private*) codec->codec_data; |
| 443 | |
| 444 | if (attr->info.pt != priv->pt) |
| 445 | return PJMEDIA_EINVALIDPT; |
| 446 | |
| 447 | #if !PLC_DISABLED |
| 448 | priv->plc_enabled = (attr->setting.plc != 0); |
| 449 | #endif |
| 450 | priv->vad_enabled = (attr->setting.vad != 0); |
| 451 | |
| 452 | return PJ_SUCCESS; |
| 453 | } |
| 454 | |
| 455 | static pj_status_t g711_parse( pjmedia_codec *codec, |
| 456 | void *pkt, |
| 457 | pj_size_t pkt_size, |
| 458 | const pj_timestamp *ts, |
| 459 | unsigned *frame_cnt, |
| 460 | pjmedia_frame frames[]) |
| 461 | { |
| 462 | unsigned count = 0; |
| 463 | |
| 464 | PJ_UNUSED_ARG(codec); |
| 465 | |
| 466 | PJ_ASSERT_RETURN(ts && frame_cnt && frames, PJ_EINVAL); |
| 467 | |
| 468 | while (pkt_size >= FRAME_SIZE && count < *frame_cnt) { |
| 469 | frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 470 | frames[count].buf = pkt; |
| 471 | frames[count].size = FRAME_SIZE; |
| 472 | frames[count].timestamp.u64 = ts->u64 + SAMPLES_PER_FRAME * count; |
| 473 | |
| 474 | pkt = ((char*)pkt) + FRAME_SIZE; |
| 475 | pkt_size -= FRAME_SIZE; |
| 476 | |
| 477 | ++count; |
| 478 | } |
| 479 | |
| 480 | *frame_cnt = count; |
| 481 | return PJ_SUCCESS; |
| 482 | } |
| 483 | |
| 484 | static pj_status_t g711_encode(pjmedia_codec *codec, |
| 485 | const struct pjmedia_frame *input, |
| 486 | unsigned output_buf_len, |
| 487 | struct pjmedia_frame *output) |
| 488 | { |
| 489 | pj_int16_t *samples = (pj_int16_t*) input->buf; |
| 490 | struct g711_private *priv = (struct g711_private*) codec->codec_data; |
| 491 | |
| 492 | /* Check output buffer length */ |
| 493 | if (output_buf_len < (input->size >> 1)) |
| 494 | return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 495 | |
| 496 | /* Detect silence if VAD is enabled */ |
| 497 | if (priv->vad_enabled) { |
| 498 | pj_bool_t is_silence; |
| 499 | pj_int32_t silence_period; |
| 500 | |
| 501 | silence_period = pj_timestamp_diff32(&priv->last_tx, |
| 502 | &input->timestamp); |
| 503 | |
| 504 | is_silence = pjmedia_silence_det_detect(priv->vad, |
| 505 | (const pj_int16_t*) input->buf, |
| 506 | (input->size >> 1), NULL); |
| 507 | if (is_silence && |
| 508 | (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 || |
| 509 | silence_period < PJMEDIA_CODEC_MAX_SILENCE_PERIOD*8000/1000)) |
| 510 | { |
| 511 | output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 512 | output->buf = NULL; |
| 513 | output->size = 0; |
| 514 | output->timestamp = input->timestamp; |
| 515 | return PJ_SUCCESS; |
| 516 | } else { |
| 517 | priv->last_tx = input->timestamp; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /* Encode */ |
| 522 | if (priv->pt == PJMEDIA_RTP_PT_PCMA) { |
| 523 | unsigned i, n; |
| 524 | pj_uint8_t *dst = (pj_uint8_t*) output->buf; |
| 525 | |
| 526 | n = ((unsigned)input->size >> 1); |
| 527 | for (i=0; i!=n; ++i, ++dst) { |
| 528 | *dst = pjmedia_linear2alaw(samples[i]); |
| 529 | } |
| 530 | } else if (priv->pt == PJMEDIA_RTP_PT_PCMU) { |
| 531 | unsigned i, n; |
| 532 | pj_uint8_t *dst = (pj_uint8_t*) output->buf; |
| 533 | |
| 534 | n = ((unsigned)input->size >> 1); |
| 535 | for (i=0; i!=n; ++i, ++dst) { |
| 536 | *dst = pjmedia_linear2ulaw(samples[i]); |
| 537 | } |
| 538 | |
| 539 | } else { |
| 540 | return PJMEDIA_EINVALIDPT; |
| 541 | } |
| 542 | |
| 543 | output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 544 | output->size = (input->size >> 1); |
| 545 | output->timestamp = input->timestamp; |
| 546 | |
| 547 | return PJ_SUCCESS; |
| 548 | } |
| 549 | |
| 550 | static pj_status_t g711_decode(pjmedia_codec *codec, |
| 551 | const struct pjmedia_frame *input, |
| 552 | unsigned output_buf_len, |
| 553 | struct pjmedia_frame *output) |
| 554 | { |
| 555 | struct g711_private *priv = (struct g711_private*) codec->codec_data; |
| 556 | |
| 557 | /* Check output buffer length */ |
| 558 | PJ_ASSERT_RETURN(output_buf_len >= (input->size << 1), |
| 559 | PJMEDIA_CODEC_EPCMTOOSHORT); |
| 560 | |
| 561 | /* Input buffer MUST have exactly 80 bytes long */ |
| 562 | PJ_ASSERT_RETURN(input->size == FRAME_SIZE, |
| 563 | PJMEDIA_CODEC_EFRMINLEN); |
| 564 | |
| 565 | /* Decode */ |
| 566 | if (priv->pt == PJMEDIA_RTP_PT_PCMA) { |
| 567 | unsigned i; |
| 568 | pj_uint8_t *src = (pj_uint8_t*) input->buf; |
| 569 | pj_uint16_t *dst = (pj_uint16_t*) output->buf; |
| 570 | |
| 571 | for (i=0; i!=input->size; ++i) { |
| 572 | *dst++ = (pj_uint16_t) pjmedia_alaw2linear(*src++); |
| 573 | } |
| 574 | } else if (priv->pt == PJMEDIA_RTP_PT_PCMU) { |
| 575 | unsigned i; |
| 576 | pj_uint8_t *src = (pj_uint8_t*) input->buf; |
| 577 | pj_uint16_t *dst = (pj_uint16_t*) output->buf; |
| 578 | |
| 579 | for (i=0; i!=input->size; ++i) { |
| 580 | *dst++ = (pj_uint16_t) pjmedia_ulaw2linear(*src++); |
| 581 | } |
| 582 | |
| 583 | } else { |
| 584 | return PJMEDIA_EINVALIDPT; |
| 585 | } |
| 586 | |
| 587 | output->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 588 | output->size = (input->size << 1); |
| 589 | output->timestamp = input->timestamp; |
| 590 | |
| 591 | #if !PLC_DISABLED |
| 592 | if (priv->plc_enabled) |
| 593 | pjmedia_plc_save( priv->plc, (pj_int16_t*)output->buf); |
| 594 | #endif |
| 595 | |
| 596 | return PJ_SUCCESS; |
| 597 | } |
| 598 | |
| 599 | #if !PLC_DISABLED |
| 600 | static pj_status_t g711_recover( pjmedia_codec *codec, |
| 601 | unsigned output_buf_len, |
| 602 | struct pjmedia_frame *output) |
| 603 | { |
| 604 | struct g711_private *priv = (struct g711_private*) codec->codec_data; |
| 605 | |
| 606 | if (!priv->plc_enabled) |
| 607 | return PJ_EINVALIDOP; |
| 608 | |
| 609 | PJ_ASSERT_RETURN(output_buf_len >= SAMPLES_PER_FRAME * 2, |
| 610 | PJMEDIA_CODEC_EPCMTOOSHORT); |
| 611 | |
| 612 | pjmedia_plc_generate(priv->plc, (pj_int16_t*)output->buf); |
| 613 | output->size = SAMPLES_PER_FRAME * 2; |
| 614 | |
| 615 | return PJ_SUCCESS; |
| 616 | } |
| 617 | #endif |
| 618 | |
| 619 | #endif /* PJMEDIA_HAS_G711_CODEC */ |
| 620 | |
| 621 | |
| 622 | |