You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

375 lines
14KB

  1. /*
  2. * Copyright (c) CMU 1993 Computer Science, Speech Group
  3. * Chengxiang Lu and Alex Hauptmann
  4. * Copyright (c) 2005 Steve Underwood <steveu at coppice.org>
  5. * Copyright (c) 2009 Kenan Gillet
  6. * Copyright (c) 2010 Martin Storsjo
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * G.722 ADPCM audio encoder
  27. */
  28. #include "avcodec.h"
  29. #include "g722.h"
  30. #define FREEZE_INTERVAL 128
  31. /* This is an arbitrary value. Allowing insanely large values leads to strange
  32. problems, so we limit it to a reasonable value */
  33. #define MAX_FRAME_SIZE 32768
  34. /* We clip the value of avctx->trellis to prevent data type overflows and
  35. undefined behavior. Using larger values is insanely slow anyway. */
  36. #define MIN_TRELLIS 0
  37. #define MAX_TRELLIS 16
  38. static av_cold int g722_encode_init(AVCodecContext * avctx)
  39. {
  40. G722Context *c = avctx->priv_data;
  41. if (avctx->channels != 1) {
  42. av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
  43. return AVERROR_INVALIDDATA;
  44. }
  45. c->band[0].scale_factor = 8;
  46. c->band[1].scale_factor = 2;
  47. c->prev_samples_pos = 22;
  48. if (avctx->trellis) {
  49. int frontier = 1 << avctx->trellis;
  50. int max_paths = frontier * FREEZE_INTERVAL;
  51. int i;
  52. for (i = 0; i < 2; i++) {
  53. c->paths[i] = av_mallocz(max_paths * sizeof(**c->paths));
  54. c->node_buf[i] = av_mallocz(2 * frontier * sizeof(**c->node_buf));
  55. c->nodep_buf[i] = av_mallocz(2 * frontier * sizeof(**c->nodep_buf));
  56. }
  57. }
  58. if (avctx->frame_size) {
  59. /* validate frame size */
  60. if (avctx->frame_size & 1 || avctx->frame_size > MAX_FRAME_SIZE) {
  61. int new_frame_size;
  62. if (avctx->frame_size == 1)
  63. new_frame_size = 2;
  64. else if (avctx->frame_size > MAX_FRAME_SIZE)
  65. new_frame_size = MAX_FRAME_SIZE;
  66. else
  67. new_frame_size = avctx->frame_size - 1;
  68. av_log(avctx, AV_LOG_WARNING, "Requested frame size is not "
  69. "allowed. Using %d instead of %d\n", new_frame_size,
  70. avctx->frame_size);
  71. avctx->frame_size = new_frame_size;
  72. }
  73. } else {
  74. /* This is arbitrary. We use 320 because it's 20ms @ 16kHz, which is
  75. a common packet size for VoIP applications */
  76. avctx->frame_size = 320;
  77. }
  78. if (avctx->trellis) {
  79. /* validate trellis */
  80. if (avctx->trellis < MIN_TRELLIS || avctx->trellis > MAX_TRELLIS) {
  81. int new_trellis = av_clip(avctx->trellis, MIN_TRELLIS, MAX_TRELLIS);
  82. av_log(avctx, AV_LOG_WARNING, "Requested trellis value is not "
  83. "allowed. Using %d instead of %d\n", new_trellis,
  84. avctx->trellis);
  85. avctx->trellis = new_trellis;
  86. }
  87. }
  88. return 0;
  89. }
  90. static av_cold int g722_encode_close(AVCodecContext *avctx)
  91. {
  92. G722Context *c = avctx->priv_data;
  93. int i;
  94. for (i = 0; i < 2; i++) {
  95. av_freep(&c->paths[i]);
  96. av_freep(&c->node_buf[i]);
  97. av_freep(&c->nodep_buf[i]);
  98. }
  99. return 0;
  100. }
  101. static const int16_t low_quant[33] = {
  102. 35, 72, 110, 150, 190, 233, 276, 323,
  103. 370, 422, 473, 530, 587, 650, 714, 786,
  104. 858, 940, 1023, 1121, 1219, 1339, 1458, 1612,
  105. 1765, 1980, 2195, 2557, 2919
  106. };
  107. static inline void filter_samples(G722Context *c, const int16_t *samples,
  108. int *xlow, int *xhigh)
  109. {
  110. int xout1, xout2;
  111. c->prev_samples[c->prev_samples_pos++] = samples[0];
  112. c->prev_samples[c->prev_samples_pos++] = samples[1];
  113. ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2);
  114. *xlow = xout1 + xout2 >> 13;
  115. *xhigh = xout1 - xout2 >> 13;
  116. if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
  117. memmove(c->prev_samples,
  118. c->prev_samples + c->prev_samples_pos - 22,
  119. 22 * sizeof(c->prev_samples[0]));
  120. c->prev_samples_pos = 22;
  121. }
  122. }
  123. static inline int encode_high(const struct G722Band *state, int xhigh)
  124. {
  125. int diff = av_clip_int16(xhigh - state->s_predictor);
  126. int pred = 141 * state->scale_factor >> 8;
  127. /* = diff >= 0 ? (diff < pred) + 2 : diff >= -pred */
  128. return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0);
  129. }
  130. static inline int encode_low(const struct G722Band* state, int xlow)
  131. {
  132. int diff = av_clip_int16(xlow - state->s_predictor);
  133. /* = diff >= 0 ? diff : -(diff + 1) */
  134. int limit = diff ^ (diff >> (sizeof(diff)*8-1));
  135. int i = 0;
  136. limit = limit + 1 << 10;
  137. if (limit > low_quant[8] * state->scale_factor)
  138. i = 9;
  139. while (i < 29 && limit > low_quant[i] * state->scale_factor)
  140. i++;
  141. return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
  142. }
  143. static void g722_encode_trellis(G722Context *c, int trellis,
  144. uint8_t *dst, int nb_samples,
  145. const int16_t *samples)
  146. {
  147. int i, j, k;
  148. int frontier = 1 << trellis;
  149. struct TrellisNode **nodes[2];
  150. struct TrellisNode **nodes_next[2];
  151. int pathn[2] = {0, 0}, froze = -1;
  152. struct TrellisPath *p[2];
  153. for (i = 0; i < 2; i++) {
  154. nodes[i] = c->nodep_buf[i];
  155. nodes_next[i] = c->nodep_buf[i] + frontier;
  156. memset(c->nodep_buf[i], 0, 2 * frontier * sizeof(*c->nodep_buf));
  157. nodes[i][0] = c->node_buf[i] + frontier;
  158. nodes[i][0]->ssd = 0;
  159. nodes[i][0]->path = 0;
  160. nodes[i][0]->state = c->band[i];
  161. }
  162. for (i = 0; i < nb_samples >> 1; i++) {
  163. int xlow, xhigh;
  164. struct TrellisNode *next[2];
  165. int heap_pos[2] = {0, 0};
  166. for (j = 0; j < 2; j++) {
  167. next[j] = c->node_buf[j] + frontier*(i & 1);
  168. memset(nodes_next[j], 0, frontier * sizeof(**nodes_next));
  169. }
  170. filter_samples(c, &samples[2*i], &xlow, &xhigh);
  171. for (j = 0; j < frontier && nodes[0][j]; j++) {
  172. /* Only k >> 2 affects the future adaptive state, therefore testing
  173. * small steps that don't change k >> 2 is useless, the original
  174. * value from encode_low is better than them. Since we step k
  175. * in steps of 4, make sure range is a multiple of 4, so that
  176. * we don't miss the original value from encode_low. */
  177. int range = j < frontier/2 ? 4 : 0;
  178. struct TrellisNode *cur_node = nodes[0][j];
  179. int ilow = encode_low(&cur_node->state, xlow);
  180. for (k = ilow - range; k <= ilow + range && k <= 63; k += 4) {
  181. int decoded, dec_diff, pos;
  182. uint32_t ssd;
  183. struct TrellisNode* node;
  184. if (k < 0)
  185. continue;
  186. decoded = av_clip((cur_node->state.scale_factor *
  187. ff_g722_low_inv_quant6[k] >> 10)
  188. + cur_node->state.s_predictor, -16384, 16383);
  189. dec_diff = xlow - decoded;
  190. #define STORE_NODE(index, UPDATE, VALUE)\
  191. ssd = cur_node->ssd + dec_diff*dec_diff;\
  192. /* Check for wraparound. Using 64 bit ssd counters would \
  193. * be simpler, but is slower on x86 32 bit. */\
  194. if (ssd < cur_node->ssd)\
  195. continue;\
  196. if (heap_pos[index] < frontier) {\
  197. pos = heap_pos[index]++;\
  198. assert(pathn[index] < FREEZE_INTERVAL * frontier);\
  199. node = nodes_next[index][pos] = next[index]++;\
  200. node->path = pathn[index]++;\
  201. } else {\
  202. /* Try to replace one of the leaf nodes with the new \
  203. * one, but not always testing the same leaf position */\
  204. pos = (frontier>>1) + (heap_pos[index] & ((frontier>>1) - 1));\
  205. if (ssd >= nodes_next[index][pos]->ssd)\
  206. continue;\
  207. heap_pos[index]++;\
  208. node = nodes_next[index][pos];\
  209. }\
  210. node->ssd = ssd;\
  211. node->state = cur_node->state;\
  212. UPDATE;\
  213. c->paths[index][node->path].value = VALUE;\
  214. c->paths[index][node->path].prev = cur_node->path;\
  215. /* Sift the newly inserted node up in the heap to restore \
  216. * the heap property */\
  217. while (pos > 0) {\
  218. int parent = (pos - 1) >> 1;\
  219. if (nodes_next[index][parent]->ssd <= ssd)\
  220. break;\
  221. FFSWAP(struct TrellisNode*, nodes_next[index][parent],\
  222. nodes_next[index][pos]);\
  223. pos = parent;\
  224. }
  225. STORE_NODE(0, ff_g722_update_low_predictor(&node->state, k >> 2), k);
  226. }
  227. }
  228. for (j = 0; j < frontier && nodes[1][j]; j++) {
  229. int ihigh;
  230. struct TrellisNode *cur_node = nodes[1][j];
  231. /* We don't try to get any initial guess for ihigh via
  232. * encode_high - since there's only 4 possible values, test
  233. * them all. Testing all of these gives a much, much larger
  234. * gain than testing a larger range around ilow. */
  235. for (ihigh = 0; ihigh < 4; ihigh++) {
  236. int dhigh, decoded, dec_diff, pos;
  237. uint32_t ssd;
  238. struct TrellisNode* node;
  239. dhigh = cur_node->state.scale_factor *
  240. ff_g722_high_inv_quant[ihigh] >> 10;
  241. decoded = av_clip(dhigh + cur_node->state.s_predictor,
  242. -16384, 16383);
  243. dec_diff = xhigh - decoded;
  244. STORE_NODE(1, ff_g722_update_high_predictor(&node->state, dhigh, ihigh), ihigh);
  245. }
  246. }
  247. for (j = 0; j < 2; j++) {
  248. FFSWAP(struct TrellisNode**, nodes[j], nodes_next[j]);
  249. if (nodes[j][0]->ssd > (1 << 16)) {
  250. for (k = 1; k < frontier && nodes[j][k]; k++)
  251. nodes[j][k]->ssd -= nodes[j][0]->ssd;
  252. nodes[j][0]->ssd = 0;
  253. }
  254. }
  255. if (i == froze + FREEZE_INTERVAL) {
  256. p[0] = &c->paths[0][nodes[0][0]->path];
  257. p[1] = &c->paths[1][nodes[1][0]->path];
  258. for (j = i; j > froze; j--) {
  259. dst[j] = p[1]->value << 6 | p[0]->value;
  260. p[0] = &c->paths[0][p[0]->prev];
  261. p[1] = &c->paths[1][p[1]->prev];
  262. }
  263. froze = i;
  264. pathn[0] = pathn[1] = 0;
  265. memset(nodes[0] + 1, 0, (frontier - 1)*sizeof(**nodes));
  266. memset(nodes[1] + 1, 0, (frontier - 1)*sizeof(**nodes));
  267. }
  268. }
  269. p[0] = &c->paths[0][nodes[0][0]->path];
  270. p[1] = &c->paths[1][nodes[1][0]->path];
  271. for (j = i; j > froze; j--) {
  272. dst[j] = p[1]->value << 6 | p[0]->value;
  273. p[0] = &c->paths[0][p[0]->prev];
  274. p[1] = &c->paths[1][p[1]->prev];
  275. }
  276. c->band[0] = nodes[0][0]->state;
  277. c->band[1] = nodes[1][0]->state;
  278. }
  279. static av_always_inline void encode_byte(G722Context *c, uint8_t *dst,
  280. const int16_t *samples)
  281. {
  282. int xlow, xhigh, ilow, ihigh;
  283. filter_samples(c, samples, &xlow, &xhigh);
  284. ihigh = encode_high(&c->band[1], xhigh);
  285. ilow = encode_low (&c->band[0], xlow);
  286. ff_g722_update_high_predictor(&c->band[1], c->band[1].scale_factor *
  287. ff_g722_high_inv_quant[ihigh] >> 10, ihigh);
  288. ff_g722_update_low_predictor(&c->band[0], ilow >> 2);
  289. *dst = ihigh << 6 | ilow;
  290. }
  291. static void g722_encode_no_trellis(G722Context *c,
  292. uint8_t *dst, int nb_samples,
  293. const int16_t *samples)
  294. {
  295. int i;
  296. for (i = 0; i < nb_samples; i += 2)
  297. encode_byte(c, dst++, &samples[i]);
  298. }
  299. static int g722_encode_frame(AVCodecContext *avctx,
  300. uint8_t *dst, int buf_size, void *data)
  301. {
  302. G722Context *c = avctx->priv_data;
  303. const int16_t *samples = data;
  304. int nb_samples;
  305. nb_samples = avctx->frame_size - (avctx->frame_size & 1);
  306. if (avctx->trellis)
  307. g722_encode_trellis(c, avctx->trellis, dst, nb_samples, samples);
  308. else
  309. g722_encode_no_trellis(c, dst, nb_samples, samples);
  310. /* handle last frame with odd frame_size */
  311. if (nb_samples < avctx->frame_size) {
  312. int16_t last_samples[2] = { samples[nb_samples], samples[nb_samples] };
  313. encode_byte(c, &dst[nb_samples >> 1], last_samples);
  314. }
  315. return (avctx->frame_size + 1) >> 1;
  316. }
  317. AVCodec ff_adpcm_g722_encoder = {
  318. .name = "g722",
  319. .type = AVMEDIA_TYPE_AUDIO,
  320. .id = CODEC_ID_ADPCM_G722,
  321. .priv_data_size = sizeof(G722Context),
  322. .init = g722_encode_init,
  323. .close = g722_encode_close,
  324. .encode = g722_encode_frame,
  325. .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
  326. .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
  327. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  328. };