Skip to content

Commit 1c88e1f

Browse files
authored
Merge pull request opencv#19661 from alalek:ffmpeg_fixes_3.4
* videoio(ffmpeg): eliminate MSVC build warnings * videoio(ffmpeg): update initialization code - repair FFmpeg logger settings on each .open() call
1 parent ba7d0c5 commit 1c88e1f

File tree

1 file changed

+59
-43
lines changed

1 file changed

+59
-43
lines changed

modules/videoio/src/cap_ffmpeg_impl.hpp

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
#ifdef __GNUC__
6262
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
6363
#endif
64+
#ifdef _MSC_VER
65+
#pragma warning(disable: 4996) // was declared deprecated
66+
#endif
6467

6568
#ifndef CV_UNUSED // Required for standalone compilation mode (OpenCV defines this in base.hpp)
6669
#define CV_UNUSED(name) (void)name
@@ -250,7 +253,7 @@ inline void get_monotonic_time(timespec *tv)
250253

251254
t.QuadPart -= offset.QuadPart;
252255
microseconds = (double)t.QuadPart / frequencyToMicroseconds;
253-
t.QuadPart = microseconds;
256+
t.QuadPart = (LONGLONG)microseconds;
254257
tv->tv_sec = t.QuadPart / 1000000;
255258
tv->tv_nsec = (t.QuadPart % 1000000) * 1000;
256259
}
@@ -722,16 +725,6 @@ struct ImplMutex::Impl
722725
int refcount;
723726
};
724727

725-
#ifndef __GNUC__
726-
static int _interlockedExchangeAdd(int* addr, int delta)
727-
{
728-
#if defined _MSC_VER && _MSC_VER >= 1500
729-
return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
730-
#else
731-
return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
732-
#endif
733-
}
734-
#endif // __GNUC__
735728

736729
#elif defined __APPLE__
737730

@@ -859,51 +852,70 @@ static void ffmpeg_log_callback(void *ptr, int level, const char *fmt, va_list v
859852

860853
class InternalFFMpegRegister
861854
{
862-
public:
863-
InternalFFMpegRegister()
855+
static void init_()
864856
{
865-
AutoLock lock(_mutex);
866-
if (!_initialized)
857+
static InternalFFMpegRegister instance;
858+
}
859+
860+
static void initLogger_()
861+
{
862+
#ifndef NO_GETENV
863+
char* debug_option = getenv("OPENCV_FFMPEG_DEBUG");
864+
if (debug_option != NULL)
867865
{
868-
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
869-
avformat_network_init();
866+
av_log_set_level(AV_LOG_VERBOSE);
867+
av_log_set_callback(ffmpeg_log_callback);
868+
}
869+
else
870870
#endif
871+
{
872+
av_log_set_level(AV_LOG_ERROR);
873+
}
874+
}
871875

872-
/* register all codecs, demux and protocols */
873-
av_register_all();
874-
875-
/* register a callback function for synchronization */
876-
av_lockmgr_register(&LockCallBack);
877-
878-
#ifndef NO_GETENV
879-
char* debug_option = getenv("OPENCV_FFMPEG_DEBUG");
880-
if (debug_option != NULL)
876+
public:
877+
static void init()
878+
{
879+
if (!_initialized)
880+
{
881+
AutoLock lock(_mutex);
882+
if (!_initialized)
881883
{
882-
av_log_set_level(AV_LOG_VERBOSE);
883-
av_log_set_callback(ffmpeg_log_callback);
884+
init_();
884885
}
885-
else
886+
}
887+
initLogger_(); // update logger setup unconditionally (GStreamer's libav plugin may override these settings)
888+
}
889+
890+
InternalFFMpegRegister()
891+
{
892+
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
893+
avformat_network_init();
886894
#endif
887-
{
888-
av_log_set_level(AV_LOG_ERROR);
889-
}
890895

891-
_initialized = true;
892-
}
896+
/* register all codecs, demux and protocols */
897+
av_register_all();
898+
899+
/* register a callback function for synchronization */
900+
av_lockmgr_register(&LockCallBack);
901+
902+
_initialized = true;
893903
}
894904

895905
~InternalFFMpegRegister()
896906
{
897907
_initialized = false;
898908
av_lockmgr_register(NULL);
909+
av_log_set_callback(NULL);
899910
}
900911
};
901912

