Code:
#include "windows.h"
#include "avisynth.h"
class RandomSource : public IClip {
private:
const VideoInfo vi;
bool parity;
public:
RandomSource(const VideoInfo& _vi,bool _parity) : vi(_vi), parity(_parity) {}
~RandomSource(){}
void __stdcall GetAudio(void* buf, __int64 start, __int64 count, IScriptEnvironment* env) {}
const VideoInfo& __stdcall GetVideoInfo() { return vi; }
bool __stdcall GetParity(int n) { return (vi.IsFieldBased() ? (n&1) : false) ^ parity; }
void __stdcall SetCacheHints(int cachehints,int frame_range) {};
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env);
};
PVideoFrame __stdcall RandomSource::GetFrame(int n, IScriptEnvironment* env) {
PVideoFrame frame = env->NewVideoFrame(vi);
BYTE* p = frame->GetWritePtr();
int size = frame->GetPitch() * frame->GetHeight();
for (int i=0; i<size; i+=4) {
*(p+i+0) = rand() & 0xFF; // B
*(p+i+1) = rand() & 0xFF; // G
*(p+i+2) = rand() & 0xFF; // R
*(p+i+3) = 0; // Alpha
}
return frame;
}
static AVSValue __cdecl Create_RandomSource(AVSValue args, void*, IScriptEnvironment* env) {
VideoInfo vi;
memset(&vi, 0, sizeof(VideoInfo));
vi.width =640;
vi.height =480;
vi.fps_numerator =24;
vi.fps_denominator =1;
vi.num_frames =10000;
vi.pixel_type =VideoInfo::CS_BGR32;
vi.audio_samples_per_second =0; // 0 means no audio
vi.sample_type =0; // as of 2.5
vi.num_audio_samples =0; // changed as of 2.5
vi.nchannels =0; // as of 2.5
vi.SetFieldBased(false);
bool parity=false;
return new RandomSource(vi, parity);
}
extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit2(IScriptEnvironment* env) {
env->AddFunction("RandomSource","",Create_RandomSource, 0);
return "`RandomSource' RandomSource plugin";
}
Memory usage constantly creeps upwards (in AvsMeter), am I doing wrong or is that normal.
(EDIT: Memory usage does max out so looks not to be buggy.)
If normal, is there a way of limiting memory usage within plugin, eg
SetCacheHints(CACHE_NOTHING, 0) or whatever, but as we are a source plugin, is it us that need to
do something for the SetCacheHints call ?
EDIT:
I would be quite happy with a single non-cached re-writeable frame.
Also, perhaps an int 'last' variable implemented in class, to track previously generated frame,
to avoid regeneration when called by another filter on the same frame number.
Want to use this as test clip generator to verify similar results when modding a filter
(in the immediate case for a ShowChannels update).
See Mediafire in sig below for dll + source.