-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
431 lines (397 loc) · 17.3 KB
/
main.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <array>
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <locale>
#include <codecvt>
#include "flipnote_file.h"
#include "INIReader.h"
#include "lodepng.h"
#include "util.h"
#include "adpcm_encoder.h"
#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"
// this code sucks. this code sucks. this code sucks. this code sucks.
// this.
// code.
// sucks.
// but it sucks a bit less now :)
template <class T>
void write_whatever_ptr(FILE* file, T good_variable_name, size_t size) {
for (size_t i = 0; i < size; ++i) {
char temp = reinterpret_cast<char*>(good_variable_name)[i];
fputc(temp, file);
}
}
void write_zeros(FILE* file, size_t count) {
for (size_t i = 0; i < count; ++i) {
fputc(0, file);
}
}
class EncoderSettings {
public:
std::string data_dir;
std::array<uint8_t, 8> fsid;
std::array<uint8_t, 8> filename;
std::array<uint8_t, 8> partial_filename;
std::wstring author;
bool use_bgm;
uint32_t timestamp;
int frame_speed;
int bgm_frame_speed;
};
int encode(EncoderSettings settings) {
std::vector<std::vector<char>> frame_data;
std::vector<char> bgm;
unsigned int frame_count = 0;
if (settings.use_bgm) {
std::cout << "Loading BGM..." << std::endl;
std::string bgm_path = settings.data_dir + "/audio.wav";
unsigned int channels;
unsigned int sample_rate; // won't actually be used, but there's not much i can do about that
uint64_t pcm_frame_count;
int16_t* sample_data = drwav_open_file_and_read_pcm_frames_s16(bgm_path.c_str(), &channels, &sample_rate, &pcm_frame_count);
AdpcmEncoder encoder;
if (!sample_data)
throw "Could not parse audio.wav!";
if (channels != 1)
throw "WAV file needs to be mono!";
if (pcm_frame_count > UINT32_MAX)
throw "BGM is too big!";
for (uint64_t i = 0; i < pcm_frame_count; i += 2) {
bgm.push_back(encoder.EncodeSample(sample_data[i]) | encoder.EncodeSample(sample_data[i + 1]) << 4);
}
}
// load the thumbnail, expected size is 1536 or 0x600 bytes
std::cout << "Loading thumbnail..." << std::endl;
std::string thumbnail_path = settings.data_dir + "/thumbnail.bin";
FILE* thumbnail_file = fopen(thumbnail_path.c_str(), "rb");
if (!thumbnail_file)
throw "Failed to open thumbnail.bin!";
fseek(thumbnail_file, 0, SEEK_END);
if (ftell(thumbnail_file) != 0x600)
throw "thumbnail.bin isn't 0x600 bytes long!";
rewind(thumbnail_file);
std::cout << "Getting frame count..." << std::endl;
for (unsigned int frame = 0; ; ++frame) {
std::stringstream frame_filename;
frame_filename << settings.data_dir << "/frame_" << frame << ".png";
FILE* input = fopen(frame_filename.str().c_str(), "rb");
if (!input) {
break;
}
fclose(input);
++frame_count;
}
if (frame_count == 0)
throw "Couldn't find any frames! Did you remember a frame_0?";
std::cout << frame_count << " frames" << std::endl;
// time to actually start doing flipnote stuff
// create the animation data first
// only doing line type 0 (empty) and 1 (compressed bitmap)
// TODO: implement 2 and 3
std::cout << "Encoding frames..." << std::endl;
char previous_frame[256 * 192];
for (unsigned int frame = 0; frame < frame_count; ++frame) {
char temp_frame[256 * 192];
char frame_to_encode[256 * 192];
std::vector<char> encoded_frame;
unsigned char current_line_encoding_flags[192] = {}; // only layer 1 for now,
// layer 2 will be blank
unsigned char packed_line_encoding_flags[48] = {};
std::stringstream frame_filename;
frame_filename << settings.data_dir << "/frame_" << frame << ".png";
std::vector<unsigned char> decoded_frame;
unsigned int width, height;
unsigned int load_ret = lodepng::decode(decoded_frame, width, height, frame_filename.str());
if (load_ret)
throw "Failed to decode a frame!"; // TODO: make exceptions std::string so these can be better
if (width != 256 && height != 192)
throw "Frames need to be 256x192!";
// convert the frame to something simpler so it's easier to work with
for (int y = 0; y < 192; ++y) {
for (int x = 0; x < 256; ++x) {
unsigned char r_value = decoded_frame.at((x + y * 256) * 4);
if (r_value < 127) // input is expected to be greyscale, so it doesn't really matter what channel i check
temp_frame[y * 256 + x] = 1;
else
temp_frame[y * 256 + x] = 0;
}
}
if (frame == 0) { // first frame, don't do diffing
memcpy(frame_to_encode, temp_frame, 256 * 192);
memcpy(previous_frame, temp_frame, 256 * 192);
} else {
for (int i = 0; i < 256 * 192; ++i) {
frame_to_encode[i] = temp_frame[i] ^ previous_frame[i];
}
memcpy(previous_frame, temp_frame, 256 * 192);
}
// make the frame header
char frame_header = 0;
WRITETO(frame_header, 0, 1, 1); // white paper
WRITETO(frame_header, 1, 1, 1); // layer 1 will be inverse of paper color
WRITETO(frame_header, 4, 1, 1); // layer 2 will be red, not that it really matters
if (frame == 0)
WRITETO(frame_header, 7, 1, 1); // new frame
encoded_frame.push_back(frame_header);
int line_type;
int byte_index;
int bit_index;
// check if lines are blank or not, and write to line_encoding_flags
// accordingly
for (unsigned int line = 0; line < 192; ++line) {
line_type = 0; // empty
for (unsigned int pixel = 0; pixel < 256; ++pixel) {
if (frame_to_encode[line * 256 + pixel] == 1) {
line_type = 1; // compressed bitmap
break;
}
}
current_line_encoding_flags[line] = line_type;
}
// pack line_encoding_flags
// this could probably be merged into the above loop
for (int line = 0; line < 192; ++line) {
byte_index = line / 4; // 4 2-bit pixels per byte
bit_index = line % 4;
switch (current_line_encoding_flags[line]) {
case 0:
WRITETO(packed_line_encoding_flags[byte_index], bit_index * 2, 2, 0);
break;
case 1:
WRITETO(packed_line_encoding_flags[byte_index], bit_index * 2, 2, 1);
break;
case 2:
WRITETO(packed_line_encoding_flags[byte_index], bit_index * 2, 2, 2);
break;
case 3:
WRITETO(packed_line_encoding_flags[byte_index], bit_index * 2, 2, 3);
break;
default:
printf("wtf\n");
}
}
for (int i = 0; i < 48; ++i) {
encoded_frame.push_back(packed_line_encoding_flags[i]);
}
for (int i = 0; i < 48; ++i) {
encoded_frame.push_back(0); // layer 2 is blank
}
// time to encode the frame!
for (unsigned int line = 0; line < 192; ++line) {
switch (current_line_encoding_flags[line]) {
case 0: // empty
continue;
case 1: // compressed bitmap
std::vector<char> chunks;
char used_chunks[32] = {};
unsigned int chunk_flag = 0;
// first we need to figure out what chunks are used and write the chunk flag
for (unsigned int chunk = 0; chunk < 32; ++chunk) {
for (unsigned int pixel = 0; pixel < 8; ++pixel) {
if (frame_to_encode[line * 256 + (chunk * 8 + pixel)]) {
used_chunks[chunk] = 1;
WRITETO(chunk_flag, 31 - chunk, 1, 1);
break;
}
}
}
// chunk_flag needs to be endian-flipped because of the way i'm handling it (and this is expected to run on little-endian systems)
chunk_flag = Util::SwapEndian<unsigned int>(chunk_flag);
for (int i = 0; i < 4; ++i) {
encoded_frame.push_back(reinterpret_cast<char*>(&chunk_flag)[i]); // my code just gets worse and worse
}
// then we make the chunks
// could probably be merged with above loop
for (unsigned int chunk = 0; chunk < 32; ++chunk) {
if (!used_chunks[chunk])
continue;
char encoded_chunk = 0;
for (unsigned int pixel = 0; pixel < 8; ++pixel) {
if (frame_to_encode[line * 256 + (chunk * 8 + pixel)]) {
WRITETO(encoded_chunk, pixel, 1, 1);
}
}
encoded_frame.push_back(encoded_chunk);
}
}
}
frame_data.push_back(encoded_frame);
}
size_t anim_data_size = 0;
for (size_t frame = 0; frame < frame_data.size(); ++frame) {
anim_data_size += frame_data.at(frame).size();
}
if (anim_data_size > UINT32_MAX)
throw "Animation data is too big! (total frame data size exceeds 32-bit unsigned integer limit)";
std::vector<uint32_t> frame_offset_table;
size_t current_offset = 0;
for (unsigned int frame = 0; frame < frame_count; ++frame) {
frame_offset_table.push_back(current_offset);
current_offset += frame_data.at(frame).size();
if (current_offset > UINT32_MAX)
throw "Animation data is too big! (frame offset exceeds 32-bit unsigned integer limit)";
}
std::cout << "Writing PPM..." << std::endl;
dsiflipencode::FileHeader header;
header.magic = 0x41524150; // PARA endian-flipped
header.anim_data_size = anim_data_size + 8 + frame_count * 4;
// padding is so weird, i'd rather not get into it
int align_size = 4 - ((0x6A0 + header.anim_data_size + frame_count) % 4);
if (align_size != 4)
header.anim_data_size += align_size;
if (settings.use_bgm)
header.sound_data_size = bgm.size();
else
header.sound_data_size = 0;
header.frame_count = frame_count - 1;
header.version = 0x24;
header.locked = 1;
header.thumbnail_frame_index = 0;
memset(header.root_author_name, 0, 22);
memset(header.parent_author_name, 0, 22);
memset(header.current_author_name, 0, 22);
memcpy(header.root_author_name, settings.author.c_str(), settings.author.length() * 2 - 1); // we don't want the null terminator
memcpy(header.parent_author_name, settings.author.c_str(), settings.author.length() * 2 - 1);
memcpy(header.current_author_name, settings.author.c_str(), settings.author.length() * 2 - 1);
std::array<uint8_t, 8> reversed_fsid = settings.fsid;
std::reverse(reversed_fsid.begin(), reversed_fsid.end());
memcpy(header.parent_author_id, reversed_fsid.data(), 8);
memcpy(header.current_author_id, reversed_fsid.data(), 8);
memcpy(header.root_author_id, reversed_fsid.data(), 8);
std::stringstream filename_hex;
filename_hex << std::hex << std::setfill('0');
for (int i = 0; i < 8; ++i) {
filename_hex << std::uppercase << std::setw(2) << static_cast<unsigned>(settings.filename[i]);
}
// yes, the filenames really are internally stored in this way
memcpy(header.parent_filename, settings.fsid.data() + 5, 3);
memcpy(header.parent_filename + 3, filename_hex.str().c_str(), 13);
memcpy(header.current_filename, header.parent_filename, 18);
memcpy(header.partial_filename, settings.partial_filename.data(), 8);
header.timestamp = settings.timestamp;
header.unk2 = 0;
dsiflipencode::AnimationSectionHeader anim_header;
anim_header.frame_offset_table_size = frame_count * 4;
anim_header.unk1 = 0;
anim_header.flags = 0x410000; // no idea
dsiflipencode::SoundSectionHeader sound_header;
if (settings.use_bgm)
sound_header.bgm_size = bgm.size();
else
sound_header.bgm_size = 0;
sound_header.se1_size = 0;
sound_header.se2_size = 0;
sound_header.se3_size = 0;
sound_header.frame_speed = 8 - settings.frame_speed;
sound_header.bgm_frame_speed = 8 - settings.bgm_frame_speed;
sound_header.encoded = 1;
memset(sound_header.pad44, 0, 12);
// https://stackoverflow.com/questions/7639656/getting-a-buffer-into-a-stringstream-in-hex-representation/
std::stringstream ppm_filename;
ppm_filename << std::hex << std::setfill('0');
for (int i = 0; i < 3; ++i) {
ppm_filename << std::uppercase << std::setw(2) << static_cast<unsigned>(settings.fsid[i + 5]);
}
ppm_filename << "_";
for (int i = 0; i < 8; ++i) {
ppm_filename << std::uppercase << std::setw(2) << static_cast<unsigned>(settings.filename[i]);
}
ppm_filename << ".ppm";
FILE* ppm_file = fopen(ppm_filename.str().insert(20, "_").c_str() , "wb");
if (!ppm_file) {
throw "Could not open output file!";
}
// write ppm header
write_whatever_ptr(ppm_file, &header, sizeof(header));
// write thumbnail
for (int i = 0; i < 0x600; ++i) {
char byte;
fread(&byte, 1, 1, thumbnail_file);
fputc(byte, ppm_file);
}
// write animation section header
write_whatever_ptr(ppm_file, &anim_header, sizeof(anim_header));
// write frame offset table
for (size_t i = 0; i < frame_offset_table.size(); ++i) {
write_whatever_ptr(ppm_file, &frame_offset_table.at(i), 4);
}
// write frame data
for (size_t frame = 0; frame < frame_data.size(); ++frame) {
write_whatever_ptr(ppm_file, frame_data.at(frame).data(), frame_data.at(frame).size());
}
// write sound effect usage flags
write_zeros(ppm_file, frame_count);
// padding
if (align_size != 4)
write_zeros(ppm_file, align_size);
// write sound header
write_whatever_ptr(ppm_file, &sound_header, sizeof(sound_header));
// write bgm (no sound effects for now...)
if (settings.use_bgm)
write_whatever_ptr(ppm_file, bgm.data(), bgm.size());
// write dummy signature
write_zeros(ppm_file, 0x80);
// write padding
write_zeros(ppm_file, 0x10);
if (ftell(ppm_file) > 1024000)
std::cout << "The PPM file is over 1MB, so it might not load on a real DSi." << std::endl;
fclose(ppm_file);
return 0;
}
int main(int argc, char** argv) {
if (argc < 2) {
std::cout << "You need to provide a data directory!";
return 1;
}
const std::string data_dir(argv[1]);
INIReader reader(data_dir + "/config.ini");
EncoderSettings settings;
// https://stackoverflow.com/questions/7153935/how-to-convert-utf-8-stdstring-to-utf-16-stdwstring
std::wstringstream author;
std::string filename;
std::string fsid;
std::string partial_filename;
std::vector<uint8_t> filename_vector;
std::vector<uint8_t> fsid_vector;
std::vector<uint8_t> partial_filename_vector;
if (reader.ParseError() > 0) {
std::cout << "Failed to parse " << data_dir << "/config.ini!" << std::endl;
return 3;
}
author << reader.Get("", "author", "dsiflipenc").c_str();
filename = reader.Get("", "filename", "116AE34C2880B000");
fsid = reader.Get("", "fsid", "5473EB00A0BC70FA");
partial_filename = reader.Get("", "partial_filename", "BC70FA116AE34C28");
filename_vector = Util::HexStringToVector(filename, true);
fsid_vector = Util::HexStringToVector(fsid, true);
partial_filename_vector = Util::HexStringToVector(partial_filename, true);
// https://stackoverflow.com/questions/21276889/copy-stdvector-into-stdarray
std::copy_n(filename_vector.begin(), 8, settings.filename.begin());
std::copy_n(fsid_vector.begin(), 8, settings.fsid.begin());
std::copy_n(partial_filename_vector.begin(), 8, settings.partial_filename.begin());
settings.author = author.str();
settings.data_dir = data_dir;
settings.use_bgm = reader.GetBoolean("", "use_bgm", true);
settings.frame_speed = std::clamp(static_cast<int>(reader.GetInteger("", "frame_speed", 8)), 0, 8);
settings.bgm_frame_speed = std::clamp(static_cast<int>(reader.GetInteger("", "bgm_frame_speed", 8)), 0, 8);
uint64_t unix_timestamp = std::stoul(reader.Get("", "timestamp", "3133684800"));
if (unix_timestamp > UINT32_MAX + static_cast<uint64_t>(946684800)) {
std::cout << "Timestamp is too large!" << std::endl;
return 4;
}
settings.timestamp = unix_timestamp - 946684800;
try {
encode(settings);
return 0;
}
catch(const char* e) {
std::cout << "Encoder threw an exception: " << e << std::endl;
return 2;
}
}