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
/*
 * Copyright (C) 2013 - 2017 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 "ldacBT_internal.h"
 
 
/* Get LDAC library version */
#define LDACBT_LIB_VER_MAJOR   2
#define LDACBT_LIB_VER_MINOR   0
#define LDACBT_LIB_VER_BRANCH  2
LDACBT_API int ldacBT_get_version( void )
{
    return ((LDACBT_LIB_VER_MAJOR)<<16)|((LDACBT_LIB_VER_MINOR)<<8)|(LDACBT_LIB_VER_BRANCH);
}
 
/* Get LDAC handle */
LDACBT_API HANDLE_LDAC_BT ldacBT_get_handle( void )
{
    HANDLE_LDAC_BT hLdacBT;
    hLdacBT = (HANDLE_LDAC_BT)malloc( sizeof(STRUCT_LDACBT_HANDLE) );
    if( hLdacBT == NULL ){ return NULL; }
 
    /* Get ldaclib Handler */
    if( (hLdacBT->hLDAC = ldaclib_get_handle()) == NULL ){
        ldacBT_free_handle( hLdacBT );
        return NULL;
    }
 
    ldacBT_param_clear( hLdacBT );
    return hLdacBT;
}
 
/* Free LDAC handle */
LDACBT_API void ldacBT_free_handle( HANDLE_LDAC_BT hLdacBT )
{
    if( hLdacBT == NULL ){ return; }
 
    if( hLdacBT->hLDAC != NULL ){
        /* close ldaclib handle */
        if( hLdacBT->proc_mode == LDACBT_PROCMODE_ENCODE ){
            ldaclib_free_encode( hLdacBT->hLDAC );
        }
        /* free ldaclib handle */
        ldaclib_free_handle( hLdacBT->hLDAC );
        hLdacBT->hLDAC = NULL;
    }
    /* free ldacbt handle */
    free( hLdacBT );
}
 
/* Close LDAC handle */
LDACBT_API void ldacBT_close_handle( HANDLE_LDAC_BT hLdacBT )
{
    if( hLdacBT == NULL ){ return; }
 
    if( hLdacBT->hLDAC != NULL ){
        /* close ldaclib handle */
        if( hLdacBT->proc_mode == LDACBT_PROCMODE_ENCODE ){
            ldaclib_free_encode( hLdacBT->hLDAC );
        }
        /* clear error code */
        ldaclib_clear_error_code(hLdacBT->hLDAC);
        ldaclib_clear_internal_error_code(hLdacBT->hLDAC);
    }
    /* clear ldacbt handle */
    ldacBT_param_clear( hLdacBT );
}
 
 
/* Get ERROR CODE */
LDACBT_API int ldacBT_get_error_code( HANDLE_LDAC_BT hLdacBT )
{
    int error_code;
    if( hLdacBT == NULL ){return LDACBT_ERR_FATAL_HANDLE<<10;}
    ldacBT_check_ldaclib_error_code( hLdacBT );
    if( hLdacBT->error_code_api == LDACBT_GET_LDACLIB_ERROR_CODE ){
        error_code = LDACBT_ERR_FATAL << 20 | hLdacBT->error_code;
    }else if( hLdacBT->error_code_api != LDACBT_ERR_NONE ){
        error_code = hLdacBT->error_code_api << 20 | hLdacBT->error_code;
    }else{
        error_code = hLdacBT->error_code_api << 20;
    }
    return error_code;
}
 
 
/* Get Configured Sampling frequency */
LDACBT_API int ldacBT_get_sampling_freq( HANDLE_LDAC_BT hLdacBT )
{
    if( hLdacBT == NULL ){
        return LDACBT_E_FAIL;
    }
    if( hLdacBT->proc_mode != LDACBT_PROCMODE_ENCODE )
    {
        hLdacBT->error_code_api = LDACBT_ERR_HANDLE_NOT_INIT;
        return LDACBT_E_FAIL;
    }
    return hLdacBT->pcm.sf;
}
 
