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.

332 lines
11KB

  1. /*
  2. * G.729 decoder
  3. * Copyright (c) 2008 Vladimir Voroshilov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdlib.h>
  22. #include <inttypes.h>
  23. #include <limits.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <math.h>
  27. #include <assert.h>
  28. #include "avcodec.h"
  29. #include "libavutil/avutil.h"
  30. #include "get_bits.h"
  31. #include "lsp.h"
  32. #include "celp_math.h"
  33. #include "acelp_filters.h"
  34. #include "acelp_pitch_delay.h"
  35. #include "acelp_vectors.h"
  36. #include "g729data.h"
  37. /**
  38. * minimum quantized LSF value (3.2.4)
  39. * 0.005 in Q13
  40. */
  41. #define LSFQ_MIN 40
  42. /**
  43. * maximum quantized LSF value (3.2.4)
  44. * 3.135 in Q13
  45. */
  46. #define LSFQ_MAX 25681
  47. /**
  48. * minimum LSF distance (3.2.4)
  49. * 0.0391 in Q13
  50. */
  51. #define LSFQ_DIFF_MIN 321
  52. /**
  53. * minimum gain pitch value (3.8, Equation 47)
  54. * 0.2 in (1.14)
  55. */
  56. #define SHARP_MIN 3277
  57. /**
  58. * maximum gain pitch value (3.8, Equation 47)
  59. * (EE) This does not comply with the specification.
  60. * Specification says about 0.8, which should be
  61. * 13107 in (1.14), but reference C code uses
  62. * 13017 (equals to 0.7945) instead of it.
  63. */
  64. #define SHARP_MAX 13017
  65. /**
  66. * subframe size
  67. */
  68. #define SUBFRAME_SIZE 40
  69. typedef struct {
  70. uint8_t ac_index_bits[2]; ///< adaptive codebook index for second subframe (size in bits)
  71. uint8_t parity_bit; ///< parity bit for pitch delay
  72. uint8_t gc_1st_index_bits; ///< gain codebook (first stage) index (size in bits)
  73. uint8_t gc_2nd_index_bits; ///< gain codebook (second stage) index (size in bits)
  74. uint8_t fc_signs_bits; ///< number of pulses in fixed-codebook vector
  75. uint8_t fc_indexes_bits; ///< size (in bits) of fixed-codebook index entry
  76. } G729FormatDescription;
  77. typedef struct {
  78. int pitch_delay_int_prev; ///< integer part of previous subframe's pitch delay (4.1.3)
  79. /// (2.13) LSP quantizer outputs
  80. int16_t past_quantizer_output_buf[MA_NP + 1][10];
  81. int16_t* past_quantizer_outputs[MA_NP + 1];
  82. int16_t lsfq[10]; ///< (2.13) quantized LSF coefficients from previous frame
  83. int16_t lsp_buf[2][10]; ///< (0.15) LSP coefficients (previous and current frames) (3.2.5)
  84. int16_t *lsp[2]; ///< pointers to lsp_buf
  85. } G729Context;
  86. static const G729FormatDescription format_g729_8k = {
  87. .ac_index_bits = {8,5},
  88. .parity_bit = 1,
  89. .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
  90. .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
  91. .fc_signs_bits = 4,
  92. .fc_indexes_bits = 13,
  93. };
  94. static const G729FormatDescription format_g729d_6k4 = {
  95. .ac_index_bits = {8,4},
  96. .parity_bit = 0,
  97. .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
  98. .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
  99. .fc_signs_bits = 2,
  100. .fc_indexes_bits = 9,
  101. };
  102. /**
  103. * \brief pseudo random number generator
  104. */
  105. static inline uint16_t g729_prng(uint16_t value)
  106. {
  107. return 31821 * value + 13849;
  108. }
  109. /**
  110. * Get parity bit of bit 2..7
  111. */
  112. static inline int get_parity(uint8_t value)
  113. {
  114. return (0x6996966996696996ULL >> (value >> 2)) & 1;
  115. }
  116. static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1],
  117. int16_t ma_predictor,
  118. int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high)
  119. {
  120. int i,j;
  121. static const uint8_t min_distance[2]={10, 5}; //(2.13)
  122. int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
  123. for (i = 0; i < 5; i++) {
  124. quantizer_output[i] = cb_lsp_1st[vq_1st][i ] + cb_lsp_2nd[vq_2nd_low ][i ];
  125. quantizer_output[i + 5] = cb_lsp_1st[vq_1st][i + 5] + cb_lsp_2nd[vq_2nd_high][i + 5];
  126. }
  127. for (j = 0; j < 2; j++) {
  128. for (i = 1; i < 10; i++) {
  129. int diff = (quantizer_output[i - 1] - quantizer_output[i] + min_distance[j]) >> 1;
  130. if (diff > 0) {
  131. quantizer_output[i - 1] -= diff;
  132. quantizer_output[i ] += diff;
  133. }
  134. }
  135. }
  136. for (i = 0; i < 10; i++) {
  137. int sum = quantizer_output[i] * cb_ma_predictor_sum[ma_predictor][i];
  138. for (j = 0; j < MA_NP; j++)
  139. sum += past_quantizer_outputs[j][i] * cb_ma_predictor[ma_predictor][j][i];
  140. lsfq[i] = sum >> 15;
  141. }
  142. /* Rotate past_quantizer_outputs. */
  143. memmove(past_quantizer_outputs + 1, past_quantizer_outputs, MA_NP * sizeof(int16_t*));
  144. past_quantizer_outputs[0] = quantizer_output;
  145. ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10);
  146. }
  147. static av_cold int decoder_init(AVCodecContext * avctx)
  148. {
  149. G729Context* ctx = avctx->priv_data;
  150. int i,k;
  151. if (avctx->channels != 1) {
  152. av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels);
  153. return AVERROR(EINVAL);
  154. }
  155. /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
  156. avctx->frame_size = SUBFRAME_SIZE << 1;
  157. for (k = 0; k < MA_NP + 1; k++) {
  158. ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k];
  159. for (i = 1; i < 11; i++)
  160. ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3;
  161. }
  162. ctx->lsp[0] = ctx->lsp_buf[0];
  163. ctx->lsp[1] = ctx->lsp_buf[1];
  164. memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
  165. return 0;
  166. }
  167. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  168. AVPacket *avpkt)
  169. {
  170. const uint8_t *buf = avpkt->data;
  171. int buf_size = avpkt->size;
  172. int16_t *out_frame = data;
  173. GetBitContext gb;
  174. G729FormatDescription format;
  175. int frame_erasure = 0; ///< frame erasure detected during decoding
  176. int bad_pitch = 0; ///< parity check failed
  177. int i;
  178. G729Context *ctx = avctx->priv_data;
  179. int16_t lp[2][11]; // (3.12)
  180. uint8_t ma_predictor; ///< switched MA predictor of LSP quantizer
  181. uint8_t quantizer_1st; ///< first stage vector of quantizer
  182. uint8_t quantizer_2nd_lo; ///< second stage lower vector of quantizer (size in bits)
  183. uint8_t quantizer_2nd_hi; ///< second stage higher vector of quantizer (size in bits)
  184. int pitch_delay_int; // pitch delay, integer part
  185. int pitch_delay_3x; // pitch delay, multiplied by 3
  186. if (*data_size < SUBFRAME_SIZE << 2) {
  187. av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n");
  188. return AVERROR(EIO);
  189. }
  190. if (buf_size == 10) {
  191. format = format_g729_8k;
  192. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
  193. } else if (buf_size == 8) {
  194. format = format_g729d_6k4;
  195. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
  196. } else {
  197. av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
  198. return AVERROR_INVALIDDATA;
  199. }
  200. for (i=0; i < buf_size; i++)
  201. frame_erasure |= buf[i];
  202. frame_erasure = !frame_erasure;
  203. init_get_bits(&gb, buf, buf_size);
  204. ma_predictor = get_bits(&gb, 1);
  205. quantizer_1st = get_bits(&gb, VQ_1ST_BITS);
  206. quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
  207. quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
  208. lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs,
  209. ma_predictor,
  210. quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi);
  211. ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
  212. ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
  213. FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
  214. for (i = 0; i < 2; i++) {
  215. uint8_t ac_index; ///< adaptive codebook index
  216. uint8_t pulses_signs; ///< fixed-codebook vector pulse signs
  217. int fc_indexes; ///< fixed-codebook indexes
  218. uint8_t gc_1st_index; ///< gain codebook (first stage) index
  219. uint8_t gc_2nd_index; ///< gain codebook (second stage) index
  220. ac_index = get_bits(&gb, format.ac_index_bits[i]);
  221. if(!i && format.parity_bit)
  222. bad_pitch = get_parity(ac_index) == get_bits1(&gb);
  223. fc_indexes = get_bits(&gb, format.fc_indexes_bits);
  224. pulses_signs = get_bits(&gb, format.fc_signs_bits);
  225. gc_1st_index = get_bits(&gb, format.gc_1st_index_bits);
  226. gc_2nd_index = get_bits(&gb, format.gc_2nd_index_bits);
  227. if(!i) {
  228. if (bad_pitch)
  229. pitch_delay_3x = 3 * ctx->pitch_delay_int_prev;
  230. else
  231. pitch_delay_3x = ff_acelp_decode_8bit_to_1st_delay3(ac_index);
  232. } else {
  233. int pitch_delay_min = av_clip(ctx->pitch_delay_int_prev - 5,
  234. PITCH_DELAY_MIN, PITCH_DELAY_MAX - 9);
  235. if(packet_type == FORMAT_G729D_6K4)
  236. pitch_delay_3x = ff_acelp_decode_4bit_to_2nd_delay3(ac_index, pitch_delay_min);
  237. else
  238. pitch_delay_3x = ff_acelp_decode_5_6_bit_to_2nd_delay3(ac_index, pitch_delay_min);
  239. }
  240. /* Round pitch delay to nearest (used everywhere except ff_acelp_interpolate). */
  241. pitch_delay_int = (pitch_delay_3x + 1) / 3;
  242. ff_acelp_weighted_vector_sum(fc + pitch_delay_int,
  243. fc + pitch_delay_int,
  244. fc, 1 << 14,
  245. av_clip(ctx->gain_pitch, SHARP_MIN, SHARP_MAX),
  246. 0, 14,
  247. SUBFRAME_SIZE - pitch_delay_int);
  248. if (frame_erasure) {
  249. ctx->gain_pitch = (29491 * ctx->gain_pitch) >> 15; // 0.90 (0.15)
  250. ctx->gain_code = ( 2007 * ctx->gain_code ) >> 11; // 0.98 (0.11)
  251. gain_corr_factor = 0;
  252. } else {
  253. ctx->gain_pitch = cb_gain_1st_8k[gc_1st_index][0] +
  254. cb_gain_2nd_8k[gc_2nd_index][0];
  255. gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
  256. cb_gain_2nd_8k[gc_2nd_index][1];
  257. ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
  258. ctx->exc + i * SUBFRAME_SIZE, fc,
  259. (!voicing && frame_erasure) ? 0 : ctx->gain_pitch,
  260. ( voicing && frame_erasure) ? 0 : ctx->gain_code,
  261. 1 << 13, 14, SUBFRAME_SIZE);
  262. ctx->pitch_delay_int_prev = pitch_delay_int;
  263. }
  264. *data_size = SUBFRAME_SIZE << 2;
  265. return buf_size;
  266. }
  267. AVCodec ff_g729_decoder =
  268. {
  269. "g729",
  270. AVMEDIA_TYPE_AUDIO,
  271. CODEC_ID_G729,
  272. sizeof(G729Context),
  273. decoder_init,
  274. NULL,
  275. NULL,
  276. decode_frame,
  277. .long_name = NULL_IF_CONFIG_SMALL("G.729"),
  278. };