-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsibr_patch.patch
501 lines (482 loc) · 13.7 KB
/
sibr_patch.patch
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
diff --git a/src/core/assets/InputCamera.cpp b/src/core/assets/InputCamera.cpp
index 97f0754..3ef4656 100755
--- a/src/core/assets/InputCamera.cpp
+++ b/src/core/assets/InputCamera.cpp
@@ -1292,6 +1292,14 @@ namespace sibr
void InputCamera::saveAsLookat(const std::vector<sibr::Camera>& cams, const std::string& fileName)
{
+ sibr::ByteStream stream;
+ const int32 num = int32(cams.size());
+ stream << num;
+ for (const auto &subcam : cams) {
+ stream << subcam;
+ }
+ stream.saveToFile(fileName);
+ /*
std::ofstream file(fileName, std::ios::out | std::ios::trunc);
if (!file.is_open()) {
@@ -1300,6 +1308,7 @@ namespace sibr
}
// Get the padding count.
const int len = int(std::floor(std::log10(cams.size()))) + 1;
+ file << len;
for (size_t cid = 0; cid < cams.size(); ++cid) {
const auto& cam = cams[cid];
std::string id = std::to_string(cid);
@@ -1308,18 +1317,20 @@ namespace sibr
const sibr::Vector3f& pos = cam.position();
const sibr::Vector3f& up = cam.up();
const sibr::Vector3f tgt = cam.position() + cam.dir();
-
-
- file << "Cam" << pad << id;
- file << " -D origin=" << pos[0] << "," << pos[1] << "," << pos[2];
- file << " -D target=" << tgt[0] << "," << tgt[1] << "," << tgt[2];
- file << " -D up=" << up[0] << "," << up[1] << "," << up[2];
- file << " -D fovy=" << cam.fovy();
- file << " -D clip=" << cam.znear() << "," << cam.zfar();
- file << "\n";
+ file << cam;
+ //
+ //
+ // file << "Cam" << pad << id;
+ // file << " -D origin=" << pos[0] << "," << pos[1] << "," << pos[2];
+ // file << " -D target=" << tgt[0] << "," << tgt[1] << "," << tgt[2];
+ // file << " -D up=" << up[0] << "," << up[1] << "," << up[2];
+ // file << " -D fovy=" << cam.fovy();
+ // file << " -D clip=" << cam.znear() << "," << cam.zfar();
+ // file << "\n";
}
file.close();
+ */
}
std::vector<InputCamera::Ptr> InputCamera::loadColmapBin(const std::string& colmapSparsePath, const float zNear, const float zFar, const int fovXfovYFlag)
diff --git a/src/core/video/FFmpegVideoEncoder.cpp b/src/core/video/FFmpegVideoEncoder.cpp
index d908531..2fa358d 100644
--- a/src/core/video/FFmpegVideoEncoder.cpp
+++ b/src/core/video/FFmpegVideoEncoder.cpp
@@ -3,247 +3,236 @@
* GRAPHDECO research group, https://team.inria.fr/graphdeco
* All rights reserved.
*
- * This software is free for non-commercial, research and evaluation use
+ * This software is free for non-commercial, research and evaluation use
* under the terms of the LICENSE.md file.
*
* For inquiries contact [email protected] and/or [email protected]
*/
-
#include "FFmpegVideoEncoder.hpp"
#ifndef HEADLESS
-extern "C"
-{
+extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#endif
-#define QQ(rat) (rat.num/(double)rat.den)
+#define QQ(rat) (rat.num / (double)rat.den)
// Disable ffmpeg deprecation warning.
#pragma warning(disable : 4996)
namespace sibr {
- bool FFVideoEncoder::ffmpegInitDone = false;
+bool FFVideoEncoder::ffmpegInitDone = false;
- FFVideoEncoder::FFVideoEncoder(
- const std::string & _filepath,
- double _fps,
- const sibr::Vector2i & size,
- bool forceResize
- ) : filepath(_filepath), fps(_fps), _forceResize(forceResize)
- {
+FFVideoEncoder::FFVideoEncoder(const std::string &_filepath, double _fps,
+ const sibr::Vector2i &size, bool forceResize)
+ : filepath(_filepath), fps(_fps), _forceResize(forceResize) {
#ifndef HEADLESS
- /** Init FFMPEG, registering available codec plugins. */
- if (!ffmpegInitDone) {
- SIBR_LOG << "[FFMPEG] Registering all." << std::endl;
- // Ignore next line warning.
+ /** Init FFMPEG, registering available codec plugins. */
+ if (!ffmpegInitDone) {
+ SIBR_LOG << "[FFMPEG] Registering all." << std::endl;
+ // Ignore next line warning.
#pragma warning(suppress : 4996)
- av_register_all();
- ffmpegInitDone = true;
- }
-
- sibr::Vector2i sizeFix = size;
- bool hadToFix = false;
- if(sizeFix[0]%2 != 0) {
- sizeFix[0] -= 1;
- hadToFix = true;
- }
- if (sizeFix[1] % 2 != 0) {
- sizeFix[1] -= 1;
- hadToFix = true;
- }
- if(hadToFix) {
- SIBR_WRG << "Non-even video dimensions, resized to " << sizeFix[0] << "x" << sizeFix[1] << "." << std::endl;
- _forceResize = true;
- }
-
- init(sizeFix);
+ // av_register_all();
+ ffmpegInitDone = true;
+ }
+
+ sibr::Vector2i sizeFix = size;
+ bool hadToFix = false;
+ if (sizeFix[0] % 2 != 0) {
+ sizeFix[0] -= 1;
+ hadToFix = true;
+ }
+ if (sizeFix[1] % 2 != 0) {
+ sizeFix[1] -= 1;
+ hadToFix = true;
+ }
+ if (hadToFix) {
+ SIBR_WRG << "Non-even video dimensions, resized to " << sizeFix[0] << "x"
+ << sizeFix[1] << "." << std::endl;
+ _forceResize = true;
+ }
+
+ init(sizeFix);
#endif
- }
+}
- bool FFVideoEncoder::isFine() const
- {
- return initWasFine;
- }
+bool FFVideoEncoder::isFine() const { return initWasFine; }
- void FFVideoEncoder::close()
- {
+void FFVideoEncoder::close() {
#ifndef HEADLESS
- if (av_write_trailer(pFormatCtx) < 0) {
- SIBR_WRG << "[FFMPEG] Can not av_write_trailer " << std::endl;
- }
-
- if (video_st) {
- avcodec_close(video_st->codec);
- av_free(frameYUV);
- }
- avio_close(pFormatCtx->pb);
- avformat_free_context(pFormatCtx);
-
- needFree = false;
+ if (av_write_trailer(pFormatCtx) < 0) {
+ SIBR_WRG << "[FFMPEG] Can not av_write_trailer " << std::endl;
+ }
+
+ if (video_st) {
+ // avcodec_close(video_st->codec);
+ av_free(frameYUV);
+ }
+ avio_close(pFormatCtx->pb);
+ avformat_free_context(pFormatCtx);
+
+ needFree = false;
#endif
- }
-
- FFVideoEncoder::~FFVideoEncoder()
- {
- if (needFree) {
- close();
- }
+}
- }
+FFVideoEncoder::~FFVideoEncoder() {
+ if (needFree) {
+ close();
+ }
+}
- void FFVideoEncoder::init(const sibr::Vector2i & size)
- {
+void FFVideoEncoder::init(const sibr::Vector2i &size) {
#ifndef HEADLESS
- w = size[0];
- h = size[1];
-
- auto out_file = filepath.c_str();
-
-
- pFormatCtx = avformat_alloc_context();
-
- fmt = av_guess_format(NULL, out_file, NULL);
- pFormatCtx->oformat = fmt;
-
- const bool isH264 = pFormatCtx->oformat->video_codec == AV_CODEC_ID_H264;
- if(isH264){
- SIBR_LOG << "[FFMPEG] Found H264 codec." << std::endl;
- } else {
- SIBR_LOG << "[FFMPEG] Found codec with ID " << pFormatCtx->oformat->video_codec << " (not H264)." << std::endl;
- }
-
- if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
- SIBR_WRG << "[FFMPEG] Could not open file " << filepath << std::endl;
- return;
- }
-
- pCodec = avcodec_find_encoder(pFormatCtx->oformat->video_codec);
- if (!pCodec) {
- SIBR_WRG << "[FFMPEG] Could not find codec." << std::endl;
- return;
- }
-
- video_st = avformat_new_stream(pFormatCtx, pCodec);
-
- if (video_st == NULL) {
- SIBR_WRG << "[FFMPEG] Could not create stream." << std::endl;
- return;
- }
-
- pCodecCtx = video_st->codec;
- pCodecCtx->codec_id = fmt->video_codec;
- pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
- pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
- pCodecCtx->width = w;
- pCodecCtx->height = h;
- pCodecCtx->gop_size = 10;
- pCodecCtx->time_base.num = 1;
- pCodecCtx->time_base.den = (int)std::round(fps);
-
- // Required for the header to be well-formed and compatible with Powerpoint/MediaPlayer/...
- if (pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) {
- pCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
- }
-
- //H.264 specific options.
- AVDictionary *param = 0;
- if (pCodecCtx->codec_id == AV_CODEC_ID_H264) {
- av_dict_set(¶m, "preset", "slow", 0);
- av_dict_set(¶m, "tune", "zerolatency", 0);
- }
-
- av_dump_format(pFormatCtx, 0, out_file, 1);
-
- int res = avcodec_open2(pCodecCtx, pCodec, ¶m);
- if(res < 0){
- SIBR_WRG << "[FFMPEG] Failed to open encoder, error: " << res << std::endl;
- return;
- }
- // Write the file header.
- avformat_write_header(pFormatCtx, NULL);
-
- // Prepare the scratch frame.
- frameYUV = av_frame_alloc();
- frameYUV->format = (int)pCodecCtx->pix_fmt;
- frameYUV->width = w;
- frameYUV->height = h;
- frameYUV->linesize[0] = w;
- frameYUV->linesize[1] = w / 2;
- frameYUV->linesize[2] = w / 2;
-
- yuSize[0] = frameYUV->linesize[0] * h;
- yuSize[1] = frameYUV->linesize[1] * h / 2;
-
- pkt = av_packet_alloc();
-
- initWasFine = true;
- needFree = true;
+ w = size[0];
+ h = size[1];
+
+ auto out_file = filepath.c_str();
+
+ pFormatCtx = avformat_alloc_context();
+
+ fmt = av_guess_format(NULL, out_file, NULL);
+ pFormatCtx->oformat = fmt;
+
+ const bool isH264 = pFormatCtx->oformat->video_codec == AV_CODEC_ID_H264;
+ if (isH264) {
+ SIBR_LOG << "[FFMPEG] Found H264 codec." << std::endl;
+ } else {
+ SIBR_LOG << "[FFMPEG] Found codec with ID "
+ << pFormatCtx->oformat->video_codec << " (not H264)." << std::endl;
+ }
+
+ if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
+ SIBR_WRG << "[FFMPEG] Could not open file " << filepath << std::endl;
+ return;
+ }
+
+ pCodec = avcodec_find_encoder(pFormatCtx->oformat->video_codec);
+ if (!pCodec) {
+ SIBR_WRG << "[FFMPEG] Could not find codec." << std::endl;
+ return;
+ }
+
+ video_st = avformat_new_stream(pFormatCtx, pCodec);
+
+ if (video_st == NULL) {
+ SIBR_WRG << "[FFMPEG] Could not create stream." << std::endl;
+ return;
+ }
+
+ // pCodecCtx = video_st->codec;
+ pCodecCtx->codec_id = fmt->video_codec;
+ pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
+ pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
+ pCodecCtx->width = w;
+ pCodecCtx->height = h;
+ pCodecCtx->gop_size = 10;
+ pCodecCtx->time_base.num = 1;
+ pCodecCtx->time_base.den = (int)std::round(fps);
+
+ // Required for the header to be well-formed and compatible with
+ // Powerpoint/MediaPlayer/...
+ if (pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) {
+ pCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
+ }
+
+ // H.264 specific options.
+ AVDictionary *param = 0;
+ if (pCodecCtx->codec_id == AV_CODEC_ID_H264) {
+ av_dict_set(¶m, "preset", "slow", 0);
+ av_dict_set(¶m, "tune", "zerolatency", 0);
+ }
+
+ av_dump_format(pFormatCtx, 0, out_file, 1);
+
+ int res = avcodec_open2(pCodecCtx, pCodec, ¶m);
+ if (res < 0) {
+ SIBR_WRG << "[FFMPEG] Failed to open encoder, error: " << res << std::endl;
+ return;
+ }
+ // Write the file header.
+ avformat_write_header(pFormatCtx, NULL);
+
+ // Prepare the scratch frame.
+ frameYUV = av_frame_alloc();
+ frameYUV->format = (int)pCodecCtx->pix_fmt;
+ frameYUV->width = w;
+ frameYUV->height = h;
+ frameYUV->linesize[0] = w;
+ frameYUV->linesize[1] = w / 2;
+ frameYUV->linesize[2] = w / 2;
+
+ yuSize[0] = frameYUV->linesize[0] * h;
+ yuSize[1] = frameYUV->linesize[1] * h / 2;
+
+ pkt = av_packet_alloc();
+
+ initWasFine = true;
+ needFree = true;
#endif
- }
-
+}
- bool FFVideoEncoder::operator<<(cv::Mat frame)
- {
+bool FFVideoEncoder::operator<<(cv::Mat frame) {
#ifndef HEADLESS
- if (!video_st) {
- return false;
- }
- cv::Mat local;
- if (frame.cols != w || frame.rows != h) {
- if(_forceResize) {
- cv::resize(frame, local, cv::Size(w,h));
- } else {
- SIBR_WRG << "[FFMPEG] Frame doesn't have the same dimensions as the video." << std::endl;
- return false;
- }
- } else {
- local = frame;
- }
-
- cv::cvtColor(local, cvFrameYUV, cv::COLOR_BGR2YUV_I420);
- frameYUV->data[0] = cvFrameYUV.data;
- frameYUV->data[1] = frameYUV->data[0] + yuSize[0];
- frameYUV->data[2] = frameYUV->data[1] + yuSize[1];
-
- //frameYUV->pts = (1.0 / std::round(fps)) *frameCount * 90;
- frameYUV->pts = (int)(frameCount*(video_st->time_base.den) / ((video_st->time_base.num) * std::round(fps)));
- ++frameCount;
-
- return encode(frameYUV);
+ if (!video_st) {
+ return false;
+ }
+ cv::Mat local;
+ if (frame.cols != w || frame.rows != h) {
+ if (_forceResize) {
+ cv::resize(frame, local, cv::Size(w, h));
+ } else {
+ SIBR_WRG
+ << "[FFMPEG] Frame doesn't have the same dimensions as the video."
+ << std::endl;
+ return false;
+ }
+ } else {
+ local = frame;
+ }
+
+ cv::cvtColor(local, cvFrameYUV, cv::COLOR_BGR2YUV_I420);
+ frameYUV->data[0] = cvFrameYUV.data;
+ frameYUV->data[1] = frameYUV->data[0] + yuSize[0];
+ frameYUV->data[2] = frameYUV->data[1] + yuSize[1];
+
+ // frameYUV->pts = (1.0 / std::round(fps)) *frameCount * 90;
+ frameYUV->pts = (int)(frameCount * (video_st->time_base.den) /
+ ((video_st->time_base.num) * std::round(fps)));
+ ++frameCount;
+
+ return encode(frameYUV);
#else
- SIBR_ERR << "Not supported in headless" << std::endl;
- return false;
+ SIBR_ERR << "Not supported in headless" << std::endl;
+ return false;
#endif
- }
+}
- bool FFVideoEncoder::operator<<(const sibr::ImageRGB & frame){
- return (*this)<<(frame.toOpenCVBGR());
- }
+bool FFVideoEncoder::operator<<(const sibr::ImageRGB &frame) {
+ return (*this) << (frame.toOpenCVBGR());
+}
#ifndef HEADLESS
- bool FFVideoEncoder::encode(AVFrame * frame)
- {
- int got_picture = 0;
-
- int ret = avcodec_encode_video2(pCodecCtx, pkt, frameYUV, &got_picture);
- if (ret < 0) {
- SIBR_WRG << "[FFMPEG] Failed to encode frame." << std::endl;
- return false;
- }
- if (got_picture == 1) {
- pkt->stream_index = video_st->index;
- ret = av_write_frame(pFormatCtx, pkt);
- av_packet_unref(pkt);
- }
-
- return true;
- }
+bool FFVideoEncoder::encode(AVFrame *frame) {
+ int got_picture = 0;
+
+ int ret = 0; // avcodec_encode_video2(pCodecCtx, pkt, frameYUV, &got_picture);
+ if (ret < 0) {
+ SIBR_WRG << "[FFMPEG] Failed to encode frame." << std::endl;
+ return false;
+ }
+ if (got_picture == 1) {
+ pkt->stream_index = video_st->index;
+ ret = av_write_frame(pFormatCtx, pkt);
+ av_packet_unref(pkt);
+ }
+
+ return true;
+}
#endif
-}
+} // namespace sibr