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.

229 lines
7.4KB

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