/* Get bitrate */
LDACBT_API int  ldacBT_get_bitrate( HANDLE_LDAC_BT hLdacBT )
{
    if( hLdacBT == NULL ){
        return LDACBT_E_FAIL;
    }
    if( hLdacBT->proc_mode != LDACBT_PROCMODE_ENCODE )
    {
        hLdacBT->error_code_api = LDACBT_ERR_HANDLE_NOT_INIT;
        return LDACBT_E_FAIL;
    }
    return hLdacBT->bitrate;
}
 
/* Init LDAC handle for ENCODE */
LDACBT_API int ldacBT_init_handle_encode( HANDLE_LDAC_BT hLdacBT, int mtu, int eqmid,
                                      int cm, LDACBT_SMPL_FMT_T fmt, int sf )
{
    LDAC_RESULT result;
    int sfid, frame_samples, cci;
    int nbasebands, grad_mode, grad_qu_l, grad_qu_h, grad_ofst_l, grad_ofst_h, abc_flag;
    P_LDACBT_CONFIG pCfg;
    const int a_cci_nch[] = { 1, 2, 2 };
 
    /* check arguments */
    if( hLdacBT == NULL ){ return LDACBT_E_FAIL; }
    if( (hLdacBT->error_code_api = ldacBT_assert_mtu( mtu )) != LDACBT_ERR_NONE ){
        return LDACBT_E_FAIL;
    }
    if( (hLdacBT->error_code_api = ldacBT_assert_eqmid( eqmid )) != LDACBT_ERR_NONE ){
        return LDACBT_E_FAIL;
    }
    if( (hLdacBT->error_code_api = ldacBT_assert_cm( cm )) != LDACBT_ERR_NONE ){
        return LDACBT_E_FAIL;
    }
    if( (hLdacBT->error_code_api = ldacBT_assert_sample_format( fmt )) != LDACBT_ERR_NONE ){
        return LDACBT_E_FAIL;
    }
    if( (hLdacBT->error_code_api = ldacBT_assert_pcm_sampling_freq( sf )) != LDACBT_ERR_NONE ){
        return LDACBT_E_FAIL;
    }
 
    ldacBT_close_handle( hLdacBT );
 
    /* initialize handle for encode processing */
    hLdacBT->proc_mode = LDACBT_PROCMODE_ENCODE;
    hLdacBT->flg_encode_flushed = FALSE;
 
    /* transport setting */
    /* The ldac frame header is REQUIRED for A2DP streaming. */
    hLdacBT->transport = TRUE;
    hLdacBT->tx.mtu = mtu;
    hLdacBT->tx.pkt_hdr_sz = LDACBT_TX_HEADER_SIZE;
    hLdacBT->tx.tx_size = LDACBT_MTU_REQUIRED;
    hLdacBT->tx.pkt_type = _2_DH5;
    /* - BT TRANS HEADER etc */
    hLdacBT->tx.tx_size -= hLdacBT->tx.pkt_hdr_sz;
    if( hLdacBT->tx.tx_size > (hLdacBT->tx.mtu - hLdacBT->tx.pkt_hdr_sz) ){
        /* never happen, mtu must be larger than LDACBT_MTU_REQUIRED(2DH5) */
        hLdacBT->tx.tx_size = (hLdacBT->tx.mtu - hLdacBT->tx.pkt_hdr_sz);
    }
 
    /* channel configration */
    cci = ldacBT_cm_to_cci(cm);
    hLdacBT->cm = cm;
    hLdacBT->cci = cci;
    /* input pcm configuration */
    hLdacBT->pcm.ch = a_cci_nch[cci];
    hLdacBT->pcm.sf = sf;
    hLdacBT->pcm.fmt = fmt;
    switch(hLdacBT->pcm.fmt){
      case LDACBT_SMPL_FMT_S16:
        hLdacBT->pcm.wl = 2;
        break;
      case LDACBT_SMPL_FMT_S24:
        hLdacBT->pcm.wl = 3;
        break;
      case LDACBT_SMPL_FMT_S32:
      case LDACBT_SMPL_FMT_F32:
        hLdacBT->pcm.wl = 4;
        break;
      default:
        // must be rejected by ldacBT_assert_sample_format()
        hLdacBT->pcm.wl = 4;
        break;
    }
 
    /* initilize ldac encode */
    /* Get sampling frequency index */
    result = ldaclib_get_sampling_rate_index( hLdacBT->pcm.sf, &sfid );
    if( LDAC_FAILED ( result ) ){
        hLdacBT->error_code_api = LDACBT_ERR_ILL_SAMPLING_FREQ;
        return LDACBT_E_FAIL;
    }
    hLdacBT->sfid = sfid;
 
    /* Get number of frame samples */
    result = ldaclib_get_frame_samples(sfid, &frame_samples);
    if (LDAC_FAILED(result)) {
        hLdacBT->error_code_api = LDACBT_ERR_ILL_SAMPLING_FREQ;
        return LDACBT_E_FAIL;
    }
    hLdacBT->frm_samples = frame_samples;
 
 
    /* Set Parameters by Encode Quality Mode Index */
    hLdacBT->eqmid = eqmid;
    /* get frame_length of EQMID */
    pCfg = ldacBT_get_config( hLdacBT->eqmid, hLdacBT->tx.pkt_type );
    /* set frame_length */
    hLdacBT->frmlen_tx = hLdacBT->pcm.ch * pCfg->frmlen_1ch;
    hLdacBT->frmlen = hLdacBT->frmlen_tx;
    if (hLdacBT->transport) {
        /* Adjust frame_length for Transport Header Data */
        hLdacBT->frmlen -= LDACBT_FRMHDRBYTES;
    }
 
    /* Calculate how many LDAC frames fit into payload packet */
    hLdacBT->tx.nfrm_in_pkt = hLdacBT->tx.tx_size / hLdacBT->frmlen_tx;
    
    
    /* Get ldac encode setting */
    result = ldaclib_get_encode_setting( pCfg->frmlen_1ch, sfid, &nbasebands, &grad_mode,
                     &grad_qu_l, &grad_qu_h, &grad_ofst_l, &grad_ofst_h, &abc_flag);
    if (LDAC_FAILED(result)) {
        hLdacBT->error_code_api = LDACBT_ERR_ILL_PARAM;
        return LDACBT_E_FAIL;
    }
 
    /* Set Configuration Information */
    result = ldaclib_set_config_info( hLdacBT->hLDAC, hLdacBT->sfid, hLdacBT->cci,
                                      hLdacBT->frmlen, hLdacBT->frm_status);
    if (LDAC_FAILED(result)) {
        hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
        return LDACBT_E_FAIL;
    }
    else if (result != LDAC_S_OK) {
        hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
    }
 
    /* Set Encoding Information */
    result = ldaclib_set_encode_info(hLdacBT->hLDAC, nbasebands, grad_mode,
                                     grad_qu_l, grad_qu_h, grad_ofst_l, grad_ofst_h, abc_flag);
    if (LDAC_FAILED(result)) {
        hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
        return LDACBT_E_FAIL;
    }
    else if (result != LDAC_S_OK) {
        hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
    }
 
    /* Initialize ldaclib for Encoding */
    result = ldaclib_init_encode(hLdacBT->hLDAC);
    if (LDAC_FAILED(result)) {
        hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
        return LDACBT_E_FAIL;
    }
    else if (result != LDAC_S_OK) {
        hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
    }
 
    /* reset target eqmid as current setting */
    hLdacBT->tgt_eqmid = hLdacBT->eqmid;
    hLdacBT->tgt_nfrm_in_pkt = hLdacBT->tx.nfrm_in_pkt;
    hLdacBT->tgt_frmlen = hLdacBT->frmlen;
    hLdacBT->stat_alter_op = LDACBT_ALTER_OP__NON;
 
    /* get bitrate */
    hLdacBT->bitrate = ldacBT_frmlen_to_bitrate( hLdacBT->frmlen, hLdacBT->transport,
                                                 hLdacBT->pcm.sf, hLdacBT->frm_samples );
 
    return (hLdacBT->error_code_api==LDACBT_ERR_NONE?LDACBT_S_OK:LDACBT_E_FAIL);
}
 
