1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
From a2a98bc5a5d809105586f3b6f33fa45ba80dc3fa Mon Sep 17 00:00:00 2001
From: Dirk Vanden Boer <dirk.vdb@gmail.com>
Date: Thu, 3 Mar 2016 20:53:34 +0100
Subject: [PATCH] Restored deinterlace functionality using the libavfilter
library
---
CMakeLists.txt | 20 ++--
libffmpegthumbnailer/moviedecoder.cpp | 177 ++++++++++++++++------------------
libffmpegthumbnailer/moviedecoder.h | 12 ++-
3 files changed, 101 insertions(+), 108 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 72a5169..7d6d370 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -52,7 +52,7 @@ FIND_PACKAGE(PkgConfig)
PKG_CHECK_MODULES(AVCODEC_PKG libavcodec REQUIRED)
PKG_CHECK_MODULES(AVFORMAT_PKG libavformat REQUIRED)
PKG_CHECK_MODULES(AVUTIL_PKG libavutil REQUIRED)
-PKG_CHECK_MODULES(SWSCALE_PKG libswscale REQUIRED)
+PKG_CHECK_MODULES(AVFILTER_PKG libavfilter REQUIRED)
FIND_PATH(AVCODEC_INCLUDE_DIR
NAMES libavcodec/avcodec.h
@@ -69,9 +69,9 @@ FIND_PATH(AVUTIL_INCLUDE_DIR
PATHS ${AVUTIL_PKG_INCLUDE_DIRS}
)
-FIND_PATH(SWSCALE_INCLUDE_DIR
- NAMES libswscale/swscale.h
- PATHS ${SWSCALE_PKG_INCLUDE_DIRS}
+FIND_PATH(AVFILTER_INCLUDE_DIR
+ NAMES libavfilter/avfilter.h
+ PATHS ${AVFILTER_PKG_INCLUDE_DIRS}
)
FIND_LIBRARY(AVCODEC_LIBRARY
@@ -89,9 +89,9 @@ FIND_LIBRARY(AVUTIL_LIBRARY
PATHS ${AVUTIL_PKG_LIBRARY_DIRS}
)
-FIND_LIBRARY(SWSCALE_LIBRARY
- NAMES ${SWSCALE_PKG_LIBRARIES}
- PATHS ${SWSCALE_PKG_LIBRARY_DIRS}
+FIND_LIBRARY(AVFILTER_LIBRARY
+ NAMES ${AVFILTER_PKG_LIBRARIES}
+ PATHS ${AVFILTER_PKG_LIBRARY_DIRS}
)
IF (ENABLE_GIO)
@@ -101,10 +101,10 @@ IF (ENABLE_GIO)
LIST(APPEND FFMPEGTHUMBNAILER_LIBRARIES ${DL_LIBRARY})
ENDIF ()
-LIST(APPEND FFMPEGTHUMBNAILER_INCLUDE_DIRS ${AVFORMAT_INCLUDE_DIR} ${AVCODEC_INCLUDE_DIR} ${AVUTIL_INCLUDE_DIR} ${SWSCALE_INCLUDE_DIR})
-LIST(APPEND FFMPEGTHUMBNAILER_LIBRARIES ${AVFORMAT_LIBRARY} ${AVCODEC_LIBRARY} ${AVUTIL_LIBRARY} ${SWSCALE_LIBRARY})
+LIST(APPEND FFMPEGTHUMBNAILER_INCLUDE_DIRS ${AVFORMAT_INCLUDE_DIR} ${AVCODEC_INCLUDE_DIR} ${AVUTIL_INCLUDE_DIR} ${AVFILTER_INCLUDE_DIR})
+LIST(APPEND FFMPEGTHUMBNAILER_LIBRARIES ${AVFORMAT_LIBRARY} ${AVCODEC_LIBRARY} ${AVUTIL_LIBRARY} ${AVFILTER_LIBRARY})
-SET(FFMPEGTHUMBNAILER_PKG_LIBS "-L${AVCODEC_PKG_LIBRARY_DIRS} -l${AVCODEC_PKG_LIBRARIES} -l${AVUTIL_PKG_LIBRARIES} -l${AVFORMAT_PKG_LIBRARIES} -l${SWSCALE_PKG_LIBRARIES}")
+SET(FFMPEGTHUMBNAILER_PKG_LIBS "-L${AVCODEC_PKG_LIBRARY_DIRS} -l${AVCODEC_PKG_LIBRARIES} -l${AVUTIL_PKG_LIBRARIES} -l${AVFILTER_PKG_LIBRARIES} -l${AVFORMAT_PKG_LIBRARIES}")
FOREACH(VAL ${JPEG_LIBRARIES})
SET(FFMPEGTHUMBNAILER_PKG_LIBS "${FFMPEGTHUMBNAILER_PKG_LIBS} ${VAL}")
ENDFOREACH()
diff --git a/libffmpegthumbnailer/moviedecoder.cpp b/libffmpegthumbnailer/moviedecoder.cpp
index f48aa80..d22c6b6 100644
--- a/libffmpegthumbnailer/moviedecoder.cpp
+++ b/libffmpegthumbnailer/moviedecoder.cpp
@@ -23,12 +23,16 @@
#include <cassert>
#include <cstring>
#include <array>
+#include <sstream>
+#include <memory>
extern "C" {
#include <libavutil/opt.h>
-#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
-//#include <libavfilter/avfilter.h>
+#include <libavfilter/avfilter.h>
+#include <libavfilter/avfiltergraph.h>
+#include <libavfilter/buffersink.h>
+#include <libavfilter/buffersrc.h>
}
using namespace std;
@@ -46,9 +50,11 @@ MovieDecoder::MovieDecoder(const string& filename, AVFormatContext* pavContext)
, m_pFormatContext(pavContext)
, m_pVideoCodecContext(nullptr)
, m_pVideoCodec(nullptr)
+, m_pFilterGraph(nullptr)
+, m_pFilterSource(nullptr)
+, m_pFilterSink(nullptr)
, m_pVideoStream(nullptr)
, m_pFrame(nullptr)
-, m_pFrameBuffer(nullptr)
, m_pPacket(nullptr)
, m_FormatContextWasGiven(pavContext != nullptr)
, m_AllowSeek(true)
@@ -79,7 +85,7 @@ void MovieDecoder::initialize(const string& filename)
if (avformat_find_stream_info(m_pFormatContext, nullptr) < 0)
{
destroy();
- throw logic_error(string("Could not find stream information"));
+ throw logic_error("Could not find stream information");
}
initializeVideo();
@@ -111,12 +117,6 @@ void MovieDecoder::destroy()
av_frame_free(&m_pFrame);
}
- if (m_pFrameBuffer)
- {
- av_free(m_pFrameBuffer);
- m_pFrameBuffer = nullptr;
- }
-
m_VideoStream = -1;
avformat_network_deinit();
@@ -170,19 +170,54 @@ void MovieDecoder::initializeVideo()
}
}
-void MovieDecoder::initializeFilterGraph()
+void MovieDecoder::initializeFilterGraph(AVRational timeBase, int width, int height)
{
- // avfilter_register_all();
+ static const AVPixelFormat pixelFormats[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
+
+ auto del = [] (AVBufferSinkParams* p) { av_freep(p); };
+ std::unique_ptr<AVBufferSinkParams, decltype(del)> buffersinkParams(av_buffersink_params_alloc(), del);
+
+ avfilter_register_all();
+
+ m_pFilterGraph = avfilter_graph_alloc();
+ assert(m_pFilterGraph);
+
+ std::stringstream ss;
+ ss << "video_size=" << m_pVideoCodecContext->width << "x" << m_pVideoCodecContext->height
+ << ":pix_fmt=" << m_pVideoCodecContext->pix_fmt
+ << ":time_base=" << timeBase.num << "/" << timeBase.den
+ << ":pixel_aspect=" << m_pVideoCodecContext->sample_aspect_ratio.num << "/" << FFMAX(m_pVideoCodecContext->sample_aspect_ratio.den, 1);
+
+ checkRc(avfilter_graph_create_filter(&m_pFilterSource, avfilter_get_by_name("buffer"), "thumb_buffer", ss.str().c_str(), nullptr, m_pFilterGraph),
+ "Failed to create filter source");
+ buffersinkParams->pixel_fmts = pixelFormats;
+ checkRc(avfilter_graph_create_filter(&m_pFilterSink, avfilter_get_by_name("buffersink"), "thumb_buffersink", nullptr, buffersinkParams.get(), m_pFilterGraph),
+ "Failed to create filter sink");
+ buffersinkParams.release();
+
+ AVFilterContext* yadifFilter = nullptr;
+ checkRc(avfilter_graph_create_filter(&yadifFilter, avfilter_get_by_name("yadif"), "thumb_deint", "deint=1", nullptr, m_pFilterGraph),
+ "Failed to create deinterlace filter");
+
+ std::stringstream scale;
+ scale << "w=" << width
+ << ":h=" << height
+ << ":sws_flags=bicubic";
+
+ AVFilterContext* scaleFilter = nullptr;
+ checkRc(avfilter_graph_create_filter(&scaleFilter, avfilter_get_by_name("scale"), "thumb_scale", scale.str().c_str(), nullptr, m_pFilterGraph),
+ "Failed to create scale filter");
- // auto yadifFilter = avfilter_get_by_name("yadif");
+ AVFilterContext* formatFilter = nullptr;
+ checkRc(avfilter_graph_create_filter(&formatFilter, avfilter_get_by_name("format"), "thumb_format", "pix_fmts=rgb24", nullptr, m_pFilterGraph),
+ "Failed to create scale filter");
- // AVFilterContext* filterContext;
- // avfilter_open(&filterContext, yadifFilter, nullptr);
- // if (avfilter_init_str(filterContext, "\"yadif=1:-1\"") < 0)
- // {
- // destroy();
- // throw logic_error("Failed to initialize filter");
- // }
+ checkRc(avfilter_link(formatFilter, 0, m_pFilterSink, 0), "Failed to link filters");
+ checkRc(avfilter_link(scaleFilter, 0, formatFilter, 0), "Failed to link filters");
+ checkRc(avfilter_link(yadifFilter, 0, scaleFilter, 0), "Failed to link filters");
+ checkRc(avfilter_link(m_pFilterSource, 0, yadifFilter, 0), "Failed to link filters");
+
+ checkRc(avfilter_graph_config(m_pFilterGraph, nullptr), "Failed to configure filter graph");
}
int MovieDecoder::getWidth()
@@ -229,15 +264,8 @@ void MovieDecoder::seek(int timeInSeconds)
timestamp = 0;
}
- int ret = av_seek_frame(m_pFormatContext, -1, timestamp, 0);
- if (ret >= 0)
- {
- avcodec_flush_buffers(m_pFormatContext->streams[m_VideoStream]->codec);
- }
- else
- {
- throw logic_error("Seeking in video failed");
- }
+ checkRc(av_seek_frame(m_pFormatContext, -1, timestamp, 0), "Seeking in video failed");
+ avcodec_flush_buffers(m_pFormatContext->streams[m_VideoStream]->codec);
int keyFrameAttempts = 0;
bool gotFrame = 0;
@@ -267,12 +295,11 @@ void MovieDecoder::seek(int timeInSeconds)
}
}
-
void MovieDecoder::decodeVideoFrame()
{
bool frameFinished = false;
- while(!frameFinished && getVideoPacket())
+ while (!frameFinished && getVideoPacket())
{
frameFinished = decodeVideoPacket();
}
@@ -336,71 +363,32 @@ bool MovieDecoder::getVideoPacket()
void MovieDecoder::getScaledVideoFrame(int scaledSize, bool maintainAspectRatio, VideoFrame& videoFrame)
{
- // TODO: deinterlacing
-
-
- int scaledWidth, scaledHeight;
- convertAndScaleFrame(AV_PIX_FMT_RGB24, scaledSize, maintainAspectRatio, scaledWidth, scaledHeight);
+ int scaledWidth = 0;
+ int scaledHeight = 0;
+ calculateDimensions(scaledSize, maintainAspectRatio, scaledWidth, scaledHeight);
- videoFrame.width = scaledWidth;
- videoFrame.height = scaledHeight;
- videoFrame.lineSize = m_pFrame->linesize[0];
+ initializeFilterGraph(m_pFormatContext->streams[m_VideoStream]->time_base, scaledWidth, scaledHeight);
- videoFrame.frameData.clear();
- videoFrame.frameData.resize(videoFrame.lineSize * videoFrame.height);
- memcpy((&(videoFrame.frameData.front())), m_pFrame->data[0], videoFrame.lineSize * videoFrame.height);
-}
+ checkRc(av_buffersrc_write_frame(m_pFilterSource, m_pFrame), "Failed to write frame tp filter graph");
+ decodeVideoFrame();
+ checkRc(av_buffersrc_write_frame(m_pFilterSource, m_pFrame), "Failed to write frame tp filter graph");
-void MovieDecoder::convertAndScaleFrame(AVPixelFormat format, int scaledSize, bool maintainAspectRatio, int& scaledWidth, int& scaledHeight)
-{
- calculateDimensions(scaledSize, maintainAspectRatio, scaledWidth, scaledHeight);
+ auto del = [] (AVFrame* f) { av_frame_free(&f); };
+ std::unique_ptr<AVFrame, decltype(del)> res(av_frame_alloc(), del);
+ checkRc(av_buffersink_get_frame(m_pFilterSink, res.get()), "Failed to get buffer from filter");
- SwsContext* scaleContext = sws_alloc_context();
- if (scaleContext == nullptr)
- {
- throw std::logic_error("Failed to allocate scale context");
- }
+ videoFrame.width = res->width;
+ videoFrame.height = res->height;
+ videoFrame.lineSize = res->linesize[0];
- av_opt_set_int(scaleContext, "srcw", m_pVideoCodecContext->width, 0);
- av_opt_set_int(scaleContext, "srch", m_pVideoCodecContext->height, 0);
- av_opt_set_int(scaleContext, "src_format", m_pVideoCodecContext->pix_fmt, 0);
- av_opt_set_int(scaleContext, "dstw", scaledWidth, 0);
- av_opt_set_int(scaleContext, "dsth", scaledHeight, 0);
- av_opt_set_int(scaleContext, "dst_format", format, 0);
- av_opt_set_int(scaleContext, "sws_flags", SWS_BICUBIC, 0);
+ videoFrame.frameData.resize(videoFrame.lineSize * videoFrame.height);
+ memcpy((videoFrame.frameData.data()), res->data[0], videoFrame.frameData.size());
- const int* coeff = sws_getCoefficients(SWS_CS_DEFAULT);
- if (sws_setColorspaceDetails(scaleContext, coeff, m_pVideoCodecContext->pix_fmt, coeff, format, 0, 1<<16, 1<<16) < 0)
- {
- sws_freeContext(scaleContext);
- throw std::logic_error("Failed to set colorspace details");
- }
- if (sws_init_context(scaleContext, nullptr, nullptr) < 0)
+ if (m_pFilterGraph)
{
- sws_freeContext(scaleContext);
- throw std::logic_error("Failed to initialise scale context");
+ avfilter_graph_free(&m_pFilterGraph);
}
-
- if (nullptr == scaleContext)
- {
- throw logic_error("Failed to create resize context");
- }
-
- AVFrame* convertedFrame = nullptr;
- uint8_t* convertedFrameBuffer = nullptr;
-
- createAVFrame(&convertedFrame, &convertedFrameBuffer, scaledWidth, scaledHeight, format);
-
- sws_scale(scaleContext, m_pFrame->data, m_pFrame->linesize, 0, m_pVideoCodecContext->height,
- convertedFrame->data, convertedFrame->linesize);
- sws_freeContext(scaleContext);
-
- av_free(m_pFrame);
- av_free(m_pFrameBuffer);
-
- m_pFrame = convertedFrame;
- m_pFrameBuffer = convertedFrameBuffer;
}
void MovieDecoder::calculateDimensions(int squareSize, bool maintainAspectRatio, int& destWidth, int& destHeight)
@@ -444,14 +432,15 @@ void MovieDecoder::calculateDimensions(int squareSize, bool maintainAspectRatio,
}
}
-void MovieDecoder::createAVFrame(AVFrame** pAvFrame, uint8_t** pFrameBuffer, int width, int height, AVPixelFormat format)
+void MovieDecoder::checkRc(int ret, const std::string& message)
{
- *pAvFrame = av_frame_alloc();
- auto* avPicture = reinterpret_cast<AVPicture*>(*pAvFrame);
-
- int numBytes = av_image_get_buffer_size(format, width, height, 1);
- *pFrameBuffer = reinterpret_cast<uint8_t*>(av_malloc(numBytes));
- av_image_fill_arrays(avPicture->data, avPicture->linesize, *pFrameBuffer, format, width, height, 1);
+ if (ret < 0)
+ {
+ char buf[256];
+ buf[0] = ' ';
+ av_strerror(ret, &buf[1], sizeof(buf) - 1);
+ throw std::logic_error(message + buf);
+ }
}
}
diff --git a/libffmpegthumbnailer/moviedecoder.h b/libffmpegthumbnailer/moviedecoder.h
index 19581b2..638d7d1 100644
--- a/libffmpegthumbnailer/moviedecoder.h
+++ b/libffmpegthumbnailer/moviedecoder.h
@@ -31,6 +31,9 @@ extern "C" {
#include <libavformat/avformat.h>
}
+struct AVFilterGraph;
+struct AVFilterContext;
+
namespace ffmpegthumbnailer
{
@@ -56,24 +59,25 @@ class MovieDecoder
private:
void initializeVideo();
- void initializeFilterGraph();
+ void initializeFilterGraph(AVRational timeBase, int width, int height);
bool decodeVideoPacket();
bool getVideoPacket();
- void convertAndScaleFrame(AVPixelFormat format, int scaledSize, bool maintainAspectRatio, int& scaledWidth, int& scaledHeight);
- void createAVFrame(AVFrame** pAvFrame, uint8_t** pFrameBuffer, int width, int height, AVPixelFormat format);
void calculateDimensions(int squareSize, bool maintainAspectRatio, int& destWidth, int& destHeight);
void addLogCallback();
+ void checkRc(int ret, const std::string& message);
private:
int m_VideoStream;
AVFormatContext* m_pFormatContext;
AVCodecContext* m_pVideoCodecContext;
AVCodec* m_pVideoCodec;
+ AVFilterGraph* m_pFilterGraph;
+ AVFilterContext* m_pFilterSource;
+ AVFilterContext* m_pFilterSink;
AVStream* m_pVideoStream;
AVFrame* m_pFrame;
- uint8_t* m_pFrameBuffer;
AVPacket* m_pPacket;
bool m_FormatContextWasGiven;
bool m_AllowSeek;
|