/// @brief Constructor /// @param filename /// FFmpegSourceAudioProvider::FFmpegSourceAudioProvider(std::string filename) : AudioSource(NULL) , COMInited(false) { #ifdef WIN32 HRESULT res; res = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); if (SUCCEEDED(res)) COMInited = true; else if (res != RPC_E_CHANGED_MODE) throw AudioOpenError("COM initialization failure"); #endif // initialize ffmpegsource // FIXME: CPU detection? #if FFMS_VERSION >= ((2 << 24) | (14 << 16) | (0 << 8) | 0) FFMS_Init(0, 1); #else FFMS_Init(0); #endif ErrInfo.Buffer = FFMSErrMsg; ErrInfo.BufferSize = sizeof(FFMSErrMsg); ErrInfo.ErrorType = FFMS_ERROR_SUCCESS; ErrInfo.SubType = FFMS_ERROR_SUCCESS; // SetLogLevel(); try { LoadAudio(filename); } catch (...) { Close(); throw; } }
/// @brief Constructor /// @param filename The filename to open FFmpegSourceVideoProvider::FFmpegSourceVideoProvider(wxString filename) : VideoSource(NULL) , VideoInfo(NULL) , Width(-1) , Height(-1) , FrameNumber(-1) , COMInited(false) { #ifdef WIN32 HRESULT res = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); if (SUCCEEDED(res)) COMInited = true; else if (res != RPC_E_CHANGED_MODE) throw VideoOpenError("COM initialization failure"); #endif // initialize ffmpegsource // FIXME: CPU detection? #if FFMS_VERSION >= ((2 << 24) | (14 << 16) | (0 << 8) | 0) FFMS_Init(0, 1); #else FFMS_Init(0); #endif ErrInfo.Buffer = FFMSErrMsg; ErrInfo.BufferSize = sizeof(FFMSErrMsg); ErrInfo.ErrorType = FFMS_ERROR_SUCCESS; ErrInfo.SubType = FFMS_ERROR_SUCCESS; SetLogLevel(); // and here we go try { LoadVideo(filename); } catch (wxString const& err) { Close(); throw VideoOpenError(STD_STR(err)); } catch (...) { Close(); throw; } }
FFmpegSourceProvider::FFmpegSourceProvider() : COMInited(false, deinit_com) { #ifdef _WIN32 HRESULT res = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); if (SUCCEEDED(res)) COMInited = true; else if (res != RPC_E_CHANGED_MODE) throw "COM initialization failure"; #endif // initialize ffmpegsource FFMS_Init(0, 1); }
int wmain(int argc, const wchar_t *_argv[]) { std::vector<const char *> StringPtrs(argc); std::vector<std::string> StringStorage(argc); for (int i = 0; i < argc; i++) { StringStorage[i] = utf16_to_utf8(_argv[i]); StringPtrs[i] = StringStorage[i].c_str(); } const char **argv = StringPtrs.data(); #else int main(int argc, const char *argv[]) { #endif try { if (argc <= 1) { PrintUsage(); return 0; } ParseCMDLine(argc, argv); } catch (Error const& e) { std::cout << e.msg << std::endl; return 1; } FFMS_Init(0, 0); switch (Verbose) { case 0: FFMS_SetLogLevel(FFMS_LOG_QUIET); break; case 1: FFMS_SetLogLevel(FFMS_LOG_WARNING); break; case 2: FFMS_SetLogLevel(FFMS_LOG_INFO); break; case 3: FFMS_SetLogLevel(FFMS_LOG_VERBOSE); break; default: FFMS_SetLogLevel(FFMS_LOG_DEBUG); // if user used -v 4 or more times, he deserves the spam } try { DoIndexing(); } catch (Error const& e) { std::cout << e.msg << std::endl; FFMS_Deinit(); return 1; } FFMS_Deinit(); return 0; }
int main() { int argc; wchar_t **_argv; wchar_t **env; int si = 0; __wgetmainargs(&argc, &_argv, &env, _CRT_glob, &si); #else int wmain(int argc, wchar_t *_argv[]) { #endif char **argv = (char**)malloc(argc*sizeof(char*)); for (int i=0; i<argc; i++) { int len = WideCharToMultiByte(CP_UTF8, 0, _argv[i], -1, NULL, 0, NULL, NULL); if (!len) { std::cout << "Failed to translate commandline to Unicode" << std::endl; return 1; } char *temp = (char*)malloc(len*sizeof(char)); len = WideCharToMultiByte(CP_UTF8, 0, _argv[i], -1, temp, len, NULL, NULL); if (!len) { std::cout << "Failed to translate commandline to Unicode" << std::endl; return 1; } argv[i] = temp; } #else /* defined(_WIN32) && !defined(__MINGW32__) */ int main(int argc, char *argv[]) { #endif /* defined(_WIN32) && !defined(__MINGW32__) */ try { ParseCMDLine(argc, argv); } catch (const char *Error) { std::cout << std::endl << Error << std::endl; return 1; } catch (std::string Error) { std::cout << std::endl << Error << std::endl; return 1; } catch (...) { std::cout << std::endl << "Unknown error" << std::endl; return 1; } #ifdef _WIN32 if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) { std::cout << "COM initialization failure" << std::endl; return 1; } #endif /* _WIN32 */ FFMS_Init(0, 1); switch (Verbose) { case 0: FFMS_SetLogLevel(AV_LOG_QUIET); break; case 1: FFMS_SetLogLevel(AV_LOG_WARNING); break; case 2: FFMS_SetLogLevel(AV_LOG_INFO); break; case 3: FFMS_SetLogLevel(AV_LOG_VERBOSE); break; default: FFMS_SetLogLevel(AV_LOG_DEBUG); // if user used -v 4 or more times, he deserves the spam } try { DoIndexing(); } catch (const char *Error) { std::cout << Error << std::endl; if (Index) FFMS_DestroyIndex(Index); return 1; } catch (std::string Error) { std::cout << std::endl << Error << std::endl; if (Index) FFMS_DestroyIndex(Index); return 1; } catch (...) { std::cout << std::endl << "Unknown error" << std::endl; if (Index) FFMS_DestroyIndex(Index); return 1; } if (Index) FFMS_DestroyIndex(Index); #ifdef _WIN32 CoUninitialize(); #endif return 0; }
static AVSValue __cdecl CreateFFVideoSource(AVSValue Args, void* UserData, IScriptEnvironment* Env) { FFMS_Init((int)AvisynthToFFCPUFlags(Env->GetCPUFlags()), Args[15].AsBool(false)); char ErrorMsg[1024]; FFMS_ErrorInfo E; E.Buffer = ErrorMsg; E.BufferSize = sizeof(ErrorMsg); if (!Args[0].Defined()) Env->ThrowError("FFVideoSource: No source specified"); const char *Source = Args[0].AsString(); int Track = Args[1].AsInt(-1); bool Cache = Args[2].AsBool(true); const char *CacheFile = Args[3].AsString(""); int FPSNum = Args[4].AsInt(-1); int FPSDen = Args[5].AsInt(1); int Threads = Args[6].AsInt(-1); const char *Timecodes = Args[7].AsString(""); int SeekMode = Args[8].AsInt(1); int RFFMode = Args[9].AsInt(0); int Width = Args[10].AsInt(0); int Height = Args[11].AsInt(0); const char *Resizer = Args[12].AsString("BICUBIC"); const char *ColorSpace = Args[13].AsString(""); const char *VarPrefix = Args[15].AsString(""); if (FPSDen < 1) Env->ThrowError("FFVideoSource: FPS denominator needs to be 1 or higher"); if (Track <= -2) Env->ThrowError("FFVideoSource: No video track selected"); if (SeekMode < -1 || SeekMode > 3) Env->ThrowError("FFVideoSource: Invalid seekmode selected"); if (RFFMode < 0 || RFFMode > 2) Env->ThrowError("FFVideoSource: Invalid RFF mode selected"); if (RFFMode > 0 && FPSNum > 0) Env->ThrowError("FFVideoSource: RFF modes may not be combined with CFR conversion"); if (!_stricmp(Source, Timecodes)) Env->ThrowError("FFVideoSource: Timecodes will overwrite the source"); FFMS_Index *Index = NULL; std::string DefaultCache; if (Cache) { if (*CacheFile) { if (!_stricmp(Source, CacheFile)) Env->ThrowError("FFVideoSource: Cache will overwrite the source"); Index = FFMS_ReadIndex(CacheFile, &E); } else { DefaultCache = Source; DefaultCache += ".ffindex"; CacheFile = DefaultCache.c_str(); Index = FFMS_ReadIndex(CacheFile, &E); // Reindex if the index doesn't match the file and its name wasn't // explicitly given if (Index && FFMS_IndexBelongsToFile(Index, Source, 0) != FFMS_ERROR_SUCCESS) { FFMS_DestroyIndex(Index); Index = 0; } } } if (!Index) { if (!(Index = FFMS_MakeIndex(Source, 0, 0, NULL, NULL, true, NULL, NULL, &E))) Env->ThrowError("FFVideoSource: %s", E.Buffer); if (Cache) if (FFMS_WriteIndex(CacheFile, Index, &E)) { FFMS_DestroyIndex(Index); Env->ThrowError("FFVideoSource: %s", E.Buffer); } } if (Track == -1) Track = FFMS_GetFirstIndexedTrackOfType(Index, FFMS_TYPE_VIDEO, &E); if (Track < 0) Env->ThrowError("FFVideoSource: No video track found"); if (strcmp(Timecodes, "")) { if (FFMS_WriteTimecodes(FFMS_GetTrackFromIndex(Index, Track), Timecodes, &E)) { FFMS_DestroyIndex(Index); Env->ThrowError("FFVideoSource: %s", E.Buffer); } } AvisynthVideoSource *Filter; try { Filter = new AvisynthVideoSource(Source, Track, Index, FPSNum, FPSDen, Threads, SeekMode, RFFMode, Width, Height, Resizer, ColorSpace, VarPrefix, Env); } catch (...) { FFMS_DestroyIndex(Index); throw; } FFMS_DestroyIndex(Index); return Filter; }
static AVSValue __cdecl CreateFFIndex(AVSValue Args, void* UserData, IScriptEnvironment* Env) { FFMS_Init((int)AvisynthToFFCPUFlags(Env->GetCPUFlags()), Args[7].AsBool(false)); char ErrorMsg[1024]; FFMS_ErrorInfo E; E.Buffer = ErrorMsg; E.BufferSize = sizeof(ErrorMsg); if (!Args[0].Defined()) Env->ThrowError("FFIndex: No source specified"); const char *Source = Args[0].AsString(); const char *CacheFile = Args[1].AsString(""); int IndexMask = Args[2].AsInt(-1); int DumpMask = Args[3].AsInt(0); const char *AudioFile = Args[4].AsString("%sourcefile%.%trackzn%.w64"); int ErrorHandling = Args[5].AsInt(FFMS_IEH_IGNORE); bool OverWrite = Args[6].AsBool(false); const char *DemuxerStr = Args[8].AsString("default"); std::string DefaultCache(Source); DefaultCache.append(".ffindex"); if (!strcmp(CacheFile, "")) CacheFile = DefaultCache.c_str(); if (!strcmp(AudioFile, "")) Env->ThrowError("FFIndex: Specifying an empty audio filename is not allowed"); int Demuxer; if (!strcmp(DemuxerStr, "default")) Demuxer = FFMS_SOURCE_DEFAULT; else if (!strcmp(DemuxerStr, "lavf")) Demuxer = FFMS_SOURCE_LAVF; else if (!strcmp(DemuxerStr, "matroska")) Demuxer = FFMS_SOURCE_MATROSKA; else if (!strcmp(DemuxerStr, "haalimpeg")) Demuxer = FFMS_SOURCE_HAALIMPEG; else if (!strcmp(DemuxerStr, "haaliogg")) Demuxer = FFMS_SOURCE_HAALIOGG; else Env->ThrowError("FFIndex: Invalid demuxer requested"); FFMS_Index *Index = FFMS_ReadIndex(CacheFile, &E); if (OverWrite || !Index || (Index && FFMS_IndexBelongsToFile(Index, Source, 0) != FFMS_ERROR_SUCCESS)) { FFMS_Indexer *Indexer = FFMS_CreateIndexerWithDemuxer(Source, Demuxer, &E); if (!Indexer) Env->ThrowError("FFIndex: %s", E.Buffer); if (!(Index = FFMS_DoIndexing(Indexer, IndexMask, DumpMask, FFMS_DefaultAudioFilename, (void *)AudioFile, ErrorHandling, NULL, NULL, &E))) Env->ThrowError("FFIndex: %s", E.Buffer); if (FFMS_WriteIndex(CacheFile, Index, &E)) { FFMS_DestroyIndex(Index); Env->ThrowError("FFIndex: %s", E.Buffer); } FFMS_DestroyIndex(Index); if (!OverWrite) return AVSValue(1); else return AVSValue(2); } else { FFMS_DestroyIndex(Index); return AVSValue(0); } }
static AVSValue __cdecl CreateFFAudioSource(AVSValue Args, void* UserData, IScriptEnvironment* Env) { FFMS_Init((int)AvisynthToFFCPUFlags(Env->GetCPUFlags()), Args[5].AsBool(false)); char ErrorMsg[1024]; FFMS_ErrorInfo E; E.Buffer = ErrorMsg; E.BufferSize = sizeof(ErrorMsg); if (!Args[0].Defined()) Env->ThrowError("FFAudioSource: No source specified"); const char *Source = Args[0].AsString(); int Track = Args[1].AsInt(-1); bool Cache = Args[2].AsBool(true); const char *CacheFile = Args[3].AsString(""); int AdjustDelay = Args[4].AsInt(-1); const char *VarPrefix = Args[6].AsString(""); if (Track <= -2) Env->ThrowError("FFAudioSource: No audio track selected"); FFMS_Index *Index = NULL; std::string DefaultCache; if (Cache) { if (*CacheFile) { if (!_stricmp(Source, CacheFile)) Env->ThrowError("FFAudioSource: Cache will overwrite the source"); Index = FFMS_ReadIndex(CacheFile, &E); } else { DefaultCache = Source; DefaultCache += ".ffindex"; CacheFile = DefaultCache.c_str(); Index = FFMS_ReadIndex(CacheFile, &E); // Reindex if the index doesn't match the file and its name wasn't // explicitly given if (Index && FFMS_IndexBelongsToFile(Index, Source, 0) != FFMS_ERROR_SUCCESS) { FFMS_DestroyIndex(Index); Index = 0; } } } // Index needs to be remade if it is an unindexed audio track if (Index && Track >= 0 && Track < FFMS_GetNumTracks(Index) && FFMS_GetTrackType(FFMS_GetTrackFromIndex(Index, Track)) == FFMS_TYPE_AUDIO && FFMS_GetNumFrames(FFMS_GetTrackFromIndex(Index, Track)) == 0) { FFMS_DestroyIndex(Index); Index = NULL; } // More complicated for finding a default track, reindex the file if at least one audio track exists if (Index && FFMS_GetFirstTrackOfType(Index, FFMS_TYPE_AUDIO, &E) >= 0 && FFMS_GetFirstIndexedTrackOfType(Index, FFMS_TYPE_AUDIO, &E) < 0) { for (int i = 0; i < FFMS_GetNumTracks(Index); i++) { if (FFMS_GetTrackType(FFMS_GetTrackFromIndex(Index, i)) == FFMS_TYPE_AUDIO) { FFMS_DestroyIndex(Index); Index = NULL; break; } } } if (!Index) { if (!(Index = FFMS_MakeIndex(Source, -1, 0, NULL, NULL, true, NULL, NULL, &E))) Env->ThrowError("FFAudioSource: %s", E.Buffer); if (Cache) if (FFMS_WriteIndex(CacheFile, Index, &E)) { FFMS_DestroyIndex(Index); Env->ThrowError("FFAudioSource: %s", E.Buffer); } } if (Track == -1) Track = FFMS_GetFirstIndexedTrackOfType(Index, FFMS_TYPE_AUDIO, &E); if (Track < 0) Env->ThrowError("FFAudioSource: No audio track found"); if (AdjustDelay < -3) Env->ThrowError("FFAudioSource: Invalid delay adjustment specified"); if (AdjustDelay >= FFMS_GetNumTracks(Index)) Env->ThrowError("FFAudioSource: Invalid track to calculate delay from specified"); AvisynthAudioSource *Filter; try { Filter = new AvisynthAudioSource(Source, Track, Index, AdjustDelay, VarPrefix, Env); } catch (...) { FFMS_DestroyIndex(Index); throw; } FFMS_DestroyIndex(Index); return Filter; }
// Constructor and destructor c_media_file_ffms::c_media_file_ffms(boost::filesystem::path path) : // Path m_path(path), // File m_source(nullptr), // Info m_frames(0), m_rate(0), m_aspect(1), m_width(0), m_height(0) { // Debug //std::cout << boost::format("FFMS: Opening file! path = %1%") % path << std::endl; // FFMS error m_fferr.Buffer = m_ffmsg.data(); m_fferr.BufferSize = m_ffmsg.size(); m_fferr.ErrorType = FFMS_ERROR_SUCCESS; m_fferr.SubType = FFMS_ERROR_SUCCESS; // Library FFMS_Init(0, 1); // Index FFMS_Index* index = nullptr; // Cached index auto path_index = m_path; path_index.replace_extension(".ffindex"); if (boost::filesystem::is_regular_file(path_index)) { // Read index index = FFMS_ReadIndex(path_index.c_str(), &m_fferr); if (index) { // Check validity int result = FFMS_IndexBelongsToFile(index, m_path.c_str(), &m_fferr); if (result) { // Invalid index FFMS_DestroyIndex(index); index = nullptr; // Delete index file too boost::filesystem::remove(path_index); } } } // Create index if (!index) { // Indexer FFMS_Indexer* indexer = FFMS_CreateIndexer(m_path.c_str(), &m_fferr); if (!indexer) throw c_exception("FFMS: Could not create indexer!", { throw_format("path", m_path) }); //index = FFMS_DoIndexing2(indexer, FFMS_IEH_ABORT, &m_fferr); index = FFMS_DoIndexing(indexer, 0, 0, nullptr, nullptr, FFMS_IEH_ABORT, nullptr, nullptr, &m_fferr); if (!index) throw c_exception("FFMS: Failed to index media!", { throw_format("path", m_path) }); // Write index to file FFMS_WriteIndex(path_index.c_str(), index, &m_fferr); } // Track int track_id = FFMS_GetFirstTrackOfType(index, FFMS_TYPE_VIDEO, &m_fferr); if (track_id < 0) { FFMS_DestroyIndex(index); throw c_exception("FFMS: Failed to find any video tracks!", { throw_format("path", m_path) }); } // Source m_source = FFMS_CreateVideoSource(m_path.c_str(), track_id, index, 1, FFMS_SEEK_NORMAL, &m_fferr); if (!m_source) { FFMS_DestroyIndex(index); throw c_exception("FFMS: Failed to create video source!", { throw_format("path", m_path) }); } // Destroy index FFMS_DestroyIndex(index); index = nullptr; // Video properties const FFMS_VideoProperties* props = FFMS_GetVideoProperties(m_source); m_frames = props->NumFrames; if (props->FirstTime < props->LastTime && props->LastTime > 0.0) m_rate = (props->LastTime - props->FirstTime) / static_cast<double>(m_frames); else if (props->FPSNumerator != 0) m_rate = static_cast<double>(props->FPSNumerator) / static_cast<double>(props->FPSDenominator); if (props->SARNum != 0) m_aspect = static_cast<double>(props->SARNum) / static_cast<double>(props->SARDen); // First frame const FFMS_Frame* frame = FFMS_GetFrame(m_source, 0, &m_fferr); if (!frame) throw c_exception("FFMS: Failed to get first video frame!", { throw_format("path", m_path) }); if (frame->ScaledWidth > 0) m_width = frame->ScaledWidth; else m_width = frame->EncodedWidth; if (frame->ScaledHeight > 0) m_height = frame->ScaledHeight; else m_height = frame->EncodedHeight; // Conversion int pixfmts[2]; pixfmts[0] = FFMS_GetPixFmt("rgb24"); pixfmts[1] = -1; if (FFMS_SetOutputFormatV2(m_source, pixfmts, frame->EncodedWidth, frame->EncodedHeight, FFMS_RESIZER_POINT, &m_fferr)) throw c_exception("FFMS: Failed to set output format!", { throw_format("path", m_path) }); // Info std::cout << boost::format("FFMS: width = %d, height = %d, frames = %d, rate = %.3f, aspect = %.3f") % m_width % m_height % m_frames % m_rate % m_aspect << std::endl; }