Browse code

avcodec/alsdec: Implement floating point sample data decoding

It conforms to the RM22 version of the reference encoder

Signed-off-by: Umair Khan <omerjerk@gmail.com>

Umair Khan authored on 2016/08/12 21:21:41
Showing 3 changed files
... ...
@@ -16,6 +16,7 @@ version <next>:
16 16
 - crystalizer audio filter
17 17
 - acrusher audio filter
18 18
 - bitplanenoise video filter
19
+- floating point support in als decoder
19 20
 
20 21
 
21 22
 version 3.1:
... ...
@@ -163,7 +163,7 @@ OBJS-$(CONFIG_ALAC_DECODER)            += alac.o alac_data.o alacdsp.o
163 163
 OBJS-$(CONFIG_ALAC_ENCODER)            += alacenc.o alac_data.o
164 164
 OBJS-$(CONFIG_ALIAS_PIX_DECODER)       += aliaspixdec.o
165 165
 OBJS-$(CONFIG_ALIAS_PIX_ENCODER)       += aliaspixenc.o
166
-OBJS-$(CONFIG_ALS_DECODER)             += alsdec.o bgmc.o mpeg4audio.o
166
+OBJS-$(CONFIG_ALS_DECODER)             += alsdec.o bgmc.o mlz.o mpeg4audio.o
167 167
 OBJS-$(CONFIG_AMRNB_DECODER)           += amrnbdec.o celp_filters.o   \
168 168
                                           celp_math.o acelp_filters.o \
169 169
                                           acelp_vectors.o             \
... ...
@@ -35,8 +35,12 @@
35 35
 #include "bgmc.h"
36 36
 #include "bswapdsp.h"
37 37
 #include "internal.h"
38
+#include "mlz.h"
38 39
 #include "libavutil/samplefmt.h"
39 40
 #include "libavutil/crc.h"
41
+#include "libavutil/softfloat_ieee754.h"
42
+#include "libavutil/intfloat.h"
43
+#include "libavutil/intreadwrite.h"
40 44
 
41 45
 #include <stdint.h>
42 46
 
... ...
@@ -225,6 +229,14 @@ typedef struct ALSDecContext {
225 225
     int32_t **raw_samples;          ///< decoded raw samples for each channel
226 226
     int32_t *raw_buffer;            ///< contains all decoded raw samples including carryover samples
227 227
     uint8_t *crc_buffer;            ///< buffer of byte order corrected samples used for CRC check
228
+    MLZ* mlz;                       ///< masked lz decompression structure
229
+    SoftFloat_IEEE754 *acf;         ///< contains common multiplier for all channels
230
+    int *last_acf_mantissa;         ///< contains the last acf mantissa data of common multiplier for all channels
231
+    int *shift_value;               ///< value by which the binary point is to be shifted for all channels
232
+    int *last_shift_value;          ///< contains last shift value for all channels
233
+    int **raw_mantissa;             ///< decoded mantissa bits of the difference signal
234
+    unsigned char *larray;          ///< buffer to store the output of masked lz decompression
235
+    int *nbits;                     ///< contains the number of bits to read for masked lz decompression for all samples
228 236
 } ALSDecContext;
229 237
 
230 238
 
... ...
@@ -441,7 +453,6 @@ static int check_specific_config(ALSDecContext *ctx)
441 441
         }                                               \
442 442
     }
443 443
 
444
-    MISSING_ERR(sconf->floating,  "Floating point decoding",     AVERROR_PATCHWELCOME);
445 444
     MISSING_ERR(sconf->rlslms,    "Adaptive RLS-LMS prediction", AVERROR_PATCHWELCOME);
446 445
 
447 446
     return error;
... ...
@@ -1356,6 +1367,238 @@ static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
1356 1356
 }
1357 1357
 
1358 1358
 