/* Set Encode Quality Mode index */
LDACBT_API int ldacBT_set_eqmid( HANDLE_LDAC_BT hLdacBT, int eqmid )
{
    if( hLdacBT == NULL ){
        return LDACBT_E_FAIL;
    }
    if( hLdacBT->proc_mode != LDACBT_PROCMODE_ENCODE ){
        hLdacBT->error_code_api = LDACBT_ERR_HANDLE_NOT_INIT;
        return LDACBT_E_FAIL;
    }
 
    if( (hLdacBT->error_code_api = ldacBT_assert_eqmid( eqmid )) != LDACBT_ERR_NONE ){
        return LDACBT_E_FAIL; /* fatal */
    }
   ldacBT_set_eqmid_core( hLdacBT, eqmid );
 
   return LDACBT_S_OK;
}
 
/* Get Encode Quality Mode index */
LDACBT_API int ldacBT_get_eqmid( HANDLE_LDAC_BT hLdacBT )
{
    if( hLdacBT == NULL ){
        return LDACBT_E_FAIL;
    }
    if( hLdacBT->proc_mode != LDACBT_PROCMODE_ENCODE ){
        hLdacBT->error_code_api = LDACBT_ERR_HANDLE_NOT_INIT;
        return LDACBT_E_FAIL;
    }
    return hLdacBT->tgt_eqmid;
}
 
