Libav 0.7.1
|
00001 /* 00002 * MLP codec common code 00003 * Copyright (c) 2007-2008 Ian Caulfield 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include <stdint.h> 00023 00024 #include "libavutil/crc.h" 00025 #include "libavutil/intreadwrite.h" 00026 #include "mlp.h" 00027 00028 const uint8_t ff_mlp_huffman_tables[3][18][2] = { 00029 { /* Huffman table 0, -7 - +10 */ 00030 {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, 00031 {0x04, 3}, {0x05, 3}, {0x06, 3}, {0x07, 3}, 00032 {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, 00033 }, { /* Huffman table 1, -7 - +8 */ 00034 {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, 00035 {0x02, 2}, {0x03, 2}, 00036 {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, 00037 }, { /* Huffman table 2, -7 - +7 */ 00038 {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, 00039 {0x01, 1}, 00040 {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, 00041 } 00042 }; 00043 00044 static int crc_init = 0; 00045 #if CONFIG_SMALL 00046 #define CRC_TABLE_SIZE 257 00047 #else 00048 #define CRC_TABLE_SIZE 1024 00049 #endif 00050 static AVCRC crc_63[CRC_TABLE_SIZE]; 00051 static AVCRC crc_1D[CRC_TABLE_SIZE]; 00052 static AVCRC crc_2D[CRC_TABLE_SIZE]; 00053 00054 av_cold void ff_mlp_init_crc(void) 00055 { 00056 if (!crc_init) { 00057 av_crc_init(crc_63, 0, 8, 0x63, sizeof(crc_63)); 00058 av_crc_init(crc_1D, 0, 8, 0x1D, sizeof(crc_1D)); 00059 av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D)); 00060 crc_init = 1; 00061 } 00062 } 00063 00064 uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size) 00065 { 00066 uint16_t crc; 00067 00068 crc = av_crc(crc_2D, 0, buf, buf_size - 2); 00069 crc ^= AV_RL16(buf + buf_size - 2); 00070 return crc; 00071 } 00072 00073 uint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size) 00074 { 00075 uint8_t checksum = av_crc(crc_63, 0x3c, buf, buf_size - 1); // crc_63[0xa2] == 0x3c 00076 checksum ^= buf[buf_size-1]; 00077 return checksum; 00078 } 00079 00080 uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size) 00081 { 00082 int i; 00083 int num_bytes = (bit_size + 2) / 8; 00084 00085 int crc = crc_1D[buf[0] & 0x3f]; 00086 crc = av_crc(crc_1D, crc, buf + 1, num_bytes - 2); 00087 crc ^= buf[num_bytes - 1]; 00088 00089 for (i = 0; i < ((bit_size + 2) & 7); i++) { 00090 crc <<= 1; 00091 if (crc & 0x100) 00092 crc ^= 0x11D; 00093 crc ^= (buf[num_bytes] >> (7 - i)) & 1; 00094 } 00095 00096 return crc; 00097 } 00098 00099 uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size) 00100 { 00101 uint32_t scratch = 0; 00102 const uint8_t *buf_end = buf + buf_size; 00103 00104 for (; ((intptr_t) buf & 3) && buf < buf_end; buf++) 00105 scratch ^= *buf; 00106 for (; buf < buf_end - 3; buf += 4) 00107 scratch ^= *((const uint32_t*)buf); 00108 00109 scratch = xor_32_to_8(scratch); 00110 00111 for (; buf < buf_end; buf++) 00112 scratch ^= *buf; 00113 00114 return scratch; 00115 }