# A2DSource library: Universal AviSynth Source Functions
# Written by MrC (November 2008)
# Updated by manolito (December 2013)
# Updated by MrC (January 2015)
# Updated by MrC (July 2015)
# Updated by MrC (October 2016)
# Following AviSynth plugins are required in the same folder:
#   ffms2.dll		more info @ https://github.com/ffms/ffms2
#   GetSystemEnv.dll	more info @ http://avisynth.org/stickboy
#   NicAudio.dll        more info @ http://nicaudio.codeplex.com


global OPT_AllowFloatAudio = True

LoadCPlugin("ffms2.dll")
LoadPlugin("GetSystemEnv.dll")
LoadPlugin("NicAudio.dll")


Function A2DVideoSource(string VideoSource, string "CacheFolder", bool "DebugVideo", bool "DebugTxt", bool "VFR", int "FrameRateNum", int "FrameRateDen", float "FrameRate")
{
CacheFolder = default(CacheFolder, GetSystemEnv("temp"))
DebugVideo = default(DebugVideo, false)
DebugTxt = default(DebugTxt, true)
# for DSS assuming VFR is safer
VFR = default(VFR, true)
FrameRateNum = default(FrameRateNum, 25)
FrameRateDen = default(FrameRateDen, 1)
FrameRate = default(FrameRate, Float(FrameRateNum) / Float(FrameRateDen))
Try {
SourceStr = "A2DVideoSource: AVISource"
Video = AVISource(VideoSource, audio = false)
Video = (Video.Width % 4 == 0) ? Video : Error
}
Catch(Err_Msg) {
	Try {
	SourceStr = "A2DVideoSource: DirectShowSource"
	Video = DirectShowSource(VideoSource, fps = FrameRate, convertfps = VFR, audio = false)
	Video = (Video.Width % 4 == 0) ? Video : Error
	}
	Catch(Err_Msg) {
		Try {
		SourceStr = "A2DVideoSource: FFmpegSource2 (cache off)"
		Video = FFVideoSource(VideoSource, track = -1, cache = false, fpsnum = FrameRateNum, fpsden = FrameRateDen, seekmode = 0)
		}
		Catch(Err_Msg) {
			SourceStr = "A2DVideoSource: FFmpegSource2 (cache on)"
			Video = FFVideoSource(VideoSource, track = -1, cachefile = CacheFolder + "\A2DFFV_" + String(Rand()) + ".cache", fpsnum = FrameRateNum, fpsden = FrameRateDen, seekmode = 0)
		}
	}
}
Video = (DebugVideo == true) ? Subtitle(Video.Info(), SourceStr, font = "courier new", size = 16, align = 1) : Video
Video = (DebugTxt == true) ? Video.WriteFileStart(CacheFolder + "\A2DVideoInfo.cache", """ "FileName: " """, "VideoSource", true) : Video
Video = (DebugTxt == true) ? Video.WriteFileStart(CacheFolder + "\A2DVideoInfo.cache", "SourceStr", "Chr(13)", true) : Video
#Timing = Time("%#c")
#Video = (DebugTxt == true) ? Video.WriteFileStart(CacheFolder + "\A2DVideoInfo.cache", """ "Time: " """, "Timing", "Chr(13)", true) : Video
	
Return Video 
}


Function A2DAudioSource(string AudioSource, string "CacheFolder", bool "DebugTxt", bool "AudioOnly")
{
CacheFolder = default(CacheFolder, GetSystemEnv("temp"))
DebugTxt = default(DebugTxt, true)
AudioOnly = default(AudioOnly, false)
AudioOnly == true ? Eval("""
	Try {
	SourceStr = "A2DAudioSource: DirectShowSource"
	Audio = DirectShowSource(AudioSource, video = false)
	}
	Catch(Err_Msg) {
		Try {
		SourceStr = "A2DAudioSource: WAVSource"
		Audio = WAVSource(AudioSource)
		}
		Catch(Err_Msg) {
			Try {
			SourceStr = "A2DAudioSource: NicDTSSource"
			Audio = NicDTSSource(AudioSource)
			}
			Catch(Err_Msg) {
				Try {
				SourceStr = "A2DAudioSource: NicAC3Source"
				Audio = NicAC3Source(AudioSource)
				}
				Catch(Err_Msg) {
					SourceStr = "A2DAudioSource: NicMPG123Source"
					Audio = NicMPG123Source(AudioSource)
				}
			}
		}
	}
""") : Eval("""
	Try {
	SourceStr = "A2DAudioSource: DirectShowSource"
	Audio = DirectShowSource(AudioSource, video = false)
	}
	Catch(Err_Msg) {
		Try {
		SourceStr = "A2DAudioSource: FFmpegSource2 (cache off)"
		Audio = FFAudioSource(AudioSource, track = -1, cache = false)
		}
		Catch(Err_Msg) {
			SourceStr = "A2DAudioSource: FFmpegSource2 (cache on)"
			Audio = FFAudioSource(AudioSource, track = -1, cachefile = CacheFolder + "\A2DFFA_" + String(Rand()) + ".cache")
		}
	}
""")
Audio = (DebugTxt == true) ? Audio.WriteFileStart(CacheFolder + "\A2DAudioInfo.cache", """ "FileName: " """, "AudioSource", true) : Audio
Audio = (DebugTxt == true) ? Audio.WriteFileStart(CacheFolder + "\A2DAudioInfo.cache", "SourceStr", "Chr(13)", true) : Audio

Return Audio
}

