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 "ffavisynth.h"
|
2009-05-23 16:18:51 +02:00
|
|
|
#include <cmath>
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
AvisynthVideoSource::AvisynthVideoSource(const char *SourceFile, int Track, FFIndex *Index, int FPSNum, int FPSDen, const char *PP, int Threads, int SeekMode, IScriptEnvironment* Env, char *ErrorMsg, unsigned MsgSize) {
|
2008-09-19 23:35:46 +02:00
|
|
|
memset(&VI, 0, sizeof(VI));
|
2008-10-01 20:13:12 +02:00
|
|
|
this->FPSNum = FPSNum;
|
|
|
|
this->FPSDen = FPSDen;
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
V = FFMS_CreateVideoSource(SourceFile, Track, Index, PP, Threads, SeekMode, ErrorMsg, MsgSize);
|
|
|
|
if (!V)
|
2008-09-01 23:16:13 +02:00
|
|
|
Env->ThrowError(ErrorMsg);
|
|
|
|
|
2009-05-23 16:18:51 +02:00
|
|
|
try {
|
|
|
|
InitOutputFormat(Env);
|
|
|
|
} catch (AvisynthError &) {
|
|
|
|
FFMS_DestroyVideoSource(V);
|
|
|
|
throw;
|
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-23 16:18:51 +02:00
|
|
|
const TVideoProperties *VP = FFMS_GetVideoProperties(V);
|
2008-10-01 20:13:12 +02:00
|
|
|
|
|
|
|
if (FPSNum > 0 && FPSDen > 0) {
|
|
|
|
VI.fps_denominator = FPSDen;
|
|
|
|
VI.fps_numerator = FPSNum;
|
2009-05-23 16:18:51 +02:00
|
|
|
VI.num_frames = static_cast<int>(ceil(((VP->LastTime - VP->FirstTime) * FPSNum) / FPSDen));
|
2008-10-01 20:13:12 +02:00
|
|
|
} else {
|
2009-05-23 16:18:51 +02:00
|
|
|
VI.fps_denominator = VP->FPSDenominator;
|
|
|
|
VI.fps_numerator = VP->FPSNumerator;
|
|
|
|
VI.num_frames = VP->NumFrames;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set AR variables
|
2009-05-23 16:18:51 +02:00
|
|
|
Env->SetVar("FFSAR_NUM", VP->SARNum);
|
|
|
|
Env->SetVar("FFSAR_DEN", VP->SARDen);
|
|
|
|
if (VP->SARNum > 0 && VP->SARDen > 0)
|
|
|
|
Env->SetVar("FFSAR", VP->SARNum / (double)VP->SARDen);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
// Set crop variables
|
2009-05-23 16:18:51 +02:00
|
|
|
Env->SetVar("FFCROP_LEFT", VP->CropLeft);
|
|
|
|
Env->SetVar("FFCROP_RIGHT", VP->CropRight);
|
|
|
|
Env->SetVar("FFCROP_TOP", VP->CropTop);
|
|
|
|
Env->SetVar("FFCROP_BOTTOM", VP->CropBottom);
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AvisynthVideoSource::~AvisynthVideoSource() {
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyVideoSource(V);
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2009-05-23 16:18:51 +02:00
|
|
|
void AvisynthVideoSource::InitOutputFormat(IScriptEnvironment *Env) {
|
|
|
|
const TVideoProperties *VP = FFMS_GetVideoProperties(V);
|
|
|
|
|
|
|
|
if (FFMS_SetOutputFormat(V, (1 << FFMS_GetPixFmt("yuvj420p")) |
|
|
|
|
(1 << FFMS_GetPixFmt("yuv420p")) | (1 << FFMS_GetPixFmt("yuyv422")) |
|
|
|
|
(1 << FFMS_GetPixFmt("rgb32")) | (1 << FFMS_GetPixFmt("bgr24")),
|
|
|
|
VP->Width, VP->Height, NULL, 0))
|
|
|
|
Env->ThrowError("FFVideoSource: No suitable output format found");
|
|
|
|
|
|
|
|
VP = FFMS_GetVideoProperties(V);
|
|
|
|
|
|
|
|
if (VP->VPixelFormat == FFMS_GetPixFmt("yuvj420p") || VP->VPixelFormat == FFMS_GetPixFmt("yuv420p"))
|
|
|
|
VI.pixel_type = VideoInfo::CS_I420;
|
|
|
|
else if (VP->VPixelFormat == FFMS_GetPixFmt("yuyv422"))
|
|
|
|
VI.pixel_type = VideoInfo::CS_YUY2;
|
|
|
|
else if (VP->VPixelFormat == FFMS_GetPixFmt("rgb32"))
|
|
|
|
VI.pixel_type = VideoInfo::CS_BGR32;
|
|
|
|
else if (VP->VPixelFormat == FFMS_GetPixFmt("bgr24"))
|
|
|
|
VI.pixel_type = VideoInfo::CS_BGR24;
|
|
|
|
else
|
|
|
|
Env->ThrowError("FFVideoSource: No suitable output format found");
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-23 16:18:51 +02:00
|
|
|
VI.image_type = VideoInfo::IT_TFF;
|
|
|
|
VI.width = VP->Width;
|
|
|
|
VI.height = VP->Height;
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-23 16:18:51 +02:00
|
|
|
// Crop to obey avisynth's even width/height requirements
|
2009-05-28 21:33:14 +02:00
|
|
|
if (VI.pixel_type == VideoInfo::CS_I420) {
|
2008-09-01 23:16:13 +02:00
|
|
|
VI.height -= VI.height & 1;
|
|
|
|
VI.width -= VI.width & 1;
|
|
|
|
}
|
|
|
|
|
2009-05-28 21:33:14 +02:00
|
|
|
if (VI.pixel_type == VideoInfo::CS_YUY2) {
|
2008-09-01 23:16:13 +02:00
|
|
|
VI.width -= VI.width & 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
PVideoFrame AvisynthVideoSource::OutputFrame(const TAVFrameLite *Frame, IScriptEnvironment *Env) {
|
2009-05-23 16:18:51 +02:00
|
|
|
TAVFrameLite *SrcPicture = const_cast<TAVFrameLite *>(Frame);
|
2008-09-01 23:16:13 +02:00
|
|
|
PVideoFrame Dst = Env->NewVideoFrame(VI);
|
|
|
|
|
2009-05-23 16:18:51 +02:00
|
|
|
if (VI.pixel_type == VideoInfo::CS_I420) {
|
|
|
|
Env->BitBlt(Dst->GetWritePtr(PLANAR_Y), Dst->GetPitch(PLANAR_Y), SrcPicture->Data[0], SrcPicture->Linesize[0], Dst->GetRowSize(PLANAR_Y), Dst->GetHeight(PLANAR_Y));
|
|
|
|
Env->BitBlt(Dst->GetWritePtr(PLANAR_U), Dst->GetPitch(PLANAR_U), SrcPicture->Data[1], SrcPicture->Linesize[1], Dst->GetRowSize(PLANAR_U), Dst->GetHeight(PLANAR_U));
|
|
|
|
Env->BitBlt(Dst->GetWritePtr(PLANAR_V), Dst->GetPitch(PLANAR_V), SrcPicture->Data[2], SrcPicture->Linesize[2], Dst->GetRowSize(PLANAR_V), Dst->GetHeight(PLANAR_V));
|
|
|
|
} else if (VI.IsRGB()) {
|
|
|
|
Env->BitBlt(Dst->GetWritePtr() + Dst->GetPitch() * (Dst->GetHeight() - 1), -Dst->GetPitch(), SrcPicture->Data[0], SrcPicture->Linesize[0], Dst->GetRowSize(), Dst->GetHeight());
|
2009-05-28 21:33:14 +02:00
|
|
|
} else { // YUY2
|
2009-05-23 16:18:51 +02:00
|
|
|
Env->BitBlt(Dst->GetWritePtr(), Dst->GetPitch(), SrcPicture->Data[0], SrcPicture->Linesize[0], Dst->GetRowSize(), Dst->GetHeight());
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
PVideoFrame AvisynthVideoSource::GetFrame(int n, IScriptEnvironment *Env) {
|
|
|
|
char ErrorMsg[1024];
|
|
|
|
unsigned MsgSize = sizeof(ErrorMsg);
|
2009-05-16 01:11:18 +02:00
|
|
|
const TAVFrameLite *Frame;
|
2008-10-01 20:13:12 +02:00
|
|
|
|
|
|
|
if (FPSNum > 0 && FPSDen > 0)
|
2009-05-18 00:02:43 +02:00
|
|
|
Frame = FFMS_GetFrameByTime(V, FFMS_GetVideoProperties(V)->FirstTime + (double)(n * (int64_t)FPSDen) / FPSNum, ErrorMsg, MsgSize);
|
2008-10-01 20:13:12 +02:00
|
|
|
else
|
2009-05-16 01:11:18 +02:00
|
|
|
Frame = FFMS_GetFrame(V, n, ErrorMsg, MsgSize);
|
2008-10-01 20:13:12 +02:00
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
if (Frame == NULL)
|
|
|
|
Env->ThrowError("FFVideoSource: %s", ErrorMsg);
|
|
|
|
|
|
|
|
Env->SetVar("FFPICT_TYPE", Frame->PictType);
|
|
|
|
return OutputFrame(Frame, Env);
|
|
|
|
}
|
2008-09-19 23:35:46 +02:00
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
AvisynthAudioSource::AvisynthAudioSource(const char *SourceFile, int Track, FFIndex *Index, IScriptEnvironment* Env, char *ErrorMsg, unsigned MsgSize) {
|
2008-09-19 23:35:46 +02:00
|
|
|
memset(&VI, 0, sizeof(VI));
|
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
A = FFMS_CreateAudioSource(SourceFile, Track, Index, ErrorMsg, MsgSize);
|
|
|
|
if (!A)
|
2008-09-19 23:35:46 +02:00
|
|
|
Env->ThrowError(ErrorMsg);
|
|
|
|
|
2009-05-18 00:02:43 +02:00
|
|
|
const TAudioProperties AP = *FFMS_GetAudioProperties(A);
|
2008-09-19 23:35:46 +02:00
|
|
|
|
|
|
|
VI.nchannels = AP.Channels;
|
|
|
|
VI.num_audio_samples = AP.NumSamples;
|
|
|
|
VI.audio_samples_per_second = AP.SampleRate;
|
|
|
|
|
|
|
|
if (AP.Float && AP.BitsPerSample == 32) {
|
|
|
|
VI.sample_type = SAMPLE_FLOAT;
|
|
|
|
} else {
|
|
|
|
switch (AP.BitsPerSample) {
|
|
|
|
case 8: VI.sample_type = SAMPLE_INT8; break;
|
|
|
|
case 16: VI.sample_type = SAMPLE_INT16; break;
|
|
|
|
case 24: VI.sample_type = SAMPLE_INT24; break;
|
|
|
|
case 32: VI.sample_type = SAMPLE_INT32; break;
|
2008-09-27 03:21:57 +02:00
|
|
|
default: Env->ThrowError("FFAudioSource: Bad audio format");
|
2008-09-19 23:35:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AvisynthAudioSource::~AvisynthAudioSource() {
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyAudioSource(A);
|
2008-09-19 23:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AvisynthAudioSource::GetAudio(void* Buf, __int64 Start, __int64 Count, IScriptEnvironment *Env) {
|
|
|
|
char ErrorMsg[1024];
|
|
|
|
unsigned MsgSize = sizeof(ErrorMsg);
|
2009-05-16 01:11:18 +02:00
|
|
|
if (FFMS_GetAudio(A, Buf, Start, Count, ErrorMsg, MsgSize))
|
2008-09-19 23:35:46 +02:00
|
|
|
Env->ThrowError(ErrorMsg);
|
2009-05-21 12:39:14 +02:00
|
|
|
}
|