-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
208 lines (176 loc) · 7.98 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
/* SPDX-FileCopyrightText: Copyright 2024 Azamat H. Hackimov <azamat.hackimov@gmail.com> */
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <vector>
#include "CLI11.hpp"
#include "adpcm_codec.h"
#include "byteswap.h"
#include "manhuntribber_version.h"
typedef struct WAV_HEADER {
char RIFF[4] = {'R', 'I', 'F', 'F'}; // RIFF Header Magic header
uint32_t ChunkSize = 0; // RIFF Chunk Size
char WAVE[4] = {'W', 'A', 'V', 'E'}; // WAVE Header
char fmt[4] = {'f', 'm', 't', ' '}; // FMT header
uint32_t Subchunk1Size = UTILS::convert_le(16); // Size of the fmt chunk
uint16_t AudioFormat = UTILS::convert_le(1); // Audio format 1=PCM
uint16_t NumOfChan = UTILS::convert_le(2); // Number of channels 1=Mono 2=Stereo
uint32_t SamplesPerSec = UTILS::convert_le(44100); // Sampling Frequency in Hz
uint32_t bytesPerSec = UTILS::convert_le(176400); // bytes per second (SamplesPerSec * blockAlign)
uint16_t blockAlign = UTILS::convert_le(4); // 2=16-bit mono, 4=16-bit stereo
uint16_t bitsPerSample = UTILS::convert_le(16); // Number of bits per sample
char Subchunk2ID[4] = {'d', 'a', 't', 'a'}; // "data" string
uint32_t Subchunk2Size = 0; // Sampled data length
} wav_hdr;
// Some globals
int interleave = 0x10000;
int chunk_size = 0x400;
int nb_chunks_in_interleave = interleave / chunk_size;
int nb_chunk_encoded = chunk_size - 4;
int nb_chuck_decoded = 2 * nb_chunk_encoded + 1;
int nb_channels = 2; // Mono/Stereo
void decode(const std::filesystem::path &in_file, std::filesystem::path out_file, bool is_mono) {
if (out_file.empty()) {
(out_file = in_file).replace_extension("wav");
}
std::ifstream input_file(in_file, std::ios::binary);
std::ofstream output_file(out_file, std::ios::binary);
if (is_mono) {
chunk_size = 0x200;
nb_channels = 1;
nb_chunks_in_interleave = interleave / chunk_size;
nb_chunk_encoded = chunk_size - 4;
nb_chuck_decoded = 2 * nb_chunk_encoded + 1;
}
if (!input_file.is_open()) {
std::cout << std::format("Can't open input file for reading {}", in_file.string()) << std::endl;
exit(1);
}
if (!output_file.is_open()) {
std::cout << std::format("Can't open output file for writing {}", out_file.string()) << std::endl;
exit(1);
}
std::cout << std::format("Decoding {} to {}... ", in_file.string(), out_file.string());
input_file.seekg(0, std::ios::end);
size_t input_size = input_file.tellg();
input_file.seekg(0, std::ios::beg);
size_t nb_samples = input_size / (nb_channels * interleave);
int nb_chunks = interleave / chunk_size;
wav_hdr wave_header;
output_file.write(reinterpret_cast<char *>(&wave_header), sizeof(wav_hdr));
for (int i = 0; i < nb_samples; i++) {
std::vector<std::shared_ptr<std::vector<int16_t>>> outputs(nb_channels);
for (auto &out : outputs) {
out = std::make_shared<std::vector<int16_t>>();
}
for (int ch = 0; ch < nb_channels; ch++) {
for (int j = 0; j < nb_chunks; j++) {
std::vector<int8_t> input_buffer(chunk_size);
input_file.read(reinterpret_cast<char *>(input_buffer.data()), chunk_size);
adpcm_rib_decode_frame(std::make_shared<std::vector<int8_t>>(input_buffer), outputs[ch]);
}
}
for (int j = 0; j < nb_chuck_decoded * nb_chunks; j++) {
for (int ch = 0; ch < nb_channels; ch++) {
int16_t r = UTILS::convert_le(outputs[ch]->at(j));
output_file.write(reinterpret_cast<char *>(&r), 2);
}
}
}
size_t size = output_file.tellp();
// Rewrite wave header with actual sizes
wave_header.ChunkSize = UTILS::convert_le(size - 8);
wave_header.Subchunk2Size = UTILS::convert_le(size - 44);
wave_header.NumOfChan = UTILS::convert_le(nb_channels);
if (is_mono) {
wave_header.SamplesPerSec = UTILS::convert_le(22100);
wave_header.bytesPerSec = UTILS::convert_le(88200);
}
output_file.seekp(0, std::ios::beg);
output_file.write(reinterpret_cast<char *>(&wave_header), sizeof(wav_hdr));
input_file.close();
output_file.close();
std::cout << "done!" << std::endl;
}
void encode(const std::filesystem::path &in_file, std::filesystem::path out_file) {
if (out_file.empty()) {
(out_file = in_file).replace_extension("rib");
}
std::ifstream input_file(in_file, std::ios::binary);
std::ofstream output_file(out_file, std::ios::binary);
if (!input_file.is_open()) {
std::cout << std::format("Can't open input file for reading {}", in_file.string()) << std::endl;
exit(1);
}
if (!output_file.is_open()) {
std::cout << std::format("Can't open output file for writing {}", out_file.string()) << std::endl;
exit(1);
}
std::cout << std::format("Encoding {} to {}... ", in_file.string(), out_file.string());
std::vector<std::vector<int16_t>> inputs(nb_channels);
std::vector<std::shared_ptr<std::vector<int8_t>>> outputs(nb_channels);
for (auto &out : outputs) {
out = std::make_shared<std::vector<int8_t>>();
}
wav_hdr wave_header;
input_file.read(reinterpret_cast<char *>(&wave_header), sizeof(wav_hdr));
nb_channels = UTILS::convert_le(wave_header.NumOfChan);
if (nb_channels == 1) {
chunk_size = 0x200;
nb_chunks_in_interleave = interleave / chunk_size;
nb_chunk_encoded = chunk_size - 4;
nb_chuck_decoded = 2 * nb_chunk_encoded + 1;
}
while (!input_file.eof()) {
for (int ch = 0; ch < nb_channels; ch++) {
int16_t r;
input_file.read(reinterpret_cast<char *>(&r), 2);
inputs[ch].push_back(UTILS::convert_le(r));
}
}
input_file.close();
int nb_interleaves_per_ch = std::floor(inputs[0].size() / (2 * nb_chunk_encoded * nb_chunks_in_interleave));
for (int ch = 0; ch < nb_channels; ch++) {
// Reserve additional size in inputs
inputs[ch].reserve(nb_interleaves_per_ch * 2 * nb_chunk_encoded * nb_chunks_in_interleave);
auto channel_status = std::make_shared<ADPCMChannelStatus>();
for (int sample = 0; sample < nb_interleaves_per_ch * nb_chunks_in_interleave; sample++) {
std::vector<int16_t> input_frame(inputs[ch].begin() + sample * nb_chuck_decoded,
inputs[ch].begin() + (sample + 1) * nb_chuck_decoded);
adpcm_rib_encode_frame(channel_status, std::make_shared<std::vector<int16_t>>(input_frame), outputs[ch]);
}
}
for (int i = 0; i < nb_interleaves_per_ch * interleave; i += interleave) {
for (int ch = 0; ch < nb_channels; ch++) {
std::vector<int8_t> buffer(outputs[ch]->begin() + i, outputs[ch]->begin() + (i + interleave));
output_file.write(reinterpret_cast<char *>(buffer.data()), interleave);
}
}
output_file.close();
std::cout << "done!" << std::endl;
}
int main(int argc, char *argv[]) {
std::filesystem::path in_file;
std::filesystem::path out_file;
bool is_mono = false;
CLI::App app{"ManhuntRIBber - encode/decode RIB files from Rockstar's Manhunt PC game"};
app.set_version_flag("-v", MANHUNTRIBBER_VERSION);
argv = app.ensure_utf8(argv);
std::cout << std::format("ManhuntRIBber {} https://github.com/winterheart/ManhuntRIBber\n"
"(c) 2024 Azamat H. Hackimov <azamat.hackimov@gmail.com>\n",
app.version())
<< std::endl;
auto encode_cmd =
app.add_subcommand("encode", "Encode WAV file to RIB")->callback([&]() { encode(in_file, out_file); });
encode_cmd->add_option("input", in_file, "Input RIB file")->required()->check(CLI::ExistingFile);
encode_cmd->add_option("output", out_file, "Output WAV file");
auto decode_cmd =
app.add_subcommand("decode", "Decode RIB file to WAV")->callback([&]() { decode(in_file, out_file, is_mono); });
decode_cmd->add_flag("-m", is_mono, "Threats input file as Mono stream");
decode_cmd->add_option("input", in_file, "Input WAV file")->required()->check(CLI::ExistingFile);
decode_cmd->add_option("output", out_file, "Output RIB file");
CLI11_PARSE(app, argc, argv);
return 0;
}