blob: b8cc9e886dc89ac3960ddd6ef53757278dd472c8 [file] [log] [blame]
xingri.gaoc18d4472023-02-28 02:51:02 +00001/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18**
19** Any non-GPL usage of this software or parts of this software is strictly
20** forbidden.
21**
22** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24**
25** Commercial non-GPL licensing of this software is possible.
26** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27**
28** $Id: lt_predict.c,v 1.27 2007/11/01 12:33:31 menno Exp $
29**/
30
31
32#include "common.h"
33#include "structs.h"
34
35#ifdef LTP_DEC
36
37#include "syntax.h"
38#include "lt_predict.h"
39#include "filtbank.h"
40#include "tns.h"
41
42
43/* static function declarations */
44static int16_t real_to_int16(real_t sig_in);
45
46
47/* check if the object type is an object type that can have LTP */
48uint8_t is_ltp_ot(uint8_t object_type)
49{
50#ifdef LTP_DEC
51 if ((object_type == LTP)
52#ifdef ERROR_RESILIENCE
53 || (object_type == ER_LTP)
54#endif
55#ifdef LD_DEC
56 || (object_type == LD)
57#endif
58 ) {
59 return 1;
60 }
61#endif
62
63 return 0;
64}
65
66ALIGN static const real_t codebook[8] = {
67 REAL_CONST(0.570829),
68 REAL_CONST(0.696616),
69 REAL_CONST(0.813004),
70 REAL_CONST(0.911304),
71 REAL_CONST(0.984900),
72 REAL_CONST(1.067894),
73 REAL_CONST(1.194601),
74 REAL_CONST(1.369533)
75};
76
77void lt_prediction(ic_stream *ics, ltp_info *ltp, real_t *spec,
78 int16_t *lt_pred_stat, fb_info *fb, uint8_t win_shape,
79 uint8_t win_shape_prev, uint8_t sr_index,
80 uint8_t object_type, uint16_t frame_len)
81{
82 uint8_t sfb;
83 uint16_t bin, i, num_samples;
yuliang.hu585b7202024-09-06 17:28:13 +080084 ALIGN real_t *x_est = NULL;
85 ALIGN real_t *X_est = NULL;
86 memset(fb->process_buf, 0, fb->process_buf_size);
87 memset(fb->process_buf2, 0, fb->process_buf_size2);
88 x_est = (real_t *)fb->process_buf;
89 X_est = (real_t *)fb->process_buf2;
xingri.gaoc18d4472023-02-28 02:51:02 +000090
91 if (ics->window_sequence != EIGHT_SHORT_SEQUENCE) {
92 if (ltp->data_present) {
93 num_samples = frame_len << 1;
94
95 for (i = 0; i < num_samples; i++) {
96 /* The extra lookback M (N/2 for LD, 0 for LTP) is handled
97 in the buffer updating */
98
99#if 0
100 x_est[i] = MUL_R_C(lt_pred_stat[num_samples + i - ltp->lag],
101 codebook[ltp->coef]);
102#else
103 /* lt_pred_stat is a 16 bit int, multiplied with the fixed point real
104 this gives a real for x_est
105 */
106 x_est[i] = (real_t)lt_pred_stat[num_samples + i - ltp->lag] * codebook[ltp->coef];
107#endif
108 }
109
110 filter_bank_ltp(fb, ics->window_sequence, win_shape, win_shape_prev,
111 x_est, X_est, object_type, frame_len);
112
113 tns_encode_frame(ics, &(ics->tns), sr_index, object_type, X_est,
114 frame_len);
115
116 for (sfb = 0; sfb < ltp->last_band; sfb++) {
117 if (ltp->long_used[sfb]) {
118 uint16_t low = ics->swb_offset[sfb];
119 uint16_t high = min(ics->swb_offset[sfb + 1], ics->swb_offset_max);
120
121 for (bin = low; bin < high; bin++) {
122 spec[bin] += X_est[bin];
123 }
124 }
125 }
126 }
127 }
128}
129
130#ifdef FIXED_POINT
131static INLINE int16_t real_to_int16(real_t sig_in)
132{
133 if (sig_in >= 0) {
134 sig_in += (1 << (REAL_BITS - 1));
135 if (sig_in >= REAL_CONST(32768)) {
136 return 32767;
137 }
138 } else {
139 sig_in += -(1 << (REAL_BITS - 1));
140 if (sig_in <= REAL_CONST(-32768)) {
141 return -32768;
142 }
143 }
144
145 return (sig_in >> REAL_BITS);
146}
147#else
148static INLINE int16_t real_to_int16(real_t sig_in)
149{
150 if (sig_in >= 0) {
151#ifndef HAS_LRINTF
152 sig_in += 0.5f;
153#endif
154 if (sig_in >= 32768.0f) {
155 return 32767;
156 }
157 } else {
158#ifndef HAS_LRINTF
159 sig_in += -0.5f;
160#endif
161 if (sig_in <= -32768.0f) {
162 return -32768;
163 }
164 }
165
166 return lrintf(sig_in);
167}
168#endif
169
170void lt_update_state(int16_t *lt_pred_stat, real_t *time, real_t *overlap,
171 uint16_t frame_len, uint8_t object_type)
172{
173 uint16_t i;
174
175 /*
176 * The reference point for index i and the content of the buffer
177 * lt_pred_stat are arranged so that lt_pred_stat(0 ... N/2 - 1) contains the
178 * last aliased half window from the IMDCT, and lt_pred_stat(N/2 ... N-1)
179 * is always all zeros. The rest of lt_pred_stat (i<0) contains the previous
180 * fully reconstructed time domain samples, i.e., output of the decoder.
181 *
182 * These values are shifted up by N*2 to avoid (i<0)
183 *
184 * For the LD object type an extra 512 samples lookback is accomodated here.
185 */
186#ifdef LD_DEC
187 if (object_type == LD) {
188 for (i = 0; i < frame_len; i++) {
189 lt_pred_stat[i] /* extra 512 */ = lt_pred_stat[i + frame_len];
190 lt_pred_stat[frame_len + i] = lt_pred_stat[i + (frame_len * 2)];
191 lt_pred_stat[(frame_len * 2) + i] = real_to_int16(time[i]);
192 lt_pred_stat[(frame_len * 3) + i] = real_to_int16(overlap[i]);
193 }
194 } else {
195#endif
196 for (i = 0; i < frame_len; i++) {
197 lt_pred_stat[i] = lt_pred_stat[i + frame_len];
198 lt_pred_stat[frame_len + i] = real_to_int16(time[i]);
199 lt_pred_stat[(frame_len * 2) + i] = real_to_int16(overlap[i]);
200#if 0 /* set to zero once upon initialisation */
201 lt_pred_stat[(frame_len * 3) + i] = 0;
202#endif
203 }
204#ifdef LD_DEC
205 }
206#endif
207}
208
209#endif