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.

246 lines
8.0KB

  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. int16_t lsp_buf[2][10]; ///< (0.15) LSP coefficients (previous and current frames) (3.2.5)
  76. int16_t *lsp[2]; ///< pointers to lsp_buf
  77. } G729Context;
  78. static const G729FormatDescription format_g729_8k = {
  79. .ac_index_bits = {8,5},
  80. .parity_bit = 1,
  81. .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
  82. .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
  83. .fc_signs_bits = 4,
  84. .fc_indexes_bits = 13,
  85. };
  86. static const G729FormatDescription format_g729d_6k4 = {
  87. .ac_index_bits = {8,4},
  88. .parity_bit = 0,
  89. .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
  90. .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
  91. .fc_signs_bits = 2,
  92. .fc_indexes_bits = 9,
  93. };
  94. /**
  95. * \brief pseudo random number generator
  96. */
  97. static inline uint16_t g729_prng(uint16_t value)
  98. {
  99. return 31821 * value + 13849;
  100. }
  101. /**
  102. * Get parity bit of bit 2..7
  103. */
  104. static inline int get_parity(uint8_t value)
  105. {
  106. return (0x6996966996696996ULL >> (value >> 2)) & 1;
  107. }
  108. static av_cold int decoder_init(AVCodecContext * avctx)
  109. {
  110. if (avctx->channels != 1) {
  111. av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels);
  112. return AVERROR_NOFMT;
  113. }
  114. /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
  115. avctx->frame_size = SUBFRAME_SIZE << 1;
  116. ctx->lsp[0] = ctx->lsp_buf[0];
  117. ctx->lsp[1] = ctx->lsp_buf[1];
  118. memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
  119. return 0;
  120. }
  121. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  122. AVPacket *avpkt)
  123. {
  124. const uint8_t *buf = avpkt->data;
  125. int buf_size = avpkt->size;
  126. int16_t *out_frame = data;
  127. GetBitContext gb;
  128. G729FormatDescription format;
  129. int frame_erasure = 0; ///< frame erasure detected during decoding
  130. int bad_pitch = 0; ///< parity check failed
  131. int i;
  132. G729Context *ctx = avctx->priv_data;
  133. int16_t lp[2][11]; // (3.12)
  134. uint8_t ma_predictor; ///< switched MA predictor of LSP quantizer
  135. uint8_t quantizer_1st; ///< first stage vector of quantizer
  136. uint8_t quantizer_2nd_lo; ///< second stage lower vector of quantizer (size in bits)
  137. uint8_t quantizer_2nd_hi; ///< second stage higher vector of quantizer (size in bits)
  138. if (*data_size < SUBFRAME_SIZE << 2) {
  139. av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n");
  140. return AVERROR(EIO);
  141. }
  142. if (buf_size == 10) {
  143. format = format_g729_8k;
  144. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
  145. } else if (buf_size == 8) {
  146. format = format_g729d_6k4;
  147. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
  148. } else {
  149. av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
  150. return (AVERROR_NOFMT);
  151. }
  152. for (i=0; i < buf_size; i++)
  153. frame_erasure |= buf[i];
  154. frame_erasure = !frame_erasure;
  155. init_get_bits(&gb, buf, buf_size);
  156. ma_predictor = get_bits(&gb, 1);
  157. quantizer_1st = get_bits(&gb, VQ_1ST_BITS);
  158. quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
  159. quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
  160. ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
  161. ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
  162. FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
  163. for (i = 0; i < 2; i++) {
  164. uint8_t ac_index; ///< adaptive codebook index
  165. uint8_t pulses_signs; ///< fixed-codebook vector pulse signs
  166. int fc_indexes; ///< fixed-codebook indexes
  167. uint8_t gc_1st_index; ///< gain codebook (first stage) index
  168. uint8_t gc_2nd_index; ///< gain codebook (second stage) index
  169. ac_index = get_bits(&gb, format.ac_index_bits[i]);
  170. if(!i && format.parity_bit)
  171. bad_pitch = get_parity(ac_index) == get_bits1(&gb);
  172. fc_indexes = get_bits(&gb, format.fc_indexes_bits);
  173. pulses_signs = get_bits(&gb, format.fc_signs_bits);
  174. gc_1st_index = get_bits(&gb, format.gc_1st_index_bits);
  175. gc_2nd_index = get_bits(&gb, format.gc_2nd_index_bits);
  176. ff_acelp_weighted_vector_sum(fc + pitch_delay_int[i],
  177. fc + pitch_delay_int[i],
  178. fc, 1 << 14,
  179. av_clip(ctx->gain_pitch, SHARP_MIN, SHARP_MAX),
  180. 0, 14,
  181. SUBFRAME_SIZE - pitch_delay_int[i]);
  182. if (frame_erasure) {
  183. ctx->gain_pitch = (29491 * ctx->gain_pitch) >> 15; // 0.90 (0.15)
  184. ctx->gain_code = ( 2007 * ctx->gain_code ) >> 11; // 0.98 (0.11)
  185. gain_corr_factor = 0;
  186. } else {
  187. ctx->gain_pitch = cb_gain_1st_8k[gc_1st_index][0] +
  188. cb_gain_2nd_8k[gc_2nd_index][0];
  189. gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
  190. cb_gain_2nd_8k[gc_2nd_index][1];
  191. ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
  192. ctx->exc + i * SUBFRAME_SIZE, fc,
  193. (!voicing && frame_erasure) ? 0 : ctx->gain_pitch,
  194. ( voicing && frame_erasure) ? 0 : ctx->gain_code,
  195. 1 << 13, 14, SUBFRAME_SIZE);
  196. }
  197. *data_size = SUBFRAME_SIZE << 2;
  198. return buf_size;
  199. }
  200. AVCodec g729_decoder =
  201. {
  202. "g729",
  203. CODEC_TYPE_AUDIO,
  204. CODEC_ID_G729,
  205. sizeof(G729Context),
  206. decoder_init,
  207. NULL,
  208. NULL,
  209. decode_frame,
  210. .long_name = NULL_IF_CONFIG_SMALL("G.729"),
  211. };