mirror of
https://github.com/steve-m/hsdaoh-rp2350.git
synced 2025-12-09 23:34:39 +01:00
271 lines
10 KiB
C
271 lines
10 KiB
C
/*
|
|
* Implementation of HDMI data packet and info frame encoding
|
|
* (removed the audio frame encoding not needed by hsdaoh)
|
|
*
|
|
* Copyright (c) 2021-2022 by Shuichi Takano
|
|
* https://github.com/shuichitakano/pico_lib/blob/master/dvi/data_packet.cpp
|
|
*
|
|
* ported to C by Marcelo Lorenzati:
|
|
* https://github.com/mlorenzati/PicoDVI/blob/master/software/libdvi/data_packet.c
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
* a copy of this software and associated documentation files (the
|
|
* "Software"), to deal in the Software without restriction, including
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
* the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included
|
|
* in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
#include "data_packet.h"
|
|
#include <string.h>
|
|
|
|
// Compute 8 Parity Start
|
|
// Parity table is build statically with the following code
|
|
// for (int i = 0; i < 256; ++i){v_[i] = (i ^ (i >> 1) ^ (i >> 2) ^ (i >> 3) ^ (i >> 4) ^ (i >> 5) ^ (i >> 6) ^ (i >> 7)) & 1;}
|
|
const uint8_t parityTable[32] = { 0x96, 0x69, 0x69, 0x96, 0x69, 0x96, 0x96, 0x69,
|
|
0x69, 0x96, 0x96, 0x69, 0x96, 0x69, 0x69, 0x96,
|
|
0x69, 0x96, 0x96, 0x69, 0x96, 0x69, 0x69, 0x96,
|
|
0x96, 0x69, 0x69, 0x96, 0x69, 0x96, 0x96, 0x69 };
|
|
bool compute8(uint8_t index) {
|
|
return (parityTable[index / 8] >> (index % 8)) & 0x01;
|
|
}
|
|
bool compute8_2(uint8_t index1, uint8_t index2) {
|
|
return compute8(index1) ^ compute8(index2);
|
|
}
|
|
bool compute8_3(uint8_t index1, uint8_t index2, uint8_t index3) {
|
|
return compute8(index1) ^ compute8(index2) ^ compute8(index3);
|
|
}
|
|
// Compute 8 Parity End
|
|
|
|
// BCH Encoding Start
|
|
const uint8_t bchTable_[256] = {
|
|
0x00, 0xd9, 0xb5, 0x6c, 0x6d, 0xb4, 0xd8, 0x01,
|
|
0xda, 0x03, 0x6f, 0xb6, 0xb7, 0x6e, 0x02, 0xdb,
|
|
0xb3, 0x6a, 0x06, 0xdf, 0xde, 0x07, 0x6b, 0xb2,
|
|
0x69, 0xb0, 0xdc, 0x05, 0x04, 0xdd, 0xb1, 0x68,
|
|
0x61, 0xb8, 0xd4, 0x0d, 0x0c, 0xd5, 0xb9, 0x60,
|
|
0xbb, 0x62, 0x0e, 0xd7, 0xd6, 0x0f, 0x63, 0xba,
|
|
0xd2, 0x0b, 0x67, 0xbe, 0xbf, 0x66, 0x0a, 0xd3,
|
|
0x08, 0xd1, 0xbd, 0x64, 0x65, 0xbc, 0xd0, 0x09,
|
|
0xc2, 0x1b, 0x77, 0xae, 0xaf, 0x76, 0x1a, 0xc3,
|
|
0x18, 0xc1, 0xad, 0x74, 0x75, 0xac, 0xc0, 0x19,
|
|
0x71, 0xa8, 0xc4, 0x1d, 0x1c, 0xc5, 0x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};
|
|
|
|
int encode_BCH_3(const uint8_t *p) {
|
|
uint8_t v = bchTable_[p[0]];
|
|
v = bchTable_[p[1] ^ v];
|
|
v = bchTable_[p[2] ^ v];
|
|
return v;
|
|
}
|
|
|
|
int encode_BCH_7(const uint8_t *p) {
|
|
uint8_t v = bchTable_[p[0]];
|
|
v = bchTable_[p[1] ^ v];
|
|
v = bchTable_[p[2] ^ v];
|
|
v = bchTable_[p[3] ^ v];
|
|
v = bchTable_[p[4] ^ v];
|
|
v = bchTable_[p[5] ^ v];
|
|
v = bchTable_[p[6] ^ v];
|
|
return v;
|
|
}
|
|
// BCH Encoding End
|
|
|
|
// TERC4 Start
|
|
uint16_t TERC4Syms_[16] = {
|
|
0b1010011100,
|
|
0b1001100011,
|
|
0b1011100100,
|
|
0b1011100010,
|
|
0b0101110001,
|
|
0b0100011110,
|
|
0b0110001110,
|
|
0b0100111100,
|
|
0b1011001100,
|
|
0b0100111001,
|
|
0b0110011100,
|
|
0b1011000110,
|
|
0b1010001110,
|
|
0b1001110001,
|
|
0b0101100011,
|
|
0b1011000011,
|
|
};
|
|
|
|
uint32_t makeTERC4x2Char(int i) { return TERC4Syms_[i] | (TERC4Syms_[i] << 10); }
|
|
uint32_t makeTERC4x2Char_2(int i0, int i1) { return TERC4Syms_[i0] | (TERC4Syms_[i1] << 10); }
|
|
#define TERC4_0x2CharSym_ 0x000A729C // Build time generated -> makeTERC4x2Char(0);
|
|
#define dataGaurdbandSym_ 0x0004CD33 // Build time generated -> 0b0100110011'0100110011;
|
|
uint32_t defaultDataPacket12_[N_DATA_ISLAND_WORDS] = {
|
|
dataGaurdbandSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
TERC4_0x2CharSym_,
|
|
dataGaurdbandSym_,
|
|
};
|
|
|
|
// This table is built in compilation time from a function that uses makeTERC4x2Char
|
|
uint32_t defaultDataPackets0_[4][N_DATA_ISLAND_WORDS] = {
|
|
{ 0xa3a8e, 0xa729c, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xb32cc, 0xa3a8e},
|
|
{ 0x9c671, 0x98e63, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x4e539, 0x9c671},
|
|
{ 0x58d63, 0xb92e4, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x6719c, 0x58d63},
|
|
{ 0xb0ec3, 0xb8ae2, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb1ac6, 0xb0ec3}
|
|
};
|
|
|
|
uint32_t *getDefaultDataPacket0(bool vsync, bool hsync) {
|
|
return defaultDataPackets0_[(vsync << 1) | hsync];
|
|
}
|
|
|
|
// TERC4 End
|
|
|
|
void compute_header_parity(data_packet_t *data_packet) {
|
|
data_packet->header[3] = encode_BCH_3(data_packet->header);
|
|
}
|
|
|
|
void compute_subpacket_parity(data_packet_t *data_packet, int i) {
|
|
data_packet->subpacket[i][7] = encode_BCH_7(data_packet->subpacket[i]);
|
|
}
|
|
|
|
void compute_parity(data_packet_t *data_packet) {
|
|
compute_header_parity(data_packet);
|
|
compute_subpacket_parity(data_packet, 0);
|
|
compute_subpacket_parity(data_packet, 1);
|
|
compute_subpacket_parity(data_packet, 2);
|
|
compute_subpacket_parity(data_packet, 3);
|
|
}
|
|
|
|
void compute_info_frame_checkSum(data_packet_t *data_packet) {
|
|
int s = 0;
|
|
for (int i = 0; i < 3; ++i)
|
|
{
|
|
s += data_packet->header[i];
|
|
}
|
|
int n = data_packet->header[2] + 1;
|
|
for (int j = 0; j < 4; ++j)
|
|
{
|
|
for (int i = 0; i < 7 && n; ++i, --n)
|
|
{
|
|
s += data_packet->subpacket[j][i];
|
|
}
|
|
}
|
|
data_packet->subpacket[0][0] = -s;
|
|
}
|
|
|
|
void encode_header(const data_packet_t *data_packet, uint32_t *dst, int hv, bool firstPacket) {
|
|
int hv1 = hv | 8;
|
|
if (!firstPacket) {
|
|
hv = hv1;
|
|
}
|
|
for (int i = 0; i < 4; ++i) {
|
|
uint8_t h = data_packet->header[i];
|
|
dst[0] = makeTERC4x2Char_2(((h << 2) & 4) | hv, ((h << 1) & 4) | hv1);
|
|
dst[1] = makeTERC4x2Char_2((h & 4) | hv1, ((h >> 1) & 4) | hv1);
|
|
dst[2] = makeTERC4x2Char_2(((h >> 2) & 4) | hv1, ((h >> 3) & 4) | hv1);
|
|
dst[3] = makeTERC4x2Char_2(((h >> 4) & 4) | hv1, ((h >> 5) & 4) | hv1);
|
|
dst += 4;
|
|
hv = hv1;
|
|
}
|
|
}
|
|
|
|
void encode_subpacket(const data_packet_t *data_packet, uint32_t *dst1, uint32_t *dst2) {
|
|
for (int i = 0; i < 8; ++i) {
|
|
uint32_t v = (data_packet->subpacket[0][i] << 0) | (data_packet->subpacket[1][i] << 8) |
|
|
(data_packet->subpacket[2][i] << 16) | (data_packet->subpacket[3][i] << 24);
|
|
uint32_t t = (v ^ (v >> 7)) & 0x00aa00aa;
|
|
v = v ^ t ^ (t << 7);
|
|
t = (v ^ (v >> 14)) & 0x0000cccc;
|
|
v = v ^ t ^ (t << 14);
|
|
// 01234567 89abcdef ghijklmn opqrstuv
|
|
// 08go4cks 19hp5dlt 2aiq6emu 3bjr7fnv
|
|
dst1[0] = makeTERC4x2Char_2((v >> 0) & 15, (v >> 16) & 15);
|
|
dst1[1] = makeTERC4x2Char_2((v >> 4) & 15, (v >> 20) & 15);
|
|
dst2[0] = makeTERC4x2Char_2((v >> 8) & 15, (v >> 24) & 15);
|
|
dst2[1] = makeTERC4x2Char_2((v >> 12) & 15, (v >> 28) & 15);
|
|
dst1 += 2;
|
|
dst2 += 2;
|
|
}
|
|
}
|
|
|
|
void set_null(data_packet_t *data_packet) {
|
|
memset(data_packet, 0, sizeof(data_packet_t));
|
|
}
|
|
|
|
void set_AVI_info_frame(data_packet_t *data_packet, scan_info s, pixel_format y, colorimetry c, picture_aspect_ratio m,
|
|
active_format_aspect_ratio r, RGB_quantization_range q, video_code vic) {
|
|
set_null(data_packet);
|
|
data_packet->header[0] = 0x82;
|
|
data_packet->header[1] = 2; // version
|
|
data_packet->header[2] = 13; // len
|
|
|
|
int sc = 0;
|
|
// int sc = 3; // scaled hv
|
|
|
|
data_packet->subpacket[0][1] = (int)(s) | (r == ACTIVE_FORMAT_ASPECT_RATIO_NO_DATA ? 0 : 16) | ((int)(y) << 5);
|
|
data_packet->subpacket[0][2] = (int)(r) | ((int)(m) << 4) | ((int)(c) << 6);
|
|
data_packet->subpacket[0][3] = sc | ((int)(q) << 2);
|
|
data_packet->subpacket[0][4] = (int)(vic);
|
|
|
|
compute_info_frame_checkSum(data_packet);
|
|
compute_parity(data_packet);
|
|
}
|
|
|
|
void encode_data_island(data_island_stream_t *dst, const data_packet_t *packet, bool vsync, bool hsync) {
|
|
int hv = (vsync ? 2 : 0) | (hsync ? 1 : 0);
|
|
dst->data[0][0] = makeTERC4x2Char(0b1100 | hv);
|
|
dst->data[1][0] = dataGaurdbandSym_;
|
|
dst->data[2][0] = dataGaurdbandSym_;
|
|
|
|
encode_header(packet, &dst->data[0][1], hv, true);
|
|
encode_subpacket(packet, &dst->data[1][1], &dst->data[2][1]);
|
|
|
|
dst->data[0][N_DATA_ISLAND_WORDS - 1] = makeTERC4x2Char(0b1100 | hv);
|
|
dst->data[1][N_DATA_ISLAND_WORDS - 1] = dataGaurdbandSym_;
|
|
dst->data[2][N_DATA_ISLAND_WORDS - 1] = dataGaurdbandSym_;
|
|
}
|