File: dab.c

package info (click to toggle)
twolame 0.3.13-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,800 kB
  • sloc: sh: 11,099; ansic: 9,332; perl: 286; makefile: 171
file content (143 lines) | stat: -rw-r--r-- 4,263 bytes parent folder | download | duplicates (7)
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
/*
 *  TwoLAME: an optimized MPEG Audio Layer Two encoder
 *
 *  Copyright (C) 2001-2004 Michael Cheng
 *  Copyright (C) 2004-2006 The TwoLAME Project
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id$
 *
 */


#include <stdio.h>
#include <string.h>

#include "twolame.h"
#include "common.h"
#include "dab.h"


void
dab_crc_calc(twolame_options * glopts,
             unsigned int bit_alloc[2][SBLIMIT],
             unsigned int scfsi[2][SBLIMIT],
             unsigned int scalar[2][3][SBLIMIT], unsigned int *crc, int packed)
{
    int i, j, k;
    int nch = glopts->num_channels_out;
    int nb_scalar;
    int f[5] = { 0, 4, 8, 16, 30 };
    int first, last;

    first = f[packed];
    last = f[packed + 1];
    if (last > glopts->sblimit)
        last = glopts->sblimit;

    nb_scalar = 0;
    *crc = 0x0;
    for (i = first; i < last; i++)
        for (k = 0; k < nch; k++)
            if (bit_alloc[k][i])    /* above jsbound, bit_alloc[0][i] == ba[1][i] */
                switch (scfsi[k][i]) {
                case 0:
                    for (j = 0; j < 3; j++) {
                        nb_scalar++;
                        dab_crc_update(scalar[k][j][i] >> 3, 3, crc);
                    }
                    break;
                case 1:
                case 3:
                    nb_scalar += 2;
                    dab_crc_update(scalar[k][0][i] >> 3, 3, crc);
                    dab_crc_update(scalar[k][2][i] >> 3, 3, crc);
                    break;
                case 2:
                    nb_scalar++;
                    dab_crc_update(scalar[k][0][i] >> 3, 3, crc);
                }
}

void dab_crc_update(unsigned int data, unsigned int length, unsigned int *crc)
{
    unsigned int masking, carry;

    masking = 1 << length;

    while ((masking >>= 1)) {
        carry = *crc & 0x80;
        *crc <<= 1;
        if (!carry ^ !(data & masking))
            *crc ^= CRC8_POLYNOMIAL;
    }
    *crc &= 0xff;
}


#ifdef OLD_BROKEN_DAB_STUFF
// Leaving these here while DAB is reimplemented properly

#define		MINIMUM			4   /* Minimum size of the buffer in bytes */
#define		MAX_LENGTH		32  /* Maximum length of word written */


/*
  19 Jul 03
  DAB stuff got broken in the update to 02m
  Not sure who uses this. I would like to get it working again eventually.
*/

/*
  Just after the available bits calculation in twolame, 
  allocated some bits for the DAB stuff 
*/

if (header.dab_extension) {
    /* in 24 kHz we always have 4 bytes */
    if (header.sampling_frequency == 1)
        header.dab_extension = 4;
    /* You must have one frame in memory if you are in DAB mode */
    /* in conformity of the norme ETS 300 401 http://www.etsi.org */
    /* see bitstream.c */
    if (frameNum == 1)
        minimum = lg_frame + MINIMUM;
    adb -= header.dab_extension * 8 + header.dab_length * 8 + 16;
}


/* 
   Just after we finishing padding up to the number of ADB
   Put the extension stuff in (ancillary data, right at the end)
*/
if (header.dab_extension) {
    /* Reserve some bytes for X-PAD in DAB mode */
    putbits(mybs, 0, header.dab_length * 8);

    for (i = header.dab_extension - 1; i >= 0; i--) {
        CRC_calcDAB(&frame, bit_alloc, scfsi, scalar, &crc, i);
        /* this crc is for the previous frame in DAB mode */
        if (mybs->buf_byte_idx + lg_frame < mybs->buf_size)
            mybs->buf[mybs->buf_byte_idx + lg_frame] = crc;
        /* reserved 2 bytes for F-PAD in DAB mode */
        putbits(mybs, crc, 8);
    }
    putbits(mybs, 0, 16);
}
#endif


// vim:ts=4:sw=4:nowrap: