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