/* Alter encode quality mode index */
LDACBT_API int  ldacBT_alter_eqmid_priority( HANDLE_LDAC_BT hLdacBT, int priority )
{
    int target_eqmid;
    if( hLdacBT == NULL ){ return LDACBT_E_FAIL; }
    if( hLdacBT->proc_mode != LDACBT_PROCMODE_ENCODE ){
        hLdacBT->error_code_api = LDACBT_ERR_HANDLE_NOT_INIT;
        return LDACBT_E_FAIL;
    }
    if( (priority != LDACBT_EQMID_INC_QUALITY) &&
        (priority != LDACBT_EQMID_INC_CONNECTION )
        ){
            hLdacBT->error_code_api = LDACBT_ERR_ILL_PARAM;
            return LDACBT_E_FAIL;
    }
 
    target_eqmid = ldacBT_get_altered_eqmid( hLdacBT,  priority);
    if( target_eqmid < 0 ){
        hLdacBT->error_code_api = LDACBT_ERR_ALTER_EQMID_LIMITED;
        return LDACBT_E_FAIL;
    }
 
    ldacBT_set_eqmid_core( hLdacBT, target_eqmid );
    return LDACBT_S_OK;
}
 
/* LDAC encode proccess */
LDACBT_API int ldacBT_encode( HANDLE_LDAC_BT hLdacBT, void *p_pcm, int *pcm_used,
                          unsigned char *p_stream, int *stream_sz, int *frame_num )
{
    LDAC_RESULT result;
    LDACBT_SMPL_FMT_T fmt;
    LDACBT_TRANSPORT_FRM_BUF *ptfbuf;
    LDACBT_PCM_RING_BUF *ppcmring;
    P_LDACBT_CONFIG pCfg;
    int frmlen, frmlen_wrote, frmlen_adj;
    int frm_status, flg_Do_Encode;
    int nFrmToPkt, ch, wl;
    unsigned char *p_ldac_transport_frame;
    unsigned char a_frm_header[LDACBT_FRMHDRBYTES + 2];
    if( hLdacBT == NULL ){
        return LDACBT_E_FAIL;
    }
    if( hLdacBT->hLDAC == NULL ){
        return LDACBT_E_FAIL;
    }
    if( hLdacBT->proc_mode != LDACBT_PROCMODE_ENCODE ){
        hLdacBT->error_code_api = LDACBT_ERR_HANDLE_NOT_INIT;
        return LDACBT_E_FAIL;
    }
    /* Clear Error Codes */
    hLdacBT->error_code_api = LDACBT_ERR_NONE;
    ldaclib_clear_error_code( hLdacBT->hLDAC );
    ldaclib_clear_internal_error_code( hLdacBT->hLDAC );
 
    if( ( pcm_used == NULL) ||
        ( p_stream == NULL ) ||
        ( stream_sz == NULL ) ||
        ( frame_num == NULL )
        ){
            hLdacBT->error_code_api = LDACBT_ERR_ILL_PARAM;
            return LDACBT_E_FAIL;
    }
    /* reset parameters */
    *pcm_used = 0;
    *stream_sz = 0;
    *frame_num = 0;
    flg_Do_Encode = 0;
    fmt = hLdacBT->pcm.fmt;
    ch = hLdacBT->pcm.ch;
    wl = hLdacBT->pcm.wl;
    ptfbuf = &hLdacBT->ldac_trns_frm_buf;
    ppcmring = &hLdacBT->pcmring;
 
    /* update input pcm data */
    if( p_pcm != NULL ){
        int nByteCpy, sz;
        nByteCpy = LDACBT_ENC_LSU * wl * ch;
        sz = ppcmring->nsmpl * wl * ch + nByteCpy;
        if( sz < LDACBT_ENC_PCM_BUF_SZ ){
            copy_data_ldac( p_pcm, ppcmring->buf + ppcmring->wp, nByteCpy );
            ppcmring->wp += nByteCpy;
            if( ppcmring->wp >= LDACBT_ENC_PCM_BUF_SZ ){
                ppcmring->wp = 0;
            }
            ppcmring->nsmpl += LDACBT_ENC_LSU;
            *pcm_used = nByteCpy;
        }else{
            /* Not enough space to copy.
             * This will happen when the last encode process failed.
             */
            *pcm_used = 0;
        }
 
        if( ppcmring->nsmpl >= hLdacBT->frm_samples )
        {
            flg_Do_Encode = 1;
        }
    }else{
        if (hLdacBT->flg_encode_flushed != TRUE){
            flg_Do_Encode = 1;
        }
    }
 
    if( !flg_Do_Encode ){
        /* nothing to do */
        return LDACBT_S_OK;
    }
 
    /* update frame_length if needed */
    if( (hLdacBT->tgt_eqmid != UNSET) && (hLdacBT->tgt_eqmid != hLdacBT->eqmid) ){
        if( ptfbuf->nfrm_in == 0 ){
            ldacBT_update_frmlen( hLdacBT, hLdacBT->tgt_frmlen );
            hLdacBT->stat_alter_op = LDACBT_ALTER_OP__NON;
        }
        else if( hLdacBT->tgt_nfrm_in_pkt > hLdacBT->tx.nfrm_in_pkt ){
            /* for better connectivity, apply ASAP */
            if( !hLdacBT->stat_alter_op ){
                nFrmToPkt = hLdacBT->tgt_nfrm_in_pkt - ptfbuf->nfrm_in;
                if( nFrmToPkt > 0 ){
                    pCfg = ldacBT_get_config(LDACBT_EQMID_END, hLdacBT->tx.pkt_type);
                    if( pCfg != NULL ){
                        do{
                            frmlen_adj = (hLdacBT->tx.tx_size - ptfbuf->used) / nFrmToPkt;
                            if( frmlen_adj > hLdacBT->tgt_frmlen ) {
                                frmlen_adj = hLdacBT->tgt_frmlen;
                            }
                            frmlen_adj -= LDACBT_FRMHDRBYTES;
                            if( frmlen_adj >= pCfg->frmlen ){
                                if( ldacBT_update_frmlen( hLdacBT, frmlen_adj ) == LDACBT_S_OK ){
                                    hLdacBT->stat_alter_op = LDACBT_ALTER_OP__ACTIVE;
                                    break;
                                }
                            }
                        }while( --nFrmToPkt > 0 );
                    }
                    if( !hLdacBT->stat_alter_op ){
                        /* force to flash streams */
                        hLdacBT->stat_alter_op = LDACBT_ALTER_OP__FLASH;
                    }
                }
            }
        }
        else{
            /* wait the condition ptfbuf->nfrm_in == 0 for apply new frame_length */
            hLdacBT->stat_alter_op = LDACBT_ALTER_OP__STANDBY;
        }
 
    }
    else if( hLdacBT->tgt_frmlen != hLdacBT->frmlen ){
        if( ptfbuf->nfrm_in == 0 ){
            ldacBT_update_frmlen( hLdacBT, hLdacBT->tgt_frmlen );
            hLdacBT->stat_alter_op = LDACBT_ALTER_OP__NON;
        }else{
            if( hLdacBT->tgt_nfrm_in_pkt == hLdacBT->tx.nfrm_in_pkt ){
                ldacBT_update_frmlen( hLdacBT, hLdacBT->tgt_frmlen );
                hLdacBT->stat_alter_op = LDACBT_ALTER_OP__NON;
            }else{
                if( hLdacBT->tgt_nfrm_in_pkt > hLdacBT->tx.nfrm_in_pkt ){
                    /* for better connectivity, apply ASAP */
                    if( !hLdacBT->stat_alter_op ){
                        nFrmToPkt = hLdacBT->tgt_nfrm_in_pkt - ptfbuf->nfrm_in;
                        if( nFrmToPkt > 0 ){
                            frmlen_adj = (hLdacBT->tx.tx_size - ptfbuf->used) / nFrmToPkt;
                            if( frmlen_adj > hLdacBT->tgt_frmlen ) {
                                frmlen_adj = hLdacBT->tgt_frmlen;
                            }
                            if( ldacBT_update_frmlen( hLdacBT, frmlen_adj ) == LDACBT_S_OK ){
                                hLdacBT->stat_alter_op = LDACBT_ALTER_OP__ACTIVE;
                            }
                            if( !hLdacBT->stat_alter_op ){
                                /* flash streams */
                                hLdacBT->stat_alter_op = LDACBT_ALTER_OP__FLASH;
                            }
                        }
                    }
                }else{
                    /* wait the condition ptfbuf->nfrm_in == 0 for apply new frame_length */
                    hLdacBT->stat_alter_op = LDACBT_ALTER_OP__STANDBY;
                }
            }
        }
    }
 
    /* check write space for encoded data */
    ldaclib_get_encode_frame_length( hLdacBT->hLDAC, &frmlen );
 
    if( (( ptfbuf->used + frmlen + LDACBT_FRMHDRBYTES) > hLdacBT->tx.tx_size) ||
        (hLdacBT->stat_alter_op == LDACBT_ALTER_OP__FLASH) || /* need to flash streams? */
        (( ptfbuf->used + frmlen + LDACBT_FRMHDRBYTES) >= LDACBT_ENC_STREAM_BUF_SZ )
        )
    {
        copy_data_ldac( ptfbuf->buf, p_stream, ptfbuf->used );
        *stream_sz = ptfbuf->used;
        *frame_num = ptfbuf->nfrm_in;
        clear_data_ldac( ptfbuf->buf, sizeof(char)*LDACBT_ENC_STREAM_BUF_SZ);
        ptfbuf->used = 0;
        ptfbuf->nfrm_in = 0;
        if( hLdacBT->stat_alter_op != LDACBT_ALTER_OP__NON ){
            /* update frame length */
            ldacBT_update_frmlen( hLdacBT, hLdacBT->tgt_frmlen );
            hLdacBT->stat_alter_op = LDACBT_ALTER_OP__NON;
        }
    }
    p_ldac_transport_frame = ptfbuf->buf + ptfbuf->used;
 
    /* Encode Frame */
    if( ppcmring->nsmpl > 0 ){
        char *p_pcm_ring_r;
        int nsmpl_to_clr;
        nsmpl_to_clr = hLdacBT->frm_samples - ppcmring->nsmpl;
        if( nsmpl_to_clr > 0 ){
            int pos, nBytesToZero;
            pos = ppcmring->rp + ppcmring->nsmpl * wl * ch;
            nBytesToZero = nsmpl_to_clr * wl * ch;
            while( nBytesToZero > 0 ){
                int clearBytes;
                clearBytes = nBytesToZero;
                if ( pos + clearBytes >= LDACBT_ENC_PCM_BUF_SZ ){
                    clearBytes = (LDACBT_ENC_PCM_BUF_SZ - pos);
                }
                clear_data_ldac( ppcmring->buf + pos, clearBytes);
                nBytesToZero -= clearBytes;
                if( (pos += clearBytes) >= LDACBT_ENC_PCM_BUF_SZ ){
                    pos = 0;
                }
            }
        }
        p_pcm_ring_r = ppcmring->buf + ppcmring->rp;
        ldacBT_prepare_pcm_encode( p_pcm_ring_r, hLdacBT->pp_pcm, hLdacBT->frm_samples, ch, fmt );
        result = ldaclib_encode(hLdacBT->hLDAC, hLdacBT->pp_pcm, (LDAC_SMPL_FMT_T)fmt,
                         p_ldac_transport_frame+LDACBT_FRMHDRBYTES, &frmlen_wrote);
        if( !LDAC_FAILED(result) ){
            ppcmring->rp += hLdacBT->frm_samples * wl * ch;
            ppcmring->nsmpl -= hLdacBT->frm_samples;
            if( ppcmring->rp >= LDACBT_ENC_PCM_BUF_SZ ){ ppcmring->rp = 0; }
            if( ppcmring->nsmpl < 0 ){ ppcmring->nsmpl = 0; }
        }
    }else{
        result = ldaclib_flush_encode(hLdacBT->hLDAC, (LDAC_SMPL_FMT_T)fmt,
                             p_ldac_transport_frame+LDACBT_FRMHDRBYTES, &frmlen_wrote);
        hLdacBT->flg_encode_flushed = TRUE;
    }
 
    if( LDAC_FAILED(result) ){
        hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
        return LDACBT_E_FAIL;
    }
    else if( result != LDAC_S_OK ){
        hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
    }
 
    if( frmlen_wrote > 0 ){
        if( hLdacBT->transport == TRUE ){
            /* Set Frame Header Data */
            clear_data_ldac( a_frm_header, LDACBT_FRMHDRBYTES+2 );
            /* Get Frame Header Information */
            result = ldaclib_get_config_info(hLdacBT->hLDAC, &hLdacBT->sfid, &hLdacBT->cci,
                            &frmlen, &frm_status);
            if( LDAC_FAILED(result) ){
                hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
                return LDACBT_E_FAIL;
            }
            else if (result != LDAC_S_OK) {
                hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
            }
 
            /* Set Frame Header */
            result = ldaclib_set_frame_header(hLdacBT->hLDAC, a_frm_header, hLdacBT->sfid,
                            hLdacBT->cci, frmlen, frm_status);
            if( LDAC_FAILED(result) ){
                hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
                return LDACBT_E_FAIL;
            }
            else if (result != LDAC_S_OK) {
                hLdacBT->error_code_api = LDACBT_GET_LDACLIB_ERROR_CODE;
            }
            copy_data_ldac( a_frm_header, p_ldac_transport_frame, LDACBT_FRMHDRBYTES );
            frmlen_wrote += LDACBT_FRMHDRBYTES;
        }
        ptfbuf->used += frmlen_wrote;
        ptfbuf->nfrm_in ++;
    }
 
    /* check for next frame buffer status */
    if( *stream_sz == 0 ){
        if( (( ptfbuf->used + frmlen_wrote) > hLdacBT->tx.tx_size) ||
            (  ptfbuf->nfrm_in >= LDACBT_NFRM_TX_MAX ) || 
            (( ptfbuf->used + frmlen_wrote) >= LDACBT_ENC_STREAM_BUF_SZ ) ||
            ( p_pcm == NULL ) /* flush encode */
            )
        {
            copy_data_ldac( ptfbuf->buf, p_stream, ptfbuf->used );
            *stream_sz = ptfbuf->used;
            *frame_num = ptfbuf->nfrm_in;
            clear_data_ldac( ptfbuf->buf, sizeof(char)*LDACBT_ENC_STREAM_BUF_SZ);
            ptfbuf->used = 0;
            ptfbuf->nfrm_in = 0;
            if( hLdacBT->stat_alter_op != LDACBT_ALTER_OP__NON ){
                ldacBT_update_frmlen( hLdacBT, hLdacBT->tgt_frmlen );
                hLdacBT->stat_alter_op = LDACBT_ALTER_OP__NON;
            }
        }
    }
 
    return LDACBT_S_OK;
}