summaryrefslogtreecommitdiff
path: root/cesar/mac/common/src/tonemap.c
blob: c6e9afa19b96da62dfb50670729a31601286d42a (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/common/src/tonemap.c
 * \brief   Tone maps handling functions.
 * \ingroup mac_common
 */
#include "common/std.h"
#include <string.h> //memset // FIXME!
#include "mac/common/tonemap.h"
#include "mac/common/timings.h"
#include "ce/common/mod.h"
#include "hal/arch/arch.h"

/** Maximum encodable BLE value. */
#define TONEMAP_BLE_MAX (((31 + 32) << 3) + (1 << 2))

/** Length of data symbols. */
static uint tonemap_data_symbol_tck[] = {
    MAC_DX417_TCK, MAC_DX567_TCK, MAC_DX3534_TCK };

/**
 * Compute log2 for BLE values.
 * \param  n  integer number
 * \return  logarithm of base 2
 *
 * Do not need fractional part for this.
 */
static inline uint
tonemap_ble_log2 (uint n)
{
    dbg_assert (n >= 2);
    dbg_assert (n < TONEMAP_BLE_MAX);
    /* Use the assertion that: 2^8 < 508 < 2^9, where 508 is the maximum
     * encodable value. */
    uint a = 1, b = 9, i;
    dbg_assert (1 << (a - 1) < 2 && 2 <= 1 << a);
    dbg_assert (1 << (b - 1) < TONEMAP_BLE_MAX && TONEMAP_BLE_MAX < 1 << b);
    do {
        i = (a + b) / 2;
        if (n < 1u << i)
            b = i;
        else
            a = i;
    } while (a + 1 != b);
    return a;
}

/**
 * Initialize a tone maps set to default values.
 * \param  tms  the tone maps set to initialize.
 */
static inline void
tonemaps_init (tonemaps_t *tms)
{
    /* Check parameter. */
    dbg_assert (tms);

    u8 i;
    for (i = 0; i < TONEMAP_INDEX_NB; i++)
        tms->tm[i] = NULL;
    tms->max_fl_av = MAC_MAX_FL_MIN_FL;
    tms->rifs_av_one_sym_tck = MAC_RIFS_AV_ONE_SYM_TCK;
    tms->rifs_av_two_sym_tck = MAC_RIFS_AV_TWO_SYM_TCK;
    tms->rifs_av_g2_sym_tck = MAC_RIFS_AV_G2_SYM_TCK;
    tms->max_tm = TONEMAP_MAX;
    tms->scl_cp = 0;
    tms->scl_cfp = 0;
    tms->expiration_s = 0;
    tms->tm_sound_complete_bitmap = 0;
    tms->intervals = &tms->swap_intervals[0];
    tms->intervals_temp = &tms->swap_intervals[1];
    tms->intervals->intervals_nb = 0;
    tms->intervals->version = 0;
    tms->refresh_counter_s = 0;
}

tonemap_t *
tonemap_alloc ()
{
    /* Allocate tone map block. */
    blk_t *first, *last;
    tonemap_t *tm_entry;
    first = blk_alloc_desc_range (2, (blk_t **) &last);
    /* No next block at the end. */
    last->next = NULL;
    /* FIXME: this should be removed in the future. Why? Just because this is
     * not useful... */
    memset (first->data, 0xFF, BLK_SIZE);
    memset (last->data, 0xFF, BLK_SIZE);
    memset (last->data + TONEMAP_HEADER_POS_BYTE, 0x00,
            BLK_SIZE - TONEMAP_HEADER_POS_BYTE);
    /* Ensure we have the necessary space to store the information required by
     * the tonemap_t structure (it can grow). */
    dbg_assert (sizeof (tonemap_t) < (BLK_SIZE - TONEMAP_HEADER_POS_BYTE));
    /* Store the header on the second half of the second block. */
    tm_entry = (tonemap_t *) (last->data + TONEMAP_HEADER_POS_BYTE) ;
    tm_entry->tmdma_desc_head = first;
    /* Tone map in use. */
    tm_entry->released = TONEMAP_IN_USE;
    /* Initialize release_link. */
    list_init_node (&tm_entry->release_link);
    /* Return allocated tone map. */
    return tm_entry;
}

void
tonemap_free (tonemap_t *tm)
{
    /* Check parameter. */
    dbg_assert (tm);

    /* Delete block. */
    blk_t *f = tm->tmdma_desc_head;
    blk_release_desc_range (f, f->next);
}

void
tonemap_disable (tonemaps_t *tms, u8 tmi)
{
    /* Check parameters. */
    dbg_assert (tms);
    dbg_assert ((TONEMAP_INDEX_IS_NEGOTIATED (tmi)));
    dbg_assert (tms->tm[tmi]);

    /* Mark tone map as released. */
    tms->tm[tmi]->released = TONEMAP_RELEASE_TIMER_S;
}

void
tonemap_release (tonemaps_t *tms, u8 tmi,
                 tonemap_release_list_t *tm_release_list)
{
    /* Check parameters. */
    dbg_assert (tms);
    dbg_assert ((TONEMAP_INDEX_IS_NEGOTIATED (tmi)));
    tonemap_t *tm = tms->tm[tmi];
    dbg_assert (tm);
    dbg_assert (tm_release_list);

    /* Put the tonemap in the release list and get it out of the
     * station set. */
    list_push (tm_release_list, &tm->release_link);
    tm->released = TONEMAP_RELEASE_TIMER_S;
    tms->tm[tmi] = NULL;
}

bool
tonemap_release_list_clean (tonemap_release_list_t *tm_release_list)
{
    /* Check parameter. */
    dbg_assert (tm_release_list);

    /* Parse the release list to free the expired tonemaps. */
    list_node_t *i, *i_next;
    for (i = list_begin (tm_release_list); i != list_end (tm_release_list);
         i = i_next)
    {
        i_next = list_next (i);
        tonemap_t *tm = PARENT_OF (tonemap_t, release_link, i);
        if (!--tm->released)
        {
            list_remove (tm_release_list, i);
            tonemap_free (tm);
        }
    }
    return list_empty (tm_release_list);
}

void
tonemap_copy (tonemap_t *dst, tonemap_t *src)
{
    /* Check parameters. */
    dbg_assert (dst);
    dbg_assert (src);

    /* Copy first block. */
    memcpy (dst->tmdma_desc_head->data, src->tmdma_desc_head->data, BLK_SIZE);
    /* Copy the second block. */
    memcpy (dst->tmdma_desc_head->next->data,
            src->tmdma_desc_head->next->data,
            BLK_SIZE - TONEMAP_HEADER_POS_BYTE);
    /* Copy header. */
    blk_t *copy = dst->tmdma_desc_head;
    memcpy (dst, src, sizeof (*dst));
    dst->tmdma_desc_head = copy;
}

tonemap_t *
tonemap_get (tonemaps_t *tms, u8 tmi)
{
    /* Check parameters. */
    dbg_assert (tms);
    dbg_assert (TONEMAP_INDEX_IS_NEGOTIATED (tmi));

    /* If tone map corresponding to the desired TMI exists and not marked as
     * released. */
    if (tms->tm[tmi] && (tms->tm[tmi]->released == TONEMAP_IN_USE))
        /* Returns it. */
        return tms->tm[tmi];
    else
        /* Error. */
        return NULL;
}

u8
tonemaps_allocated_count (const tonemaps_t *tms)
{
    /* Check parameter. */
    dbg_assert (tms);

    u8 i, cpt = 0;
    for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB; i++)
    {
        if (tms->tm[i])
        {
            cpt++;
        }
    }
    return cpt;
}

