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
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-12-22 22:25:49 +01:00
|
|
|
#ifdef WITH_FFMS2
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include <objbase.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2009-07-16 16:48:47 +02:00
|
|
|
#include <wx/choicdlg.h>
|
2009-09-10 12:26:50 +02:00
|
|
|
#include <wx/msgdlg.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#include <wx/utils.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "aegisub_endian.h"
|
2010-08-02 08:32:01 +02:00
|
|
|
#include "compat.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "main.h"
|
2010-12-31 22:03:11 +01:00
|
|
|
#include "utils.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "video_context.h"
|
|
|
|
#include "video_provider_ffmpegsource.h"
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Constructor
|
2009-09-26 23:58:00 +02:00
|
|
|
/// @param filename The filename to open
|
2012-02-20 19:22:12 +01:00
|
|
|
FFmpegSourceVideoProvider::FFmpegSourceVideoProvider(wxString filename) try
|
2012-11-13 17:51:01 +01:00
|
|
|
: VideoSource(nullptr, FFMS_DestroyVideoSource)
|
|
|
|
, VideoInfo(nullptr)
|
2010-08-02 08:32:01 +02:00
|
|
|
, Width(-1)
|
|
|
|
, Height(-1)
|
|
|
|
, FrameNumber(-1)
|
|
|
|
{
|
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();
|
|
|
|
|
2008-09-03 19:03:20 +02:00
|
|
|
// and here we go
|
2012-02-20 19:22:12 +01:00
|
|
|
LoadVideo(filename);
|
2008-09-03 19:03:20 +02:00
|
|
|
}
|
2012-02-20 19:22:12 +01:00
|
|
|
catch (wxString const& err) {
|
|
|
|
throw VideoOpenError(STD_STR(err));
|
|
|
|
}
|
|
|
|
catch (const char * err) {
|
|
|
|
throw VideoOpenError(err);
|
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
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Opens video
|
2009-09-26 23:58:00 +02:00
|
|
|
/// @param filename The filename to open
|
2009-07-23 17:16:53 +02:00
|
|
|
void FFmpegSourceVideoProvider::LoadVideo(wxString filename) {
|
2012-03-25 06:05:06 +02:00
|
|
|
wxString FileNameShort = wxFileName(filename).GetShortPath();
|
2008-09-03 19:03:20 +02:00
|
|
|
|
2009-09-26 23:58:00 +02:00
|
|
|
FFMS_Indexer *Indexer = FFMS_CreateIndexer(FileNameShort.utf8_str(), &ErrInfo);
|
2012-02-20 19:22:12 +01:00
|
|
|
if (!Indexer)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw agi::FileNotFoundError(ErrInfo.Buffer);
|
2009-07-16 16:48:47 +02:00
|
|
|
|
|
|
|
std::map<int,wxString> TrackList = GetTracksOfType(Indexer, FFMS_TYPE_VIDEO);
|
|
|
|
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
|
|
|
|
|
|
|
// initialize the track number to an invalid value so we can detect later on
|
|
|
|
// whether the user actually had to choose a track or not
|
|
|
|
int TrackNumber = -1;
|
|
|
|
if (TrackList.size() > 1) {
|
|
|
|
TrackNumber = AskForTrackSelection(TrackList, FFMS_TYPE_VIDEO);
|
|
|
|
// if it's still -1 here, user pressed cancel
|
|
|
|
if (TrackNumber == -1)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw agi::UserCancelException("video loading cancelled by user");
|
2009-07-16 16:48:47 +02:00
|
|
|
}
|
|
|
|
|
2008-09-03 19:03:20 +02:00
|
|
|
// generate a name for the cache file
|
2009-07-23 21:57:57 +02:00
|
|
|
wxString 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*)>
|
|
|
|
Index(FFMS_ReadIndex(CacheName.utf8_str(), &ErrInfo), FFMS_DestroyIndex);
|
|
|
|
|
2012-02-29 02:24:16 +01:00
|
|
|
if (Index && FFMS_IndexBelongsToFile(Index, FileNameShort.utf8_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) {
|
2012-02-28 02:22:29 +01:00
|
|
|
int TrackMask = FFMS_TRACKMASK_NONE;
|
|
|
|
if (OPT_GET("Provider/FFmpegSource/Index All Tracks")->GetBool() || OPT_GET("Video/Open Audio")->GetBool())
|
|
|
|
TrackMask = FFMS_TRACKMASK_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
|
2009-09-26 23:58:00 +02:00
|
|
|
wxFileName(CacheName).Touch();
|
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
|
|
|
}
|
|
|
|
|
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();
|
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;
|
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
|
|
|
|
2009-09-26 23:58:00 +02:00
|
|
|
VideoSource = FFMS_CreateVideoSource(FileNameShort.utf8_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
|
|
|
|
2012-05-11 16:38:44 +02:00
|
|
|
// Assuming TV for unspecified
|
|
|
|
wxString ColorRange = TempFrame->ColorRange == FFMS_CR_JPEG ? "PC" : "TV";
|
|
|
|
|
2012-03-07 05:25:46 +01:00
|
|
|
int CS = TempFrame->ColorSpace;
|
|
|
|
#if FFMS_VERSION >= ((2 << 24) | (17 << 16) | (1 << 8) | 0)
|
2012-04-17 01:55:21 +02:00
|
|
|
if (CS != FFMS_CS_RGB && CS != FFMS_CS_BT470BG && OPT_GET("Video/Force BT.601")->GetBool()) {
|
2012-06-15 15:09:15 +02:00
|
|
|
if (FFMS_SetInputFormatV(VideoSource, FFMS_CS_BT470BG, TempFrame->ColorRange, FFMS_GetPixFmt(""), &ErrInfo))
|
2012-03-07 05:25:46 +01:00
|
|
|
throw VideoOpenError(std::string("Failed to set input format: ") + ErrInfo.Buffer);
|
|
|
|
|
|
|
|
CS = FFMS_CS_BT470BG;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (CS) {
|
2012-05-11 16:38:44 +02:00
|
|
|
case FFMS_CS_RGB:
|
|
|
|
ColorSpace = "None";
|
|
|
|
break;
|
|
|
|
case FFMS_CS_BT709:
|
|
|
|
ColorSpace = wxString::Format("%s.709", ColorRange);
|
|
|
|
break;
|
|
|
|
case FFMS_CS_UNSPECIFIED:
|
|
|
|
ColorSpace = wxString::Format("%s.%s", ColorRange, Width > 1024 || Height >= 600 ? "709" : "601");
|
|
|
|
break;
|
|
|
|
case FFMS_CS_FCC:
|
|
|
|
ColorSpace = wxString::Format("%s.FCC", ColorRange);
|
|
|
|
break;
|
|
|
|
case FFMS_CS_BT470BG:
|
|
|
|
case FFMS_CS_SMPTE170M:
|
|
|
|
ColorSpace = wxString::Format("%s.601", ColorRange);
|
|
|
|
break;
|
|
|
|
case FFMS_CS_SMPTE240M:
|
2012-05-11 16:42:30 +02:00
|
|
|
ColorSpace = wxString::Format("%s.240M", ColorRange);
|
2012-05-11 16:38:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw VideoOpenError("Unknown video color space");
|
|
|
|
break;
|
2012-01-24 00:07:35 +01:00
|
|
|
}
|
|
|
|
|
2011-10-17 19:52:11 +02:00
|
|
|
const int TargetFormat[] = { FFMS_GetPixFmt("bgra"), -1 };
|
|
|
|
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
|
|
|
|
2009-09-26 23:58:00 +02:00
|
|
|
const FFMS_FrameInfo *CurFrameData;
|
2008-09-03 19:03:20 +02:00
|
|
|
|
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++) {
|
2009-05-23 16:18:51 +02:00
|
|
|
CurFrameData = FFMS_GetFrameInfo(FrameData, CurFrameNum);
|
2012-11-13 17:51:01 +01:00
|
|
|
if (CurFrameData == nullptr) {
|
2011-09-28 21:43:11 +02:00
|
|
|
throw VideoOpenError(STD_STR(wxString::Format("Couldn't get info about frame %d", CurFrameNum)));
|
2008-09-03 19:03:20 +02:00
|
|
|
}
|
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
|
|
|
|
|
|
|
FrameNumber = 0;
|
|
|
|
}
|
|
|
|
|
2010-12-31 22:03:11 +01:00
|
|
|
const AegiVideoFrame FFmpegSourceVideoProvider::GetFrame(int n) {
|
|
|
|
FrameNumber = mid(0, n, GetFrameCount() - 1);
|
2008-09-10 23:05:54 +02:00
|
|
|
|
|
|
|
// decode frame
|
2010-12-31 22:03:11 +01:00
|
|
|
const FFMS_Frame *SrcFrame = FFMS_GetFrame(VideoSource, FrameNumber, &ErrInfo);
|
2012-11-13 17:51:01 +01:00
|
|
|
if (SrcFrame == nullptr) {
|
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
|
|
|
}
|
|
|
|
|
2010-12-31 22:03:11 +01:00
|
|
|
CurFrame.SetTo(SrcFrame->Data[0], Width, Height, SrcFrame->Linesize[0]);
|
2009-10-24 04:07:56 +02:00
|
|
|
return CurFrame;
|
2008-09-03 19:03:20 +02:00
|
|
|
}
|
2012-01-24 00:07:35 +01:00
|
|
|
|
2011-12-22 22:25:49 +01:00
|
|
|
#endif /* WITH_FFMS2 */
|