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; }
/// @brief Load audio file /// @param filename /// void FFmpegSourceAudioProvider::LoadAudio(std::string filename) { // wxString FileNameShort = wxFileName(filename).GetShortPath(); FFMS_Indexer *Indexer = FFMS_CreateIndexer(filename.c_str(), &ErrInfo); if (Indexer == NULL) { throw agi::FileNotFoundError(ErrInfo.Buffer); } std::map<int,std::string> TrackList = GetTracksOfType(Indexer, FFMS_TYPE_AUDIO); if (TrackList.size() <= 0) throw AudioOpenError("no audio tracks found"); // 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_AUDIO); // if it's still -1 here, user pressed cancel if (TrackNumber == -1) throw agi::UserCancelException("audio loading cancelled by user"); } // generate a name for the cache file std::string CacheName = GetCacheFilename(filename); // try to read index FFMS_Index *Index = NULL; Index = FFMS_ReadIndex(CacheName.c_str(), &ErrInfo); bool IndexIsValid = false; if (Index != NULL) { if (FFMS_IndexBelongsToFile(Index, filename.c_str(), &ErrInfo)) { FFMS_DestroyIndex(Index); Index = NULL; } else IndexIsValid = true; } // index valid but track number still not set? if (IndexIsValid) { // track number not set? just grab the first track if (TrackNumber < 0) TrackNumber = FFMS_GetFirstTrackOfType(Index, FFMS_TYPE_AUDIO, &ErrInfo); if (TrackNumber < 0) { FFMS_DestroyIndex(Index); Index = NULL; throw AudioOpenError(std::string("Couldn't find any audio tracks: ") + ErrInfo.Buffer); } // index is valid and track number is now set, // but do we have indexing info for the desired audio track? FFMS_Track *TempTrackData = FFMS_GetTrackFromIndex(Index, TrackNumber); if (FFMS_GetNumFrames(TempTrackData) <= 0) { IndexIsValid = false; FFMS_DestroyIndex(Index); Index = NULL; } } // no valid index exists and the file only has one audio track, index it else if (TrackNumber < 0) TrackNumber = FFMS_TRACKMASK_ALL; // else: do nothing (keep track mask as it is) // moment of truth if (!IndexIsValid) { int TrackMask; // if (OPT_GET("Provider/FFmpegSource/Index All Tracks")->GetBool() || TrackNumber == FFMS_TRACKMASK_ALL) if (TrackNumber == FFMS_TRACKMASK_ALL) TrackMask = FFMS_TRACKMASK_ALL; else TrackMask = (1 << TrackNumber); try { Index = DoIndexing(Indexer, CacheName, TrackMask, GetErrorHandlingMode()); } catch (std::string const& err) { throw AudioOpenError(err); } // if tracknumber still isn't set we need to set it now if (TrackNumber == FFMS_TRACKMASK_ALL) TrackNumber = FFMS_GetFirstTrackOfType(Index, FFMS_TYPE_AUDIO, &ErrInfo); } // update access time of index file so it won't get cleaned away ///XXX: Add something to libaegisub to support this. // if (!wxFileName(CacheName).Touch()) { // warn user? // } #if FFMS_VERSION >= ((2 << 24) | (14 << 16) | (1 << 8) | 0) AudioSource = FFMS_CreateAudioSource(filename.c_str(), TrackNumber, Index, -1, &ErrInfo); #else AudioSource = FFMS_CreateAudioSource(filename.c_str(), TrackNumber, Index, &ErrInfo); #endif FFMS_DestroyIndex(Index); Index = NULL; if (!AudioSource) { throw AudioOpenError(std::string("Failed to open audio track: %s") + ErrInfo.Buffer); } const FFMS_AudioProperties AudioInfo = *FFMS_GetAudioProperties(AudioSource); channels = AudioInfo.Channels; sample_rate = AudioInfo.SampleRate; num_samples = AudioInfo.NumSamples; if (channels <= 0 || sample_rate <= 0 || num_samples <= 0) throw AudioOpenError("sanity check failed, consult your local psychiatrist"); // FIXME: use the actual sample format too? // why not just bits_per_sample/8? maybe there's some oddball format with half bytes out there somewhere... switch (AudioInfo.BitsPerSample) { case 8: bytes_per_sample = 1; break; case 16: bytes_per_sample = 2; break; case 24: bytes_per_sample = 3; break; case 32: bytes_per_sample = 4; break; default: throw AudioOpenError("unknown or unsupported sample format"); } }
// 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; }