liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
 * Copyright (C) 2003 - 2016 Sony Corporation
 *
 * 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.
 */
 
#include "ldaclib.h"
#include "ldac.h"
 
#define LDACLIB_MAJOR_VERSION  01
#define LDACLIB_MINOR_VERSION  00
#define LDACLIB_BRANCH_VERSION 00
 
/***************************************************************************************************
    Local Assert Functions
***************************************************************************************************/
static int ldaclib_assert_sampling_rate_index(
int smplrate_id)
{
    if ((LDAC_SMPLRATEID_0 <= smplrate_id) && (smplrate_id < LDAC_NSMPLRATEID)) {
        return LDAC_TRUE;
    }
    else {
        return LDAC_FALSE;
    }
}
 
static int ldaclib_assert_supported_sampling_rate_index(
int smplrate_id)
{
    if ((LDAC_SMPLRATEID_0 <= smplrate_id) && (smplrate_id < LDAC_NSUPSMPLRATEID)) {
        return LDAC_TRUE;
    }
    else {
        return LDAC_FALSE;
    }
}
 
static int ldaclib_assert_channel_config_index(
int chconfig_id)
{
    if ((chconfig_id == LDAC_CHCONFIGID_MN)
            || (chconfig_id == LDAC_CHCONFIGID_DL) || (chconfig_id == LDAC_CHCONFIGID_ST)) {
        return LDAC_TRUE;
    }
    else {
        return LDAC_FALSE;
    }
}
 
static int ldaclib_assert_channel(
int ch)
{
    if ((ch == LDAC_CHANNEL_1CH) || (ch == LDAC_CHANNEL_2CH)) {
        return LDAC_TRUE;
    }
    else {
        return LDAC_FALSE;
    }
}
 
static int ldaclib_assert_frame_length(
int frame_length)
{
    if ((0 < frame_length) && (frame_length <= LDAC_MAXNBYTES)) {
        return LDAC_TRUE;
    }
    else {
        return LDAC_FALSE;
    }
}
 
static int ldaclib_assert_supported_frame_length(
int frame_length,
int chconfig_id)
{
    if (chconfig_id == LDAC_CHCONFIGID_MN) {
        if ((LDAC_MINSUPNBYTES/2 <= frame_length) && (frame_length <= LDAC_MAXSUPNBYTES/2)) {
            return LDAC_TRUE;
        }
        else {
            return LDAC_FALSE;
        }
    }
    else if ((chconfig_id == LDAC_CHCONFIGID_DL) || (chconfig_id == LDAC_CHCONFIGID_ST)) {
        if ((LDAC_MINSUPNBYTES <= frame_length) && (frame_length <= LDAC_MAXSUPNBYTES)) {
            return LDAC_TRUE;
        }
        else {
            return LDAC_FALSE;
        }
    }
    else {
        return LDAC_FALSE;
    }
}
 
static int ldaclib_assert_frame_status(
int frame_status)
{
    if ((LDAC_FRMSTAT_LEV_0 <= frame_status) && (frame_status <= LDAC_FRMSTAT_LEV_3)) {
        return LDAC_TRUE;
    }
    else {
        return LDAC_FALSE;
    }
}
 
static int ldaclib_assert_nlnn_shift(
int nlnn_shift)
{
    if ((-2 <= nlnn_shift) && (nlnn_shift < LDAC_NSFTSTEP-2)) {
        return LDAC_TRUE;
    }
    else {
        return LDAC_FALSE;
    }
}
 
