You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.4KB

  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. #include <stdint.h>
  22. #include "libavutil/crc.h"
  23. #include "mlp.h"
  24. const uint8_t ff_mlp_huffman_tables[3][18][2] = {
  25. { /* Huffman table 0, -7 - +10 */
  26. {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},
  27. {0x04, 3}, {0x05, 3}, {0x06, 3}, {0x07, 3},
  28. {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},
  29. }, { /* Huffman table 1, -7 - +8 */
  30. {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},
  31. {0x02, 2}, {0x03, 2},
  32. {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},
  33. }, { /* Huffman table 2, -7 - +7 */
  34. {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},
  35. {0x01, 1},
  36. {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},
  37. }
  38. };
  39. static int crc_init = 0;
  40. static AVCRC crc_63[1024];
  41. static AVCRC crc_1D[1024];
  42. static int crc_init_2D = 0;
  43. static AVCRC crc_2D[1024];
  44. int av_cold ff_mlp_init_crc2D(AVCodecParserContext *s)
  45. {
  46. if (!crc_init_2D) {
  47. av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
  48. crc_init_2D = 1;
  49. }
  50. return 0;
  51. }
  52. void av_cold ff_mlp_init_crc()
  53. {
  54. if (!crc_init) {
  55. av_crc_init(crc_63, 0, 8, 0x63, sizeof(crc_63));
  56. av_crc_init(crc_1D, 0, 8, 0x1D, sizeof(crc_1D));
  57. crc_init = 1;
  58. }
  59. }
  60. uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
  61. {
  62. uint16_t crc;
  63. crc = av_crc(crc_2D, 0, buf, buf_size - 2);
  64. crc ^= AV_RL16(buf + buf_size - 2);
  65. return crc;
  66. }
  67. uint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size)
  68. {
  69. uint8_t checksum = av_crc(crc_63, 0x3c, buf, buf_size - 1); // crc_63[0xa2] == 0x3c
  70. checksum ^= buf[buf_size-1];
  71. return checksum;
  72. }
  73. uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size)
  74. {
  75. int i;
  76. int num_bytes = (bit_size + 2) / 8;
  77. int crc = crc_1D[buf[0] & 0x3f];
  78. crc = av_crc(crc_1D, crc, buf + 1, num_bytes - 2);
  79. crc ^= buf[num_bytes - 1];
  80. for (i = 0; i < ((bit_size + 2) & 7); i++) {
  81. crc <<= 1;
  82. if (crc & 0x100)
  83. crc ^= 0x11D;
  84. crc ^= (buf[num_bytes] >> (7 - i)) & 1;
  85. }
  86. return crc;
  87. }
  88. uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size)
  89. {
  90. uint32_t scratch = 0;
  91. const uint8_t *buf_end = buf + buf_size;
  92. for (; buf < buf_end - 3; buf += 4)
  93. scratch ^= *((const uint32_t*)buf);
  94. scratch = xor_32_to_8(scratch);
  95. for (; buf < buf_end; buf++)
  96. scratch ^= *buf;
  97. return scratch;
  98. }