Revision ce15710f libavcodec/mlpdec.c

View differences:

libavcodec/mlpdec.c
32 32
#include "libavutil/crc.h"
33 33
#include "parser.h"
34 34
#include "mlp_parser.h"
35

  
36
/** Maximum number of channels that can be decoded. */
37
#define MAX_CHANNELS        16
38

  
39
/** Maximum number of matrices used in decoding; most streams have one matrix
40
 *  per output channel, but some rematrix a channel (usually 0) more than once.
41
 */
42

  
43
#define MAX_MATRICES        15
44

  
45
/** Maximum number of substreams that can be decoded. This could also be set
46
 *  higher, but I haven't seen any examples with more than two. */
47
#define MAX_SUBSTREAMS      2
48

  
49
/** maximum sample frequency seen in files */
50
#define MAX_SAMPLERATE      192000
51

  
52
/** maximum number of audio samples within one access unit */
53
#define MAX_BLOCKSIZE       (40 * (MAX_SAMPLERATE / 48000))
54
/** next power of two greater than MAX_BLOCKSIZE */
55
#define MAX_BLOCKSIZE_POW2  (64 * (MAX_SAMPLERATE / 48000))
56

  
57
/** number of allowed filters */
58
#define NUM_FILTERS         2
59

  
60
/** The maximum number of taps in either the IIR or FIR filter;
61
 *  I believe MLP actually specifies the maximum order for IIR filters as four,
62
 *  and that the sum of the orders of both filters must be <= 8. */
63
#define MAX_FILTER_ORDER    8
35
#include "mlp.h"
64 36

  
65 37
/** number of bits used for VLC lookup - longest Huffman code is 9 */
66 38
#define VLC_BITS            9
......
139 111

  
140 112
} SubStream;
141 113

  
142
#define FIR 0
143
#define IIR 1
144

  
145
/** filter data */
146
typedef struct {
147
    uint8_t     order; ///< number of taps in filter
148
    uint8_t     shift; ///< Right shift to apply to output of filter.
149

  
150
    int32_t     coeff[MAX_FILTER_ORDER];
151
    int32_t     state[MAX_FILTER_ORDER];
152
} FilterParams;
153

  
154
/** sample data coding information */
155
typedef struct {
156
    FilterParams filter_params[NUM_FILTERS];
157

  
158
    int16_t     huff_offset;      ///< Offset to apply to residual values.
159
    int32_t     sign_huff_offset; ///< sign/rounding-corrected version of huff_offset
160
    uint8_t     codebook;         ///< Which VLC codebook to use to read residuals.
161
    uint8_t     huff_lsbs;        ///< Size of residual suffix not encoded using VLC.
162
} ChannelParams;
163

  
164 114
typedef struct MLPDecodeContext {
165 115
    AVCodecContext *avctx;
166 116

  
......
187 137
    int32_t     sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS+2];
188 138
} MLPDecodeContext;
189 139

  
190
/** Tables defining the Huffman codes.
191
 *  There are three entropy coding methods used in MLP (four if you count
192
 *  "none" as a method). These use the same sequences for codes starting with
193
 *  00 or 01, but have different codes starting with 1. */
194

  
195
static const uint8_t huffman_tables[3][18][2] = {
196
    {    /* Huffman table 0, -7 - +10 */
197
        {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},
198
        {0x04, 3}, {0x05, 3}, {0x06, 3}, {0x07, 3},
199
        {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},
200
    }, { /* Huffman table 1, -7 - +8 */
201
        {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},
202
        {0x02, 2}, {0x03, 2},
203
        {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},
204
    }, { /* Huffman table 2, -7 - +7 */
205
        {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},
206
        {0x01, 1},
207
        {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},
208
    }
209
};
210

  
211 140
static VLC huff_vlc[3];
212 141

  
213
static int crc_init = 0;
214
static AVCRC crc_63[1024];
215
static AVCRC crc_1D[1024];
216

  
217

  
218 142
/** Initialize static data, constant between all invocations of the codec. */
219 143

  
220 144
static av_cold void init_static()
221 145
{
222 146
    INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,
223
                &huffman_tables[0][0][1], 2, 1,
224
                &huffman_tables[0][0][0], 2, 1, 512);
147
                &ff_mlp_huffman_tables[0][0][1], 2, 1,
148
                &ff_mlp_huffman_tables[0][0][0], 2, 1, 512);
225 149
    INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,
226
                &huffman_tables[1][0][1], 2, 1,
227
                &huffman_tables[1][0][0], 2, 1, 512);
150
                &ff_mlp_huffman_tables[1][0][1], 2, 1,
