g722.c 22 KB

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