1359
+/** multiply two softfloats and handle the rounding off
1360
+ */
1361
+static SoftFloat_IEEE754 multiply(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b) {
1362
+    uint64_t mantissa_temp;
1363
+    uint64_t mask_64;
1364
+    int cutoff_bit_count;
1365
+    unsigned char last_2_bits;
1366
+    unsigned int mantissa;
1367
+    int32_t sign;
1368
+    uint32_t return_val = 0;
1369
+    int bit_count       = 48;
1370
+
1371
+    sign = a.sign ^ b.sign;
1372
+
1373
+    // Multiply mantissa bits in a 64-bit register
1374
+    mantissa_temp = (uint64_t)a.mant * (uint64_t)b.mant;
1375
+    mask_64       = (uint64_t)0x1 << 47;
1376
+
1377
+    // Count the valid bit count
1378
+    while (!(mantissa_temp & mask_64) && mask_64) {
1379
+        bit_count--;
1380
+        mask_64 >>= 1;
1381
+    }
1382
+
1383
+    // Round off
1384
+    cutoff_bit_count = bit_count - 24;
1385
+    if (cutoff_bit_count > 0) {
1386
+        last_2_bits = (unsigned char)(((unsigned int)mantissa_temp >> (cutoff_bit_count - 1)) & 0x3 );
1387
+        if ((last_2_bits == 0x3) || ((last_2_bits == 0x1) && ((unsigned int)mantissa_temp & ((0x1UL << (cutoff_bit_count - 1)) - 1)))) {
1388
+            // Need to round up
1389
+            mantissa_temp += (uint64_t)0x1 << cutoff_bit_count;
1390
+        }
1391
+    }
1392
+
1393
+    mantissa = (unsigned int)(mantissa_temp >> cutoff_bit_count);
1394
+
1395
+    // Need one more shift?
1396
+    if (mantissa & 0x01000000ul) {
1397
+        bit_count++;
1398
+        mantissa >>= 1;
1399
+    }
1400
+
1401
+    if (!sign) {
1402
+        return_val = 0x80000000U;
1403
+    }
1404
+
1405
+    return_val |= (a.exp + b.exp + bit_count - 47) << 23;
1406
+    return_val |= mantissa;
1407
+    return av_bits2sf_ieee754(return_val);
1408
+}
1409
+
1410
+
1411
+/** Read and decode the floating point sample data
1412
+ */
1413
+static int read_diff_float_data(ALSDecContext *ctx, unsigned int ra_frame) {
1414
+    AVCodecContext *avctx   = ctx->avctx;
1415
+    GetBitContext *gb       = &ctx->gb;
1416
+    SoftFloat_IEEE754 *acf  = ctx->acf;
1417
+    int *shift_value        = ctx->shift_value;
1418
+    int *last_shift_value   = ctx->last_shift_value;
1419
+    int *last_acf_mantissa  = ctx->last_acf_mantissa;
1420
+    int **raw_mantissa      = ctx->raw_mantissa;
1421
+    int *nbits              = ctx->nbits;
1422
+    unsigned char *larray   = ctx->larray;
1423
+    int frame_length        = ctx->cur_frame_length;
1424
+    SoftFloat_IEEE754 scale = av_int2sf_ieee754(0x1u, 23);
1425
+    unsigned int partA_flag;
1426
+    unsigned int highest_byte;
1427
+    unsigned int shift_amp;
1428
+    uint32_t tmp_32;
1429
+    int use_acf;
1430
+    int nchars;
1431
+    int i;
1432
+    int c;
1433
+    long k;
1434
+    long nbits_aligned;
1435
+    unsigned long acc;
1436
+    unsigned long j;
1437
+    uint32_t sign;
1438
+    uint32_t e;
1439
+    uint32_t mantissa;
1440
+
1441
+    skip_bits_long(gb, 32); //num_bytes_diff_float
1442
+    use_acf = get_bits1(gb);
1443
+
1444
+    if (ra_frame) {
1445
+        memset(last_acf_mantissa, 0, avctx->channels * sizeof(*last_acf_mantissa));
1446
+        memset(last_shift_value,  0, avctx->channels * sizeof(*last_shift_value) );
1447
+        ff_mlz_flush_dict(ctx->mlz);
1448
+    }
1449
+
1450
+    for (c = 0; c < avctx->channels; ++c) {
1451
+        if (use_acf) {
1452
+            //acf_flag
1453
+            if (get_bits1(gb)) {
1454
+                tmp_32 = get_bits(gb, 23);
1455
+                last_acf_mantissa[c] = tmp_32;
1456
+            } else {
1457
+                tmp_32 = last_acf_mantissa[c];
1458
+            }
1459
+            acf[c] = av_bits2sf_ieee754(tmp_32);
1460
+        } else {
1461
+            acf[c] = FLOAT_1;
1462
+        }
1463
+
1464
+        highest_byte = get_bits(gb, 2);
1465
+        partA_flag   = get_bits1(gb);
1466
+        shift_amp    = get_bits1(gb);
1467
+
1468
+        if (shift_amp) {
1469
+            shift_value[c] = get_bits(gb, 8);
1470
+            last_shift_value[c] = shift_value[c];
1471
+        } else {
1472
+            shift_value[c] = last_shift_value[c];
1473
+        }
1474
+
1475
+        if (partA_flag) {
1476
+            if (!get_bits1(gb)) { //uncompressed
1477
+                for (i = 0; i < frame_length; ++i) {
1478
+                    if (ctx->raw_samples[c][i] == 0) {
1479
+                        ctx->raw_mantissa[c][i] = get_bits_long(gb, 32);
1480
+                    }
1481
+                }
1482
+            } else { //compressed
1483
+                nchars = 0;
1484
+                for (i = 0; i < frame_length; ++i) {
1485
+                    if (ctx->raw_samples[c][i] == 0) {
1486
+                        nchars += 4;
1487
+                    }
1488
+                }
1489
+
1490
+                tmp_32 = ff_mlz_decompression(ctx->mlz, gb, nchars, larray);
1491
+                if(tmp_32 != nchars) {
1492
+                    av_log(ctx->avctx, AV_LOG_ERROR, "Error in MLZ decompression (%d, %d).\n", tmp_32, nchars);
1493
+                    return AVERROR_INVALIDDATA;
1494
+                }
1495
+
1496
+                for (i = 0; i < frame_length; ++i) {
1497
+                    ctx->raw_mantissa[c][i] = AV_RB32(larray);
1498
+                }
1499
+            }
1500
+        }
1501
+
1502
+        //decode part B
1503
+        if (highest_byte) {
1504
+            for (i = 0; i < frame_length; ++i) {
1505
+                if (ctx->raw_samples[c][i] != 0) {
1506
+                    //The following logic is taken from Tabel 14.45 and 14.46 from the ISO spec
1507
+                    if (av_cmp_sf_ieee754(acf[c], FLOAT_1)) {
1508
+                        nbits[i] = 23 - av_log2(abs(ctx->raw_samples[c][i]));
1509
+                    } else {
1510
+                        nbits[i] = 23;
1511
+                    }
1512
+                    nbits[i] = FFMIN(nbits[i], highest_byte*8);
1513
+                }
1514
+            }
1515
+
1516
+            if (!get_bits1(gb)) { //uncompressed
1517
+                for (i = 0; i < frame_length; ++i) {
1518
+                    if (ctx->raw_samples[c][i] != 0) {
1519
+                        raw_mantissa[c][i] = get_bits(gb, nbits[i]);
1520
+                    }
1521
+                }
1522
+            } else { //compressed
1523
+                nchars = 0;
1524
+                for (i = 0; i < frame_length; ++i) {
1525
+                    if (ctx->raw_samples[c][i]) {
1526
+                        nchars += (int) nbits[i] / 8;
1527
+                        if (nbits[i] & 7) {
1528
+                            ++nchars;
1529
+                        }
1530
+                    }
1531
+                }
1532
+
1533
+                tmp_32 = ff_mlz_decompression(ctx->mlz, gb, nchars, larray);
1534
+                if(tmp_32 != nchars) {
1535
+                    av_log(ctx->avctx, AV_LOG_ERROR, "Error in MLZ decompression (%d, %d).\n", tmp_32, nchars);
1536
+                    return AVERROR_INVALIDDATA;
1537
+                }
1538
+
1539
+                j = 0;
1540
+                for (i = 0; i < frame_length; ++i) {
1541
+                    if (ctx->raw_samples[c][i]) {
1542
+                        if (nbits[i] & 7) {
1543
+                            nbits_aligned = 8 * ((unsigned int)(nbits[i] / 8) + 1);
1544
+                        } else {
1545
+                            nbits_aligned = nbits[i];
1546
+                        }
1547
+                        acc = 0;
1548
+                        for (k = 0; k < nbits_aligned/8; ++k) {
1549
+                            acc = (acc << 8) + larray[j++];
1550
+                        }
1551
+                        acc >>= (nbits_aligned - nbits[i]);
1552
+                        raw_mantissa[c][i] = acc;
1553
+                    }
1554
+                }
1555
+            }
1556
+        }
1557
+
1558
+        for (i = 0; i < frame_length; ++i) {
1559
+            SoftFloat_IEEE754 pcm_sf = av_int2sf_ieee754(ctx->raw_samples[c][i], 0);
1560
+            pcm_sf = av_div_sf_ieee754(pcm_sf, scale);
1561
+
1562
+            if (ctx->raw_samples[c][i] != 0) {
1563
+                if (!av_cmp_sf_ieee754(acf[c], FLOAT_1)) {
1564
+                    pcm_sf = multiply(acf[c], pcm_sf);
1565
+                }
1566
+
1567
+                sign = pcm_sf.sign;
1568
+                e = pcm_sf.exp;
1569
+                mantissa = (pcm_sf.mant | 0x800000) + raw_mantissa[c][i];
1570
+
1571
+                while(mantissa >= 0x1000000) {
1572
+                    e++;
1573
+                    mantissa >>= 1;
1574
+                }
1575
+
1576
+                if (mantissa) e += (shift_value[c] - 127);
1577
+                mantissa &= 0x007fffffUL;
1578
+
1579
+                tmp_32 = (sign << 31) | ((e + EXP_BIAS) << 23) | (mantissa);
1580
+                ctx->raw_samples[c][i] = tmp_32;
1581
+            } else {
1582
+                ctx->raw_samples[c][i] = raw_mantissa[c][i] & 0x007fffffUL;
1583
+            }
1584
+        }
1585
+        align_get_bits(gb);
1586
+    }
1587
+    return 0;
1588
+}
1589
+
1590
+
1359 1591
 /** Read the frame data.
1360 1592
  */
