2009-07-16 16:48:47 +02:00
|
|
|
// Copyright (c) 2008-2009, Karl Blomster
|
2008-09-03 19:03:20 +02:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file video_provider_ffmpegsource.cpp
|
|
|
|
/// @brief FFmpegSource2-based video provider
|
|
|
|
/// @ingroup video_input ffms
|
|
|
|
///
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2011-12-22 22:25:49 +01:00
|
|
|
#ifdef WITH_FFMS2
|
2014-03-24 15:05:01 +01:00
|
|
|
#include "ffmpegsource_common.h"
|
|
|
|
#include "include/aegisub/video_provider.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2010-12-31 22:03:11 +01:00
|
|
|
#include "utils.h"
|
2013-07-01 05:15:43 +02:00
|
|
|
#include "video_frame.h"
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2013-10-17 16:23:45 +02:00
|
|
|
namespace {
|
2018-11-22 01:41:05 +01:00
|
|
|
typedef enum AGI_ColorSpaces {
|
|
|
|
AGI_CS_RGB = 0,
|
|
|
|
AGI_CS_BT709 = 1,
|
|
|
|
AGI_CS_UNSPECIFIED = 2,
|
|
|
|
AGI_CS_FCC = 4,
|
|
|
|
AGI_CS_BT470BG = 5,
|
|
|
|
AGI_CS_SMPTE170M = 6,
|
|
|
|
AGI_CS_SMPTE240M = 7,
|
|
|
|
AGI_CS_YCOCG = 8,
|
|
|
|
AGI_CS_BT2020_NCL = 9,
|
|
|
|
AGI_CS_BT2020_CL = 10,
|
|
|
|
AGI_CS_SMPTE2085 = 11,
|
|
|
|
AGI_CS_CHROMATICITY_DERIVED_NCL = 12,
|
|
|
|
AGI_CS_CHROMATICITY_DERIVED_CL = 13,
|
|
|
|
AGI_CS_ICTCP = 14
|
|
|
|
} AGI_ColorSpaces;
|
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
/// @class FFmpegSourceVideoProvider
|
|
|
|
/// @brief Implements video loading through the FFMS library.
|
|
|
|
class FFmpegSourceVideoProvider final : public VideoProvider, FFmpegSourceProvider {
|
|
|
|
/// video source object
|
|
|
|
agi::scoped_holder<FFMS_VideoSource*, void (FFMS_CC*)(FFMS_VideoSource*)> VideoSource;
|
|
|
|
const FFMS_VideoProperties *VideoInfo = nullptr; ///< video properties
|
|
|
|
|
|
|
|
int Width = -1; ///< width in pixels
|
|
|
|
int Height = -1; ///< height in pixels
|
2014-05-20 04:21:50 +02:00
|
|
|
int CS = -1; ///< Reported colorspace of first frame
|
|
|
|
int CR = -1; ///< Reported colorrange of first frame
|
2014-03-24 15:05:01 +01:00
|
|
|
double DAR; ///< display aspect ratio
|
|
|
|
std::vector<int> KeyFramesList; ///< list of keyframes
|
|
|
|
agi::vfr::Framerate Timecodes; ///< vfr object
|
|
|
|
std::string ColorSpace; ///< Colorspace name
|
2014-05-20 01:47:54 +02:00
|
|
|
std::string RealColorSpace; ///< Colorspace name
|
2014-03-24 15:05:01 +01:00
|
|
|
|
|
|
|
char FFMSErrMsg[1024]; ///< FFMS error message
|
|
|
|
FFMS_ErrorInfo ErrInfo; ///< FFMS error codes/messages
|
2014-05-23 15:34:52 +02:00
|
|
|
bool has_audio = false;
|
2014-03-24 15:05:01 +01:00
|
|
|
|
|
|
|
void LoadVideo(agi::fs::path const& filename, std::string const& colormatrix);
|
|
|
|
|
|
|
|
public:
|
2014-03-25 17:51:38 +01:00
|
|
|
FFmpegSourceVideoProvider(agi::fs::path const& filename, std::string const& colormatrix, agi::BackgroundRunner *br);
|
2014-03-24 15:05:01 +01:00
|
|
|
|
2014-06-12 23:34:21 +02:00
|
|
|
void GetFrame(int n, VideoFrame &out) override;
|
2014-03-24 15:05:01 +01:00
|
|
|
|
2014-05-20 04:21:50 +02:00
|
|
|
void SetColorSpace(std::string const& matrix) override {
|
|
|
|
#if FFMS_VERSION >= ((2 << 24) | (17 << 16) | (1 << 8) | 0)
|
|
|
|
if (matrix == ColorSpace) return;
|
|
|
|
if (matrix == RealColorSpace)
|
|
|
|
FFMS_SetInputFormatV(VideoSource, CS, CR, FFMS_GetPixFmt(""), nullptr);
|
|
|
|
else if (matrix == "TV.601")
|
2018-11-22 01:41:05 +01:00
|
|
|
FFMS_SetInputFormatV(VideoSource, AGI_CS_BT470BG, CR, FFMS_GetPixFmt(""), nullptr);
|
2014-05-20 04:21:50 +02:00
|
|
|
else
|
|
|
|
return;
|
|
|
|
ColorSpace = matrix;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-23 15:34:52 +02:00
|
|
|
int GetFrameCount() const override { return VideoInfo->NumFrames; }
|
|
|
|
int GetWidth() const override { return Width; }
|
|
|
|
int GetHeight() const override { return Height; }
|
|
|
|
double GetDAR() const override { return DAR; }
|
|
|
|
agi::vfr::Framerate GetFPS() const override { return Timecodes; }
|
|
|
|
std::string GetColorSpace() const override { return ColorSpace; }
|
2014-05-20 01:47:54 +02:00
|
|
|
std::string GetRealColorSpace() const override { return RealColorSpace; }
|
2014-03-24 15:05:01 +01:00
|
|
|
std::vector<int> GetKeyFrames() const override { return KeyFramesList; };
|
2014-05-23 15:34:52 +02:00
|
|
|
std::string GetDecoderName() const override { return "FFmpegSource"; }
|
|
|
|
bool WantsCaching() const override { return true; }
|
|
|
|
bool HasAudio() const override { return has_audio; }
|
2014-03-24 15:05:01 +01:00
|
|
|
};
|
|
|
|
|
2013-10-17 16:23:45 +02:00
|
|
|
std::string colormatrix_description(int cs, int cr) {
|
|
|
|
// Assuming TV for unspecified
|
|
|
|
std::string str = cr == FFMS_CR_JPEG ? "PC" : "TV";
|
|
|
|
|
|
|
|
switch (cs) {
|
2018-11-22 01:41:05 +01:00
|
|
|
case AGI_CS_RGB:
|
2013-10-17 16:23:45 +02:00
|
|
|
return "None";
|
2018-11-22 01:41:05 +01:00
|
|
|
case AGI_CS_BT709:
|
2013-10-17 16:23:45 +02:00
|
|
|
return str + ".709";
|
2018-11-22 01:41:05 +01:00
|
|
|
case AGI_CS_FCC:
|
2013-10-17 16:23:45 +02:00
|
|
|
return str + ".FCC";
|
2018-11-22 01:41:05 +01:00
|
|
|
case AGI_CS_BT470BG:
|
|
|
|
case AGI_CS_SMPTE170M:
|
2013-10-17 16:23:45 +02:00
|
|
|
return str + ".601";
|
2018-11-22 01:41:05 +01:00
|
|
|
case AGI_CS_SMPTE240M:
|
2013-10-17 16:23:45 +02:00
|
|
|
return str + ".240M";
|
|
|
|
default:
|
|
|
|
throw VideoOpenError("Unknown video color space");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 17:51:38 +01:00
|
|
|
FFmpegSourceVideoProvider::FFmpegSourceVideoProvider(agi::fs::path const& filename, std::string const& colormatrix, agi::BackgroundRunner *br) try
|
|
|
|
: FFmpegSourceProvider(br)
|
|
|
|
, VideoSource(nullptr, FFMS_DestroyVideoSource)
|
2010-08-02 08:32:01 +02:00
|
|
|
{
|
2009-09-26 23:58:00 +02:00
|
|
|
ErrInfo.Buffer = FFMSErrMsg;
|
|
|
|
ErrInfo.BufferSize = sizeof(FFMSErrMsg);
|
|
|
|
ErrInfo.ErrorType = FFMS_ERROR_SUCCESS;
|
|
|
|
ErrInfo.SubType = FFMS_ERROR_SUCCESS;
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2009-07-24 03:41:16 +02:00
|
|
|
SetLogLevel();
|
|
|
|
|
2013-10-17 16:23:45 +02:00
|
|
|
LoadVideo(filename, colormatrix);
|
2008-09-03 19:03:20 +02:00
|
|
|
}
|
2014-06-11 00:28:45 +02:00
|
|
|
catch (agi::EnvironmentError const& err) {
|
|
|
|
throw VideoOpenError(err.GetMessage());
|
2008-09-03 19:03:20 +02:00
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2013-10-17 16:23:45 +02:00
|
|
|
void FFmpegSourceVideoProvider::LoadVideo(agi::fs::path const& filename, std::string const& colormatrix) {
|
2013-01-04 16:01:50 +01:00
|
|
|
FFMS_Indexer *Indexer = FFMS_CreateIndexer(filename.string().c_str(), &ErrInfo);
|
2012-12-17 19:11:33 +01:00
|
|
|
if (!Indexer) {
|
|
|
|
if (ErrInfo.SubType == FFMS_ERROR_FILE_READ)
|
2013-01-04 16:01:50 +01:00
|
|
|
throw agi::fs::FileNotFound(std::string(ErrInfo.Buffer));
|
2012-12-17 19:11:33 +01:00
|
|
|
else
|
|
|
|
throw VideoNotSupported(ErrInfo.Buffer);
|
|
|
|
}
|
2009-07-16 16:48:47 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::map<int, std::string> TrackList = GetTracksOfType(Indexer, FFMS_TYPE_VIDEO);
|
2009-07-16 16:48:47 +02:00
|
|
|
if (TrackList.size() <= 0)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoNotSupported("no video tracks found");
|
2009-07-16 16:48:47 +02:00
|
|
|
|
|
|
|
int TrackNumber = -1;
|
|
|
|
if (TrackList.size() > 1) {
|
2016-04-06 20:24:21 +02:00
|
|
|
auto Selection = AskForTrackSelection(TrackList, FFMS_TYPE_VIDEO);
|
|
|
|
if (Selection == TrackSelection::None)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw agi::UserCancelException("video loading cancelled by user");
|
2016-04-06 20:24:21 +02:00
|
|
|
TrackNumber = static_cast<int>(Selection);
|
2009-07-16 16:48:47 +02:00
|
|
|
}
|
|
|
|
|
2008-09-03 19:03:20 +02:00
|
|
|
// generate a name for the cache file
|
2013-01-04 16:01:50 +01:00
|
|
|
auto CacheName = GetCacheFilename(filename);
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2008-09-05 00:17:34 +02:00
|
|
|
// try to read index
|
2012-02-20 19:22:12 +01:00
|
|
|
agi::scoped_holder<FFMS_Index*, void (FFMS_CC*)(FFMS_Index*)>
|
2013-01-04 16:01:50 +01:00
|
|
|
Index(FFMS_ReadIndex(CacheName.string().c_str(), &ErrInfo), FFMS_DestroyIndex);
|
2012-02-20 19:22:12 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
if (Index && FFMS_IndexBelongsToFile(Index, filename.string().c_str(), &ErrInfo))
|
2012-11-13 17:51:01 +01:00
|
|
|
Index = nullptr;
|
2009-07-14 00:30:48 +02:00
|
|
|
|
2009-07-16 16:48:47 +02:00
|
|
|
// time to examine the index and check if the track we want is indexed
|
|
|
|
// technically this isn't really needed since all video tracks should always be indexed,
|
|
|
|
// but a bit of sanity checking never hurt anyone
|
2012-02-20 19:22:12 +01:00
|
|
|
if (Index && TrackNumber >= 0) {
|
2009-09-26 23:58:00 +02:00
|
|
|
FFMS_Track *TempTrackData = FFMS_GetTrackFromIndex(Index, TrackNumber);
|
2012-02-20 19:22:12 +01:00
|
|
|
if (FFMS_GetNumFrames(TempTrackData) <= 0)
|
2012-11-13 17:51:01 +01:00
|
|
|
Index = nullptr;
|
2009-07-16 16:48:47 +02:00
|
|
|
}
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2009-07-16 16:48:47 +02:00
|
|
|
// moment of truth
|
2012-02-20 19:22:12 +01:00
|
|
|
if (!Index) {
|
2016-04-06 20:24:21 +02:00
|
|
|
auto TrackMask = TrackSelection::None;
|
2012-02-28 02:22:29 +01:00
|
|
|
if (OPT_GET("Provider/FFmpegSource/Index All Tracks")->GetBool() || OPT_GET("Video/Open Audio")->GetBool())
|
2016-04-06 20:24:21 +02:00
|
|
|
TrackMask = TrackSelection::All;
|
2012-06-30 17:27:11 +02:00
|
|
|
Index = DoIndexing(Indexer, CacheName, TrackMask, GetErrorHandlingMode());
|
2008-09-03 19:03:20 +02:00
|
|
|
}
|
2012-07-23 02:44:47 +02:00
|
|
|
else {
|
|
|
|
FFMS_CancelIndexing(Indexer);
|
|
|
|
}
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2009-05-03 20:05:30 +02:00
|
|
|
// update access time of index file so it won't get cleaned away
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::Touch(CacheName);
|
2009-05-03 20:05:30 +02:00
|
|
|
|
|
|
|
// we have now read the index and may proceed with cleaning the index cache
|
2012-04-04 00:44:40 +02:00
|
|
|
CleanCache();
|
2009-05-03 20:05:30 +02:00
|
|
|
|
2009-07-16 16:48:47 +02:00
|
|
|
// track number still not set?
|
|
|
|
if (TrackNumber < 0) {
|
|
|
|
// just grab the first track
|
2009-09-26 23:58:00 +02:00
|
|
|
TrackNumber = FFMS_GetFirstIndexedTrackOfType(Index, FFMS_TYPE_VIDEO, &ErrInfo);
|
2012-02-20 19:22:12 +01:00
|
|
|
if (TrackNumber < 0)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoNotSupported(std::string("Couldn't find any video tracks: ") + ErrInfo.Buffer);
|
2009-07-16 16:48:47 +02:00
|
|
|
}
|
|
|
|
|
2014-05-23 15:34:52 +02:00
|
|
|
// Check if there's an audio track
|
|
|
|
has_audio = FFMS_GetFirstTrackOfType(Index, FFMS_TYPE_AUDIO, nullptr) != -1;
|
|
|
|
|
2008-09-03 23:22:33 +02:00
|
|
|
// set thread count
|
2010-05-21 03:13:36 +02:00
|
|
|
int Threads = OPT_GET("Provider/Video/FFmpegSource/Decoding Threads")->GetInt();
|
2018-11-22 01:41:05 +01:00
|
|
|
#if FFMS_VERSION < ((2 << 24) | (30 << 16) | (0 << 8) | 0)
|
2012-06-16 06:10:32 +02:00
|
|
|
if (FFMS_GetVersion() < ((2 << 24) | (17 << 16) | (2 << 8) | 1) && FFMS_GetSourceType(Index) == FFMS_SOURCE_LAVF)
|
2012-05-11 04:58:34 +02:00
|
|
|
Threads = 1;
|
2018-11-22 01:41:05 +01:00
|
|
|
#endif
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2008-09-03 23:22:33 +02:00
|
|
|
// set seekmode
|
|
|
|
// TODO: give this its own option?
|
|
|
|
int SeekMode;
|
2010-06-03 07:07:10 +02:00
|
|
|
if (OPT_GET("Provider/Video/FFmpegSource/Unsafe Seeking")->GetBool())
|
2009-04-26 01:08:45 +02:00
|
|
|
SeekMode = FFMS_SEEK_UNSAFE;
|
2012-03-25 06:05:06 +02:00
|
|
|
else
|
2009-04-26 01:08:45 +02:00
|
|
|
SeekMode = FFMS_SEEK_NORMAL;
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
VideoSource = FFMS_CreateVideoSource(filename.string().c_str(), TrackNumber, Index, Threads, SeekMode, &ErrInfo);
|
2012-02-20 19:22:12 +01:00
|
|
|
if (!VideoSource)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError(std::string("Failed to open video track: ") + ErrInfo.Buffer);
|
2008-09-03 19:03:20 +02:00
|
|
|
|
|
|
|
// load video properties
|
2009-05-18 00:12:46 +02:00
|
|
|
VideoInfo = FFMS_GetVideoProperties(VideoSource);
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2009-09-26 23:58:00 +02:00
|
|
|
const FFMS_Frame *TempFrame = FFMS_GetFrame(VideoSource, 0, &ErrInfo);
|
2012-02-23 02:30:59 +01:00
|
|
|
if (!TempFrame)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError(std::string("Failed to decode first frame: ") + ErrInfo.Buffer);
|
2012-02-23 02:30:59 +01:00
|
|
|
|
|
|
|
Width = TempFrame->EncodedWidth;
|
|
|
|
Height = TempFrame->EncodedHeight;
|
|
|
|
if (VideoInfo->SARDen > 0 && VideoInfo->SARNum > 0)
|
2012-03-12 00:51:04 +01:00
|
|
|
DAR = double(Width) * VideoInfo->SARNum / ((double)Height * VideoInfo->SARDen);
|
2012-02-23 02:30:59 +01:00
|
|
|
else
|
|
|
|
DAR = double(Width) / Height;
|
2009-09-26 23:58:00 +02:00
|
|
|
|
2017-02-15 17:21:39 +01:00
|
|
|
int VideoCS = CS = TempFrame->ColorSpace;
|
2014-05-20 04:21:50 +02:00
|
|
|
CR = TempFrame->ColorRange;
|
|
|
|
|
2018-11-22 01:41:05 +01:00
|
|
|
if (CS == AGI_CS_UNSPECIFIED)
|
|
|
|
CS = Width > 1024 || Height >= 600 ? AGI_CS_BT709 : AGI_CS_BT470BG;
|
2014-05-20 04:21:50 +02:00
|
|
|
RealColorSpace = ColorSpace = colormatrix_description(CS, CR);
|
2012-05-11 16:38:44 +02:00
|
|
|
|
2012-03-07 05:25:46 +01:00
|
|
|
#if FFMS_VERSION >= ((2 << 24) | (17 << 16) | (1 << 8) | 0)
|
2018-03-25 15:30:29 +02:00
|
|
|
if (CS != AGI_CS_RGB && CS != AGI_CS_BT470BG && ColorSpace != colormatrix && colormatrix == "TV.601") {
|
2018-11-22 01:41:05 +01:00
|
|
|
CS = AGI_CS_BT470BG;
|
|
|
|
ColorSpace = colormatrix_description(AGI_CS_BT470BG, CR);
|
2012-03-07 05:25:46 +01:00
|
|
|
}
|
2017-02-15 17:21:39 +01:00
|
|
|
|
|
|
|
if (CS != VideoCS) {
|
|
|
|
if (FFMS_SetInputFormatV(VideoSource, CS, CR, FFMS_GetPixFmt(""), &ErrInfo))
|
|
|
|
throw VideoOpenError(std::string("Failed to set input format: ") + ErrInfo.Buffer);
|
|
|
|
}
|
2012-03-07 05:25:46 +01:00
|
|
|
#endif
|
|
|
|
|
2011-10-17 19:52:11 +02:00
|
|
|
const int TargetFormat[] = { FFMS_GetPixFmt("bgra"), -1 };
|
2014-05-20 04:21:50 +02:00
|
|
|
if (FFMS_SetOutputFormatV2(VideoSource, TargetFormat, Width, Height, FFMS_RESIZER_BICUBIC, &ErrInfo))
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError(std::string("Failed to set output format: ") + ErrInfo.Buffer);
|
2009-07-20 05:50:25 +02:00
|
|
|
|
2008-09-03 19:03:20 +02:00
|
|
|
// get frame info data
|
2009-09-26 23:58:00 +02:00
|
|
|
FFMS_Track *FrameData = FFMS_GetTrackFromVideo(VideoSource);
|
2012-11-13 17:51:01 +01:00
|
|
|
if (FrameData == nullptr)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("failed to get frame data");
|
2009-09-26 23:58:00 +02:00
|
|
|
const FFMS_TrackTimeBase *TimeBase = FFMS_GetTimeBase(FrameData);
|
2012-11-13 17:51:01 +01:00
|
|
|
if (TimeBase == nullptr)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("failed to get track time base");
|
2008-09-03 22:27:50 +02:00
|
|
|
|
|
|
|
// build list of keyframes and timecodes
|
2010-07-08 06:29:04 +02:00
|
|
|
std::vector<int> TimecodesVector;
|
2008-09-03 19:03:20 +02:00
|
|
|
for (int CurFrameNum = 0; CurFrameNum < VideoInfo->NumFrames; CurFrameNum++) {
|
2013-01-04 16:01:50 +01:00
|
|
|
const FFMS_FrameInfo *CurFrameData = FFMS_GetFrameInfo(FrameData, CurFrameNum);
|
|
|
|
if (!CurFrameData)
|
|
|
|
throw VideoOpenError("Couldn't get info about frame " + std::to_string(CurFrameNum));
|
2008-09-03 22:27:50 +02:00
|
|
|
|
|
|
|
// keyframe?
|
|
|
|
if (CurFrameData->KeyFrame)
|
2010-07-08 06:29:04 +02:00
|
|
|
KeyFramesList.push_back(CurFrameNum);
|
2008-09-03 22:27:50 +02:00
|
|
|
|
|
|
|
// calculate timestamp and add to timecodes vector
|
2009-11-21 22:15:02 +01:00
|
|
|
int Timestamp = (int)((CurFrameData->PTS * TimeBase->Num) / TimeBase->Den);
|
2008-09-06 04:54:22 +02:00
|
|
|
TimecodesVector.push_back(Timestamp);
|
2008-09-03 19:03:20 +02:00
|
|
|
}
|
2011-09-29 22:27:23 +02:00
|
|
|
if (TimecodesVector.size() < 2)
|
|
|
|
Timecodes = 25.0;
|
|
|
|
else
|
|
|
|
Timecodes = agi::vfr::Framerate(TimecodesVector);
|
2008-09-03 19:03:20 +02:00
|
|
|
}
|
|
|
|
|
2014-06-12 23:34:21 +02:00
|
|
|
void FFmpegSourceVideoProvider::GetFrame(int n, VideoFrame &out) {
|
2013-07-01 05:15:43 +02:00
|
|
|
n = mid(0, n, GetFrameCount() - 1);
|
2008-09-10 23:05:54 +02:00
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
auto frame = FFMS_GetFrame(VideoSource, n, &ErrInfo);
|
|
|
|
if (!frame)
|
2012-03-12 00:51:10 +01:00
|
|
|
throw VideoDecodeError(std::string("Failed to retrieve frame: ") + ErrInfo.Buffer);
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2014-06-12 23:34:21 +02:00
|
|
|
out.data.assign(frame->Data[0], frame->Data[0] + frame->Linesize[0] * Height);
|
|
|
|
out.flipped = false;
|
|
|
|
out.width = Width;
|
|
|
|
out.height = Height;
|
|
|
|
out.pitch = frame->Linesize[0];
|
2008-09-03 19:03:20 +02:00
|
|
|
}
|
2014-03-24 15:05:01 +01:00
|
|
|
}
|
|
|
|
|
2014-03-25 17:51:38 +01:00
|
|
|
std::unique_ptr<VideoProvider> CreateFFmpegSourceVideoProvider(agi::fs::path const& path, std::string const& colormatrix, agi::BackgroundRunner *br) {
|
2014-04-23 22:53:24 +02:00
|
|
|
return agi::make_unique<FFmpegSourceVideoProvider>(path, colormatrix, br);
|
2014-03-24 15:05:01 +01:00
|
|
|
}
|
2012-01-24 00:07:35 +01:00
|
|
|
|
2011-12-22 22:25:49 +01:00
|
|
|
#endif /* WITH_FFMS2 */
|