static int ldaclib_assert_sample_format(
LDAC_SMPL_FMT_T sample_format)
{
#ifndef _32BIT_FIXED_POINT
    if ((LDAC_SMPL_FMT_S16 <= sample_format) && (sample_format <= LDAC_SMPL_FMT_F32)) {
#else /* _32BIT_FIXED_POINT */
    if ((LDAC_SMPL_FMT_S16 <= sample_format) && (sample_format <= LDAC_SMPL_FMT_S32)) {
#endif /* _32BIT_FIXED_POINT */
        return LDAC_TRUE;
    }
    else {
        return LDAC_FALSE;
    }
}
 
 
/***************************************************************************************************
    Common API Functions
***************************************************************************************************/
 
/***************************************************************************************************
    Get Library Version
***************************************************************************************************/
DECLSPEC int ldaclib_get_version(void) {
    return (LDACLIB_MAJOR_VERSION<<16) | (LDACLIB_MINOR_VERSION<<8) | LDACLIB_BRANCH_VERSION;
}
 
DECLSPEC int ldaclib_get_major_version(void) {
    return LDACLIB_MAJOR_VERSION;
}
 
DECLSPEC int ldaclib_get_minor_version(void) {
    return LDACLIB_MINOR_VERSION;
}
 
DECLSPEC int ldaclib_get_branch_version(void) {
    return LDACLIB_BRANCH_VERSION;
}
 
/***************************************************************************************************
    Get Basic Parameters
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_get_sampling_rate_index(
int smplrate,
int *p_smplrate_id)
{
    if (smplrate == 44100) {
        *p_smplrate_id = LDAC_SMPLRATEID_0;
    }
    else if (smplrate == 48000) {
        *p_smplrate_id = LDAC_SMPLRATEID_1;
    }
    else if (smplrate == 88200) {
        *p_smplrate_id = LDAC_SMPLRATEID_2;
    }
    else if (smplrate == 96000) {
        *p_smplrate_id = LDAC_SMPLRATEID_3;
    }
    else {
        return LDAC_E_FAIL;
    }
 
    return LDAC_S_OK;
}
 
DECLSPEC LDAC_RESULT ldaclib_get_sampling_rate(
int smplrate_id,
int *p_smplrate)
{
    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
        return LDAC_E_FAIL;
    }
    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
        return LDAC_E_FAIL;
    }
 
    *p_smplrate = ga_smplrate_ldac[smplrate_id];
 
    return LDAC_S_OK;
}
 
DECLSPEC LDAC_RESULT ldaclib_get_frame_samples(
int smplrate_id,
int *p_framesmpls)
{
    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
        return LDAC_E_FAIL;
    }
    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
        return LDAC_E_FAIL;
    }
 
    *p_framesmpls = ga_framesmpls_ldac[smplrate_id];
 
    return LDAC_S_OK;
}
 
DECLSPEC LDAC_RESULT ldaclib_get_nlnn(
int smplrate_id,
int *p_nlnn)
{
    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
        return LDAC_E_FAIL;
    }
    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
        return LDAC_E_FAIL;
    }
 
    *p_nlnn = ga_ln_framesmpls_ldac[smplrate_id];
 
    return LDAC_S_OK;
}
 
DECLSPEC LDAC_RESULT ldaclib_get_channel(
int chconfig_id,
int *p_ch)
{
    if (!ldaclib_assert_channel_config_index(chconfig_id)) {
        return LDAC_E_FAIL;
    }
 
    *p_ch = ga_ch_ldac[chconfig_id];
 
    return LDAC_S_OK;
}
 
DECLSPEC LDAC_RESULT ldaclib_get_channel_config_index(
int ch,
int *p_chconfig_id)
{
    if (!ldaclib_assert_channel(ch)) {
        return LDAC_E_FAIL;
    }
 
    *p_chconfig_id = ga_chconfig_id_ldac[ch];
 
    return LDAC_S_OK;
}
 
DECLSPEC LDAC_RESULT ldaclib_check_nlnn_shift(
int smplrate_id,
int nlnn_shift)
{
    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
        return LDAC_E_FAIL;
    }
    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
        return LDAC_E_FAIL;
    }
    if (!ldaclib_assert_nlnn_shift(nlnn_shift)) {
        return LDAC_E_FAIL;
    }
 
    if (gaa_nlnn_shift_ldac[smplrate_id][nlnn_shift+2] < 0) {
        return LDAC_E_FAIL;
    }
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Get Handle
***************************************************************************************************/
DECLSPEC HANDLE_LDAC ldaclib_get_handle(
void)
{
    HANDLE_LDAC hData;
 
    hData = (HANDLE_LDAC)malloc(sizeof(HANDLE_LDAC_STRUCT));
    if (hData != (HANDLE_LDAC)NULL) {
        clear_data_ldac(hData, sizeof(HANDLE_LDAC_STRUCT));
        hData->sfinfo.p_mempos = (char *)NULL;
        hData->error_code = LDAC_ERR_NONE;
    }
 
    return hData;
}
 
/***************************************************************************************************
    Free Handle
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_free_handle(
HANDLE_LDAC hData)
{
    if (hData != (HANDLE_LDAC)NULL) {
        if (hData->sfinfo.p_mempos != (char *)NULL) {
            return LDAC_S_OK;
        }
 
        free(hData);
    }
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Set Configuration Information
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_set_config_info(
HANDLE_LDAC hData,
int smplrate_id,
int chconfig_id,
int frame_length,
int frame_status)
{
    CFG *p_cfg = &hData->sfinfo.cfg;
 
    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
        hData->error_code = LDAC_ERR_ASSERT_SAMPLING_RATE;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
        hData->error_code = LDAC_ERR_ASSERT_SUP_SAMPLING_RATE;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_channel_config_index(chconfig_id)) {
        hData->error_code = LDAC_ERR_ASSERT_CHANNEL_CONFIG;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_frame_length(frame_length)) {
        hData->error_code = LDAC_ERR_ASSERT_FRAME_LENGTH;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_supported_frame_length(frame_length, chconfig_id)) {
        hData->error_code = LDAC_ERR_ASSERT_SUP_FRAME_LENGTH;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_frame_status(frame_status)) {
        hData->error_code = LDAC_ERR_ASSERT_FRAME_STATUS;
        return LDAC_E_FAIL;
    }
 
    p_cfg->smplrate_id = smplrate_id;
    p_cfg->chconfig_id = chconfig_id;
    p_cfg->frame_length = frame_length;
    p_cfg->frame_status = frame_status;
 
    ldaclib_get_channel(chconfig_id, &p_cfg->ch);
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Get Configuration Information
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_get_config_info(
HANDLE_LDAC hData,
int *p_smplrate_id,
int *p_chconfig_id,
int *p_frame_length,
int *p_frame_status)
{
    CFG *p_cfg = &hData->sfinfo.cfg;
 
    *p_smplrate_id = p_cfg->smplrate_id;
    *p_chconfig_id = p_cfg->chconfig_id;
    *p_frame_length = p_cfg->frame_length;
    *p_frame_status = p_cfg->frame_status;
 
    return LDAC_S_OK;
}
 
 
/***************************************************************************************************
    Set Frame Header
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_set_frame_header(
HANDLE_LDAC hData,
unsigned char *p_stream,
int smplrate_id,
int chconfig_id,
int frame_length,
int frame_status)
{
    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
        hData->error_code = LDAC_ERR_ASSERT_SAMPLING_RATE;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
        hData->error_code = LDAC_ERR_ASSERT_SUP_SAMPLING_RATE;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_channel_config_index(chconfig_id)) {
        hData->error_code = LDAC_ERR_ASSERT_CHANNEL_CONFIG;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_frame_length(frame_length)) {
        hData->error_code = LDAC_ERR_ASSERT_FRAME_LENGTH;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_supported_frame_length(frame_length, chconfig_id)) {
        hData->error_code = LDAC_ERR_ASSERT_SUP_FRAME_LENGTH;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_frame_status(frame_status)) {
        hData->error_code = LDAC_ERR_ASSERT_FRAME_STATUS;
        return LDAC_E_FAIL;
    }
 
    pack_frame_header_ldac(smplrate_id, chconfig_id, frame_length, frame_status,
            (STREAM *)p_stream);
 
    return LDAC_S_OK;
}
 
 
/***************************************************************************************************
    Encoder API Functions
***************************************************************************************************/
 
/***************************************************************************************************
    Get Encoder Setting
***************************************************************************************************/
#define LDAC_ENC_NSETTING 15
#define LDAC_ENC_NPROPERTY 9
 
static const int saa_encode_setting_ldac[LDAC_ENC_NSETTING][LDAC_ENC_NPROPERTY] = {
    {0, 512,  17,   0,  28,  44,   8,  24,   0},
    {0, 256,  17,   0,  28,  44,   6,  22,   0},
#ifdef MODIFY_LDAC_ENC_SETTING_FOR_ABR_DEBUG  // See file "ldacBT_abr.h" for description
    {0, 164,  16,   0,  18,  32,   7,  23,   0},
    {0, 110,   8,   0,  16,  32,  10,  31,   0},
    {0,  82,   6,   0,  16,  32,  12,  31,   0},
    {0,  66,   4,   0,  14,  26,  12,  31,   0},
    {0,  54,   2,   0,  14,  26,  12,  31,   0},
    {0,  46,   2,   1,  10,  26,  12,  31,   0},
    {0,  40,   2,   2,  10,  26,  12,  31,   0},
    {0,  36,   2,   2,   8,  26,  12,  31,   0},
    {0,  32,   2,   2,   8,  26,  16,  31,   0},
    {0,  30,   2,   2,   4,  26,  16,  31,   0},
    {0,  26,   2,   3,   4,  26,  16,  31,   0},
    {0,  24,   2,   3,   4,  26,  16,  31,   0},
#else
    {0, 164,  16,   0,  18,  32,   7,  23,   0},
    {0, 110,  13,   0,  16,  32,  10,  31,   0},
    {0,  82,  12,   0,  16,  32,  12,  31,   0},
    {0,  66,  11,   0,  14,  26,  12,  31,   0},
    {0,  54,  10,   0,  14,  26,  12,  31,   0},
    {0,  46,   9,   1,  10,  26,  12,  31,   0},
    {0,  40,   8,   2,  10,  26,  12,  31,   0},
    {0,  36,   7,   2,   8,  26,  12,  31,   0},
    {0,  32,   6,   2,   8,  26,  16,  31,   0},
    {0,  30,   5,   2,   4,  26,  16,  31,   0},
    {0,  26,   4,   3,   4,  26,  16,  31,   0},
    {0,  24,   3,   3,   4,  26,  16,  31,   0},
#endif
    {0,  22,   2,   3,   4,  26,  16,  31,   0},
};
 
DECLSPEC LDAC_RESULT ldaclib_get_encode_setting(
int nbytes_ch,
int smplrate_id,
int *p_nbands,
int *p_grad_mode,
int *p_grad_qu_l,
int *p_grad_qu_h,
int *p_grad_os_l,
int *p_grad_os_h,
int *p_abc_status)
{
    int i, id;
 
    id = LDAC_ENC_NSETTING-1;
    for (i = LDAC_ENC_NSETTING-1; i >= 0; i--) {
        if (nbytes_ch >= saa_encode_setting_ldac[i][1]) {
            id = i;
        }
    }
 
    *p_nbands = min_ldac(saa_encode_setting_ldac[id][2], ga_max_nbands_ldac[smplrate_id]);
    *p_grad_mode = saa_encode_setting_ldac[id][3];
    *p_grad_qu_l = saa_encode_setting_ldac[id][4];
    *p_grad_qu_h = saa_encode_setting_ldac[id][5];
    *p_grad_os_l = saa_encode_setting_ldac[id][6];
    *p_grad_os_h = saa_encode_setting_ldac[id][7];
    *p_abc_status = saa_encode_setting_ldac[id][8];
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Set Frame Length
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_set_encode_frame_length(
HANDLE_LDAC hData,
int frame_length)
{
    CFG *p_cfg = &hData->sfinfo.cfg;
 
    if (!ldaclib_assert_frame_length(frame_length)) {
        hData->error_code = LDAC_ERR_ASSERT_FRAME_LENGTH;
        return LDAC_E_FAIL;
    }
 
    if (!ldaclib_assert_supported_frame_length(frame_length, p_cfg->chconfig_id)) {
        hData->error_code = LDAC_ERR_ASSERT_SUP_FRAME_LENGTH;
        return LDAC_E_FAIL;
    }
 
    p_cfg->frame_length = frame_length;
 
    calc_initial_bits_ldac(&hData->sfinfo);
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Get Frame Length
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_get_encode_frame_length(
HANDLE_LDAC hData,
int *p_frame_length)
{
    CFG *p_cfg = &hData->sfinfo.cfg;
 
    *p_frame_length = p_cfg->frame_length;
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Set Information
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_set_encode_info(
HANDLE_LDAC hData,
int nbands,
int grad_mode,
int grad_qu_l,
int grad_qu_h,
int grad_os_l,
int grad_os_h,
int abc_status)
{
    if ((nbands < LDAC_BAND_OFFSET) ||
            (ga_max_nbands_ldac[hData->sfinfo.cfg.smplrate_id] < nbands)) {
        hData->error_code = LDAC_ERR_ENC_ILL_NBANDS;
        return LDAC_E_FAIL;
    }
 
    if ((grad_mode < LDAC_MODE_0) || (LDAC_MODE_3 < grad_mode)) {
            hData->error_code = LDAC_ERR_ENC_ILL_GRADMODE;
            return LDAC_E_FAIL;
    }
 
    if (grad_mode == LDAC_MODE_0) {
        if ((grad_qu_l < 0) || (LDAC_MAXGRADQU <= grad_qu_l)) {
            hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_A;
            return LDAC_E_FAIL;
        }
 
        if ((grad_qu_h < 1) || (LDAC_MAXGRADQU+1 <= grad_qu_h) || (grad_qu_h < grad_qu_l)) {
            hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_B;
            return LDAC_E_FAIL;
        }
 
        if ((grad_os_h < 0) || (LDAC_NIDSF <= grad_os_h)) {
            hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_C;
            return LDAC_E_FAIL;
        }
    }
    else {
        if ((grad_qu_l < 0) || (LDAC_DEFGRADQUH < grad_qu_l)) {
            hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_A;
            return LDAC_E_FAIL;
        }
    }
 
    if ((grad_os_l < 0) || (LDAC_NIDSF <= grad_os_l)) {
        hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_D;
        return LDAC_E_FAIL;
    }
 
    hData->nbands = nbands;
    hData->grad_mode = grad_mode;
    hData->grad_qu_l = grad_qu_l;
    hData->grad_os_l = grad_os_l;
    if (grad_mode == LDAC_MODE_0) {
        hData->grad_qu_h = grad_qu_h;
        hData->grad_os_h = grad_os_h;
    }
    else {
        hData->grad_qu_h = LDAC_DEFGRADQUH;
        hData->grad_os_h = LDAC_DEFGRADOSH;
    }
    hData->abc_status = abc_status;
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Initialize
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_init_encode(
HANDLE_LDAC hData)
{
    SFINFO *p_sfinfo = &hData->sfinfo;
    LDAC_RESULT result;
 
 
    ldaclib_get_nlnn(p_sfinfo->cfg.smplrate_id, &hData->nlnn);
 
    set_mdct_table_ldac(hData->nlnn);
 
    result = init_encode_ldac(p_sfinfo);
    if (result != LDAC_S_OK) {
        hData->error_code = LDAC_ERR_ENC_INIT_ALLOC;
        return LDAC_E_FAIL;
    }
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Free
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_free_encode(
HANDLE_LDAC hData)
{
    if (hData->sfinfo.p_mempos == NULL) {
        free_encode_ldac(&hData->sfinfo);
    }
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Encode
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_encode(
HANDLE_LDAC hData,
char *ap_pcm[],
LDAC_SMPL_FMT_T sample_format,
unsigned char *p_stream,
int *p_nbytes_used)
{
    SFINFO *p_sfinfo = &hData->sfinfo;
    int loc = 0;
    int error_code;
    int frame_length;
 
 
    if (!ldaclib_assert_sample_format(sample_format)) {
        hData->error_code = LDAC_ERR_ILL_SMPL_FORMAT;
        return LDAC_E_FAIL;
    }
 
    frame_length = p_sfinfo->cfg.frame_length;
    clear_data_ldac(p_stream, frame_length*sizeof(unsigned char));
 
    set_input_pcm_ldac(p_sfinfo, ap_pcm, sample_format, hData->nlnn);
 
    proc_mdct_ldac(p_sfinfo, hData->nlnn);
 
    p_sfinfo->cfg.frame_status = ana_frame_status_ldac(p_sfinfo, hData->nlnn);
 
    error_code = encode_ldac(p_sfinfo, hData->nbands, hData->grad_mode,
            hData->grad_qu_l, hData->grad_qu_h, hData->grad_os_l, hData->grad_os_h,
            hData->abc_status);
    if (LDAC_ERROR(error_code) && !LDAC_FATAL_ERROR(error_code)) {
        int error_code2;
        error_code2 = pack_null_data_frame_ldac(p_sfinfo, (STREAM *)p_stream, &loc, p_nbytes_used);
        if (LDAC_FATAL_ERROR(error_code2)) {
            clear_data_ldac(p_stream, frame_length*sizeof(unsigned char));
            hData->error_code = error_code2;
            return LDAC_E_FAIL;
        }
        hData->error_code = error_code;
        return LDAC_S_FALSE;
    }
 
    error_code = pack_raw_data_frame_ldac(p_sfinfo, (STREAM *)p_stream, &loc, p_nbytes_used);
    if (LDAC_FATAL_ERROR(error_code)) {
        int error_code2;
        loc = 0;
        clear_data_ldac(p_stream, frame_length*sizeof(unsigned char));
        error_code2 = pack_null_data_frame_ldac(p_sfinfo, (STREAM *)p_stream, &loc, p_nbytes_used);
        if (LDAC_FATAL_ERROR(error_code2)) {
            clear_data_ldac(p_stream, frame_length*sizeof(unsigned char));
            hData->error_code = error_code2;
            return LDAC_E_FAIL;
        }
        hData->error_code = error_code;
        return LDAC_E_FAIL;
    }
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Flush Encode
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_flush_encode(
HANDLE_LDAC hData,
LDAC_SMPL_FMT_T sample_format,
unsigned char *p_stream,
int *p_nbytes_used)
{
    LDAC_RESULT result;
    int ich;
    char *ap_buf[LDAC_PRCNCH];
    int a_buf[LDAC_MAXLSU*LDAC_PRCNCH];
 
    if (!ldaclib_assert_sample_format(sample_format)) {
        hData->error_code = LDAC_ERR_ILL_SMPL_FORMAT;
        return LDAC_E_FAIL;
    }
 
    clear_data_ldac(a_buf, (LDAC_MAXLSU*LDAC_PRCNCH)*sizeof(int));
 
    for (ich = 0; ich < LDAC_PRCNCH; ich++) {
        ap_buf[ich] = (char *)(a_buf + ich * LDAC_MAXLSU);
    }
 
    result = ldaclib_encode(hData, ap_buf, sample_format, p_stream, p_nbytes_used);
 
    return result;
}
 
 
 
 
/***************************************************************************************************
    Error Code Dispatch
***************************************************************************************************/
 
/***************************************************************************************************
    Clear Error Code at Handle Level
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_clear_error_code(
HANDLE_LDAC hData)
{
    hData->error_code = LDAC_ERR_NONE;
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Get Error Code at Handle Level
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_get_error_code(
HANDLE_LDAC hData,
int *p_error_code)
{
    *p_error_code = hData->error_code;
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Clear Error Code at Internal Block Level
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_clear_internal_error_code(
HANDLE_LDAC hData)
{
    hData->sfinfo.error_code = LDAC_ERR_NONE;
 
    return LDAC_S_OK;
}
 
/***************************************************************************************************
    Get Error Code at Internal Block Level
***************************************************************************************************/
DECLSPEC LDAC_RESULT ldaclib_get_internal_error_code(
HANDLE_LDAC hData,
int *p_error_code)
{
    *p_error_code = hData->sfinfo.error_code;
 
    return LDAC_S_OK;
}