Open dialectrics opened 1 year ago
same problem, I used ubuntu 22, cuda 11.8 gcc 9.0 and tried several older version of ffmpeg. and basic these four errors:
‘av_register_all’ was not declared in this scope error: ‘struct AVStream’ has no member named ‘codec’ error: ‘avcodec_encode_video2’ was not declared in this scope; did you mean ‘avcodec_encode_subtitle’?
Also same problem, Ubuntu 22, cuda 11.8. The problem seems to be in the FFmpeg C++ file, which is based off of a 7 year old extremely outdated file (found link in the header file)
I solved the problem, but since I cannot access the original Gitlab of the SIBR viewer, I can only publish my solution here. I'd appreciate if anyone could contact the author team of the SIBR viewer.
FFmpegVideoEncoder.zip
The solution is to replace the FFmpegVideoEncoder.cpp
file under SIBR_viewers/src/core/video
folder and compile again.
I hope the author notices this problem ASAP.
This fix seemed to work for me
Here is @qgli's changes as a patch file (in case it helps others quickly get up and running in Ubuntu 22.04:
diff --git a/src/core/video/FFmpegVideoEncoder.cpp b/src/core/video/FFmpegVideoEncoder.cpp
index d908531..0fbba4f 100644
--- a/src/core/video/FFmpegVideoEncoder.cpp
+++ b/src/core/video/FFmpegVideoEncoder.cpp
@@ -43,7 +43,7 @@ namespace sibr {
SIBR_LOG << "[FFMPEG] Registering all." << std::endl;
// Ignore next line warning.
#pragma warning(suppress : 4996)
- av_register_all();
+ //av_register_all();
ffmpegInitDone = true;
}
@@ -74,18 +74,18 @@ namespace sibr {
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_free_context(&pCodecCtx); // 释放AVCodecContext
+ av_free(frameYUV);
+ }
+ avio_close(pFormatCtx->pb);
+ avformat_free_context(pFormatCtx);
+
+ needFree = false;
#endif
}
@@ -97,95 +97,96 @@ namespace sibr {
}
- 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;
+
+ 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;
+ }
+
+ // This is the major change.
+ pCodecCtx = avcodec_alloc_context3(pCodec);
+ if (!pCodecCtx) {
+ SIBR_WRG << "[FFMPEG] Could not allocate codec context." << std::endl;
+ return;
+ }
+
+ 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;
+ }
+ // Associating the codec context with the stream.
+ video_st->codecpar = avcodec_parameters_alloc();
+ avcodec_parameters_from_context(video_st->codecpar, pCodecCtx);
+
+
+ // 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)
@@ -227,23 +228,32 @@ namespace sibr {
}
#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 ret;
+
+ // Send the frame for encoding
+ ret = avcodec_send_frame(pCodecCtx, frameYUV);
+ if (ret < 0) {
+ SIBR_WRG << "[FFMPEG] Failed to send frame for encoding." << std::endl;
+ return false;
+ }
+
+ // Receive the encoded packet
+ ret = avcodec_receive_packet(pCodecCtx, pkt);
+ if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
+ SIBR_WRG << "[FFMPEG] Failed to receive encoded packet." << std::endl;
+ return false;
+ }
+ if (ret >= 0) {
+ pkt->stream_index = video_st->index;
+ ret = av_write_frame(pFormatCtx, pkt);
+ av_packet_unref(pkt);
+ }
+
+ return true;
+}
#endif
+
}
Hello, I have same error but I am using ubuntu 18.04 and it's on school server so I can not do anything about that. I tried to compile after replacing the file but the issue persists, Could anyone please help I have never worked with these kind of files so I am really stuck. Thanks.
I'm also experiencing this problem, how did you fix it? I downloaded the latest source code.
Building the SIBR viewer was successful, but cmake fails when installing the target seemingly due to use of deprecated FFMPEG functions. Curious if anyone else building SIBR from source on linux has succeeded and if so what was the formula for success. I've tried a couple versions of FFMPEG and libavcodec but they turn up same results. For the record, I'm able to train successfully and used the forked nerfstudio viewer to visualize the result with zero issues. Any input appreciated, maybe the answer is right under my nose and I just can't see it. Here's the command line output when I follow the SIBR install instructions: