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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
// Copyright (C) 2010 Dirk Vanden Boer <dirk.vdb@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "moviedecoder.h"
#include "videoframe.h"
#include <stdexcept>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <array>
#include <sstream>
#include <memory>
extern "C" {
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
}
using namespace std;
namespace ffmpegthumbnailer
{
struct SilenceLogLevel
{
SilenceLogLevel() { av_log_set_level(AV_LOG_QUIET); }
};
MovieDecoder::MovieDecoder(const string& filename, AVFormatContext* pavContext)
: m_VideoStream(-1)
, 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_pPacket(nullptr)
, m_FormatContextWasGiven(pavContext != nullptr)
, m_AllowSeek(true)
{
initialize(filename);
}
MovieDecoder::~MovieDecoder()
{
destroy();
}
void MovieDecoder::initialize(const string& filename)
{
av_register_all();
avcodec_register_all();
avformat_network_init();
string inputFile = filename == "-" ? "pipe:" : filename;
m_AllowSeek = (filename != "-") && (filename.find("rtsp://") != 0) && (filename.find("udp://") != 0);
if ((!m_FormatContextWasGiven) && avformat_open_input(&m_pFormatContext, inputFile.c_str(), nullptr, nullptr) != 0)
{
destroy();
throw logic_error(string("Could not open input file: ") + filename);
}
if (avformat_find_stream_info(m_pFormatContext, nullptr) < 0)
{
destroy();
throw logic_error("Could not find stream information");
}
initializeVideo();
m_pFrame = av_frame_alloc();
}
void MovieDecoder::destroy()
{
if (m_pVideoCodecContext)
{
avcodec_close(m_pVideoCodecContext);
m_pVideoCodecContext = nullptr;
}
if ((!m_FormatContextWasGiven) && m_pFormatContext)
{
avformat_close_input(&m_pFormatContext);
}
if (m_pPacket)
{
av_packet_unref(m_pPacket);
delete m_pPacket;
m_pPacket = nullptr;
}
if (m_pFrame)
{
av_frame_free(&m_pFrame);
}
m_VideoStream = -1;
avformat_network_deinit();
}
string MovieDecoder::getCodec()
{
if (m_pVideoCodec)
{
return m_pVideoCodec->name;
}
return "";
}
void MovieDecoder::initializeVideo()
{
for (unsigned int i = 0; i < m_pFormatContext->nb_streams; ++i)
{
if (m_pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
m_pVideoStream = m_pFormatContext->streams[i];
m_VideoStream = i;
break;
}
}
if (m_VideoStream == -1)
{
destroy();
throw logic_error("Could not find video stream");
}
m_pVideoCodecContext = m_pFormatContext->streams[m_VideoStream]->codec;
m_pVideoCodec = avcodec_find_decoder(m_pVideoCodecContext->codec_id);
if (m_pVideoCodec == nullptr)
{
// set to nullptr, otherwise avcodec_close(m_pVideoCodecContext) crashes
m_pVideoCodecContext = nullptr;
destroy();
throw logic_error("Video Codec not found");
}
m_pVideoCodecContext->workaround_bugs = 1;
if (avcodec_open2(m_pVideoCodecContext, m_pVideoCodec, nullptr) < 0)
{
destroy();
throw logic_error("Could not open video codec");
}
}
void MovieDecoder::initializeFilterGraph(AVRational timeBase, int width, int height)
{
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");
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");
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()
{
if (m_pVideoCodecContext)
{
return m_pVideoCodecContext->width;
}
return -1;
}
int MovieDecoder::getHeight()
{
if (m_pVideoCodecContext)
{
return m_pVideoCodecContext->height;
}
return -1;
}
int MovieDecoder::getDuration()
{
if (m_pFormatContext)
{
return static_cast<int>(m_pFormatContext->duration / AV_TIME_BASE);
}
return 0;
}
void MovieDecoder::seek(int timeInSeconds)
{
if (!m_AllowSeek)
{
return;
}
int64_t timestamp = AV_TIME_BASE * static_cast<int64_t>(timeInSeconds);
if (timestamp < 0)
{
timestamp = 0;
}
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;
do
{
int count = 0;
gotFrame = 0;
while (!gotFrame && count < 20)
{
getVideoPacket();
try
{
gotFrame = decodeVideoPacket();
}
catch(logic_error&) {}
++count;
}
++keyFrameAttempts;
} while ((!gotFrame || !m_pFrame->key_frame) && keyFrameAttempts < 200);
if (gotFrame == 0)
{
throw logic_error("Seeking in video failed");
}
}
void MovieDecoder::decodeVideoFrame()
{
bool frameFinished = false;
while (!frameFinished && getVideoPacket())
{
frameFinished = decodeVideoPacket();
}
if (!frameFinished)
{
throw logic_error("decodeVideoFrame() failed: frame not finished");
}
}
bool MovieDecoder::decodeVideoPacket()
{
if (m_pPacket->stream_index != m_VideoStream)
{
return false;
}
av_frame_unref(m_pFrame);
int frameFinished;
int bytesDecoded = avcodec_decode_video2(m_pVideoCodecContext, m_pFrame, &frameFinished, m_pPacket);
if (bytesDecoded < 0)
{
throw logic_error("Failed to decode video frame: bytesDecoded < 0");
}
return frameFinished > 0;
}
bool MovieDecoder::getVideoPacket()
{
bool framesAvailable = true;
bool frameDecoded = false;
int attempts = 0;
if (m_pPacket)
{
av_packet_unref(m_pPacket);
delete m_pPacket;
}
m_pPacket = new AVPacket();
while (framesAvailable && !frameDecoded && (attempts++ < 1000))
{
framesAvailable = av_read_frame(m_pFormatContext, m_pPacket) >= 0;
if (framesAvailable)
{
frameDecoded = m_pPacket->stream_index == m_VideoStream;
if (!frameDecoded)
{
av_packet_unref(m_pPacket);
}
}
}
return frameDecoded;
}
void MovieDecoder::getScaledVideoFrame(int scaledSize, bool maintainAspectRatio, VideoFrame& videoFrame)
{
int scaledWidth = 0;
int scaledHeight = 0;
calculateDimensions(scaledSize, maintainAspectRatio, scaledWidth, scaledHeight);
initializeFilterGraph(m_pFormatContext->streams[m_VideoStream]->time_base, scaledWidth, scaledHeight);
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");
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");
videoFrame.width = res->width;
videoFrame.height = res->height;
videoFrame.lineSize = res->linesize[0];
videoFrame.frameData.resize(videoFrame.lineSize * videoFrame.height);
memcpy((videoFrame.frameData.data()), res->data[0], videoFrame.frameData.size());
if (m_pFilterGraph)
{
avfilter_graph_free(&m_pFilterGraph);
}
}
void MovieDecoder::calculateDimensions(int squareSize, bool maintainAspectRatio, int& destWidth, int& destHeight)
{
AVRational par = av_guess_sample_aspect_ratio(m_pFormatContext, m_pVideoStream, m_pFrame);
// if the pixel aspect ratio is defined and is not 1, we have an anamorphic stream
bool anamorphic = par.num != 0 && par.num != par.den;
if (squareSize == 0)
{
// use original video size
squareSize = max(m_pVideoCodecContext->width, m_pVideoCodecContext->height);
}
if (!maintainAspectRatio)
{
destWidth = squareSize;
destHeight = squareSize;
}
else
{
int srcWidth = m_pVideoCodecContext->width;
int srcHeight = m_pVideoCodecContext->height;
if (anamorphic)
{
srcWidth = srcWidth * par.num / par.den;
}
if (srcWidth > srcHeight)
{
destWidth = squareSize;
destHeight = static_cast<int>(static_cast<float>(squareSize) / srcWidth * srcHeight);
}
else
{
destWidth = static_cast<int>(static_cast<float>(squareSize) / srcHeight * srcWidth);
destHeight = squareSize;
}
}
}
void MovieDecoder::checkRc(int ret, const std::string& message)
{
if (ret < 0)
{
char buf[256];
buf[0] = ' ';
av_strerror(ret, &buf[1], sizeof(buf) - 1);
throw std::logic_error(message + buf);
}
}
}
|