yading@10
|
1 /*
|
yading@10
|
2 * MLP codec common code
|
yading@10
|
3 * Copyright (c) 2007-2008 Ian Caulfield
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 #include <stdint.h>
|
yading@10
|
23
|
yading@10
|
24 #include "libavutil/crc.h"
|
yading@10
|
25 #include "libavutil/intreadwrite.h"
|
yading@10
|
26 #include "mlp.h"
|
yading@10
|
27
|
yading@10
|
28 const uint8_t ff_mlp_huffman_tables[3][18][2] = {
|
yading@10
|
29 { /* Huffman table 0, -7 - +10 */
|
yading@10
|
30 {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},
|
yading@10
|
31 {0x04, 3}, {0x05, 3}, {0x06, 3}, {0x07, 3},
|
yading@10
|
32 {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},
|
yading@10
|
33 }, { /* Huffman table 1, -7 - +8 */
|
yading@10
|
34 {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},
|
yading@10
|
35 {0x02, 2}, {0x03, 2},
|
yading@10
|
36 {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},
|
yading@10
|
37 }, { /* Huffman table 2, -7 - +7 */
|
yading@10
|
38 {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},
|
yading@10
|
39 {0x01, 1},
|
yading@10
|
40 {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},
|
yading@10
|
41 }
|
yading@10
|
42 };
|
yading@10
|
43
|
yading@10
|
44 static int crc_init = 0;
|
yading@10
|
45 #if CONFIG_SMALL
|
yading@10
|
46 #define CRC_TABLE_SIZE 257
|
yading@10
|
47 #else
|
yading@10
|
48 #define CRC_TABLE_SIZE 1024
|
yading@10
|
49 #endif
|
yading@10
|
50 static AVCRC crc_63[CRC_TABLE_SIZE];
|
yading@10
|
51 static AVCRC crc_1D[CRC_TABLE_SIZE];
|
yading@10
|
52 static AVCRC crc_2D[CRC_TABLE_SIZE];
|
yading@10
|
53
|
yading@10
|
54 av_cold void ff_mlp_init_crc(void)
|
yading@10
|
55 {
|
yading@10
|
56 if (!crc_init) {
|
yading@10
|
57 av_crc_init(crc_63, 0, 8, 0x63, sizeof(crc_63));
|
yading@10
|
58 av_crc_init(crc_1D, 0, 8, 0x1D, sizeof(crc_1D));
|
yading@10
|
59 av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
|
yading@10
|
60 crc_init = 1;
|
yading@10
|
61 }
|
yading@10
|
62 }
|
yading@10
|
63
|
yading@10
|
64 uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
|
yading@10
|
65 {
|
yading@10
|
66 uint16_t crc;
|
yading@10
|
67
|
yading@10
|
68 crc = av_crc(crc_2D, 0, buf, buf_size - 2);
|
yading@10
|
69 crc ^= AV_RL16(buf + buf_size - 2);
|
yading@10
|
70 return crc;
|
yading@10
|
71 }
|
yading@10
|
72
|
yading@10
|
73 uint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size)
|
yading@10
|
74 {
|
yading@10
|
75 uint8_t checksum = av_crc(crc_63, 0x3c, buf, buf_size - 1); // crc_63[0xa2] == 0x3c
|
yading@10
|
76 checksum ^= buf[buf_size-1];
|
yading@10
|
77 return checksum;
|
yading@10
|
78 }
|
yading@10
|
79
|
yading@10
|
80 uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size)
|
yading@10
|
81 {
|
yading@10
|
82 int i;
|
yading@10
|
83 int num_bytes = (bit_size + 2) / 8;
|
yading@10
|
84
|
yading@10
|
85 int crc = crc_1D[buf[0] & 0x3f];
|
yading@10
|
86 crc = av_crc(crc_1D, crc, buf + 1, num_bytes - 2);
|
yading@10
|
87 crc ^= buf[num_bytes - 1];
|
yading@10
|
88
|
yading@10
|
89 for (i = 0; i < ((bit_size + 2) & 7); i++) {
|
yading@10
|
90 crc <<= 1;
|
yading@10
|
91 if (crc & 0x100)
|
yading@10
|
92 crc ^= 0x11D;
|
yading@10
|
93 crc ^= (buf[num_bytes] >> (7 - i)) & 1;
|
yading@10
|
94 }
|
yading@10
|
95
|
yading@10
|
96 return crc;
|
yading@10
|
97 }
|
yading@10
|
98
|
yading@10
|
99 uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size)
|
yading@10
|
100 {
|
yading@10
|
101 uint32_t scratch = 0;
|
yading@10
|
102 const uint8_t *buf_end = buf + buf_size;
|
yading@10
|
103
|
yading@10
|
104 for (; ((intptr_t) buf & 3) && buf < buf_end; buf++)
|
yading@10
|
105 scratch ^= *buf;
|
yading@10
|
106 for (; buf < buf_end - 3; buf += 4)
|
yading@10
|
107 scratch ^= *((const uint32_t*)buf);
|
yading@10
|
108
|
yading@10
|
109 scratch = xor_32_to_8(scratch);
|
yading@10
|
110
|
yading@10
|
111 for (; buf < buf_end; buf++)
|
yading@10
|
112 scratch ^= *buf;
|
yading@10
|
113
|
yading@10
|
114 return scratch;
|
yading@10
|
115 }
|