amr_helper.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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. #ifndef __PJMEDIA_CODECS_AMR_HELPER_H__
  20. #define __PJMEDIA_CODECS_AMR_HELPER_H__
  21. /**
  22. * @file pjmedia-codec/amr_helper.h
  23. * @brief Common tables and helper functions for AMR codec (NB & WB).
  24. */
  25. #ifdef _MSC_VER
  26. # pragma warning(disable:4214) // bit field types other than int
  27. #endif
  28. /**
  29. * @defgroup PJMED_AMR_CODEC_HELPER AMR Codec Helper
  30. * @ingroup PJMEDIA_CODEC_CODECS
  31. * @brief AMR common tables and helper functions.
  32. * @{
  33. *
  34. * This sections describes common AMR constants tables (e.g: bits sensitivity
  35. * order map, frame lengths, bitrates) and helper functions (e.g: pack AMR
  36. * payload in octet-aligned mode or bandwidth-efficient mode, payload parser,
  37. * reorder AMR bitstream).
  38. */
  39. PJ_BEGIN_DECL
  40. /* AMR bits sensitivity order maps */
  41. const pj_int16_t pjmedia_codec_amrnb_ordermap122[244] =
  42. {
  43. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  44. 10, 11, 12, 13, 14, 23, 15, 16, 17, 18,
  45. 19, 20, 21, 22, 24, 25, 26, 27, 28, 38,
  46. 141, 39, 142, 40, 143, 41, 144, 42, 145, 43,
  47. 146, 44, 147, 45, 148, 46, 149, 47, 97, 150,
  48. 200, 48, 98, 151, 201, 49, 99, 152, 202, 86,
  49. 136, 189, 239, 87, 137, 190, 240, 88, 138, 191,
  50. 241, 91, 194, 92, 195, 93, 196, 94, 197, 95,
  51. 198, 29, 30, 31, 32, 33, 34, 35, 50, 100,
  52. 153, 203, 89, 139, 192, 242, 51, 101, 154, 204,
  53. 55, 105, 158, 208, 90, 140, 193, 243, 59, 109,
  54. 162, 212, 63, 113, 166, 216, 67, 117, 170, 220,
  55. 36, 37, 54, 53, 52, 58, 57, 56, 62, 61,
  56. 60, 66, 65, 64, 70, 69, 68, 104, 103, 102,
  57. 108, 107, 106, 112, 111, 110, 116, 115, 114, 120,
  58. 119, 118, 157, 156, 155, 161, 160, 159, 165, 164,
  59. 163, 169, 168, 167, 173, 172, 171, 207, 206, 205,
  60. 211, 210, 209, 215, 214, 213, 219, 218, 217, 223,
  61. 222, 221, 73, 72, 71, 76, 75, 74, 79, 78,
  62. 77, 82, 81, 80, 85, 84, 83, 123, 122, 121,
  63. 126, 125, 124, 129, 128, 127, 132, 131, 130, 135,
  64. 134, 133, 176, 175, 174, 179, 178, 177, 182, 181,
  65. 180, 185, 184, 183, 188, 187, 186, 226, 225, 224,
  66. 229, 228, 227, 232, 231, 230, 235, 234, 233, 238,
  67. 237, 236, 96, 199
  68. };
  69. const pj_int16_t pjmedia_codec_amrnb_ordermap102[204] =
  70. {
  71. 7, 6, 5, 4, 3, 2, 1, 0, 16, 15,
  72. 14, 13, 12, 11, 10, 9, 8, 26, 27, 28,
  73. 29, 30, 31, 115, 116, 117, 118, 119, 120, 72,
  74. 73, 161, 162, 65, 68, 69, 108, 111, 112, 154,
  75. 157, 158, 197, 200, 201, 32, 33, 121, 122, 74,
  76. 75, 163, 164, 66, 109, 155, 198, 19, 23, 21,
  77. 22, 18, 17, 20, 24, 25, 37, 36, 35, 34,
  78. 80, 79, 78, 77, 126, 125, 124, 123, 169, 168,
  79. 167, 166, 70, 67, 71, 113, 110, 114, 159, 156,
  80. 160, 202, 199, 203, 76, 165, 81, 82, 92, 91,
  81. 93, 83, 95, 85, 84, 94, 101, 102, 96, 104,
  82. 86, 103, 87, 97, 127, 128, 138, 137, 139, 129,
  83. 141, 131, 130, 140, 147, 148, 142, 150, 132, 149,
  84. 133, 143, 170, 171, 181, 180, 182, 172, 184, 174,
  85. 173, 183, 190, 191, 185, 193, 175, 192, 176, 186,
  86. 38, 39, 49, 48, 50, 40, 52, 42, 41, 51,
  87. 58, 59, 53, 61, 43, 60, 44, 54, 194, 179,
  88. 189, 196, 177, 195, 178, 187, 188, 151, 136, 146,
  89. 153, 134, 152, 135, 144, 145, 105, 90, 100, 107,
  90. 88, 106, 89, 98, 99, 62, 47, 57, 64, 45,
  91. 63, 46, 55, 56
  92. };
  93. const pj_int16_t pjmedia_codec_amrnb_ordermap795[159] =
  94. {
  95. 8, 7, 6, 5, 4, 3, 2, 14, 16, 9,
  96. 10, 12, 13, 15, 11, 17, 20, 22, 24, 23,
  97. 19, 18, 21, 56, 88, 122, 154, 57, 89, 123,
  98. 155, 58, 90, 124, 156, 52, 84, 118, 150, 53,
  99. 85, 119, 151, 27, 93, 28, 94, 29, 95, 30,
  100. 96, 31, 97, 61, 127, 62, 128, 63, 129, 59,
  101. 91, 125, 157, 32, 98, 64, 130, 1, 0, 25,
  102. 26, 33, 99, 34, 100, 65, 131, 66, 132, 54,
  103. 86, 120, 152, 60, 92, 126, 158, 55, 87, 121,
  104. 153, 117, 116, 115, 46, 78, 112, 144, 43, 75,
  105. 109, 141, 40, 72, 106, 138, 36, 68, 102, 134,
  106. 114, 149, 148, 147, 146, 83, 82, 81, 80, 51,
  107. 50, 49, 48, 47, 45, 44, 42, 39, 35, 79,
  108. 77, 76, 74, 71, 67, 113, 111, 110, 108, 105,
  109. 101, 145, 143, 142, 140, 137, 133, 41, 73, 107,
  110. 139, 37, 69, 103, 135, 38, 70, 104, 136
  111. };
  112. const pj_int16_t pjmedia_codec_amrnb_ordermap74[148] =
  113. {
  114. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  115. 10, 11, 12, 13, 14, 15, 16, 26, 87, 27,
  116. 88, 28, 89, 29, 90, 30, 91, 51, 80, 112,
  117. 141, 52, 81, 113, 142, 54, 83, 115, 144, 55,
  118. 84, 116, 145, 58, 119, 59, 120, 21, 22, 23,
  119. 17, 18, 19, 31, 60, 92, 121, 56, 85, 117,
  120. 146, 20, 24, 25, 50, 79, 111, 140, 57, 86,
  121. 118, 147, 49, 78, 110, 139, 48, 77, 53, 82,
  122. 114, 143, 109, 138, 47, 76, 108, 137, 32, 33,
  123. 61, 62, 93, 94, 122, 123, 41, 42, 43, 44,
  124. 45, 46, 70, 71, 72, 73, 74, 75, 102, 103,
  125. 104, 105, 106, 107, 131, 132, 133, 134, 135, 136,
  126. 34, 63, 95, 124, 35, 64, 96, 125, 36, 65,
  127. 97, 126, 37, 66, 98, 127, 38, 67, 99, 128,
  128. 39, 68, 100, 129, 40, 69, 101, 130
  129. };
  130. const pj_int16_t pjmedia_codec_amrnb_ordermap67[134] =
  131. {
  132. 0, 1, 4, 3, 5, 6, 13, 7, 2, 8,
  133. 9, 11, 15, 12, 14, 10, 28, 82, 29, 83,
  134. 27, 81, 26, 80, 30, 84, 16, 55, 109, 56,
  135. 110, 31, 85, 57, 111, 48, 73, 102, 127, 32,
  136. 86, 51, 76, 105, 130, 52, 77, 106, 131, 58,
  137. 112, 33, 87, 19, 23, 53, 78, 107, 132, 21,
  138. 22, 18, 17, 20, 24, 25, 50, 75, 104, 129,
  139. 47, 72, 101, 126, 54, 79, 108, 133, 46, 71,
  140. 100, 125, 128, 103, 74, 49, 45, 70, 99, 124,
  141. 42, 67, 96, 121, 39, 64, 93, 118, 38, 63,
  142. 92, 117, 35, 60, 89, 114, 34, 59, 88, 113,
  143. 44, 69, 98, 123, 43, 68, 97, 122, 41, 66,
  144. 95, 120, 40, 65, 94, 119, 37, 62, 91, 116,
  145. 36, 61, 90, 115
  146. };
  147. const pj_int16_t pjmedia_codec_amrnb_ordermap59[118] =
  148. {
  149. 0, 1, 4, 5, 3, 6, 7, 2, 13, 15,
  150. 8, 9, 11, 12, 14, 10, 16, 28, 74, 29,
  151. 75, 27, 73, 26, 72, 30, 76, 51, 97, 50,
  152. 71, 96, 117, 31, 77, 52, 98, 49, 70, 95,
  153. 116, 53, 99, 32, 78, 33, 79, 48, 69, 94,
  154. 115, 47, 68, 93, 114, 46, 67, 92, 113, 19,
  155. 21, 23, 22, 18, 17, 20, 24, 111, 43, 89,
  156. 110, 64, 65, 44, 90, 25, 45, 66, 91, 112,
  157. 54, 100, 40, 61, 86, 107, 39, 60, 85, 106,
  158. 36, 57, 82, 103, 35, 56, 81, 102, 34, 55,
  159. 80, 101, 42, 63, 88, 109, 41, 62, 87, 108,
  160. 38, 59, 84, 105, 37, 58, 83, 104
  161. };
  162. const pj_int16_t pjmedia_codec_amrnb_ordermap515[103] =
  163. {
  164. 7, 6, 5, 4, 3, 2, 1, 0, 15, 14,
  165. 13, 12, 11, 10, 9, 8, 23, 24, 25, 26,
  166. 27, 46, 65, 84, 45, 44, 43, 64, 63, 62,
  167. 83, 82, 81, 102, 101, 100, 42, 61, 80, 99,
  168. 28, 47, 66, 85, 18, 41, 60, 79, 98, 29,
  169. 48, 67, 17, 20, 22, 40, 59, 78, 97, 21,
  170. 30, 49, 68, 86, 19, 16, 87, 39, 38, 58,
  171. 57, 77, 35, 54, 73, 92, 76, 96, 95, 36,
  172. 55, 74, 93, 32, 51, 33, 52, 70, 71, 89,
  173. 90, 31, 50, 69, 88, 37, 56, 75, 94, 34,
  174. 53, 72, 91
  175. };
  176. const pj_int16_t pjmedia_codec_amrnb_ordermap475[95] =
  177. {
  178. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  179. 10, 11, 12, 13, 14, 15, 23, 24, 25, 26,
  180. 27, 28, 48, 49, 61, 62, 82, 83, 47, 46,
  181. 45, 44, 81, 80, 79, 78, 17, 18, 20, 22,
  182. 77, 76, 75, 74, 29, 30, 43, 42, 41, 40,
  183. 38, 39, 16, 19, 21, 50, 51, 59, 60, 63,
  184. 64, 72, 73, 84, 85, 93, 94, 32, 33, 35,
  185. 36, 53, 54, 56, 57, 66, 67, 69, 70, 87,
  186. 88, 90, 91, 34, 55, 68, 89, 37, 58, 71,
  187. 92, 31, 52, 65, 86
  188. };
  189. const pj_int16_t pjmedia_codec_amrwb_ordermap_660[] =
  190. {
  191. 0, 5, 6, 7, 61, 84, 107, 130, 62, 85,
  192. 8, 4, 37, 38, 39, 40, 58, 81, 104, 127,
  193. 60, 83, 106, 129, 108, 131, 128, 41, 42, 80,
  194. 126, 1, 3, 57, 103, 82, 105, 59, 2, 63,
  195. 109, 110, 86, 19, 22, 23, 64, 87, 18, 20,
  196. 21, 17, 13, 88, 43, 89, 65, 111, 14, 24,
  197. 25, 26, 27, 28, 15, 16, 44, 90, 66, 112,
  198. 9, 11, 10, 12, 67, 113, 29, 30, 31, 32,
  199. 34, 33, 35, 36, 45, 51, 68, 74, 91, 97,
  200. 114, 120, 46, 69, 92, 115, 52, 75, 98, 121,
  201. 47, 70, 93, 116, 53, 76, 99, 122, 48, 71,
  202. 94, 117, 54, 77, 100, 123, 49, 72, 95, 118,
  203. 55, 78, 101, 124, 50, 73, 96, 119, 56, 79,
  204. 102, 125
  205. };
  206. const pj_int16_t pjmedia_codec_amrwb_ordermap_885[] =
  207. {
  208. 0, 4, 6, 7, 5, 3, 47, 48, 49, 112,
  209. 113, 114, 75, 106, 140, 171, 80, 111, 145, 176,
  210. 77, 108, 142, 173, 78, 109, 143, 174, 79, 110,
  211. 144, 175, 76, 107, 141, 172, 50, 115, 51, 2,
  212. 1, 81, 116, 146, 19, 21, 12, 17, 18, 20,
  213. 16, 25, 13, 10, 14, 24, 23, 22, 26, 8,
  214. 15, 52, 117, 31, 82, 147, 9, 33, 11, 83,
  215. 148, 53, 118, 28, 27, 84, 149, 34, 35, 29,
  216. 46, 32, 30, 54, 119, 37, 36, 39, 38, 40,
  217. 85, 150, 41, 42, 43, 44, 45, 55, 60, 65,
  218. 70, 86, 91, 96, 101, 120, 125, 130, 135, 151,
  219. 156, 161, 166, 56, 87, 121, 152, 61, 92, 126,
  220. 157, 66, 97, 131, 162, 71, 102, 136, 167, 57,
  221. 88, 122, 153, 62, 93, 127, 158, 67, 98, 132,
  222. 163, 72, 103, 137, 168, 58, 89, 123, 154, 63,
  223. 94, 128, 159, 68, 99, 133, 164, 73, 104, 138,
  224. 169, 59, 90, 124, 155, 64, 95, 129, 160, 69,
  225. 100, 134, 165, 74, 105, 139, 170
  226. };
  227. const pj_int16_t pjmedia_codec_amrwb_ordermap_1265[] =
  228. {
  229. 0, 4, 6, 93, 143, 196, 246, 7, 5, 3,
  230. 47, 48, 49, 50, 51, 150, 151, 152, 153, 154,
  231. 94, 144, 197, 247, 99, 149, 202, 252, 96, 146,
  232. 199, 249, 97, 147, 200, 250, 100, 203, 98, 148,
  233. 201, 251, 95, 145, 198, 248, 52, 2, 1, 101,
  234. 204, 155, 19, 21, 12, 17, 18, 20, 16, 25,
  235. 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
  236. 156, 31, 102, 205, 9, 33, 11, 103, 206, 54,
  237. 157, 28, 27, 104, 207, 34, 35, 29, 46, 32,
  238. 30, 55, 158, 37, 36, 39, 38, 40, 105, 208,
  239. 41, 42, 43, 44, 45, 56, 106, 159, 209, 57,
  240. 66, 75, 84, 107, 116, 125, 134, 160, 169, 178,
  241. 187, 210, 219, 228, 237, 58, 108, 161, 211, 62,
  242. 112, 165, 215, 67, 117, 170, 220, 71, 121, 174,
  243. 224, 76, 126, 179, 229, 80, 130, 183, 233, 85,
  244. 135, 188, 238, 89, 139, 192, 242, 59, 109, 162,
  245. 212, 63, 113, 166, 216, 68, 118, 171, 221, 72,
  246. 122, 175, 225, 77, 127, 180, 230, 81, 131, 184,
  247. 234, 86, 136, 189, 239, 90, 140, 193, 243, 60,
  248. 110, 163, 213, 64, 114, 167, 217, 69, 119, 172,
  249. 222, 73, 123, 176, 226, 78, 128, 181, 231, 82,
  250. 132, 185, 235, 87, 137, 190, 240, 91, 141, 194,
  251. 244, 61, 111, 164, 214, 65, 115, 168, 218, 70,
  252. 120, 173, 223, 74, 124, 177, 227, 79, 129, 182,
  253. 232, 83, 133, 186, 236, 88, 138, 191, 241, 92,
  254. 142, 195, 245
  255. };
  256. const pj_int16_t pjmedia_codec_amrwb_ordermap_1425[] =
  257. {
  258. 0, 4, 6, 101, 159, 220, 278, 7, 5, 3,
  259. 47, 48, 49, 50, 51, 166, 167, 168, 169, 170,
  260. 102, 160, 221, 279, 107, 165, 226, 284, 104, 162,
  261. 223, 281, 105, 163, 224, 282, 108, 227, 106, 164,
  262. 225, 283, 103, 161, 222, 280, 52, 2, 1, 109,
  263. 228, 171, 19, 21, 12, 17, 18, 20, 16, 25,
  264. 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
  265. 172, 31, 110, 229, 9, 33, 11, 111, 230, 54,
  266. 173, 28, 27, 112, 231, 34, 35, 29, 46, 32,
  267. 30, 55, 174, 37, 36, 39, 38, 40, 113, 232,
  268. 41, 42, 43, 44, 45, 56, 114, 175, 233, 62,
  269. 120, 181, 239, 75, 133, 194, 252, 57, 115, 176,
  270. 234, 63, 121, 182, 240, 70, 128, 189, 247, 76,
  271. 134, 195, 253, 83, 141, 202, 260, 92, 150, 211,
  272. 269, 84, 142, 203, 261, 93, 151, 212, 270, 85,
  273. 143, 204, 262, 94, 152, 213, 271, 86, 144, 205,
  274. 263, 95, 153, 214, 272, 64, 122, 183, 241, 77,
  275. 135, 196, 254, 65, 123, 184, 242, 78, 136, 197,
  276. 255, 87, 145, 206, 264, 96, 154, 215, 273, 58,
  277. 116, 177, 235, 66, 124, 185, 243, 71, 129, 190,
  278. 248, 79, 137, 198, 256, 88, 146, 207, 265, 97,
  279. 155, 216, 274, 59, 117, 178, 236, 67, 125, 186,
  280. 244, 72, 130, 191, 249, 80, 138, 199, 257, 89,
  281. 147, 208, 266, 98, 156, 217, 275, 60, 118, 179,
  282. 237, 68, 126, 187, 245, 73, 131, 192, 250, 81,
  283. 139, 200, 258, 90, 148, 209, 267, 99, 157, 218,
  284. 276, 61, 119, 180, 238, 69, 127, 188, 246, 74,
  285. 132, 193, 251, 82, 140, 201, 259, 91, 149, 210,
  286. 268, 100, 158, 219, 277
  287. };
  288. const pj_int16_t pjmedia_codec_amrwb_ordermap_1585[] =
  289. {
  290. 0, 4, 6, 109, 175, 244, 310, 7, 5, 3,
  291. 47, 48, 49, 50, 51, 182, 183, 184, 185, 186,
  292. 110, 176, 245, 311, 115, 181, 250, 316, 112, 178,
  293. 247, 313, 113, 179, 248, 314, 116, 251, 114, 180,
  294. 249, 315, 111, 177, 246, 312, 52, 2, 1, 117,
  295. 252, 187, 19, 21, 12, 17, 18, 20, 16, 25,
  296. 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
  297. 188, 31, 118, 253, 9, 33, 11, 119, 254, 54,
  298. 189, 28, 27, 120, 255, 34, 35, 29, 46, 32,
  299. 30, 55, 190, 37, 36, 39, 38, 40, 121, 256,
  300. 41, 42, 43, 44, 45, 56, 122, 191, 257, 63,
  301. 129, 198, 264, 76, 142, 211, 277, 89, 155, 224,
  302. 290, 102, 168, 237, 303, 57, 123, 192, 258, 70,
  303. 136, 205, 271, 83, 149, 218, 284, 96, 162, 231,
  304. 297, 62, 128, 197, 263, 75, 141, 210, 276, 88,
  305. 154, 223, 289, 101, 167, 236, 302, 58, 124, 193,
  306. 259, 71, 137, 206, 272, 84, 150, 219, 285, 97,
  307. 163, 232, 298, 59, 125, 194, 260, 64, 130, 199,
  308. 265, 67, 133, 202, 268, 72, 138, 207, 273, 77,
  309. 143, 212, 278, 80, 146, 215, 281, 85, 151, 220,
  310. 286, 90, 156, 225, 291, 93, 159, 228, 294, 98,
  311. 164, 233, 299, 103, 169, 238, 304, 106, 172, 241,
  312. 307, 60, 126, 195, 261, 65, 131, 200, 266, 68,
  313. 134, 203, 269, 73, 139, 208, 274, 78, 144, 213,
  314. 279, 81, 147, 216, 282, 86, 152, 221, 287, 91,
  315. 157, 226, 292, 94, 160, 229, 295, 99, 165, 234,
  316. 300, 104, 170, 239, 305, 107, 173, 242, 308, 61,
  317. 127, 196, 262, 66, 132, 201, 267, 69, 135, 204,
  318. 270, 74, 140, 209, 275, 79, 145, 214, 280, 82,
  319. 148, 217, 283, 87, 153, 222, 288, 92, 158, 227,
  320. 293, 95, 161, 230, 296, 100, 166, 235, 301, 105,
  321. 171, 240, 306, 108, 174, 243, 309
  322. };
  323. const pj_int16_t pjmedia_codec_amrwb_ordermap_1825[] =
  324. {
  325. 0, 4, 6, 121, 199, 280, 358, 7, 5, 3,
  326. 47, 48, 49, 50, 51, 206, 207, 208, 209, 210,
  327. 122, 200, 281, 359, 127, 205, 286, 364, 124, 202,
  328. 283, 361, 125, 203, 284, 362, 128, 287, 126, 204,
  329. 285, 363, 123, 201, 282, 360, 52, 2, 1, 129,
  330. 288, 211, 19, 21, 12, 17, 18, 20, 16, 25,
  331. 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
  332. 212, 31, 130, 289, 9, 33, 11, 131, 290, 54,
  333. 213, 28, 27, 132, 291, 34, 35, 29, 46, 32,
  334. 30, 55, 214, 37, 36, 39, 38, 40, 133, 292,
  335. 41, 42, 43, 44, 45, 56, 134, 215, 293, 198,
  336. 299, 136, 120, 138, 60, 279, 58, 62, 357, 139,
  337. 140, 295, 156, 57, 219, 297, 63, 217, 137, 170,
  338. 300, 222, 64, 106, 61, 78, 294, 92, 142, 141,
  339. 135, 221, 296, 301, 343, 59, 298, 184, 329, 315,
  340. 220, 216, 265, 251, 218, 237, 352, 223, 157, 86,
  341. 171, 87, 164, 351, 111, 302, 65, 178, 115, 323,
  342. 72, 192, 101, 179, 93, 73, 193, 151, 337, 309,
  343. 143, 274, 69, 324, 165, 150, 97, 338, 110, 310,
  344. 330, 273, 68, 107, 175, 245, 114, 79, 113, 189,
  345. 246, 259, 174, 71, 185, 96, 344, 100, 322, 83,
  346. 334, 316, 333, 252, 161, 348, 147, 82, 269, 232,
  347. 260, 308, 353, 347, 163, 231, 306, 320, 188, 270,
  348. 146, 177, 266, 350, 256, 85, 149, 116, 191, 160,
  349. 238, 258, 336, 305, 255, 88, 224, 99, 339, 230,
  350. 228, 227, 272, 242, 241, 319, 233, 311, 102, 74,
  351. 180, 275, 66, 194, 152, 325, 172, 247, 244, 261,
  352. 117, 158, 166, 354, 75, 144, 108, 312, 94, 186,
  353. 303, 80, 234, 89, 195, 112, 340, 181, 345, 317,
  354. 326, 276, 239, 167, 118, 313, 70, 355, 327, 253,
  355. 190, 176, 271, 104, 98, 153, 103, 90, 76, 267,
  356. 277, 248, 225, 262, 182, 84, 154, 235, 335, 168,
  357. 331, 196, 341, 249, 162, 307, 148, 349, 263, 321,
  358. 257, 243, 229, 356, 159, 119, 67, 187, 173, 145,
  359. 240, 77, 304, 332, 314, 342, 109, 254, 81, 278,
  360. 105, 91, 346, 318, 183, 250, 197, 328, 95, 155,
  361. 169, 268, 226, 236, 264
  362. };
  363. const pj_int16_t pjmedia_codec_amrwb_ordermap_1985[] =
  364. {
  365. 0, 4, 6, 129, 215, 304, 390, 7, 5, 3,
  366. 47, 48, 49, 50, 51, 222, 223, 224, 225, 226,
  367. 130, 216, 305, 391, 135, 221, 310, 396, 132, 218,
  368. 307, 393, 133, 219, 308, 394, 136, 311, 134, 220,
  369. 309, 395, 131, 217, 306, 392, 52, 2, 1, 137,
  370. 312, 227, 19, 21, 12, 17, 18, 20, 16, 25,
  371. 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
  372. 228, 31, 138, 313, 9, 33, 11, 139, 314, 54,
  373. 229, 28, 27, 140, 315, 34, 35, 29, 46, 32,
  374. 30, 55, 230, 37, 36, 39, 38, 40, 141, 316,
  375. 41, 42, 43, 44, 45, 56, 142, 231, 317, 63,
  376. 73, 92, 340, 82, 324, 149, 353, 159, 334, 165,
  377. 338, 178, 163, 254, 77, 168, 257, 153, 343, 57,
  378. 248, 238, 79, 252, 166, 67, 80, 201, 101, 267,
  379. 143, 164, 341, 255, 339, 187, 376, 318, 78, 328,
  380. 362, 115, 232, 242, 253, 290, 276, 62, 58, 158,
  381. 68, 93, 179, 319, 148, 169, 154, 72, 385, 329,
  382. 333, 344, 102, 83, 144, 233, 323, 124, 243, 192,
  383. 354, 237, 64, 247, 202, 209, 150, 116, 335, 268,
  384. 239, 299, 188, 196, 298, 94, 195, 258, 123, 363,
  385. 384, 109, 325, 371, 170, 370, 84, 110, 295, 180,
  386. 74, 210, 191, 106, 291, 205, 367, 381, 377, 206,
  387. 355, 122, 119, 120, 383, 160, 105, 108, 277, 380,
  388. 294, 284, 285, 345, 208, 269, 249, 366, 386, 300,
  389. 297, 259, 125, 369, 197, 97, 194, 286, 211, 281,
  390. 280, 183, 372, 87, 155, 283, 59, 348, 327, 184,
  391. 76, 111, 330, 203, 349, 69, 98, 152, 145, 189,
  392. 66, 320, 337, 173, 358, 251, 198, 174, 263, 262,
  393. 126, 241, 193, 88, 388, 117, 95, 387, 112, 359,
  394. 287, 244, 103, 272, 301, 171, 162, 234, 273, 127,
  395. 373, 181, 292, 85, 378, 302, 121, 107, 364, 346,
  396. 356, 212, 278, 213, 65, 382, 288, 207, 113, 175,
  397. 99, 296, 374, 368, 199, 260, 185, 336, 331, 161,
  398. 270, 264, 250, 240, 75, 350, 151, 60, 89, 321,
  399. 156, 274, 360, 326, 70, 282, 167, 146, 352, 81,
  400. 91, 389, 266, 245, 177, 235, 190, 256, 204, 342,
  401. 128, 118, 303, 104, 379, 182, 114, 375, 200, 96,
  402. 293, 172, 214, 365, 279, 86, 289, 351, 347, 357,
  403. 261, 186, 176, 271, 90, 100, 147, 322, 275, 361,
  404. 71, 332, 61, 265, 157, 246, 236
  405. };
  406. const pj_int16_t pjmedia_codec_amrwb_ordermap_2305[] =
  407. {
  408. 0, 4, 6, 145, 247, 352, 454, 7, 5, 3,
  409. 47, 48, 49, 50, 51, 254, 255, 256, 257, 258,
  410. 146, 248, 353, 455, 151, 253, 358, 460, 148, 250,
  411. 355, 457, 149, 251, 356, 458, 152, 359, 150, 252,
  412. 357, 459, 147, 249, 354, 456, 52, 2, 1, 153,
  413. 360, 259, 19, 21, 12, 17, 18, 20, 16, 25,
  414. 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
  415. 260, 31, 154, 361, 9, 33, 11, 155, 362, 54,
  416. 261, 28, 27, 156, 363, 34, 35, 29, 46, 32,
  417. 30, 55, 262, 37, 36, 39, 38, 40, 157, 364,
  418. 41, 42, 43, 44, 45, 56, 158, 263, 365, 181,
  419. 192, 170, 79, 57, 399, 90, 159, 297, 377, 366,
  420. 275, 68, 183, 388, 286, 194, 299, 92, 70, 182,
  421. 401, 172, 59, 91, 58, 400, 368, 161, 81, 160,
  422. 264, 171, 80, 389, 390, 378, 379, 193, 298, 69,
  423. 266, 265, 367, 277, 288, 276, 287, 184, 60, 195,
  424. 82, 93, 71, 369, 402, 173, 162, 444, 300, 391,
  425. 98, 76, 278, 61, 267, 374, 135, 411, 167, 102,
  426. 380, 200, 87, 178, 65, 94, 204, 124, 72, 342,
  427. 189, 305, 381, 396, 433, 301, 226, 407, 289, 237,
  428. 113, 215, 185, 128, 309, 403, 116, 320, 196, 331,
  429. 370, 422, 174, 64, 392, 83, 425, 219, 134, 188,
  430. 432, 112, 427, 139, 279, 163, 436, 208, 447, 218,
  431. 236, 229, 97, 294, 385, 230, 166, 268, 177, 443,
  432. 225, 426, 101, 272, 138, 127, 290, 117, 347, 199,
  433. 414, 95, 140, 240, 410, 395, 209, 129, 283, 346,
  434. 105, 241, 437, 86, 308, 448, 203, 345, 186, 107,
  435. 220, 415, 334, 319, 106, 313, 118, 123, 73, 207,
  436. 421, 214, 384, 373, 438, 62, 371, 341, 75, 449,
  437. 168, 323, 164, 242, 416, 324, 304, 197, 335, 404,
  438. 271, 63, 191, 325, 96, 169, 231, 280, 312, 187,
  439. 406, 84, 201, 100, 67, 382, 175, 336, 202, 330,
  440. 269, 393, 376, 383, 293, 307, 409, 179, 285, 314,
  441. 302, 372, 398, 190, 180, 89, 99, 103, 232, 78,
  442. 88, 77, 136, 387, 165, 198, 394, 125, 176, 428,
  443. 74, 375, 238, 227, 66, 273, 282, 141, 306, 412,
  444. 114, 85, 130, 348, 119, 291, 296, 386, 233, 397,
  445. 303, 405, 284, 445, 423, 221, 210, 205, 450, 108,
  446. 274, 434, 216, 343, 337, 142, 243, 321, 408, 451,
  447. 310, 292, 120, 109, 281, 439, 270, 429, 332, 295,
  448. 418, 211, 315, 222, 326, 131, 430, 244, 327, 349,
  449. 417, 316, 143, 338, 440, 234, 110, 212, 452, 245,
  450. 121, 419, 350, 223, 132, 441, 328, 413, 317, 339,
  451. 126, 104, 137, 446, 344, 239, 435, 115, 333, 206,
  452. 322, 217, 228, 424, 453, 311, 351, 111, 442, 224,
  453. 213, 122, 431, 340, 235, 246, 133, 144, 420, 329,
  454. 318
  455. };
  456. const pj_int16_t pjmedia_codec_amrwb_ordermap_2385[] =
  457. {
  458. 0, 4, 6, 145, 251, 360, 466, 7, 5, 3,
  459. 47, 48, 49, 50, 51, 262, 263, 264, 265, 266,
  460. 146, 252, 361, 467, 151, 257, 366, 472, 148, 254,
  461. 363, 469, 149, 255, 364, 470, 156, 371, 150, 256,
  462. 365, 471, 147, 253, 362, 468, 52, 2, 1, 157,
  463. 372, 267, 19, 21, 12, 17, 18, 20, 16, 25,
  464. 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
  465. 268, 31, 152, 153, 154, 155, 258, 259, 260, 261,
  466. 367, 368, 369, 370, 473, 474, 475, 476, 158, 373,
  467. 9, 33, 11, 159, 374, 54, 269, 28, 27, 160,
  468. 375, 34, 35, 29, 46, 32, 30, 55, 270, 37,
  469. 36, 39, 38, 40, 161, 376, 41, 42, 43, 44,
  470. 45, 56, 162, 271, 377, 185, 196, 174, 79, 57,
  471. 411, 90, 163, 305, 389, 378, 283, 68, 187, 400,
  472. 294, 198, 307, 92, 70, 186, 413, 176, 59, 91,
  473. 58, 412, 380, 165, 81, 164, 272, 175, 80, 401,
  474. 402, 390, 391, 197, 306, 69, 274, 273, 379, 285,
  475. 296, 284, 295, 188, 60, 199, 82, 93, 71, 381,
  476. 414, 177, 166, 456, 308, 403, 98, 76, 286, 61,
  477. 275, 386, 135, 423, 171, 102, 392, 204, 87, 182,
  478. 65, 94, 208, 124, 72, 350, 193, 313, 393, 408,
  479. 445, 309, 230, 419, 297, 241, 113, 219, 189, 128,
  480. 317, 415, 116, 328, 200, 339, 382, 434, 178, 64,
  481. 404, 83, 437, 223, 134, 192, 444, 112, 439, 139,
  482. 287, 167, 448, 212, 459, 222, 240, 233, 97, 302,
  483. 397, 234, 170, 276, 181, 455, 229, 438, 101, 280,
  484. 138, 127, 298, 117, 355, 203, 426, 95, 140, 244,
  485. 422, 407, 213, 129, 291, 354, 105, 245, 449, 86,
  486. 316, 460, 207, 353, 190, 107, 224, 427, 342, 327,
  487. 106, 321, 118, 123, 73, 211, 433, 218, 396, 385,
  488. 450, 62, 383, 349, 75, 461, 172, 331, 168, 246,
  489. 428, 332, 312, 201, 343, 416, 279, 63, 195, 333,
  490. 96, 173, 235, 288, 320, 191, 418, 84, 205, 100,
  491. 67, 394, 179, 344, 206, 338, 277, 405, 388, 395,
  492. 301, 315, 421, 183, 293, 322, 310, 384, 410, 194,
  493. 184, 89, 99, 103, 236, 78, 88, 77, 136, 399,
  494. 169, 202, 406, 125, 180, 440, 74, 387, 242, 231,
  495. 66, 281, 290, 141, 314, 424, 114, 85, 130, 356,
  496. 119, 299, 304, 398, 237, 409, 311, 417, 292, 457,
  497. 435, 225, 214, 209, 462, 108, 282, 446, 220, 351,
  498. 345, 142, 247, 329, 420, 463, 318, 300, 120, 109,
  499. 289, 451, 278, 441, 340, 303, 430, 215, 323, 226,
  500. 334, 131, 442, 248, 335, 357, 429, 324, 143, 346,
  501. 452, 238, 110, 216, 464, 249, 121, 431, 358, 227,
  502. 132, 453, 336, 425, 325, 347, 126, 104, 137, 458,
  503. 352, 243, 447, 115, 341, 210, 330, 221, 232, 436,
  504. 465, 319, 359, 111, 454, 228, 217, 122, 443, 348,
  505. 239, 250, 133, 144, 432, 337, 326
  506. };
  507. /**
  508. * AMR-NB bitstream sensitivity order maps.
  509. */
  510. const pj_int16_t* const pjmedia_codec_amrnb_ordermaps[8] =
  511. {
  512. pjmedia_codec_amrnb_ordermap475,
  513. pjmedia_codec_amrnb_ordermap515,
  514. pjmedia_codec_amrnb_ordermap59,
  515. pjmedia_codec_amrnb_ordermap67,
  516. pjmedia_codec_amrnb_ordermap74,
  517. pjmedia_codec_amrnb_ordermap795,
  518. pjmedia_codec_amrnb_ordermap102,
  519. pjmedia_codec_amrnb_ordermap122
  520. };
  521. /**
  522. * AMR-WB bitstream sensitivity order maps.
  523. */
  524. const pj_int16_t* const pjmedia_codec_amrwb_ordermaps[9] =
  525. {
  526. pjmedia_codec_amrwb_ordermap_660,
  527. pjmedia_codec_amrwb_ordermap_885,
  528. pjmedia_codec_amrwb_ordermap_1265,
  529. pjmedia_codec_amrwb_ordermap_1425,
  530. pjmedia_codec_amrwb_ordermap_1585,
  531. pjmedia_codec_amrwb_ordermap_1825,
  532. pjmedia_codec_amrwb_ordermap_1985,
  533. pjmedia_codec_amrwb_ordermap_2305,
  534. pjmedia_codec_amrwb_ordermap_2385
  535. };
  536. /**
  537. * Constant of AMR-NB frame lengths in bytes.
  538. */
  539. const pj_uint8_t pjmedia_codec_amrnb_framelen[16] =
  540. {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  541. /**
  542. * Constant of AMR-NB frame lengths in bits.
  543. */
  544. const pj_uint16_t pjmedia_codec_amrnb_framelenbits[9] =
  545. {95, 103, 118, 134, 148, 159, 204, 244, 39};
  546. /**
  547. * Constant of AMR-NB bitrates.
  548. */
  549. const pj_uint16_t pjmedia_codec_amrnb_bitrates[8] =
  550. {4750, 5150, 5900, 6700, 7400, 7950, 10200, 12200};
  551. /**
  552. * Constant of AMR-WB frame lengths in bytes.
  553. */
  554. const pj_uint8_t pjmedia_codec_amrwb_framelen[16] =
  555. {17, 23, 32, 37, 40, 46, 50, 58, 60, 5, 0, 0, 0, 0, 0, 0};
  556. /**
  557. * Constant of AMR-WB frame lengths in bits.
  558. */
  559. const pj_uint16_t pjmedia_codec_amrwb_framelenbits[10] =
  560. {132, 177, 253, 285, 317, 365, 397, 461, 477, 40};
  561. /**
  562. * Constant of AMR-WB bitrates.
  563. */
  564. const pj_uint16_t pjmedia_codec_amrwb_bitrates[9] =
  565. {6600, 8850, 12650, 14250, 15850, 18250, 19850, 23050, 23850};
  566. /**
  567. * This structure describes AMR frame info, to be fitted into #pjmedia_frame
  568. * bit info.
  569. */
  570. #pragma pack(1)
  571. typedef struct pjmedia_codec_amr_bit_info {
  572. pj_uint8_t frame_type; /**< AMR frame type. */
  573. pj_int8_t mode; /**< AMR mode. */
  574. pj_uint8_t start_bit; /**< Frame start bit. */
  575. pj_uint8_t good_quality:1; /**< Flag if frame is good/degraded. */
  576. pj_uint8_t STI:1; /**< STI mode (first/update). */
  577. } pjmedia_codec_amr_bit_info;
  578. #pragma pack()
  579. /**
  580. * This structure describes AMR settings.
  581. */
  582. typedef struct pjmedia_codec_amr_pack_setting {
  583. pj_uint8_t amr_nb:1; /**< Set 1 for AMR-NB, 0 for AMR-WB. */
  584. pj_uint8_t reorder:1; /**< Reorder bitstream into descending
  585. sensitivity order or vice versa. */
  586. pj_uint8_t octet_aligned:1; /**< TRUE if payload is in octet-aligned mode,
  587. FALSE if payload is in bandwidth
  588. efficient mode. */
  589. pj_uint8_t cmr:4; /**< Change Mode Request for remote
  590. encoder. */
  591. } pjmedia_codec_amr_pack_setting;
  592. /**
  593. * Get AMR mode based on bitrate.
  594. *
  595. * @param bitrate AMR bitrate.
  596. *
  597. * @return AMR mode.
  598. */
  599. PJ_INLINE(pj_int8_t) pjmedia_codec_amr_get_mode(unsigned bitrate)
  600. {
  601. pj_int8_t mode = -1;
  602. if(bitrate==4750){
  603. mode = 0;
  604. } else if(bitrate==5150){
  605. mode = 1;
  606. } else if(bitrate==5900){
  607. mode = 2;
  608. } else if(bitrate==6700){
  609. mode = 3;
  610. } else if(bitrate==7400){
  611. mode = 4;
  612. } else if(bitrate==7950){
  613. mode = 5;
  614. } else if(bitrate==10200){
  615. mode = 6;
  616. } else if(bitrate==12200){
  617. mode = 7;
  618. /* AMRWB */
  619. } else if(bitrate==6600){
  620. mode = 0;
  621. } else if(bitrate==8850){
  622. mode = 1;
  623. } else if(bitrate==12650){
  624. mode = 2;
  625. } else if(bitrate==14250){
  626. mode = 3;
  627. } else if(bitrate==15850){
  628. mode = 4;
  629. } else if(bitrate==18250){
  630. mode = 5;
  631. } else if(bitrate==19850){
  632. mode = 6;
  633. } else if(bitrate==23050){
  634. mode = 7;
  635. } else if(bitrate==23850){
  636. mode = 8;
  637. }
  638. return mode;
  639. }
  640. /**
  641. * Get AMR mode based on frame length.
  642. *
  643. * @param amrnb Set to PJ_TRUE for AMR-NB domain or PJ_FALSE for AMR-WB.
  644. * @param frame_len The frame length.
  645. *
  646. * @return AMR mode.
  647. */
  648. PJ_INLINE(pj_int8_t) pjmedia_codec_amr_get_mode2(pj_bool_t amrnb,
  649. unsigned frame_len)
  650. {
  651. int i;
  652. if (amrnb) {
  653. for (i = 0; i < 9; ++i)
  654. if (frame_len == pjmedia_codec_amrnb_framelen[i])
  655. return (pj_int8_t)i;
  656. } else {
  657. for (i = 0; i < 10; ++i) {
  658. if (frame_len == pjmedia_codec_amrwb_framelen[i])
  659. return (pj_int8_t)i;
  660. }
  661. }
  662. pj_assert(!"Invalid AMR frame length");
  663. return -1;
  664. }
  665. /**
  666. * Prepare a frame before pass it to decoder. This function will do:
  667. * - reorder AMR bitstream from descending sensitivity order into
  668. * encoder bits order. This can be enabled/disabled via param
  669. * 'setting' by setting/resetting field 'reorder'.
  670. * - align left the start bit (make the start_bit to be 0).
  671. *
  672. * @param in Input frame.
  673. * @param setting Settings, see #pjmedia_codec_amr_pack_setting.
  674. * @param out Output frame.
  675. *
  676. * @return PJ_SUCCESS on success.
  677. */
  678. PJ_INLINE(pj_status_t) pjmedia_codec_amr_predecode(
  679. const pjmedia_frame *in,
  680. const pjmedia_codec_amr_pack_setting *setting,
  681. pjmedia_frame *out)
  682. {
  683. pj_int8_t amr_bits[477 + 7] = {0};
  684. pj_int8_t *p_amr_bits = &amr_bits[0];
  685. pj_uint8_t *r = (pj_uint8_t*) in->buf; /* read cursor */
  686. pj_uint8_t *w = (pj_uint8_t*) out->buf; /* write cursor */
  687. /* env vars for AMR or AMRWB */
  688. pj_uint8_t SID_FT;
  689. const pj_uint8_t *framelen_tbl;
  690. const pj_uint16_t *framelenbit_tbl;
  691. const pj_uint16_t *bitrate_tbl;
  692. const pj_int16_t* const *order_maps;
  693. pjmedia_codec_amr_bit_info *in_info =
  694. (pjmedia_codec_amr_bit_info*) &in->bit_info;
  695. pjmedia_codec_amr_bit_info *out_info =
  696. (pjmedia_codec_amr_bit_info*) &out->bit_info;
  697. unsigned i;
  698. *out_info = *in_info;
  699. if (setting->amr_nb) {
  700. SID_FT = 8;
  701. framelen_tbl = pjmedia_codec_amrnb_framelen;
  702. framelenbit_tbl = pjmedia_codec_amrnb_framelenbits;
  703. bitrate_tbl = pjmedia_codec_amrnb_bitrates;
  704. order_maps = pjmedia_codec_amrnb_ordermaps;
  705. } else {
  706. SID_FT = 9;
  707. framelen_tbl = pjmedia_codec_amrwb_framelen;
  708. framelenbit_tbl = pjmedia_codec_amrwb_framelenbits;
  709. bitrate_tbl = pjmedia_codec_amrwb_bitrates;
  710. order_maps = pjmedia_codec_amrwb_ordermaps;
  711. }
  712. PJ_UNUSED_ARG(bitrate_tbl);
  713. /* unpack AMR bitstream if there is any data */
  714. if (in_info->frame_type <= SID_FT) {
  715. i = 0;
  716. if (in_info->start_bit) {
  717. for (; i < (unsigned)(8-in_info->start_bit); ++i)
  718. *p_amr_bits++ = (pj_uint8_t)
  719. ((*r >> (7-in_info->start_bit-i)) & 1);
  720. ++r;
  721. }
  722. for(; i < framelenbit_tbl[in_info->frame_type]; i += 8, ++r) {
  723. *p_amr_bits++ = (pj_uint8_t)((*r >> 7) & 1);
  724. *p_amr_bits++ = (pj_uint8_t)((*r >> 6) & 1);
  725. *p_amr_bits++ = (pj_uint8_t)((*r >> 5) & 1);
  726. *p_amr_bits++ = (pj_uint8_t)((*r >> 4) & 1);
  727. *p_amr_bits++ = (pj_uint8_t)((*r >> 3) & 1);
  728. *p_amr_bits++ = (pj_uint8_t)((*r >> 2) & 1);
  729. *p_amr_bits++ = (pj_uint8_t)((*r >> 1) & 1);
  730. *p_amr_bits++ = (pj_uint8_t)((*r ) & 1);
  731. }
  732. }
  733. if (in_info->frame_type < SID_FT) {
  734. /* Speech */
  735. out_info->mode = in_info->frame_type;
  736. out->size = framelen_tbl[out_info->mode];
  737. pj_bzero(out->buf, out->size);
  738. if (setting->reorder) {
  739. const pj_int16_t *order_map;
  740. order_map = order_maps[out_info->mode];
  741. for(i = 0; i < framelenbit_tbl[out_info->mode]; ++i) {
  742. if (amr_bits[i]) {
  743. pj_uint16_t bitpos;
  744. bitpos = order_map[i];
  745. w[bitpos>>3] |= 1 << (7 - (bitpos % 8));
  746. }
  747. }
  748. } else {
  749. for(i = 0; i < framelenbit_tbl[out_info->mode]; ++i) {
  750. if (amr_bits[i])
  751. w[i >> 3] |= 1 << (7 - (i % 8));
  752. }
  753. }
  754. } else if (in_info->frame_type == SID_FT) {
  755. /* SID */
  756. pj_uint8_t w_bitptr = 0;
  757. pj_uint8_t FT_;
  758. if (setting->amr_nb)
  759. FT_ = (pj_uint8_t)((amr_bits[36] << 2) | (amr_bits[37] << 1) |
  760. amr_bits[38]);
  761. else
  762. FT_ = (pj_uint8_t)((amr_bits[36] << 3) | (amr_bits[37] << 2) |
  763. (amr_bits[38] << 1) | amr_bits[39]);
  764. out_info->mode = FT_;
  765. out->size = 5;
  766. pj_bzero(out->buf, out->size);
  767. for(i = 0; i < framelenbit_tbl[SID_FT]; ++i) {
  768. if (amr_bits[i])
  769. *w |= (1 << (7-w_bitptr));
  770. if (++w_bitptr == 8) {
  771. ++w;
  772. w_bitptr = 0;
  773. }
  774. }
  775. } else {
  776. /* NO DATA */
  777. out->size = 0;
  778. out_info->mode = -1;
  779. }
  780. out_info->start_bit = 0;
  781. return PJ_SUCCESS;
  782. }
  783. /**
  784. * Pack encoded AMR frame(s) into an RTP payload.
  785. *
  786. * @param frames AMR frames to be packed.
  787. * @param nframes Number of frames to be packed.
  788. * @param setting Settings, see #pjmedia_codec_amr_pack_setting.
  789. * @param pkt Payload.
  790. * @param pkt_size Payload size, as input this specifies payload maximum size,
  791. * as output this specifies payload packed size.
  792. *
  793. * @return PJ_SUCCESS on success.
  794. */
  795. PJ_INLINE (pj_status_t) pjmedia_codec_amr_pack(
  796. const pjmedia_frame frames[],
  797. unsigned nframes,
  798. const pjmedia_codec_amr_pack_setting *setting,
  799. void *pkt,
  800. pj_size_t *pkt_size)
  801. {
  802. /* Write cursor */
  803. pj_uint8_t *w = (pj_uint8_t*)pkt;
  804. pj_uint8_t w_bitptr = 0;
  805. /* Read cursor */
  806. pj_uint8_t *r;
  807. /* env vars for AMR or AMRWB */
  808. pj_uint8_t SID_FT;
  809. const pj_uint8_t *framelen_tbl;
  810. const pj_uint16_t *framelenbit_tbl;
  811. const pj_uint16_t *bitrate_tbl;
  812. const pj_int16_t* const *order_maps;
  813. /* frame info */
  814. pjmedia_codec_amr_bit_info *info;
  815. unsigned i, max_pkt_size;
  816. max_pkt_size = (unsigned)*pkt_size;
  817. if (setting->amr_nb) {
  818. SID_FT = 8;
  819. framelen_tbl = pjmedia_codec_amrnb_framelen;
  820. framelenbit_tbl = pjmedia_codec_amrnb_framelenbits;
  821. bitrate_tbl = pjmedia_codec_amrnb_bitrates;
  822. order_maps = pjmedia_codec_amrnb_ordermaps;
  823. } else {
  824. SID_FT = 9;
  825. framelen_tbl = pjmedia_codec_amrwb_framelen;
  826. framelenbit_tbl = pjmedia_codec_amrwb_framelenbits;
  827. bitrate_tbl = pjmedia_codec_amrwb_bitrates;
  828. order_maps = pjmedia_codec_amrwb_ordermaps;
  829. }
  830. PJ_UNUSED_ARG(bitrate_tbl);
  831. /* Code Mode Request, 4 bits */
  832. *w = (pj_uint8_t)(setting->cmr << 4);
  833. w_bitptr = 4;
  834. if (setting->octet_aligned) {
  835. ++w;
  836. w_bitptr = 0;
  837. }
  838. /* Table Of Contents, 6 bits each */
  839. for (i = 0; i < nframes; ++i) {
  840. pj_uint8_t TOC, FT, Q;
  841. pj_bool_t F;
  842. info = (pjmedia_codec_amr_bit_info*)&frames[i].bit_info;
  843. F = (i != nframes-1);
  844. FT = info->frame_type;
  845. Q = (pj_uint8_t)(info->good_quality == 1);
  846. pj_assert(FT <= SID_FT || FT == 14 || FT == 15);
  847. /* Check buffer availability */
  848. *pkt_size = w - (pj_uint8_t*)pkt + 1;
  849. PJ_ASSERT_RETURN(*pkt_size <= max_pkt_size, PJ_ETOOSMALL);
  850. TOC = (pj_uint8_t)((F<<5) | (FT<<1) | Q);
  851. if (w_bitptr == 0) {
  852. *w = (pj_uint8_t)(TOC<<2);
  853. w_bitptr = 6;
  854. } else if (w_bitptr == 2) {
  855. *w++ |= TOC;
  856. w_bitptr = 0;
  857. } else if (w_bitptr == 4) {
  858. *w++ |= TOC>>2;
  859. *w = (pj_uint8_t)(TOC<<6);
  860. w_bitptr = 2;
  861. } else if (w_bitptr == 6) {
  862. *w++ |= TOC>>4;
  863. *w = (pj_uint8_t)(TOC<<4);
  864. w_bitptr = 4;
  865. }
  866. if (setting->octet_aligned) {
  867. ++w;
  868. w_bitptr = 0;
  869. }
  870. }
  871. /* Encoded data */
  872. for (i = 0; i < nframes; ++i) {
  873. pj_int8_t amr_bits[477 + 7] = {0};
  874. pj_int8_t *p_amr_bits = &amr_bits[0];
  875. unsigned j;
  876. info = (pjmedia_codec_amr_bit_info*)&frames[i].bit_info;
  877. /* Check buffer availability */
  878. *pkt_size = w - (pj_uint8_t*)pkt;
  879. if (info->frame_type <= SID_FT)
  880. *pkt_size += framelen_tbl[info->frame_type] + 1;
  881. PJ_ASSERT_RETURN(*pkt_size <= max_pkt_size, PJ_ETOOSMALL);
  882. /* Skip if there is no data */
  883. if (info->frame_type > SID_FT)
  884. continue;
  885. /* Unpack bits */
  886. r = (pj_uint8_t*) frames[i].buf;
  887. j = 0;
  888. if (info->start_bit) {
  889. for (; j < (unsigned)(8-info->start_bit); ++j)
  890. *p_amr_bits++ = (pj_uint8_t)
  891. ((*r >> (7-info->start_bit-j)) & 1);
  892. ++r;
  893. }
  894. for(; j < framelenbit_tbl[info->frame_type]; j+=8, ++r) {
  895. *p_amr_bits++ = (pj_uint8_t)((*r >> 7) & 1);
  896. *p_amr_bits++ = (pj_uint8_t)((*r >> 6) & 1);
  897. *p_amr_bits++ = (pj_uint8_t)((*r >> 5) & 1);
  898. *p_amr_bits++ = (pj_uint8_t)((*r >> 4) & 1);
  899. *p_amr_bits++ = (pj_uint8_t)((*r >> 3) & 1);
  900. *p_amr_bits++ = (pj_uint8_t)((*r >> 2) & 1);
  901. *p_amr_bits++ = (pj_uint8_t)((*r >> 1) & 1);
  902. *p_amr_bits++ = (pj_uint8_t)((*r ) & 1);
  903. }
  904. if (info->frame_type < SID_FT) {
  905. /* Speech */
  906. if (w_bitptr == 0) *w = 0;
  907. if (setting->reorder) {
  908. const pj_int16_t *order_map;
  909. /* Put bits in the packet, sensitivity descending ordered */
  910. order_map = order_maps[info->frame_type];
  911. for(j = 0; j < framelenbit_tbl[info->frame_type]; ++j) {
  912. if (amr_bits[order_map[j]])
  913. *w |= (1 << (7-w_bitptr));
  914. if (++w_bitptr == 8) {
  915. w_bitptr = 0;
  916. ++w;
  917. *w = 0;
  918. }
  919. }
  920. } else {
  921. for(j = 0; j < framelenbit_tbl[info->frame_type]; ++j) {
  922. if (amr_bits[j])
  923. *w |= (1 << (7-w_bitptr));
  924. if (++w_bitptr == 8) {
  925. w_bitptr = 0;
  926. ++w;
  927. *w = 0;
  928. }
  929. }
  930. }
  931. } else if (info->frame_type == SID_FT) {
  932. /* SID */
  933. amr_bits[35] |= info->STI;
  934. if (setting->amr_nb) {
  935. amr_bits[36] = (pj_uint8_t)((info->mode >> 2) & 1);
  936. amr_bits[37] = (pj_uint8_t)((info->mode >> 1) & 1);
  937. amr_bits[38] = (pj_uint8_t)((info->mode) & 1);
  938. } else {
  939. amr_bits[36] = (pj_uint8_t)((info->mode >> 3) & 1);
  940. amr_bits[37] = (pj_uint8_t)((info->mode >> 2) & 1);
  941. amr_bits[38] = (pj_uint8_t)((info->mode >> 1) & 1);
  942. amr_bits[39] = (pj_uint8_t)((info->mode) & 1);
  943. }
  944. if (w_bitptr == 0) *w = 0;
  945. for(j = 0; j < framelenbit_tbl[info->frame_type]; ++j) {
  946. if (amr_bits[j])
  947. *w |= (1 << (7-w_bitptr));
  948. if (++w_bitptr == 8) {
  949. w_bitptr = 0;
  950. ++w;
  951. *w = 0;
  952. }
  953. }
  954. }
  955. if (setting->octet_aligned) {
  956. ++w;
  957. w_bitptr = 0;
  958. }
  959. }
  960. *pkt_size = w - (pj_uint8_t*)pkt;
  961. if (w_bitptr)
  962. *pkt_size += 1;
  963. return PJ_SUCCESS;
  964. }
  965. /**
  966. * Parse AMR payload into frames.
  967. *
  968. * @param pkt Payload.
  969. * @param pkt_size Payload size.
  970. * @param ts Base timestamp.
  971. * @param setting Settings, see #pjmedia_codec_amr_pack_setting.
  972. * @param frames Frames parsed.
  973. * @param nframes Number of frames parsed.
  974. * @param cmr Change Mode Request message for local encoder.
  975. *
  976. * @return PJ_SUCCESS on success.
  977. */
  978. PJ_INLINE(pj_status_t) pjmedia_codec_amr_parse(
  979. void *pkt,
  980. pj_size_t pkt_size,
  981. const pj_timestamp *ts,
  982. const pjmedia_codec_amr_pack_setting* setting,
  983. pjmedia_frame frames[],
  984. unsigned *nframes,
  985. pj_uint8_t *cmr)
  986. {
  987. unsigned cnt = 0;
  988. pj_timestamp ts_ = *ts;
  989. /* Read cursor */
  990. pj_uint8_t r_bitptr = 0;
  991. pj_uint8_t *r = (pj_uint8_t*)pkt;
  992. /* env vars for AMR or AMRWB */
  993. pj_uint8_t SID_FT;
  994. const pj_uint8_t *framelen_tbl;
  995. const pj_uint16_t *framelenbit_tbl;
  996. const pj_int16_t* const *order_maps;
  997. /* frame info */
  998. pjmedia_codec_amr_bit_info *info;
  999. if (setting->amr_nb) {
  1000. SID_FT = 8;
  1001. framelen_tbl = pjmedia_codec_amrnb_framelen;
  1002. framelenbit_tbl = pjmedia_codec_amrnb_framelenbits;
  1003. order_maps = pjmedia_codec_amrnb_ordermaps;
  1004. } else {
  1005. SID_FT = 9;
  1006. framelen_tbl = pjmedia_codec_amrwb_framelen;
  1007. framelenbit_tbl = pjmedia_codec_amrwb_framelenbits;
  1008. order_maps = pjmedia_codec_amrwb_ordermaps;
  1009. }
  1010. PJ_UNUSED_ARG(pkt_size);
  1011. PJ_UNUSED_ARG(order_maps);
  1012. /* Code Mode Request, 4 bits */
  1013. *cmr = (pj_uint8_t)((*r >> 4) & 0x0F);
  1014. r_bitptr = 4;
  1015. if (setting->octet_aligned) {
  1016. ++r;
  1017. r_bitptr = 0;
  1018. }
  1019. /* Table Of Contents, 6 bits each */
  1020. for (;;) {
  1021. pj_uint8_t TOC = 0;
  1022. pj_uint8_t F, FT, Q;
  1023. if (r_bitptr == 0) {
  1024. TOC = (pj_uint8_t)(*r >> 2);
  1025. r_bitptr = 6;
  1026. } else if (r_bitptr == 2) {
  1027. TOC = (pj_uint8_t)(*r++ & 0x3F);
  1028. r_bitptr = 0;
  1029. } else if (r_bitptr == 4) {
  1030. TOC = (pj_uint8_t)((*r++ & 0x0f) << 2);
  1031. TOC |= *r >> 6;
  1032. r_bitptr = 2;
  1033. } else if (r_bitptr == 6) {
  1034. TOC = (pj_uint8_t)((*r++ & 0x03) << 4);
  1035. TOC |= *r >> 4;
  1036. r_bitptr = 4;
  1037. }
  1038. F = (pj_uint8_t)(TOC >> 5);
  1039. FT = (pj_uint8_t)((TOC >> 1) & 0x0F);
  1040. Q = (pj_uint8_t)(TOC & 1);
  1041. if (FT > SID_FT && FT < 14) {
  1042. /* Discard the whole packet (RFC4867 4.3.2) */
  1043. //pj_assert(!"Invalid AMR frametype, stream may be corrupted!");
  1044. cnt = 0;
  1045. PJ_LOG(4, ("AMR parser", "Invalid AMR frametype, stream may be "
  1046. "corrupted!"));
  1047. break;
  1048. }
  1049. if (setting->octet_aligned) {
  1050. ++r;
  1051. r_bitptr = 0;
  1052. }
  1053. /* Set frame attributes */
  1054. info = (pjmedia_codec_amr_bit_info*) &frames[cnt].bit_info;
  1055. info->frame_type = FT;
  1056. info->mode = (pj_int8_t)((FT < SID_FT)? FT : -1);
  1057. info->good_quality = (pj_uint8_t)(Q == 1);
  1058. info->start_bit = 0;
  1059. info->STI = 0;
  1060. frames[cnt].timestamp = ts_;
  1061. frames[cnt].type = PJMEDIA_FRAME_TYPE_AUDIO;
  1062. /* AMR frame length is 20ms */
  1063. ts_.u64 += setting->amr_nb? 160 : 320;
  1064. if (++cnt == *nframes || !F)
  1065. break;
  1066. }
  1067. *nframes = cnt;
  1068. cnt = 0;
  1069. /* Speech frames */
  1070. while (cnt < *nframes) {
  1071. pj_uint8_t FT;
  1072. info = (pjmedia_codec_amr_bit_info*) &frames[cnt].bit_info;
  1073. FT = info->frame_type;
  1074. frames[cnt].buf = r;
  1075. info->start_bit = r_bitptr;
  1076. if (FT == SID_FT) {
  1077. unsigned sti_bitptr;
  1078. sti_bitptr = r_bitptr + 35;
  1079. info->STI = (pj_uint8_t)
  1080. (r[sti_bitptr >> 3] >> (7 - (sti_bitptr % 8))) & 1;
  1081. }
  1082. if (setting->octet_aligned) {
  1083. r += framelen_tbl[FT];
  1084. frames[cnt].size = framelen_tbl[FT];
  1085. } else {
  1086. if (FT == 14 || FT == 15) {
  1087. /* NO DATA */
  1088. frames[cnt].size = 0;
  1089. } else {
  1090. unsigned adv_bit;
  1091. adv_bit = framelenbit_tbl[FT] + r_bitptr;
  1092. r += adv_bit >> 3;
  1093. r_bitptr = (pj_uint8_t)(adv_bit % 8);
  1094. frames[cnt].size = adv_bit >> 3;
  1095. if (r_bitptr)
  1096. ++frames[cnt].size;
  1097. }
  1098. }
  1099. ++cnt;
  1100. }
  1101. return PJ_SUCCESS;
  1102. }
  1103. PJ_END_DECL
  1104. /**
  1105. * @}
  1106. */
  1107. #endif /* __PJMEDIA_CODECS_AMR_HELPER_H__ */