mirror of
https://github.com/opencv/opencv.git
synced 2025-06-09 02:23:23 +08:00
Merge pull request #23601 from cudawarped:videocapture_threading
Videoio: FFMpeg remove locks from `VideoCapure/VideoWriter::open()` to fix 20114
This commit is contained in:
commit
c946285a07
@ -909,9 +909,11 @@ static void ffmpeg_log_callback(void *ptr, int level, const char *fmt, va_list v
|
|||||||
class InternalFFMpegRegister
|
class InternalFFMpegRegister
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void init()
|
static void init(const bool threadSafe)
|
||||||
{
|
{
|
||||||
AutoLock lock(_mutex);
|
std::unique_lock<cv::Mutex> lock(_mutex, std::defer_lock);
|
||||||
|
if(!threadSafe)
|
||||||
|
lock.lock();
|
||||||
static InternalFFMpegRegister instance;
|
static InternalFFMpegRegister instance;
|
||||||
initLogger_(); // update logger setup unconditionally (GStreamer's libav plugin may override these settings)
|
initLogger_(); // update logger setup unconditionally (GStreamer's libav plugin may override these settings)
|
||||||
}
|
}
|
||||||
@ -1001,11 +1003,22 @@ inline void fill_codec_context(AVCodecContext * enc, AVDictionary * dict)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool isThreadSafe() {
|
||||||
|
const bool threadSafe = utils::getConfigurationParameterBool("OPENCV_FFMPEG_IS_THREAD_SAFE", false);
|
||||||
|
if (threadSafe) {
|
||||||
|
CV_LOG_WARNING(NULL, "VIDEOIO/FFMPEG: OPENCV_FFMPEG_IS_THREAD_SAFE == 1, all OpenCV locks removed, relying on FFmpeg to provide thread safety. If FFmpeg is not thread safe isOpened() may return false when multiple threads try to call open() at the same time.");
|
||||||
|
}
|
||||||
|
return threadSafe;
|
||||||
|
}
|
||||||
|
|
||||||
bool CvCapture_FFMPEG::open(const char* _filename, const VideoCaptureParameters& params)
|
bool CvCapture_FFMPEG::open(const char* _filename, const VideoCaptureParameters& params)
|
||||||
{
|
{
|
||||||
InternalFFMpegRegister::init();
|
const bool threadSafe = isThreadSafe();
|
||||||
|
InternalFFMpegRegister::init(threadSafe);
|
||||||
|
|
||||||
AutoLock lock(_mutex);
|
std::unique_lock<cv::Mutex> lock(_mutex, std::defer_lock);
|
||||||
|
if(!threadSafe)
|
||||||
|
lock.lock();
|
||||||
|
|
||||||
unsigned i;
|
unsigned i;
|
||||||
bool valid = false;
|
bool valid = false;
|
||||||
@ -2658,9 +2671,12 @@ static inline void cv_ff_codec_tag_dump(const AVCodecTag *const *tags)
|
|||||||
bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
|
bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
|
||||||
double fps, int width, int height, const VideoWriterParameters& params)
|
double fps, int width, int height, const VideoWriterParameters& params)
|
||||||
{
|
{
|
||||||
InternalFFMpegRegister::init();
|
const bool threadSafe = isThreadSafe();
|
||||||
|
InternalFFMpegRegister::init(threadSafe);
|
||||||
|
|
||||||
AutoLock lock(_mutex);
|
std::unique_lock<cv::Mutex> lock(_mutex, std::defer_lock);
|
||||||
|
if (!threadSafe)
|
||||||
|
lock.lock();
|
||||||
|
|
||||||
CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
|
CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
|
||||||
AVPixelFormat codec_pix_fmt;
|
AVPixelFormat codec_pix_fmt;
|
||||||
|
Loading…
Reference in New Issue
Block a user