Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/g722.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/g722.h>
20 #include <pjmedia/codec.h>
21 #include <pjmedia/errno.h>
22 #include <pjmedia/endpoint.h>
23 #include <pjmedia/plc.h>
24 #include <pjmedia/port.h>
25 #include <pjmedia/silencedet.h>
26 #include <pj/assert.h>
27 #include <pj/log.h>
28 #include <pj/pool.h>
29 #include <pj/string.h>
30 #include <pj/os.h>
31
32 #if defined(PJMEDIA_HAS_G722_CODEC) && (PJMEDIA_HAS_G722_CODEC != 0)
33
34 #include "g722/g722_enc.h"
35 #include "g722/g722_dec.h"
36
37 #define THIS_FILE "g722.c"
38
39 /* Defines */
40 #define PTIME (10)
41 #define SAMPLES_PER_FRAME (16000 * PTIME /1000)
42 #define FRAME_LEN (80)
43 #define PLC_DISABLED 0
44
45 /* Tracing */
46 #ifndef PJ_TRACE
47 # define PJ_TRACE 0
48 #endif
49
50 #if PJ_TRACE
51 # define TRACE_(expr) PJ_LOG(4,expr)
52 #else
53 # define TRACE_(expr)
54 #endif
55
56 /* Prototypes for G722 factory */
57 static pj_status_t g722_test_alloc(pjmedia_codec_factory *factory,
58 const pjmedia_codec_info *id );
59 static pj_status_t g722_default_attr(pjmedia_codec_factory *factory,
60 const pjmedia_codec_info *id,
61 pjmedia_codec_param *attr );
62 static pj_status_t g722_enum_codecs(pjmedia_codec_factory *factory,
63 unsigned *count,
64 pjmedia_codec_info codecs[]);
65 static pj_status_t g722_alloc_codec(pjmedia_codec_factory *factory,
66 const pjmedia_codec_info *id,
67 pjmedia_codec **p_codec);
68 static pj_status_t g722_dealloc_codec(pjmedia_codec_factory *factory,
69 pjmedia_codec *codec );
70
71 /* Prototypes for G722 implementation. */
72 static pj_status_t g722_codec_init(pjmedia_codec *codec,
73 pj_pool_t *pool );
74 static pj_status_t g722_codec_open(pjmedia_codec *codec,
75 pjmedia_codec_param *attr );
76 static pj_status_t g722_codec_close(pjmedia_codec *codec );
77 static pj_status_t g722_codec_modify(pjmedia_codec *codec,
78 const pjmedia_codec_param *attr );
79 static pj_status_t g722_codec_parse(pjmedia_codec *codec,
80 void *pkt,
81 pj_size_t pkt_size,
82 const pj_timestamp *ts,
83 unsigned *frame_cnt,
84 pjmedia_frame frames[]);
85 static pj_status_t g722_codec_encode(pjmedia_codec *codec,
86 const struct pjmedia_frame *input,
87 unsigned output_buf_len,
88 struct pjmedia_frame *output);
89 static pj_status_t g722_codec_decode(pjmedia_codec *codec,
90 const struct pjmedia_frame *input,
91 unsigned output_buf_len,
92 struct pjmedia_frame *output);
93 #if !PLC_DISABLED
94 static pj_status_t g722_codec_recover(pjmedia_codec *codec,
95 unsigned output_buf_len,
96 struct pjmedia_frame *output);
97 #endif
98
99 /* Definition for G722 codec operations. */
100 static pjmedia_codec_op g722_op =
101 {
102 &g722_codec_init,
103 &g722_codec_open,
104 &g722_codec_close,
105 &g722_codec_modify,
106 &g722_codec_parse,
107 &g722_codec_encode,
108 &g722_codec_decode,
109 #if !PLC_DISABLED
110 &g722_codec_recover
111 #else
112 NULL
113 #endif
114 };
115
116 /* Definition for G722 codec factory operations. */
117 static pjmedia_codec_factory_op g722_factory_op =
118 {
119 &g722_test_alloc,
120 &g722_default_attr,
121 &g722_enum_codecs,
122 &g722_alloc_codec,
123 &g722_dealloc_codec,
124 &pjmedia_codec_g722_deinit
125 };
126
127 /* G722 factory */
128 static struct g722_codec_factory
129 {
130 pjmedia_codec_factory base;
131 pjmedia_endpt *endpt;
132 pj_pool_t *pool;
133 pj_mutex_t *mutex;
134 pjmedia_codec codec_list;
135 unsigned pcm_shift;
136 } g722_codec_factory;
137
138
139 /* G722 codec private data. */
140 struct g722_data
141 {
142 g722_enc_t encoder;
143 g722_dec_t decoder;
144 pj_int16_t pcm_shift_val;
145 pj_int16_t pcm_min;
146 pj_int16_t pcm_max;
147 pj_bool_t plc_enabled;
148 pj_bool_t vad_enabled;
149 pjmedia_silence_det *vad;
150 pj_timestamp last_tx;
151 #if !PLC_DISABLED
152 pjmedia_plc *plc;
153 #endif
154 };
155
156
157
158 /*
159 * Initialize and register G722 codec factory to pjmedia endpoint.
160 */
161 PJ_DEF(pj_status_t) pjmedia_codec_g722_init( pjmedia_endpt *endpt )
162 {
163 pjmedia_codec_mgr *codec_mgr;
164 pj_status_t status;
165
166 if (g722_codec_factory.pool != NULL)
167 return PJ_SUCCESS;
168
169 /* Create G722 codec factory. */
170 g722_codec_factory.base.op = &g722_factory_op;
171 g722_codec_factory.base.factory_data = NULL;
172 g722_codec_factory.endpt = endpt;
173 g722_codec_factory.pcm_shift = PJMEDIA_G722_DEFAULT_PCM_SHIFT;
174
175 g722_codec_factory.pool = pjmedia_endpt_create_pool(endpt, "g722",
176 PJMEDIA_POOL_LEN_G722_CODEC,
177 PJMEDIA_POOL_INC_G722_CODEC);
178 if (!g722_codec_factory.pool)
179 return PJ_ENOMEM;
180
181 pj_list_init(&g722_codec_factory.codec_list);
182
183 /* Create mutex. */
184 status = pj_mutex_create_simple(g722_codec_factory.pool, "g722",
185 &g722_codec_factory.mutex);
186 if (status != PJ_SUCCESS)
187 goto on_error;
188
189 /* Get the codec manager. */
190 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
191 if (!codec_mgr) {
192 status = PJ_EINVALIDOP;
193 goto on_error;
194 }
195
196 /* Register codec factory to endpoint. */
197 status = pjmedia_codec_mgr_register_factory(codec_mgr,
198 &g722_codec_factory.base);
199 if (status != PJ_SUCCESS)
200 goto on_error;
201
202 TRACE_((THIS_FILE, "G722 codec factory initialized"));
203
204 /* Done. */
205 return PJ_SUCCESS;
206
207 on_error:
208 pj_pool_release(g722_codec_factory.pool);
209 g722_codec_factory.pool = NULL;
210 return status;
211 }
212
213 /*
214 * Unregister G722 codec factory from pjmedia endpoint and deinitialize
215 * the G722 codec library.
216 */
217 PJ_DEF(pj_status_t) pjmedia_codec_g722_deinit(void)
218 {
219 pjmedia_codec_mgr *codec_mgr;
220 pj_status_t status;
221
222 if (g722_codec_factory.pool == NULL)
223 return PJ_SUCCESS;
224
225 /* Get the codec manager. */
226 codec_mgr = pjmedia_endpt_get_codec_mgr(g722_codec_factory.endpt);
227 if (!codec_mgr) {
228 pj_pool_release(g722_codec_factory.pool);
229 g722_codec_factory.pool = NULL;
230 return PJ_EINVALIDOP;
231 }
232
233 /* Unregister G722 codec factory. */
234 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
235 &g722_codec_factory.base);
236
237 /* Destroy mutex. */
238 pj_mutex_destroy(g722_codec_factory.mutex);
239
240 /* Destroy pool. */
241 pj_pool_release(g722_codec_factory.pool);
242 g722_codec_factory.pool = NULL;
243
244 TRACE_((THIS_FILE, "G722 codec factory shutdown"));
245 return status;
246 }
247
248
249 /*
250 * Set level adjustment.
251 */
252 PJ_DEF(pj_status_t) pjmedia_codec_g722_set_pcm_shift(unsigned val)
253 {
254 g722_codec_factory.pcm_shift = val;
255 return PJ_SUCCESS;
256 }
257
258
259 /*
260 * Check if factory can allocate the specified codec.
261 */
262 static pj_status_t g722_test_alloc(pjmedia_codec_factory *factory,
263 const pjmedia_codec_info *info )
264 {
265 PJ_UNUSED_ARG(factory);
266
267 /* Check payload type. */
268 if (info->pt != PJMEDIA_RTP_PT_G722)
269 return PJMEDIA_CODEC_EUNSUP;
270
271 /* Ignore the rest, since it's static payload type. */
272
273 return PJ_SUCCESS;
274 }
275
276 /*
277 * Generate default attribute.
278 */
279 static pj_status_t g722_default_attr( pjmedia_codec_factory *factory,
280 const pjmedia_codec_info *id,
281 pjmedia_codec_param *attr )
282 {
283 PJ_UNUSED_ARG(factory);
284 PJ_UNUSED_ARG(id);
285
286 pj_bzero(attr, sizeof(pjmedia_codec_param));
287 attr->info.clock_rate = 16000;
288 attr->info.channel_cnt = 1;
289 attr->info.avg_bps = 64000;
290 attr->info.max_bps = 64000;
291 attr->info.pcm_bits_per_sample = 16;
292 attr->info.frm_ptime = PTIME;
293 attr->info.pt = PJMEDIA_RTP_PT_G722;
294
295 attr->setting.frm_per_pkt = 2;
296 attr->setting.vad = 1;
297 attr->setting.plc = 1;
298
299 /* Default all other flag bits disabled. */
300
301 return PJ_SUCCESS;
302 }
303
304 /*
305 * Enum codecs supported by this factory (i.e. only G722!).
306 */
307 static pj_status_t g722_enum_codecs(pjmedia_codec_factory *factory,
308 unsigned *count,
309 pjmedia_codec_info codecs[])
310 {
311 PJ_UNUSED_ARG(factory);
312 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
313
314 pj_bzero(&codecs[0], sizeof(pjmedia_codec_info));
315 codecs[0].encoding_name = pj_str("G722");
316 codecs[0].pt = PJMEDIA_RTP_PT_G722;
317 codecs[0].type = PJMEDIA_TYPE_AUDIO;
318 codecs[0].clock_rate = 16000;
319 codecs[0].channel_cnt = 1;
320
321 *count = 1;
322
323 return PJ_SUCCESS;
324 }
325
326 /*
327 * Allocate a new G722 codec instance.
328 */
329 static pj_status_t g722_alloc_codec(pjmedia_codec_factory *factory,
330 const pjmedia_codec_info *id,
331 pjmedia_codec **p_codec)
332 {
333 pjmedia_codec *codec;
334 struct g722_data *g722_data;
335
336 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
337 PJ_ASSERT_RETURN(factory == &g722_codec_factory.base, PJ_EINVAL);
338
339 pj_mutex_lock(g722_codec_factory.mutex);
340
341 /* Get free nodes, if any. */
342 if (!pj_list_empty(&g722_codec_factory.codec_list)) {
343 codec = g722_codec_factory.codec_list.next;
344 pj_list_erase(codec);
345 } else {
346 pj_status_t status;
347
348 codec = PJ_POOL_ZALLOC_T(g722_codec_factory.pool, pjmedia_codec);
349 PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM);
350 codec->op = &g722_op;
351 codec->factory = factory;
352
353 g722_data = PJ_POOL_ZALLOC_T(g722_codec_factory.pool, struct g722_data);
354 codec->codec_data = g722_data;
355
356 #if !PLC_DISABLED
357 /* Create PLC */
358 status = pjmedia_plc_create(g722_codec_factory.pool, 16000,
359 SAMPLES_PER_FRAME, 0, &g722_data->plc);
360 if (status != PJ_SUCCESS) {
361 pj_mutex_unlock(g722_codec_factory.mutex);
362 return status;
363 }
364 #endif
365
366 /* Create silence detector */
367 status = pjmedia_silence_det_create(g722_codec_factory.pool,
368 16000, SAMPLES_PER_FRAME,
369 &g722_data->vad);
370 if (status != PJ_SUCCESS) {
371 pj_mutex_unlock(g722_codec_factory.mutex);
372 TRACE_((THIS_FILE, "Create silence detector failed (status = %d)",
373 status));
374 return status;
375 }
376 }
377
378
379 pj_mutex_unlock(g722_codec_factory.mutex);
380
381 *p_codec = codec;
382 return PJ_SUCCESS;
383 }
384
385 /*
386 * Free codec.
387 */
388 static pj_status_t g722_dealloc_codec(pjmedia_codec_factory *factory,
389 pjmedia_codec *codec )
390 {
391 struct g722_data *g722_data;
392 int i;
393
394 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
395 PJ_ASSERT_RETURN(factory == &g722_codec_factory.base, PJ_EINVAL);
396
397 g722_data = (struct g722_data*) codec->codec_data;
398
399 /* Close codec, if it's not closed. */
400 g722_codec_close(codec);
401
402 #if !PLC_DISABLED
403 /* Clear left samples in the PLC, since codec+plc will be reused
404 * next time.
405 */
406 for (i=0; i<2; ++i) {
407 pj_int16_t frame[SAMPLES_PER_FRAME];
408 pjmedia_zero_samples(frame, PJ_ARRAY_SIZE(frame));
409 pjmedia_plc_save(g722_data->plc, frame);
410 }
411 #else
412 PJ_UNUSED_ARG(i);
413 #endif
414
415 /* Re-init silence_period */
416 pj_set_timestamp32(&g722_data->last_tx, 0, 0);
417
418 /* Put in the free list. */
419 pj_mutex_lock(g722_codec_factory.mutex);
420 pj_list_push_front(&g722_codec_factory.codec_list, codec);
421 pj_mutex_unlock(g722_codec_factory.mutex);
422
423 return PJ_SUCCESS;
424 }
425
426 /*
427 * Init codec.
428 */
429 static pj_status_t g722_codec_init(pjmedia_codec *codec,
430 pj_pool_t *pool )
431 {
432 PJ_UNUSED_ARG(codec);
433 PJ_UNUSED_ARG(pool);
434 return PJ_SUCCESS;
435 }
436
437 /*
438 * Open codec.
439 */
440 static pj_status_t g722_codec_open(pjmedia_codec *codec,
441 pjmedia_codec_param *attr )
442 {
443 struct g722_data *g722_data = (struct g722_data*) codec->codec_data;
444 pj_status_t status;
445
446 PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL);
447 PJ_ASSERT_RETURN(g722_data != NULL, PJ_EINVALIDOP);
448
449 status = g722_enc_init(&g722_data->encoder);
450 if (status != PJ_SUCCESS) {
451 TRACE_((THIS_FILE, "g722_enc_init() failed, status=%d", status));
452 pj_mutex_unlock(g722_codec_factory.mutex);
453 return PJMEDIA_CODEC_EFAILED;
454 }
455
456 status = g722_dec_init(&g722_data->decoder);
457 if (status != PJ_SUCCESS) {
458 TRACE_((THIS_FILE, "g722_dec_init() failed, status=%d", status));
459 pj_mutex_unlock(g722_codec_factory.mutex);
460 return PJMEDIA_CODEC_EFAILED;
461 }
462
463 g722_data->vad_enabled = (attr->setting.vad != 0);
464 g722_data->plc_enabled = (attr->setting.plc != 0);
465 g722_data->pcm_shift_val = 1 << (pj_int16_t)g722_codec_factory.pcm_shift;
466 g722_data->pcm_max = 0x7FFF / g722_data->pcm_shift_val;
467 g722_data->pcm_min = -0x7FFF / g722_data->pcm_shift_val - 1;
468
469 TRACE_((THIS_FILE, "G722 codec opened: vad=%d, plc=%d",
470 g722_data->vad_enabled, g722_data->plc_enabled));
471 return PJ_SUCCESS;
472 }
473
474 /*
475 * Close codec.
476 */
477 static pj_status_t g722_codec_close( pjmedia_codec *codec )
478 {
479 /* The codec, encoder, and decoder will be reused, so there's
480 * nothing to do here
481 */
482
483 PJ_UNUSED_ARG(codec);
484
485 TRACE_((THIS_FILE, "G722 codec closed"));
486 return PJ_SUCCESS;
487 }
488
489
490 /*
491 * Modify codec settings.
492 */
493 static pj_status_t g722_codec_modify(pjmedia_codec *codec,
494 const pjmedia_codec_param *attr )
495 {
496 struct g722_data *g722_data = (struct g722_data*) codec->codec_data;
497
498 pj_assert(g722_data != NULL);
499
500 g722_data->vad_enabled = (attr->setting.vad != 0);
501 g722_data->plc_enabled = (attr->setting.plc != 0);
502
503 TRACE_((THIS_FILE, "G722 codec modified: vad=%d, plc=%d",
504 g722_data->vad_enabled, g722_data->plc_enabled));
505 return PJ_SUCCESS;
506 }
507
508
509 /*
510 * Get frames in the packet.
511 */
512 static pj_status_t g722_codec_parse(pjmedia_codec *codec,
513 void *pkt,
514 pj_size_t pkt_size,
515 const pj_timestamp *ts,
516 unsigned *frame_cnt,
517 pjmedia_frame frames[])
518 {
519 unsigned count = 0;
520
521 PJ_UNUSED_ARG(codec);
522
523 PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
524
525 TRACE_((THIS_FILE, "G722 parse(): input len=%d", pkt_size));
526
527 while (pkt_size >= FRAME_LEN && count < *frame_cnt) {
528 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
529 frames[count].buf = pkt;
530 frames[count].size = FRAME_LEN;
531 frames[count].timestamp.u64 = ts->u64 + count * SAMPLES_PER_FRAME;
532
533 pkt = ((char*)pkt) + FRAME_LEN;
534 pkt_size -= FRAME_LEN;
535
536 ++count;
537 }
538
539 TRACE_((THIS_FILE, "G722 parse(): got %d frames", count));
540
541 *frame_cnt = count;
542 return PJ_SUCCESS;
543 }
544
545 /*
546 * Encode frame.
547 */
548 static pj_status_t g722_codec_encode(pjmedia_codec *codec,
549 const struct pjmedia_frame *input,
550 unsigned output_buf_len,
551 struct pjmedia_frame *output)
552 {
553 struct g722_data *g722_data = (struct g722_data*) codec->codec_data;
554 pj_status_t status;
555
556 pj_assert(g722_data && input && output);
557
558 PJ_ASSERT_RETURN((input->size >> 2) <= output_buf_len,
559 PJMEDIA_CODEC_EFRMTOOSHORT);
560
561 /* Detect silence */
562 if (g722_data->vad_enabled) {
563 pj_bool_t is_silence;
564 pj_int32_t silence_duration;
565
566 silence_duration = pj_timestamp_diff32(&g722_data->last_tx,
567 &input->timestamp);
568
569 is_silence = pjmedia_silence_det_detect(g722_data->vad,
570 (const pj_int16_t*) input->buf,
571 (input->size >> 1),
572 NULL);
573 if (is_silence &&
574 (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
575 silence_duration < PJMEDIA_CODEC_MAX_SILENCE_PERIOD*16000/1000))
576 {
577 output->type = PJMEDIA_FRAME_TYPE_NONE;
578 output->buf = NULL;
579 output->size = 0;
580 output->timestamp = input->timestamp;
581 return PJ_SUCCESS;
582 } else {
583 g722_data->last_tx = input->timestamp;
584 }
585 }
586
587 /* Adjust input signal level from 16-bit to 14-bit */
588 if (g722_data->pcm_shift_val > 1) {
589 pj_int16_t *p, *end;
590
591 p = (pj_int16_t*)input->buf;
592 end = p + input->size/2;
593 while (p < end) {
594 *p = *p / g722_data->pcm_shift_val;
595 ++p;
596 }
597 }
598
599 /* Encode to temporary buffer */
600 output->size = output_buf_len;
601 status = g722_enc_encode(&g722_data->encoder, (pj_int16_t*)input->buf,
602 (input->size >> 1), output->buf, &output->size);
603 if (status != PJ_SUCCESS) {
604 output->size = 0;
605 output->buf = NULL;
606 output->type = PJMEDIA_FRAME_TYPE_NONE;
607 TRACE_((THIS_FILE, "G722 encode() status: %d", status));
608 return PJMEDIA_CODEC_EFAILED;
609 }
610
611 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
612 output->timestamp = input->timestamp;
613
614 TRACE_((THIS_FILE, "G722 encode(): size=%d", output->size));
615 return PJ_SUCCESS;
616 }
617
618 /*
619 * Decode frame.
620 */
621 static pj_status_t g722_codec_decode(pjmedia_codec *codec,
622 const struct pjmedia_frame *input,
623 unsigned output_buf_len,
624 struct pjmedia_frame *output)
625 {
626 struct g722_data *g722_data = (struct g722_data*) codec->codec_data;
627 pj_status_t status;
628
629 pj_assert(g722_data != NULL);
630 PJ_ASSERT_RETURN(input && output, PJ_EINVAL);
631
632 TRACE_((THIS_FILE, "G722 decode(): inbuf=%p, insize=%d, outbuf=%p,"
633 "outsize=%d",
634 input->buf, input->size, output->buf, output_buf_len));
635
636 if (output_buf_len < SAMPLES_PER_FRAME * 2) {
637 TRACE_((THIS_FILE, "G722 decode() ERROR: PJMEDIA_CODEC_EPCMTOOSHORT"));
638 return PJMEDIA_CODEC_EPCMTOOSHORT;
639 }
640
641 if (input->size != FRAME_LEN) {
642 TRACE_((THIS_FILE, "G722 decode() ERROR: PJMEDIA_CODEC_EFRMTOOSHORT"));
643 return PJMEDIA_CODEC_EFRMTOOSHORT;
644 }
645
646
647 /* Decode */
648 output->size = SAMPLES_PER_FRAME;
649 status = g722_dec_decode(&g722_data->decoder, input->buf, input->size,
650 (pj_int16_t*)output->buf, &output->size);
651 if (status != PJ_SUCCESS) {
652 TRACE_((THIS_FILE, "G722 decode() status: %d", status));
653 return PJMEDIA_CODEC_EFAILED;
654 }
655
656 pj_assert(output->size == SAMPLES_PER_FRAME);
657
658 /* Adjust input signal level from 14-bit to 16-bit */
659 if (g722_data->pcm_shift_val > 1) {
660 pj_int16_t *p, *end;
661
662 p = (pj_int16_t*)output->buf;
663 end = p + output->size;
664 while (p < end) {
665 #if PJMEDIA_G722_STOP_PCM_SHIFT_ON_CLIPPING
666 /* If there is clipping, reduce/stop the PCM shifting */
667 if (*p < g722_data->pcm_min || *p > g722_data->pcm_max) {
668 g722_data->pcm_shift_val /= 2;
669 g722_data->pcm_max = 0x7FFF/g722_data->pcm_shift_val;
670 g722_data->pcm_min = -0x7FFF/g722_data->pcm_shift_val-1;
671 break;
672 }
673 #else
674 /* Avoid PCM shift overflow if we don't stop on clipping */
675 if (*p < g722_data->pcm_min)
676 *p = g722_data->pcm_min;
677 else if (*p > g722_data->pcm_max)
678 *p = g722_data->pcm_max;
679
680 #endif
681 *p = *p * g722_data->pcm_shift_val;
682 ++p;
683 }
684 }
685
686 output->size = SAMPLES_PER_FRAME * 2;
687 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
688 output->timestamp = input->timestamp;
689
690 #if !PLC_DISABLED
691 if (g722_data->plc_enabled)
692 pjmedia_plc_save(g722_data->plc, (pj_int16_t*)output->buf);
693 #endif
694
695 TRACE_((THIS_FILE, "G722 decode done"));
696 return PJ_SUCCESS;
697 }
698
699
700 #if !PLC_DISABLED
701 /*
702 * Recover lost frame.
703 */
704 static pj_status_t g722_codec_recover(pjmedia_codec *codec,
705 unsigned output_buf_len,
706 struct pjmedia_frame *output)
707 {
708 struct g722_data *g722_data = (struct g722_data*)codec->codec_data;
709
710 PJ_ASSERT_RETURN(g722_data->plc_enabled, PJ_EINVALIDOP);
711
712 PJ_ASSERT_RETURN(output_buf_len >= SAMPLES_PER_FRAME * 2,
713 PJMEDIA_CODEC_EPCMTOOSHORT);
714
715 pjmedia_plc_generate(g722_data->plc, (pj_int16_t*)output->buf);
716
717 output->size = SAMPLES_PER_FRAME * 2;
718 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
719
720 return PJ_SUCCESS;
721 }
722 #endif
723
724 #endif // PJMEDIA_HAS_G722_CODEC
725