-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvid2h.cpp
More file actions
767 lines (757 loc) · 36.2 KB
/
vid2h.cpp
File metadata and controls
767 lines (757 loc) · 36.2 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
#include "audio/audiohelpers.h"
#include "audio/audioprocessing.h"
#include "audio/resampler.h"
#include "audio/samplebuffer.h"
#include "audio/wavwriter.h"
#include "color/colorhelpers.h"
#include "color/conversions.h"
#include "compression/lzss.h"
#include "image/imagehelpers.h"
#include "image/imageprocessing.h"
#include "image/spritehelpers.h"
#include "io/ffmpegreader.h"
#include "io/textio.h"
#include "io/vid2hio.h"
#include "subtitles/srtio.h"
#include "processing/datahelpers.h"
#include "processing/processingoptions.h"
#include "statistics/statisticswindow.h"
#include "statistics/statisticswriter.h"
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include "cxxopts/include/cxxopts.hpp"
std::string m_inFile;
std::string m_outFile;
ProcessingOptions options;
std::string getCommandLine(int argc, const char *argv[])
{
std::string result;
for (int i = 1; i < argc; i++)
{
result += std::string(argv[i]);
if (i < (argc - 1))
{
result += " ";
}
}
return result;
}
bool readArguments(int argc, const char *argv[])
{
try
{
cxxopts::Options opts("vid2h", "Convert and compress a video file to .h / .c files or a binary file");
opts.add_option("", {"h,help", "Print help"});
opts.add_option("", options.audio.cxxOption);
opts.add_option("", options.video.cxxOption);
opts.add_option("", options.blackWhite.cxxOption);
opts.add_option("", options.paletted.cxxOption);
opts.add_option("", options.truecolor.cxxOption);
opts.add_option("", options.outformat.cxxOption);
opts.add_option("", options.addColor0.cxxOption);
opts.add_option("", options.moveColor0.cxxOption);
opts.add_option("", options.shiftIndices.cxxOption);
opts.add_option("", options.pruneIndices.cxxOption);
opts.add_option("", options.sprites.cxxOption);
opts.add_option("", options.tiles.cxxOption);
opts.add_option("", options.deltaImage.cxxOption);
opts.add_option("", options.delta8.cxxOption);
opts.add_option("", options.delta16.cxxOption);
opts.add_option("", options.dxt.cxxOption);
opts.add_option("", options.dxtv.cxxOption);
// opts.add_option("", options.gvid.cxxOption);
// opts.add_option("", options.rle.cxxOption);
opts.add_option("", options.lz4.cxxOption);
opts.add_option("", options.lz10.cxxOption);
opts.add_option("", options.vram.cxxOption);
opts.add_option("", options.channelFormat.cxxOption);
opts.add_option("", options.sampleFormat.cxxOption);
opts.add_option("", options.sampleRateHz.cxxOption);
opts.add_option("", options.adpcm.cxxOption);
opts.add_option("", options.subtitlesFile.cxxOption);
opts.add_option("", options.metaFile.cxxOption);
opts.add_option("", options.metaString.cxxOption);
opts.add_option("", options.printStats.cxxOption);
opts.add_option("", options.dryRun.cxxOption);
opts.add_option("", options.outputStats.cxxOption);
opts.add_option("", {"infile", "Input video file to convert, e.g. \"foo.avi\"", cxxopts::value<std::string>()});
opts.add_option("", {"outname", "Output file and variable name, e.g \"foo\". This will name the output files \"foo.h\" and \"foo.c\" and variable names will start with \"FOO_\"", cxxopts::value<std::string>()});
opts.parse_positional({"infile", "outname"});
auto result = opts.parse(argc, argv);
// check if help was requested
if (result.count("h"))
{
return false;
}
// get output file / name
if (result.count("outname"))
{
m_outFile = result["outname"].as<std::string>();
}
// get input file
if (result.count("infile"))
{
m_inFile = result["infile"].as<std::string>();
if (!std::filesystem::exists(m_inFile))
{
std::cout << "Input file \"" << m_inFile << "\" does not exist!" << std::endl;
return false;
}
}
// if input or output are empty, get positional arguments as input first, then output
if (result.count("positional"))
{
auto positional = result["positional"].as<std::vector<std::string>>();
auto pIt = positional.cbegin();
while (pIt != positional.cend() && (m_inFile.empty() || m_outFile.empty()))
{
if (m_inFile.empty())
{
m_inFile = *pIt++;
}
if (m_outFile.empty())
{
m_outFile = *pIt++;
}
}
}
// check if exclusive options set
if (!options.audio && !options.video)
{
std::cerr << "Can only exclude audio OR video from output" << std::endl;
return false;
}
options.subtitlesFile.parse(result);
options.metaFile.parse(result);
options.metaString.parse(result);
if (options.metaFile && options.metaString)
{
std::cerr << "Can only add a file OR string meta data to output" << std::endl;
return false;
}
options.blackWhite.parse(result);
options.paletted.parse(result);
options.truecolor.parse(result);
if ((options.blackWhite + options.paletted + options.truecolor) == 0)
{
std::cerr << "One format option is needed." << std::endl;
return false;
}
if ((options.blackWhite + options.paletted + options.truecolor) > 1)
{
std::cerr << "Only a single format option is allowed." << std::endl;
return false;
}
options.outformat.parse(result);
if (!options.outformat)
{
std::cerr << "Output color format must be set." << std::endl;
return false;
}
options.quantizationmethod.parse(result);
options.addColor0.parse(result);
options.moveColor0.parse(result);
options.shiftIndices.parse(result);
options.pruneIndices.parse(result);
options.sprites.parse(result);
options.dxtv.parse(result);
options.channelFormat.parse(result);
options.sampleFormat.parse(result);
options.sampleRateHz.parse(result);
}
catch (const cxxopts::exceptions::parsing &e)
{
std::cerr << "Argument error: " << e.what() << std::endl;
return false;
}
return true;
}
void printUsage()
{
// 80 chars: --------------------------------------------------------------------------------
std::cout << "Convert and compress a video file to .h / .c files or a binary file" << std::endl;
std::cout << "Usage: vid2h IMG [IMG_CONV] [IMG_COMP] [COMP] AUD [AUD_COMP] INFILE OUTNAME" << std::endl;
std::cout << "General options (mutually exclusive):" << std::endl;
std::cout << options.video.helpString() << std::endl;
std::cout << options.audio.helpString() << std::endl;
std::cout << "Image format options (mutually exclusive):" << std::endl;
std::cout << options.blackWhite.helpString() << std::endl;
std::cout << options.paletted.helpString() << std::endl;
std::cout << options.truecolor.helpString() << std::endl;
std::cout << "Output color format (must be set):" << std::endl;
std::cout << options.outformat.helpString() << std::endl;
std::cout << "Image conversion options (all optional):" << std::endl;
std::cout << options.quantizationmethod.helpString() << std::endl;
std::cout << options.addColor0.helpString() << std::endl;
std::cout << options.moveColor0.helpString() << std::endl;
std::cout << options.shiftIndices.helpString() << std::endl;
std::cout << options.pruneIndices.helpString() << std::endl;
std::cout << options.tiles.helpString() << std::endl;
std::cout << options.sprites.helpString() << std::endl;
std::cout << options.deltaImage.helpString() << std::endl;
std::cout << options.delta8.helpString() << std::endl;
std::cout << options.delta16.helpString() << std::endl;
std::cout << "Image compression options (mutually exclusive):" << std::endl;
std::cout << options.dxt.helpString() << std::endl;
std::cout << options.dxtv.helpString() << std::endl;
// std::cout << options.gvid.helpString() << std::endl;
std::cout << "Compression options (mutually exclusive):" << std::endl;
// std::cout << options.rle.helpString() << std::endl;
std::cout << options.lz4.helpString() << std::endl;
std::cout << options.lz10.helpString() << std::endl;
std::cout << "Compression modifiers (optional):" << std::endl;
std::cout << options.vram.helpString() << std::endl;
std::cout << "Output audio format (all optional):" << std::endl;
std::cout << options.channelFormat.helpString() << std::endl;
std::cout << options.sampleFormat.helpString() << std::endl;
std::cout << options.sampleRateHz.helpString() << std::endl;
std::cout << "Audio compression options (all optional):" << std::endl;
std::cout << options.adpcm.helpString() << std::endl;
std::cout << "Subtitles options:" << std::endl;
std::cout << options.subtitlesFile.helpString() << std::endl;
std::cout << "Meta data options (mutually exclusive):" << std::endl;
std::cout << options.metaFile.helpString() << std::endl;
std::cout << options.metaString.helpString() << std::endl;
std::cout << "INFILE: Input video file to convert, e.g. \"foo.avi\"" << std::endl;
std::cout << "OUTNAME: is determined from the first non-existant file path. It can be an " << std::endl;
std::cout << "absolute or relative file path or a file base name. Two files OUTNAME.h and " << std::endl;
std::cout << "OUTNAME.c will be generated. All variables will begin with the base name " << std::endl;
std::cout << "portion of OUTNAME." << std::endl;
std::cout << "Misc options (all optional):" << std::endl;
std::cout << options.printStats.helpString() << std::endl;
std::cout << options.dryRun.helpString() << std::endl;
std::cout << options.outputStats.helpString() << std::endl;
std::cout << "h / help: Show this help." << std::endl;
std::cout << "Image order: input, color conversion, addcolor0, movecolor0, shift, sprites, " << std::endl;
std::cout << "tiles, deltaimage, dxtg / dtxv, delta8 / delta16, lz4 / lz10, output" << std::endl;
std::cout << "Note: Multi-channel audio will be converted to stereo and sample bit depth will " << std::endl;
std::cout << "be converted to 16 bit" << std::endl;
}
auto buildImageProcessing(const ProcessingOptions &opts) -> Image::Processing
{
Image::Processing videoProcessing;
if (opts.blackWhite)
{
videoProcessing.addStep(Image::ProcessingType::ConvertBlackWhite, {opts.quantizationmethod.value, opts.blackWhite.value});
}
else if (opts.paletted)
{
// add palette conversion using a RGB555 or RGB565 reference color map
std::vector<Color::XRGB8888> colorSpaceMap;
switch (opts.outformat.value)
{
case Color::Format::XBGR1555:
colorSpaceMap = ColorHelpers::buildColorMapFor(Color::Format::XRGB1555);
break;
case Color::Format::BGR565:
colorSpaceMap = ColorHelpers::buildColorMapFor(Color::Format::RGB565);
break;
default:
colorSpaceMap = ColorHelpers::buildColorMapFor(opts.outformat.value);
}
videoProcessing.addStep(Image::ProcessingType::ConvertPaletted, {opts.quantizationmethod.value, opts.paletted.value, colorSpaceMap});
}
else if (opts.commonPalette)
{
// add common palette conversion using a RGB555 or RGB565 reference color map
std::vector<Color::XRGB8888> colorSpaceMap;
switch (opts.outformat.value)
{
case Color::Format::XBGR1555:
colorSpaceMap = ColorHelpers::buildColorMapFor(Color::Format::XRGB1555);
break;
case Color::Format::BGR565:
colorSpaceMap = ColorHelpers::buildColorMapFor(Color::Format::RGB565);
break;
default:
colorSpaceMap = ColorHelpers::buildColorMapFor(opts.outformat.value);
}
videoProcessing.addStep(Image::ProcessingType::ConvertCommonPalette, {opts.quantizationmethod.value, opts.commonPalette.value, colorSpaceMap});
}
else if (opts.truecolor)
{
videoProcessing.addStep(Image::ProcessingType::ConvertTruecolor, {opts.truecolor.value});
}
// build image processing pipeline - conversion
if (opts.paletted)
{
videoProcessing.addStep(Image::ProcessingType::ReorderColors, {});
if (opts.addColor0)
{
videoProcessing.addStep(Image::ProcessingType::AddColor0, {opts.addColor0.value});
}
if (opts.moveColor0)
{
videoProcessing.addStep(Image::ProcessingType::MoveColor0, {opts.moveColor0.value});
}
if (opts.shiftIndices)
{
videoProcessing.addStep(Image::ProcessingType::ShiftIndices, {opts.shiftIndices.value});
}
if (opts.pruneIndices)
{
videoProcessing.addStep(Image::ProcessingType::PruneIndices, {});
// TODO store 1, 2, 4 bits
videoProcessing.addStep(Image::ProcessingType::PadColorMap, {uint32_t(16)});
}
else
{
videoProcessing.addStep(Image::ProcessingType::PadColorMap, {opts.paletted.value + (opts.addColor0 ? 1 : 0)});
}
videoProcessing.addStep(Image::ProcessingType::ConvertColorMapToRaw, {opts.outformat.value});
videoProcessing.addStep(Image::ProcessingType::PadColorMapData, {uint32_t(4)});
}
if (opts.sprites)
{
videoProcessing.addStep(Image::ProcessingType::ConvertSprites, {opts.sprites.value.front()});
}
if (opts.tiles)
{
videoProcessing.addStep(Image::ProcessingType::ConvertTiles, {});
}
// build image processing pipeline - image compression
if (opts.deltaImage)
{
videoProcessing.addStep(Image::ProcessingType::DeltaImage, {});
}
if (opts.dxt)
{
videoProcessing.addStep(Image::ProcessingType::CompressDXT, {opts.outformat.value}, true, opts.printStats);
}
if (opts.dxtv)
{
videoProcessing.addStep(Image::ProcessingType::CompressDXTV, {opts.outformat.value, opts.dxtv.value}, true, opts.printStats);
}
/*if (options.gvid)
{
processing.addStep(Image::ProcessingType::CompressGVID, {}, true, true);
}*/
// convert to raw data (only if not raw data already)
videoProcessing.addStep(Image::ProcessingType::ConvertPixelsToRaw, {opts.outformat.value});
// build image processing pipeline - entropy compression
if (opts.delta8)
{
videoProcessing.addStep(Image::ProcessingType::ConvertDelta8, {});
}
if (opts.delta16)
{
videoProcessing.addStep(Image::ProcessingType::ConvertDelta16, {});
}
/*if (options.rle)
{
processing.addStep(Image::ProcessingType::CompressRLE, {options.vram.isSet}, true);
}*/
if (opts.lz4)
{
videoProcessing.addStep(Image::ProcessingType::CompressLZ4_40, {opts.vram.isSet}, true, opts.printStats);
}
if (opts.lz10)
{
videoProcessing.addStep(Image::ProcessingType::CompressLZSS_10, {opts.vram.isSet}, true, opts.printStats);
}
if (opts.rans)
{
videoProcessing.addStep(Image::ProcessingType::CompressRANS_50, {}, true, opts.printStats);
}
videoProcessing.addStep(Image::ProcessingType::PadPixelData, {uint32_t(4)});
return videoProcessing;
}
auto buildAudioProcessing(const ProcessingOptions &opts, const Media::Reader::MediaInfo &mediaInfo) -> Audio::Processing
{
Audio::Processing audioProcessing;
const auto audioOutSampleRateHz = opts.sampleRateHz ? opts.sampleRateHz.value : mediaInfo.audioSampleRateHz;
if (opts.channelFormat || opts.sampleRateHz || opts.sampleFormat)
{
const auto audioOutChannelFormat = opts.channelFormat ? opts.channelFormat.value : mediaInfo.audioChannelFormat;
const auto audioOutSampleFormat = opts.sampleFormat ? opts.sampleFormat.value : mediaInfo.audioSampleFormat;
audioProcessing.addStep(Audio::ProcessingType::Resample, {audioOutChannelFormat, audioOutSampleRateHz, audioOutSampleFormat});
}
// ----- build audio processing pipeline - frame sample packaging
// We need to provide enough samples for one frame of video at the video frame rate
// We also need to make sure audio frames size requirements are met:
// * Multiple of 16 int8_t samples per channel for GBA audio playback (sound DMA always copies 16 * int8_t when buffer drains)
// * Multiple of 4 bytes per channel for NDS audio playback
// This can result in the buffer size varying between frames, otherwise the buffer would grow during playback
const double audioOutSamplesPerFrame = audioOutSampleRateHz / mediaInfo.videoFrameRateHz; // Number of audio samples per channel needed per frame of video
audioProcessing.addStep(Audio::ProcessingType::Repackage, {audioOutSamplesPerFrame, uint32_t(16)});
// build audio processing pipeline - audio compression
if (opts.adpcm)
{
audioProcessing.addStep(Audio::ProcessingType::CompressADPCM, {}, true);
}
if (opts.lz4)
{
audioProcessing.addStep(Audio::ProcessingType::CompressLZ4_40, {opts.vram.isSet}, true, opts.printStats);
}
return audioProcessing;
}
auto buildMetaData(const ProcessingOptions &opts) -> std::vector<uint8_t>
{
std::vector<uint8_t> metaData;
if (options.metaFile)
{
// open input file and read content
const auto metaDataFileName = options.metaFile.value;
auto metaDataStream = std::ifstream(metaDataFileName, std::ios::in | std::ios::binary);
REQUIRE(metaDataStream.is_open(), std::runtime_error, "Failed to open meta data file " << metaDataFileName << " for reading");
std::cout << "Reading meta data from " << metaDataFileName << std::endl;
metaDataStream.seekg(0, std::ios_base::end);
REQUIRE(!metaDataStream.fail(), std::runtime_error, "Failed to seek to end of meta data file");
const auto fileSize = metaDataStream.tellg();
REQUIRE(fileSize > 0, std::runtime_error, "Meta data file size must be > 0");
REQUIRE(fileSize < 65536, std::runtime_error, "Meta data file size must be < 65536");
metaDataStream.seekg(0, std::ios_base::beg);
REQUIRE(!metaDataStream.fail(), std::runtime_error, "Failed to seek to start of meta data file");
metaData.resize(fileSize);
metaDataStream.read(reinterpret_cast<char *>(metaData.data()), fileSize);
REQUIRE(!metaDataStream.fail(), std::runtime_error, "Failed to read from meta data file");
metaDataStream.close();
}
else if (options.metaString)
{
// convert string to binary
REQUIRE(options.metaString.value.size() > 0, std::runtime_error, "Meta data string size must be > 0");
REQUIRE(options.metaString.value.size() < 65536, std::runtime_error, "Meta data string size must be < 65536");
metaData.resize(options.metaString.value.size());
std::copy(options.metaString.value.cbegin(), options.metaString.value.cend(), metaData.begin());
}
return metaData;
}
int main(int argc, const char *argv[])
{
try
{
// check arguments
if (argc < 3 || !readArguments(argc, argv))
{
printUsage();
return 2;
}
// check input and output
if (m_inFile.empty())
{
std::cerr << "No input file passed. Aborting." << std::endl;
return 1;
}
if (m_outFile.empty())
{
std::cerr << "No output name passed. Aborting." << std::endl;
return 1;
}
// set up number of cores for parallel processing
const auto nrOfProcessors = omp_get_num_procs();
omp_set_num_threads(nrOfProcessors);
// fire up video reader and open video file
Media::FFmpegReader mediaReader;
Media::Reader::MediaInfo mediaInfo;
bool sourceHasVideo = false;
bool sourceHasAudio = false;
try
{
std::cout << "Opening " << m_inFile << "..." << std::endl;
mediaReader.open(m_inFile);
mediaInfo = mediaReader.getInfo();
sourceHasVideo = mediaInfo.fileType & IO::FileType::Video;
sourceHasAudio = mediaInfo.fileType & IO::FileType::Audio;
if (sourceHasVideo)
{
std::cout << "Video stream #" << mediaInfo.videoStreamIndex << ": " << mediaInfo.videoCodecName << ", " << mediaInfo.videoWidth << "x" << mediaInfo.videoHeight << "@" << mediaInfo.videoFrameRateHz;
std::cout << ", duration " << mediaInfo.videoDurationS << "s, " << mediaInfo.videoNrOfFrames << " frames" << std::endl;
}
if (sourceHasAudio)
{
std::cout << "Audio stream #" << mediaInfo.audioStreamIndex << ": " << mediaInfo.audioCodecName << ", " << Audio::formatInfo(mediaInfo.audioChannelFormat).description << ", " << mediaInfo.audioSampleRateHz << " Hz, ";
std::cout << Audio::formatInfo(mediaInfo.audioSampleFormat).description;
std::cout << ", duration " << mediaInfo.audioDurationS << "s, " << mediaInfo.audioNrOfFrames << " frames, " << mediaInfo.audioNrOfSamples << " samples, offset " << mediaInfo.audioOffsetS << "s" << std::endl;
}
}
catch (const std::runtime_error &e)
{
std::cerr << "Failed to open video file: " << e.what() << std::endl;
return 1;
}
// check if processing makes sense
if (sourceHasVideo && !options.video && !sourceHasAudio)
{
std::cerr << "Chose not to output video, but source has no audio. Output would be empty. Exiting..." << std::endl;
return 1;
}
if (sourceHasAudio && !options.audio && !sourceHasVideo)
{
std::cerr << "Chose not to output audio, but source has no video. Output would be empty. Exiting..." << std::endl;
return 1;
}
// ----- build image processing pipeline - input -----
Image::Processing videoProcessing;
if (options.video)
{
videoProcessing = buildImageProcessing(options);
// print image processing pipeline configuration
const auto imageProcessingDesc = videoProcessing.getProcessingDescription();
std::cout << "Applying image processing: " << imageProcessingDesc << std::endl;
}
else
{
std::cout << "Ignoring video. Won't add video to output" << std::endl;
}
// ----- build audio processing pipeline - input -----
Audio::Processing audioProcessing;
if (options.audio)
{
audioProcessing = buildAudioProcessing(options, mediaInfo);
// print audio processing pipeline configuration
const auto audioProcessingDesc = audioProcessing.getProcessingDescription();
std::cout << "Applying audio processing: " << audioProcessingDesc << std::endl;
}
else
{
std::cout << "Ignoring audio. Won't add audio to output" << std::endl;
}
// ----- read subtitles -----
bool outputHasSubtitles = false;
std::vector<Subtitles::Frame> subtitles;
if (options.subtitlesFile)
{
std::cout << "Reading subtitles from: " << options.subtitlesFile.value << std::endl;
subtitles = IO::SRT::readSRT(options.subtitlesFile.value);
std::cout << "Found " << subtitles.size() << " subtitle entries" << std::endl;
// check length of subtitles
for (std::size_t si = 0; si < subtitles.size(); ++si)
{
const auto &subtitle = subtitles[si];
if (subtitle.text.size() > Subtitles::MaxSubTitleLength)
{
std::cout << "Warning: Subtitle #" << subtitle.index << " exceeds max. subtitle length of " << Subtitles::MaxSubTitleLength << std::endl;
}
}
outputHasSubtitles = true;
}
// ----- get meta data -----
const auto metaData = buildMetaData(options);
if ((options.metaFile || options.metaString) && !metaData.empty())
{
std::cout << "Adding " << metaData.size() << " Bytes of meta data to output" << std::endl;
}
// check if we want to write output files
const bool outputHasVideo = (mediaInfo.fileType & IO::FileType::Video) && options.video;
const bool outputHasAudio = (mediaInfo.fileType & IO::FileType::Audio) && options.audio;
IO::Vid2h::FileDataInfo fileDataInfo;
std::ofstream binFile;
if (!options.dryRun)
{
// open output file and write initial file header
binFile = std::ofstream(m_outFile + ".bin", std::ios::out | std::ios::binary);
if (binFile.is_open())
{
std::cout << "Writing output file " << m_outFile << ".bin" << std::endl;
try
{
const auto contentType = static_cast<IO::FileType>((outputHasVideo ? IO::FileType::Video : IO::FileType::Unknown) | (outputHasAudio ? IO::FileType::Audio : IO::FileType::Unknown) | (outputHasSubtitles ? IO::FileType::Subtitles : IO::FileType::Unknown));
fileDataInfo = IO::Vid2h::writeFileHeader(binFile, contentType);
}
catch (const std::runtime_error &e)
{
binFile.close();
std::cerr << "Failed to write data to output file: " << e.what() << std::endl;
return 1;
}
}
else
{
std::cerr << "Failed to open " << m_outFile << ".bin for writing" << std::endl;
return 1;
}
}
// create statistics writer
IO::StatisticsWriter statisticsWriter;
if (options.outputStats)
{
std::vector<std::string> types;
if (outputHasAudio)
{
types.push_back("audio");
}
if (outputHasVideo)
{
types.push_back("video");
}
statisticsWriter.open(m_outFile, types);
}
// create statistics window
Statistics::Window window(2 * mediaInfo.videoWidth, 2 * mediaInfo.videoHeight, "vid2h");
auto statistics = window.getStatisticsContainer();
// process media frames
uint32_t lastProgress = 0;
auto startTime = std::chrono::steady_clock::now();
// Video info
uint32_t videoFrameIndex = 0; // Index of last frame processed
uint64_t videoOutCompressedSize = 0; // Combined size of compressed video data
uint32_t videoOutMaxMemoryNeeded = 0; // Maximum memory needed for decoding video frames
Image::FrameInfo videoOutInfo; // Information about video from media decoder
// Audio info
uint64_t audioOutCompressedSize = 0; // Combined size of compressed audio data
int32_t audioFirstFrameOffset = 0; // Offset of first audio frame in samples
// Subtitles info
uint32_t subtitleFrameIndex = 0; // Index of last processed subtitle
while (videoFrameIndex < mediaInfo.videoNrOfFrames && audioProcessing.nrOfInputFrames() < mediaInfo.audioNrOfFrames)
{
const auto inFrame = mediaReader.readFrame();
// check if EOF
if (inFrame.frameType == IO::FrameType::Unknown)
{
REQUIRE(!outputHasVideo || videoFrameIndex == (mediaInfo.videoNrOfFrames - 1), std::runtime_error, "Expected " << mediaInfo.videoNrOfFrames << " video frames, but got " << videoFrameIndex);
REQUIRE(!outputHasAudio || audioProcessing.nrOfInputFrames() == (mediaInfo.audioNrOfFrames - 1), std::runtime_error, "Expected " << mediaInfo.audioNrOfFrames << " audio frames, but got " << audioProcessing.nrOfInputFrames());
break;
}
// check if we need to store a subtitle frame
// we do this before adding the actual frame, because its present time might already be higher
if (outputHasSubtitles && subtitleFrameIndex < subtitles.size())
{
const auto &subtitle = subtitles[subtitleFrameIndex];
if (inFrame.presentTimeInS >= subtitle.startTimeS)
{
// write subtitle to file
if (!options.dryRun && binFile.is_open())
{
IO::Vid2h::writeFrame(binFile, subtitle);
}
++subtitleFrameIndex;
}
}
// check if image frame
if (inFrame.frameType == IO::FrameType::Pixels && outputHasVideo)
{
const auto &inImage = std::get<std::vector<Color::XRGB8888>>(inFrame.data);
REQUIRE(inImage.size() == mediaInfo.videoWidth * mediaInfo.videoHeight, std::runtime_error, "Unexpected image size");
// build internal image from pixels and apply processing
const Image::FrameInfo imageInfo = {{mediaInfo.videoWidth, mediaInfo.videoHeight}, Color::Format::Unknown, Color::Format::Unknown, 0, 0};
const Image::MapInfo mapInfo = {{0, 0}, {}};
const auto outFrame = videoProcessing.processStream(Image::Frame{videoFrameIndex, "", Image::DataType(Image::DataType::Flags::Bitmap), imageInfo, inImage, mapInfo}, statistics);
videoOutCompressedSize += outFrame.data.pixels().rawSize() + (options.paletted ? outFrame.data.colorMap().rawSize() : 0);
videoOutMaxMemoryNeeded = videoOutMaxMemoryNeeded < outFrame.info.maxMemoryNeeded ? outFrame.info.maxMemoryNeeded : videoOutMaxMemoryNeeded;
videoOutInfo = outFrame.info;
// write image to file
if (!options.dryRun && binFile.is_open())
{
IO::Vid2h::writeFrame(binFile, outFrame);
}
++videoFrameIndex;
// write output statistics
if (options.outputStats)
{
auto compressed = outFrame.data.pixels().convertDataToRaw();
statisticsWriter.writeFrame("video", compressed);
}
}
// check if audio frame
else if (inFrame.frameType == IO::FrameType::Audio && outputHasAudio)
{
// build audio frame from sample data
const Audio::Frame audioFrame = {audioProcessing.nrOfInputFrames(), "", {mediaInfo.audioSampleRateHz, mediaInfo.audioChannelFormat, mediaInfo.audioSampleFormat, false, 0}, std::get<std::vector<int16_t>>(inFrame.data), 0};
const auto outFrameOpt = audioProcessing.processStream(audioFrame, false, statistics);
if (outFrameOpt.has_value())
{
const auto &outFrame = outFrameOpt.value();
audioOutCompressedSize += AudioHelpers::rawDataSize(outFrame.data);
// write samples to file
if (!options.dryRun && binFile.is_open())
{
IO::Vid2h::writeFrame(binFile, outFrame);
}
// write output statistics
if (options.outputStats)
{
auto compressed = AudioHelpers::toRawData(outFrame.data, outFrame.info.channelFormat);
statisticsWriter.writeFrame("audio", compressed);
}
}
}
// calculate progress
const uint32_t newProgress = ((100 * videoFrameIndex) / mediaInfo.videoNrOfFrames);
if (lastProgress != newProgress)
{
lastProgress = newProgress;
const auto newTime = std::chrono::steady_clock::now();
const auto timePassedMs = std::chrono::duration<double>(newTime - startTime);
const auto fps = static_cast<double>(videoFrameIndex) / timePassedMs.count();
const auto restS = (mediaInfo.videoNrOfFrames - videoFrameIndex) / fps;
std::cout << std::fixed << std::setprecision(1) << lastProgress << "%, " << fps << " fps, " << restS << "s remaining" << std::endl;
}
// update statistics
window.update();
}
// flush remaining buffers
auto outFrameOpt = audioProcessing.processStream(Audio::Frame(), true, statistics);
if (outFrameOpt.has_value())
{
const auto &outFrame = outFrameOpt.value();
audioOutCompressedSize += AudioHelpers::rawDataSize(outFrame.data);
// write samples to file
if (!options.dryRun && binFile.is_open())
{
IO::Vid2h::writeFrame(binFile, outFrame);
}
}
// write final file header to start of stream
if (!options.dryRun && binFile.is_open())
{
// write headers
if (outputHasAudio)
{
auto audioHeader = IO::Vid2h::createAudioHeader(audioProcessing.outputFrameInfo(), audioProcessing.nrOfOutputFrames(), audioProcessing.nrOfOutputSamples(), audioFirstFrameOffset, audioProcessing.outputMaxMemoryNeeded(), audioProcessing.getDecodingSteps());
IO::Vid2h::writeAudioHeader(binFile, fileDataInfo, audioHeader);
}
if (outputHasVideo)
{
auto videoHeader = IO::Vid2h::createVideoHeader(videoOutInfo, mediaInfo.videoNrOfFrames, mediaInfo.videoFrameRateHz, videoOutMaxMemoryNeeded, 0, videoProcessing.getDecodingSteps());
IO::Vid2h::writeVideoHeader(binFile, fileDataInfo, videoHeader);
}
if (outputHasSubtitles)
{
auto subtitlesHeader = IO::Vid2h::createSubtitlesHeader(subtitleFrameIndex);
IO::Vid2h::writeSubtitlesHeader(binFile, fileDataInfo, subtitlesHeader);
}
// write meta data
if ((options.metaFile || options.metaString) && !metaData.empty())
{
IO::Vid2h::writeMetaData(binFile, fileDataInfo, metaData);
}
binFile.close();
}
// output some info about data
if (outputHasVideo)
{
const auto videoInputSize = mediaInfo.videoWidth * mediaInfo.videoHeight * 3 * mediaInfo.videoNrOfFrames;
std::cout << "Video:" << std::endl;
std::cout << " Video input size: " << static_cast<double>(videoInputSize) / (1024 * 1024) << " MB" << std::endl;
std::cout << " Compressed size: " << std::fixed << std::setprecision(2) << static_cast<double>(videoOutCompressedSize) / (1024 * 1024) << " MB" << std::endl;
std::cout << " Avg. bit rate: " << std::fixed << std::setprecision(2) << (static_cast<double>(videoOutCompressedSize) / 1024) / mediaInfo.videoDurationS << " kB/s" << std::endl;
std::cout << " Avg. frame size: " << videoOutCompressedSize / mediaInfo.videoNrOfFrames << " Byte" << std::endl;
std::cout << " Max. intermediate memory for decompression: " << videoOutMaxMemoryNeeded << " Byte" << std::endl;
}
if (outputHasAudio)
{
std::cout << "Audio:" << std::endl;
std::cout << " Compressed size: " << std::fixed << std::setprecision(2) << static_cast<double>(audioOutCompressedSize) / (1024 * 1024) << " MB" << std::endl;
std::cout << " Avg. frame size: " << audioOutCompressedSize / audioProcessing.nrOfOutputFrames() << " Byte" << std::endl;
std::cout << " Max. intermediate memory for decompression: " << audioProcessing.outputMaxMemoryNeeded() << " Byte" << std::endl;
}
if (outputHasSubtitles)
{
std::cout << "Subtitles:" << std::endl;
std::cout << " Wrote " << subtitleFrameIndex << " subtitle frames to output" << std::endl;
}
}
catch (const std::runtime_error &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
std::cout << "Done" << std::endl;
return 0;
}