forked from WerWolv/ImHex-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmk.hexpat
More file actions
218 lines (192 loc) · 5.48 KB
/
smk.hexpat
File metadata and controls
218 lines (192 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#pragma author xZise
#pragma description Smacker video file
#pragma endian little
#pragma magic [53 4D 4B 32] @ 0x0
import std.core;
import std.io;
import std.mem;
bitfield HeaderFlags {
ContainsRingFrame : 1;
YInterlaced : 1;
YDoubled : 1;
padding : 29;
};
bitfield HeaderAudioFlags {
isCompressed : 1;
hasAudio : 1;
is16BitAudio : 1;
isStereo : 1;
soundDecompression : 2;
padding : 2;
audioSampleRate : 24;
};
fn format_frame_rate(auto frame_rate) {
float fps;
if (frame_rate > 0) {
fps = 1000.0 / frame_rate;
} else if (frame_rate < 0) {
fps = 100000.0 / (-frame_rate);
} else {
fps = 10;
}
return std::format("{} fps", fps);
};
struct Header {
char Signature[4];
u32 Width;
u32 Height;
u32 FrameCount;
s32 FrameRate [[format("format_frame_rate")]];
HeaderFlags Flags;
u32 AudioSize[7];
u32 TreesSize;
u32 MMap_Size;
u32 MClr_Size;
u32 Full_Size;
u32 Type_Size;
HeaderAudioFlags AudioRate[7];
padding[4];
};
fn format_size(auto size) {
if (size.keyframe != 0) {
return std::format("[K] {} B", size.size);
}
return std::format("[ ] {} B", size.size);
};
bitfield Size {
keyframe : 1;
padding : 1;
dwordCount : 30;
u32 size = dwordCount * 4;
} [[format("format_size")]];
bitfield FrameType {
ContainsPaletteRecord : 1;
ContainsAudioTrack0 : 1;
ContainsAudioTrack1 : 1;
ContainsAudioTrack2 : 1;
ContainsAudioTrack3 : 1;
ContainsAudioTrack4 : 1;
ContainsAudioTrack5 : 1;
ContainsAudioTrack6 : 1;
};
fn format_palette_copy_only(auto copy_only) {
return std::format("copy: {0}", copy_only.copy);
};
bitfield PaletteCopyOnly {
copy: 7;
mode: 1;
} [[format("format_palette_copy_only")]];
fn format_palette_copy_skip(auto copy_skip) {
return std::format("copy: {0}, skip: {1}", copy_skip.copy, copy_skip.skip);
};
bitfield PaletteCopySkip {
copy: 6;
mode: 2;
skip: 8;
} [[format("format_palette_copy_skip")]];
bitfield PaletteColor {
blue: 6 [[color("0000FF")]];
mode: 2;
green: 6 [[color("00FF00")]];
padding: 2;
red: 6 [[color("FF0000")]];
padding: 2;
u8 r8 = red << 2 | red >> 4;
u8 g8 = green << 2 | green >> 4;
u8 b8 = blue << 2 | blue >> 4;
} [[hex::inline_visualize("color", r8, g8, b8, 0xff)]];
fn format_palette_chunk_block(auto entry) {
u8 first = entry.type;
if (first & 0x80 == 0x80) {
return format_palette_copy_only(entry.copy);
} else if (first & 0x40 == 0x40) {
return format_palette_copy_skip(entry.copy_skip);
} else {
return std::format("color: 0x{0:02x}{1:02x}{2:02x}", entry.color.r8, entry.color.g8, entry.color.b8);
}
};
enum PaletteChunkBlockType: u8 {
Color = 0x00 ... 0x3F,
CopySkip = 0x40 ... 0x7F,
CopyOnly = 0x80 ... 0xFF
};
// Unfortunately the match expression does not support ranges, so this is a helper for the following code:
// PaletteChunkBlockType type = std::mem::read_unsigned($, 1)
fn get_palette_chunk_block_type() {
u8 type = std::mem::read_unsigned($, 1);
if (type & 0x80 == 0x80) {
return PaletteChunkBlockType::CopyOnly;
} else if (type & 0x40 == 0x40) {
return PaletteChunkBlockType::CopySkip;
} else {
return PaletteChunkBlockType::Color;
}
};
fn get_last_position() {
PaletteChunkBlockType type = get_palette_chunk_block_type();
match (type) {
(PaletteChunkBlockType::CopyOnly): return $;
(PaletteChunkBlockType::CopySkip): return $ + 1;
(PaletteChunkBlockType::Color): return $ + 2;
}
};
struct PaletteChunkBlock {
PaletteChunkBlockType type = get_palette_chunk_block_type() [[export]];
match (type) {
(PaletteChunkBlockType::CopyOnly): PaletteCopyOnly copy;
(PaletteChunkBlockType::CopySkip): PaletteCopySkip copy_skip;
(PaletteChunkBlockType::Color): PaletteColor color;
}
} [[format("format_palette_chunk_block")]];
struct PaletteChunk {
u8 length;
u128 end = $ + length * 4 - 1;
PaletteChunkBlock blocks[while(get_last_position() < end)];
if ($ < end) {
padding[end - $];
}
};
struct AudioTrack<auto trackIndex> {
u32 length;
if (parent.parent.header.AudioRate[trackIndex].isCompressed) {
u32 decompressedSize;
}
u8 trackData[length - ($ - addressof(this))];
};
struct FramesData {
u32 frame_index = std::core::array_index();
u32 size = parent.sizes[frame_index].size;
if (parent.frameTypes[frame_index].ContainsPaletteRecord) {
PaletteChunk palette;
}
if (parent.frameTypes[frame_index].ContainsAudioTrack0) {
AudioTrack<0> audioTrack0;
}
if (parent.frameTypes[frame_index].ContainsAudioTrack1) {
AudioTrack<1> audioTrack1;
}
if (parent.frameTypes[frame_index].ContainsAudioTrack2) {
AudioTrack<2> audioTrack2;
}
if (parent.frameTypes[frame_index].ContainsAudioTrack3) {
AudioTrack<3> audioTrack3;
}
if (parent.frameTypes[frame_index].ContainsAudioTrack4) {
AudioTrack<4> audioTrack4;
}
if (parent.frameTypes[frame_index].ContainsAudioTrack5) {
AudioTrack<5> audioTrack5;
}
if (parent.frameTypes[frame_index].ContainsAudioTrack6) {
AudioTrack<6> audioTrack6;
}
u8 video[size - ($ - addressof(this))];
};
struct SMK {
Header header;
Size sizes[header.FrameCount];
FrameType frameTypes[header.FrameCount] ;
u8 trees[header.TreesSize];
FramesData frames[header.FrameCount];
};
SMK smk @ 0x00 [[inline]];