u8
tonemap_get_tone (tonemap_t *tm, uint index, u32 **word_ptr)
{
    /* Check parameters. */
    dbg_assert (tm);
    dbg_assert (index < PHY_CARRIER_NB);
    dbg_assert (word_ptr);

    /* Get the word where the tone is stored. */
    *word_ptr = tonemap_get_tone_word (tm, index);

    /* Copy value. */
    u32 word = **word_ptr;

    /* Get tone from word. */
    return ((word >> ((index % 8) * 4)) & 0xF);
}

void
tonemap_write_tone_to_word (u32 *word_ptr, uint index, uint tone)
{
    /* Check parameters. */
    dbg_assert (word_ptr);
    dbg_assert (index < PHY_CARRIER_NB);
    dbg_assert (tone < CE_MOD_COUNT);

    /* Copy word value. */
    u32 word = *word_ptr;

    /* Change with the new tone. */
    word &= ~((u32) (0xF << (index % 8) * 4));
    word |= tone << ((index % 8) * 4);
    /* Commit change. */
    *word_ptr = word;
}

void
tonemap_write_tone (tonemap_t *tm, uint index, uint tone)
{
    /* Check parameters. */
    dbg_assert (tm);
    dbg_assert (index < PHY_CARRIER_NB);
    dbg_assert (tone < CE_MOD_COUNT);

    /* Get tone word. */
    u32 *word_ptr = tonemap_get_tone_word (tm, index);

    /* Copy word value. */
    u32 word = *word_ptr;

    /* Write tone to the word. */
    word &= ~((u32) (0xF << (index % 8) * 4));
    word |= tone << ((index % 8) * 4);
    /* Commit change. */
    *word_ptr = word;
}

bool
tonemap_decrease_tone (tonemap_t *tonemap, uint index)
{
    u32 *word_ptr = NULL;
    u8 mod = tonemap_get_tone (tonemap, index, &word_ptr);

    dbg_assert (word_ptr);

    /* Already on the min modulation. */
    if (mod == 0)
        return false;
    else
        tonemap_write_tone_to_word (word_ptr, index, mod - 1);
    return true;
}

bool
tonemap_increase_tone (tonemap_t *tonemap, uint index)
{
    u32 *word_ptr = NULL;
    u8 mod = tonemap_get_tone (tonemap, index, &word_ptr);

    dbg_assert (word_ptr);

    /* Already on the max modulation. */
    if (mod == CE_MOD_COUNT - 1)
        return false;
    else
        tonemap_write_tone_to_word (word_ptr, index, mod + 1);
    return true;
}

uint
tonemap_bits_per_pb (phy_mod_t mod, phy_fecrate_t fecrate,
                     phy_pb_size_t pb_size, uint bits_per_symbol)
{
    /* Check parameters. */
    dbg_assert (mod < PHY_MOD_NONE);
    dbg_assert (fecrate < PHY_FEC_RATE_NONE);
    dbg_assert (pb_size < PHY_PB_SIZE_NONE);
    dbg_assert (mod == PHY_MOD_TM || bits_per_symbol);

    uint bits;
    /* Integrate the FEC rate 1/2 ratio... */
    bits = pb_size == PHY_PB_SIZE_136 ? 136 * 8 * 2 : 520 * 8 * 2;
    /* And undo it if necessary. */
    if (fecrate != PHY_FEC_RATE_1_2)
        bits = bits / (16 * 2) * 21; /* Always divisible by (16 * 2). */
    /* For ROBO modes, there is padding. */
    if (mod != PHY_MOD_TM)
    {
        bits = (bits + bits_per_symbol - 1) / bits_per_symbol
            * bits_per_symbol;
    }
    return bits;
}

uint
tonemap_tcc_halfit (uint bits_per_symbol, uint bits_per_pb, phy_gil_t gil)
{
    dbg_assert (bits_per_symbol);
    dbg_assert (bits_per_pb);
    dbg_assert (gil < PHY_GIL_NB);
    uint tcc_time_tck;
    /* Compute time available for TCC work. */
    if (bits_per_pb > bits_per_symbol)
        tcc_time_tck = bits_per_pb / bits_per_symbol
            * tonemap_data_symbol_tck[gil];
    else
        tcc_time_tck = tonemap_data_symbol_tck[gil]
            / CEIL_DIV (bits_per_symbol, bits_per_pb);
    /* Compute number of half iterations. */
    uint tcc_time_cycles = tcc_time_tck
        * (PHY_DSP_CLOCK_MHZ / MAC_US_TO_TCK (1));
    uint halfit = 2 * (tcc_time_cycles - 50) / 1064 - 1;
    return MIN (halfit, 31u);
}

