| Line | Branch | Exec | Source |
|---|---|---|---|
| 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/channel_layout.h" | ||
| 25 | #include "libavutil/crc.h" | ||
| 26 | #include "libavutil/intreadwrite.h" | ||
| 27 | #include "libavutil/thread.h" | ||
| 28 | #include "mlp.h" | ||
| 29 | |||
| 30 | const uint8_t ff_mlp_huffman_tables[3][18][2] = { | ||
| 31 | { /* Huffman table 0, -7 - +10 */ | ||
| 32 | {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, | ||
| 33 | {0x04, 3}, {0x05, 3}, {0x06, 3}, {0x07, 3}, | ||
| 34 | {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, | ||
| 35 | }, { /* Huffman table 1, -7 - +8 */ | ||
| 36 | {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, | ||
| 37 | {0x02, 2}, {0x03, 2}, | ||
| 38 | {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, | ||
| 39 | }, { /* Huffman table 2, -7 - +7 */ | ||
| 40 | {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, | ||
| 41 | {0x01, 1}, | ||
| 42 | {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, | ||
| 43 | } | ||
| 44 | }; | ||
| 45 | |||
| 46 | const ChannelInformation ff_mlp_ch_info[21] = { | ||
| 47 | { 0x01, 0x01, 0x00, 0x1f }, { 0x03, 0x02, 0x00, 0x1b }, | ||
| 48 | { 0x07, 0x02, 0x01, 0x1f }, { 0x0F, 0x02, 0x02, 0x19 }, | ||
| 49 | { 0x07, 0x02, 0x01, 0x03 }, { 0x0F, 0x02, 0x02, 0x1f }, | ||
| 50 | { 0x1F, 0x02, 0x03, 0x01 }, { 0x07, 0x02, 0x01, 0x1a }, | ||
| 51 | { 0x0F, 0x02, 0x02, 0x1f }, { 0x1F, 0x02, 0x03, 0x18 }, | ||
| 52 | { 0x0F, 0x02, 0x02, 0x02 }, { 0x1F, 0x02, 0x03, 0x1f }, | ||
| 53 | { 0x3F, 0x02, 0x04, 0x00 }, { 0x0F, 0x03, 0x01, 0x1f }, | ||
| 54 | { 0x1F, 0x03, 0x02, 0x18 }, { 0x0F, 0x03, 0x01, 0x02 }, | ||
| 55 | { 0x1F, 0x03, 0x02, 0x1f }, { 0x3F, 0x03, 0x03, 0x00 }, | ||
| 56 | { 0x1F, 0x04, 0x01, 0x01 }, { 0x1F, 0x04, 0x01, 0x18 }, | ||
| 57 | { 0x3F, 0x04, 0x02, 0x00 }, | ||
| 58 | }; | ||
| 59 | |||
| 60 | const AVChannelLayout ff_mlp_ch_layouts[12] = { | ||
| 61 | AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO, AV_CHANNEL_LAYOUT_2_1, | ||
| 62 | AV_CHANNEL_LAYOUT_QUAD, AV_CHANNEL_LAYOUT_2POINT1, AV_CHANNEL_LAYOUT_SURROUND, | ||
| 63 | AV_CHANNEL_LAYOUT_4POINT0, AV_CHANNEL_LAYOUT_5POINT0_BACK, AV_CHANNEL_LAYOUT_3POINT1, | ||
| 64 | AV_CHANNEL_LAYOUT_4POINT1, AV_CHANNEL_LAYOUT_5POINT1_BACK, { 0 }, | ||
| 65 | }; | ||
| 66 | |||
| 67 | #if CONFIG_SMALL | ||
| 68 | #define CRC_TABLE_SIZE 257 | ||
| 69 | #else | ||
| 70 | #define CRC_TABLE_SIZE 1024 | ||
| 71 | #endif | ||
| 72 | static AVCRC crc_63[CRC_TABLE_SIZE]; | ||
| 73 | static AVCRC crc_2D[CRC_TABLE_SIZE]; | ||
| 74 | |||
| 75 | 9 | static av_cold void mlp_init_crc(void) | |
| 76 | { | ||
| 77 | 9 | av_crc_init(crc_63, 0, 8, 0x63, sizeof(crc_63)); | |
| 78 | 9 | av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D)); | |
| 79 | 9 | } | |
| 80 | |||
| 81 | 18 | av_cold void ff_mlp_init_crc(void) | |
| 82 | { | ||
| 83 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
| 84 | 18 | ff_thread_once(&init_static_once, mlp_init_crc); | |
| 85 | 18 | } | |
| 86 | |||
| 87 | 4658 | uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size) | |
| 88 | { | ||
| 89 | uint16_t crc; | ||
| 90 | |||
| 91 | 4658 | crc = av_crc(crc_2D, 0, buf, buf_size - 2); | |
| 92 | 4658 | crc ^= AV_RL16(buf + buf_size - 2); | |
| 93 | 4658 | return crc; | |
| 94 | } | ||
| 95 | |||
| 96 | 28029 | uint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size) | |
| 97 | { | ||
| 98 | 28029 | uint8_t checksum = av_crc(crc_63, 0x3c, buf, buf_size - 1); // crc_63[0xa2] == 0x3c | |
| 99 | 28029 | checksum ^= buf[buf_size-1]; | |
| 100 | 28029 | return checksum; | |
| 101 | } | ||
| 102 | |||
| 103 | 2220 | uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size) | |
| 104 | { | ||
| 105 | 2220 | const AVCRC *crc_1D = av_crc_get_table(AV_CRC_8_EBU); | |
| 106 | int i; | ||
| 107 | 2220 | int num_bytes = (bit_size + 2) / 8; | |
| 108 | |||
| 109 | // The two most significant bits of buf[0] are not supposed | ||
| 110 | // to be contained in the checksum; using buf[0] & 0xC0 as start value | ||
| 111 | // achieves this. | ||
| 112 | 2220 | int crc = av_crc(crc_1D, buf[0] & 0xC0, buf, num_bytes - 1); | |
| 113 | |||
| 114 | 2220 | crc ^= buf[num_bytes - 1]; | |
| 115 | |||
| 116 |
2/2✓ Branch 0 taken 15476 times.
✓ Branch 1 taken 2220 times.
|
17696 | for (i = 0; i < ((bit_size + 2) & 7); i++) { |
| 117 | 15476 | crc <<= 1; | |
| 118 |
2/2✓ Branch 0 taken 7618 times.
✓ Branch 1 taken 7858 times.
|
15476 | if (crc & 0x100) |
| 119 | 7618 | crc ^= 0x11D; | |
| 120 | 15476 | crc ^= (buf[num_bytes] >> (7 - i)) & 1; | |
| 121 | } | ||
| 122 | |||
| 123 | 2220 | return crc; | |
| 124 | } | ||
| 125 | |||
| 126 | 68307 | uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size) | |
| 127 | { | ||
| 128 | 68307 | uint32_t scratch = 0; | |
| 129 | 68307 | const uint8_t *buf_end = buf + buf_size; | |
| 130 | |||
| 131 |
3/4✓ Branch 0 taken 25830 times.
✓ Branch 1 taken 68307 times.
✓ Branch 2 taken 25830 times.
✗ Branch 3 not taken.
|
94137 | for (; ((intptr_t) buf & 3) && buf < buf_end; buf++) |
| 132 | 25830 | scratch ^= *buf; | |
| 133 |
2/2✓ Branch 0 taken 635482 times.
✓ Branch 1 taken 68307 times.
|
703789 | for (; buf < buf_end - 3; buf += 4) |
| 134 | 635482 | scratch ^= *((const uint32_t*)buf); | |
| 135 | |||
| 136 | 68307 | scratch = xor_32_to_8(scratch); | |
| 137 | |||
| 138 |
2/2✓ Branch 0 taken 46802 times.
✓ Branch 1 taken 68307 times.
|
115109 | for (; buf < buf_end; buf++) |
| 139 | 46802 | scratch ^= *buf; | |
| 140 | |||
| 141 | 68307 | return scratch; | |
| 142 | } | ||
| 143 |