151
                &ff_mlp_huffman_tables[1][0][0], 2, 1, 512);
228 152
    INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,
229
                &huffman_tables[2][0][1], 2, 1,
230
                &huffman_tables[2][0][0], 2, 1, 512);
153
                &ff_mlp_huffman_tables[2][0][1], 2, 1,
154
                &ff_mlp_huffman_tables[2][0][0], 2, 1, 512);
231 155

  
232
    if (!crc_init) {
233
        av_crc_init(crc_63, 0,  8,   0x63, sizeof(crc_63));
234
        av_crc_init(crc_1D, 0,  8,   0x1D, sizeof(crc_1D));
235
        crc_init = 1;
236
    }
237
}
238

  
239

  
240
/** MLP uses checksums that seem to be based on the standard CRC algorithm, but
241
 *  are not (in implementation terms, the table lookup and XOR are reversed).
242
 *  We can implement this behavior using a standard av_crc on all but the
243
 *  last element, then XOR that with the last element. */
244

  
245
static uint8_t mlp_checksum8(const uint8_t *buf, unsigned int buf_size)
246
{
247
    uint8_t checksum = av_crc(crc_63, 0x3c, buf, buf_size - 1); // crc_63[0xa2] == 0x3c
248
    checksum ^= buf[buf_size-1];
249
    return checksum;
250
}
251

  
252
/** Calculate an 8-bit checksum over a restart header -- a non-multiple-of-8
253
 *  number of bits, starting two bits into the first byte of buf. */
254

  
255
static uint8_t mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size)
256
{
257
    int i;
258
    int num_bytes = (bit_size + 2) / 8;
259

  
260
    int crc = crc_1D[buf[0] & 0x3f];
261
    crc = av_crc(crc_1D, crc, buf + 1, num_bytes - 2);
262
    crc ^= buf[num_bytes - 1];
263

  
264
    for (i = 0; i < ((bit_size + 2) & 7); i++) {
265
        crc <<= 1;
266
        if (crc & 0x100)
267
            crc ^= 0x11D;
268
        crc ^= (buf[num_bytes] >> (7 - i)) & 1;
269
    }
270

  
271
    return crc;
156
    ff_mlp_init_crc();
272 157
}
273 158

  
274 159
static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
......
506 391
        }
507 392
    }
508 393

  
509
    checksum = mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
394
    checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
510 395

  
511 396
    if (checksum != get_bits(gbp, 8))
512 397
        av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
......
975 860
}
976 861

  
977 862

  
978
/** XOR together all the bytes of a buffer.
979
 *  Does this belong in dspcontext? */
980

  
981
static uint8_t calculate_parity(const uint8_t *buf, unsigned int buf_size)
982
{
983
    uint32_t scratch = 0;
984
    const uint8_t *buf_end = buf + buf_size;
985

  
986
    for (; buf < buf_end - 3; buf += 4)
987
        scratch ^= *((const uint32_t*)buf);
988

  
989
    scratch ^= scratch >> 16;
990
    scratch ^= scratch >> 8;
991

  
992
    for (; buf < buf_end; buf++)
993
        scratch ^= *buf;
994

  
995
    return scratch;
996
}
997

  
998 863
/** Read an access unit from the stream.
999 864
 *  Returns < 0 on error, 0 if not enough data is present in the input stream
1000 865
 *  otherwise returns the number of bytes consumed. */
......
1078 943
        substream_start = end;
1079 944
    }
1080 945

  
1081
    parity_bits  = calculate_parity(buf, 4);
1082
    parity_bits ^= calculate_parity(buf + header_size, substr_header_size);
946
    parity_bits  = ff_mlp_calculate_parity(buf, 4);
947
    parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
1083 948

  
1084 949
    if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
1085 950
        av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
......
1145 1010
            substream_parity_present[substr]) {
1146 1011
            uint8_t parity, checksum;
1147 1012

  
1148
            parity = calculate_parity(buf, substream_data_len[substr] - 2);
1013
            parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
1149 1014
            if ((parity ^ get_bits(&gb, 8)) != 0xa9)
1150 1015
                av_log(m->avctx, AV_LOG_ERROR,
1151 1016
                       "Substream %d parity check failed.\n", substr);
1152 1017

  
1153
            checksum = mlp_checksum8(buf, substream_data_len[substr] - 2);
1018
            checksum = ff_mlp_checksum8(buf, substream_data_len[substr] - 2);
1154 1019
            if (checksum != get_bits(&gb, 8))
1155 1020
                av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n",
1156 1021
                       substr);

Also available in: Unified diff