Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/l16.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjmedia-codec/l16.h>
20 #include <pjmedia/codec.h>
21 #include <pjmedia/errno.h>
22 #include <pjmedia/endpoint.h>
23 #include <pjmedia/plc.h>
24 #include <pjmedia/silencedet.h>
25 #include <pj/assert.h>
26 #include <pj/pool.h>
27 #include <pj/sock.h>
28 #include <pj/string.h>
29
30
31 /*
32 * Only build this file if PJMEDIA_HAS_L16_CODEC != 0
33 */
34 #if defined(PJMEDIA_HAS_L16_CODEC) && PJMEDIA_HAS_L16_CODEC != 0
35
36 #define PLC_DISABLED 0
37
38
39 static const pj_str_t STR_L16 = { "L16", 3 };
40
41 /* To keep frame size below 1400 MTU, set ptime to 10ms for
42 * sampling rate > 35 KHz
43 */
44 #define GET_PTIME(clock_rate) ((pj_uint16_t)(clock_rate > 35000 ? 10 : 20))
45
46
47 /* Prototypes for L16 factory */
48 static pj_status_t l16_test_alloc( pjmedia_codec_factory *factory,
49 const pjmedia_codec_info *id );
50 static pj_status_t l16_default_attr( pjmedia_codec_factory *factory,
51 const pjmedia_codec_info *id,
52 pjmedia_codec_param *attr );
53 static pj_status_t l16_enum_codecs (pjmedia_codec_factory *factory,
54 unsigned *count,
55 pjmedia_codec_info codecs[]);
56 static pj_status_t l16_alloc_codec( pjmedia_codec_factory *factory,
57 const pjmedia_codec_info *id,
58 pjmedia_codec **p_codec);
59 static pj_status_t l16_dealloc_codec( pjmedia_codec_factory *factory,
60 pjmedia_codec *codec );
61
62 /* Prototypes for L16 implementation. */
63 static pj_status_t l16_init( pjmedia_codec *codec,
64 pj_pool_t *pool );
65 static pj_status_t l16_open( pjmedia_codec *codec,
66 pjmedia_codec_param *attr );
67 static pj_status_t l16_close( pjmedia_codec *codec );
68 static pj_status_t l16_modify(pjmedia_codec *codec,
69 const pjmedia_codec_param *attr );
70 static pj_status_t l16_parse(pjmedia_codec *codec,
71 void *pkt,
72 pj_size_t pkt_size,
73 const pj_timestamp *ts,
74 unsigned *frame_cnt,
75 pjmedia_frame frames[]);
76 static pj_status_t l16_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 l16_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 l16_recover(pjmedia_codec *codec,
86 unsigned output_buf_len,
87 struct pjmedia_frame *output);
88 #endif
89
90 /* Definition for L16 codec operations. */
91 static pjmedia_codec_op l16_op =
92 {
93 &l16_init,
94 &l16_open,
95 &l16_close,
96 &l16_modify,
97 &l16_parse,
98 &l16_encode,
99 &l16_decode,
100 #if !PLC_DISABLED
101 &l16_recover
102 #else
103 NULL
104 #endif
105 };
106
107 /* Definition for L16 codec factory operations. */
108 static pjmedia_codec_factory_op l16_factory_op =
109 {
110 &l16_test_alloc,
111 &l16_default_attr,
112 &l16_enum_codecs,
113 &l16_alloc_codec,
114 &l16_dealloc_codec,
115 &pjmedia_codec_l16_deinit
116 };
117
118 /* L16 factory private data */
119 static struct l16_factory
120 {
121 pjmedia_codec_factory base;
122 pjmedia_endpt *endpt;
123 pj_pool_t *pool;
124 pj_mutex_t *mutex;
125 } l16_factory;
126
127
128 /* L16 codec private data. */
129 struct l16_data
130 {
131 pj_pool_t *pool;
132 unsigned frame_size; /* Frame size, in bytes */
133 unsigned clock_rate; /* Clock rate */
134
135 #if !PLC_DISABLED
136 pj_bool_t plc_enabled;
137 pjmedia_plc *plc;
138 #endif
139 pj_bool_t vad_enabled;
140 pjmedia_silence_det *vad;
141 pj_timestamp last_tx;
142 };
143
144
145
146 PJ_DEF(pj_status_t) pjmedia_codec_l16_init(pjmedia_endpt *endpt,
147 unsigned options)
148 {
149 pjmedia_codec_mgr *codec_mgr;
150 pj_status_t status;
151
152
153 PJ_UNUSED_ARG(options);
154
155
156 if (l16_factory.endpt != NULL) {
157 /* Already initialized. */
158 return PJ_SUCCESS;
159 }
160
161 /* Init factory */
162 l16_factory.base.op = &l16_factory_op;
163 l16_factory.base.factory_data = NULL;
164 l16_factory.endpt = endpt;
165
166 /* Create pool */
167 l16_factory.pool = pjmedia_endpt_create_pool(endpt, "l16", 1000, 4000);
168 if (!l16_factory.pool)
169 return PJ_ENOMEM;
170
171 /* Create mutex. */
172 status = pj_mutex_create_simple(l16_factory.pool, "l16",
173 &l16_factory.mutex);
174 if (status != PJ_SUCCESS)
175 goto on_error;
176
177 /* Get the codec manager. */
178 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
179 if (!codec_mgr) {
180 return PJ_EINVALIDOP;
181 }
182
183 /* Register codec factory to endpoint. */
184 status = pjmedia_codec_mgr_register_factory(codec_mgr,
185 &l16_factory.base);
186 if (status != PJ_SUCCESS)
187 return status;
188
189
190 return PJ_SUCCESS;
191
192 on_error:
193 if (l16_factory.mutex) {
194 pj_mutex_destroy(l16_factory.mutex);
195 l16_factory.mutex = NULL;
196 }
197 if (l16_factory.pool) {
198 pj_pool_release(l16_factory.pool);
199 l16_factory.pool = NULL;
200 }
201 return status;
202 }
203
204 PJ_DEF(pj_status_t) pjmedia_codec_l16_deinit(void)
205 {
206 pjmedia_codec_mgr *codec_mgr;
207 pj_status_t status;
208
209 if (l16_factory.endpt == NULL) {
210 /* Not registered. */
211 return PJ_SUCCESS;
212 }
213
214 /* Lock mutex. */
215 pj_mutex_lock(l16_factory.mutex);
216
217 /* Get the codec manager. */
218 codec_mgr = pjmedia_endpt_get_codec_mgr(l16_factory.endpt);
219 if (!codec_mgr) {
220 l16_factory.endpt = NULL;
221 pj_mutex_unlock(l16_factory.mutex);
222 return PJ_EINVALIDOP;
223 }
224
225 /* Release the factory mutex before calling unregister to avoid lock
226 * order inversion with codec manager mutex.
227 */
228 pj_mutex_unlock(l16_factory.mutex);
229
230 /* Unregister L16 codec factory. */
231 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
232 &l16_factory.base);
233 l16_factory.endpt = NULL;
234
235 /* Destroy mutex. */
236 pj_mutex_destroy(l16_factory.mutex);
237 l16_factory.mutex = NULL;
238
239
240 /* Release pool. */
241 pj_pool_release(l16_factory.pool);
242 l16_factory.pool = NULL;
243
244
245 return status;
246 }
247
248 static pj_status_t l16_test_alloc(pjmedia_codec_factory *factory,
249 const pjmedia_codec_info *id )
250 {
251 PJ_UNUSED_ARG(factory);
252
253 if (pj_stricmp(&id->encoding_name, &STR_L16)==0) {
254 /* Match! */
255 return PJ_SUCCESS;
256 }
257
258 return -1;
259 }
260
261 static pj_status_t l16_default_attr( pjmedia_codec_factory *factory,
262 const pjmedia_codec_info *id,
263 pjmedia_codec_param *attr )
264 {
265 PJ_UNUSED_ARG(factory);
266
267 pj_bzero(attr, sizeof(pjmedia_codec_param));
268 attr->info.pt = (pj_uint8_t)id->pt;
269 attr->info.clock_rate = id->clock_rate;
270 attr->info.channel_cnt = id->channel_cnt;
271 attr->info.avg_bps = id->clock_rate * id->channel_cnt * 16;
272 attr->info.max_bps = attr->info.avg_bps;
273 attr->info.pcm_bits_per_sample = 16;
274
275 /* To keep frame size below 1400 MTU, set ptime to 10ms for
276 * sampling rate > 35 KHz
277 */
278 attr->info.frm_ptime = GET_PTIME(id->clock_rate);
279
280 attr->setting.frm_per_pkt = 1;
281
282 attr->setting.vad = 1;
283 #if !PLC_DISABLED
284 attr->setting.plc = 1;
285 #endif
286
287 return PJ_SUCCESS;
288 }
289
290 static pj_status_t l16_enum_codecs( pjmedia_codec_factory *factory,
291 unsigned *max_count,
292 pjmedia_codec_info codecs[])
293 {
294 unsigned count = 0;
295
296 PJ_UNUSED_ARG(factory);
297
298 if (count < *max_count) {
299 /* Register 44100Hz 1 channel L16 codec */
300 codecs[count].type = PJMEDIA_TYPE_AUDIO;
301 codecs[count].pt = PJMEDIA_RTP_PT_L16_1;
302 codecs[count].encoding_name = STR_L16;
303 codecs[count].clock_rate = 44100;
304 codecs[count].channel_cnt = 1;
305 ++count;
306 }
307
308 if (count < *max_count) {
309 /* Register 44100Hz 2 channels L16 codec */
310 codecs[count].type = PJMEDIA_TYPE_AUDIO;
311 codecs[count].pt = PJMEDIA_RTP_PT_L16_2;
312 codecs[count].encoding_name = STR_L16;
313 codecs[count].clock_rate = 44100;
314 codecs[count].channel_cnt = 2;
315 ++count;
316 }
317
318 #if PJMEDIA_CODEC_L16_HAS_8KHZ_MONO
319 if (count < *max_count) {
320 /* 8KHz mono */
321 codecs[count].type = PJMEDIA_TYPE_AUDIO;
322 codecs[count].pt = PJMEDIA_RTP_PT_L16_8KHZ_MONO;
323 codecs[count].encoding_name = STR_L16;
324 codecs[count].clock_rate = 8000;
325 codecs[count].channel_cnt = 1;
326 ++count;
327 }
328 #endif
329
330 #if PJMEDIA_CODEC_L16_HAS_8KHZ_STEREO
331 if (count < *max_count) {
332 /* 8KHz stereo */
333 codecs[count].type = PJMEDIA_TYPE_AUDIO;
334 codecs[count].pt = PJMEDIA_RTP_PT_L16_8KHZ_STEREO;
335 codecs[count].encoding_name = STR_L16;
336 codecs[count].clock_rate = 8000;
337 codecs[count].channel_cnt = 2;
338 ++count;
339 }
340 #endif
341
342 // disable some L16 modes
343 #if 0
344 if (count < *max_count) {
345 /* 11025 Hz mono */
346 codecs[count].type = PJMEDIA_TYPE_AUDIO;
347 codecs[count].pt = PJMEDIA_RTP_PT_L16_11KHZ_MONO;
348 codecs[count].encoding_name = STR_L16;
349 codecs[count].clock_rate = 11025;
350 codecs[count].channel_cnt = 1;
351 ++count;
352 }
353
354 if (count < *max_count) {
355 /* 11025 Hz stereo */
356 codecs[count].type = PJMEDIA_TYPE_AUDIO;
357 codecs[count].pt = PJMEDIA_RTP_PT_L16_11KHZ_STEREO;
358 codecs[count].encoding_name = STR_L16;
359 codecs[count].clock_rate = 11025;
360 codecs[count].channel_cnt = 2;
361 ++count;
362 }
363 #endif
364
365 #if PJMEDIA_CODEC_L16_HAS_16KHZ_MONO
366 if (count < *max_count) {
367 /* 16000 Hz mono */
368 codecs[count].type = PJMEDIA_TYPE_AUDIO;
369 codecs[count].pt = PJMEDIA_RTP_PT_L16_16KHZ_MONO;
370 codecs[count].encoding_name = STR_L16;
371 codecs[count].clock_rate = 16000;
372 codecs[count].channel_cnt = 1;
373 ++count;
374 }
375 #endif
376
377 #if PJMEDIA_CODEC_L16_HAS_16KHZ_STEREO
378 if (count < *max_count) {
379 /* 16000 Hz stereo */
380 codecs[count].type = PJMEDIA_TYPE_AUDIO;
381 codecs[count].pt = PJMEDIA_RTP_PT_L16_16KHZ_STEREO;
382 codecs[count].encoding_name = STR_L16;
383 codecs[count].clock_rate = 16000;
384 codecs[count].channel_cnt = 2;
385 ++count;
386 }
387 #endif
388
389 // disable some L16 modes
390 #if 0
391 if (count < *max_count) {
392 /* 22050 Hz mono */
393 codecs[count].type = PJMEDIA_TYPE_AUDIO;
394 codecs[count].pt = PJMEDIA_RTP_PT_L16_22KHZ_MONO;
395 codecs[count].encoding_name = STR_L16;
396 codecs[count].clock_rate = 22050;
397 codecs[count].channel_cnt = 1;
398 ++count;
399 }
400
401
402 if (count < *max_count) {
403 /* 22050 Hz stereo */
404 codecs[count].type = PJMEDIA_TYPE_AUDIO;
405 codecs[count].pt = PJMEDIA_RTP_PT_L16_22KHZ_STEREO;
406 codecs[count].encoding_name = STR_L16;
407 codecs[count].clock_rate = 22050;
408 codecs[count].channel_cnt = 2;
409 ++count;
410 }
411
412 if (count < *max_count) {
413 /* 32000 Hz mono */
414 codecs[count].type = PJMEDIA_TYPE_AUDIO;
415 codecs[count].pt = PJMEDIA_RTP_PT_L16_32KHZ_MONO;
416 codecs[count].encoding_name = STR_L16;
417 codecs[count].clock_rate = 32000;
418 codecs[count].channel_cnt = 1;
419 ++count;
420 }
421
422 if (count < *max_count) {
423 /* 32000 Hz stereo */
424 codecs[count].type = PJMEDIA_TYPE_AUDIO;
425 codecs[count].pt = PJMEDIA_RTP_PT_L16_32KHZ_STEREO;
426 codecs[count].encoding_name = STR_L16;
427 codecs[count].clock_rate = 32000;
428 codecs[count].channel_cnt = 2;
429 ++count;
430 }
431 #endif
432
433 #if PJMEDIA_CODEC_L16_HAS_48KHZ_MONO
434 if (count < *max_count) {
435 /* 48KHz mono */
436 codecs[count].type = PJMEDIA_TYPE_AUDIO;
437 codecs[count].pt = PJMEDIA_RTP_PT_L16_48KHZ_MONO;
438 codecs[count].encoding_name = STR_L16;
439 codecs[count].clock_rate = 48000;
440 codecs[count].channel_cnt = 1;
441 ++count;
442 }
443 #endif
444
445 #if PJMEDIA_CODEC_L16_HAS_48KHZ_STEREO
446 if (count < *max_count) {
447 /* 48KHz stereo */
448 codecs[count].type = PJMEDIA_TYPE_AUDIO;
449 codecs[count].pt = PJMEDIA_RTP_PT_L16_48KHZ_STEREO;
450 codecs[count].encoding_name = STR_L16;
451 codecs[count].clock_rate = 48000;
452 codecs[count].channel_cnt = 2;
453 ++count;
454 }
455 #endif
456
457
458 *max_count = count;
459
460 return PJ_SUCCESS;
461 }
462
463 static pj_status_t l16_alloc_codec( pjmedia_codec_factory *factory,
464 const pjmedia_codec_info *id,
465 pjmedia_codec **p_codec)
466 {
467 pjmedia_codec *codec = NULL;
468 struct l16_data *data;
469 unsigned ptime;
470 pj_pool_t *pool;
471
472 pj_status_t status;
473
474 PJ_ASSERT_RETURN(factory==&l16_factory.base, PJ_EINVAL);
475
476 /* Lock mutex. */
477 pj_mutex_lock(l16_factory.mutex);
478
479
480 pool = pjmedia_endpt_create_pool(l16_factory.endpt, "l16", 4000, 4000);
481 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
482 codec->codec_data = pj_pool_alloc(pool, sizeof(struct l16_data));
483 codec->factory = factory;
484 codec->op = &l16_op;
485
486 /* Init private data */
487 ptime = GET_PTIME(id->clock_rate);
488 data = (struct l16_data*) codec->codec_data;
489 data->frame_size = ptime * id->clock_rate * id->channel_cnt * 2 / 1000;
490 data->clock_rate = id->clock_rate;
491 data->pool = pool;
492
493 #if !PLC_DISABLED
494 /* Create PLC */
495 status = pjmedia_plc_create(pool, id->clock_rate,
496 data->frame_size >> 1, 0,
497 &data->plc);
498 if (status != PJ_SUCCESS) {
499 pj_mutex_unlock(l16_factory.mutex);
500 return status;
501 }
502 #endif
503
504 /* Create silence detector */
505 status = pjmedia_silence_det_create(pool, id->clock_rate,
506 data->frame_size >> 1,
507 &data->vad);
508 if (status != PJ_SUCCESS) {
509 pj_mutex_unlock(l16_factory.mutex);
510 return status;
511 }
512
513 *p_codec = codec;
514
515 /* Unlock mutex. */
516 pj_mutex_unlock(l16_factory.mutex);
517
518 return PJ_SUCCESS;
519 }
520
521 static pj_status_t l16_dealloc_codec(pjmedia_codec_factory *factory,
522 pjmedia_codec *codec )
523 {
524 struct l16_data *data;
525
526 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
527 PJ_ASSERT_RETURN(factory==&l16_factory.base, PJ_EINVAL);
528
529 /* Lock mutex. */
530 pj_mutex_lock(l16_factory.mutex);
531
532 /* Just release codec data pool */
533 data = (struct l16_data*) codec->codec_data;
534 pj_assert(data);
535 pj_pool_release(data->pool);
536
537 /* Unlock mutex. */
538 pj_mutex_unlock(l16_factory.mutex);
539
540 return PJ_SUCCESS;
541 }
542
543 static pj_status_t l16_init( pjmedia_codec *codec, pj_pool_t *pool )
544 {
545 /* There's nothing to do here really */
546 PJ_UNUSED_ARG(codec);
547 PJ_UNUSED_ARG(pool);
548
549 return PJ_SUCCESS;
550 }
551
552 static pj_status_t l16_open(pjmedia_codec *codec,
553 pjmedia_codec_param *attr )
554 {
555 struct l16_data *data = NULL;
556
557 PJ_ASSERT_RETURN(codec && codec->codec_data && attr, PJ_EINVAL);
558
559 data = (struct l16_data*) codec->codec_data;
560
561 data->vad_enabled = (attr->setting.vad != 0);
562 #if !PLC_DISABLED
563 data->plc_enabled = (attr->setting.plc != 0);
564 #endif
565
566 return PJ_SUCCESS;
567 }
568
569 static pj_status_t l16_close( pjmedia_codec *codec )
570 {
571 PJ_UNUSED_ARG(codec);
572 /* Nothing to do */
573 return PJ_SUCCESS;
574 }
575
576 static pj_status_t l16_modify(pjmedia_codec *codec,
577 const pjmedia_codec_param *attr )
578 {
579 struct l16_data *data = (struct l16_data*) codec->codec_data;
580
581 pj_assert(data != NULL);
582
583 data->vad_enabled = (attr->setting.vad != 0);
584 #if !PLC_DISABLED
585 data->plc_enabled = (attr->setting.plc != 0);
586 #endif
587
588 return PJ_SUCCESS;
589 }
590
591 static pj_status_t l16_parse( pjmedia_codec *codec,
592 void *pkt,
593 pj_size_t pkt_size,
594 const pj_timestamp *ts,
595 unsigned *frame_cnt,
596 pjmedia_frame frames[])
597 {
598 unsigned count = 0;
599 struct l16_data *data = (struct l16_data*) codec->codec_data;
600
601 PJ_UNUSED_ARG(codec);
602 PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
603
604 while (pkt_size >= data->frame_size && count < *frame_cnt) {
605 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
606 frames[count].buf = pkt;
607 frames[count].size = data->frame_size;
608 frames[count].timestamp.u64 = ts->u64 + (count * data->frame_size >> 1);
609
610 pkt = ((char*)pkt) + data->frame_size;
611 pkt_size -= data->frame_size;
612
613 ++count;
614 }
615
616 *frame_cnt = count;
617 return PJ_SUCCESS;
618 }
619
620 static pj_status_t l16_encode(pjmedia_codec *codec,
621 const struct pjmedia_frame *input,
622 unsigned output_buf_len,
623 struct pjmedia_frame *output)
624 {
625 struct l16_data *data = (struct l16_data*) codec->codec_data;
626 const pj_int16_t *samp = (const pj_int16_t*) input->buf;
627 const pj_int16_t *samp_end = samp + input->size/sizeof(pj_int16_t);
628 pj_int16_t *samp_out = (pj_int16_t*) output->buf;
629
630 pj_assert(data && input && output);
631
632 /* Check output buffer length */
633 if (output_buf_len < input->size)
634 return PJMEDIA_CODEC_EFRMTOOSHORT;
635
636 /* Detect silence */
637 if (data->vad_enabled) {
638 pj_bool_t is_silence;
639 pj_int32_t silence_duration;
640
641 silence_duration = pj_timestamp_diff32(&data->last_tx,
642 &input->timestamp);
643
644 is_silence = pjmedia_silence_det_detect(data->vad,
645 (const pj_int16_t*) input->buf,
646 (input->size >> 1),
647 NULL);
648 if (is_silence &&
649 (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
650 silence_duration < PJMEDIA_CODEC_MAX_SILENCE_PERIOD*
651 (int)data->clock_rate/1000))
652 {
653 output->type = PJMEDIA_FRAME_TYPE_NONE;
654 output->buf = NULL;
655 output->size = 0;
656 output->timestamp = input->timestamp;
657 return PJ_SUCCESS;
658 } else {
659 data->last_tx = input->timestamp;
660 }
661 }
662
663 /* Encode */
664 #if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0
665 while (samp!=samp_end)
666 *samp_out++ = pj_htons(*samp++);
667 #else
668 pjmedia_copy_samples(samp_out, samp, input->size >> 1);
669 #endif
670
671
672 /* Done */
673 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
674 output->size = input->size;
675 output->timestamp = input->timestamp;
676
677 return PJ_SUCCESS;
678 }
679
680 static pj_status_t l16_decode(pjmedia_codec *codec,
681 const struct pjmedia_frame *input,
682 unsigned output_buf_len,
683 struct pjmedia_frame *output)
684 {
685 struct l16_data *l16_data = (struct l16_data*) codec->codec_data;
686 const pj_int16_t *samp = (const pj_int16_t*) input->buf;
687 const pj_int16_t *samp_end = samp + input->size/sizeof(pj_int16_t);
688 pj_int16_t *samp_out = (pj_int16_t*) output->buf;
689
690 pj_assert(l16_data != NULL);
691 PJ_ASSERT_RETURN(input && output, PJ_EINVAL);
692
693
694 /* Check output buffer length */
695 if (output_buf_len < input->size)
696 return PJMEDIA_CODEC_EPCMTOOSHORT;
697
698
699 /* Decode */
700 #if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0
701 while (samp!=samp_end)
702 *samp_out++ = pj_ntohs(*samp++);
703 #else
704 pjmedia_copy_samples(samp_out, samp, input->size >> 1);
705 #endif
706
707
708 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
709 output->size = input->size;
710 output->timestamp = input->timestamp;
711
712 #if !PLC_DISABLED
713 if (l16_data->plc_enabled)
714 pjmedia_plc_save( l16_data->plc, (pj_int16_t*)output->buf);
715 #endif
716
717 return PJ_SUCCESS;
718 }
719
720 #if !PLC_DISABLED
721 /*
722 * Recover lost frame.
723 */
724 static pj_status_t l16_recover(pjmedia_codec *codec,
725 unsigned output_buf_len,
726 struct pjmedia_frame *output)
727 {
728 struct l16_data *data = (struct l16_data*) codec->codec_data;
729
730 PJ_ASSERT_RETURN(data->plc_enabled, PJ_EINVALIDOP);
731
732 PJ_ASSERT_RETURN(output_buf_len >= data->frame_size,
733 PJMEDIA_CODEC_EPCMTOOSHORT);
734
735 pjmedia_plc_generate(data->plc, (pj_int16_t*)output->buf);
736 output->size = data->frame_size;
737
738 return PJ_SUCCESS;
739 }
740 #endif
741
742 #endif /* PJMEDIA_HAS_L16_CODEC */
743
744