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