902-
static InternalFFMpegRegister _init;
903-
904913
bool CvCapture_FFMPEG::open( const char* _filename )
905914
{
915+
InternalFFMpegRegister::init();
916+
906917
AutoLock lock(_mutex);
918+
907919
unsigned i;
908920
bool valid = false;
909921

@@ -1416,9 +1428,9 @@ double CvCapture_FFMPEG::getProperty( int property_id ) const
14161428
case CV_FFMPEG_CAP_PROP_FRAME_COUNT:
14171429
return (double)get_total_frames();
14181430
case CV_FFMPEG_CAP_PROP_FRAME_WIDTH:
1419-
return (double)((rotation_auto && rotation_angle%180) ? frame.height : frame.width);
1431+
return (double)((rotation_auto && ((rotation_angle%180) != 0)) ? frame.height : frame.width);
14201432
case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
1421-
return (double)((rotation_auto && rotation_angle%180) ? frame.width : frame.height);
1433+
return (double)((rotation_auto && ((rotation_angle%180) != 0)) ? frame.width : frame.height);
14221434
case CV_FFMPEG_CAP_PROP_FPS:
14231435
return get_fps();
14241436
case CV_FFMPEG_CAP_PROP_FOURCC:
@@ -1658,10 +1670,10 @@ bool CvCapture_FFMPEG::setProperty( int property_id, double value )
16581670
case CV_FFMPEG_CAP_PROP_ORIENTATION_AUTO:
16591671
#if ((LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)) && \
16601672
(LIBAVUTIL_BUILD >= CALC_FFMPEG_VERSION(52, 94, 100)))
1661-
rotation_auto = static_cast<bool>(value);
1673+
rotation_auto = value != 0 ? true : false;
16621674
return true;
16631675
#else
1664-
rotation_auto = 0;
1676+
rotation_auto = false;
16651677
return false;
16661678
#endif
16671679
break;
@@ -2096,7 +2108,7 @@ bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int
20962108
// 2. (dataend - SIMD_SIZE) and (dataend + SIMD_SIZE) is from the same 4k page
20972109
const int CV_STEP_ALIGNMENT = 32;
20982110
const size_t CV_SIMD_SIZE = 32;
2099-
const size_t CV_PAGE_MASK = ~(4096 - 1);
2111+
const size_t CV_PAGE_MASK = ~(size_t)(4096 - 1);
21002112
const unsigned char* dataend = data + ((size_t)height * step);
21012113
if (step % CV_STEP_ALIGNMENT != 0 ||
21022114
(((size_t)dataend - CV_SIMD_SIZE) & CV_PAGE_MASK) != (((size_t)dataend + CV_SIMD_SIZE) & CV_PAGE_MASK))
@@ -2295,6 +2307,10 @@ static inline void cv_ff_codec_tag_dump(const AVCodecTag *const *tags)
22952307
bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
22962308
double fps, int width, int height, bool is_color )
22972309
{
2310+
InternalFFMpegRegister::init();
2311+
2312+
AutoLock lock(_mutex);
2313+
22982314
CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
22992315
int err, codec_pix_fmt;
23002316
double bitrate_scale = 1;
@@ -2569,7 +2585,7 @@ bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
25692585
}
25702586

25712587
int64_t lbit_rate = (int64_t)c->bit_rate;
2572-
lbit_rate += (bitrate / 2);
2588+
lbit_rate += (int64_t)(bitrate / 2);
25732589
lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
25742590
c->bit_rate_tolerance = (int)lbit_rate;
25752591
c->bit_rate = (int)lbit_rate;
@@ -2962,7 +2978,7 @@ bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height,
29622978
#endif
29632979

29642980
c->codec_tag = MKTAG('H', '2', '6', '4');
2965-
c->bit_rate_tolerance = c->bit_rate;
2981+
c->bit_rate_tolerance = (int)(c->bit_rate);
29662982

29672983
// open the output file, if needed
29682984
if (!(fmt_->flags & AVFMT_NOFILE))

0 commit comments

Comments
 (0)