1cabdff1aSopenharmony_ci/* 2cabdff1aSopenharmony_ci * MLP codec common code 3cabdff1aSopenharmony_ci * Copyright (c) 2007-2008 Ian Caulfield 4cabdff1aSopenharmony_ci * 5cabdff1aSopenharmony_ci * This file is part of FFmpeg. 6cabdff1aSopenharmony_ci * 7cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or 8cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public 9cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either 10cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version. 11cabdff1aSopenharmony_ci * 12cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful, 13cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 14cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15cabdff1aSopenharmony_ci * Lesser General Public License for more details. 16cabdff1aSopenharmony_ci * 17cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public 18cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software 19cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20cabdff1aSopenharmony_ci */ 21cabdff1aSopenharmony_ci 22cabdff1aSopenharmony_ci#include <stdint.h> 23cabdff1aSopenharmony_ci 24cabdff1aSopenharmony_ci#include "libavutil/channel_layout.h" 25cabdff1aSopenharmony_ci#include "libavutil/crc.h" 26cabdff1aSopenharmony_ci#include "libavutil/intreadwrite.h" 27cabdff1aSopenharmony_ci#include "libavutil/thread.h" 28cabdff1aSopenharmony_ci#include "mlp.h" 29cabdff1aSopenharmony_ci 30cabdff1aSopenharmony_ciconst uint8_t ff_mlp_huffman_tables[3][18][2] = { 31cabdff1aSopenharmony_ci { /* Huffman table 0, -7 - +10 */ 32cabdff1aSopenharmony_ci {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, 33cabdff1aSopenharmony_ci {0x04, 3}, {0x05, 3}, {0x06, 3}, {0x07, 3}, 34cabdff1aSopenharmony_ci {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, 35cabdff1aSopenharmony_ci }, { /* Huffman table 1, -7 - +8 */ 36cabdff1aSopenharmony_ci {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, 37cabdff1aSopenharmony_ci {0x02, 2}, {0x03, 2}, 38cabdff1aSopenharmony_ci {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, 39cabdff1aSopenharmony_ci }, { /* Huffman table 2, -7 - +7 */ 40cabdff1aSopenharmony_ci {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, 41cabdff1aSopenharmony_ci {0x01, 1}, 42cabdff1aSopenharmony_ci {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, 43cabdff1aSopenharmony_ci } 44cabdff1aSopenharmony_ci}; 45cabdff1aSopenharmony_ci 46cabdff1aSopenharmony_ciconst ChannelInformation ff_mlp_ch_info[21] = { 47cabdff1aSopenharmony_ci { 0x01, 0x01, 0x00, 0x1f }, { 0x03, 0x02, 0x00, 0x1b }, 48cabdff1aSopenharmony_ci { 0x07, 0x02, 0x01, 0x1f }, { 0x0F, 0x02, 0x02, 0x19 }, 49cabdff1aSopenharmony_ci { 0x07, 0x02, 0x01, 0x03 }, { 0x0F, 0x02, 0x02, 0x1f }, 50cabdff1aSopenharmony_ci { 0x1F, 0x02, 0x03, 0x01 }, { 0x07, 0x02, 0x01, 0x1a }, 51cabdff1aSopenharmony_ci { 0x0F, 0x02, 0x02, 0x1f }, { 0x1F, 0x02, 0x03, 0x18 }, 52cabdff1aSopenharmony_ci { 0x0F, 0x02, 0x02, 0x02 }, { 0x1F, 0x02, 0x03, 0x1f }, 53cabdff1aSopenharmony_ci { 0x3F, 0x02, 0x04, 0x00 }, { 0x0F, 0x03, 0x01, 0x1f }, 54cabdff1aSopenharmony_ci { 0x1F, 0x03, 0x02, 0x18 }, { 0x0F, 0x03, 0x01, 0x02 }, 55cabdff1aSopenharmony_ci { 0x1F, 0x03, 0x02, 0x1f }, { 0x3F, 0x03, 0x03, 0x00 }, 56cabdff1aSopenharmony_ci { 0x1F, 0x04, 0x01, 0x01 }, { 0x1F, 0x04, 0x01, 0x18 }, 57cabdff1aSopenharmony_ci { 0x3F, 0x04, 0x02, 0x00 }, 58cabdff1aSopenharmony_ci}; 59cabdff1aSopenharmony_ci 60cabdff1aSopenharmony_ci#if FF_API_OLD_CHANNEL_LAYOUT 61cabdff1aSopenharmony_ciconst uint64_t ff_mlp_channel_layouts[12] = { 62cabdff1aSopenharmony_ci AV_CH_LAYOUT_MONO, AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_2_1, 63cabdff1aSopenharmony_ci AV_CH_LAYOUT_QUAD, AV_CH_LAYOUT_2POINT1, AV_CH_LAYOUT_SURROUND, 64cabdff1aSopenharmony_ci AV_CH_LAYOUT_4POINT0, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_3POINT1, 65cabdff1aSopenharmony_ci AV_CH_LAYOUT_4POINT1, AV_CH_LAYOUT_5POINT1_BACK, 0, 66cabdff1aSopenharmony_ci}; 67cabdff1aSopenharmony_ci#endif 68cabdff1aSopenharmony_ci 69cabdff1aSopenharmony_ciconst AVChannelLayout ff_mlp_ch_layouts[12] = { 70cabdff1aSopenharmony_ci AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO, AV_CHANNEL_LAYOUT_2_1, 71cabdff1aSopenharmony_ci AV_CHANNEL_LAYOUT_QUAD, AV_CHANNEL_LAYOUT_2POINT1, AV_CHANNEL_LAYOUT_SURROUND, 72cabdff1aSopenharmony_ci AV_CHANNEL_LAYOUT_4POINT0, AV_CHANNEL_LAYOUT_5POINT0_BACK, AV_CHANNEL_LAYOUT_3POINT1, 73cabdff1aSopenharmony_ci AV_CHANNEL_LAYOUT_4POINT1, AV_CHANNEL_LAYOUT_5POINT1_BACK, { 0 }, 74cabdff1aSopenharmony_ci}; 75cabdff1aSopenharmony_ci 76cabdff1aSopenharmony_ci#if CONFIG_SMALL 77cabdff1aSopenharmony_ci#define CRC_TABLE_SIZE 257 78cabdff1aSopenharmony_ci#else 79cabdff1aSopenharmony_ci#define CRC_TABLE_SIZE 1024 80cabdff1aSopenharmony_ci#endif 81cabdff1aSopenharmony_cistatic AVCRC crc_63[CRC_TABLE_SIZE]; 82cabdff1aSopenharmony_cistatic AVCRC crc_1D[CRC_TABLE_SIZE]; 83cabdff1aSopenharmony_cistatic AVCRC crc_2D[CRC_TABLE_SIZE]; 84cabdff1aSopenharmony_ci 85cabdff1aSopenharmony_cistatic av_cold void mlp_init_crc(void) 86cabdff1aSopenharmony_ci{ 87cabdff1aSopenharmony_ci av_crc_init(crc_63, 0, 8, 0x63, sizeof(crc_63)); 88cabdff1aSopenharmony_ci av_crc_init(crc_1D, 0, 8, 0x1D, sizeof(crc_1D)); 89cabdff1aSopenharmony_ci av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D)); 90cabdff1aSopenharmony_ci} 91cabdff1aSopenharmony_ci 92cabdff1aSopenharmony_ciav_cold void ff_mlp_init_crc(void) 93cabdff1aSopenharmony_ci{ 94cabdff1aSopenharmony_ci static AVOnce init_static_once = AV_ONCE_INIT; 95cabdff1aSopenharmony_ci ff_thread_once(&init_static_once, mlp_init_crc); 96cabdff1aSopenharmony_ci} 97cabdff1aSopenharmony_ci 98cabdff1aSopenharmony_ciuint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size) 99cabdff1aSopenharmony_ci{ 100cabdff1aSopenharmony_ci uint16_t crc; 101cabdff1aSopenharmony_ci 102cabdff1aSopenharmony_ci crc = av_crc(crc_2D, 0, buf, buf_size - 2); 103cabdff1aSopenharmony_ci crc ^= AV_RL16(buf + buf_size - 2); 104cabdff1aSopenharmony_ci return crc; 105cabdff1aSopenharmony_ci} 106cabdff1aSopenharmony_ci 107cabdff1aSopenharmony_ciuint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size) 108cabdff1aSopenharmony_ci{ 109cabdff1aSopenharmony_ci uint8_t checksum = av_crc(crc_63, 0x3c, buf, buf_size - 1); // crc_63[0xa2] == 0x3c 110cabdff1aSopenharmony_ci checksum ^= buf[buf_size-1]; 111cabdff1aSopenharmony_ci return checksum; 112cabdff1aSopenharmony_ci} 113cabdff1aSopenharmony_ci 114cabdff1aSopenharmony_ciuint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size) 115cabdff1aSopenharmony_ci{ 116cabdff1aSopenharmony_ci int i; 117cabdff1aSopenharmony_ci int num_bytes = (bit_size + 2) / 8; 118cabdff1aSopenharmony_ci 119cabdff1aSopenharmony_ci int crc = crc_1D[buf[0] & 0x3f]; 120cabdff1aSopenharmony_ci crc = av_crc(crc_1D, crc, buf + 1, num_bytes - 2); 121cabdff1aSopenharmony_ci crc ^= buf[num_bytes - 1]; 122cabdff1aSopenharmony_ci 123cabdff1aSopenharmony_ci for (i = 0; i < ((bit_size + 2) & 7); i++) { 124cabdff1aSopenharmony_ci crc <<= 1; 125cabdff1aSopenharmony_ci if (crc & 0x100) 126cabdff1aSopenharmony_ci crc ^= 0x11D; 127cabdff1aSopenharmony_ci crc ^= (buf[num_bytes] >> (7 - i)) & 1; 128cabdff1aSopenharmony_ci } 129cabdff1aSopenharmony_ci 130cabdff1aSopenharmony_ci return crc; 131cabdff1aSopenharmony_ci} 132cabdff1aSopenharmony_ci 133cabdff1aSopenharmony_ciuint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size) 134cabdff1aSopenharmony_ci{ 135cabdff1aSopenharmony_ci uint32_t scratch = 0; 136cabdff1aSopenharmony_ci const uint8_t *buf_end = buf + buf_size; 137cabdff1aSopenharmony_ci 138cabdff1aSopenharmony_ci for (; ((intptr_t) buf & 3) && buf < buf_end; buf++) 139cabdff1aSopenharmony_ci scratch ^= *buf; 140cabdff1aSopenharmony_ci for (; buf < buf_end - 3; buf += 4) 141cabdff1aSopenharmony_ci scratch ^= *((const uint32_t*)buf); 142cabdff1aSopenharmony_ci 143cabdff1aSopenharmony_ci scratch = xor_32_to_8(scratch); 144cabdff1aSopenharmony_ci 145cabdff1aSopenharmony_ci for (; buf < buf_end; buf++) 146cabdff1aSopenharmony_ci scratch ^= *buf; 147cabdff1aSopenharmony_ci 148cabdff1aSopenharmony_ci return scratch; 149cabdff1aSopenharmony_ci} 150