1361 1593
 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
... ...
@@ -1497,7 +1740,9 @@ static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
1497 1497
                     sizeof(*ctx->raw_samples[c]) * sconf->max_order);
1498 1498
     }
1499 1499
 
1500
-    // TODO: read_diff_float_data
1500
+    if (sconf->floating) {
1501
+        read_diff_float_data(ctx, ra_frame);
1502
+    }
1501 1503
 
1502 1504
     if (get_bits_left(gb) < 0) {
1503 1505
         av_log(ctx->avctx, AV_LOG_ERROR, "Overread %d\n", -get_bits_left(gb));
... ...
@@ -1667,6 +1912,14 @@ static av_cold int decode_end(AVCodecContext *avctx)
1667 1667
     av_freep(&ctx->chan_data_buffer);
1668 1668
     av_freep(&ctx->reverted_channels);
1669 1669
     av_freep(&ctx->crc_buffer);
1670
+    av_freep(&ctx->mlz);
1671
+    av_freep(&ctx->acf);
1672
+    av_freep(&ctx->last_acf_mantissa);
1673
+    av_freep(&ctx->shift_value);
1674
+    av_freep(&ctx->last_shift_value);
1675
+    av_freep(&ctx->raw_mantissa);
1676
+    av_freep(&ctx->larray);
1677
+    av_freep(&ctx->nbits);
1670 1678
 
1671 1679
     return 0;
1672 1680
 }
