2009-04-06 21:14:55 +02:00
|
|
|
// Copyright (c) 2007-2009 Fredrik Mellbin
|
2008-09-01 23:16:13 +02:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include "ffms.h"
|
|
|
|
#include "ffavisynth.h"
|
|
|
|
#include "ffswscale.h"
|
|
|
|
#include "ffpp.h"
|
2008-09-08 18:11:09 +02:00
|
|
|
|
|
|
|
int GetNumberOfLogicalCPUs() {
|
|
|
|
SYSTEM_INFO SI;
|
|
|
|
GetSystemInfo(&SI);
|
|
|
|
return SI.dwNumberOfProcessors;
|
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
AVSValue __cdecl CreateFFIndex(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
|
2008-09-02 01:20:28 +02:00
|
|
|
FFMS_Init();
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
char ErrorMsg[1024];
|
|
|
|
unsigned MsgSize = sizeof(ErrorMsg);
|
|
|
|
|
|
|
|
|
|
|
|
if (!Args[0].Defined())
|
|
|
|
Env->ThrowError("FFIndex: No source specified");
|
|
|
|
|
|
|
|
const char *Source = Args[0].AsString();
|
|
|
|
const char *CacheFile = Args[1].AsString("");
|
2008-09-24 00:00:22 +02:00
|
|
|
int IndexMask = Args[2].AsInt(0);
|
|
|
|
int DumpMask = Args[3].AsInt(0);
|
|
|
|
const char *AudioFile = Args[4].AsString("");
|
|
|
|
bool OverWrite = Args[5].AsBool(false);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
std::string DefaultCache(Source);
|
|
|
|
DefaultCache.append(".ffindex");
|
|
|
|
if (!strcmp(CacheFile, ""))
|
|
|
|
CacheFile = DefaultCache.c_str();
|
|
|
|
|
|
|
|
if (!strcmp(AudioFile, ""))
|
|
|
|
AudioFile = Source;
|
|
|
|
|
|
|
|
// Return values
|
|
|
|
// 0: Index already present
|
|
|
|
// 1: Index generated
|
|
|
|
// 2: Index forced to be overwritten
|
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
FFIndex *Index = NULL;
|
2008-09-04 00:26:27 +02:00
|
|
|
if (OverWrite || !(Index = FFMS_ReadIndex(CacheFile, ErrorMsg, MsgSize))) {
|
2008-09-24 16:11:45 +02:00
|
|
|
if (!(Index = FFMS_MakeIndex(Source, IndexMask, DumpMask, AudioFile, true, NULL, NULL, ErrorMsg, MsgSize)))
|
2008-09-01 23:16:13 +02:00
|
|
|
Env->ThrowError("FFIndex: %s", ErrorMsg);
|
2008-09-04 00:26:27 +02:00
|
|
|
if (FFMS_WriteIndex(CacheFile, Index, ErrorMsg, MsgSize)) {
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-01 23:16:13 +02:00
|
|
|
Env->ThrowError("FFIndex: %s", ErrorMsg);
|
2008-09-04 00:26:27 +02:00
|
|
|
}
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-01 23:16:13 +02:00
|
|
|
if (!OverWrite)
|
|
|
|
return AVSValue(1);
|
|
|
|
else
|
|
|
|
return AVSValue(2);
|
|
|
|
} else {
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-01 23:16:13 +02:00
|
|
|
return AVSValue(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AVSValue __cdecl CreateFFVideoSource(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
|
2008-09-02 01:20:28 +02:00
|
|
|
FFMS_Init();
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
char ErrorMsg[1024];
|
|
|
|
unsigned MsgSize = 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("");
|
2008-10-01 20:13:12 +02:00
|
|
|
int FPSNum = Args[4].AsInt(-1);
|
|
|
|
int FPSDen = Args[5].AsInt(1);
|
|
|
|
const char *PP = Args[6].AsString("");
|
|
|
|
int Threads = Args[7].AsInt(-1);
|
|
|
|
const char *Timecodes = Args[8].AsString("");
|
|
|
|
int SeekMode = Args[9].AsInt(1);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
if (Track <= -2)
|
|
|
|
Env->ThrowError("FFVideoSource: No video track selected");
|
|
|
|
|
|
|
|
if (SeekMode < -1 || SeekMode > 3)
|
|
|
|
Env->ThrowError("FFVideoSource: Invalid seekmode selected");
|
|
|
|
|
|
|
|
if (Threads <= 0)
|
|
|
|
Threads = GetNumberOfLogicalCPUs();
|
|
|
|
if (Threads < 1)
|
|
|
|
Env->ThrowError("FFVideoSource: Invalid thread count");
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
if (!_stricmp(Source, Timecodes))
|
|
|
|
Env->ThrowError("FFVideoSource: Timecodes will overwrite the source");
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
std::string DefaultCache(Source);
|
|
|
|
DefaultCache.append(".ffindex");
|
|
|
|
if (!strcmp(CacheFile, ""))
|
|
|
|
CacheFile = DefaultCache.c_str();
|
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
FFIndex *Index = NULL;
|
2008-10-01 21:07:59 +02:00
|
|
|
if (Cache)
|
|
|
|
Index = FFMS_ReadIndex(CacheFile, ErrorMsg, MsgSize);
|
|
|
|
if (!Index) {
|
|
|
|
if (!(Index = FFMS_MakeIndex(Source, 0, 0, NULL, true, NULL, NULL, ErrorMsg, MsgSize)))
|
|
|
|
Env->ThrowError("FFVideoSource: %s", ErrorMsg);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2008-10-01 21:07:59 +02:00
|
|
|
if (Cache)
|
|
|
|
if (FFMS_WriteIndex(CacheFile, Index, ErrorMsg, MsgSize)) {
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-10-01 21:07:59 +02:00
|
|
|
Env->ThrowError("FFVideoSource: %s", ErrorMsg);
|
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-27 21:05:10 +02:00
|
|
|
if (Track == -1)
|
|
|
|
Track = FFMS_GetFirstTrackOfType(Index, FFMS_TYPE_VIDEO, ErrorMsg, MsgSize);
|
|
|
|
if (Track < 0)
|
|
|
|
Env->ThrowError("FFVideoSource: No video track found");
|
|
|
|
|
|
|
|
if (strcmp(Timecodes, "")) {
|
2009-05-18 00:02:43 +02:00
|
|
|
if (FFMS_WriteTimecodes(FFMS_GetTrackFromIndex(Index, Track, ErrorMsg, MsgSize), Timecodes, ErrorMsg, MsgSize)) {
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-27 21:05:10 +02:00
|
|
|
Env->ThrowError("FFVideoSource: %s", ErrorMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
AvisynthVideoSource *Filter;
|
|
|
|
|
|
|
|
try {
|
2008-10-01 20:13:12 +02:00
|
|
|
Filter = new AvisynthVideoSource(Source, Track, Index, FPSNum, FPSDen, PP, Threads, SeekMode, Env, ErrorMsg, MsgSize);
|
2008-09-04 00:26:27 +02:00
|
|
|
} catch (...) {
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-04 00:26:27 +02:00
|
|
|
throw;
|
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-19 23:35:46 +02:00
|
|
|
return Filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVSValue __cdecl CreateFFAudioSource(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
|
|
|
|
FFMS_Init();
|
|
|
|
|
|
|
|
char ErrorMsg[1024];
|
|
|
|
unsigned MsgSize = 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("");
|
|
|
|
|
|
|
|
if (Track <= -2)
|
2008-09-23 16:58:39 +02:00
|
|
|
Env->ThrowError("FFAudioSource: No audio track selected");
|
2008-09-19 23:35:46 +02:00
|
|
|
|
|
|
|
std::string DefaultCache(Source);
|
|
|
|
DefaultCache.append(".ffindex");
|
|
|
|
if (!strcmp(CacheFile, ""))
|
|
|
|
CacheFile = DefaultCache.c_str();
|
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
FFIndex *Index = NULL;
|
2008-10-01 21:07:59 +02:00
|
|
|
if (Cache)
|
|
|
|
Index = FFMS_ReadIndex(CacheFile, ErrorMsg, MsgSize);
|
|
|
|
if (!Index) {
|
|
|
|
if (!(Index = FFMS_MakeIndex(Source, -1, 0, CacheFile, true, NULL, NULL, ErrorMsg, MsgSize)))
|
|
|
|
Env->ThrowError("FFAudioSource: %s", ErrorMsg);
|
2008-09-19 23:35:46 +02:00
|
|
|
|
2008-10-01 21:07:59 +02:00
|
|
|
if (Cache)
|
|
|
|
if (FFMS_WriteIndex(CacheFile, Index, ErrorMsg, MsgSize)) {
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-10-01 21:07:59 +02:00
|
|
|
Env->ThrowError("FFAudioSource: %s", ErrorMsg);
|
|
|
|
}
|
2008-09-19 23:35:46 +02:00
|
|
|
}
|
|
|
|
|
2008-09-27 21:05:10 +02:00
|
|
|
if (Track == -1)
|
|
|
|
Track = FFMS_GetFirstTrackOfType(Index, FFMS_TYPE_AUDIO, ErrorMsg, MsgSize);
|
|
|
|
if (Track < 0)
|
|
|
|
Env->ThrowError("FFAudioSource: No audio track found");
|
|
|
|
|
2008-09-19 23:35:46 +02:00
|
|
|
AvisynthAudioSource *Filter;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Filter = new AvisynthAudioSource(Source, Track, Index, Env, ErrorMsg, MsgSize);
|
|
|
|
} catch (...) {
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-19 23:35:46 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-01 23:16:13 +02:00
|
|
|
return Filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVSValue __cdecl CreateFFPP(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
|
|
|
|
return new FFPP(Args[0].AsClip(), Args[1].AsString(""), Env);
|
|
|
|
}
|
|
|
|
|
|
|
|
AVSValue __cdecl CreateSWScale(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
|
|
|
|
return new SWScale(Args[0].AsClip(), Args[1].AsInt(0), Args[2].AsInt(0), Args[3].AsString("BICUBIC"), Args[4].AsString(""), Env);
|
|
|
|
}
|
|
|
|
|
2009-05-12 22:50:40 +02:00
|
|
|
AVSValue __cdecl FFGetLogLevel(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
|
|
|
|
return FFMS_GetLogLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
AVSValue __cdecl FFSetLogLevel(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
|
|
|
|
FFMS_SetLogLevel(Args[0].AsInt());
|
|
|
|
return FFMS_GetLogLevel();
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit2(IScriptEnvironment* Env) {
|
2008-09-24 00:00:22 +02:00
|
|
|
Env->AddFunction("FFIndex", "[source]s[cachefile]s[indexmask]i[dumpmask]i[audiofile]s[overwrite]b", CreateFFIndex, 0);
|
2008-10-01 20:13:12 +02:00
|
|
|
Env->AddFunction("FFVideoSource", "[source]s[track]i[cache]b[cachefile]s[fpsnum]i[fpsden]i[pp]s[threads]i[timecodes]s[seekmode]i", CreateFFVideoSource, 0);
|
2008-09-19 23:35:46 +02:00
|
|
|
Env->AddFunction("FFAudioSource", "[source]s[track]i[cache]b[cachefile]s", CreateFFAudioSource, 0);
|
2008-09-01 23:16:13 +02:00
|
|
|
Env->AddFunction("FFPP", "c[pp]s", CreateFFPP, 0);
|
|
|
|
Env->AddFunction("SWScale", "c[width]i[height]i[resizer]s[colorspace]s", CreateSWScale, 0);
|
2009-05-12 22:50:40 +02:00
|
|
|
Env->AddFunction("FFGetLogLevel", "", FFGetLogLevel, 0);
|
|
|
|
Env->AddFunction("FFSetLogLevel", "i", FFSetLogLevel, 0);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
return "FFmpegSource - The Second Coming";
|
|
|
|
}
|