Skip to content

Commit

Permalink
Merge pull request #2 from lucky9-cyou/ylin/fix-ffmpeg
Browse files Browse the repository at this point in the history
[fix]: fix ffmpeg bug for ffmpeg > 5.0, fix glfw bug
  • Loading branch information
lucky9-cyou authored Oct 12, 2023
2 parents 8d1fd23 + 6268f24 commit 36de064
Show file tree
Hide file tree
Showing 79 changed files with 41,619 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

CMAKE_MINIMUM_REQUIRED(VERSION 3.22)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set (CMAKE_SYSTEM_VERSION 10.0.15063.0 CACHE INTERNAL "Cmake system version" FORCE)
PROJECT(sibr_projects)

Expand Down Expand Up @@ -193,6 +194,13 @@ endif()

add_custom_target(PREBUILD ALL)

if(USE_PATCHED_GLFW)
add_subdirectory(third_party/glfw)
set(GLFW_LIBRARIES glfw)
else()
pkg_search_module(GLFW REQUIRED glfw3>=3.1)
endif()

## Include all projects
set(SIBR_PROJECTS_SAMPLES_SUBPAGE_REF "")
set(SIBR_PROJECTS_OURS_SUBPAGE_REF "")
Expand Down
1 change: 0 additions & 1 deletion cmake/linux/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ sibr_addlibrary(
##message("***********=============> GLFW IS " ${GLFW_LIBRARY})
##message("***********=============> GLFW IS " ${GLFW_LIBRARIES})

find_package(glfw3 REQUIRED)

sibr_gitlibrary(TARGET imgui
GIT_REPOSITORY "https://gitlab.inria.fr/sibr/libs/imgui.git"
Expand Down
63 changes: 60 additions & 3 deletions src/core/video/FFmpegVideoEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


#include "FFmpegVideoEncoder.hpp"
#include "core/video/Video.hpp"

#ifndef HEADLESS
extern "C"
Expand All @@ -22,6 +23,22 @@ extern "C"
#endif

#define QQ(rat) (rat.num/(double)rat.den)
#define CALC_FFMPEG_VERSION(a,b,c) ( a<<16 | b<<8 | c )

// https://github.com/FFmpeg/FFmpeg/blob/b6af56c034759b81985f8ea094e41cbd5f7fecfb/doc/APIchanges#L602-L605
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(58, 9, 100)
# define CV_FFMPEG_REGISTER
#endif

// AVStream.codec deprecated in favor of AVStream.codecpar
// https://github.com/FFmpeg/FFmpeg/blob/b6af56c034759b81985f8ea094e41cbd5f7fecfb/doc/APIchanges#L1039-L1040
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(59, 16, 100)
//#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(57, 33, 100)
# define CV_FFMPEG_CODECPAR
# define CV_FFMPEG_CODEC_FIELD codecpar
#else
# define CV_FFMPEG_CODEC_FIELD codec
#endif

// Disable ffmpeg deprecation warning.
#pragma warning(disable : 4996)
Expand All @@ -43,7 +60,10 @@ namespace sibr {
SIBR_LOG << "[FFMPEG] Registering all." << std::endl;
// Ignore next line warning.
#pragma warning(suppress : 4996)
av_register_all();
#ifdef CV_FFMPEG_REGISTER
/* register all codecs, demux and protocols */
av_register_all();
#endif
ffmpegInitDone = true;
}

Expand Down Expand Up @@ -78,10 +98,20 @@ namespace sibr {
SIBR_WRG << "[FFMPEG] Can not av_write_trailer " << std::endl;
}


if (video_st) {
avcodec_close(video_st->codec);
#ifdef CV_FFMPEG_CODECPAR
avcodec_close(pCodecCtx);
#endif
video_st = nullptr;
av_free(frameYUV);
}

if (pCodecCtx) {
#ifdef CV_FFMPEG_CODECPAR
avcodec_free_context(&pCodecCtx);
#endif
}
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);

Expand Down Expand Up @@ -136,7 +166,12 @@ namespace sibr {
return;
}

#ifndef CV_FFMPEG_CODECPAR
pCodecCtx = video_st->codec;
#else
pCodecCtx = avcodec_alloc_context3(pCodec);
#endif

pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
Expand Down Expand Up @@ -229,8 +264,29 @@ namespace sibr {
#ifndef HEADLESS
bool FFVideoEncoder::encode(AVFrame * frame)
{
int got_picture = 0;


#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(59, 16, 100)
int result = avcodec_send_frame(pCodecCtx, frameYUV);
if (result == AVERROR_EOF)
return true;
else if (result < 0)
return false;
else {
pkt = av_packet_alloc();
if (pkt != NULL) {
while (avcodec_receive_packet(pCodecCtx, pkt) == 0) {
av_write_frame(pFormatCtx, pkt);
av_packet_unref(pkt);
}
av_packet_free(&pkt);
return true;
} else {
return false;
}
}
#else
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;
Expand All @@ -243,6 +299,7 @@ namespace sibr {
}

return true;
#endif
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions src/core/video/FFmpegVideoEncoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ namespace sibr {

#ifndef HEADLESS
AVFormatContext* pFormatCtx; ///< Format context.
AVOutputFormat* fmt; ///< Output format.
const AVOutputFormat* fmt; ///< Output format.
AVStream* video_st; ///< Output stream.
AVCodecContext* pCodecCtx; ///< Codec context.
AVCodec* pCodec; ///< Codec.
const AVCodec* pCodec; ///< Codec.
AVPacket * pkt; ///< Encoding packet.

#endif
Expand Down
5 changes: 4 additions & 1 deletion src/core/video/VideoUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#pragma once

#include "Config.hpp"
#include <cstddef>
#include <opencv2/opencv.hpp>
#include <functional>
#include "FFmpegVideoEncoder.hpp"
Expand Down Expand Up @@ -937,11 +938,13 @@ namespace sibr {

uint getModeIndice() const {
uint mode, mode_size = 0;
for (const auto & [key, val] : bins) {
size_t key = 0;
for (const auto& val : bins) {
if (val > mode_size) {
mode_size = val;
mode = key;
}
key++;
}
return mode;
}
Expand Down
48 changes: 48 additions & 0 deletions third_party/glfw/CMake/GenerateMappings.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Usage:
# cmake -P GenerateMappings.cmake <path/to/mappings.h.in> <path/to/mappings.h>

set(source_url "https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt")
set(source_path "${CMAKE_CURRENT_BINARY_DIR}/gamecontrollerdb.txt")
set(template_path "${CMAKE_ARGV3}")
set(target_path "${CMAKE_ARGV4}")

if (NOT EXISTS "${template_path}")
message(FATAL_ERROR "Failed to find template file ${template_path}")
endif()

file(DOWNLOAD "${source_url}" "${source_path}"
STATUS download_status
TLS_VERIFY on)

list(GET download_status 0 status_code)
list(GET download_status 1 status_message)

if (status_code)
message(FATAL_ERROR "Failed to download ${source_url}: ${status_message}")
endif()

file(STRINGS "${source_path}" lines)
foreach(line ${lines})
if (line MATCHES "^[0-9a-fA-F]")
if (line MATCHES "platform:Windows")
if (GLFW_WIN32_MAPPINGS)
set(GLFW_WIN32_MAPPINGS "${GLFW_WIN32_MAPPINGS}\n")
endif()
set(GLFW_WIN32_MAPPINGS "${GLFW_WIN32_MAPPINGS}\"${line}\",")
elseif (line MATCHES "platform:Mac OS X")
if (GLFW_COCOA_MAPPINGS)
set(GLFW_COCOA_MAPPINGS "${GLFW_COCOA_MAPPINGS}\n")
endif()
set(GLFW_COCOA_MAPPINGS "${GLFW_COCOA_MAPPINGS}\"${line}\",")
elseif (line MATCHES "platform:Linux")
if (GLFW_LINUX_MAPPINGS)
set(GLFW_LINUX_MAPPINGS "${GLFW_LINUX_MAPPINGS}\n")
endif()
set(GLFW_LINUX_MAPPINGS "${GLFW_LINUX_MAPPINGS}\"${line}\",")
endif()
endif()
endforeach()

configure_file("${template_path}" "${target_path}" @ONLY NEWLINE_STYLE UNIX)
file(REMOVE "${source_path}")

17 changes: 17 additions & 0 deletions third_party/glfw/CMake/modules/FindEpollShim.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Find EpollShim
# Once done, this will define
#
# EPOLLSHIM_FOUND - System has EpollShim
# EPOLLSHIM_INCLUDE_DIRS - The EpollShim include directories
# EPOLLSHIM_LIBRARIES - The libraries needed to use EpollShim

find_path(EPOLLSHIM_INCLUDE_DIRS NAMES sys/epoll.h sys/timerfd.h HINTS /usr/local/include/libepoll-shim)
find_library(EPOLLSHIM_LIBRARIES NAMES epoll-shim libepoll-shim HINTS /usr/local/lib)

if (EPOLLSHIM_INCLUDE_DIRS AND EPOLLSHIM_LIBRARIES)
set(EPOLLSHIM_FOUND TRUE)
endif (EPOLLSHIM_INCLUDE_DIRS AND EPOLLSHIM_LIBRARIES)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(EpollShim DEFAULT_MSG EPOLLSHIM_LIBRARIES EPOLLSHIM_INCLUDE_DIRS)
mark_as_advanced(EPOLLSHIM_INCLUDE_DIRS EPOLLSHIM_LIBRARIES)
18 changes: 18 additions & 0 deletions third_party/glfw/CMake/modules/FindOSMesa.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Try to find OSMesa on a Unix system
#
# This will define:
#
# OSMESA_LIBRARIES - Link these to use OSMesa
# OSMESA_INCLUDE_DIR - Include directory for OSMesa
#
# Copyright (c) 2014 Brandon Schaefer <[email protected]>

if (NOT WIN32)

find_package (PkgConfig)
pkg_check_modules (PKG_OSMESA QUIET osmesa)

set (OSMESA_INCLUDE_DIR ${PKG_OSMESA_INCLUDE_DIRS})
set (OSMESA_LIBRARIES ${PKG_OSMESA_LIBRARIES})

endif ()
26 changes: 26 additions & 0 deletions third_party/glfw/CMake/modules/FindWaylandProtocols.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
find_package(PkgConfig)

pkg_check_modules(WaylandProtocols QUIET wayland-protocols>=${WaylandProtocols_FIND_VERSION})

execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=pkgdatadir wayland-protocols
OUTPUT_VARIABLE WaylandProtocols_PKGDATADIR
RESULT_VARIABLE _pkgconfig_failed)
if (_pkgconfig_failed)
message(FATAL_ERROR "Missing wayland-protocols pkgdatadir")
endif()

string(REGEX REPLACE "[\r\n]" "" WaylandProtocols_PKGDATADIR "${WaylandProtocols_PKGDATADIR}")

find_package_handle_standard_args(WaylandProtocols
FOUND_VAR
WaylandProtocols_FOUND
REQUIRED_VARS
WaylandProtocols_PKGDATADIR
VERSION_VAR
WaylandProtocols_VERSION
HANDLE_COMPONENTS
)

set(WAYLAND_PROTOCOLS_FOUND ${WaylandProtocols_FOUND})
set(WAYLAND_PROTOCOLS_PKGDATADIR ${WaylandProtocols_PKGDATADIR})
set(WAYLAND_PROTOCOLS_VERSION ${WaylandProtocols_VERSION})
34 changes: 34 additions & 0 deletions third_party/glfw/CMake/modules/FindXKBCommon.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# - Try to find XKBCommon
# Once done, this will define
#
# XKBCOMMON_FOUND - System has XKBCommon
# XKBCOMMON_INCLUDE_DIRS - The XKBCommon include directories
# XKBCOMMON_LIBRARIES - The libraries needed to use XKBCommon
# XKBCOMMON_DEFINITIONS - Compiler switches required for using XKBCommon

find_package(PkgConfig)
pkg_check_modules(PC_XKBCOMMON QUIET xkbcommon)
set(XKBCOMMON_DEFINITIONS ${PC_XKBCOMMON_CFLAGS_OTHER})

find_path(XKBCOMMON_INCLUDE_DIR
NAMES xkbcommon/xkbcommon.h
HINTS ${PC_XKBCOMMON_INCLUDE_DIR} ${PC_XKBCOMMON_INCLUDE_DIRS}
)

find_library(XKBCOMMON_LIBRARY
NAMES xkbcommon
HINTS ${PC_XKBCOMMON_LIBRARY} ${PC_XKBCOMMON_LIBRARY_DIRS}
)

set(XKBCOMMON_LIBRARIES ${XKBCOMMON_LIBRARY})
set(XKBCOMMON_LIBRARY_DIRS ${XKBCOMMON_LIBRARY_DIRS})
set(XKBCOMMON_INCLUDE_DIRS ${XKBCOMMON_INCLUDE_DIR})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(XKBCommon DEFAULT_MSG
XKBCOMMON_LIBRARY
XKBCOMMON_INCLUDE_DIR
)

mark_as_advanced(XKBCOMMON_LIBRARY XKBCOMMON_INCLUDE_DIR)

Loading

0 comments on commit 36de064

Please sign in to comment.