Commit eb3c9ed1 authored by Andrey Kamaev's avatar Andrey Kamaev

Fix ffmpeg wrapper compatibility with libavcodec > 53.25.0

Based on pull request #685
parent 1e9ed142
...@@ -148,6 +148,14 @@ extern "C" { ...@@ -148,6 +148,14 @@ extern "C" {
#define AVERROR_EOF (-MKTAG( 'E','O','F',' ')) #define AVERROR_EOF (-MKTAG( 'E','O','F',' '))
#endif #endif
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
# define CV_CODEC_ID AVCodecID
# define CV_CODEC(name) AV_##name
#else
# define CV_CODEC_ID CodecID
# define CV_CODEC(name) name
#endif
static int get_number_of_cpus(void) static int get_number_of_cpus(void)
{ {
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0) #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0)
...@@ -1021,7 +1029,7 @@ static const char * icvFFMPEGErrStr(int err) ...@@ -1021,7 +1029,7 @@ static const char * icvFFMPEGErrStr(int err)
/* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/ /* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
extern "C" { extern "C" {
enum CodecID codec_get_bmp_id(unsigned int tag); enum CV_CODEC_ID codec_get_bmp_id(unsigned int tag);
} }
void CvVideoWriter_FFMPEG::init() void CvVideoWriter_FFMPEG::init()
...@@ -1073,7 +1081,7 @@ static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bo ...@@ -1073,7 +1081,7 @@ static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bo
/* add a video output stream to the container */ /* add a video output stream to the container */
static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc, static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
CodecID codec_id, CV_CODEC_ID codec_id,
int w, int h, int bitrate, int w, int h, int bitrate,
double fps, int pixel_format) double fps, int pixel_format)
{ {
...@@ -1105,7 +1113,7 @@ static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc, ...@@ -1105,7 +1113,7 @@ static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
c->codec_id = oc->oformat->video_codec; c->codec_id = oc->oformat->video_codec;
#endif #endif
if(codec_id != CODEC_ID_NONE){ if(codec_id != CV_CODEC(CODEC_ID_NONE)){
c->codec_id = codec_id; c->codec_id = codec_id;
} }
...@@ -1166,10 +1174,10 @@ static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc, ...@@ -1166,10 +1174,10 @@ static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
c->gop_size = 12; /* emit one intra frame every twelve frames at most */ c->gop_size = 12; /* emit one intra frame every twelve frames at most */
c->pix_fmt = (PixelFormat) pixel_format; c->pix_fmt = (PixelFormat) pixel_format;
if (c->codec_id == CODEC_ID_MPEG2VIDEO) { if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO)) {
c->max_b_frames = 2; c->max_b_frames = 2;
} }
if (c->codec_id == CODEC_ID_MPEG1VIDEO || c->codec_id == CODEC_ID_MSMPEG4V3){ if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3)){
/* needed to avoid using macroblocks in which some coeffs overflow /* needed to avoid using macroblocks in which some coeffs overflow
this doesnt happen with normal video, it just happens here as the this doesnt happen with normal video, it just happens here as the
motion of the chroma plane doesnt match the luma plane */ motion of the chroma plane doesnt match the luma plane */
...@@ -1263,7 +1271,7 @@ bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int ...@@ -1263,7 +1271,7 @@ bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int
#if LIBAVFORMAT_BUILD < 5231 #if LIBAVFORMAT_BUILD < 5231
// It is not needed in the latest versions of the ffmpeg // It is not needed in the latest versions of the ffmpeg
if( c->codec_id == CODEC_ID_RAWVIDEO && origin != 1 ) if( c->codec_id == CV_CODEC(CODEC_ID_RAWVIDEO) && origin != 1 )
{ {
if( !temp_image.data ) if( !temp_image.data )
{ {
...@@ -1450,7 +1458,7 @@ void CvVideoWriter_FFMPEG::close() ...@@ -1450,7 +1458,7 @@ void CvVideoWriter_FFMPEG::close()
bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc, bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
double fps, int width, int height, bool is_color ) double fps, int width, int height, bool is_color )
{ {
CodecID codec_id = CODEC_ID_NONE; CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
int err, codec_pix_fmt; int err, codec_pix_fmt;
double bitrate_scale = 1; double bitrate_scale = 1;
...@@ -1491,11 +1499,11 @@ bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc, ...@@ -1491,11 +1499,11 @@ bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
/* Lookup codec_id for given fourcc */ /* Lookup codec_id for given fourcc */
#if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0) #if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
if( (codec_id = codec_get_bmp_id( fourcc )) == CODEC_ID_NONE ) if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
return false; return false;
#else #else
const struct AVCodecTag * tags[] = { codec_bmp_tags, NULL}; const struct AVCodecTag * tags[] = { codec_bmp_tags, NULL};
if( (codec_id = av_codec_get_id(tags, fourcc)) == CODEC_ID_NONE ) if( (codec_id = av_codec_get_id(tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
return false; return false;
#endif #endif
...@@ -1517,20 +1525,20 @@ bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc, ...@@ -1517,20 +1525,20 @@ bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
// set a few optimal pixel formats for lossless codecs of interest.. // set a few optimal pixel formats for lossless codecs of interest..
switch (codec_id) { switch (codec_id) {
#if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0) #if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
case CODEC_ID_JPEGLS: case CV_CODEC(CODEC_ID_JPEGLS):
// BGR24 or GRAY8 depending on is_color... // BGR24 or GRAY8 depending on is_color...
codec_pix_fmt = input_pix_fmt; codec_pix_fmt = input_pix_fmt;
break; break;
#endif #endif
case CODEC_ID_HUFFYUV: case CV_CODEC(CODEC_ID_HUFFYUV):
codec_pix_fmt = PIX_FMT_YUV422P; codec_pix_fmt = PIX_FMT_YUV422P;
break; break;
case CODEC_ID_MJPEG: case CV_CODEC(CODEC_ID_MJPEG):
case CODEC_ID_LJPEG: case CV_CODEC(CODEC_ID_LJPEG):
codec_pix_fmt = PIX_FMT_YUVJ420P; codec_pix_fmt = PIX_FMT_YUVJ420P;
bitrate_scale = 3; bitrate_scale = 3;
break; break;
case CODEC_ID_RAWVIDEO: case CV_CODEC(CODEC_ID_RAWVIDEO):
codec_pix_fmt = input_pix_fmt == PIX_FMT_GRAY8 || codec_pix_fmt = input_pix_fmt == PIX_FMT_GRAY8 ||
input_pix_fmt == PIX_FMT_GRAY16LE || input_pix_fmt == PIX_FMT_GRAY16LE ||
input_pix_fmt == PIX_FMT_GRAY16BE ? input_pix_fmt : PIX_FMT_YUV420P; input_pix_fmt == PIX_FMT_GRAY16BE ? input_pix_fmt : PIX_FMT_YUV420P;
...@@ -1761,7 +1769,7 @@ struct OutputMediaStream_FFMPEG ...@@ -1761,7 +1769,7 @@ struct OutputMediaStream_FFMPEG
void write(unsigned char* data, int size, int keyFrame); void write(unsigned char* data, int size, int keyFrame);
// add a video output stream to the container // add a video output stream to the container
static AVStream* addVideoStream(AVFormatContext *oc, CodecID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format); static AVStream* addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format);
AVOutputFormat* fmt_; AVOutputFormat* fmt_;
AVFormatContext* oc_; AVFormatContext* oc_;
...@@ -1808,7 +1816,7 @@ void OutputMediaStream_FFMPEG::close() ...@@ -1808,7 +1816,7 @@ void OutputMediaStream_FFMPEG::close()
} }
} }
AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CodecID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format) AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format)
{ {
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0) #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
AVStream* st = avformat_new_stream(oc, 0); AVStream* st = avformat_new_stream(oc, 0);
...@@ -1888,10 +1896,10 @@ AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CodecID ...@@ -1888,10 +1896,10 @@ AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CodecID
c->gop_size = 12; // emit one intra frame every twelve frames at most c->gop_size = 12; // emit one intra frame every twelve frames at most
c->pix_fmt = pixel_format; c->pix_fmt = pixel_format;
if (c->codec_id == CODEC_ID_MPEG2VIDEO) if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO))
c->max_b_frames = 2; c->max_b_frames = 2;
if (c->codec_id == CODEC_ID_MPEG1VIDEO || c->codec_id == CODEC_ID_MSMPEG4V3) if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3))
{ {
// needed to avoid using macroblocks in which some coeffs overflow // needed to avoid using macroblocks in which some coeffs overflow
// this doesnt happen with normal video, it just happens here as the // this doesnt happen with normal video, it just happens here as the
...@@ -1928,7 +1936,7 @@ bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height, ...@@ -1928,7 +1936,7 @@ bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height,
if (!fmt_) if (!fmt_)
return false; return false;
CodecID codec_id = CODEC_ID_H264; CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_H264);
// alloc memory for context // alloc memory for context
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0) #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
...@@ -2129,23 +2137,23 @@ bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma ...@@ -2129,23 +2137,23 @@ bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma
switch (enc->codec_id) switch (enc->codec_id)
{ {
case CODEC_ID_MPEG1VIDEO: case CV_CODEC(CODEC_ID_MPEG1VIDEO):
*codec = ::VideoCodec_MPEG1; *codec = ::VideoCodec_MPEG1;
break; break;
case CODEC_ID_MPEG2VIDEO: case CV_CODEC(CODEC_ID_MPEG2VIDEO):
*codec = ::VideoCodec_MPEG2; *codec = ::VideoCodec_MPEG2;
break; break;
case CODEC_ID_MPEG4: case CV_CODEC(CODEC_ID_MPEG4):
*codec = ::VideoCodec_MPEG4; *codec = ::VideoCodec_MPEG4;
break; break;
case CODEC_ID_VC1: case CV_CODEC(CODEC_ID_VC1):
*codec = ::VideoCodec_VC1; *codec = ::VideoCodec_VC1;
break; break;
case CODEC_ID_H264: case CV_CODEC(CODEC_ID_H264):
*codec = ::VideoCodec_H264; *codec = ::VideoCodec_H264;
break; break;
......
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