... ...
@@ -1678,6 +1931,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
1678 1678
 {
1679 1679
     unsigned int c;
1680 1680
     unsigned int channel_size;
1681
+    unsigned int i;
1681 1682
     int num_buffers, ret;
1682 1683
     ALSDecContext *ctx = avctx->priv_data;
1683 1684
     ALSSpecificConfig *sconf = &ctx->sconf;
... ...
@@ -1803,6 +2057,32 @@ static av_cold int decode_init(AVCodecContext *avctx)
1803 1803
     ctx->raw_buffer       = av_mallocz_array(avctx->channels * channel_size, sizeof(*ctx->raw_buffer));
1804 1804
     ctx->raw_samples      = av_malloc_array(avctx->channels, sizeof(*ctx->raw_samples));
1805 1805
 
1806
+    if (sconf->floating) {
1807
+        ctx->acf               = av_malloc_array(avctx->channels, sizeof(*ctx->acf));
1808
+        ctx->shift_value       = av_malloc_array(avctx->channels, sizeof(*ctx->shift_value));
1809
+        ctx->last_shift_value  = av_malloc_array(avctx->channels, sizeof(*ctx->last_shift_value));
1810
+        ctx->last_acf_mantissa = av_malloc_array(avctx->channels, sizeof(*ctx->last_acf_mantissa));
1811
+        ctx->raw_mantissa      = av_malloc_array(avctx->channels, sizeof(*ctx->raw_mantissa));
1812
+
1813
+        ctx->larray = av_malloc_array(ctx->cur_frame_length * 4, sizeof(*ctx->larray));
1814
+        ctx->nbits  = av_malloc_array(ctx->cur_frame_length, sizeof(*ctx->nbits));
1815
+        ctx->mlz    = av_malloc(sizeof(*ctx->mlz));
1816
+
1817
+        if (!ctx->mlz || !ctx->acf || !ctx->shift_value || !ctx->last_shift_value
1818
+            || !ctx->last_acf_mantissa || !ctx->raw_mantissa) {
1819
+            av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
1820
+            ret = AVERROR(ENOMEM);
1821
+            goto fail;
1822
+        }
1823
+
1824
+        ff_mlz_init_dict(avctx, ctx->mlz);
1825
+        ff_mlz_flush_dict(ctx->mlz);
1826
+
1827
+        for (c = 0; c < avctx->channels; ++c) {
1828
+            ctx->raw_mantissa[c] = av_mallocz_array(ctx->cur_frame_length, sizeof(**ctx->raw_mantissa));
1829
+        }
1830
+    }
1831
+
1806 1832
     // allocate previous raw sample buffer
1807 1833
     if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
1808 1834
         av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");