1/*
2 * Copyright (C) 2003 - 2016 Sony Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ldaclib.h"
18#include "ldac.h"
19
20#define LDACLIB_MAJOR_VERSION  01
21#define LDACLIB_MINOR_VERSION  00
22#define LDACLIB_BRANCH_VERSION 00
23
24/***************************************************************************************************
25    Local Assert Functions
26***************************************************************************************************/
27static int ldaclib_assert_sampling_rate_index(
28int smplrate_id)
29{
30    if ((LDAC_SMPLRATEID_0 <= smplrate_id) && (smplrate_id < LDAC_NSMPLRATEID)) {
31        return LDAC_TRUE;
32    }
33    else {
34        return LDAC_FALSE;
35    }
36}
37
38static int ldaclib_assert_supported_sampling_rate_index(
39int smplrate_id)
40{
41    if ((LDAC_SMPLRATEID_0 <= smplrate_id) && (smplrate_id < LDAC_NSUPSMPLRATEID)) {
42        return LDAC_TRUE;
43    }
44    else {
45        return LDAC_FALSE;
46    }
47}
48
49static int ldaclib_assert_channel_config_index(
50int chconfig_id)
51{
52    if ((chconfig_id == LDAC_CHCONFIGID_MN)
53            || (chconfig_id == LDAC_CHCONFIGID_DL) || (chconfig_id == LDAC_CHCONFIGID_ST)) {
54        return LDAC_TRUE;
55    }
56    else {
57        return LDAC_FALSE;
58    }
59}
60
61static int ldaclib_assert_channel(
62int ch)
63{
64    if ((ch == LDAC_CHANNEL_1CH) || (ch == LDAC_CHANNEL_2CH)) {
65        return LDAC_TRUE;
66    }
67    else {
68        return LDAC_FALSE;
69    }
70}
71
72static int ldaclib_assert_frame_length(
73int frame_length)
74{
75    if ((0 < frame_length) && (frame_length <= LDAC_MAXNBYTES)) {
76        return LDAC_TRUE;
77    }
78    else {
79        return LDAC_FALSE;
80    }
81}
82
83static int ldaclib_assert_supported_frame_length(
84int frame_length,
85int chconfig_id)
86{
87    if (chconfig_id == LDAC_CHCONFIGID_MN) {
88        if ((LDAC_MINSUPNBYTES/2 <= frame_length) && (frame_length <= LDAC_MAXSUPNBYTES/2)) {
89            return LDAC_TRUE;
90        }
91        else {
92            return LDAC_FALSE;
93        }
94    }
95    else if ((chconfig_id == LDAC_CHCONFIGID_DL) || (chconfig_id == LDAC_CHCONFIGID_ST)) {
96        if ((LDAC_MINSUPNBYTES <= frame_length) && (frame_length <= LDAC_MAXSUPNBYTES)) {
97            return LDAC_TRUE;
98        }
99        else {
100            return LDAC_FALSE;
101        }
102    }
103    else {
104        return LDAC_FALSE;
105    }
106}
107
108static int ldaclib_assert_frame_status(
109int frame_status)
110{
111    if ((LDAC_FRMSTAT_LEV_0 <= frame_status) && (frame_status <= LDAC_FRMSTAT_LEV_3)) {
112        return LDAC_TRUE;
113    }
114    else {
115        return LDAC_FALSE;
116    }
117}
118
119static int ldaclib_assert_nlnn_shift(
120int nlnn_shift)
121{
122    if ((-2 <= nlnn_shift) && (nlnn_shift < LDAC_NSFTSTEP-2)) {
123        return LDAC_TRUE;
124    }
125    else {
126        return LDAC_FALSE;
127    }
128}
129
130static int ldaclib_assert_sample_format(
131LDAC_SMPL_FMT_T sample_format)
132{
133#ifndef _32BIT_FIXED_POINT
134    if ((LDAC_SMPL_FMT_S16 <= sample_format) && (sample_format <= LDAC_SMPL_FMT_F32)) {
135#else /* _32BIT_FIXED_POINT */
136    if ((LDAC_SMPL_FMT_S16 <= sample_format) && (sample_format <= LDAC_SMPL_FMT_S32)) {
137#endif /* _32BIT_FIXED_POINT */
138        return LDAC_TRUE;
139    }
140    else {
141        return LDAC_FALSE;
142    }
143}
144
145
146/***************************************************************************************************
147    Common API Functions
148***************************************************************************************************/
149
150/***************************************************************************************************
151    Get Library Version
152***************************************************************************************************/
153DECLSPEC int ldaclib_get_version(void) {
154    return (LDACLIB_MAJOR_VERSION<<16) | (LDACLIB_MINOR_VERSION<<8) | LDACLIB_BRANCH_VERSION;
155}
156
157DECLSPEC int ldaclib_get_major_version(void) {
158    return LDACLIB_MAJOR_VERSION;
159}
160
161DECLSPEC int ldaclib_get_minor_version(void) {
162    return LDACLIB_MINOR_VERSION;
163}
164
165DECLSPEC int ldaclib_get_branch_version(void) {
166    return LDACLIB_BRANCH_VERSION;
167}
168
169/***************************************************************************************************
170    Get Basic Parameters
171***************************************************************************************************/
172DECLSPEC LDAC_RESULT ldaclib_get_sampling_rate_index(
173int smplrate,
174int *p_smplrate_id)
175{
176    if (smplrate == 44100) {
177        *p_smplrate_id = LDAC_SMPLRATEID_0;
178    }
179    else if (smplrate == 48000) {
180        *p_smplrate_id = LDAC_SMPLRATEID_1;
181    }
182    else if (smplrate == 88200) {
183        *p_smplrate_id = LDAC_SMPLRATEID_2;
184    }
185    else if (smplrate == 96000) {
186        *p_smplrate_id = LDAC_SMPLRATEID_3;
187    }
188    else {
189        return LDAC_E_FAIL;
190    }
191
192    return LDAC_S_OK;
193}
194
195DECLSPEC LDAC_RESULT ldaclib_get_sampling_rate(
196int smplrate_id,
197int *p_smplrate)
198{
199    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
200        return LDAC_E_FAIL;
201    }
202    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
203        return LDAC_E_FAIL;
204    }
205
206    *p_smplrate = ga_smplrate_ldac[smplrate_id];
207
208    return LDAC_S_OK;
209}
210
211DECLSPEC LDAC_RESULT ldaclib_get_frame_samples(
212int smplrate_id,
213int *p_framesmpls)
214{
215    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
216        return LDAC_E_FAIL;
217    }
218    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
219        return LDAC_E_FAIL;
220    }
221
222    *p_framesmpls = ga_framesmpls_ldac[smplrate_id];
223
224    return LDAC_S_OK;
225}
226
227DECLSPEC LDAC_RESULT ldaclib_get_nlnn(
228int smplrate_id,
229int *p_nlnn)
230{
231    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
232        return LDAC_E_FAIL;
233    }
234    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
235        return LDAC_E_FAIL;
236    }
237
238    *p_nlnn = ga_ln_framesmpls_ldac[smplrate_id];
239
240    return LDAC_S_OK;
241}
242
243DECLSPEC LDAC_RESULT ldaclib_get_channel(
244int chconfig_id,
245int *p_ch)
246{
247    if (!ldaclib_assert_channel_config_index(chconfig_id)) {
248        return LDAC_E_FAIL;
249    }
250
251    *p_ch = ga_ch_ldac[chconfig_id];
252
253    return LDAC_S_OK;
254}
255
256DECLSPEC LDAC_RESULT ldaclib_get_channel_config_index(
257int ch,
258int *p_chconfig_id)
259{
260    if (!ldaclib_assert_channel(ch)) {
261        return LDAC_E_FAIL;
262    }
263
264    *p_chconfig_id = ga_chconfig_id_ldac[ch];
265
266    return LDAC_S_OK;
267}
268
269DECLSPEC LDAC_RESULT ldaclib_check_nlnn_shift(
270int smplrate_id,
271int nlnn_shift)
272{
273    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
274        return LDAC_E_FAIL;
275    }
276    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
277        return LDAC_E_FAIL;
278    }
279    if (!ldaclib_assert_nlnn_shift(nlnn_shift)) {
280        return LDAC_E_FAIL;
281    }
282
283    if (gaa_nlnn_shift_ldac[smplrate_id][nlnn_shift+2] < 0) {
284        return LDAC_E_FAIL;
285    }
286
287    return LDAC_S_OK;
288}
289
290/***************************************************************************************************
291    Get Handle
292***************************************************************************************************/
293DECLSPEC HANDLE_LDAC ldaclib_get_handle(
294void)
295{
296    HANDLE_LDAC hData;
297
298    hData = (HANDLE_LDAC)malloc(sizeof(HANDLE_LDAC_STRUCT));
299    if (hData != (HANDLE_LDAC)NULL) {
300        clear_data_ldac(hData, sizeof(HANDLE_LDAC_STRUCT));
301        hData->sfinfo.p_mempos = (char *)NULL;
302        hData->error_code = LDAC_ERR_NONE;
303    }
304
305    return hData;
306}
307
308/***************************************************************************************************
309    Free Handle
310***************************************************************************************************/
311DECLSPEC LDAC_RESULT ldaclib_free_handle(
312HANDLE_LDAC hData)
313{
314    if (hData != (HANDLE_LDAC)NULL) {
315        if (hData->sfinfo.p_mempos != (char *)NULL) {
316            return LDAC_S_OK;
317        }
318
319        free(hData);
320    }
321
322    return LDAC_S_OK;
323}
324
325/***************************************************************************************************
326    Set Configuration Information
327***************************************************************************************************/
328DECLSPEC LDAC_RESULT ldaclib_set_config_info(
329HANDLE_LDAC hData,
330int smplrate_id,
331int chconfig_id,
332int frame_length,
333int frame_status)
334{
335    CFG *p_cfg = &hData->sfinfo.cfg;
336
337    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
338        hData->error_code = LDAC_ERR_ASSERT_SAMPLING_RATE;
339        return LDAC_E_FAIL;
340    }
341
342    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
343        hData->error_code = LDAC_ERR_ASSERT_SUP_SAMPLING_RATE;
344        return LDAC_E_FAIL;
345    }
346
347    if (!ldaclib_assert_channel_config_index(chconfig_id)) {
348        hData->error_code = LDAC_ERR_ASSERT_CHANNEL_CONFIG;
349        return LDAC_E_FAIL;
350    }
351
352    if (!ldaclib_assert_frame_length(frame_length)) {
353        hData->error_code = LDAC_ERR_ASSERT_FRAME_LENGTH;
354        return LDAC_E_FAIL;
355    }
356
357    if (!ldaclib_assert_supported_frame_length(frame_length, chconfig_id)) {
358        hData->error_code = LDAC_ERR_ASSERT_SUP_FRAME_LENGTH;
359        return LDAC_E_FAIL;
360    }
361
362    if (!ldaclib_assert_frame_status(frame_status)) {
363        hData->error_code = LDAC_ERR_ASSERT_FRAME_STATUS;
364        return LDAC_E_FAIL;
365    }
366
367    p_cfg->smplrate_id = smplrate_id;
368    p_cfg->chconfig_id = chconfig_id;
369    p_cfg->frame_length = frame_length;
370    p_cfg->frame_status = frame_status;
371
372    ldaclib_get_channel(chconfig_id, &p_cfg->ch);
373
374    return LDAC_S_OK;
375}
376
377/***************************************************************************************************
378    Get Configuration Information
379***************************************************************************************************/
380DECLSPEC LDAC_RESULT ldaclib_get_config_info(
381HANDLE_LDAC hData,
382int *p_smplrate_id,
383int *p_chconfig_id,
384int *p_frame_length,
385int *p_frame_status)
386{
387    CFG *p_cfg = &hData->sfinfo.cfg;
388
389    *p_smplrate_id = p_cfg->smplrate_id;
390    *p_chconfig_id = p_cfg->chconfig_id;
391    *p_frame_length = p_cfg->frame_length;
392    *p_frame_status = p_cfg->frame_status;
393
394    return LDAC_S_OK;
395}
396
397
398/***************************************************************************************************
399    Set Frame Header
400***************************************************************************************************/
401DECLSPEC LDAC_RESULT ldaclib_set_frame_header(
402HANDLE_LDAC hData,
403unsigned char *p_stream,
404int smplrate_id,
405int chconfig_id,
406int frame_length,
407int frame_status)
408{
409    if (!ldaclib_assert_sampling_rate_index(smplrate_id)) {
410        hData->error_code = LDAC_ERR_ASSERT_SAMPLING_RATE;
411        return LDAC_E_FAIL;
412    }
413
414    if (!ldaclib_assert_supported_sampling_rate_index(smplrate_id)) {
415        hData->error_code = LDAC_ERR_ASSERT_SUP_SAMPLING_RATE;
416        return LDAC_E_FAIL;
417    }
418
419    if (!ldaclib_assert_channel_config_index(chconfig_id)) {
420        hData->error_code = LDAC_ERR_ASSERT_CHANNEL_CONFIG;
421        return LDAC_E_FAIL;
422    }
423
424    if (!ldaclib_assert_frame_length(frame_length)) {
425        hData->error_code = LDAC_ERR_ASSERT_FRAME_LENGTH;
426        return LDAC_E_FAIL;
427    }
428
429    if (!ldaclib_assert_supported_frame_length(frame_length, chconfig_id)) {
430        hData->error_code = LDAC_ERR_ASSERT_SUP_FRAME_LENGTH;
431        return LDAC_E_FAIL;
432    }
433
434    if (!ldaclib_assert_frame_status(frame_status)) {
435        hData->error_code = LDAC_ERR_ASSERT_FRAME_STATUS;
436        return LDAC_E_FAIL;
437    }
438
439    pack_frame_header_ldac(smplrate_id, chconfig_id, frame_length, frame_status,
440            (STREAM *)p_stream);
441
442    return LDAC_S_OK;
443}
444
445
446/***************************************************************************************************
447    Encoder API Functions
448***************************************************************************************************/
449
450/***************************************************************************************************
451    Get Encoder Setting
452***************************************************************************************************/
453#define LDAC_ENC_NSETTING 15
454#define LDAC_ENC_NPROPERTY 9
455
456static const int saa_encode_setting_ldac[LDAC_ENC_NSETTING][LDAC_ENC_NPROPERTY] = {
457    {0, 512,  17,   0,  28,  44,   8,  24,   0},
458    {0, 256,  17,   0,  28,  44,   6,  22,   0},
459#ifdef MODIFY_LDAC_ENC_SETTING_FOR_ABR_DEBUG  // See file "ldacBT_abr.h" for description
460    {0, 164,  16,   0,  18,  32,   7,  23,   0},
461    {0, 110,   8,   0,  16,  32,  10,  31,   0},
462    {0,  82,   6,   0,  16,  32,  12,  31,   0},
463    {0,  66,   4,   0,  14,  26,  12,  31,   0},
464    {0,  54,   2,   0,  14,  26,  12,  31,   0},
465    {0,  46,   2,   1,  10,  26,  12,  31,   0},
466    {0,  40,   2,   2,  10,  26,  12,  31,   0},
467    {0,  36,   2,   2,   8,  26,  12,  31,   0},
468    {0,  32,   2,   2,   8,  26,  16,  31,   0},
469    {0,  30,   2,   2,   4,  26,  16,  31,   0},
470    {0,  26,   2,   3,   4,  26,  16,  31,   0},
471    {0,  24,   2,   3,   4,  26,  16,  31,   0},
472#else
473    {0, 164,  16,   0,  18,  32,   7,  23,   0},
474    {0, 110,  13,   0,  16,  32,  10,  31,   0},
475    {0,  82,  12,   0,  16,  32,  12,  31,   0},
476    {0,  66,  11,   0,  14,  26,  12,  31,   0},
477    {0,  54,  10,   0,  14,  26,  12,  31,   0},
478    {0,  46,   9,   1,  10,  26,  12,  31,   0},
479    {0,  40,   8,   2,  10,  26,  12,  31,   0},
480    {0,  36,   7,   2,   8,  26,  12,  31,   0},
481    {0,  32,   6,   2,   8,  26,  16,  31,   0},
482    {0,  30,   5,   2,   4,  26,  16,  31,   0},
483    {0,  26,   4,   3,   4,  26,  16,  31,   0},
484    {0,  24,   3,   3,   4,  26,  16,  31,   0},
485#endif
486    {0,  22,   2,   3,   4,  26,  16,  31,   0},
487};
488
489DECLSPEC LDAC_RESULT ldaclib_get_encode_setting(
490int nbytes_ch,
491int smplrate_id,
492int *p_nbands,
493int *p_grad_mode,
494int *p_grad_qu_l,
495int *p_grad_qu_h,
496int *p_grad_os_l,
497int *p_grad_os_h,
498int *p_abc_status)
499{
500    int i, id;
501
502    id = LDAC_ENC_NSETTING-1;
503    for (i = LDAC_ENC_NSETTING-1; i >= 0; i--) {
504        if (nbytes_ch >= saa_encode_setting_ldac[i][1]) {
505            id = i;
506        }
507    }
508
509    *p_nbands = min_ldac(saa_encode_setting_ldac[id][2], ga_max_nbands_ldac[smplrate_id]);
510    *p_grad_mode = saa_encode_setting_ldac[id][3];
511    *p_grad_qu_l = saa_encode_setting_ldac[id][4];
512    *p_grad_qu_h = saa_encode_setting_ldac[id][5];
513    *p_grad_os_l = saa_encode_setting_ldac[id][6];
514    *p_grad_os_h = saa_encode_setting_ldac[id][7];
515    *p_abc_status = saa_encode_setting_ldac[id][8];
516
517    return LDAC_S_OK;
518}
519
520/***************************************************************************************************
521    Set Frame Length
522***************************************************************************************************/
523DECLSPEC LDAC_RESULT ldaclib_set_encode_frame_length(
524HANDLE_LDAC hData,
525int frame_length)
526{
527    CFG *p_cfg = &hData->sfinfo.cfg;
528
529    if (!ldaclib_assert_frame_length(frame_length)) {
530        hData->error_code = LDAC_ERR_ASSERT_FRAME_LENGTH;
531        return LDAC_E_FAIL;
532    }
533
534    if (!ldaclib_assert_supported_frame_length(frame_length, p_cfg->chconfig_id)) {
535        hData->error_code = LDAC_ERR_ASSERT_SUP_FRAME_LENGTH;
536        return LDAC_E_FAIL;
537    }
538
539    p_cfg->frame_length = frame_length;
540
541    calc_initial_bits_ldac(&hData->sfinfo);
542
543    return LDAC_S_OK;
544}
545
546/***************************************************************************************************
547    Get Frame Length
548***************************************************************************************************/
549DECLSPEC LDAC_RESULT ldaclib_get_encode_frame_length(
550HANDLE_LDAC hData,
551int *p_frame_length)
552{
553    CFG *p_cfg = &hData->sfinfo.cfg;
554
555    *p_frame_length = p_cfg->frame_length;
556
557    return LDAC_S_OK;
558}
559
560/***************************************************************************************************
561    Set Information
562***************************************************************************************************/
563DECLSPEC LDAC_RESULT ldaclib_set_encode_info(
564HANDLE_LDAC hData,
565int nbands,
566int grad_mode,
567int grad_qu_l,
568int grad_qu_h,
569int grad_os_l,
570int grad_os_h,
571int abc_status)
572{
573    if ((nbands < LDAC_BAND_OFFSET) ||
574            (ga_max_nbands_ldac[hData->sfinfo.cfg.smplrate_id] < nbands)) {
575        hData->error_code = LDAC_ERR_ENC_ILL_NBANDS;
576        return LDAC_E_FAIL;
577    }
578
579    if ((grad_mode < LDAC_MODE_0) || (LDAC_MODE_3 < grad_mode)) {
580            hData->error_code = LDAC_ERR_ENC_ILL_GRADMODE;
581            return LDAC_E_FAIL;
582    }
583
584    if (grad_mode == LDAC_MODE_0) {
585        if ((grad_qu_l < 0) || (LDAC_MAXGRADQU <= grad_qu_l)) {
586            hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_A;
587            return LDAC_E_FAIL;
588        }
589
590        if ((grad_qu_h < 1) || (LDAC_MAXGRADQU+1 <= grad_qu_h) || (grad_qu_h < grad_qu_l)) {
591            hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_B;
592            return LDAC_E_FAIL;
593        }
594
595        if ((grad_os_h < 0) || (LDAC_NIDSF <= grad_os_h)) {
596            hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_C;
597            return LDAC_E_FAIL;
598        }
599    }
600    else {
601        if ((grad_qu_l < 0) || (LDAC_DEFGRADQUH < grad_qu_l)) {
602            hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_A;
603            return LDAC_E_FAIL;
604        }
605    }
606
607    if ((grad_os_l < 0) || (LDAC_NIDSF <= grad_os_l)) {
608        hData->error_code = LDAC_ERR_ENC_ILL_GRADPAR_D;
609        return LDAC_E_FAIL;
610    }
611
612    hData->nbands = nbands;
613    hData->grad_mode = grad_mode;
614    hData->grad_qu_l = grad_qu_l;
615    hData->grad_os_l = grad_os_l;
616    if (grad_mode == LDAC_MODE_0) {
617        hData->grad_qu_h = grad_qu_h;
618        hData->grad_os_h = grad_os_h;
619    }
620    else {
621        hData->grad_qu_h = LDAC_DEFGRADQUH;
622        hData->grad_os_h = LDAC_DEFGRADOSH;
623    }
624    hData->abc_status = abc_status;
625
626    return LDAC_S_OK;
627}
628
629/***************************************************************************************************
630    Initialize
631***************************************************************************************************/
632DECLSPEC LDAC_RESULT ldaclib_init_encode(
633HANDLE_LDAC hData)
634{
635    SFINFO *p_sfinfo = &hData->sfinfo;
636    LDAC_RESULT result;
637
638
639    ldaclib_get_nlnn(p_sfinfo->cfg.smplrate_id, &hData->nlnn);
640
641    set_mdct_table_ldac(hData->nlnn);
642
643    result = init_encode_ldac(p_sfinfo);
644    if (result != LDAC_S_OK) {
645        hData->error_code = LDAC_ERR_ENC_INIT_ALLOC;
646        return LDAC_E_FAIL;
647    }
648
649    return LDAC_S_OK;
650}
651
652/***************************************************************************************************
653    Free
654***************************************************************************************************/
655DECLSPEC LDAC_RESULT ldaclib_free_encode(
656HANDLE_LDAC hData)
657{
658    if (hData->sfinfo.p_mempos == NULL) {
659        free_encode_ldac(&hData->sfinfo);
660    }
661
662    return LDAC_S_OK;
663}
664
665/***************************************************************************************************
666    Encode
667***************************************************************************************************/
668DECLSPEC LDAC_RESULT ldaclib_encode(
669HANDLE_LDAC hData,
670char *ap_pcm[],
671LDAC_SMPL_FMT_T sample_format,
672unsigned char *p_stream,
673int *p_nbytes_used)
674{
675    SFINFO *p_sfinfo = &hData->sfinfo;
676    int loc = 0;
677    int error_code;
678    int frame_length;
679
680
681    if (!ldaclib_assert_sample_format(sample_format)) {
682        hData->error_code = LDAC_ERR_ILL_SMPL_FORMAT;
683        return LDAC_E_FAIL;
684    }
685
686    frame_length = p_sfinfo->cfg.frame_length;
687    clear_data_ldac(p_stream, frame_length*sizeof(unsigned char));
688
689    set_input_pcm_ldac(p_sfinfo, ap_pcm, sample_format, hData->nlnn);
690
691    proc_mdct_ldac(p_sfinfo, hData->nlnn);
692
693    p_sfinfo->cfg.frame_status = ana_frame_status_ldac(p_sfinfo, hData->nlnn);
694
695    error_code = encode_ldac(p_sfinfo, hData->nbands, hData->grad_mode,
696            hData->grad_qu_l, hData->grad_qu_h, hData->grad_os_l, hData->grad_os_h,
697            hData->abc_status);
698    if (LDAC_ERROR(error_code) && !LDAC_FATAL_ERROR(error_code)) {
699        int error_code2;
700        error_code2 = pack_null_data_frame_ldac(p_sfinfo, (STREAM *)p_stream, &loc, p_nbytes_used);
701        if (LDAC_FATAL_ERROR(error_code2)) {
702            clear_data_ldac(p_stream, frame_length*sizeof(unsigned char));
703            hData->error_code = error_code2;
704            return LDAC_E_FAIL;
705        }
706        hData->error_code = error_code;
707        return LDAC_S_FALSE;
708    }
709
710    error_code = pack_raw_data_frame_ldac(p_sfinfo, (STREAM *)p_stream, &loc, p_nbytes_used);
711    if (LDAC_FATAL_ERROR(error_code)) {
712        int error_code2;
713        loc = 0;
714        clear_data_ldac(p_stream, frame_length*sizeof(unsigned char));
715        error_code2 = pack_null_data_frame_ldac(p_sfinfo, (STREAM *)p_stream, &loc, p_nbytes_used);
716        if (LDAC_FATAL_ERROR(error_code2)) {
717            clear_data_ldac(p_stream, frame_length*sizeof(unsigned char));
718            hData->error_code = error_code2;
719            return LDAC_E_FAIL;
720        }
721        hData->error_code = error_code;
722        return LDAC_E_FAIL;
723    }
724
725    return LDAC_S_OK;
726}
727
728/***************************************************************************************************
729    Flush Encode
730***************************************************************************************************/
731DECLSPEC LDAC_RESULT ldaclib_flush_encode(
732HANDLE_LDAC hData,
733LDAC_SMPL_FMT_T sample_format,
734unsigned char *p_stream,
735int *p_nbytes_used)
736{
737    LDAC_RESULT result;
738    int ich;
739    char *ap_buf[LDAC_PRCNCH];
740    int a_buf[LDAC_MAXLSU*LDAC_PRCNCH];
741
742    if (!ldaclib_assert_sample_format(sample_format)) {
743        hData->error_code = LDAC_ERR_ILL_SMPL_FORMAT;
744        return LDAC_E_FAIL;
745    }
746
747    clear_data_ldac(a_buf, (LDAC_MAXLSU*LDAC_PRCNCH)*sizeof(int));
748
749    for (ich = 0; ich < LDAC_PRCNCH; ich++) {
750        ap_buf[ich] = (char *)(a_buf + ich * LDAC_MAXLSU);
751    }
752
753    result = ldaclib_encode(hData, ap_buf, sample_format, p_stream, p_nbytes_used);
754
755    return result;
756}
757
758
759
760
761/***************************************************************************************************
762    Error Code Dispatch
763***************************************************************************************************/
764
765/***************************************************************************************************
766    Clear Error Code at Handle Level
767***************************************************************************************************/
768DECLSPEC LDAC_RESULT ldaclib_clear_error_code(
769HANDLE_LDAC hData)
770{
771    hData->error_code = LDAC_ERR_NONE;
772
773    return LDAC_S_OK;
774}
775
776/***************************************************************************************************
777    Get Error Code at Handle Level
778***************************************************************************************************/
779DECLSPEC LDAC_RESULT ldaclib_get_error_code(
780HANDLE_LDAC hData,
781int *p_error_code)
782{
783    *p_error_code = hData->error_code;
784
785    return LDAC_S_OK;
786}
787
788/***************************************************************************************************
789    Clear Error Code at Internal Block Level
790***************************************************************************************************/
791DECLSPEC LDAC_RESULT ldaclib_clear_internal_error_code(
792HANDLE_LDAC hData)
793{
794    hData->sfinfo.error_code = LDAC_ERR_NONE;
795
796    return LDAC_S_OK;
797}
798
799/***************************************************************************************************
800    Get Error Code at Internal Block Level
801***************************************************************************************************/
802DECLSPEC LDAC_RESULT ldaclib_get_internal_error_code(
803HANDLE_LDAC hData,
804int *p_error_code)
805{
806    *p_error_code = hData->sfinfo.error_code;
807
808    return LDAC_S_OK;
809}
810
811