-
Notifications
You must be signed in to change notification settings - Fork 229
/
main.cpp
194 lines (173 loc) · 5.18 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
/*
Untrunc - main.cpp
Untrunc is GPL software; you can freely distribute,
redistribute, modify & use under the terms of the GNU General
Public License; either version 2 or its successor.
Untrunc is distributed under the GPL "AS IS", without
any warranty; without the implied warranty of merchantability
or fitness for either an expressed or implied particular purpose.
Please see the included GNU General Public License (GPL) for
your rights and further details; see the file COPYING. If you
cannot, write to the Free Software Foundation, 59 Temple Place
Suite 330, Boston, MA 02111-1307, USA. Or www.fsf.org
Copyright 2010 Federico Ponchio
*/
#include "mp4.h"
#include "atom.h"
#include "log.h"
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void usage() {
cout << "Usage: untrunc [-aisdetmMbNvwqeo] <ok.mp4> [<corrupt.mp4>]\n\n"
<< " -o: output filename (if repairing)\n"
<< " -i: info about codecs and mov structure\n"
<< " -a: test the ok video\n"
<< " -s: simulate recovering the ok video for debug purposes\n"
<< " -t: analyze track\n"
<< " -m: use the same offset for mdat beginning\n"
<< " -M: search for probable packet starts for mdat\n"
<< " -b: specify initial byte for mdat content\n"
<< " -N: don't skip zeros. (useful for pcm audio)"
<< " -d: attepmt to fix audio/video drifting"
<< " -q: silent\n"
<< " -e: error\n"
<< " -v; verbose\n"
<< " -w: debug info\n\n";
}
void searchFile(std::string ok, std::vector<uint8_t> search) {
FILE * fp = fopen(ok.c_str(), "r");
if(!fp) {
cerr << "Could not open file: " << ok << endl;
return;
}
int batch = 1<<20;
uint8_t buffer[batch];
size_t pos = 0;
while(1) {
int readed = fread(buffer, 1, batch, fp);
if(readed <= search.size()) break;
for(int k = 0; k < readed - search.size(); k++) {
int i = 0;
while(i < search.size()) {
if(search[i] != buffer[k+i])
break;
i++;
}
if(i == search.size()) {
cout << "Found at:" << pos + k << endl;
}
}
pos += readed - search.size();
fseek(fp, - search.size(), SEEK_CUR);
}
}
int toHex(char s) {
if(s >= 'a') return 10 + (s - 'a');
if(s >= 'A') return 10 + (s - 'A');
return s - '0';
}
std::vector<uint8_t> hexToStr(const char *str) {
std::vector<uint8_t> s;
while(*str != 0) {
uint8_t c = 16*toHex(*str);
str++;
if(*str == 0) break;
c += toHex(*str);
s.push_back(c);
str++;
}
return s;
}
int main(int argc, char *argv[]) {
std::string output_filename;
bool info = false;
bool analyze = false;
bool simulate = false;
int analyze_track = -1;
bool drifting = false;
Mp4::MdatStrategy mdat_strategy = Mp4::FIRST;
//bool same_mdat_start = false; //if mdat can be found or starting of packets try using the same absolute offset.
//bool ignore_mdat_start = false; //ignore mdat string and look for first recognizable packet.
bool skip_zeros = true;
int64_t mdat_begin = -1; //start of packets if specified.
int i = 1;
std::vector<uint8_t> search;
for(; i < argc; i++) {
string arg(argv[i]);
if(arg[0] == '-') {
switch(arg[1]) {
case 'o': output_filename = std::string(argv[i+1]); i++; break;
case 'i': info = true; break;
case 'a': analyze = true; break;
case 's': simulate = true; break;
case 'd': drifting = true; break;
case 't': analyze_track = atoi(argv[i+1]); i++; break;
case 'q': Logger::log_level = Logger::SILENT; break;
case 'e': Logger::log_level = Logger::ERROR; break;
case 'v': Logger::log_level = Logger::INFO; break;
case 'w': Logger::log_level = Logger::DEBUG; break;
case 'm': mdat_strategy = Mp4::SAME; break;
case 'M': mdat_strategy = Mp4::SEARCH; break;
case 'b': mdat_strategy = Mp4::SPECIFIED; mdat_begin = atoi(argv[i+1]); i++; break;
case 'B': skip_zeros = false; break;
case 'S': search = hexToStr(argv[i+1]); i++; break;
}
} else
break;
}
if(argc == i) {
usage();
return -1;
}
string ok = argv[i];
if(search.size()) {
searchFile(ok, search);
return 0;
}
string corrupt;
i++;
if(i < argc)
corrupt = argv[i];
Log::info << "Reading: " << ok << endl;
Mp4 mp4;
try {
mp4.open(ok);
if(info) {
mp4.printMediaInfo();
mp4.printAtoms();
}
if(analyze) {
mp4.analyze(analyze_track);
}
if(simulate)
mp4.simulate(mdat_strategy, mdat_begin);
if(corrupt.size()) {
bool success = mp4.repair(corrupt, mdat_strategy, mdat_begin, skip_zeros, drifting);
//if the user didn't specify the strategy, try them all.
if(!success && mdat_strategy == Mp4::FIRST) {
vector<Mp4::MdatStrategy> strategies = { Mp4::SAME, Mp4::SEARCH, Mp4::LAST };
for(Mp4::MdatStrategy strategy: strategies) {
Log::info << "\n\nTrying a different approach to locate mdat start" << endl;
success = mp4.repair(corrupt, strategy, mdat_begin, skip_zeros);
if(success) break;
}
}
if(!success) {
Log::error << "Failed recovering the file\n";
}
size_t lastindex = corrupt.find_last_of(".");
if(output_filename.size() == 0)
output_filename = corrupt.substr(0, lastindex) + "_fixed.mp4";
mp4.saveVideo(output_filename);
}
} catch(string e) {
Log::error << e << endl;
return -1;
} catch(const char *e) {
Log::error << e << endl;
return -1;
}
return 0;
}