Commit b7c2a468 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

Merge pull request #4107 from SpecLad:libav-compat

parents 719099ad 50842b90
......@@ -3,9 +3,11 @@ set(HAVE_FFMPEG_CODEC 1)
set(HAVE_FFMPEG_FORMAT 1)
set(HAVE_FFMPEG_UTIL 1)
set(HAVE_FFMPEG_SWSCALE 1)
set(HAVE_FFMPEG_RESAMPLE 0)
set(HAVE_GENTOO_FFMPEG 1)
set(ALIASOF_libavcodec_VERSION 55.18.102)
set(ALIASOF_libavformat_VERSION 55.12.100)
set(ALIASOF_libavutil_VERSION 52.38.100)
set(ALIASOF_libswscale_VERSION 2.3.100)
set(ALIASOF_libavresample_VERSION 1.0.1)
\ No newline at end of file
......@@ -889,6 +889,7 @@ if(DEFINED WITH_FFMPEG)
status(" format:" HAVE_FFMPEG_FORMAT THEN "YES (ver ${ALIASOF_libavformat_VERSION})" ELSE NO)
status(" util:" HAVE_FFMPEG_UTIL THEN "YES (ver ${ALIASOF_libavutil_VERSION})" ELSE NO)
status(" swscale:" HAVE_FFMPEG_SWSCALE THEN "YES (ver ${ALIASOF_libswscale_VERSION})" ELSE NO)
status(" resample:" HAVE_FFMPEG_RESAMPLE THEN "YES (ver ${ALIASOF_libavresample_VERSION})" ELSE NO)
status(" gentoo-style:" HAVE_GENTOO_FFMPEG THEN YES ELSE NO)
endif(DEFINED WITH_FFMPEG)
......
......@@ -190,7 +190,7 @@ if(WITH_XIMEA)
endif(WITH_XIMEA)
# --- FFMPEG ---
ocv_clear_vars(HAVE_FFMPEG HAVE_FFMPEG_CODEC HAVE_FFMPEG_FORMAT HAVE_FFMPEG_UTIL HAVE_FFMPEG_SWSCALE HAVE_GENTOO_FFMPEG HAVE_FFMPEG_FFMPEG)
ocv_clear_vars(HAVE_FFMPEG HAVE_FFMPEG_CODEC HAVE_FFMPEG_FORMAT HAVE_FFMPEG_UTIL HAVE_FFMPEG_SWSCALE HAVE_FFMPEG_RESAMPLE HAVE_GENTOO_FFMPEG HAVE_FFMPEG_FFMPEG)
if(WITH_FFMPEG)
if(WIN32 AND NOT ARM)
include("${OpenCV_SOURCE_DIR}/3rdparty/ffmpeg/ffmpeg_version.cmake")
......@@ -199,6 +199,7 @@ if(WITH_FFMPEG)
CHECK_MODULE(libavformat HAVE_FFMPEG_FORMAT)
CHECK_MODULE(libavutil HAVE_FFMPEG_UTIL)
CHECK_MODULE(libswscale HAVE_FFMPEG_SWSCALE)
CHECK_MODULE(libavresample HAVE_FFMPEG_RESAMPLE)
CHECK_INCLUDE_FILE(libavformat/avformat.h HAVE_GENTOO_FFMPEG)
CHECK_INCLUDE_FILE(ffmpeg/avformat.h HAVE_FFMPEG_FFMPEG)
......
......@@ -792,7 +792,9 @@ double CvCapture_FFMPEG::getProperty( int property_id )
case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
return (double)frame.height;
case CV_FFMPEG_CAP_PROP_FPS:
#if LIBAVCODEC_BUILD > 4753
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
return av_q2d(video_st->avg_frame_rate);
#elif LIBAVCODEC_BUILD > 4753
return av_q2d(video_st->r_frame_rate);
#else
return (double)video_st->codec.frame_rate
......@@ -840,7 +842,11 @@ int CvCapture_FFMPEG::get_bitrate()
double CvCapture_FFMPEG::get_fps()
{
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
double fps = r2d(ic->streams[video_stream]->avg_frame_rate);
#else
double fps = r2d(ic->streams[video_stream]->r_frame_rate);
#endif
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
if (fps < eps_zero)
......@@ -1001,6 +1007,7 @@ struct CvVideoWriter_FFMPEG
int input_pix_fmt;
Image_FFMPEG temp_image;
int frame_width, frame_height;
int frame_idx;
bool ok;
struct SwsContext *img_convert_ctx;
};
......@@ -1078,6 +1085,7 @@ void CvVideoWriter_FFMPEG::init()
memset(&temp_image, 0, sizeof(temp_image));
img_convert_ctx = 0;
frame_width = frame_height = 0;
frame_idx = 0;
ok = false;
}
......@@ -1228,15 +1236,20 @@ static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;
static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st, uint8_t * outbuf, uint32_t outbuf_size, AVFrame * picture )
static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st,
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
uint8_t *, uint32_t,
#else
uint8_t * outbuf, uint32_t outbuf_size,
#endif
AVFrame * picture )
{
#if LIBAVFORMAT_BUILD > 4628
AVCodecContext * c = video_st->codec;
#else
AVCodecContext * c = &(video_st->codec);
#endif
int out_size;
int ret = 0;
int ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
if (oc->oformat->flags & AVFMT_RAWPICTURE) {
/* raw video case. The API will change slightly in the near
......@@ -1256,12 +1269,32 @@ static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st,
ret = av_write_frame(oc, &pkt);
} else {
/* encode the image */
out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
AVPacket pkt;
av_init_packet(&pkt);
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
int got_output = 0;
pkt.data = NULL;
pkt.size = 0;
ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
if (ret < 0)
;
else if (got_output) {
if (pkt.pts != (int64_t)AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(pkt.pts, c->time_base, video_st->time_base);
if (pkt.dts != (int64_t)AV_NOPTS_VALUE)
pkt.dts = av_rescale_q(pkt.dts, c->time_base, video_st->time_base);
if (pkt.duration)
pkt.duration = av_rescale_q(pkt.duration, c->time_base, video_st->time_base);
pkt.stream_index= video_st->index;
ret = av_write_frame(oc, &pkt);
av_free_packet(&pkt);
}
else
ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
#else
int out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
/* if zero size, it means the image was buffered */
if (out_size > 0) {
AVPacket pkt;
av_init_packet(&pkt);
#if LIBAVFORMAT_BUILD > 4752
if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
......@@ -1276,9 +1309,8 @@ static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st,
/* write the compressed frame in the media file */
ret = av_write_frame(oc, &pkt);
} else {
ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
}
#endif
}
return ret;
}
......@@ -1385,7 +1417,9 @@ bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int
(PixelFormat)input_pix_fmt, width, height);
}
picture->pts = frame_idx;
ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
frame_idx++;
return ret;
}
......@@ -1697,6 +1731,7 @@ bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
}
frame_width = width;
frame_height = height;
frame_idx = 0;
ok = true;
return true;
......
This diff is collapsed.
......@@ -132,6 +132,7 @@ public:
writer << img;
}
writer.release();
if (!created) created = true;
else remove(filename.c_str());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment