3 * Copyright (c) 2008 Vladimir Voroshilov
5 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/avutil.h"
35 #include "celp_math.h"
36 #include "acelp_filters.h"
37 #include "acelp_pitch_delay.h"
38 #include "acelp_vectors.h"
42 * minimum quantized LSF value (3.2.4)
48 * maximum quantized LSF value (3.2.4)
51 #define LSFQ_MAX 25681
54 * minimum LSF distance (3.2.4)
57 #define LSFQ_DIFF_MIN 321
60 * minimum gain pitch value (3.8, Equation 47)
63 #define SHARP_MIN 3277
66 * maximum gain pitch value (3.8, Equation 47)
67 * (EE) This does not comply with the specification.
68 * Specification says about 0.8, which should be
69 * 13107 in (1.14), but reference C code uses
70 * 13017 (equals to 0.7945) instead of it.
72 #define SHARP_MAX 13017
75 uint8_t ac_index_bits
[2]; ///< adaptive codebook index for second subframe (size in bits)
76 uint8_t parity_bit
; ///< parity bit for pitch delay
77 uint8_t gc_1st_index_bits
; ///< gain codebook (first stage) index (size in bits)
78 uint8_t gc_2nd_index_bits
; ///< gain codebook (second stage) index (size in bits)
79 uint8_t fc_signs_bits
; ///< number of pulses in fixed-codebook vector
80 uint8_t fc_indexes_bits
; ///< size (in bits) of fixed-codebook index entry
81 } G729FormatDescription
;
84 int16_t lsp_buf
[2][10]; ///< (0.15) LSP coefficients (previous and current frames) (3.2.5)
85 int16_t *lsp
[2]; ///< pointers to lsp_buf
88 static const G729FormatDescription format_g729_8k
= {
89 .ac_index_bits
= {8,5},
91 .gc_1st_index_bits
= GC_1ST_IDX_BITS_8K
,
92 .gc_2nd_index_bits
= GC_2ND_IDX_BITS_8K
,
94 .fc_indexes_bits
= 13,
97 static const G729FormatDescription format_g729d_6k4
= {
98 .ac_index_bits
= {8,4},
100 .gc_1st_index_bits
= GC_1ST_IDX_BITS_6K4
,
101 .gc_2nd_index_bits
= GC_2ND_IDX_BITS_6K4
,
103 .fc_indexes_bits
= 9,
107 * \brief pseudo random number generator
109 static inline uint16_t g729_prng(uint16_t value
)
111 return 31821 * value
+ 13849;
115 * Get parity bit of bit 2..7
117 static inline int get_parity(uint8_t value
)
119 return (0x6996966996696996ULL
>> (value
>> 2)) & 1;
122 static av_cold
int decoder_init(AVCodecContext
* avctx
)
124 if (avctx
->channels
!= 1) {
125 av_log(avctx
, AV_LOG_ERROR
, "Only mono sound is supported (requested channels: %d).\n", avctx
->channels
);
126 return AVERROR_NOFMT
;
129 /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
130 avctx
->frame_size
= SUBFRAME_SIZE
<< 1;
132 ctx
->lsp
[0] = ctx
->lsp_buf
[0];
133 ctx
->lsp
[1] = ctx
->lsp_buf
[1];
134 memcpy(ctx
->lsp
[0], lsp_init
, 10 * sizeof(int16_t));
139 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *data_size
,
142 const uint8_t *buf
= avpkt
->data
;
143 int buf_size
= avpkt
->size
;
144 int16_t *out_frame
= data
;
146 G729FormatDescription format
;
147 int frame_erasure
= 0; ///< frame erasure detected during decoding
148 int bad_pitch
= 0; ///< parity check failed
150 G729Context
*ctx
= avctx
->priv_data
;
151 int16_t lp
[2][11]; // (3.12)
152 uint8_t ma_predictor
; ///< switched MA predictor of LSP quantizer
153 uint8_t quantizer_1st
; ///< first stage vector of quantizer
154 uint8_t quantizer_2nd_lo
; ///< second stage lower vector of quantizer (size in bits)
155 uint8_t quantizer_2nd_hi
; ///< second stage higher vector of quantizer (size in bits)
157 if (*data_size
< SUBFRAME_SIZE
<< 2) {
158 av_log(avctx
, AV_LOG_ERROR
, "Error processing packet: output buffer too small\n");
162 if (buf_size
== 10) {
163 format
= format_g729_8k
;
164 av_log(avctx
, AV_LOG_DEBUG
, "Packet type: %s\n", "G.729 @ 8kbit/s");
165 } else if (buf_size
== 8) {
166 format
= format_g729d_6k4
;
167 av_log(avctx
, AV_LOG_DEBUG
, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
169 av_log(avctx
, AV_LOG_ERROR
, "Packet size %d is unknown.\n", buf_size
);
170 return (AVERROR_NOFMT
);
173 for (i
=0; i
< buf_size
; i
++)
174 frame_erasure
|= buf
[i
];
175 frame_erasure
= !frame_erasure
;
177 init_get_bits(&gb
, buf
, buf_size
);
179 ma_predictor
= get_bits(&gb
, 1);
180 quantizer_1st
= get_bits(&gb
, VQ_1ST_BITS
);
181 quantizer_2nd_lo
= get_bits(&gb
, VQ_2ND_BITS
);
182 quantizer_2nd_hi
= get_bits(&gb
, VQ_2ND_BITS
);
184 ff_acelp_lsf2lsp(ctx
->lsp
[1], ctx
->lsfq
, 10);
186 ff_acelp_lp_decode(&lp
[0][0], &lp
[1][0], ctx
->lsp
[1], ctx
->lsp
[0], 10);
188 FFSWAP(int16_t*, ctx
->lsp
[1], ctx
->lsp
[0]);
190 for (i
= 0; i
< 2; i
++) {
191 uint8_t ac_index
; ///< adaptive codebook index
192 uint8_t pulses_signs
; ///< fixed-codebook vector pulse signs
193 int fc_indexes
; ///< fixed-codebook indexes
194 uint8_t gc_1st_index
; ///< gain codebook (first stage) index
195 uint8_t gc_2nd_index
; ///< gain codebook (second stage) index
197 ac_index
= get_bits(&gb
, format
.ac_index_bits
[i
]);
198 if(!i
&& format
.parity_bit
)
199 bad_pitch
= get_parity(ac_index
) == get_bits1(&gb
);
200 fc_indexes
= get_bits(&gb
, format
.fc_indexes_bits
);
201 pulses_signs
= get_bits(&gb
, format
.fc_signs_bits
);
202 gc_1st_index
= get_bits(&gb
, format
.gc_1st_index_bits
);
203 gc_2nd_index
= get_bits(&gb
, format
.gc_2nd_index_bits
);
205 ff_acelp_weighted_vector_sum(fc
+ pitch_delay_int
[i
],
206 fc
+ pitch_delay_int
[i
],
208 av_clip(ctx
->gain_pitch
, SHARP_MIN
, SHARP_MAX
),
210 SUBFRAME_SIZE
- pitch_delay_int
[i
]);
213 ctx
->gain_pitch
= (29491 * ctx
->gain_pitch
) >> 15; // 0.90 (0.15)
214 ctx
->gain_code
= ( 2007 * ctx
->gain_code
) >> 11; // 0.98 (0.11)
216 gain_corr_factor
= 0;
218 ctx
->gain_pitch
= cb_gain_1st_8k
[gc_1st_index
][0] +
219 cb_gain_2nd_8k
[gc_2nd_index
][0];
220 gain_corr_factor
= cb_gain_1st_8k
[gc_1st_index
][1] +
221 cb_gain_2nd_8k
[gc_2nd_index
][1];
223 ff_acelp_weighted_vector_sum(ctx
->exc
+ i
* SUBFRAME_SIZE
,
224 ctx
->exc
+ i
* SUBFRAME_SIZE
, fc
,
225 (!voicing
&& frame_erasure
) ? 0 : ctx
->gain_pitch
,
226 ( voicing
&& frame_erasure
) ? 0 : ctx
->gain_code
,
227 1 << 13, 14, SUBFRAME_SIZE
);
230 *data_size
= SUBFRAME_SIZE
<< 2;
234 AVCodec g729_decoder
=
244 .long_name
= NULL_IF_CONFIG_SMALL("G.729"),