uint
tonemap_ble_mant_2_uf5 (u8 ble)
{
    u8 mant = ble >> 3;
    u8 exp = ble & ((1 << 3) - 1);

    return (mant + 32) * (1 << (exp + 1)) + (1 << (exp));
}

u8
tonemap_ble_uf5_2_mant (uint ble_uf5)
{
    if (ble_uf5 >= TONEMAP_BLE_MAX * (1u << 5))
        return 0xff;
    else if (ble_uf5 < (2 * (1u << 5)) + 1)
        return 0;
    else
    {
        uint exp, mant;
        exp = tonemap_ble_log2 (ble_uf5 >> 5) - 1;
        dbg_assert (exp < (1u << 3));
        mant = (ble_uf5 >> (exp + 1)) - 32;
        dbg_assert (mant < (1u << 5));
        return mant << 3 | exp;
    }
}

u8
tonemap_ble (uint bits_per_symbol, phy_fecrate_t fecrate, u32 p_pberror_uf32,
             phy_gil_t gil)
{
    /* Check parameters. */
    dbg_assert (fecrate < PHY_FEC_RATE_NONE);
    dbg_assert (gil < PHY_GIL_NB);

    /* TODO: take ROBO padding into account. */
    uint ble_uf5;
    /* Compute BLE.
     * bits_per_symbol max < 20480. */
    ble_uf5 = (1u << 5);
    ble_uf5 *= bits_per_symbol;
    if (fecrate == PHY_FEC_RATE_1_2)
        ble_uf5 /= 2;
    else
        ble_uf5 = ble_uf5 * 16 / 21;
    ble_uf5 = ((u64) ble_uf5 * ((1ll << 32) - p_pberror_uf32) + (1ll << 31))
        >> 32; /* Do not omit rounding. */
    /* Let the compiler optimise the division. */
    switch (gil)
    {
    case PHY_GIL_417:
        ble_uf5 /= MAC_TCK_TO_US (MAC_DX417_TCK);
        break;
    case PHY_GIL_567:
        ble_uf5 /= MAC_TCK_TO_US (MAC_DX567_TCK);
        break;
    case PHY_GIL_3534:
        ble_uf5 /= MAC_TCK_TO_US (MAC_DX3534_TCK);
        break;
    default:
        dbg_assert_default ();
    }
    /* Convert to BLE floating point format. */
    return tonemap_ble_uf5_2_mant (ble_uf5);
}

void
tonemap_update (tonemap_t *tm, u32 p_pberror_uf32)
{
    dbg_assert_ptr (tm);
    /* Update tone map fields. */
    tm->ble = tonemap_ble (tm->bits_per_symbol, tm->fecrate, p_pberror_uf32,
                           tm->gil);
    tm->bits_per_pb[PHY_PB_SIZE_136] = tonemap_bits_per_pb (
        PHY_MOD_TM, tm->fecrate, PHY_PB_SIZE_136, tm->bits_per_symbol);
    tm->bits_per_pb[PHY_PB_SIZE_520] = tonemap_bits_per_pb (
        PHY_MOD_TM, tm->fecrate, PHY_PB_SIZE_520, tm->bits_per_symbol);
    uint one_pb_symb_nb = CEIL_DIV (tm->bits_per_pb[PHY_PB_SIZE_520],
                                    tm->bits_per_symbol);
    tm->one_pb_data_tck = MAC_PAYLOAD_TCK (one_pb_symb_nb,
                                           tonemap_data_symbol_tck[tm->gil]);
    tm->phy_combo_params[PHY_PB_SIZE_136] =
        PHY_COMBO_PARAMS (PHY_MOD_TM, tm->fecrate, PHY_PB_SIZE_136);
    tm->phy_combo_params[PHY_PB_SIZE_520] =
        PHY_COMBO_PARAMS (PHY_MOD_TM, tm->fecrate, PHY_PB_SIZE_520);
    tm->tcc_halfit =
        tonemap_tcc_halfit (tm->bits_per_symbol,
                            tm->bits_per_pb[PHY_PB_SIZE_520], tm->gil);
}

