aboutsummaryrefslogtreecommitdiff
path: root/decoder/drc_src/impd_drc_gain_dec.c
blob: c8c55451ce01d7c7c2256091414bed130c34189e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/******************************************************************************
 *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "impd_type_def.h"
#include "impd_drc_extr_delta_coded_info.h"
#include "impd_drc_common.h"
#include "impd_drc_struct.h"
#include "impd_drc_interface.h"
#include "impd_drc_filter_bank.h"
#include "impd_drc_gain_dec.h"
#include "impd_parametric_drc_dec.h"
#include "impd_drc_multi_band.h"
#include "impd_drc_process_audio.h"
#include "impd_drc_eq.h"
#include "impd_drc_gain_decoder.h"

extern  ia_cicp_sigmoid_characteristic_param_struct pstr_cicp_sigmoid_characteristic_param[];

WORD32 impd_gain_db_to_lin (ia_interp_params_struct* interp_params_str,
          WORD32          drc_band,
          FLOAT32         in_param_db_gain,
          FLOAT32         in_param_db_slope,
          FLOAT32*        out_param_lin_gain,
          FLOAT32*        out_param_lin_slope)
{

    FLOAT32 loc_db_gain = in_param_db_gain;
    FLOAT32 gain_ratio = 1.0;

    ia_gain_modifiers_struct*  pstr_gain_modifiers = interp_params_str->pstr_gain_modifiers;
    if (interp_params_str->gain_modification_flag) {

        if ((interp_params_str->characteristic_index > 0) && (loc_db_gain != 0.0f)){

            gain_ratio = 1.0f;
        }


        if (loc_db_gain < 0.0f) {
            gain_ratio *= interp_params_str->compress;
        }
        else {
            gain_ratio *= interp_params_str->boost;
        }
    }
    if (pstr_gain_modifiers->gain_scaling_flag[drc_band] == 1) {
        if (loc_db_gain < 0.0) {
            gain_ratio *= pstr_gain_modifiers->attn_scaling[drc_band];
        }
        else {
            gain_ratio *= pstr_gain_modifiers->ampl_scaling[drc_band];
        }
    }
    if ((interp_params_str->pstr_ducking_modifiers->ducking_scaling_flag == 1) && (interp_params_str->ducking_flag == 1)) {
        gain_ratio *= interp_params_str->pstr_ducking_modifiers->ducking_scaling;
    }

    {
        *out_param_lin_gain = (FLOAT32)pow(2.0, (FLOAT64)(gain_ratio * loc_db_gain / 6.0f));
        *out_param_lin_slope = SLOPE_FACTOR_DB_TO_LINEAR * gain_ratio * *out_param_lin_gain * in_param_db_slope;

        if (pstr_gain_modifiers->gain_offset_flag[drc_band] == 1) {
            *out_param_lin_gain *= (FLOAT32)pow(2.0, (FLOAT64)(pstr_gain_modifiers->gain_offset[drc_band]/6.0f));
        }
        if ((interp_params_str->limiter_peak_target_present == 1)  && (interp_params_str->clipping_flag == 1)) {
            *out_param_lin_gain *= (FLOAT32)pow(2.0, max(0.0, -interp_params_str->limiter_peak_target - interp_params_str->loudness_normalization_gain_db)/6.0);
            if (*out_param_lin_gain >= 1.0) {
                *out_param_lin_gain = 1.0;
                *out_param_lin_slope = 0.0;
            }
        }
    }
    return (0);
}







WORD32
impd_compressor_io_sigmoid(ia_split_drc_characteristic_struct* split_drc_characteristic,
                     FLOAT32 in_db_level,
                     FLOAT32* out_db_gain)
{
    FLOAT32 tmp;
    FLOAT32 in_out_ratio = split_drc_characteristic->in_out_ratio;
    FLOAT32 gainDbLimit = split_drc_characteristic->gain;
    FLOAT32 exp = split_drc_characteristic->exp;

    tmp = (DRC_INPUT_LOUDNESS_TARGET - in_db_level) * in_out_ratio;
    if (exp < 1000.0f) {
        FLOAT32 x = tmp/gainDbLimit;
        if (x < 0.0f)
        {
            return(UNEXPECTED_ERROR);
        }
        *out_db_gain = (FLOAT32)(tmp / pow(1.0f + pow(x, exp), 1.0f/exp));
    } else {
        *out_db_gain = tmp;
    }
    if (split_drc_characteristic->flip_sign == 1) {
        *out_db_gain = - *out_db_gain;
    }
    return (0);
}

WORD32
impd_compressor_io_sigmoid_inv(ia_split_drc_characteristic_struct* split_drc_characteristic,
                             FLOAT32 loc_db_gain,
                             FLOAT32* in_level)
{
    FLOAT32 in_out_ratio = split_drc_characteristic->in_out_ratio;
    FLOAT32 gainDbLimit = split_drc_characteristic->gain;
    FLOAT32 exp = split_drc_characteristic->exp;
    FLOAT32 tmp = loc_db_gain;

    if (split_drc_characteristic->flip_sign == 1) {
        tmp = - loc_db_gain;
    }
    if (exp < 1000.0f) {
        FLOAT32 x = tmp/gainDbLimit;
        if (x < 0.0f)
        {
            return(UNEXPECTED_ERROR);
        }
        tmp = (FLOAT32)(tmp / pow(1.0f - pow(x, exp), 1.0f / exp));
    }
    *in_level = DRC_INPUT_LOUDNESS_TARGET - tmp / in_out_ratio;

    return(0);
}

WORD32
impd_compressor_io_nodes_lt(ia_split_drc_characteristic_struct* split_drc_characteristic,
                       FLOAT32 in_db_level,
                       FLOAT32 *out_db_gain)
{
    WORD32 n;
    FLOAT32 w;
    FLOAT32* node_level = split_drc_characteristic->node_level;
    FLOAT32* node_gain = split_drc_characteristic->node_gain;

    if (in_db_level > DRC_INPUT_LOUDNESS_TARGET)
    {
        return (UNEXPECTED_ERROR);
    }
    for (n=1; n<=split_drc_characteristic->characteristic_node_count; n++) {
        if ((in_db_level <= node_level[n-1]) && (in_db_level > node_level[n])) {
            w = (node_level[n]- in_db_level)/(node_level[n]-node_level[n-1]);
            *out_db_gain =(FLOAT32) (w * node_gain[n-1] + (1.0-w) * node_gain[n]);
        }
    }
    *out_db_gain = node_gain[split_drc_characteristic->characteristic_node_count];
    return (0);
}

WORD32
impd_compressor_io_nodes_rt(ia_split_drc_characteristic_struct* split_drc_characteristic,
                        FLOAT32 in_db_level,
                        FLOAT32 *out_db_gain)
{
    WORD32 n;
    FLOAT32 w;
    FLOAT32* node_level = split_drc_characteristic->node_level;
    FLOAT32* node_gain = split_drc_characteristic->node_gain;

    if (in_db_level < DRC_INPUT_LOUDNESS_TARGET)
    {
        return (UNEXPECTED_ERROR);
    }
    for (n=1; n<=split_drc_characteristic->characteristic_node_count; n++) {
        if ((in_db_level >= node_level[n-1]) && (in_db_level < node_level[n])) {
            w =(FLOAT32) (node_level[n]- in_db_level)/(node_level[n]-node_level[n-1]);
            *out_db_gain = (FLOAT32)(w * node_gain[n-1] + (1.0-w) * node_gain[n]);
        }
    }
    *out_db_gain = (node_gain[split_drc_characteristic->characteristic_node_count]);
    return (0);
}


WORD32
impd_compressor_io_nodes_inverse(ia_split_drc_characteristic_struct* split_drc_characteristic,
                           FLOAT32 loc_db_gain,
                           FLOAT32 *in_level)
{
    WORD32 n;
    FLOAT32 w;
    FLOAT32* node_level = split_drc_characteristic->node_level;
    FLOAT32* node_gain = split_drc_characteristic->node_gain;
    WORD32 node_count = split_drc_characteristic->characteristic_node_count;

    if (node_gain[1] < 0.0f) {
        if (loc_db_gain <= node_gain[node_count]) {
            *in_level = node_level[node_count];
        }
        else {
            if (loc_db_gain >= 0.0f) {
                *in_level = DRC_INPUT_LOUDNESS_TARGET;
            }
            else {
                for (n=1; n<=node_count; n++) {
                    if ((loc_db_gain <= node_gain[n-1]) && (loc_db_gain > node_gain[n])) {
                        w = (node_gain[n]-loc_db_gain)/(node_gain[n]-node_gain[n-1]);
                        *in_level =(FLOAT32) (w * node_level[n-1] + (1.0-w) * node_level[n]);
                    }
                }
            }
        }
    }
    else {
        if (loc_db_gain >= node_gain[node_count]) {
            *in_level = node_level[node_count];
        }
        else {
            if (loc_db_gain <= 0.0f) {
                *in_level = DRC_INPUT_LOUDNESS_TARGET;
            }
            else {
                for (n=1; n<=node_count; n++) {
                    if ((loc_db_gain >= node_gain[n-1]) && (loc_db_gain < node_gain[n])) {
                        w = (FLOAT32)(node_gain[n]-loc_db_gain)/(node_gain[n]-node_gain[n-1]);
                        *in_level =(FLOAT32) (w * node_level[n-1] + (1.0-w) * node_level[n]);
                    }
                }
            }
        }
    }
    return (0);
}


WORD32
impd_map_gain (
         ia_split_drc_characteristic_struct* split_drc_characteristic_source,
         ia_split_drc_characteristic_struct* split_drc_characteristic_target,
         FLOAT32 gain_in_db,
         FLOAT32* gain_out_db)
{
    FLOAT32 inLevel;
    WORD32 err = 0;

    switch (split_drc_characteristic_source->characteristic_format) {
        case CHARACTERISTIC_SIGMOID:
            err = impd_compressor_io_sigmoid_inv(split_drc_characteristic_source, gain_in_db, &inLevel);
            if (err) return (err);
            break;
        case CHARACTERISTIC_NODES:
            err = impd_compressor_io_nodes_inverse(split_drc_characteristic_source, gain_in_db, &inLevel);
            if (err) return (err);
            break;
        case CHARACTERISTIC_PASS_THRU:
            inLevel = gain_in_db;
            break;
        default:
            return(UNEXPECTED_ERROR);
            break;
    }
    switch (split_drc_characteristic_target->characteristic_format) {
        case CHARACTERISTIC_SIGMOID:
            err = impd_compressor_io_sigmoid(split_drc_characteristic_target, inLevel, gain_out_db);
            if (err) return (err);
            break;
        case CHARACTERISTIC_NODES:
            if (inLevel < DRC_INPUT_LOUDNESS_TARGET) {
                err = impd_compressor_io_nodes_lt(split_drc_characteristic_target, inLevel, gain_out_db);
                if (err) return (err);
            }
            else {
                err = impd_compressor_io_nodes_rt(split_drc_characteristic_target, inLevel, gain_out_db);
                if (err) return (err);
            }
            break;
        case CHARACTERISTIC_PASS_THRU:
            *gain_out_db = inLevel;
            break;
        default:
            break;
    }
    return (0);
}

WORD32
impd_conv_to_linear_domain (ia_interp_params_struct* interp_params_str,
           WORD32     drc_band,
          FLOAT32       in_param_db_gain,
          FLOAT32       in_param_db_slope,
          FLOAT32*        out_param_lin_gain,
          FLOAT32*        out_param_lin_slope)
{
    WORD32 err = 0;
    FLOAT32 loc_db_gain = in_param_db_gain;
    FLOAT32 gain_ratio = 1.0;
    FLOAT32 mapped_db_gain;
    ia_gain_modifiers_struct*  pstr_gain_modifiers = interp_params_str->pstr_gain_modifiers;
    if (interp_params_str->gain_modification_flag) {
        ia_split_drc_characteristic_struct* split_drc_characteristic_source;

        WORD32 slopeIsNegative;

        if (interp_params_str->drc_characteristic_present) {
            if (interp_params_str->drc_source_characteristic_cicp_format)
            {

            }
            else {
                slopeIsNegative = 0;
                split_drc_characteristic_source = interp_params_str->split_source_characteristic_left;
                if (split_drc_characteristic_source->characteristic_format == 0)
                {
                    slopeIsNegative = 1;
                }
                else
                {
                    if (split_drc_characteristic_source->node_gain[1] > 0.0f) {
                        slopeIsNegative = 1;
                    }
                }
                if (loc_db_gain == 0.0f) {
                    if (((pstr_gain_modifiers->target_characteristic_left_present[drc_band] == 1) &&
                        (interp_params_str->split_target_characteristic_left->characteristic_format == CHARACTERISTIC_PASS_THRU)) ||
                        ((pstr_gain_modifiers->target_characteristic_right_present[drc_band] == 1) &&
                         (interp_params_str->split_target_characteristic_right->characteristic_format == CHARACTERISTIC_PASS_THRU)))
                    {
                        mapped_db_gain = DRC_INPUT_LOUDNESS_TARGET;
                        loc_db_gain = DRC_INPUT_LOUDNESS_TARGET;
                    }
                }
                else
                {
                    if (((loc_db_gain > 0.0f) && (slopeIsNegative == 1)) || ((loc_db_gain < 0.0f) && (slopeIsNegative == 0)))
                    {
                        if (pstr_gain_modifiers->target_characteristic_left_present[drc_band] == 1)
                        {
                            err = impd_map_gain(split_drc_characteristic_source, interp_params_str->split_target_characteristic_left, loc_db_gain, &mapped_db_gain);
                            if (err) return (err);
                            gain_ratio = mapped_db_gain / loc_db_gain;
                        }

                    }
                    else if (((loc_db_gain < 0.0f) && (slopeIsNegative == 1)) || ((loc_db_gain > 0.0f) && (slopeIsNegative == 0)))
                    {
                        if (pstr_gain_modifiers->target_characteristic_right_present[drc_band] == 1)
                        {
                            split_drc_characteristic_source = interp_params_str->split_source_characteristic_right;
                            err = impd_map_gain(split_drc_characteristic_source, interp_params_str->split_target_characteristic_right, loc_db_gain, &mapped_db_gain);
                            if (err) return (err);
                            gain_ratio = mapped_db_gain / loc_db_gain;
                        }
                    }
                }
            }
        }

        if (loc_db_gain < 0.0f) {
            gain_ratio *= interp_params_str->compress;
        }
        else {
            gain_ratio *= interp_params_str->boost;
        }
    }
    if (pstr_gain_modifiers->gain_scaling_flag[drc_band] == 1) {
        if (loc_db_gain < 0.0) {
            gain_ratio *= pstr_gain_modifiers->attn_scaling[drc_band];
        }
        else {
            gain_ratio *= pstr_gain_modifiers->ampl_scaling[drc_band];
        }
    }
    if ((interp_params_str->pstr_ducking_modifiers->ducking_scaling_flag == 1) && (interp_params_str->ducking_flag == 1)) {
        gain_ratio *= interp_params_str->pstr_ducking_modifiers->ducking_scaling;
    }

    if (interp_params_str->interpolation_loud_eq == 1)
    {
        *out_param_lin_gain = gain_ratio * loc_db_gain + pstr_gain_modifiers->gain_offset[drc_band];
        *out_param_lin_slope = 0.0f;
    }
    else
    {
        *out_param_lin_gain = (FLOAT32)pow(2.0, (FLOAT64)(gain_ratio * loc_db_gain / 6.0f));
        *out_param_lin_slope = SLOPE_FACTOR_DB_TO_LINEAR * gain_ratio * *out_param_lin_gain * in_param_db_slope;

        if (pstr_gain_modifiers->gain_offset_flag[drc_band] == 1) {
            *out_param_lin_gain *= (FLOAT32)pow(2.0, (FLOAT64)(pstr_gain_modifiers->gain_offset[drc_band]/6.0f));
        }
        if ((interp_params_str->limiter_peak_target_present == 1)  && (interp_params_str->clipping_flag == 1)) {
            *out_param_lin_gain *= (FLOAT32)pow(2.0, max(0.0, -interp_params_str->limiter_peak_target - interp_params_str->loudness_normalization_gain_db)/6.0);
            if (*out_param_lin_gain >= 1.0) {
                *out_param_lin_gain = 1.0;
                *out_param_lin_slope = 0.0;
            }
        }
    }
    return (0);
}

WORD32 impd_interpolate_drc_gain(ia_interp_params_struct* interp_params_str,
                                        WORD32   drc_band,
                                        WORD32   gain_step_tdomain,
                                        FLOAT32  gain0,
                                        FLOAT32  gain1,
                                        FLOAT32  slope0,
                                        FLOAT32  slope1,
                                        FLOAT32* result)
{
    WORD32 err = 0;
    WORD32 n;
    FLOAT32 k1, k2, a, b, c, d;
    FLOAT32 slope_t1;
    FLOAT32 slope_t2;
    FLOAT32 gain_t1;
    FLOAT32 gain_t2;

    WORD32 cubic_interpolation = 1;
    WORD32 node_inser;
    FLOAT32 node_inser_float;

    if (gain_step_tdomain <= 0)
    {
        return (UNEXPECTED_ERROR);
    }

    if (interp_params_str->gain_interpolation_type == GAIN_INTERPOLATION_TYPE_SPLINE) {
        err = impd_conv_to_linear_domain(interp_params_str, drc_band, gain0, slope0, &gain_t1, &slope_t1);
        if (err)
            return (err);
        err = impd_conv_to_linear_domain (interp_params_str, drc_band, gain1, slope1, &gain_t2, &slope_t2);
        if (err)
            return (err);

        slope_t1 = slope_t1/(FLOAT32) interp_params_str->delta_tmin;
        slope_t2 = slope_t2/(FLOAT32) interp_params_str->delta_tmin;
        if ((FLOAT32)fabs((FLOAT64)slope_t1) > (FLOAT32)fabs((FLOAT64)slope_t2)) {
            node_inser_float = 2.0f * (gain_t2 - gain_t1 - slope_t2 * gain_step_tdomain) /
            (slope_t1 - slope_t2);
            node_inser = (WORD32) (0.5f + node_inser_float);
            if ((node_inser >= 0) && (node_inser < gain_step_tdomain)) {
                cubic_interpolation = 0;

                result[0] = gain_t1;
                result[gain_step_tdomain] = gain_t2;

                a = 0.5f*(slope_t2 - slope_t1) / node_inser_float;
                b = slope_t1;
                c = gain_t1;
                for (n=1; n<node_inser; n++) {
                    FLOAT32 t = (FLOAT32) n;
                    result[n] = (a * t + b ) * t + c;
                    result[n] = max(0.0f, result[n]);
                }
                a = slope_t2;
                b = gain_t2;
                for (   ; n<gain_step_tdomain; n++) {
                    FLOAT32 t = (FLOAT32) (n - gain_step_tdomain);
                    result[n] = a * t + b;
                }
            }
        }
        else if ((FLOAT32)fabs((FLOAT64)slope_t1) < (FLOAT32)fabs((FLOAT64)slope_t2))
        {
            node_inser_float = 2.0f * (gain_t1 - gain_t2 + slope_t1 * gain_step_tdomain) /
            (slope_t1 - slope_t2);
            node_inser_float = gain_step_tdomain - node_inser_float;
            node_inser = (WORD32) (0.5f + node_inser_float);
            if ((node_inser >= 0) && (node_inser < gain_step_tdomain)) {
                cubic_interpolation = 0;

                result[0] = gain_t1;
                result[gain_step_tdomain] = gain_t2;

                a = slope_t1;
                b = gain_t1;
                for (n=1; n<node_inser; n++) {
                    FLOAT32 t = (FLOAT32) n;
                    result[n] = a * t + b;
                }
                a = (slope_t2 - slope_t1) / (2.0f * (gain_step_tdomain - node_inser_float));
                b = - slope_t2;
                c = gain_t2;
                for (   ; n<gain_step_tdomain; n++) {
                    FLOAT32 t = (FLOAT32) (gain_step_tdomain-n);
                    result[n] = (a * t + b ) * t + c;
                    result[n] = max(0.0f, result[n]);
                }
            }
        }

        if (cubic_interpolation == 1)
        {
            FLOAT32 gain_step_inv = 1.0f / (FLOAT32)gain_step_tdomain;
            FLOAT32 gain_step_inv2 = gain_step_inv * gain_step_inv;

            k1 = (gain_t2 - gain_t1) * gain_step_inv2;
            k2 = slope_t2 + slope_t1;

            a = gain_step_inv * (gain_step_inv * k2 - 2.0f * k1);
            b = 3.0f * k1 - gain_step_inv * (k2 + slope_t1);
            c = slope_t1;
            d = gain_t1;

            result[0] = gain_t1;
            result[gain_step_tdomain] = gain_t2;
            for (n=1; n<gain_step_tdomain; n++) {
                FLOAT32 t = (FLOAT32) n;
                result[n] = (((a * t + b ) * t + c ) * t ) + d;
                result[n] = max(0.0f, result[n]);
            }
        }
    }
    else{

        err = impd_conv_to_linear_domain(interp_params_str, drc_band, gain1, slope1, &gain_t2, &slope_t2);
        if (err)
            return (err);

        a = 0;
        b = gain_t2;

        result[0] = gain_t2;
        result[gain_step_tdomain] = gain_t2;
        for (n=1; n<gain_step_tdomain; n++) {
            FLOAT32 t = (FLOAT32) n;
            result[n] = a * t + b;
        }
    }
    return 0;
}

WORD32
impd_advance_buf(WORD32 drc_frame_size,
              ia_gain_buffer_struct* pstr_gain_buf)
{
    WORD32 n;
    ia_interp_buf_struct* buf_interpolation;

    for (n=0; n<pstr_gain_buf->buf_interpolation_count; n++)
    {
        buf_interpolation = &(pstr_gain_buf->buf_interpolation[n]);
        buf_interpolation->prev_node = buf_interpolation->str_node;
        buf_interpolation->prev_node.time -= drc_frame_size;
        memmove(buf_interpolation->lpcm_gains, buf_interpolation->lpcm_gains + drc_frame_size, sizeof(FLOAT32) * (drc_frame_size + MAX_SIGNAL_DELAY));
    }
    return(0);
}
WORD32
impd_concatenate_segments(WORD32 drc_frame_size,
                    WORD32 drc_band,
                    ia_interp_params_struct* interp_params_str,
                    ia_spline_nodes_struct* str_spline_nodes,
                    ia_interp_buf_struct* buf_interpolation)
{
    WORD32 timePrev, duration, n, err = 0;
    FLOAT32 loc_db_gain = 0.0f, prev_db_gain, slope = 0.0f, slopePrev;

    timePrev = buf_interpolation->prev_node.time;
    prev_db_gain = buf_interpolation->prev_node.loc_db_gain;
    slopePrev = buf_interpolation->prev_node.slope;
    for (n=0; n<str_spline_nodes->num_nodes; n++)
    {
        duration = str_spline_nodes->str_node[n].time - timePrev;
        loc_db_gain = str_spline_nodes->str_node[n].loc_db_gain;
        slope = str_spline_nodes->str_node[n].slope;

        err = impd_interpolate_drc_gain(interp_params_str,
                                 drc_band,
                                 duration,
                                 prev_db_gain,
                                 loc_db_gain,
                                 slopePrev,
                                 slope,
                                 buf_interpolation->lpcm_gains + MAX_SIGNAL_DELAY + drc_frame_size + timePrev);
        if (err) return (err);

        timePrev = str_spline_nodes->str_node[n].time;
        prev_db_gain = loc_db_gain;
        slopePrev = slope;
    }

    buf_interpolation->str_node.loc_db_gain = loc_db_gain;
    buf_interpolation->str_node.slope = slope;
    buf_interpolation->str_node.time = timePrev;


    return(0);
}

WORD32
impd_get_drc_gain (ia_drc_gain_dec_struct*  p_drc_gain_dec_structs,
            ia_drc_config*                  pstr_drc_config,
            ia_drc_gain_struct*              pstr_drc_gain,
            FLOAT32                      compress,
            FLOAT32                      boost,
            WORD32                       characteristic_index,
            FLOAT32                      loudness_normalization_gain_db,
            WORD32                       sel_drc_index,
            ia_drc_gain_buffers_struct*                  drc_gain_buffers)
{
    ia_drc_params_struct* ia_drc_params_struct = &(p_drc_gain_dec_structs->ia_drc_params_struct);
    WORD32 drc_instructions_index = ia_drc_params_struct->sel_drc_array[sel_drc_index].drc_instructions_index;
    if (drc_instructions_index >= 0)
    {
        WORD32 b, g, gainElementIndex, err = 0;
        WORD32 parametricDrcInstanceIndex = 0;
        ia_interp_params_struct interp_params_str = {0};

        ia_drc_instructions_struct* str_drc_instruction_str = &(pstr_drc_config->str_drc_instruction_str[drc_instructions_index]);
        WORD32 drc_set_effect = str_drc_instruction_str->drc_set_effect;
        WORD32 num_drc_ch_groups = str_drc_instruction_str->num_drc_ch_groups;
        ia_uni_drc_coeffs_struct* str_p_loc_drc_coefficients_uni_drc = NULL;
        WORD32 drc_coeff_idx = ia_drc_params_struct->sel_drc_array[sel_drc_index].drc_coeff_idx;
        if (drc_coeff_idx >= 0)
        {
            str_p_loc_drc_coefficients_uni_drc = &(pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[drc_coeff_idx]);
            interp_params_str.interpolation_loud_eq = 0;
        }
        else
        {
            return (UNEXPECTED_ERROR);
        }

        interp_params_str.loudness_normalization_gain_db = loudness_normalization_gain_db;
        interp_params_str.characteristic_index = characteristic_index;
        interp_params_str.compress = compress;
        interp_params_str.boost = boost;
        interp_params_str.limiter_peak_target_present = str_drc_instruction_str->limiter_peak_target_present;
        interp_params_str.limiter_peak_target = str_drc_instruction_str->limiter_peak_target;

        if ( ((drc_set_effect & (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) == 0) && (drc_set_effect != EFFECT_BIT_FADE) && (drc_set_effect != EFFECT_BIT_CLIPPING))
        {
            interp_params_str.gain_modification_flag = 1;
        }
        else
        {
            interp_params_str.gain_modification_flag = 0;
        }
        if (drc_set_effect & (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF))
        {
            interp_params_str.ducking_flag = 1;
        }
        else
        {
            interp_params_str.ducking_flag = 0;
        }
        if (drc_set_effect == EFFECT_BIT_CLIPPING)
        {
            interp_params_str.clipping_flag = 1;
        }
        else
        {
            interp_params_str.clipping_flag = 0;
        }

        err = impd_advance_buf(ia_drc_params_struct->drc_frame_size, &(drc_gain_buffers->pstr_gain_buf[sel_drc_index]));
        if (err) return (err);

        gainElementIndex = 0;
        for(g=0; g<num_drc_ch_groups; g++)
        {
            WORD32 gainSet = 0;
            WORD32 num_drc_bands = 0;
            interp_params_str.gain_interpolation_type = str_drc_instruction_str->gain_interpolation_type_for_channel_group[g];
            interp_params_str.delta_tmin = str_drc_instruction_str->time_delta_min_for_channel_group[g];
            interp_params_str.pstr_ducking_modifiers = &(str_drc_instruction_str->str_ducking_modifiers_for_channel_group[g]);
            interp_params_str.pstr_gain_modifiers = &(str_drc_instruction_str->str_gain_modifiers_of_ch_group[g]);
            if (str_drc_instruction_str->ch_group_parametric_drc_flag[g] == 0) {
                gainSet = str_drc_instruction_str->gain_set_index_for_channel_group[g];
                num_drc_bands = str_drc_instruction_str->band_count_of_ch_group[g];
                for(b=0; b<num_drc_bands; b++)
                {
                    ia_gain_params_struct* gain_params = &(str_p_loc_drc_coefficients_uni_drc->gain_set_params[gainSet].gain_params[b]);
                    WORD32 seq                                                 = gain_params->gain_seq_idx;
                    interp_params_str.drc_characteristic_present            = gain_params->drc_characteristic_present;
                    interp_params_str.drc_source_characteristic_cicp_format = gain_params->drc_characteristic_format_is_cicp;
                    interp_params_str.source_drc_characteristic             = gain_params->drc_characteristic;
                    interp_params_str.split_source_characteristic_left       = &(str_p_loc_drc_coefficients_uni_drc->str_split_characteristic_left [gain_params->drc_characteristic_left_index]);
                    interp_params_str.split_source_characteristic_right      = &(str_p_loc_drc_coefficients_uni_drc->str_split_characteristic_right[gain_params->drc_characteristic_right_index]);
                    interp_params_str.split_target_characteristic_left       = &(str_p_loc_drc_coefficients_uni_drc->str_split_characteristic_left [interp_params_str.pstr_gain_modifiers->target_characteristic_left_index[b]]);
                    interp_params_str.split_target_characteristic_right      = &(str_p_loc_drc_coefficients_uni_drc->str_split_characteristic_right[interp_params_str.pstr_gain_modifiers->target_characteristic_right_index[b]]);
                    err = impd_concatenate_segments (ia_drc_params_struct->drc_frame_size,
                                               b,
                                               &interp_params_str,
                                               &(pstr_drc_gain->drc_gain_sequence[seq].str_spline_nodes[0]),
                                               &(drc_gain_buffers->pstr_gain_buf[sel_drc_index].buf_interpolation[gainElementIndex]));
                    if (err) return (err);
                    gainElementIndex++;
                }
            } else {
                if (ia_drc_params_struct->sub_band_domain_mode == SUBBAND_DOMAIN_MODE_OFF && !(p_drc_gain_dec_structs->parametricdrc_params.str_parametric_drc_instance_params[parametricDrcInstanceIndex].parametric_drc_type == PARAM_DRC_TYPE_LIM)) {
                    err = impd_parametric_drc_instance_process (p_drc_gain_dec_structs->audio_in_out_buf.audio_in_out_buf,
                                                        NULL,
                                                        NULL,
                                                        &p_drc_gain_dec_structs->parametricdrc_params,
                                                        &p_drc_gain_dec_structs->parametricdrc_params.str_parametric_drc_instance_params[parametricDrcInstanceIndex]);
                    if (err) return (err);

                    err = impd_concatenate_segments(ia_drc_params_struct->drc_frame_size,
                                              0,
                                              &interp_params_str,
                                              &p_drc_gain_dec_structs->parametricdrc_params.str_parametric_drc_instance_params[parametricDrcInstanceIndex].str_spline_nodes,
                                              &(drc_gain_buffers->pstr_gain_buf[sel_drc_index].buf_interpolation[gainElementIndex]));
                    if (err) return (err);
                } else if (ia_drc_params_struct->sub_band_domain_mode == SUBBAND_DOMAIN_MODE_OFF && p_drc_gain_dec_structs->parametricdrc_params.str_parametric_drc_instance_params[parametricDrcInstanceIndex].parametric_drc_type == PARAM_DRC_TYPE_LIM) {
                    FLOAT32* lpcm_gains = (drc_gain_buffers->pstr_gain_buf[sel_drc_index].buf_interpolation[gainElementIndex]).lpcm_gains + MAX_SIGNAL_DELAY;
                    err = impd_parametric_lim_type_drc_process(p_drc_gain_dec_structs->audio_in_out_buf.audio_in_out_buf,
                                                      loudness_normalization_gain_db,
                                                      &p_drc_gain_dec_structs->parametricdrc_params.str_parametric_drc_instance_params[parametricDrcInstanceIndex].str_parametric_drc_type_lim_params,
                                                      lpcm_gains);
                    if (err) return (err);
                } else if (ia_drc_params_struct->sub_band_domain_mode != SUBBAND_DOMAIN_MODE_OFF && !(p_drc_gain_dec_structs->parametricdrc_params.str_parametric_drc_instance_params[parametricDrcInstanceIndex].parametric_drc_type == PARAM_DRC_TYPE_LIM)) {
                    err = impd_parametric_drc_instance_process (NULL,
                                                        p_drc_gain_dec_structs->audio_in_out_buf.audio_real_buff,
                                                        p_drc_gain_dec_structs->audio_in_out_buf.audio_imag_buff,
                                                        &p_drc_gain_dec_structs->parametricdrc_params,
                                                        &p_drc_gain_dec_structs->parametricdrc_params.str_parametric_drc_instance_params[parametricDrcInstanceIndex]);
                    if (err) return (err);

                    err = impd_concatenate_segments(ia_drc_params_struct->drc_frame_size,
                                              0,
                                              &interp_params_str,
                                              &p_drc_gain_dec_structs->parametricdrc_params.str_parametric_drc_instance_params[parametricDrcInstanceIndex].str_spline_nodes,
                                              &(drc_gain_buffers->pstr_gain_buf[sel_drc_index].buf_interpolation[gainElementIndex]));
                    if (err) return (err);

    } else {
                    return (UNEXPECTED_ERROR);
    }
                gainElementIndex++;
                parametricDrcInstanceIndex++;
    }
        }
    }
    return (0);
}