Commit 44478d70 authored by André Anjos's avatar André Anjos 💬
Browse files

FFmpeg 3.0.x compatibility (closes #2)

parent 53a8e2af
...@@ -18,9 +18,11 @@ ...@@ -18,9 +18,11 @@
#include <bob.io.base/blitz_array.h> #include <bob.io.base/blitz_array.h>
#if LIBAVUTIL_VERSION_INT < 0x371167 //55.17.103 @ ffmpeg-3.0
#ifndef AV_PIX_FMT_RGB24 #ifndef AV_PIX_FMT_RGB24
#define AV_PIX_FMT_RGB24 PIX_FMT_RGB24 #define AV_PIX_FMT_RGB24 PIX_FMT_RGB24
#endif #endif
#endif
namespace bob { namespace io { namespace video { namespace bob { namespace io { namespace video {
...@@ -217,7 +219,7 @@ namespace bob { namespace io { namespace video { ...@@ -217,7 +219,7 @@ namespace bob { namespace io { namespace video {
m_codec_context = make_codec_context(filename, m_codec_context = make_codec_context(filename,
m_format_context->streams[m_stream_index], m_codec); m_format_context->streams[m_stream_index], m_codec);
m_swscaler = make_scaler(filename, m_codec_context, m_swscaler = make_scaler(filename, m_codec_context,
m_codec_context->pix_fmt, PIX_FMT_RGB24); m_codec_context->pix_fmt, AV_PIX_FMT_RGB24);
m_context_frame = make_empty_frame(filename); m_context_frame = make_empty_frame(filename);
m_rgb_array.reference(blitz::Array<uint8_t,3>(m_codec_context->height, m_rgb_array.reference(blitz::Array<uint8_t,3>(m_codec_context->height,
m_codec_context->width, 3)); m_codec_context->width, 3));
......
...@@ -20,6 +20,9 @@ extern "C" { ...@@ -20,6 +20,9 @@ extern "C" {
#endif #endif
#include <libavutil/avstring.h> #include <libavutil/avstring.h>
#include <libavutil/mathematics.h> #include <libavutil/mathematics.h>
#if LIBAVCODEC_VERSION_INT >= 0x391866 //57.24.102 @ ffmpeg-3.0
#include <libavutil/imgutils.h>
#endif
} }
#include <bob.core/logging.h> #include <bob.core/logging.h>
...@@ -37,6 +40,7 @@ extern "C" { ...@@ -37,6 +40,7 @@ extern "C" {
# define AV_CODEC_ID_MJPEG CODEC_ID_MJPEG # define AV_CODEC_ID_MJPEG CODEC_ID_MJPEG
#endif #endif
#if LIBAVUTIL_VERSION_INT < 0x371167 //55.17.103 @ ffmpeg-3.0
#ifndef AV_PKT_FLAG_KEY #ifndef AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY PKT_FLAG_KEY #define AV_PKT_FLAG_KEY PKT_FLAG_KEY
#endif #endif
...@@ -44,6 +48,7 @@ extern "C" { ...@@ -44,6 +48,7 @@ extern "C" {
#ifndef AV_PIX_FMT_YUV420P #ifndef AV_PIX_FMT_YUV420P
#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P #define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
#endif #endif
#endif
#if LIBAVCODEC_VERSION_INT < 0x347a00 //52.122.0 @ ffmpeg-0.7 #if LIBAVCODEC_VERSION_INT < 0x347a00 //52.122.0 @ ffmpeg-0.7
#define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
...@@ -752,9 +757,15 @@ static void deallocate_frame(AVFrame* f) { ...@@ -752,9 +757,15 @@ static void deallocate_frame(AVFrame* f) {
} }
} }
#if LIBAVUTIL_VERSION_INT >= 0x334A64 //51.74.100 @ ffmpeg-1.0
boost::shared_ptr<AVFrame>
bob::io::video::make_frame(const std::string& filename,
boost::shared_ptr<AVCodecContext> codec, AVPixelFormat pixfmt) {
#else
boost::shared_ptr<AVFrame> boost::shared_ptr<AVFrame>
bob::io::video::make_frame(const std::string& filename, bob::io::video::make_frame(const std::string& filename,
boost::shared_ptr<AVCodecContext> codec, PixelFormat pixfmt) { boost::shared_ptr<AVCodecContext> codec, PixelFormat pixfmt) {
#endif
/* allocate and init a re-usable frame */ /* allocate and init a re-usable frame */
#if LIBAVCODEC_VERSION_INT < 0x373466 //55.52.102 @ ffmpeg-2.2 #if LIBAVCODEC_VERSION_INT < 0x373466 //55.52.102 @ ffmpeg-2.2
...@@ -809,6 +820,7 @@ bob::io::video::make_frame(const std::string& filename, ...@@ -809,6 +820,7 @@ bob::io::video::make_frame(const std::string& filename,
retval->width = codec->width; retval->width = codec->width;
retval->height = codec->height; retval->height = codec->height;
#if LIBAVCODEC_VERSION_INT < 0x391866 //57.24.102 @ ffmpeg-3.0
AVPicture picture; AVPicture picture;
int ok = avpicture_alloc(&picture, pixfmt, codec->width, codec->height); int ok = avpicture_alloc(&picture, pixfmt, codec->width, codec->height);
...@@ -821,6 +833,15 @@ bob::io::video::make_frame(const std::string& filename, ...@@ -821,6 +833,15 @@ bob::io::video::make_frame(const std::string& filename,
/* copy data and linesize picture pointers to frame */ /* copy data and linesize picture pointers to frame */
*((AVPicture *)retval) = picture; *((AVPicture *)retval) = picture;
#else
int ok = av_image_alloc(retval->data, retval->linesize, codec->width, codec->height, pixfmt, 1);
if (ok < 0) {
av_free(retval);
boost::format m("bob::io::video::av_image_alloc(data, linesize, width=%d, height=%d, 1) failed: cannot allocate frame/picture buffer start reading or writing video file `%s'");
m % codec->width % codec->height % filename;
throw std::runtime_error(m.str());
}
#endif
#endif #endif
...@@ -857,9 +878,15 @@ static void deallocate_swscaler(SwsContext* s) { ...@@ -857,9 +878,15 @@ static void deallocate_swscaler(SwsContext* s) {
if (s) sws_freeContext(s); if (s) sws_freeContext(s);
} }
#if LIBAVUTIL_VERSION_INT >= 0x334A64 //51.74.100 @ ffmpeg-1.0
boost::shared_ptr<SwsContext> bob::io::video::make_scaler
(const std::string& filename, boost::shared_ptr<AVCodecContext> ctxt,
AVPixelFormat source_pixel_format, AVPixelFormat dest_pixel_format) {
#else
boost::shared_ptr<SwsContext> bob::io::video::make_scaler boost::shared_ptr<SwsContext> bob::io::video::make_scaler
(const std::string& filename, boost::shared_ptr<AVCodecContext> ctxt, (const std::string& filename, boost::shared_ptr<AVCodecContext> ctxt,
PixelFormat source_pixel_format, PixelFormat dest_pixel_format) { PixelFormat source_pixel_format, PixelFormat dest_pixel_format) {
#endif
/** /**
* Initializes the software scaler (SWScale) so we can convert images to * Initializes the software scaler (SWScale) so we can convert images to
...@@ -1109,7 +1136,11 @@ static AVPacket* allocate_packet() { ...@@ -1109,7 +1136,11 @@ static AVPacket* allocate_packet() {
} }
static void deallocate_packet(AVPacket* p) { static void deallocate_packet(AVPacket* p) {
#if LIBAVCODEC_VERSION_INT < 0x391866 //57.24.102 @ ffmpeg-3.0
if (p->size || p->data) av_free_packet(p); if (p->size || p->data) av_free_packet(p);
#else
if (p->size || p->data) av_packet_unref(p);
#endif
delete p; delete p;
} }
...@@ -1146,7 +1177,9 @@ void bob::io::video::flush_encoder (const std::string& filename, ...@@ -1146,7 +1177,9 @@ void bob::io::video::flush_encoder (const std::string& filename,
/* If size is zero, it means the image was buffered. */ /* If size is zero, it means the image was buffered. */
else if (got_output) { else if (got_output) {
#if LIBAVCODEC_VERSION_INT < 0x391866 //57.24.102 @ ffmpeg-3.0
if (stream->codec->coded_frame->key_frame) pkt->flags |= AV_PKT_FLAG_KEY; if (stream->codec->coded_frame->key_frame) pkt->flags |= AV_PKT_FLAG_KEY;
#endif
pkt->stream_index = stream->index; pkt->stream_index = stream->index;
/* Write the compressed frame to the media file. */ /* Write the compressed frame to the media file. */
...@@ -1186,11 +1219,13 @@ void bob::io::video::flush_encoder (const std::string& filename, ...@@ -1186,11 +1219,13 @@ void bob::io::video::flush_encoder (const std::string& filename,
AVPacket pkt; AVPacket pkt;
av_init_packet(&pkt); av_init_packet(&pkt);
#if LIBAVCODEC_VERSION_INT < 0x391866 //57.24.102 @ ffmpeg-3.0
if ((size_t)stream->codec->coded_frame->pts != AV_NOPTS_VALUE) if ((size_t)stream->codec->coded_frame->pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(stream->codec->coded_frame->pts, pkt.pts = av_rescale_q(stream->codec->coded_frame->pts,
stream->codec->time_base, stream->time_base); stream->codec->time_base, stream->time_base);
if (stream->codec->coded_frame->key_frame) if (stream->codec->coded_frame->key_frame)
pkt.flags |= AV_PKT_FLAG_KEY; pkt.flags |= AV_PKT_FLAG_KEY;
#endif
pkt.stream_index = stream->index; pkt.stream_index = stream->index;
pkt.data = buffer.get(); pkt.data = buffer.get();
...@@ -1269,8 +1304,10 @@ void bob::io::video::write_video_frame (const blitz::Array<uint8_t,3>& data, ...@@ -1269,8 +1304,10 @@ void bob::io::video::write_video_frame (const blitz::Array<uint8_t,3>& data,
/* If size is zero, it means the image was buffered. */ /* If size is zero, it means the image was buffered. */
if (!ok && got_output && pkt->size) { if (!ok && got_output && pkt->size) {
#if LIBAVCODEC_VERSION_INT < 0x391866 //57.24.102 @ ffmpeg-3.0
if (stream->codec->coded_frame && stream->codec->coded_frame->key_frame) if (stream->codec->coded_frame && stream->codec->coded_frame->key_frame)
pkt->flags |= AV_PKT_FLAG_KEY; pkt->flags |= AV_PKT_FLAG_KEY;
#endif
pkt->stream_index = stream->index; pkt->stream_index = stream->index;
...@@ -1315,11 +1352,13 @@ void bob::io::video::write_video_frame (const blitz::Array<uint8_t,3>& data, ...@@ -1315,11 +1352,13 @@ void bob::io::video::write_video_frame (const blitz::Array<uint8_t,3>& data,
AVPacket pkt; AVPacket pkt;
av_init_packet(&pkt); av_init_packet(&pkt);
#if LIBAVCODEC_VERSION_INT < 0x391866 //57.24.102 @ ffmpeg-3.0
if ((size_t)stream->codec->coded_frame->pts != AV_NOPTS_VALUE) if ((size_t)stream->codec->coded_frame->pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(stream->codec->coded_frame->pts, pkt.pts = av_rescale_q(stream->codec->coded_frame->pts,
stream->codec->time_base, stream->time_base); stream->codec->time_base, stream->time_base);
if (stream->codec->coded_frame->key_frame) if (stream->codec->coded_frame->key_frame)
pkt.flags |= AV_PKT_FLAG_KEY; pkt.flags |= AV_PKT_FLAG_KEY;
#endif
pkt.stream_index = stream->index; pkt.stream_index = stream->index;
pkt.data = buffer.get(); pkt.data = buffer.get();
...@@ -1327,7 +1366,11 @@ void bob::io::video::write_video_frame (const blitz::Array<uint8_t,3>& data, ...@@ -1327,7 +1366,11 @@ void bob::io::video::write_video_frame (const blitz::Array<uint8_t,3>& data,
/* Write the compressed frame to the media file. */ /* Write the compressed frame to the media file. */
int ok = av_interleaved_write_frame(format_context.get(), &pkt); int ok = av_interleaved_write_frame(format_context.get(), &pkt);
#if LIBAVCODEC_VERSION_INT < 0x391866 //57.24.102 @ ffmpeg-3.0
av_free_packet(&pkt); av_free_packet(&pkt);
#else
av_packet_unref(&pkt);
#endif
if (ok && (ok != AVERROR(EINVAL))) { if (ok && (ok != AVERROR(EINVAL))) {
boost::format m("bob::io::video::av_interleaved_write_frame() failed: failed to write video frame while encoding file `%s' - ffmpeg reports error %d == `%s'"); boost::format m("bob::io::video::av_interleaved_write_frame() failed: failed to write video frame while encoding file `%s' - ffmpeg reports error %d == `%s'");
m % filename % ok % ffmpeg_error(ok); m % filename % ok % ffmpeg_error(ok);
...@@ -1422,7 +1465,11 @@ bool bob::io::video::read_video_frame (const std::string& filename, ...@@ -1422,7 +1465,11 @@ bool bob::io::video::read_video_frame (const std::string& filename,
swscaler, context_frame, data, pkt, got_frame, swscaler, context_frame, data, pkt, got_frame,
throw_on_error); throw_on_error);
} }
#if LIBAVCODEC_VERSION_INT < 0x391866 //57.24.102 @ ffmpeg-3.0
av_free_packet(pkt.get()); av_free_packet(pkt.get());
#else
av_packet_unref(pkt.get());
#endif
if (got_frame) return true; //break loop if (got_frame) return true; //break loop
} }
...@@ -1516,7 +1563,11 @@ bool bob::io::video::skip_video_frame (const std::string& filename, ...@@ -1516,7 +1563,11 @@ bool bob::io::video::skip_video_frame (const std::string& filename,
dummy_decode_frame(filename, current_frame, codec_context, dummy_decode_frame(filename, current_frame, codec_context,
context_frame, pkt, got_frame, throw_on_error); context_frame, pkt, got_frame, throw_on_error);
} }
#if LIBAVCODEC_VERSION_INT < 0x391866 //57.24.102 @ ffmpeg-3.0
av_free_packet(pkt.get()); av_free_packet(pkt.get());
#else
av_packet_unref(pkt.get());
#endif
if (got_frame) return true; //break loop if (got_frame) return true; //break loop
} }
......
...@@ -120,9 +120,15 @@ namespace bob { namespace io { namespace video { ...@@ -120,9 +120,15 @@ namespace bob { namespace io { namespace video {
* conjunction with other objects required for file encoding, order must be * conjunction with other objects required for file encoding, order must be
* respected. * respected.
*/ */
#if LIBAVUTIL_VERSION_INT >= 0x334A64 //51.74.100 @ ffmpeg-3.0
boost::shared_ptr<SwsContext> make_scaler(const std::string& filename,
boost::shared_ptr<AVCodecContext> stream,
AVPixelFormat source_pixel_format, AVPixelFormat dest_pixel_format);
#else
boost::shared_ptr<SwsContext> make_scaler(const std::string& filename, boost::shared_ptr<SwsContext> make_scaler(const std::string& filename,
boost::shared_ptr<AVCodecContext> stream, boost::shared_ptr<AVCodecContext> stream,
PixelFormat source_pixel_format, PixelFormat dest_pixel_format); PixelFormat source_pixel_format, PixelFormat dest_pixel_format);
#endif
/** /**
* Allocates a frame for a particular context. The frame space will be * Allocates a frame for a particular context. The frame space will be
...@@ -134,8 +140,13 @@ namespace bob { namespace io { namespace video { ...@@ -134,8 +140,13 @@ namespace bob { namespace io { namespace video {
* conjunction with other objects required for file encoding, order must be * conjunction with other objects required for file encoding, order must be
* respected. * respected.
*/ */
#if LIBAVUTIL_VERSION_INT >= 0x334A64 //51.74.100 @ ffmpeg-3.0
boost::shared_ptr<AVFrame> make_frame(const std::string& filename,
boost::shared_ptr<AVCodecContext> stream, AVPixelFormat pixfmt);
#else
boost::shared_ptr<AVFrame> make_frame(const std::string& filename, boost::shared_ptr<AVFrame> make_frame(const std::string& filename,
boost::shared_ptr<AVCodecContext> stream, PixelFormat pixfmt); boost::shared_ptr<AVCodecContext> stream, PixelFormat pixfmt);
#endif
/************************************************************************ /************************************************************************
* Video reading specific utilities * Video reading specific utilities
......
...@@ -20,9 +20,11 @@ ...@@ -20,9 +20,11 @@
#define FFMPEG_VIDEO_BUFFER_SIZE 0 #define FFMPEG_VIDEO_BUFFER_SIZE 0
#endif #endif
#if LIBAVUTIL_VERSION_INT < 0x371167 //55.17.103 @ ffmpeg-3.0
#ifndef AV_PIX_FMT_RGB24 #ifndef AV_PIX_FMT_RGB24
#define AV_PIX_FMT_RGB24 PIX_FMT_RGB24 #define AV_PIX_FMT_RGB24 PIX_FMT_RGB24
#endif #endif
#endif
namespace bob { namespace io { namespace video { namespace bob { namespace io { namespace video {
...@@ -45,7 +47,7 @@ namespace bob { namespace io { namespace video { ...@@ -45,7 +47,7 @@ namespace bob { namespace io { namespace video {
m_codec_context(make_codec_context(filename, m_stream.get(), m_codec)), m_codec_context(make_codec_context(filename, m_stream.get(), m_codec)),
m_context_frame(make_frame(filename, m_codec_context, m_stream->codec->pix_fmt)), m_context_frame(make_frame(filename, m_codec_context, m_stream->codec->pix_fmt)),
#if LIBAVCODEC_VERSION_INT >= 0x352a00 //53.42.0 @ ffmpeg-0.9 #if LIBAVCODEC_VERSION_INT >= 0x352a00 //53.42.0 @ ffmpeg-0.9
m_swscaler(make_scaler(filename, m_codec_context, PIX_FMT_GBRP, m_stream->codec->pix_fmt)), m_swscaler(make_scaler(filename, m_codec_context, AV_PIX_FMT_GBRP, m_stream->codec->pix_fmt)),
#else #else
m_rgb24_frame(make_frame(filename, m_codec_context, AV_PIX_FMT_RGB24)), m_rgb24_frame(make_frame(filename, m_codec_context, AV_PIX_FMT_RGB24)),
m_swscaler(make_scaler(filename, m_codec_context, AV_PIX_FMT_RGB24, m_stream->codec->pix_fmt)), m_swscaler(make_scaler(filename, m_codec_context, AV_PIX_FMT_RGB24, m_stream->codec->pix_fmt)),
......
Supports Markdown
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