tonemaps_t *
tonemaps_alloc ()
{
    /* Allocate tone maps set. */
    tonemaps_t *tms = (tonemaps_t *) blk_alloc_zero ();
    /* Set default values. */
    tms->default_tmi = TONEMAP_INDEX_INITIAL_START;
    /* This need to be done only when allocating tonemaps (see #1595). */
    tms->sound_frame_counter = TONEMAP_SOUND_FRAME_COUNTER;
    tonemaps_init (tms);
    return tms;
}

void
tonemaps_release (tonemaps_t *tms)
{
    /* Check parameter. */
    dbg_assert (tms);

    int i;
    dbg_do (
        for (i = 0; i < TONEMAP_INDEX_NEGOTIATED_FIRST; i++)
        {
            dbg_assert (tms->tm[i] == NULL);
        });

    /* Go through each tone map index. */
    for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB; i++)
    {
        /* Tone map is in use. */
        if (tms->tm[i])
        {
            /* Atomic deletion. */
            tonemap_t *tm = tms->tm[i];
            tms->tm[i] = NULL;
            tonemap_free (tm);
        }
    }
    /* Clean. */
    blk_release (tms);
}

u8
tonemaps_disabled_clean (tonemaps_t *tms)
{
    /* Check parameters. */
    dbg_assert (tms);

    u8 i, counter = 0;
    /* Go through each tone maps. */
    for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB; i++)
    {
        /* If tone maps is allocated and must be deleted now. */
        if (tms->tm[i] && tms->tm[i]->released != TONEMAP_IN_USE
            && --tms->tm[i]->released == 0)
        {
            /* Atomic deletion. */
            tonemap_t *tm = tms->tm[i];
            tms->tm[i] = NULL;
            tonemap_free (tm);
            counter++;
        }
    }
    return counter;
}

void
tonemaps_reset (tonemaps_t *tms, tonemap_release_list_t *tm_release_list)
{
    /* Check parameter. */
    dbg_assert (tms);

    /* Reset default TMI. */
    tms->default_tmi = TONEMAP_INDEX_INITIAL_START;
    /* Changing default tone map must be done before invalidating. */
    arch_reorder_barrier ();
    /* Invalidate all tone maps. */
    /* Go through each tone map. */
    uint i;
    for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB; i++)
    {
        /* Release if in use. */
        if (tms->tm[i])
            tonemap_release (tms, i, tm_release_list);
    }
    /* Reset to default value. */
    tonemaps_init (tms);
}

u32
tonemaps_get_tonemap_enabled_list (tonemaps_t *tms)
{
    /* Check parameters. */
    dbg_assert (tms);

    u32 i, result = 0;
    /* Go through each tone maps. */
    for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB; i++)
    {
        /* If tone maps is allocated and must be deleted now. */
        if (tms->tm[i] && (tms->tm[i]->released == TONEMAP_IN_USE))
        {
            result |= (1 << i);
        }
    }
    return result;
}

u8
tonemaps_set_first_free_tmi (tonemaps_t *tms, tonemap_t *tm)
{
    /* Check parameters. */
    dbg_assert (tms);
    dbg_assert (tm);

    u8 i;
    for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB; i++)
    {
        if (tms->tm[i] == NULL)
        {
            /* Atomic set. */
            tms->tm[i] = tm;
            return i;
        }
    }
    return 0;
}

u8
tonemaps_get_first_free_tmi (tonemaps_t *tms)
{
    /* Check parameters. */
    dbg_assert (tms);

    u8 i;
    for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB; i++)
    {
        if (tms->tm[i] == NULL)
            return i;
    }
    /* TODO: maybe, if we reach this line, we should try to remove the tone
     * map with the released flag set to obtains new tone maps. */
    return 0;
}