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.

302 lines
10KB

  1. /*
  2. * G.729 decoder
  3. * Copyright (c) 2008 Vladimir Voroshilov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "g729.h"
  32. #include "lsp.h"
  33. #include "celp_math.h"
  34. #include "acelp_filters.h"
  35. #include "acelp_pitch_delay.h"
  36. #include "acelp_vectors.h"
  37. #include "g729data.h"
  38. /**
  39. * minimum quantized LSF value (3.2.4)
  40. * 0.005 in Q13
  41. */
  42. #define LSFQ_MIN 40
  43. /**
  44. * maximum quantized LSF value (3.2.4)
  45. * 3.135 in Q13
  46. */
  47. #define LSFQ_MAX 25681
  48. /**
  49. * minimum LSF distance (3.2.4)
  50. * 0.0391 in Q13
  51. */
  52. #define LSFQ_DIFF_MIN 321
  53. /**
  54. * minimum gain pitch value (3.8, Equation 47)
  55. * 0.2 in (1.14)
  56. */
  57. #define SHARP_MIN 3277
  58. /**
  59. * maximum gain pitch value (3.8, Equation 47)
  60. * (EE) This does not comply with the specification.
  61. * Specification says about 0.8, which should be
  62. * 13107 in (1.14), but reference C code uses
  63. * 13017 (equals to 0.7945) instead of it.
  64. */
  65. #define SHARP_MAX 13017
  66. typedef struct {
  67. uint8_t ac_index_bits[2]; ///< adaptive codebook index for second subframe (size in bits)
  68. uint8_t parity_bit; ///< parity bit for pitch delay
  69. uint8_t gc_1st_index_bits; ///< gain codebook (first stage) index (size in bits)
  70. uint8_t gc_2nd_index_bits; ///< gain codebook (second stage) index (size in bits)
  71. uint8_t fc_signs_bits; ///< number of pulses in fixed-codebook vector
  72. uint8_t fc_indexes_bits; ///< size (in bits) of fixed-codebook index entry
  73. } G729FormatDescription;
  74. typedef struct {
  75. /// (2.13) LSP quantizer outputs
  76. int16_t past_quantizer_output_buf[MA_NP + 1][10];
  77. int16_t* past_quantizer_outputs[MA_NP + 1];
  78. int16_t lsfq[10]; ///< (2.13) quantized LSF coefficients from previous frame
  79. int16_t lsp_buf[2][10]; ///< (0.15) LSP coefficients (previous and current frames) (3.2.5)
  80. int16_t *lsp[2]; ///< pointers to lsp_buf
  81. } G729Context;
  82. static const G729FormatDescription format_g729_8k = {
  83. .ac_index_bits = {8,5},
  84. .parity_bit = 1,
  85. .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
  86. .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
  87. .fc_signs_bits = 4,
  88. .fc_indexes_bits = 13,
  89. };
  90. static const G729FormatDescription format_g729d_6k4 = {
  91. .ac_index_bits = {8,4},
  92. .parity_bit = 0,
  93. .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
  94. .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
  95. .fc_signs_bits = 2,
  96. .fc_indexes_bits = 9,
  97. };
  98. /**
  99. * \brief pseudo random number generator
  100. */
  101. static inline uint16_t g729_prng(uint16_t value)
  102. {
  103. return 31821 * value + 13849;
  104. }
  105. /**
  106. * Get parity bit of bit 2..7
  107. */
  108. static inline int get_parity(uint8_t value)
  109. {
  110. return (0x6996966996696996ULL >> (value >> 2)) & 1;
  111. }
  112. static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1],
  113. int16_t ma_predictor,
  114. int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high)
  115. {
  116. int i,j;
  117. static const uint8_t min_distance[2]={10, 5}; //(2.13)
  118. int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
  119. for (i = 0; i < 5; i++) {
  120. quantizer_output[i] = cb_lsp_1st[vq_1st][i ] + cb_lsp_2nd[vq_2nd_low ][i ];
  121. quantizer_output[i + 5] = cb_lsp_1st[vq_1st][i + 5] + cb_lsp_2nd[vq_2nd_high][i + 5];
  122. }
  123. for (j = 0; j < 2; j++) {
  124. for (i = 1; i < 10; i++) {
  125. int diff = (quantizer_output[i - 1] - quantizer_output[i] + min_distance[j]) >> 1;
  126. if (diff > 0) {
  127. quantizer_output[i - 1] -= diff;
  128. quantizer_output[i ] += diff;
  129. }
  130. }
  131. }
  132. for (i = 0; i < 10; i++) {
  133. int sum = quantizer_output[i] * cb_ma_predictor_sum[ma_predictor][i];
  134. for (j = 0; j < MA_NP; j++)
  135. sum += past_quantizer_outputs[j][i] * cb_ma_predictor[ma_predictor][j][i];
  136. lsfq[i] = sum >> 15;
  137. }
  138. /* Rotate past_quantizer_outputs. */
  139. memmove(past_quantizer_outputs + 1, past_quantizer_outputs, MA_NP * sizeof(int16_t*));
  140. past_quantizer_outputs[0] = quantizer_output;
  141. ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10);
  142. }
  143. static av_cold int decoder_init(AVCodecContext * avctx)
  144. {
  145. G729Context* ctx = avctx->priv_data;
  146. int i,k;
  147. if (avctx->channels != 1) {
  148. av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels);
  149. return AVERROR_NOFMT;
  150. }
  151. /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
  152. avctx->frame_size = SUBFRAME_SIZE << 1;
  153. for (k = 0; k < MA_NP + 1; k++) {
  154. ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k];
  155. for (i = 1; i < 11; i++)
  156. ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3;
  157. }
  158. ctx->lsp[0] = ctx->lsp_buf[0];
  159. ctx->lsp[1] = ctx->lsp_buf[1];
  160. memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
  161. return 0;
  162. }
  163. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  164. AVPacket *avpkt)
  165. {
  166. const uint8_t *buf = avpkt->data;
  167. int buf_size = avpkt->size;
  168. int16_t *out_frame = data;
  169. GetBitContext gb;
  170. G729FormatDescription format;
  171. int frame_erasure = 0; ///< frame erasure detected during decoding
  172. int bad_pitch = 0; ///< parity check failed
  173. int i;
  174. G729Context *ctx = avctx->priv_data;
  175. int16_t lp[2][11]; // (3.12)
  176. uint8_t ma_predictor; ///< switched MA predictor of LSP quantizer
  177. uint8_t quantizer_1st; ///< first stage vector of quantizer
  178. uint8_t quantizer_2nd_lo; ///< second stage lower vector of quantizer (size in bits)
  179. uint8_t quantizer_2nd_hi; ///< second stage higher vector of quantizer (size in bits)
  180. if (*data_size < SUBFRAME_SIZE << 2) {
  181. av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n");
  182. return AVERROR(EIO);
  183. }
  184. if (buf_size == 10) {
  185. format = format_g729_8k;
  186. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
  187. } else if (buf_size == 8) {
  188. format = format_g729d_6k4;
  189. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
  190. } else {
  191. av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
  192. return (AVERROR_NOFMT);
  193. }
  194. for (i=0; i < buf_size; i++)
  195. frame_erasure |= buf[i];
  196. frame_erasure = !frame_erasure;
  197. init_get_bits(&gb, buf, buf_size);
  198. ma_predictor = get_bits(&gb, 1);
  199. quantizer_1st = get_bits(&gb, VQ_1ST_BITS);
  200. quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
  201. quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
  202. lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs,
  203. ma_predictor,
  204. quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi);
  205. ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
  206. ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
  207. FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
  208. for (i = 0; i < 2; i++) {
  209. uint8_t ac_index; ///< adaptive codebook index
  210. uint8_t pulses_signs; ///< fixed-codebook vector pulse signs
  211. int fc_indexes; ///< fixed-codebook indexes
  212. uint8_t gc_1st_index; ///< gain codebook (first stage) index
  213. uint8_t gc_2nd_index; ///< gain codebook (second stage) index
  214. ac_index = get_bits(&gb, format.ac_index_bits[i]);
  215. if(!i && format.parity_bit)
  216. bad_pitch = get_parity(ac_index) == get_bits1(&gb);
  217. fc_indexes = get_bits(&gb, format.fc_indexes_bits);
  218. pulses_signs = get_bits(&gb, format.fc_signs_bits);
  219. gc_1st_index = get_bits(&gb, format.gc_1st_index_bits);
  220. gc_2nd_index = get_bits(&gb, format.gc_2nd_index_bits);
  221. ff_acelp_weighted_vector_sum(fc + pitch_delay_int[i],
  222. fc + pitch_delay_int[i],
  223. fc, 1 << 14,
  224. av_clip(ctx->gain_pitch, SHARP_MIN, SHARP_MAX),
  225. 0, 14,
  226. SUBFRAME_SIZE - pitch_delay_int[i]);
  227. if (frame_erasure) {
  228. ctx->gain_pitch = (29491 * ctx->gain_pitch) >> 15; // 0.90 (0.15)
  229. ctx->gain_code = ( 2007 * ctx->gain_code ) >> 11; // 0.98 (0.11)
  230. gain_corr_factor = 0;
  231. } else {
  232. ctx->gain_pitch = cb_gain_1st_8k[gc_1st_index][0] +
  233. cb_gain_2nd_8k[gc_2nd_index][0];
  234. gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
  235. cb_gain_2nd_8k[gc_2nd_index][1];
  236. ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
  237. ctx->exc + i * SUBFRAME_SIZE, fc,
  238. (!voicing && frame_erasure) ? 0 : ctx->gain_pitch,
  239. ( voicing && frame_erasure) ? 0 : ctx->gain_code,
  240. 1 << 13, 14, SUBFRAME_SIZE);
  241. }
  242. *data_size = SUBFRAME_SIZE << 2;
  243. return buf_size;
  244. }
  245. AVCodec g729_decoder =
  246. {
  247. "g729",
  248. CODEC_TYPE_AUDIO,
  249. CODEC_ID_G729,
  250. sizeof(G729Context),
  251. decoder_init,
  252. NULL,
  253. NULL,
  254. decode_frame,
  255. .long_name = NULL_IF_CONFIG_SMALL("G.729"),
  256. };