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.

327 lines
11KB

  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. int pitch_delay_int_prev; ///< integer part of previous subframe's pitch delay (4.1.3)
  76. /// (2.13) LSP quantizer outputs
  77. int16_t past_quantizer_output_buf[MA_NP + 1][10];
  78. int16_t* past_quantizer_outputs[MA_NP + 1];
  79. int16_t lsfq[10]; ///< (2.13) quantized LSF coefficients from previous frame
  80. int16_t lsp_buf[2][10]; ///< (0.15) LSP coefficients (previous and current frames) (3.2.5)
  81. int16_t *lsp[2]; ///< pointers to lsp_buf
  82. } G729Context;
  83. static const G729FormatDescription format_g729_8k = {
  84. .ac_index_bits = {8,5},
  85. .parity_bit = 1,
  86. .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
  87. .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
  88. .fc_signs_bits = 4,
  89. .fc_indexes_bits = 13,
  90. };
  91. static const G729FormatDescription format_g729d_6k4 = {
  92. .ac_index_bits = {8,4},
  93. .parity_bit = 0,
  94. .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
  95. .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
  96. .fc_signs_bits = 2,
  97. .fc_indexes_bits = 9,
  98. };
  99. /**
  100. * \brief pseudo random number generator
  101. */
  102. static inline uint16_t g729_prng(uint16_t value)
  103. {
  104. return 31821 * value + 13849;
  105. }
  106. /**
  107. * Get parity bit of bit 2..7
  108. */
  109. static inline int get_parity(uint8_t value)
  110. {
  111. return (0x6996966996696996ULL >> (value >> 2)) & 1;
  112. }
  113. static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1],
  114. int16_t ma_predictor,
  115. int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high)
  116. {
  117. int i,j;
  118. static const uint8_t min_distance[2]={10, 5}; //(2.13)
  119. int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
  120. for (i = 0; i < 5; i++) {
  121. quantizer_output[i] = cb_lsp_1st[vq_1st][i ] + cb_lsp_2nd[vq_2nd_low ][i ];
  122. quantizer_output[i + 5] = cb_lsp_1st[vq_1st][i + 5] + cb_lsp_2nd[vq_2nd_high][i + 5];
  123. }
  124. for (j = 0; j < 2; j++) {
  125. for (i = 1; i < 10; i++) {
  126. int diff = (quantizer_output[i - 1] - quantizer_output[i] + min_distance[j]) >> 1;
  127. if (diff > 0) {
  128. quantizer_output[i - 1] -= diff;
  129. quantizer_output[i ] += diff;
  130. }
  131. }
  132. }
  133. for (i = 0; i < 10; i++) {
  134. int sum = quantizer_output[i] * cb_ma_predictor_sum[ma_predictor][i];
  135. for (j = 0; j < MA_NP; j++)
  136. sum += past_quantizer_outputs[j][i] * cb_ma_predictor[ma_predictor][j][i];
  137. lsfq[i] = sum >> 15;
  138. }
  139. /* Rotate past_quantizer_outputs. */
  140. memmove(past_quantizer_outputs + 1, past_quantizer_outputs, MA_NP * sizeof(int16_t*));
  141. past_quantizer_outputs[0] = quantizer_output;
  142. ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10);
  143. }
  144. static av_cold int decoder_init(AVCodecContext * avctx)
  145. {
  146. G729Context* ctx = avctx->priv_data;
  147. int i,k;
  148. if (avctx->channels != 1) {
  149. av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels);
  150. return AVERROR(EINVAL);
  151. }
  152. /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
  153. avctx->frame_size = SUBFRAME_SIZE << 1;
  154. for (k = 0; k < MA_NP + 1; k++) {
  155. ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k];
  156. for (i = 1; i < 11; i++)
  157. ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3;
  158. }
  159. ctx->lsp[0] = ctx->lsp_buf[0];
  160. ctx->lsp[1] = ctx->lsp_buf[1];
  161. memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
  162. return 0;
  163. }
  164. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  165. AVPacket *avpkt)
  166. {
  167. const uint8_t *buf = avpkt->data;
  168. int buf_size = avpkt->size;
  169. int16_t *out_frame = data;
  170. GetBitContext gb;
  171. G729FormatDescription format;
  172. int frame_erasure = 0; ///< frame erasure detected during decoding
  173. int bad_pitch = 0; ///< parity check failed
  174. int i;
  175. G729Context *ctx = avctx->priv_data;
  176. int16_t lp[2][11]; // (3.12)
  177. uint8_t ma_predictor; ///< switched MA predictor of LSP quantizer
  178. uint8_t quantizer_1st; ///< first stage vector of quantizer
  179. uint8_t quantizer_2nd_lo; ///< second stage lower vector of quantizer (size in bits)
  180. uint8_t quantizer_2nd_hi; ///< second stage higher vector of quantizer (size in bits)
  181. int pitch_delay_int; // pitch delay, integer part
  182. int pitch_delay_3x; // pitch delay, multiplied by 3
  183. if (*data_size < SUBFRAME_SIZE << 2) {
  184. av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n");
  185. return AVERROR(EIO);
  186. }
  187. if (buf_size == 10) {
  188. format = format_g729_8k;
  189. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
  190. } else if (buf_size == 8) {
  191. format = format_g729d_6k4;
  192. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
  193. } else {
  194. av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
  195. return AVERROR_INVALIDDATA;
  196. }
  197. for (i=0; i < buf_size; i++)
  198. frame_erasure |= buf[i];
  199. frame_erasure = !frame_erasure;
  200. init_get_bits(&gb, buf, buf_size);
  201. ma_predictor = get_bits(&gb, 1);
  202. quantizer_1st = get_bits(&gb, VQ_1ST_BITS);
  203. quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
  204. quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
  205. lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs,
  206. ma_predictor,
  207. quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi);
  208. ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
  209. ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
  210. FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
  211. for (i = 0; i < 2; i++) {
  212. uint8_t ac_index; ///< adaptive codebook index
  213. uint8_t pulses_signs; ///< fixed-codebook vector pulse signs
  214. int fc_indexes; ///< fixed-codebook indexes
  215. uint8_t gc_1st_index; ///< gain codebook (first stage) index
  216. uint8_t gc_2nd_index; ///< gain codebook (second stage) index
  217. ac_index = get_bits(&gb, format.ac_index_bits[i]);
  218. if(!i && format.parity_bit)
  219. bad_pitch = get_parity(ac_index) == get_bits1(&gb);
  220. fc_indexes = get_bits(&gb, format.fc_indexes_bits);
  221. pulses_signs = get_bits(&gb, format.fc_signs_bits);
  222. gc_1st_index = get_bits(&gb, format.gc_1st_index_bits);
  223. gc_2nd_index = get_bits(&gb, format.gc_2nd_index_bits);
  224. if(!i) {
  225. if (bad_pitch)
  226. pitch_delay_3x = 3 * ctx->pitch_delay_int_prev;
  227. else
  228. pitch_delay_3x = ff_acelp_decode_8bit_to_1st_delay3(ac_index);
  229. } else {
  230. int pitch_delay_min = av_clip(ctx->pitch_delay_int_prev - 5,
  231. PITCH_DELAY_MIN, PITCH_DELAY_MAX - 9);
  232. if(packet_type == FORMAT_G729D_6K4)
  233. pitch_delay_3x = ff_acelp_decode_4bit_to_2nd_delay3(ac_index, pitch_delay_min);
  234. else
  235. pitch_delay_3x = ff_acelp_decode_5_6_bit_to_2nd_delay3(ac_index, pitch_delay_min);
  236. }
  237. /* Round pitch delay to nearest (used everywhere except ff_acelp_interpolate). */
  238. pitch_delay_int = (pitch_delay_3x + 1) / 3;
  239. ff_acelp_weighted_vector_sum(fc + pitch_delay_int,
  240. fc + pitch_delay_int,
  241. fc, 1 << 14,
  242. av_clip(ctx->gain_pitch, SHARP_MIN, SHARP_MAX),
  243. 0, 14,
  244. SUBFRAME_SIZE - pitch_delay_int);
  245. if (frame_erasure) {
  246. ctx->gain_pitch = (29491 * ctx->gain_pitch) >> 15; // 0.90 (0.15)
  247. ctx->gain_code = ( 2007 * ctx->gain_code ) >> 11; // 0.98 (0.11)
  248. gain_corr_factor = 0;
  249. } else {
  250. ctx->gain_pitch = cb_gain_1st_8k[gc_1st_index][0] +
  251. cb_gain_2nd_8k[gc_2nd_index][0];
  252. gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
  253. cb_gain_2nd_8k[gc_2nd_index][1];
  254. ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
  255. ctx->exc + i * SUBFRAME_SIZE, fc,
  256. (!voicing && frame_erasure) ? 0 : ctx->gain_pitch,
  257. ( voicing && frame_erasure) ? 0 : ctx->gain_code,
  258. 1 << 13, 14, SUBFRAME_SIZE);
  259. ctx->pitch_delay_int_prev = pitch_delay_int;
  260. }
  261. *data_size = SUBFRAME_SIZE << 2;
  262. return buf_size;
  263. }
  264. AVCodec g729_decoder =
  265. {
  266. "g729",
  267. AVMEDIA_TYPE_AUDIO,
  268. CODEC_ID_G729,
  269. sizeof(G729Context),
  270. decoder_init,
  271. NULL,
  272. NULL,
  273. decode_frame,
  274. .long_name = NULL_IF_CONFIG_SMALL("G.729"),
  275. };